1 /* 2 * Copyright 2012, Adrien Destugues, pulkomandy@gmail.com 3 * Distributed under the terms of the MIT licence. 4 */ 5 6 7 #include "SerialWindow.h" 8 9 #include <GroupLayout.h> 10 #include <Menu.h> 11 #include <MenuBar.h> 12 #include <MenuItem.h> 13 #include <SerialPort.h> 14 15 #include "SerialApp.h" 16 #include "TermView.h" 17 18 19 SerialWindow::SerialWindow() 20 : 21 BWindow(BRect(100, 100, 400, 400), SerialWindow::kWindowTitle, 22 B_DOCUMENT_WINDOW, B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS) 23 { 24 SetLayout(new BGroupLayout(B_VERTICAL, 0.0f)); 25 26 BMenuBar* menuBar = new BMenuBar("menuBar"); 27 fTermView = new TermView(); 28 29 AddChild(menuBar); 30 AddChild(fTermView); 31 32 BMenu* connectionMenu = new BMenu("Connections"); 33 BMenu* editMenu = new BMenu("Edit"); 34 BMenu* settingsMenu = new BMenu("Settings"); 35 36 menuBar->AddItem(connectionMenu); 37 menuBar->AddItem(editMenu); 38 menuBar->AddItem(settingsMenu); 39 40 // TODO messages 41 BMenu* connect = new BMenu("Connect"); 42 connectionMenu->AddItem(connect); 43 44 BSerialPort serialPort; 45 int deviceCount = serialPort.CountDevices(); 46 47 for(int i = 0; i < deviceCount; i++) 48 { 49 char buffer[256]; 50 serialPort.GetDeviceName(i, buffer, 256); 51 52 BMessage* message = new BMessage(kMsgOpenPort); 53 message->AddString("port name", buffer); 54 BMenuItem* portItem = new BMenuItem(buffer, message); 55 56 connect->AddItem(portItem); 57 58 portItem->SetTarget(be_app); 59 } 60 61 #if SUPPORTS_MODEM 62 BMenuItem* connectModem = new BMenuItem( 63 "Connect via modem" B_UTF8_ELLIPSIS, NULL, 'M', 0); 64 connectionMenu->AddItem(connectModem); 65 #endif 66 BMenuItem* Disconnect = new BMenuItem("Disconnect", NULL, 67 'Z', B_OPTION_KEY); 68 connectionMenu->AddItem(Disconnect); 69 70 // TODO edit menu - what's in it ? 71 72 // Configuring all this by menus may be a bit unhandy. Make a setting 73 // window instead ? 74 BMenu* parity = new BMenu("Parity"); 75 settingsMenu->AddItem(parity); 76 BMenu* dataBits = new BMenu("Data bits"); 77 settingsMenu->AddItem(dataBits); 78 BMenu* stopBits = new BMenu("Stop bits"); 79 settingsMenu->AddItem(stopBits); 80 BMenu* baudRate = new BMenu("Baud rate"); 81 settingsMenu->AddItem(baudRate); 82 BMenu* flowControl = new BMenu("Flow control"); 83 settingsMenu->AddItem(flowControl); 84 85 BMenuItem* parityNone = new BMenuItem("None", NULL); 86 parity->AddItem(parityNone); 87 BMenuItem* parityOdd = new BMenuItem("Odd", NULL); 88 parity->AddItem(parityOdd); 89 BMenuItem* parityEven = new BMenuItem("Even", NULL); 90 parity->AddItem(parityEven); 91 92 BMenuItem* data7 = new BMenuItem("7", NULL); 93 dataBits->AddItem(data7); 94 BMenuItem* data8 = new BMenuItem("8", NULL); 95 dataBits->AddItem(data8); 96 97 BMenuItem* stop1 = new BMenuItem("1", NULL); 98 stopBits->AddItem(stop1); 99 BMenuItem* stop2 = new BMenuItem("2", NULL); 100 stopBits->AddItem(stop2); 101 102 static const char* baudrates[] = { "50", "75", "110", "134", "150", "200", 103 "300", "600", "1200", "1800", "2400", "4800", "9600", "19200", "31250", 104 "38400", "57600", "115200", "230400" 105 }; 106 107 // Loop backwards to add fastest rates at top of menu 108 for (int i = sizeof(baudrates) / sizeof(char*); --i >= 0;) 109 { 110 BMenuItem* item = new BMenuItem(baudrates[i], NULL); 111 baudRate->AddItem(item); 112 } 113 114 BMenuItem* rtsCts = new BMenuItem("RTS/CTS", NULL); 115 flowControl->AddItem(rtsCts); 116 BMenuItem* noFlow = new BMenuItem("None", NULL); 117 flowControl->AddItem(noFlow); 118 119 CenterOnScreen(); 120 } 121 122 123 void SerialWindow::MessageReceived(BMessage* message) 124 { 125 switch(message->what) 126 { 127 case kMsgDataRead: 128 { 129 const char* bytes; 130 ssize_t length; 131 message->FindData("data", B_RAW_TYPE, (const void**)&bytes, &length); 132 fTermView->PushBytes(bytes, length); 133 break; 134 } 135 default: 136 BWindow::MessageReceived(message); 137 } 138 } 139 140 141 const char* SerialWindow::kWindowTitle = "SerialConnect"; 142