2 // profile.cpp - Global profile storage/definition/manipulation
5 // (C) 2013 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 05/01/2013 Created this file
12 // JLH 10/02/2014 Finally fixed stuff so it works the way it should
14 // This is a profile database with two parts: One, a list of devices, and two,
15 // a list of profiles each containing a pointer to the device list, and map
16 // name, a preferred slot #, and a key/button map. All the heavy lifting (incl.
17 // autoconnection of devices to profiles to slots) is done here.
19 // Basically, how this works is that we connect the device the user plugs into
20 // the computer to a profile in the database to a slot in the virtual Jaguar.
21 // Hopefully the configuration that the user gives us is sane enough for us to
22 // figure out how to do the right thing! By default, there is always a keyboard
23 // device plugged in; any other device that gets plugged in and wants to be in
24 // slot #0 can override it. This is so there is always a sane configuration if
25 // nothing is plugged in.
27 // Devices go into the database when the user plugs them in and runs VJ, and
28 // subsequently does anything to alter any of the existing profiles. Once a
29 // device has been seen, it can't be unseen!
39 //#define DEBUG_PROFILES
40 #define MAX_DEVICES 64
43 Profile profile[MAX_PROFILES];
44 Profile profileBackup[MAX_PROFILES];
45 int controller1Profile;
46 int controller2Profile;
51 char deviceNames[MAX_DEVICES][128];
53 // This is so that new devices have something reasonable to show for default
54 uint32_t defaultMap[21] = {
55 'S', 'X', 'Z', 'C', '-','7', '4', '1', '0', '8', '5', '2', '=', '9', '6',
56 '3', 'L', 'K', 'J', 'O', 'P'
60 // Function Prototypes
61 int ConnectProfileToDevice(int deviceNum, int gamepadID = -1);
62 int FindProfileForDevice(int deviceNum, int preferred, int * found);
66 // These two functions are mainly to support the controller configuration GUI.
67 // Could just as easily go there as well (and be better placed there).
69 void SaveProfiles(void)
71 memcpy(&profileBackup, &profile, sizeof(Profile) * MAX_PROFILES);
75 void RestoreProfiles(void)
77 memcpy(&profile, &profileBackup, sizeof(Profile) * MAX_PROFILES);
81 void ReadProfiles(QSettings * set)
83 // Assume no profiles, until we read them
86 // There is always at least one device present, and it's the keyboard
87 // (hey, we're PC centric here ;-)
89 strcpy(deviceNames[0], "Keyboard");
91 // Read the rest of the devices (if any)
92 numberOfDevices += set->beginReadArray("devices");
94 for(int i=1; i<numberOfDevices; i++)
96 set->setArrayIndex(i - 1);
97 strcpy(deviceNames[i], set->value("deviceName").toString().toAscii().data());
99 printf("Read device name: %s\n", deviceNames[i]);
104 numberOfProfiles = set->beginReadArray("profiles");
105 #ifdef DEBUG_PROFILES
106 printf("Number of profiles: %u\n", numberOfProfiles);
109 for(int i=0; i<numberOfProfiles; i++)
111 set->setArrayIndex(i);
112 profile[i].device = set->value("deviceNum").toInt();
113 strcpy(profile[i].mapName, set->value("mapName").toString().toAscii().data());
114 profile[i].preferredSlot = set->value("preferredSlot").toInt();
116 for(int j=0; j<21; j++)
118 QString string = QString("map%1").arg(j);
119 profile[i].map[j] = set->value(string).toInt();
121 #ifdef DEBUG_PROFILES
122 printf("Profile #%u: device=%u (%s)\n", i, profile[i].device, deviceNames[profile[i].device]);
128 #ifdef DEBUG_PROFILES
129 printf("Number of profiles found: %u\n", numberOfProfiles);
131 // Set up a reasonable default if no profiles were found
132 if (numberOfProfiles == 0)
134 #ifdef DEBUG_PROFILES
135 printf("Setting up default profile...\n");
138 profile[0].device = 0; // Keyboard is always device #0
139 strcpy(profile[0].mapName, "Default");
140 profile[0].preferredSlot = CONTROLLER1;
142 for(int i=0; i<21; i++)
143 profile[0].map[i] = defaultMap[i];
148 void WriteProfiles(QSettings * set)
151 // Don't write anything for now...
154 // NB: Should only do this if something changed; otherwise, no need to do
156 set->beginWriteArray("devices");
158 for(int i=1; i<numberOfDevices; i++)
160 set->setArrayIndex(i - 1);
161 set->setValue("deviceName", deviceNames[i]);
165 set->beginWriteArray("profiles");
167 for(int i=0; i<numberOfProfiles; i++)
169 set->setArrayIndex(i);
170 set->setValue("deviceNum", profile[i].device);
171 set->setValue("mapName", profile[i].mapName);
172 set->setValue("preferredSlot", profile[i].preferredSlot);
174 for(int j=0; j<21; j++)
176 QString string = QString("map%1").arg(j);
177 set->setValue(string, profile[i].map[j]);
185 int GetFreeProfile(void)
187 // Check for too many, return -1 if so
188 if (numberOfProfiles == MAX_PROFILES)
191 int profileNum = numberOfProfiles;
197 void DeleteProfile(int profileToDelete)
200 if (profileToDelete >= numberOfProfiles)
203 // Trivial case: Profile at end of the array
204 if (profileToDelete == (numberOfProfiles - 1))
210 // memmove(dest, src, bytesToMove);
211 memmove(&profile[profileToDelete], &profile[profileToDelete + 1], ((numberOfProfiles - 1) - profileToDelete) * sizeof(Profile));
216 int FindDeviceNumberForName(const char * name)
218 for(int i=0; i<numberOfDevices; i++)
220 if (strcmp(deviceNames[i], name) == 0)
221 #ifdef DEBUG_PROFILES
223 printf("PROFILE: Found device #%i for name (%s)...\n", i, name);
226 #ifdef DEBUG_PROFILES
231 if (numberOfDevices == MAX_DEVICES)
234 #ifdef DEBUG_PROFILES
235 printf("Device '%s' not found, creating device...\n", name);
237 // If the device wasn't found, it must be new; so add it to the list.
238 int deviceNum = numberOfDevices;
239 deviceNames[deviceNum][127] = 0;
240 strncpy(deviceNames[deviceNum], name, 127);
247 int FindMappingsForDevice(int deviceNum, QComboBox * combo)
251 for(int i=0; i<numberOfProfiles; i++)
253 //This should *never* be the case--all profiles in list are *good*
254 // if (profile[i].device == -1)
257 if (profile[i].device == deviceNum)
259 combo->addItem(profile[i].mapName, i);
264 // If no mappings were found, create a default one for it
267 profile[numberOfProfiles].device = deviceNum;
268 strcpy(profile[numberOfProfiles].mapName, "Default");
269 profile[numberOfProfiles].preferredSlot = CONTROLLER1;
271 for(int i=0; i<21; i++)
272 profile[numberOfProfiles].map[i] = defaultMap[i];
274 combo->addItem(profile[numberOfProfiles].mapName, numberOfProfiles);
284 int FindUsableProfiles(QComboBox * combo)
288 // Check for device #0 (keyboard) profiles first
289 for(int j=0; j<numberOfProfiles; j++)
291 // Check for device *and* usable configuration
292 if ((profile[j].device == 0) && (profile[j].preferredSlot))
294 combo->addItem(QString("Keyboard::%1").arg(profile[j].mapName), j);
299 // Check for connected host devices next
300 for(int i=0; i<Gamepad::numJoysticks; i++)
302 int deviceNum = FindDeviceNumberForName(Gamepad::GetJoystickName(i));
304 for(int j=0; j<numberOfProfiles; j++)
306 if ((profile[j].device == deviceNum) && (profile[j].preferredSlot))
308 combo->addItem(QString("%1::%2").arg(Gamepad::GetJoystickName(i)).arg(profile[j].mapName), j);
318 bool ConnectProfileToController(int profileNum, int controllerNum)
324 if (profile[profileNum].device == -1)
327 if (controllerNum < 0 || controllerNum > 2)
330 uint32_t * dest = (controllerNum == 0 ? &vjs.p1KeyBindings[0] : &vjs.p2KeyBindings[0]);
332 for(int i=0; i<21; i++)
333 dest[i] = profile[profileNum].map[i];
335 WriteLog("PROFILE: Successfully mapped device '%s' (%s) to controller #%u...\n", deviceNames[profile[profileNum].device], profile[profileNum].mapName, controllerNum);
341 One more stab at this...
343 - Connect keyboard to slot #0.
344 - Loop thru all connected devices. For each device:
345 - Grab all profiles for the device. For each profile:
346 - Check to see what its preferred device is.
347 - If PD is slot #0, see if slot is already taken (gamepadIDSlot1 != -1).
348 If not taken, take it; otherwise put in list to tell user to solve the
350 - If the slot is already taken and *it's the same device* as the one
351 we're looking at, set it in slot #1.
352 - If PD is slot #1, see if slot is already taken. If not, take it;
353 otherwise, put in list to tell user to solve conflict for us.
354 - If PD is slot #0 & #1, see if either is already taken. Try #0 first,
355 then try #1. If both are already taken, skip it. Do this *after* we've
356 connected devices with preferred slots.
358 void AutoConnectProfiles(void)
360 int foundProfiles[MAX_PROFILES];
361 controller1Profile = -1;
362 controller2Profile = -1;
366 // Connect the keyboard automagically only if no gamepads are plugged in.
367 // Otherwise, check after all other devices have been checked, then try to
369 if (Gamepad::numJoysticks == 0)
371 ConnectProfileToDevice(0);
375 // Connect the profiles that prefer a slot, if any.
376 // N.B.: Conflicts are detected, but ignored. 1st controller to grab a
377 // preferred slot gets it. :-P
378 for(int i=0; i<Gamepad::numJoysticks; i++)
380 int deviceNum = FindDeviceNumberForName(Gamepad::GetJoystickName(i));
381 // bool p1Overwriteable =
383 for(int j=0; j<numberOfProfiles; j++)
385 if (deviceNum != profile[j].device)
388 int slot = profile[j].preferredSlot;
390 if (slot == CONTROLLER1)
392 if (gamepadIDSlot1 == -1)
393 controller1Profile = j, gamepadIDSlot1 = i;
396 // Autoresolve simple conflict: two controllers sharing one
397 // profile mapped to slot #0.
398 if ((deviceNum == profile[controller1Profile].device) && (controller2Profile == -1))
399 controller2Profile = j, gamepadIDSlot2 = i;
401 ; // Alert user to conflict and ask to resolve
404 else if (slot == CONTROLLER2)
406 if (gamepadIDSlot2 == -1)
407 controller2Profile = j, gamepadIDSlot2 = i;
410 // Autoresolve simple conflict: two controllers sharing one
411 // profile mapped to slot #1.
412 if ((deviceNum == profile[controller2Profile].device) && (controller1Profile == -1))
413 controller1Profile = j, gamepadIDSlot1 = i;
415 ; // Alert user to conflict and ask to resolve
421 // Connect the "don't care" states, if any. We don't roll it into the above,
422 // because it can override the profiles that have a definite preference.
423 // These should be lowest priority.
424 for(int i=0; i<Gamepad::numJoysticks; i++)
426 int deviceNum = FindDeviceNumberForName(Gamepad::GetJoystickName(i));
428 for(int j=0; j<numberOfProfiles; j++)
430 if (deviceNum != profile[j].device)
433 int slot = profile[j].preferredSlot;
435 if (slot == (CONTROLLER1 | CONTROLLER2))
437 if (gamepadIDSlot1 == -1)
438 controller1Profile = j, gamepadIDSlot1 = i;
439 else if (gamepadIDSlot2 == -1)
440 controller2Profile = j, gamepadIDSlot2 = i;
445 // Connect the keyboard device (lowest priority)
446 // N.B.: The keyboard is always mapped to profile #0, so we can locate it
448 int slot = profile[0].preferredSlot;
450 if ((slot == CONTROLLER1) && (gamepadIDSlot1 == -1))
451 controller1Profile = 0;
452 else if ((slot == CONTROLLER2) && (gamepadIDSlot2 == -1))
453 controller2Profile = 0;
454 else if (slot == (CONTROLLER1 | CONTROLLER2))
456 if (gamepadIDSlot1 == -1)
457 controller1Profile = 0;
458 else if (gamepadIDSlot2 == -1)
459 controller2Profile = 0;
462 // Finally, attempt to connect profiles to controllers
463 ConnectProfileToController(controller1Profile, 0);
464 ConnectProfileToController(controller2Profile, 1);
468 int ConnectProfileToDevice(int deviceNum, int gamepadID/*= -1*/)
470 // bool found1 = false;
471 // bool found2 = false;
472 int numberFoundForController1 = 0;
473 int numberFoundForController2 = 0;
475 for(int i=0; i<numberOfProfiles; i++)
477 // Skip profile if it's not our device
478 if (profile[i].device != deviceNum)
481 if (profile[i].preferredSlot & CONTROLLER1)
483 controller1Profile = i;
484 gamepadIDSlot1 = gamepadID;
486 numberFoundForController1++;
489 if (profile[i].preferredSlot & CONTROLLER2)
491 controller2Profile = i;
492 gamepadIDSlot2 = gamepadID;
494 numberFoundForController2++;
499 return numberFoundForController1 + numberFoundForController2;
504 int FindProfileForDevice(int deviceNum, int preferred, int * found)
508 for(int i=0; i<numberOfProfiles; i++)
510 // Return the profile only if it matches the passed in device and
511 // matches the passed in preference...
512 if ((profile[i].device == deviceNum) && (profile[i].preferredSlot == preferred))
513 found[numFound++] = i;
521 // Also note that we have the intersection of three things here: One the one
522 // hand, we have the detected joysticks with their IDs (typically in the range
523 // of 0-7), we have our gamepad profiles and their IDs (typically can have up to
524 // 64 of them), and we have our gamepad slots that the detected joysticks can be
527 // So, when the user plugs in a gamepad, it gets a joystick ID, then the profile
528 // manager checks to see if a profile (or profiles) for it exists. If so, then
529 // it assigns that joystick ID to a gamepad slot, based upon what the user
530 // requested for that profile.
532 // A problem (perhaps) arises when you have more than one profile for a certain
533 // device, how do you know which one to use? Perhaps you have a field in the
534 // profile saying that you use this profile 1st, that one 2nd, and so on...
536 // Some use cases, and how to resolve them:
538 // - User has two of the same device, and plugs them both in. There is only one
539 // profile. In this case, the sane thing to do is ignore the "preferred slot"
540 // of the dialog and use the same profile for both controllers, and plug them
541 // both into slot #0 and #1.
542 // - User has one device, and plugs it in. There are two profiles. In this case,
543 // the profile chosen should be based upon the "preferred slot", with slot #0
544 // being the winner. If both profiles are set for slot #0, ask the user which
545 // profile to use, and set a flag in the profile to say that it is a preferred
546 // profile for that device.
547 // - In any case where there are conflicts, the user must be consulted and sane