Search
lxdream.org :: lxdream/src/hotkeys.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/hotkeys.c
changeset 1024:c67f2d61ab97
prev1019:87f191f92f8f
next1038:f220d18c0615
author nkeynes
date Sat Jun 13 00:50:48 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Build drivers with library dependencies as shared objects (ie plugins)
view annotate diff log raw
     1 /**
     2  * $Id:  $
     3  *
     4  * Handles hotkeys for pause/continue, save states, quit, etc
     5  *
     6  * Copyright (c) 2009 wahrhaft
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #include <assert.h>
    19 #include <glib.h>
    20 #include <sys/stat.h>
    21 #include <stdint.h>
    22 #include <stdlib.h>
    23 #include "lxdream.h"
    24 #include "dreamcast.h"
    25 #include "display.h"
    26 #include "hotkeys.h"
    27 #include "gui.h"
    28 #include "config.h"
    30 static void hotkey_resume_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    31 static void hotkey_stop_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    32 static void hotkey_reset_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    33 static void hotkey_exit_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    34 static void hotkey_state_select_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    35 static void hotkey_state_save_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    36 static void hotkey_state_load_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
    38 static char *set_current_state_filename(int filenum);
    40 static char *current_save_state;
    42 struct lxdream_config_entry hotkeys_config[] = {
    43         {"resume", N_("Resume emulation"), CONFIG_TYPE_KEY},
    44         {"stop", N_("Stop emulation"), CONFIG_TYPE_KEY},
    45         {"reset", N_("Reset emulator"), CONFIG_TYPE_KEY},
    46         {"exit", N_("Exit emulator"), CONFIG_TYPE_KEY},
    47         {"save", N_("Save current quick save"), CONFIG_TYPE_KEY},
    48         {"load", N_("Load current quick save"), CONFIG_TYPE_KEY},
    49         {"state1", N_("Select quick save state 1"), CONFIG_TYPE_KEY},
    50         {"state2", N_("Select quick save state 2"), CONFIG_TYPE_KEY},
    51         {"state3", N_("Select quick save state 3"), CONFIG_TYPE_KEY},
    52         {"state4", N_("Select quick save state 4"), CONFIG_TYPE_KEY},
    53         {"state5", N_("Select quick save state 5"), CONFIG_TYPE_KEY},
    54         {"state6", N_("Select quick save state 6"), CONFIG_TYPE_KEY},
    55         {"state7", N_("Select quick save state 7"), CONFIG_TYPE_KEY},
    56         {"state8", N_("Select quick save state 8"), CONFIG_TYPE_KEY},
    57         {"state9", N_("Select quick save state 9"), CONFIG_TYPE_KEY},
    58         {NULL, CONFIG_TYPE_NONE}
    59 };
    61 void hotkeys_init() {
    63     char *home = getenv("HOME");
    64     char *save_path = g_strdup_printf("%s/.lxdream", home);
    65     mkdir(save_path, S_IRWXU);
    66     g_free(save_path);
    68     set_current_state_filename(1);
    70     hotkeys_register_keys();
    71 }
    73 void hotkeys_register_keys()
    74 {
    75     input_register_key(hotkeys_config[0].value, &hotkey_resume_callback, NULL, 0);
    76     input_register_key(hotkeys_config[1].value, &hotkey_stop_callback, NULL, 0);
    77     input_register_key(hotkeys_config[2].value, &hotkey_reset_callback, NULL, 0);
    78     input_register_key(hotkeys_config[3].value, &hotkey_exit_callback, NULL, 0);
    79     input_register_key(hotkeys_config[4].value, &hotkey_state_save_callback, NULL, 0);
    80     input_register_key(hotkeys_config[5].value, &hotkey_state_load_callback, NULL, 0);
    81     for (int i = 0; i < 9; i++)
    82     {
    83         input_register_key(hotkeys_config[6 + i].value, &hotkey_state_select_callback, NULL, i);
    84     }
    85 }
    87 void hotkeys_unregister_keys()
    88 {
    89     input_unregister_key(hotkeys_config[0].value, &hotkey_resume_callback, NULL, 0);
    90     input_unregister_key(hotkeys_config[1].value, &hotkey_stop_callback, NULL, 0);
    91     input_unregister_key(hotkeys_config[2].value, &hotkey_reset_callback, NULL, 0);
    92     input_unregister_key(hotkeys_config[3].value, &hotkey_exit_callback, NULL, 0);
    93     input_unregister_key(hotkeys_config[4].value, &hotkey_state_save_callback, NULL, 0);
    94     input_unregister_key(hotkeys_config[5].value, &hotkey_state_load_callback, NULL, 0);
    95     for (int i = 0; i < 9; i++)
    96     {
    97         input_unregister_key(hotkeys_config[6 + i].value, &hotkey_state_select_callback, NULL, i);
    98     }
    99 }
   101 lxdream_config_entry_t hotkeys_get_config()
   102 {
   103     return hotkeys_config;
   104 }
   107 static void hotkey_resume_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   108 {
   109     if (isKeyDown && !dreamcast_is_running() ) {
   110         gui_do_later(dreamcast_run);
   111     }
   112 }
   114 static void hotkey_stop_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   115 {
   116     if (isKeyDown && dreamcast_is_running() ) {
   117         gui_do_later(dreamcast_stop);
   118     }
   119 }
   121 static void hotkey_reset_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   122 {
   123     if (isKeyDown) {
   124         dreamcast_reset();
   125     }
   126 }
   128 static void hotkey_exit_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   129 {
   130     if (isKeyDown) {
   131         dreamcast_shutdown();
   132     }
   133     exit(0);
   134 }
   136 static void hotkey_state_select_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   137 {
   138     if (isKeyDown) {
   139         INFO("state select callback called (%d)", value);
   140         assert(value > 0 && value <= 9);
   141         set_current_state_filename(value);
   142     }
   143 }
   145 static void hotkey_state_save_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   146 {
   147     if (isKeyDown) {
   148         if (current_save_state != NULL)
   149             dreamcast_save_state(current_save_state);
   150     }
   151 }
   153 static void hotkey_state_load_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
   154 {
   155     if (isKeyDown) {
   156         if (current_save_state != NULL)
   157             dreamcast_load_state(current_save_state);
   158     }
   159 }
   161 static char *set_current_state_filename(int filenum)
   162 {
   163     char *home = getenv("HOME");
   164     if (current_save_state != NULL)
   165         g_free(current_save_state);
   166     current_save_state = g_strdup_printf("%s/.lxdream/quicksave%d.dst", home, filenum);
   167 }
.