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