xref: /haiku/src/apps/serialconnect/SerialWindow.cpp (revision 3a6bc1cf655f552194bae298fadfdde896dad690)
1dae0a4e0SAdrien Destugues - PulkoMandy /*
21eb38608SAdrien Destugues  * Copyright 2012-2019, Adrien Destugues, pulkomandy@pulkomandy.tk
3dae0a4e0SAdrien Destugues - PulkoMandy  * Distributed under the terms of the MIT licence.
4dae0a4e0SAdrien Destugues - PulkoMandy  */
5dae0a4e0SAdrien Destugues - PulkoMandy 
6dae0a4e0SAdrien Destugues - PulkoMandy 
7dae0a4e0SAdrien Destugues - PulkoMandy #include "SerialWindow.h"
8dae0a4e0SAdrien Destugues - PulkoMandy 
904434656SAdrien Destugues - PulkoMandy #include <stdio.h>
1004434656SAdrien Destugues - PulkoMandy 
112e6ca31cSMurai Takashi #include <Catalog.h>
123cfd6956SRene Gollent #include <FilePanel.h>
13dae0a4e0SAdrien Destugues - PulkoMandy #include <GroupLayout.h>
14dae0a4e0SAdrien Destugues - PulkoMandy #include <Menu.h>
15dae0a4e0SAdrien Destugues - PulkoMandy #include <MenuBar.h>
16dae0a4e0SAdrien Destugues - PulkoMandy #include <MenuItem.h>
17b59ed154SAdrien Destugues #include <ScrollView.h>
18dae0a4e0SAdrien Destugues - PulkoMandy #include <SerialPort.h>
19ce058fa0SAdrien Destugues #include <StatusBar.h>
20dae0a4e0SAdrien Destugues - PulkoMandy 
21f441fd03SAdrien Destugues - PulkoMandy #include "SerialApp.h"
22dae0a4e0SAdrien Destugues - PulkoMandy #include "TermView.h"
23dae0a4e0SAdrien Destugues - PulkoMandy 
24dae0a4e0SAdrien Destugues - PulkoMandy 
252e6ca31cSMurai Takashi #define B_TRANSLATION_CONTEXT "SerialWindow"
262e6ca31cSMurai Takashi 
272e6ca31cSMurai Takashi 
28e0bf43c1SAlexander von Gluck IV const int SerialWindow::kBaudrates[] = { 50, 75, 110, 134, 150, 200, 300, 600,
29e0bf43c1SAlexander von Gluck IV 	1200, 1800, 2400, 4800, 9600, 19200, 31250, 38400, 57600, 115200, 230400
30ccc6b9beSAdrien Destugues };
31ccc6b9beSAdrien Destugues 
32ccc6b9beSAdrien Destugues 
33e2367275SAdrien Destugues // The values for these constants are not in the expected order, so we have to
34e2367275SAdrien Destugues // rely on this lookup table if we want to keep the menu items sorted.
35e2367275SAdrien Destugues const int SerialWindow::kBaudrateConstants[] = { B_50_BPS, B_75_BPS, B_110_BPS,
36e2367275SAdrien Destugues 	B_134_BPS, B_150_BPS, B_200_BPS, B_300_BPS, B_600_BPS, B_1200_BPS,
37e2367275SAdrien Destugues 	B_1800_BPS, B_2400_BPS, B_4800_BPS, B_9600_BPS, B_19200_BPS, B_31250_BPS,
38e2367275SAdrien Destugues 	B_38400_BPS, B_57600_BPS, B_115200_BPS, B_230400_BPS
39e2367275SAdrien Destugues };
40e2367275SAdrien Destugues 
41e2367275SAdrien Destugues 
422e6ca31cSMurai Takashi const char* SerialWindow::kWindowTitle =
432e6ca31cSMurai Takashi 	B_TRANSLATE_MARK_SYSTEM_NAME("SerialConnect");
44ccc6b9beSAdrien Destugues 
45ccc6b9beSAdrien Destugues 
SerialWindow()46dae0a4e0SAdrien Destugues - PulkoMandy SerialWindow::SerialWindow()
472e6ca31cSMurai Takashi 	: BWindow(BRect(100, 100, 400, 400),
482e6ca31cSMurai Takashi 		B_TRANSLATE_NOCOLLECT_SYSTEM_NAME(SerialWindow::kWindowTitle),
49f441fd03SAdrien Destugues - PulkoMandy 		B_DOCUMENT_WINDOW, B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS)
50469e6cd2SAdrien Destugues - PulkoMandy 	, fLogFilePanel(NULL)
5183776950SAdrien Destugues 	, fSendFilePanel(NULL)
52dae0a4e0SAdrien Destugues - PulkoMandy {
53b59ed154SAdrien Destugues 	BMenuBar* menuBar = new BMenuBar(Bounds(), "menuBar");
54b59ed154SAdrien Destugues 	menuBar->ResizeToPreferred();
55dae0a4e0SAdrien Destugues - PulkoMandy 
56b59ed154SAdrien Destugues 	BRect r = Bounds();
57b59ed154SAdrien Destugues 	r.top = menuBar->Bounds().bottom + 1;
58b59ed154SAdrien Destugues 	r.right -= B_V_SCROLL_BAR_WIDTH;
59b59ed154SAdrien Destugues 	fTermView = new TermView(r);
60b59ed154SAdrien Destugues 	fTermView->ResizeToPreferred();
61b59ed154SAdrien Destugues 
62b59ed154SAdrien Destugues 	r = fTermView->Frame();
63b59ed154SAdrien Destugues 	r.left = r.right + 1;
64b59ed154SAdrien Destugues 	r.right = r.left + B_V_SCROLL_BAR_WIDTH;
65b59ed154SAdrien Destugues 	r.top -= 1;
66b59ed154SAdrien Destugues 	r.bottom -= B_H_SCROLL_BAR_HEIGHT - 1;
671eb38608SAdrien Destugues 
68b59ed154SAdrien Destugues 	BScrollBar* scrollBar = new BScrollBar(r, "scrollbar", NULL, 0, 0,
69b59ed154SAdrien Destugues 		B_VERTICAL);
70b59ed154SAdrien Destugues 
71b59ed154SAdrien Destugues 	scrollBar->SetTarget(fTermView);
72b59ed154SAdrien Destugues 
73b59ed154SAdrien Destugues 	ResizeTo(r.right - 1, r.bottom + B_H_SCROLL_BAR_HEIGHT - 1);
74dae0a4e0SAdrien Destugues - PulkoMandy 
75ce058fa0SAdrien Destugues 	r = fTermView->Frame();
76ce058fa0SAdrien Destugues 	r.top = r.bottom - 37;
77ce058fa0SAdrien Destugues 
782e6ca31cSMurai Takashi 	fStatusBar = new BStatusBar(r, B_TRANSLATE("file transfer progress"),
792e6ca31cSMurai Takashi 		NULL, NULL);
80ce058fa0SAdrien Destugues 	fStatusBar->SetResizingMode(B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT);
81ce058fa0SAdrien Destugues 	fStatusBar->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
82ce058fa0SAdrien Destugues 	fStatusBar->Hide();
83ce058fa0SAdrien Destugues 
84dae0a4e0SAdrien Destugues - PulkoMandy 	AddChild(menuBar);
85f441fd03SAdrien Destugues - PulkoMandy 	AddChild(fTermView);
86b59ed154SAdrien Destugues 	AddChild(scrollBar);
87ce058fa0SAdrien Destugues 	AddChild(fStatusBar);
88dae0a4e0SAdrien Destugues - PulkoMandy 
892e6ca31cSMurai Takashi 	fConnectionMenu = new BMenu(B_TRANSLATE("Connection"));
902e6ca31cSMurai Takashi 	fFileMenu = new BMenu(B_TRANSLATE("File"));
912e6ca31cSMurai Takashi 	BMenu* settingsMenu = new BMenu(B_TRANSLATE("Settings"));
922e6ca31cSMurai Takashi 	BMenu* editMenu = new BMenu(B_TRANSLATE("Edit"));
93dae0a4e0SAdrien Destugues - PulkoMandy 
94469e6cd2SAdrien Destugues - PulkoMandy 	fConnectionMenu->SetRadioMode(true);
95469e6cd2SAdrien Destugues - PulkoMandy 
9604434656SAdrien Destugues - PulkoMandy 	menuBar->AddItem(fConnectionMenu);
971eb38608SAdrien Destugues 	menuBar->AddItem(editMenu);
98ce058fa0SAdrien Destugues 	menuBar->AddItem(fFileMenu);
99dae0a4e0SAdrien Destugues - PulkoMandy 	menuBar->AddItem(settingsMenu);
100dae0a4e0SAdrien Destugues - PulkoMandy 
1012e6ca31cSMurai Takashi 	BMenuItem* logFile = new BMenuItem(
1022e6ca31cSMurai Takashi 		B_TRANSLATE("Log to file" B_UTF8_ELLIPSIS), new BMessage(kMsgLogfile));
103ce058fa0SAdrien Destugues 	fFileMenu->AddItem(logFile);
1048bff17cfSAdrien Destugues 
1058bff17cfSAdrien Destugues 	// The "send" items are disabled initially. They are enabled only once we
1068bff17cfSAdrien Destugues 	// are connected to a serial port.
1078bff17cfSAdrien Destugues 	BMessage* sendMsg = new BMessage(kMsgSendFile);
1088bff17cfSAdrien Destugues 	sendMsg->AddString("protocol", "xmodem");
1092e6ca31cSMurai Takashi 	BMenuItem* xmodemSend = new BMenuItem(
1102e6ca31cSMurai Takashi 		B_TRANSLATE("XModem send" B_UTF8_ELLIPSIS),
1118bff17cfSAdrien Destugues 		sendMsg);
112ce058fa0SAdrien Destugues 	fFileMenu->AddItem(xmodemSend);
113ce058fa0SAdrien Destugues 	xmodemSend->SetEnabled(false);
1148bff17cfSAdrien Destugues 
1152e6ca31cSMurai Takashi 	BMenuItem* rawSend = new BMenuItem(B_TRANSLATE("Raw send" B_UTF8_ELLIPSIS),
1168bff17cfSAdrien Destugues 		new BMessage(kMsgSendFile));
1178bff17cfSAdrien Destugues 	fFileMenu->AddItem(rawSend);
1188bff17cfSAdrien Destugues 	rawSend->SetEnabled(false);
1198bff17cfSAdrien Destugues 
120ce058fa0SAdrien Destugues #if 0
1218bff17cfSAdrien Destugues 	// TODO implement this
122469e6cd2SAdrien Destugues - PulkoMandy 	BMenuItem* xmodemReceive = new BMenuItem(
123469e6cd2SAdrien Destugues - PulkoMandy 		"X/Y/Zmodem receive" B_UTF8_ELLIPSIS, NULL);
124ce058fa0SAdrien Destugues 	fFileMenu->AddItem(xmodemReceive);
125ce058fa0SAdrien Destugues 	xmodemReceive->SetEnabled(false);
126ccc6b9beSAdrien Destugues #endif
127dae0a4e0SAdrien Destugues - PulkoMandy 
1281eb38608SAdrien Destugues 	// Items for the edit menu
1292e6ca31cSMurai Takashi 	BMenuItem* clearScreen = new BMenuItem(B_TRANSLATE("Clear history"),
130*3a6bc1cfSPulkoMandy 		new BMessage(kMsgClear), 'L');
1311eb38608SAdrien Destugues 	editMenu->AddItem(clearScreen);
1321eb38608SAdrien Destugues 
133*3a6bc1cfSPulkoMandy 	BMenuItem* paste = new BMenuItem(B_TRANSLATE("Paste"), new BMessage(B_PASTE), 'V');
134*3a6bc1cfSPulkoMandy 	editMenu->AddItem(paste);
135*3a6bc1cfSPulkoMandy 
1361eb38608SAdrien Destugues 	// TODO copy (when we have selection), paste
1371eb38608SAdrien Destugues 
13804434656SAdrien Destugues - PulkoMandy 	// Configuring all this by menus may be a bit unhandy. Make a setting
13904434656SAdrien Destugues - PulkoMandy 	// window instead ?
1402e6ca31cSMurai Takashi 	fBaudrateMenu = new BMenu(B_TRANSLATE("Baud rate"));
141ccc6b9beSAdrien Destugues 	fBaudrateMenu->SetRadioMode(true);
142ccc6b9beSAdrien Destugues 	settingsMenu->AddItem(fBaudrateMenu);
14304434656SAdrien Destugues - PulkoMandy 
1442e6ca31cSMurai Takashi 	fParityMenu = new BMenu(B_TRANSLATE("Parity"));
145ccc6b9beSAdrien Destugues 	fParityMenu->SetRadioMode(true);
146ccc6b9beSAdrien Destugues 	settingsMenu->AddItem(fParityMenu);
14704434656SAdrien Destugues - PulkoMandy 
1482e6ca31cSMurai Takashi 	fStopbitsMenu = new BMenu(B_TRANSLATE("Stop bits"));
149ccc6b9beSAdrien Destugues 	fStopbitsMenu->SetRadioMode(true);
150ccc6b9beSAdrien Destugues 	settingsMenu->AddItem(fStopbitsMenu);
15104434656SAdrien Destugues - PulkoMandy 
1522e6ca31cSMurai Takashi 	fFlowcontrolMenu = new BMenu(B_TRANSLATE("Flow control"));
153ccc6b9beSAdrien Destugues 	fFlowcontrolMenu->SetRadioMode(true);
154ccc6b9beSAdrien Destugues 	settingsMenu->AddItem(fFlowcontrolMenu);
15504434656SAdrien Destugues - PulkoMandy 
1562e6ca31cSMurai Takashi 	fDatabitsMenu = new BMenu(B_TRANSLATE("Data bits"));
157ccc6b9beSAdrien Destugues 	fDatabitsMenu->SetRadioMode(true);
158ccc6b9beSAdrien Destugues 	settingsMenu->AddItem(fDatabitsMenu);
15904434656SAdrien Destugues - PulkoMandy 
1602e6ca31cSMurai Takashi 	fLineTerminatorMenu = new BMenu(B_TRANSLATE("Line terminator"));
161534d0e61SAdrien Destugues 	fLineTerminatorMenu->SetRadioMode(true);
162534d0e61SAdrien Destugues 	settingsMenu->AddItem(fLineTerminatorMenu);
16304434656SAdrien Destugues - PulkoMandy 
16404434656SAdrien Destugues - PulkoMandy 	BMessage* message = new BMessage(kMsgSettings);
16504434656SAdrien Destugues - PulkoMandy 	message->AddInt32("parity", B_NO_PARITY);
1662e6ca31cSMurai Takashi 	BMenuItem* parityNone =
1672e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("None", "Parity"), message);
16804434656SAdrien Destugues - PulkoMandy 
16904434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
17004434656SAdrien Destugues - PulkoMandy 	message->AddInt32("parity", B_ODD_PARITY);
1712e6ca31cSMurai Takashi 	BMenuItem* parityOdd = new BMenuItem(B_TRANSLATE_COMMENT("Odd", "Parity"),
1722e6ca31cSMurai Takashi 		message);
17304434656SAdrien Destugues - PulkoMandy 
17404434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
17504434656SAdrien Destugues - PulkoMandy 	message->AddInt32("parity", B_EVEN_PARITY);
1762e6ca31cSMurai Takashi 	BMenuItem* parityEven =
1772e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("Even", "Parity"), message);
17804434656SAdrien Destugues - PulkoMandy 
179ccc6b9beSAdrien Destugues 	fParityMenu->AddItem(parityNone);
180ccc6b9beSAdrien Destugues 	fParityMenu->AddItem(parityOdd);
181ccc6b9beSAdrien Destugues 	fParityMenu->AddItem(parityEven);
182ccc6b9beSAdrien Destugues 	fParityMenu->SetTargetForItems(be_app);
18304434656SAdrien Destugues - PulkoMandy 
18404434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
18504434656SAdrien Destugues - PulkoMandy 	message->AddInt32("databits", B_DATA_BITS_7);
18604434656SAdrien Destugues - PulkoMandy 	BMenuItem* data7 = new BMenuItem("7", message);
18704434656SAdrien Destugues - PulkoMandy 
18804434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
18904434656SAdrien Destugues - PulkoMandy 	message->AddInt32("databits", B_DATA_BITS_8);
19004434656SAdrien Destugues - PulkoMandy 	BMenuItem* data8 = new BMenuItem("8", message);
19104434656SAdrien Destugues - PulkoMandy 
192ccc6b9beSAdrien Destugues 	fDatabitsMenu->AddItem(data7);
193ccc6b9beSAdrien Destugues 	fDatabitsMenu->AddItem(data8);
194ccc6b9beSAdrien Destugues 	fDatabitsMenu->SetTargetForItems(be_app);
19504434656SAdrien Destugues - PulkoMandy 
19604434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
19704434656SAdrien Destugues - PulkoMandy 	message->AddInt32("stopbits", B_STOP_BITS_1);
198ccc6b9beSAdrien Destugues 	BMenuItem* stop1 = new BMenuItem("1", message);
19904434656SAdrien Destugues - PulkoMandy 
20004434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
20104434656SAdrien Destugues - PulkoMandy 	message->AddInt32("stopbits", B_STOP_BITS_2);
202ccc6b9beSAdrien Destugues 	BMenuItem* stop2 = new BMenuItem("2", message);
20304434656SAdrien Destugues - PulkoMandy 
204ccc6b9beSAdrien Destugues 	fStopbitsMenu->AddItem(stop1);
205ccc6b9beSAdrien Destugues 	fStopbitsMenu->AddItem(stop2);
206ccc6b9beSAdrien Destugues 	fStopbitsMenu->SetTargetForItems(be_app);
20704434656SAdrien Destugues - PulkoMandy 
20804434656SAdrien Destugues - PulkoMandy 	// Loop backwards to add fastest rates at top of menu
209bc026197SAlexander von Gluck IV 	for (int i = sizeof(kBaudrates) / sizeof(kBaudrates[0]); --i >= 0;)
21004434656SAdrien Destugues - PulkoMandy 	{
21104434656SAdrien Destugues - PulkoMandy 		message = new BMessage(kMsgSettings);
212e2367275SAdrien Destugues 		message->AddInt32("baudrate", kBaudrateConstants[i]);
21304434656SAdrien Destugues - PulkoMandy 
21404434656SAdrien Destugues - PulkoMandy 		char buffer[7];
215ccc6b9beSAdrien Destugues 		sprintf(buffer, "%d", kBaudrates[i]);
21604434656SAdrien Destugues - PulkoMandy 		BMenuItem* item = new BMenuItem(buffer, message);
21704434656SAdrien Destugues - PulkoMandy 
218ccc6b9beSAdrien Destugues 		fBaudrateMenu->AddItem(item);
21904434656SAdrien Destugues - PulkoMandy 	}
22004434656SAdrien Destugues - PulkoMandy 
22196e59ccaSAdrien Destugues 	message = new BMessage(kMsgCustomBaudrate);
2222e6ca31cSMurai Takashi 	BMenuItem* custom =
2232e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("custom" B_UTF8_ELLIPSIS,
2242e6ca31cSMurai Takashi 		"Baudrate"), message);
22596e59ccaSAdrien Destugues 	fBaudrateMenu->AddItem(custom);
22696e59ccaSAdrien Destugues 
227ccc6b9beSAdrien Destugues 	fBaudrateMenu->SetTargetForItems(be_app);
22804434656SAdrien Destugues - PulkoMandy 
22904434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
23004434656SAdrien Destugues - PulkoMandy 	message->AddInt32("flowcontrol", B_HARDWARE_CONTROL);
2312e6ca31cSMurai Takashi 	BMenuItem* hardware =
2322e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("Hardware", "Flowcontrol"), message);
23304434656SAdrien Destugues - PulkoMandy 
23404434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
23504434656SAdrien Destugues - PulkoMandy 	message->AddInt32("flowcontrol", B_SOFTWARE_CONTROL);
2362e6ca31cSMurai Takashi 	BMenuItem* software =
2372e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("Software", "Flowcontrol"), message);
23804434656SAdrien Destugues - PulkoMandy 
23904434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
24004434656SAdrien Destugues - PulkoMandy 	message->AddInt32("flowcontrol", B_HARDWARE_CONTROL | B_SOFTWARE_CONTROL);
2412e6ca31cSMurai Takashi 	BMenuItem* both =
2422e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("Both", "Flowcontrol"), message);
24304434656SAdrien Destugues - PulkoMandy 
24404434656SAdrien Destugues - PulkoMandy 	message = new BMessage(kMsgSettings);
24504434656SAdrien Destugues - PulkoMandy 	message->AddInt32("flowcontrol", 0);
2462e6ca31cSMurai Takashi 	BMenuItem* noFlow =
2472e6ca31cSMurai Takashi 		new BMenuItem(B_TRANSLATE_COMMENT("None", "Flowcontrol"), message);
24804434656SAdrien Destugues - PulkoMandy 
249ccc6b9beSAdrien Destugues 	fFlowcontrolMenu->AddItem(hardware);
250ccc6b9beSAdrien Destugues 	fFlowcontrolMenu->AddItem(software);
251ccc6b9beSAdrien Destugues 	fFlowcontrolMenu->AddItem(both);
252ccc6b9beSAdrien Destugues 	fFlowcontrolMenu->AddItem(noFlow);
253ccc6b9beSAdrien Destugues 	fFlowcontrolMenu->SetTargetForItems(be_app);
25404434656SAdrien Destugues - PulkoMandy 
255534d0e61SAdrien Destugues 	message = new BMessage(kMsgSettings);
256534d0e61SAdrien Destugues 	message->AddString("terminator", "\n");
257534d0e61SAdrien Destugues 	BMenuItem* lf = new BMenuItem("LF (\\n)", message);
258534d0e61SAdrien Destugues 
259534d0e61SAdrien Destugues 	message = new BMessage(kMsgSettings);
260534d0e61SAdrien Destugues 	message->AddString("terminator", "\r");
261534d0e61SAdrien Destugues 	BMenuItem* cr = new BMenuItem("CR (\\r)", message);
262534d0e61SAdrien Destugues 
263534d0e61SAdrien Destugues 	message = new BMessage(kMsgSettings);
264534d0e61SAdrien Destugues 	message->AddString("terminator", "\r\n");
265534d0e61SAdrien Destugues 	BMenuItem* crlf = new BMenuItem("CR/LF (\\r\\n)", message);
266534d0e61SAdrien Destugues 
267534d0e61SAdrien Destugues 	fLineTerminatorMenu->AddItem(lf);
268534d0e61SAdrien Destugues 	fLineTerminatorMenu->AddItem(cr);
269534d0e61SAdrien Destugues 	fLineTerminatorMenu->AddItem(crlf);
270534d0e61SAdrien Destugues 
27104434656SAdrien Destugues - PulkoMandy 	CenterOnScreen();
27204434656SAdrien Destugues - PulkoMandy }
27304434656SAdrien Destugues - PulkoMandy 
274469e6cd2SAdrien Destugues - PulkoMandy 
~SerialWindow()275469e6cd2SAdrien Destugues - PulkoMandy SerialWindow::~SerialWindow()
276469e6cd2SAdrien Destugues - PulkoMandy {
277469e6cd2SAdrien Destugues - PulkoMandy 	delete fLogFilePanel;
27883776950SAdrien Destugues 	delete fSendFilePanel;
279469e6cd2SAdrien Destugues - PulkoMandy }
280469e6cd2SAdrien Destugues - PulkoMandy 
28104434656SAdrien Destugues - PulkoMandy 
MenusBeginning()28204434656SAdrien Destugues - PulkoMandy void SerialWindow::MenusBeginning()
28304434656SAdrien Destugues - PulkoMandy {
28404434656SAdrien Destugues - PulkoMandy 	// remove all items from the menu
285c19e7bb3SUrias McCullough 	fConnectionMenu->RemoveItems(0, fConnectionMenu->CountItems(), true);
28604434656SAdrien Destugues - PulkoMandy 
28704434656SAdrien Destugues - PulkoMandy 	// fill it with the (updated) serial port list
288dae0a4e0SAdrien Destugues - PulkoMandy 	BSerialPort serialPort;
289dae0a4e0SAdrien Destugues - PulkoMandy 	int deviceCount = serialPort.CountDevices();
290ccc6b9beSAdrien Destugues 	bool connected = false;
291dae0a4e0SAdrien Destugues - PulkoMandy 
292dae0a4e0SAdrien Destugues - PulkoMandy 	for (int i = 0; i < deviceCount; i++)
293dae0a4e0SAdrien Destugues - PulkoMandy 	{
294dae0a4e0SAdrien Destugues - PulkoMandy 		char buffer[256];
295dae0a4e0SAdrien Destugues - PulkoMandy 		serialPort.GetDeviceName(i, buffer, 256);
296dae0a4e0SAdrien Destugues - PulkoMandy 
297f441fd03SAdrien Destugues - PulkoMandy 		BMessage* message = new BMessage(kMsgOpenPort);
298f441fd03SAdrien Destugues - PulkoMandy 		message->AddString("port name", buffer);
299f441fd03SAdrien Destugues - PulkoMandy 		BMenuItem* portItem = new BMenuItem(buffer, message);
300a2021beeSAdrien Destugues - PulkoMandy 		portItem->SetTarget(be_app);
30104434656SAdrien Destugues - PulkoMandy 
302ccc6b9beSAdrien Destugues 		const BString& connectedPort = ((SerialApp*)be_app)->GetPort();
303ccc6b9beSAdrien Destugues 
304ccc6b9beSAdrien Destugues 		if (connectedPort == buffer) {
305ccc6b9beSAdrien Destugues 			connected = true;
306ccc6b9beSAdrien Destugues 			portItem->SetMarked(true);
307ccc6b9beSAdrien Destugues 		}
308ccc6b9beSAdrien Destugues 
30904434656SAdrien Destugues - PulkoMandy 		fConnectionMenu->AddItem(portItem);
310dae0a4e0SAdrien Destugues - PulkoMandy 	}
311dae0a4e0SAdrien Destugues - PulkoMandy 
31204434656SAdrien Destugues - PulkoMandy 	if (deviceCount > 0) {
31304434656SAdrien Destugues - PulkoMandy 		fConnectionMenu->AddSeparatorItem();
314dae0a4e0SAdrien Destugues - PulkoMandy 
3152e6ca31cSMurai Takashi 		BMenuItem* disconnect = new BMenuItem(B_TRANSLATE("Disconnect"),
31604434656SAdrien Destugues - PulkoMandy 			new BMessage(kMsgOpenPort), 'Z', B_OPTION_KEY);
317ccc6b9beSAdrien Destugues 		if (!connected)
318ccc6b9beSAdrien Destugues 			disconnect->SetEnabled(false);
319d9e93152SAdrien Destugues 		disconnect->SetTarget(be_app);
32004434656SAdrien Destugues - PulkoMandy 		fConnectionMenu->AddItem(disconnect);
32104434656SAdrien Destugues - PulkoMandy 	} else {
3222e6ca31cSMurai Takashi 		BMenuItem* noDevices =
3232e6ca31cSMurai Takashi 			new BMenuItem(B_TRANSLATE("<no serial port available>"), NULL);
32404434656SAdrien Destugues - PulkoMandy 		noDevices->SetEnabled(false);
32504434656SAdrien Destugues - PulkoMandy 		fConnectionMenu->AddItem(noDevices);
326dae0a4e0SAdrien Destugues - PulkoMandy 	}
327dae0a4e0SAdrien Destugues - PulkoMandy }
328dae0a4e0SAdrien Destugues - PulkoMandy 
329b59ed154SAdrien Destugues 
MessageReceived(BMessage * message)330f441fd03SAdrien Destugues - PulkoMandy void SerialWindow::MessageReceived(BMessage* message)
331f441fd03SAdrien Destugues - PulkoMandy {
332f441fd03SAdrien Destugues - PulkoMandy 	switch (message->what)
333f441fd03SAdrien Destugues - PulkoMandy 	{
334ce058fa0SAdrien Destugues 		case kMsgOpenPort:
335ce058fa0SAdrien Destugues 		{
336ce058fa0SAdrien Destugues 			BString path;
337ce058fa0SAdrien Destugues 			bool open = (message->FindString("port name", &path) == B_OK);
338ce058fa0SAdrien Destugues 			int i = 1; // Skip "log to file", which woeks even when offline.
339ce058fa0SAdrien Destugues 			BMenuItem* item;
340ce058fa0SAdrien Destugues 			while((item = fFileMenu->ItemAt(i++)))
341ce058fa0SAdrien Destugues 			{
342ce058fa0SAdrien Destugues 				item->SetEnabled(open);
343ce058fa0SAdrien Destugues 			}
344ce058fa0SAdrien Destugues 			return;
345ce058fa0SAdrien Destugues 		}
346f441fd03SAdrien Destugues - PulkoMandy 		case kMsgDataRead:
347f441fd03SAdrien Destugues - PulkoMandy 		{
348f441fd03SAdrien Destugues - PulkoMandy 			const char* bytes;
349f441fd03SAdrien Destugues - PulkoMandy 			ssize_t length;
350492ac416SAdrien Destugues 			if (message->FindData("data", B_RAW_TYPE, (const void**)&bytes,
351492ac416SAdrien Destugues 					&length) == B_OK)
352f441fd03SAdrien Destugues - PulkoMandy 				fTermView->PushBytes(bytes, length);
353d9e93152SAdrien Destugues 			return;
354f441fd03SAdrien Destugues - PulkoMandy 		}
355469e6cd2SAdrien Destugues - PulkoMandy 		case kMsgLogfile:
356469e6cd2SAdrien Destugues - PulkoMandy 		{
357469e6cd2SAdrien Destugues - PulkoMandy 			// Let's lazy init the file panel
358469e6cd2SAdrien Destugues - PulkoMandy 			if (fLogFilePanel == NULL) {
35983776950SAdrien Destugues 				fLogFilePanel = new BFilePanel(B_SAVE_PANEL,
360ce058fa0SAdrien Destugues 					&be_app_messenger, NULL, B_FILE_NODE, false);
361469e6cd2SAdrien Destugues - PulkoMandy 				fLogFilePanel->SetMessage(message);
362469e6cd2SAdrien Destugues - PulkoMandy 			}
363469e6cd2SAdrien Destugues - PulkoMandy 			fLogFilePanel->Show();
364d9e93152SAdrien Destugues 			return;
365469e6cd2SAdrien Destugues - PulkoMandy 		}
36683776950SAdrien Destugues 		case kMsgSendFile:
36783776950SAdrien Destugues 		{
36883776950SAdrien Destugues 			// Let's lazy init the file panel
36983776950SAdrien Destugues 			if (fSendFilePanel == NULL) {
37083776950SAdrien Destugues 				fSendFilePanel = new BFilePanel(B_OPEN_PANEL,
37183776950SAdrien Destugues 					&be_app_messenger, NULL, B_FILE_NODE, false);
37283776950SAdrien Destugues 			}
37383776950SAdrien Destugues 			fSendFilePanel->SetMessage(message);
37483776950SAdrien Destugues 			fSendFilePanel->Show();
37583776950SAdrien Destugues 			return;
37683776950SAdrien Destugues 		}
377ccc6b9beSAdrien Destugues 		case kMsgSettings:
378ccc6b9beSAdrien Destugues 		{
379ccc6b9beSAdrien Destugues 			int32 baudrate;
380ccc6b9beSAdrien Destugues 			stop_bits stopBits;
381ccc6b9beSAdrien Destugues 			data_bits dataBits;
382ccc6b9beSAdrien Destugues 			parity_mode parity;
383ccc6b9beSAdrien Destugues 			uint32 flowcontrol;
384534d0e61SAdrien Destugues 			BString terminator;
385ccc6b9beSAdrien Destugues 
386b59ed154SAdrien Destugues 			if (message->FindInt32("databits", (int32*)&dataBits) == B_OK) {
387b59ed154SAdrien Destugues 				for (int i = 0; i < fDatabitsMenu->CountItems(); i++) {
388ccc6b9beSAdrien Destugues 					BMenuItem* item = fDatabitsMenu->ItemAt(i);
389ccc6b9beSAdrien Destugues 					int32 code;
390ccc6b9beSAdrien Destugues 					item->Message()->FindInt32("databits", &code);
391ccc6b9beSAdrien Destugues 
392ccc6b9beSAdrien Destugues 					if (code == dataBits)
393ccc6b9beSAdrien Destugues 						item->SetMarked(true);
394ccc6b9beSAdrien Destugues 				}
395ccc6b9beSAdrien Destugues 			}
396ccc6b9beSAdrien Destugues 
397b59ed154SAdrien Destugues 			if (message->FindInt32("stopbits", (int32*)&stopBits) == B_OK) {
398b59ed154SAdrien Destugues 				for (int i = 0; i < fStopbitsMenu->CountItems(); i++) {
399ccc6b9beSAdrien Destugues 					BMenuItem* item = fStopbitsMenu->ItemAt(i);
400ccc6b9beSAdrien Destugues 					int32 code;
401ccc6b9beSAdrien Destugues 					item->Message()->FindInt32("stopbits", &code);
402ccc6b9beSAdrien Destugues 
403ccc6b9beSAdrien Destugues 					if (code == stopBits)
404ccc6b9beSAdrien Destugues 						item->SetMarked(true);
405ccc6b9beSAdrien Destugues 				}
406ccc6b9beSAdrien Destugues 			}
407ccc6b9beSAdrien Destugues 
408ccc6b9beSAdrien Destugues 			if (message->FindInt32("parity", (int32*)&parity) == B_OK)
409ccc6b9beSAdrien Destugues 			{
410b59ed154SAdrien Destugues 				for (int i = 0; i < fParityMenu->CountItems(); i++) {
411ccc6b9beSAdrien Destugues 					BMenuItem* item = fParityMenu->ItemAt(i);
412ccc6b9beSAdrien Destugues 					int32 code;
413ccc6b9beSAdrien Destugues 					item->Message()->FindInt32("parity", &code);
414ccc6b9beSAdrien Destugues 
415ccc6b9beSAdrien Destugues 					if (code == parity)
416ccc6b9beSAdrien Destugues 						item->SetMarked(true);
417ccc6b9beSAdrien Destugues 				}
418ccc6b9beSAdrien Destugues 			}
419ccc6b9beSAdrien Destugues 
420b59ed154SAdrien Destugues 			if (message->FindInt32("flowcontrol", (int32*)&flowcontrol)
421b59ed154SAdrien Destugues 					== B_OK) {
422b59ed154SAdrien Destugues 				for (int i = 0; i < fFlowcontrolMenu->CountItems(); i++) {
423ccc6b9beSAdrien Destugues 					BMenuItem* item = fFlowcontrolMenu->ItemAt(i);
424ccc6b9beSAdrien Destugues 					int32 code;
425ccc6b9beSAdrien Destugues 					item->Message()->FindInt32("flowcontrol", &code);
426ccc6b9beSAdrien Destugues 
427ccc6b9beSAdrien Destugues 					if (code == (int32)flowcontrol)
428ccc6b9beSAdrien Destugues 						item->SetMarked(true);
429ccc6b9beSAdrien Destugues 				}
430ccc6b9beSAdrien Destugues 			}
431ccc6b9beSAdrien Destugues 
432b59ed154SAdrien Destugues 			if (message->FindInt32("baudrate", &baudrate) == B_OK) {
43396e59ccaSAdrien Destugues 				int i;
43496e59ccaSAdrien Destugues 				BMenuItem* item = NULL;
43596e59ccaSAdrien Destugues 				for (i = 0; i < fBaudrateMenu->CountItems(); i++) {
43696e59ccaSAdrien Destugues 					item = fBaudrateMenu->ItemAt(i);
43796e59ccaSAdrien Destugues 					int32 code = 0;
438ccc6b9beSAdrien Destugues 					item->Message()->FindInt32("baudrate", &code);
439ccc6b9beSAdrien Destugues 
44096e59ccaSAdrien Destugues 					if (baudrate == code) {
441ccc6b9beSAdrien Destugues 						item->SetMarked(true);
44296e59ccaSAdrien Destugues 						break;
44396e59ccaSAdrien Destugues 					}
44496e59ccaSAdrien Destugues 				}
44596e59ccaSAdrien Destugues 
44696e59ccaSAdrien Destugues 				if (i == fBaudrateMenu->CountItems() && item != NULL) {
44796e59ccaSAdrien Destugues 					// Rate was not found, mark it as "custom".
44896e59ccaSAdrien Destugues 					// Since that is the last item in the menu, we still point
44996e59ccaSAdrien Destugues 					// to it.
45096e59ccaSAdrien Destugues 					item->SetMarked(true);
45196e59ccaSAdrien Destugues 					item->Message()->SetInt32("baudrate", baudrate);
452ccc6b9beSAdrien Destugues 				}
453ccc6b9beSAdrien Destugues 			}
454ccc6b9beSAdrien Destugues 
455534d0e61SAdrien Destugues 			if (message->FindString("terminator", &terminator) == B_OK) {
456534d0e61SAdrien Destugues 				fTermView->SetLineTerminator(terminator);
457534d0e61SAdrien Destugues 				for (int i = 0; i < fLineTerminatorMenu->CountItems(); i++) {
458534d0e61SAdrien Destugues 					BMenuItem* item = fLineTerminatorMenu->ItemAt(i);
459534d0e61SAdrien Destugues 					BString code;
460534d0e61SAdrien Destugues 					item->Message()->FindString("terminator", &code);
461534d0e61SAdrien Destugues 
462534d0e61SAdrien Destugues 					if (terminator == code)
463534d0e61SAdrien Destugues 						item->SetMarked(true);
464534d0e61SAdrien Destugues 				}
465534d0e61SAdrien Destugues 			}
466534d0e61SAdrien Destugues 
467d9e93152SAdrien Destugues 			return;
468ccc6b9beSAdrien Destugues 		}
4691eb38608SAdrien Destugues 		case kMsgClear:
4701eb38608SAdrien Destugues 		{
4711eb38608SAdrien Destugues 			fTermView->Clear();
4721eb38608SAdrien Destugues 			return;
4731eb38608SAdrien Destugues 		}
474*3a6bc1cfSPulkoMandy 		case B_PASTE:
475*3a6bc1cfSPulkoMandy 		{
476*3a6bc1cfSPulkoMandy 			fTermView->PasteFromClipboard();
477*3a6bc1cfSPulkoMandy 		}
478ce058fa0SAdrien Destugues 		case kMsgProgress:
479ce058fa0SAdrien Destugues 		{
480ce058fa0SAdrien Destugues 			// File transfer progress
481ce058fa0SAdrien Destugues 			int32 pos = message->FindInt32("pos");
482ce058fa0SAdrien Destugues 			int32 size = message->FindInt32("size");
483ce058fa0SAdrien Destugues 			BString label = message->FindString("info");
484ce058fa0SAdrien Destugues 
485ce058fa0SAdrien Destugues 			if (pos >= size) {
486ce058fa0SAdrien Destugues 				if (!fStatusBar->IsHidden()) {
487ce058fa0SAdrien Destugues 					fStatusBar->Hide();
488ce058fa0SAdrien Destugues 					fTermView->ResizeBy(0, fStatusBar->Bounds().Height() - 1);
489ce058fa0SAdrien Destugues 				}
490ce058fa0SAdrien Destugues 			} else {
491ce058fa0SAdrien Destugues 				BString text;
492ce058fa0SAdrien Destugues 				text.SetToFormat("%" B_PRId32 "/%" B_PRId32, pos, size);
493ce058fa0SAdrien Destugues 				fStatusBar->SetMaxValue(size);
494ce058fa0SAdrien Destugues 				fStatusBar->SetTo(pos, label, text);
495ce058fa0SAdrien Destugues 				if (fStatusBar->IsHidden()) {
496ce058fa0SAdrien Destugues 					fStatusBar->Show();
497ce058fa0SAdrien Destugues 					fTermView->ResizeBy(0, -(fStatusBar->Bounds().Height() - 1));
498ce058fa0SAdrien Destugues 				}
499ce058fa0SAdrien Destugues 			}
500ce058fa0SAdrien Destugues 			return;
501ce058fa0SAdrien Destugues 		}
502ab05d368Ssushilhub 		default:
503f441fd03SAdrien Destugues - PulkoMandy 			BWindow::MessageReceived(message);
504f441fd03SAdrien Destugues - PulkoMandy 	}
505ab05d368Ssushilhub }
506