Search
lxdream.org :: lxdream/src/hotkeys.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/hotkeys.c
changeset 1015:ad448bedc48a
next1019:87f191f92f8f
author nkeynes
date Tue Jun 02 23:16:26 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change "MythTV" patch from Wahrhaft, thanks!:
* Add support for LIRC input devices
* Add hotkey input support
* Add command-line option for immediate fullscreen
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hotkeys.c Tue Jun 02 23:16:26 2009 +0000
1.3 @@ -0,0 +1,173 @@
1.4 +/**
1.5 + * $Id: $
1.6 + *
1.7 + * Handles hotkeys for pause/continue, save states, quit, etc
1.8 + *
1.9 + * Copyright (c) 2009 wahrhaft
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +#include <assert.h>
1.22 +#include <glib.h>
1.23 +#include <sys/stat.h>
1.24 +#include <stdint.h>
1.25 +#include <stdlib.h>
1.26 +#include "lxdream.h"
1.27 +#include "dreamcast.h"
1.28 +#include "display.h"
1.29 +#include "hotkeys.h"
1.30 +#include "gui.h"
1.31 +#include "drivers/input_lirc.h"
1.32 +#include "config.h"
1.33 +
1.34 +static void hotkey_resume_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.35 +static void hotkey_stop_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.36 +static void hotkey_reset_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.37 +static void hotkey_exit_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.38 +static void hotkey_state_select_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.39 +static void hotkey_state_save_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.40 +static void hotkey_state_load_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
1.41 +
1.42 +static char *set_current_state_filename(int filenum);
1.43 +
1.44 +static char *current_save_state;
1.45 +
1.46 +struct lxdream_config_entry hotkeys_config[] = {
1.47 + {"resume", N_("Resume emulation"), CONFIG_TYPE_KEY},
1.48 + {"stop", N_("Stop emulation"), CONFIG_TYPE_KEY},
1.49 + {"reset", N_("Reset emulator"), CONFIG_TYPE_KEY},
1.50 + {"exit", N_("Exit emulator"), CONFIG_TYPE_KEY},
1.51 + {"save", N_("Save current quick save"), CONFIG_TYPE_KEY},
1.52 + {"load", N_("Load current quick save"), CONFIG_TYPE_KEY},
1.53 + {"state1", N_("Select quick save state 1"), CONFIG_TYPE_KEY},
1.54 + {"state2", N_("Select quick save state 2"), CONFIG_TYPE_KEY},
1.55 + {"state3", N_("Select quick save state 3"), CONFIG_TYPE_KEY},
1.56 + {"state4", N_("Select quick save state 4"), CONFIG_TYPE_KEY},
1.57 + {"state5", N_("Select quick save state 5"), CONFIG_TYPE_KEY},
1.58 + {"state6", N_("Select quick save state 6"), CONFIG_TYPE_KEY},
1.59 + {"state7", N_("Select quick save state 7"), CONFIG_TYPE_KEY},
1.60 + {"state8", N_("Select quick save state 8"), CONFIG_TYPE_KEY},
1.61 + {"state9", N_("Select quick save state 9"), CONFIG_TYPE_KEY},
1.62 + {NULL, CONFIG_TYPE_NONE}
1.63 +};
1.64 +
1.65 +void hotkeys_init() {
1.66 +
1.67 +#ifdef HAVE_LIRC
1.68 + input_lirc_create();
1.69 +#endif
1.70 +
1.71 + char *home = getenv("HOME");
1.72 + char *save_path = g_strdup_printf("%s/.lxdream", home);
1.73 + mkdir(save_path, S_IRWXU);
1.74 + g_free(save_path);
1.75 +
1.76 + set_current_state_filename(1);
1.77 +
1.78 + hotkeys_register_keys();
1.79 +}
1.80 +
1.81 +void hotkeys_register_keys()
1.82 +{
1.83 + input_register_key(hotkeys_config[0].value, &hotkey_resume_callback, NULL, 0);
1.84 + input_register_key(hotkeys_config[1].value, &hotkey_stop_callback, NULL, 0);
1.85 + input_register_key(hotkeys_config[2].value, &hotkey_reset_callback, NULL, 0);
1.86 + input_register_key(hotkeys_config[3].value, &hotkey_exit_callback, NULL, 0);
1.87 + input_register_key(hotkeys_config[4].value, &hotkey_state_save_callback, NULL, 0);
1.88 + input_register_key(hotkeys_config[5].value, &hotkey_state_load_callback, NULL, 0);
1.89 + for (int i = 0; i < 9; i++)
1.90 + {
1.91 + input_register_key(hotkeys_config[6 + i].value, &hotkey_state_select_callback, NULL, i);
1.92 + }
1.93 +}
1.94 +
1.95 +void hotkeys_unregister_keys()
1.96 +{
1.97 + input_unregister_key(hotkeys_config[0].value, &hotkey_resume_callback, NULL, 0);
1.98 + input_unregister_key(hotkeys_config[1].value, &hotkey_stop_callback, NULL, 0);
1.99 + input_unregister_key(hotkeys_config[2].value, &hotkey_reset_callback, NULL, 0);
1.100 + input_unregister_key(hotkeys_config[3].value, &hotkey_exit_callback, NULL, 0);
1.101 + input_unregister_key(hotkeys_config[4].value, &hotkey_state_save_callback, NULL, 0);
1.102 + input_unregister_key(hotkeys_config[5].value, &hotkey_state_load_callback, NULL, 0);
1.103 + for (int i = 0; i < 9; i++)
1.104 + {
1.105 + input_unregister_key(hotkeys_config[6 + i].value, &hotkey_state_select_callback, NULL, i);
1.106 + }
1.107 +}
1.108 +
1.109 +lxdream_config_entry_t hotkeys_get_config()
1.110 +{
1.111 + return hotkeys_config;
1.112 +}
1.113 +
1.114 +
1.115 +static void hotkey_resume_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.116 +{
1.117 + if (isKeyDown && !dreamcast_is_running() ) {
1.118 + gui_run_later();
1.119 + }
1.120 +}
1.121 +
1.122 +static void hotkey_stop_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.123 +{
1.124 + if (isKeyDown && dreamcast_is_running() ) {
1.125 + dreamcast_stop();
1.126 + }
1.127 +}
1.128 +
1.129 +static void hotkey_reset_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.130 +{
1.131 + if (isKeyDown) {
1.132 + dreamcast_reset();
1.133 + }
1.134 +}
1.135 +
1.136 +static void hotkey_exit_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.137 +{
1.138 + if (isKeyDown) {
1.139 + dreamcast_shutdown();
1.140 + }
1.141 + exit(0);
1.142 +}
1.143 +
1.144 +static void hotkey_state_select_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.145 +{
1.146 + if (isKeyDown) {
1.147 + INFO("state select callback called (%d)", value);
1.148 + assert(value > 0 && value <= 9);
1.149 + set_current_state_filename(value);
1.150 + }
1.151 +}
1.152 +
1.153 +static void hotkey_state_save_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.154 +{
1.155 + if (isKeyDown) {
1.156 + if (current_save_state != NULL)
1.157 + dreamcast_save_state(current_save_state);
1.158 + }
1.159 +}
1.160 +
1.161 +static void hotkey_state_load_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
1.162 +{
1.163 + if (isKeyDown) {
1.164 + if (current_save_state != NULL)
1.165 + dreamcast_load_state(current_save_state);
1.166 + }
1.167 +}
1.168 +
1.169 +static char *set_current_state_filename(int filenum)
1.170 +{
1.171 + char *home = getenv("HOME");
1.172 + if (current_save_state != NULL)
1.173 + g_free(current_save_state);
1.174 + current_save_state = g_strdup_printf("%s/.lxdream/quicksave%d.dst", home, filenum);
1.175 +}
1.176 +
.