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 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 82 StickItWindow::QuitRequested(void) { 83 be_app->PostMessage(B_QUIT_REQUESTED); 84 return true; 85 } 86 87 88 void 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 err = fJoystick->Open(devName); 107 if (err != B_ERROR) { 108 temp1 = AddToList(fListView1, "BJoystick::Open()"); 109 temp1 = AddToList(fListView1, "BJoystick::Open()"); 110 if(fJoystick->IsCalibrationEnabled()) 111 temp1 = AddToList(fListView1, 112 "BJoystick::IsCalibrationEnabled() - True"); 113 else 114 temp1 = AddToList(fListView1, 115 "BJoystick::IsCalibrationEnabled() - False"); 116 fJoystickWindow = new JoystickWindow(fJoystick, 117 BRect(50, 50, 405, 350)); 118 fJoystickWindow->Show(); 119 } else 120 AddToList(fListView1, 121 "No controller connected on that port. Try again."); 122 } else 123 AddToList(fListView1, "Can't use that stick. Try again."); 124 } 125 break; 126 } 127 default: 128 BWindow::MessageReceived(message); 129 break; 130 } 131 } 132 133 134 BString 135 StickItWindow::AddToList(BListView *bl, const char * str) 136 { 137 bl->AddItem(new BStringItem(str)); 138 return BString (""); 139 } 140 141 142 // PickJoystick 143 // Present a list of all game controllers and let the user choose the one to 144 // use. This will configure the "stick" object for that controller. 145 void 146 StickItWindow::PickJoystick(BJoystick *stick) 147 { 148 int32 numDevices = stick->CountDevices(); 149 BString temp1("BJoystick::CountDevices()"); 150 temp1 << ", Num = " << numDevices; 151 temp1 = AddToList(fListView1, temp1.String()); 152 status_t err = B_ERROR; 153 if (numDevices) { 154 temp1 << "There are " << numDevices 155 << " Joysticks device types available"; 156 temp1 = AddToList(fListView1, temp1.String()); 157 for (int32 i = 0; i < numDevices; i++) { 158 char devName[B_OS_NAME_LENGTH]; 159 err = stick->GetDeviceName(i, devName); 160 temp1 << "BJoystick::GetDeviceName(), id = " << i << ", name = " 161 << devName; 162 temp1 = AddToList(fListView1, temp1.String()); 163 if (err == B_OK) { 164 err = stick->Open(devName); 165 temp1 = AddToList(fListView1, "BJoystick::Open()"); 166 int32 count = stick->CountSticks(); 167 temp1 << "BJoystick::CountSticks(), number of sticks = " 168 << count; 169 temp1 = AddToList(fListView1, temp1.String()); 170 171 count = stick->CountAxes(); 172 temp1 << "BJoystick::CountAxes(), number of Axes = " 173 << count; 174 temp1 = AddToList(fListView1, temp1.String()); 175 176 count = stick->CountButtons(); 177 temp1 << "BJoystick::CountButtons(), number of Buttons = " 178 << count; 179 temp1 = AddToList(fListView1, temp1.String()); 180 181 count = stick->CountHats(); 182 temp1 << "BJoystick::CountHats(), number of Hats = " 183 << count; 184 temp1 = AddToList(fListView1, temp1.String()); 185 186 count = stick->CountDevices(); 187 temp1 << "BJoystick::CountDevices(), number of Devices = " 188 << count; 189 temp1 = AddToList(fListView1, temp1.String()); 190 191 if (err != B_ERROR) { 192 BString name; 193 err = stick->GetControllerModule(&name); 194 temp1 << "BJoystick::GetControllerModule(), name = " 195 << name; 196 temp1 = AddToList(fListView1, temp1.String()); 197 198 if (name == "Legacy") { 199 bool b = stick->EnterEnhancedMode(); 200 if (b) { 201 temp1 << "BJoystick::EnterEnhancedMode(), OK"; 202 temp1 = AddToList(fListView1, temp1.String()); 203 } else { 204 temp1 << "BJoystick::EnterEnhancedMode(), Not OK"; 205 temp1 = AddToList(fListView1, temp1.String()); 206 } 207 } 208 209 err = stick->GetControllerName(&name); 210 temp1 << "BJoystick::GetControllerName(), name = " << name; 211 temp1 = AddToList(fListView1, temp1.String()); 212 if (err == B_OK) { 213 stick->Close(); 214 temp1 = AddToList(fListView1, "BJoystick::Close()"); 215 temp1 << i+1 << " " << name.String(); 216 temp1 = AddToList(fListView2, temp1.String()); 217 } else { 218 temp1 << "Error = " << strerror(err); 219 temp1 = AddToList(fListView1, temp1.String()); 220 temp1 << "*** Can't get name of controller " 221 << devName; 222 temp1 = AddToList(fListView1, temp1.String()); 223 } 224 } else { 225 temp1 << "Error = " << strerror(err) << "err nr = " << err; 226 temp1 = AddToList(fListView1, temp1.String()); 227 temp1 << "No controller on " << devName; 228 temp1 = AddToList(fListView1, temp1.String()); 229 } 230 } else { 231 temp1 << "Error = " << strerror(err); 232 temp1 = AddToList(fListView1, temp1.String()); 233 temp1 << "*** Error while reading controller list."; 234 temp1 = AddToList(fListView1, temp1.String()); 235 } 236 } 237 } else { 238 temp1 << "Error = " << strerror(err); 239 temp1 = AddToList(fListView1, temp1.String()); 240 temp1 = AddToList(fListView1, "*** No game ports detected."); 241 } 242 } 243