1 /* 2 * Copyright 2007 Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 7 * Ryan Leavengood, leavengood@gmail.com 8 */ 9 10 11 #include "JoyWin.h" 12 #include "MessagedItem.h" 13 #include "MessageWin.h" 14 #include "CalibWin.h" 15 #include "Global.h" 16 17 #include <Box.h> 18 #include <Button.h> 19 #include <CheckBox.h> 20 #include <ListView.h> 21 #include <ScrollView.h> 22 #include <String.h> 23 #include <StringView.h> 24 #include <Application.h> 25 #include <View.h> 26 27 28 JoyWin::JoyWin(BRect frame, const char *title, window_look look, 29 window_feel feel, uint32 flags, uint32 workspace = B_CURRENT_WORKSPACE) 30 : BWindow(frame, title, look, feel, flags, workspace) 31 { 32 fProbeButton = new BButton(BRect(15.00, 260.00, 115.00, 285.00), "ProbeButton", "Probe", NULL); 33 fCalibrateButton = new BButton(BRect(270.00, 260.00, 370.00, 285.00), "CalibrateButton", "Calibrate", NULL); 34 35 fGamePortL = new BListView(BRect(15.00, 30.00, 145.00, 250.00), "gamePort"); 36 fGamePortL->SetSelectionMessage(new BMessage(PORT_SELECTED)); 37 fGamePortL->SetInvocationMessage(new BMessage(PORT_INVOKE)); 38 39 fConControllerL = new BListView(BRect(175.00,30.00,370.00,250.00),"conController"); 40 fConControllerL->SetSelectionMessage(new BMessage(JOY_SELECTED)); 41 fConControllerL->SetInvocationMessage(new BMessage(JOY_INVOKE)); 42 43 fGamePortS = new BStringView(BRect(15, 5, 160, 25), "gpString", "Game Port"); 44 fGamePortS->SetFont(be_bold_font); 45 fConControllerS = new BStringView(BRect(170, 5, 330, 25), "ccString", "Connected Controller"); 46 fConControllerS->SetFont(be_bold_font); 47 48 fCheckbox = new BCheckBox(BRect(131.00, 260.00, 227.00, 280.00), "Disabled", "Disabled", NULL); 49 fBox = new BBox( Bounds(),"box", B_FOLLOW_ALL, 50 B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE, 51 B_PLAIN_BORDER); 52 fBox->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 53 54 // Adding object 55 fBox->AddChild(fCheckbox); 56 57 fBox->AddChild(fGamePortS); 58 fBox->AddChild(fConControllerS); 59 60 // Add listViews with their scrolls 61 fBox->AddChild(new BScrollView("PortScroll", fGamePortL, 62 B_FOLLOW_LEFT | B_FOLLOW_TOP_BOTTOM, B_WILL_DRAW, false, true)); 63 fBox->AddChild(new BScrollView("ConScroll", fConControllerL, 64 B_FOLLOW_ALL, B_WILL_DRAW, false, true)); 65 66 fBox->AddChild(fProbeButton); 67 fBox->AddChild(fCalibrateButton); 68 69 AddChild(fBox); 70 71 // This resizable feature is not in R5 but. 72 // making horizontal resizing allows 73 // long joysticks names to be seen completelly 74 SetSizeLimits(400, 600, Bounds().Height(), Bounds().Height()); 75 76 /* Add all the devices */ 77 AddDevices(); 78 79 /* Add the joysticks specifications */ 80 AddJoysticks(); 81 } 82 83 84 void JoyWin::MessageReceived(BMessage *message) 85 { 86 switch(message->what) 87 { 88 case PORT_SELECTED: 89 break; 90 91 case PORT_INVOKE: 92 // Do we have any port? 93 94 // And some joysticks ? 95 96 // Probe! 97 PerformProbe(); 98 99 break; 100 101 case JOY_SELECTED: 102 break; 103 104 case JOY_INVOKE: 105 106 // Do we have a selected definition? 107 108 // And a suitale selected port? 109 110 // Calibrate it! 111 Calibrate(); 112 113 break; 114 115 default: 116 BWindow::MessageReceived(message); 117 break; 118 } 119 } 120 121 122 bool JoyWin::QuitRequested() 123 { 124 // Apply changes and save configurations etc etc 125 126 be_app->PostMessage(B_QUIT_REQUESTED); 127 128 return BWindow::QuitRequested(); 129 } 130 131 132 /* Initialization */ 133 status_t 134 JoyWin::AddDevices() 135 { 136 char name[B_FILE_NAME_LENGTH]; 137 int devId = 0; 138 MessagedItem* device; 139 BMessage* message; 140 BString str; 141 142 while (!fJoystick.GetDeviceName(devId, name, sizeof(name))) { 143 message = new BMessage(PORT_SELECTED); 144 message->AddString("devname", name); 145 // NOTE: Adding the index in the list might be useful. 146 147 // TODO: Change it with leaf path 148 str.SetTo(name); 149 //str = str.Remove(0, str.FindLast('/') ); 150 151 device = new MessagedItem(str.String(), message); 152 fGamePortL->AddItem(device); 153 154 devId++; 155 } 156 157 /* TODO: Add the Disabled devices */ 158 159 /* We do not have any Joystick dev */ 160 if (devId == 0) { 161 // keep track of it 162 // Alert it 163 164 return B_ERROR; 165 } 166 167 return B_OK; 168 } 169 170 171 status_t JoyWin::AddJoysticks() 172 { 173 return B_OK; 174 } 175 176 177 /* Management */ 178 status_t JoyWin::Calibrate() 179 { 180 CalibWin* calibw; 181 182 calibw = new CalibWin(BRect(100, 100, 500, 400), "Calibrate", 183 B_DOCUMENT_WINDOW_LOOK, 184 B_NORMAL_WINDOW_FEEL, 185 B_NOT_RESIZABLE | B_NOT_ZOOMABLE); 186 187 calibw->Show(); 188 189 return B_OK; 190 } 191 192 193 status_t JoyWin::PerformProbe() 194 { 195 MessageWin* mesgw; 196 197 // [...] 198 199 mesgw = new MessageWin(Frame(),"Probing", 200 B_MODAL_WINDOW_LOOK, 201 B_MODAL_APP_WINDOW_FEEL, 202 B_NOT_RESIZABLE | B_NOT_ZOOMABLE ); 203 204 mesgw->Show(); 205 mesgw->SetText("Looking for: X in port Y ..."); 206 return B_OK; 207 } 208 209 210 /* Configuration */ 211 status_t JoyWin::ApplyChanges() 212 { 213 return B_OK; 214 } 215 216 217 /* Configuration */ 218 status_t JoyWin::GetSettings() 219 { 220 return B_OK; 221 } 222 223