2 // profile.cpp - Global profile storage/definition/manipulation
5 // (C) 2013 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
8 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 05/01/2013 Created this file
13 // JLH 10/02/2014 Finally fixed stuff so it works the way it should
14 // JPM 06/06/2016 Visual Studio support
16 // This is a profile database with two parts: One, a list of devices, and two,
17 // a list of profiles each containing a pointer to the device list, and map
18 // name, a preferred slot #, and a key/button map. All the heavy lifting (incl.
19 // autoconnection of devices to profiles to slots) is done here.
21 // Basically, how this works is that we connect the device the user plugs into
22 // the computer to a profile in the database to a slot in the virtual Jaguar.
23 // Hopefully the configuration that the user gives us is sane enough for us to
24 // figure out how to do the right thing! By default, there is always a keyboard
25 // device plugged in; any other device that gets plugged in and wants to be in
26 // slot #0 can override it. This is so there is always a sane configuration if
27 // nothing is plugged in.
29 // Devices go into the database when the user plugs them in and runs VJ, and
30 // subsequently does anything to alter any of the existing profiles. Once a
31 // device has been seen, it can't be unseen!
41 //#define DEBUG_PROFILES
42 #define MAX_DEVICES 64
45 Profile profile
[MAX_PROFILES
];
46 Profile profileBackup
[MAX_PROFILES
];
47 int controller1Profile
;
48 int controller2Profile
;
53 char deviceNames
[MAX_DEVICES
][128];
54 static int numberOfProfilesSave
;
56 // This is so that new devices have something reasonable to show for default
57 uint32_t defaultMap
[21] = {
58 'S', 'X', 'Z', 'C', '-','7', '4', '1', '0', '8', '5', '2', '=', '9', '6',
59 '3', 'L', 'K', 'J', 'O', 'P'
63 // Function Prototypes
64 int ConnectProfileToDevice(int deviceNum
, int gamepadID
= -1);
65 int FindProfileForDevice(int deviceNum
, int preferred
, int * found
);
69 // These two functions are mainly to support the controller configuration GUI.
70 // Could just as easily go there as well (and be better placed there).
72 void SaveProfiles(void)
74 numberOfProfilesSave
= numberOfProfiles
;
75 memcpy(&profileBackup
, &profile
, sizeof(Profile
) * MAX_PROFILES
);
79 void RestoreProfiles(void)
81 memcpy(&profile
, &profileBackup
, sizeof(Profile
) * MAX_PROFILES
);
82 numberOfProfiles
= numberOfProfilesSave
;
86 void ReadProfiles(QSettings
* set
)
88 // Assume no profiles, until we read them
91 // There is always at least one device present, and it's the keyboard
92 // (hey, we're PC centric here ;-)
94 strcpy(deviceNames
[0], "Keyboard");
96 // Read the rest of the devices (if any)
97 numberOfDevices
+= set
->beginReadArray("devices");
99 for(int i
=1; i
<numberOfDevices
; i
++)
101 set
->setArrayIndex(i
- 1);
102 strcpy(deviceNames
[i
], set
->value("deviceName").toString().toUtf8().data());
103 #ifdef DEBUG_PROFILES
104 printf("Read device name: %s\n", deviceNames
[i
]);
109 numberOfProfiles
= set
->beginReadArray("profiles");
110 #ifdef DEBUG_PROFILES
111 printf("Number of profiles: %u\n", numberOfProfiles
);
114 for(int i
=0; i
<numberOfProfiles
; i
++)
116 set
->setArrayIndex(i
);
117 profile
[i
].device
= set
->value("deviceNum").toInt();
118 strcpy(profile
[i
].mapName
, set
->value("mapName").toString().toUtf8().data());
119 profile
[i
].preferredSlot
= set
->value("preferredSlot").toInt();
121 for(int j
=0; j
<21; j
++)
123 QString string
= QString("map%1").arg(j
);
124 profile
[i
].map
[j
] = set
->value(string
).toInt();
126 #ifdef DEBUG_PROFILES
127 printf("Profile #%u: device=%u (%s)\n", i
, profile
[i
].device
, deviceNames
[profile
[i
].device
]);
133 #ifdef DEBUG_PROFILES
134 printf("Number of profiles found: %u\n", numberOfProfiles
);
136 // Set up a reasonable default if no profiles were found
137 if (numberOfProfiles
== 0)
139 #ifdef DEBUG_PROFILES
140 printf("Setting up default profile...\n");
143 profile
[0].device
= 0; // Keyboard is always device #0
144 strcpy(profile
[0].mapName
, "Default");
145 profile
[0].preferredSlot
= CONTROLLER1
;
147 for(int i
=0; i
<21; i
++)
148 profile
[0].map
[i
] = defaultMap
[i
];
153 void WriteProfiles(QSettings
* set
)
156 // Don't write anything for now...
159 // NB: Should only do this if something changed; otherwise, no need to do
161 set
->beginWriteArray("devices");
163 for(int i
=1; i
<numberOfDevices
; i
++)
165 set
->setArrayIndex(i
- 1);
166 set
->setValue("deviceName", deviceNames
[i
]);
170 set
->beginWriteArray("profiles");
172 for(int i
=0; i
<numberOfProfiles
; i
++)
174 set
->setArrayIndex(i
);
175 set
->setValue("deviceNum", profile
[i
].device
);
176 set
->setValue("mapName", profile
[i
].mapName
);
177 set
->setValue("preferredSlot", profile
[i
].preferredSlot
);
179 for(int j
=0; j
<21; j
++)
181 QString string
= QString("map%1").arg(j
);
182 set
->setValue(string
, profile
[i
].map
[j
]);
190 int GetFreeProfile(void)
192 // Check for too many, return -1 if so
193 if (numberOfProfiles
== MAX_PROFILES
)
196 int profileNum
= numberOfProfiles
;
202 void DeleteProfile(int profileToDelete
)
205 if (profileToDelete
>= numberOfProfiles
)
208 // Trivial case: Profile at end of the array
209 if (profileToDelete
== (numberOfProfiles
- 1))
215 // memmove(dest, src, bytesToMove);
216 memmove(&profile
[profileToDelete
], &profile
[profileToDelete
+ 1], ((numberOfProfiles
- 1) - profileToDelete
) * sizeof(Profile
));
221 int FindDeviceNumberForName(const char * name
)
223 for(int i
=0; i
<numberOfDevices
; i
++)
225 if (strcmp(deviceNames
[i
], name
) == 0)
226 #ifdef DEBUG_PROFILES
228 printf("PROFILE: Found device #%i for name (%s)...\n", i
, name
);
231 #ifdef DEBUG_PROFILES
236 if (numberOfDevices
== MAX_DEVICES
)
239 #ifdef DEBUG_PROFILES
240 printf("Device '%s' not found, creating device...\n", name
);
242 // If the device wasn't found, it must be new; so add it to the list.
243 int deviceNum
= numberOfDevices
;
244 deviceNames
[deviceNum
][127] = 0;
245 strncpy(deviceNames
[deviceNum
], name
, 127);
252 int FindMappingsForDevice(int deviceNum
, QComboBox
* combo
)
256 for(int i
=0; i
<numberOfProfiles
; i
++)
258 //This should *never* be the case--all profiles in list are *good*
259 // if (profile[i].device == -1)
262 if (profile
[i
].device
== deviceNum
)
264 combo
->addItem(profile
[i
].mapName
, i
);
269 // If no mappings were found, create a default one for it
272 profile
[numberOfProfiles
].device
= deviceNum
;
273 strcpy(profile
[numberOfProfiles
].mapName
, "Default");
274 profile
[numberOfProfiles
].preferredSlot
= CONTROLLER1
;
276 for(int i
=0; i
<21; i
++)
277 profile
[numberOfProfiles
].map
[i
] = defaultMap
[i
];
279 combo
->addItem(profile
[numberOfProfiles
].mapName
, numberOfProfiles
);
289 int FindUsableProfiles(QComboBox
* combo
)
293 // Check for device #0 (keyboard) profiles first
294 for(int j
=0; j
<numberOfProfiles
; j
++)
296 // Check for device *and* usable configuration
297 if ((profile
[j
].device
== 0) && (profile
[j
].preferredSlot
))
299 combo
->addItem(QString("Keyboard::%1").arg(profile
[j
].mapName
), j
);
304 // Check for connected host devices next
305 for(int i
=0; i
<Gamepad::numJoysticks
; i
++)
307 int deviceNum
= FindDeviceNumberForName(Gamepad::GetJoystickName(i
));
309 for(int j
=0; j
<numberOfProfiles
; j
++)
311 if ((profile
[j
].device
== deviceNum
) && (profile
[j
].preferredSlot
))
313 combo
->addItem(QString("%1::%2").arg(Gamepad::GetJoystickName(i
)).arg(profile
[j
].mapName
), j
);
323 bool ConnectProfileToController(int profileNum
, int controllerNum
)
329 if (profile
[profileNum
].device
== -1)
332 if (controllerNum
< 0 || controllerNum
> 2)
335 uint32_t * dest
= (controllerNum
== 0 ? &vjs
.p1KeyBindings
[0] : &vjs
.p2KeyBindings
[0]);
337 for(int i
=0; i
<21; i
++)
338 dest
[i
] = profile
[profileNum
].map
[i
];
340 WriteLog("PROFILE: Successfully mapped device '%s' (%s) to controller #%u...\n", deviceNames
[profile
[profileNum
].device
], profile
[profileNum
].mapName
, controllerNum
);
346 One more stab at this...
348 - Connect keyboard to slot #0.
349 - Loop thru all connected devices. For each device:
350 - Grab all profiles for the device. For each profile:
351 - Check to see what its preferred device is.
352 - If PD is slot #0, see if slot is already taken (gamepadIDSlot1 != -1).
353 If not taken, take it; otherwise put in list to tell user to solve the
355 - If the slot is already taken and *it's the same device* as the one
356 we're looking at, set it in slot #1.
357 - If PD is slot #1, see if slot is already taken. If not, take it;
358 otherwise, put in list to tell user to solve conflict for us.
359 - If PD is slot #0 & #1, see if either is already taken. Try #0 first,
360 then try #1. If both are already taken, skip it. Do this *after* we've
361 connected devices with preferred slots.
363 void AutoConnectProfiles(void)
365 // int foundProfiles[MAX_PROFILES];
366 controller1Profile
= -1;
367 controller2Profile
= -1;
371 // Connect the keyboard automagically only if no gamepads are plugged in.
372 // Otherwise, check after all other devices have been checked, then try to
374 if (Gamepad::numJoysticks
== 0)
376 #ifdef DEBUG_PROFILES
377 printf("AutoConnect: Setting up keyboard...\n");
379 //NO! ConnectProfileToDevice(0);
381 #pragma message("Warning: !!! Need to set up scanning for multiple keyboard profiles !!!")
383 #warning "!!! Need to set up scanning for multiple keyboard profiles !!!"
385 ConnectProfileToController(0, 0);
389 // Connect the profiles that prefer a slot, if any.
390 // N.B.: Conflicts are detected, but ignored. 1st controller to grab a
391 // preferred slot gets it. :-P
392 for(int i
=0; i
<Gamepad::numJoysticks
; i
++)
394 int deviceNum
= FindDeviceNumberForName(Gamepad::GetJoystickName(i
));
395 // bool p1Overwriteable =
396 #ifdef DEBUG_PROFILES
397 printf("AutoConnect: Attempting to set up profile for device '%s' (%i)\n", Gamepad::GetJoystickName(i
), deviceNum
);
400 for(int j
=0; j
<numberOfProfiles
; j
++)
402 if (deviceNum
!= profile
[j
].device
)
405 int slot
= profile
[j
].preferredSlot
;
407 if (slot
== CONTROLLER1
)
409 if (gamepadIDSlot1
== -1)
410 controller1Profile
= j
, gamepadIDSlot1
= i
;
413 // Autoresolve simple conflict: two controllers sharing one
414 // profile mapped to slot #0.
415 if ((deviceNum
== profile
[controller1Profile
].device
) && (controller2Profile
== -1))
416 controller2Profile
= j
, gamepadIDSlot2
= i
;
418 ; // Alert user to conflict and ask to resolve
421 else if (slot
== CONTROLLER2
)
423 if (gamepadIDSlot2
== -1)
424 controller2Profile
= j
, gamepadIDSlot2
= i
;
427 // Autoresolve simple conflict: two controllers sharing one
428 // profile mapped to slot #1.
429 if ((deviceNum
== profile
[controller2Profile
].device
) && (controller1Profile
== -1))
430 controller1Profile
= j
, gamepadIDSlot1
= i
;
432 ; // Alert user to conflict and ask to resolve
438 // Connect the "don't care" states, if any. We don't roll it into the above,
439 // because it can override the profiles that have a definite preference.
440 // These should be lowest priority.
441 for(int i
=0; i
<Gamepad::numJoysticks
; i
++)
443 int deviceNum
= FindDeviceNumberForName(Gamepad::GetJoystickName(i
));
445 for(int j
=0; j
<numberOfProfiles
; j
++)
447 if (deviceNum
!= profile
[j
].device
)
450 int slot
= profile
[j
].preferredSlot
;
452 if (slot
== (CONTROLLER1
| CONTROLLER2
))
454 if (gamepadIDSlot1
== -1)
455 controller1Profile
= j
, gamepadIDSlot1
= i
;
456 else if (gamepadIDSlot2
== -1)
457 controller2Profile
= j
, gamepadIDSlot2
= i
;
462 // Connect the keyboard device (lowest priority)
463 // N.B.: The keyboard is always mapped to profile #0, so we can locate it
465 int slot
= profile
[0].preferredSlot
;
466 #ifdef DEBUG_PROFILES
467 printf("AutoConnect: Attempting to connect keyboard... (gamepadIDSlot1/2 = %i/%i)\n", gamepadIDSlot1
, gamepadIDSlot2
);
470 if ((slot
== CONTROLLER1
) && (gamepadIDSlot1
== -1))
471 controller1Profile
= 0;
472 else if ((slot
== CONTROLLER2
) && (gamepadIDSlot2
== -1))
473 controller2Profile
= 0;
474 else if (slot
== (CONTROLLER1
| CONTROLLER2
))
476 if (gamepadIDSlot1
== -1)
477 controller1Profile
= 0;
478 else if (gamepadIDSlot2
== -1)
479 controller2Profile
= 0;
482 #ifdef DEBUG_PROFILES
483 printf("AutoConnect: Profiles found: [%i, %i]\n", controller1Profile
, controller2Profile
);
485 // Finally, attempt to connect profiles to controllers
486 ConnectProfileToController(controller1Profile
, 0);
487 ConnectProfileToController(controller2Profile
, 1);
492 int ConnectProfileToDevice(int deviceNum
, int gamepadID
/*= -1*/)
494 // bool found1 = false;
495 // bool found2 = false;
496 int numberFoundForController1
= 0;
497 int numberFoundForController2
= 0;
499 for(int i
=0; i
<numberOfProfiles
; i
++)
501 // Skip profile if it's not our device
502 if (profile
[i
].device
!= deviceNum
)
505 if (profile
[i
].preferredSlot
& CONTROLLER1
)
507 controller1Profile
= i
;
508 gamepadIDSlot1
= gamepadID
;
510 numberFoundForController1
++;
513 if (profile
[i
].preferredSlot
& CONTROLLER2
)
515 controller2Profile
= i
;
516 gamepadIDSlot2
= gamepadID
;
518 numberFoundForController2
++;
523 return numberFoundForController1
+ numberFoundForController2
;
528 int FindProfileForDevice(int deviceNum
, int preferred
, int * found
)
532 for(int i
=0; i
<numberOfProfiles
; i
++)
534 // Return the profile only if it matches the passed in device and
535 // matches the passed in preference...
536 if ((profile
[i
].device
== deviceNum
) && (profile
[i
].preferredSlot
== preferred
))
537 found
[numFound
++] = i
;
545 // Also note that we have the intersection of three things here: One the one
546 // hand, we have the detected joysticks with their IDs (typically in the range
547 // of 0-7), we have our gamepad profiles and their IDs (typically can have up to
548 // 64 of them), and we have our gamepad slots that the detected joysticks can be
551 // So, when the user plugs in a gamepad, it gets a joystick ID, then the profile
552 // manager checks to see if a profile (or profiles) for it exists. If so, then
553 // it assigns that joystick ID to a gamepad slot, based upon what the user
554 // requested for that profile.
556 // A problem (perhaps) arises when you have more than one profile for a certain
557 // device, how do you know which one to use? Perhaps you have a field in the
558 // profile saying that you use this profile 1st, that one 2nd, and so on...
560 // Some use cases, and how to resolve them:
562 // - User has two of the same device, and plugs them both in. There is only one
563 // profile. In this case, the sane thing to do is ignore the "preferred slot"
564 // of the dialog and use the same profile for both controllers, and plug them
565 // both into slot #0 and #1.
566 // - User has one device, and plugs it in. There are two profiles. In this case,
567 // the profile chosen should be based upon the "preferred slot", with slot #0
568 // being the winner. If both profiles are set for slot #0, ask the user which
569 // profile to use, and set a flag in the profile to say that it is a preferred
570 // profile for that device.
571 // - In any case where there are conflicts, the user must be consulted and sane