4343
4444struct params {
4545 char * exec [NB_PARAMS_MAX ];
46+ char * keymap [NB_PARAMS_MAX ];
4647 bool needs_terminal , needs_joystick , needs_gsensor , needs_downscaling ;
4748};
4849
@@ -125,6 +126,15 @@ static int read_params(struct OPK *opk, struct params *params)
125126 continue ;
126127 }
127128
129+ if (!strncmp (key , "FK-Keymap" , skey )) {
130+ if (sval < NB_PARAMS_MAX ){
131+ strncpy (params -> keymap , val , sval );
132+ } else {
133+ fprintf (stderr , "FK-Keymap error: field length > %d\n" , NB_PARAMS_MAX );
134+ }
135+ continue ;
136+ }
137+
128138 if (!strncmp (key , "Terminal" , skey )) {
129139 params -> needs_terminal = !strncmp (val , "true" , sval );
130140 continue ;
@@ -398,8 +408,10 @@ int main(int argc, char **argv)
398408 return EXIT_FAILURE ;
399409 }
400410
411+ /* move back to OPK_MOUNTPOINT folder */
401412 chdir (OPK_MOUNTPOINT );
402413
414+ /* Enable params */
403415 if (params .needs_terminal )
404416 enable_vtcon ();
405417
@@ -412,6 +424,70 @@ int main(int argc, char **argv)
412424 if (params .needs_downscaling )
413425 enable_downscaling ();
414426
427+
428+ /* Apply keymaps if found */
429+ char * dirc = strdup (args [arg - 1 ]);
430+ char * dname = dirname (dirc );
431+ char * basec = strdup (args [arg - 1 ]);
432+ char * p , command [PATH_MAX ];
433+ FILE * fp ;
434+
435+ /* Initialize keymap command */
436+ strcpy (command , "keymap " );
437+
438+ /* Compute basename without extension */
439+ p = strrchr (basec , '.' );
440+ if (p ) {
441+ * p = '\0' ;
442+ }
443+
444+ /* Apply console (directory) keymap first, if any */
445+ sprintf (& command [7 ], "%s/default_config.key" , dname );
446+ //printf("console keymap command: \"%s\"\n", command);
447+ if (!access (& command [7 ], R_OK )) {
448+ fp = popen (command , "r" );
449+ if (fp != NULL ) {
450+ printf ("Applied console keymap command: \"%s\"\n" , command );
451+ pclose (fp );
452+ }
453+ else {
454+ fprintf (stderr , "WARNING: Cannot apply console keymap command: \"%s\"\n" , command );
455+ }
456+ }
457+
458+ /* Then apply OPk keymap, if any */
459+ if (params .keymap != NULL ) {
460+ sprintf (& command [7 ], "%s" , params .keymap );
461+ //printf("OPK keymap command: \"%s\"\n", command);
462+ if (!access (& command [7 ], R_OK )) {
463+ fp = popen (command , "r" );
464+ if (fp != NULL ) {
465+ printf ("Applied FK-Keymap command: \"%s\"\n" , command );
466+ pclose (fp );
467+ }
468+ else {
469+ fprintf (stderr , "WARNING: Cannot apply FK-Keymap command: \"%s\"\n" , command );
470+ }
471+ }
472+ }
473+
474+ /* Apply game keymap, if any */
475+ sprintf (& command [7 ], "%s.key" , basec );
476+ //printf("game keymap \"%s\"\n", command);
477+ if (!access (& command [7 ], R_OK )) {
478+ fp = popen (command , "r" );
479+ if (fp != NULL ) {
480+ printf ("Applied game keymap command: \"%s\"\n" , command );
481+ pclose (fp );
482+ }
483+ else {
484+ fprintf (stderr , "WARNING: Cannot apply game keymap command: \"%s\"\n" , command );
485+ }
486+ }
487+ free (dirc );
488+ free (basec );
489+
490+ /* Launch executable here */
415491 pid_t son = fork ();
416492 if (!son ) {
417493 /* Drop privileges */
@@ -422,17 +498,25 @@ int main(int argc, char **argv)
422498 execvp (args [0 ], args );
423499 }
424500
425- FILE * fd = fopen ("/var/run/funkey.pid" , "w" );
426- if (fd != NULL ) {
427- fprintf (fd , "%d\n" , son );
428- fclose (fd );
501+ fp = fopen ("/var/run/funkey.pid" , "w" );
502+ if (fp != NULL ) {
503+ fprintf (fp , "%d\n" , son );
504+ fclose (fp );
429505 }
430506
507+ /* Wait for son pid to finish */
431508 int status ;
432509 waitpid (son , & status , 0 );
433510
511+ /* move back to / folder */
434512 chdir ("/" );
435513
514+ /* Restore default keymap */
515+ fp = popen ("keymap default" , "r" );
516+ if (fp != NULL ) {
517+ pclose (fp );
518+ }
519+
436520 /** Multiple trials to unmount OPK_MOUNTPOINT */
437521 #define MAX_UMOUNT_TRIALS 100
438522 int retries = MAX_UMOUNT_TRIALS ;
0 commit comments