xref: /haiku/src/tests/kits/device/stickit_BJoystick/StickItWindow.cpp (revision e2d7e6c4be9142efe0d8964943d674a6a784c6de)
1 //
2 // StickIt
3 // File: jwindow.cpp
4 // Joystick window.
5 // Sampel code used in "Getting a Grip on BJoystick" by Eric Shepherd
6 //
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <OS.h>
12 
13 #include <Application.h>
14 #include <Joystick.h>
15 #include <String.h>
16 #include <StringView.h>
17 #include <Box.h>
18 #include <ListView.h>
19 #include <ScrollView.h>
20 #include <ListItem.h>
21 
22 #define SPACE 10
23 
24 #include "JoystickWindow.h"
25 
26 #define SELECTED  'sele'
27 #define INVOKE    'invo'
28 
29 #include "StickItWindow.h"
30 
StickItWindow(BRect frame)31 StickItWindow::StickItWindow(BRect frame)
32 	: BWindow(frame, "StickIt", B_TITLED_WINDOW, 0)
33 {
34 	frame = Bounds();
35 	frame.InsetBy(5, 5);
36 	BBox* box = new BBox(frame, NULL, B_FOLLOW_ALL_SIDES);
37 
38 
39 	// Allocate object
40 	BView* view = new BView(Bounds(), "", B_FOLLOW_ALL_SIDES, 0);
41 	view->SetViewColor(216, 216, 216);
42 	view->SetLowColor(216, 216, 216);
43 
44 	BRect rectString = BRect(frame.left, frame.top-10, frame.right -30, 30);
45 	BStringView* stringview1 = new BStringView(rectString,"StringView1",
46 		"This list, lists action that StickIt makes.");
47 
48 	BRect rect = BRect(rectString.left, rectString.bottom + SPACE,
49 		rectString.right, rectString.bottom + SPACE + 200);
50 	fListView1 = new BListView(rect,"ListView1");
51 
52 	rectString = BRect(rect.left, rect.bottom + SPACE, rect.right,
53 		rect.bottom + SPACE + 15);
54 	BStringView* stringview2 = new BStringView(rectString,"StringView2",
55 		"Choose Joystick below if any exists");
56 
57 	rect = BRect(rectString.left, rectString.bottom + SPACE, rectString.right,
58 		Bounds().bottom -20);
59 	fListView2 = new BListView(rect,"ListView2");
60 	fListView2->SetSelectionMessage(new BMessage(SELECTED));
61 	fListView2->SetInvocationMessage(new BMessage(INVOKE));
62 
63 
64 	// Adding object
65 	box->AddChild(new BScrollView("fListView1", fListView1,
66 		B_FOLLOW_LEFT_RIGHT, 0, false, true));
67 
68 	box->AddChild(new BScrollView("fListView2", fListView2,
69 		B_FOLLOW_ALL_SIDES, 0, false, true));
70 
71 	box->AddChild(stringview1);
72 	box->AddChild(stringview2);
73 	view->AddChild(box);
74 	AddChild(view);
75 
76 	fJoystick = new BJoystick;
77 	PickJoystick(fJoystick);
78 }
79 
80 
81 bool
QuitRequested(void)82 StickItWindow::QuitRequested(void) {
83 	be_app->PostMessage(B_QUIT_REQUESTED);
84 	return true;
85 }
86 
87 
88 void
MessageReceived(BMessage * message)89 StickItWindow::MessageReceived(BMessage *message)
90 {
91 //	message->PrintToStream();
92 	switch(message->what)
93 	{
94 		case INVOKE:
95 		case SELECTED:
96 		{
97 			int32 id = fListView2->CurrentSelection();
98 			BString temp1;
99 			if (id > -1) {
100 				char devName[B_OS_NAME_LENGTH];
101 				status_t err = fJoystick->GetDeviceName(id, devName);
102 				if (err == B_OK) {
103 					temp1 << "BJoystick::GetDeviceName(), id = " << id
104 						<< ", name = " << devName;
105 					temp1 = AddToList(fListView1, temp1.String());
106 					BJoystick *joystick = new BJoystick();
107 					err = joystick->Open(devName);
108 					if (err != B_ERROR) {
109 						temp1 = AddToList(fListView1, "BJoystick::Open()");
110 						temp1 = AddToList(fListView1, "BJoystick::Open()");
111 						if (joystick->IsCalibrationEnabled())
112 							temp1 = AddToList(fListView1,
113 								"BJoystick::IsCalibrationEnabled() - True");
114 						else
115 							temp1 = AddToList(fListView1,
116 								"BJoystick::IsCalibrationEnabled() - False");
117 						JoystickWindow *window = new(std::nothrow)
118 							JoystickWindow(devName, joystick,
119 							BRect(50, 50, 405, 350));
120 						if (window != NULL)
121 							window->Show();
122 					} else
123 						AddToList(fListView1,
124 							"No controller connected on that port. Try again.");
125 				} else
126 					AddToList(fListView1, "Can't use that stick.  Try again.");
127 			}
128 		break;
129 		}
130 		default:
131 			BWindow::MessageReceived(message);
132 			break;
133 	}
134 }
135 
136 
137 BString
AddToList(BListView * bl,const char * str)138 StickItWindow::AddToList(BListView *bl, const char * str)
139 {
140 	bl->AddItem(new BStringItem(str));
141 	return BString ("");
142 }
143 
144 
145 // PickJoystick
146 // Present a list of all game controllers and let the user choose the one to
147 // use.  This will configure the "stick" object for that controller.
148 void
PickJoystick(BJoystick * stick)149 StickItWindow::PickJoystick(BJoystick *stick)
150 {
151 	int32 numDevices = stick->CountDevices();
152 	BString temp1("BJoystick::CountDevices()");
153 	temp1 << ", Num = " << numDevices;
154 	temp1 = AddToList(fListView1, temp1.String());
155 	status_t err = B_ERROR;
156 	if (numDevices) {
157 		temp1 << "There are " << numDevices
158 			<< " Joysticks device types available";
159 		temp1 = AddToList(fListView1, temp1.String());
160 		for (int32 i = 0; i < numDevices; i++) {
161 			char devName[B_OS_NAME_LENGTH];
162 			err = stick->GetDeviceName(i, devName);
163 			temp1 << "BJoystick::GetDeviceName(), id = " << i << ", name = "
164 				<< devName;
165 			temp1 = AddToList(fListView1, temp1.String());
166 			if (err == B_OK) {
167 				err = stick->Open(devName);
168 				temp1 = AddToList(fListView1, "BJoystick::Open()");
169 				int32 count = stick->CountSticks();
170 				temp1 << "BJoystick::CountSticks(), number of sticks = "
171 					<< count;
172 				temp1 = AddToList(fListView1, temp1.String());
173 
174 				count = stick->CountAxes();
175 				temp1 << "BJoystick::CountAxes(), number of Axes = "
176 					<< count;
177 				temp1 = AddToList(fListView1, temp1.String());
178 
179 				count = stick->CountButtons();
180 				temp1 << "BJoystick::CountButtons(), number of Buttons = "
181 					<< count;
182 				temp1 = AddToList(fListView1, temp1.String());
183 
184 				count = stick->CountHats();
185 				temp1 << "BJoystick::CountHats(), number of Hats = "
186 					<< count;
187 				temp1 = AddToList(fListView1, temp1.String());
188 
189 				count = stick->CountDevices();
190 				temp1 << "BJoystick::CountDevices(), number of Devices = "
191 					<< count;
192 				temp1 = AddToList(fListView1, temp1.String());
193 
194 				if (err != B_ERROR) {
195 					BString name;
196 					err = stick->GetControllerModule(&name);
197 					temp1 << "BJoystick::GetControllerModule(), name = "
198 						<< name;
199 					temp1 = AddToList(fListView1, temp1.String());
200 
201 					if (name == "Legacy") {
202 						bool b = stick->EnterEnhancedMode();
203 						if (b) {
204 							temp1 << "BJoystick::EnterEnhancedMode(), OK";
205 							temp1 = AddToList(fListView1, temp1.String());
206 						} else {
207 							temp1 << "BJoystick::EnterEnhancedMode(), Not OK";
208 							temp1 = AddToList(fListView1, temp1.String());
209 						}
210 					}
211 
212 					err = stick->GetControllerName(&name);
213 					temp1 << "BJoystick::GetControllerName(), name = " << name;
214 					temp1 = AddToList(fListView1, temp1.String());
215 					if (err == B_OK) {
216 						stick->Close();
217 						temp1 = AddToList(fListView1, "BJoystick::Close()");
218 						temp1 << i+1 << " " << name.String();
219 						temp1 = AddToList(fListView2, temp1.String());
220 					} else {
221 						temp1 << "Error = " << strerror(err);
222 						temp1 = AddToList(fListView1, temp1.String());
223 						temp1 << "*** Can't get name of controller "
224 							<< devName;
225 						temp1 = AddToList(fListView1, temp1.String());
226 					}
227 				} else {
228 					temp1 << "Error = " << strerror(err) << "err nr = " << err;
229 					temp1 = AddToList(fListView1, temp1.String());
230 					temp1 << "No controller on " << devName;
231 					temp1 = AddToList(fListView1, temp1.String());
232 				}
233 			} else {
234 				temp1 << "Error = " << strerror(err);
235 				temp1 = AddToList(fListView1, temp1.String());
236 				temp1 << "*** Error while reading controller list.";
237 				temp1 = AddToList(fListView1, temp1.String());
238 			}
239 		}
240 	} else {
241 		temp1 << "Error = " << strerror(err);
242 		temp1 = AddToList(fListView1, temp1.String());
243 		temp1 = AddToList(fListView1, "*** No game ports detected.");
244 	}
245 }
246