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];
52 static int numberOfProfilesSave;
54 // This is so that new devices have something reasonable to show for default
55 uint32_t defaultMap[21] = {
56 'S', 'X', 'Z', 'C', '-','7', '4', '1', '0', '8', '5', '2', '=', '9', '6',
57 '3', 'L', 'K', 'J', 'O', 'P'
61 // Function Prototypes
62 int ConnectProfileToDevice(int deviceNum, int gamepadID = -1);
63 int FindProfileForDevice(int deviceNum, int preferred, int * found);
67 // These two functions are mainly to support the controller configuration GUI.
68 // Could just as easily go there as well (and be better placed there).
70 void SaveProfiles(void)
72 numberOfProfilesSave = numberOfProfiles;
73 memcpy(&profileBackup, &profile, sizeof(Profile) * MAX_PROFILES);
77 void RestoreProfiles(void)
79 memcpy(&profile, &profileBackup, sizeof(Profile) * MAX_PROFILES);
80 numberOfProfiles = numberOfProfilesSave;
84 void ReadProfiles(QSettings * set)
86 // Assume no profiles, until we read them
89 // There is always at least one device present, and it's the keyboard
90 // (hey, we're PC centric here ;-)
92 strcpy(deviceNames[0], "Keyboard");
94 // Read the rest of the devices (if any)
95 numberOfDevices += set->beginReadArray("devices");
97 for(int i=1; i<numberOfDevices; i++)
99 set->setArrayIndex(i - 1);
100 strcpy(deviceNames[i], set->value("deviceName").toString().toUtf8().data());
101 #ifdef DEBUG_PROFILES
102 printf("Read device name: %s\n", deviceNames[i]);
107 numberOfProfiles = set->beginReadArray("profiles");
108 #ifdef DEBUG_PROFILES
109 printf("Number of profiles: %u\n", numberOfProfiles);
112 for(int i=0; i<numberOfProfiles; i++)
114 set->setArrayIndex(i);
115 profile[i].device = set->value("deviceNum").toInt();
116 strcpy(profile[i].mapName, set->value("mapName").toString().toUtf8().data());
117 profile[i].preferredSlot = set->value("preferredSlot").toInt();
119 for(int j=0; j<21; j++)
121 QString string = QString("map%1").arg(j);
122 profile[i].map[j] = set->value(string).toInt();
124 #ifdef DEBUG_PROFILES
125 printf("Profile #%u: device=%u (%s)\n", i, profile[i].device, deviceNames[profile[i].device]);
131 #ifdef DEBUG_PROFILES
132 printf("Number of profiles found: %u\n", numberOfProfiles);
134 // Set up a reasonable default if no profiles were found
135 if (numberOfProfiles == 0)
137 #ifdef DEBUG_PROFILES
138 printf("Setting up default profile...\n");
141 profile[0].device = 0; // Keyboard is always device #0
142 strcpy(profile[0].mapName, "Default");
143 profile[0].preferredSlot = CONTROLLER1;
145 for(int i=0; i<21; i++)
146 profile[0].map[i] = defaultMap[i];
151 void WriteProfiles(QSettings * set)
154 // Don't write anything for now...
157 // NB: Should only do this if something changed; otherwise, no need to do
159 set->beginWriteArray("devices");
161 for(int i=1; i<numberOfDevices; i++)
163 set->setArrayIndex(i - 1);
164 set->setValue("deviceName", deviceNames[i]);
168 set->beginWriteArray("profiles");
170 for(int i=0; i<numberOfProfiles; i++)
172 set->setArrayIndex(i);
173 set->setValue("deviceNum", profile[i].device);
174 set->setValue("mapName", profile[i].mapName);
175 set->setValue("preferredSlot", profile[i].preferredSlot);
177 for(int j=0; j<21; j++)
179 QString string = QString("map%1").arg(j);
180 set->setValue(string, profile[i].map[j]);
188 int GetFreeProfile(void)
190 // Check for too many, return -1 if so
191 if (numberOfProfiles == MAX_PROFILES)
194 int profileNum = numberOfProfiles;
200 void DeleteProfile(int profileToDelete)
203 if (profileToDelete >= numberOfProfiles)
206 // Trivial case: Profile at end of the array
207 if (profileToDelete == (numberOfProfiles - 1))
213 // memmove(dest, src, bytesToMove);
214 memmove(&profile[profileToDelete], &profile[profileToDelete + 1], ((numberOfProfiles - 1) - profileToDelete) * sizeof(Profile));
219 int FindDeviceNumberForName(const char * name)
221 for(int i=0; i<numberOfDevices; i++)
223 if (strcmp(deviceNames[i], name) == 0)
224 #ifdef DEBUG_PROFILES
226 printf("PROFILE: Found device #%i for name (%s)...\n", i, name);
229 #ifdef DEBUG_PROFILES
234 if (numberOfDevices == MAX_DEVICES)
237 #ifdef DEBUG_PROFILES
238 printf("Device '%s' not found, creating device...\n", name);
240 // If the device wasn't found, it must be new; so add it to the list.
241 int deviceNum = numberOfDevices;
242 deviceNames[deviceNum][127] = 0;
243 strncpy(deviceNames[deviceNum], name, 127);
250 int FindMappingsForDevice(int deviceNum, QComboBox * combo)
254 for(int i=0; i<numberOfProfiles; i++)
256 //This should *never* be the case--all profiles in list are *good*
257 // if (profile[i].device == -1)
260 if (profile[i].device == deviceNum)
262 combo->addItem(profile[i].mapName, i);
267 // If no mappings were found, create a default one for it
270 profile[numberOfProfiles].device = deviceNum;
271 strcpy(profile[numberOfProfiles].mapName, "Default");
272 profile[numberOfProfiles].preferredSlot = CONTROLLER1;
274 for(int i=0; i<21; i++)
275 profile[numberOfProfiles].map[i] = defaultMap[i];
277 combo->addItem(profile[numberOfProfiles].mapName, numberOfProfiles);
287 int FindUsableProfiles(QComboBox * combo)
291 // Check for device #0 (keyboard) profiles first
292 for(int j=0; j<numberOfProfiles; j++)
294 // Check for device *and* usable configuration
295 if ((profile[j].device == 0) && (profile[j].preferredSlot))
297 combo->addItem(QString("Keyboard::%1").arg(profile[j].mapName), j);
302 // Check for connected host devices next
303 for(int i=0; i<Gamepad::numJoysticks; i++)
305 int deviceNum = FindDeviceNumberForName(Gamepad::GetJoystickName(i));
307 for(int j=0; j<numberOfProfiles; j++)
309 if ((profile[j].device == deviceNum) && (profile[j].preferredSlot))
311 combo->addItem(QString("%1::%2").arg(Gamepad::GetJoystickName(i)).arg(profile[j].mapName), j);
321 bool ConnectProfileToController(int profileNum, int controllerNum)
327 if (profile[profileNum].device == -1)
330 if (controllerNum < 0 || controllerNum > 2)
333 uint32_t * dest = (controllerNum == 0 ? &vjs.p1KeyBindings[0] : &vjs.p2KeyBindings[0]);
335 for(int i=0; i<21; i++)
336 dest[i] = profile[profileNum].map[i];
338 WriteLog("PROFILE: Successfully mapped device '%s' (%s) to controller #%u...\n", deviceNames[profile[profileNum].device], profile[profileNum].mapName, controllerNum);
344 One more stab at this...
346 - Connect keyboard to slot #0.
347 - Loop thru all connected devices. For each device:
348 - Grab all profiles for the device. For each profile:
349 - Check to see what its preferred device is.
350 - If PD is slot #0, see if slot is already taken (gamepadIDSlot1 != -1).
351 If not taken, take it; otherwise put in list to tell user to solve the
353 - If the slot is already taken and *it's the same device* as the one
354 we're looking at, set it in slot #1.
355 - If PD is slot #1, see if slot is already taken. If not, take it;
356 otherwise, put in list to tell user to solve conflict for us.
357 - If PD is slot #0 & #1, see if either is already taken. Try #0 first,
358 then try #1. If both are already taken, skip it. Do this *after* we've
359 connected devices with preferred slots.
361 void AutoConnectProfiles(void)
363 // int foundProfiles[MAX_PROFILES];
364 controller1Profile = -1;
365 controller2Profile = -1;
369 // Connect the keyboard automagically only if no gamepads are plugged in.
370 // Otherwise, check after all other devices have been checked, then try to
372 if (Gamepad::numJoysticks == 0)
374 #ifdef DEBUG_PROFILES
375 printf("AutoConnect: Setting up keyboard...\n");
377 //NO! ConnectProfileToDevice(0);
378 #warning "!!! Need to set up scanning for multiple keyboard profiles !!!"
379 ConnectProfileToController(0, 0);
383 // Connect the profiles that prefer a slot, if any.
384 // N.B.: Conflicts are detected, but ignored. 1st controller to grab a
385 // preferred slot gets it. :-P
386 for(int i=0; i<Gamepad::numJoysticks; i++)
388 int deviceNum = FindDeviceNumberForName(Gamepad::GetJoystickName(i));
389 // bool p1Overwriteable =
390 #ifdef DEBUG_PROFILES
391 printf("AutoConnect: Attempting to set up profile for device '%s' (%i)\n", Gamepad::GetJoystickName(i), deviceNum);
394 for(int j=0; j<numberOfProfiles; j++)
396 if (deviceNum != profile[j].device)
399 int slot = profile[j].preferredSlot;
401 if (slot == CONTROLLER1)
403 if (gamepadIDSlot1 == -1)
404 controller1Profile = j, gamepadIDSlot1 = i;
407 // Autoresolve simple conflict: two controllers sharing one
408 // profile mapped to slot #0.
409 if ((deviceNum == profile[controller1Profile].device) && (controller2Profile == -1))
410 controller2Profile = j, gamepadIDSlot2 = i;
412 ; // Alert user to conflict and ask to resolve
415 else if (slot == CONTROLLER2)
417 if (gamepadIDSlot2 == -1)
418 controller2Profile = j, gamepadIDSlot2 = i;
421 // Autoresolve simple conflict: two controllers sharing one
422 // profile mapped to slot #1.
423 if ((deviceNum == profile[controller2Profile].device) && (controller1Profile == -1))
424 controller1Profile = j, gamepadIDSlot1 = i;
426 ; // Alert user to conflict and ask to resolve
432 // Connect the "don't care" states, if any. We don't roll it into the above,
433 // because it can override the profiles that have a definite preference.
434 // These should be lowest priority.
435 for(int i=0; i<Gamepad::numJoysticks; i++)
437 int deviceNum = FindDeviceNumberForName(Gamepad::GetJoystickName(i));
439 for(int j=0; j<numberOfProfiles; j++)
441 if (deviceNum != profile[j].device)
444 int slot = profile[j].preferredSlot;
446 if (slot == (CONTROLLER1 | CONTROLLER2))
448 if (gamepadIDSlot1 == -1)
449 controller1Profile = j, gamepadIDSlot1 = i;
450 else if (gamepadIDSlot2 == -1)
451 controller2Profile = j, gamepadIDSlot2 = i;
456 // Connect the keyboard device (lowest priority)
457 // N.B.: The keyboard is always mapped to profile #0, so we can locate it
459 int slot = profile[0].preferredSlot;
460 #ifdef DEBUG_PROFILES
461 printf("AutoConnect: Attempting to connect keyboard... (gamepadIDSlot1/2 = %i/%i)\n", gamepadIDSlot1, gamepadIDSlot2);
464 if ((slot == CONTROLLER1) && (gamepadIDSlot1 == -1))
465 controller1Profile = 0;
466 else if ((slot == CONTROLLER2) && (gamepadIDSlot2 == -1))
467 controller2Profile = 0;
468 else if (slot == (CONTROLLER1 | CONTROLLER2))
470 if (gamepadIDSlot1 == -1)
471 controller1Profile = 0;
472 else if (gamepadIDSlot2 == -1)
473 controller2Profile = 0;
476 #ifdef DEBUG_PROFILES
477 printf("AutoConnect: Profiles found: [%i, %i]\n", controller1Profile, controller2Profile);
479 // Finally, attempt to connect profiles to controllers
480 ConnectProfileToController(controller1Profile, 0);
481 ConnectProfileToController(controller2Profile, 1);
486 int ConnectProfileToDevice(int deviceNum, int gamepadID/*= -1*/)
488 // bool found1 = false;
489 // bool found2 = false;
490 int numberFoundForController1 = 0;
491 int numberFoundForController2 = 0;
493 for(int i=0; i<numberOfProfiles; i++)
495 // Skip profile if it's not our device
496 if (profile[i].device != deviceNum)
499 if (profile[i].preferredSlot & CONTROLLER1)
501 controller1Profile = i;
502 gamepadIDSlot1 = gamepadID;
504 numberFoundForController1++;
507 if (profile[i].preferredSlot & CONTROLLER2)
509 controller2Profile = i;
510 gamepadIDSlot2 = gamepadID;
512 numberFoundForController2++;
517 return numberFoundForController1 + numberFoundForController2;
522 int FindProfileForDevice(int deviceNum, int preferred, int * found)
526 for(int i=0; i<numberOfProfiles; i++)
528 // Return the profile only if it matches the passed in device and
529 // matches the passed in preference...
530 if ((profile[i].device == deviceNum) && (profile[i].preferredSlot == preferred))
531 found[numFound++] = i;
539 // Also note that we have the intersection of three things here: One the one
540 // hand, we have the detected joysticks with their IDs (typically in the range
541 // of 0-7), we have our gamepad profiles and their IDs (typically can have up to
542 // 64 of them), and we have our gamepad slots that the detected joysticks can be
545 // So, when the user plugs in a gamepad, it gets a joystick ID, then the profile
546 // manager checks to see if a profile (or profiles) for it exists. If so, then
547 // it assigns that joystick ID to a gamepad slot, based upon what the user
548 // requested for that profile.
550 // A problem (perhaps) arises when you have more than one profile for a certain
551 // device, how do you know which one to use? Perhaps you have a field in the
552 // profile saying that you use this profile 1st, that one 2nd, and so on...
554 // Some use cases, and how to resolve them:
556 // - User has two of the same device, and plugs them both in. There is only one
557 // profile. In this case, the sane thing to do is ignore the "preferred slot"
558 // of the dialog and use the same profile for both controllers, and plug them
559 // both into slot #0 and #1.
560 // - User has one device, and plugs it in. There are two profiles. In this case,
561 // the profile chosen should be based upon the "preferred slot", with slot #0
562 // being the winner. If both profiles are set for slot #0, ask the user which
563 // profile to use, and set a flag in the profile to say that it is a preferred
564 // profile for that device.
565 // - In any case where there are conflicts, the user must be consulted and sane