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)
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 "config.h"
nkeynes@1015
    29
nkeynes@1015
    30
static void hotkey_resume_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    31
static void hotkey_stop_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    32
static void hotkey_reset_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    33
static void hotkey_exit_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    34
static void hotkey_state_select_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    35
static void hotkey_state_save_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    36
static void hotkey_state_load_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown );
nkeynes@1015
    37
nkeynes@1015
    38
static char *set_current_state_filename(int filenum);
nkeynes@1015
    39
nkeynes@1015
    40
static char *current_save_state;
nkeynes@1015
    41
nkeynes@1015
    42
struct lxdream_config_entry hotkeys_config[] = {
nkeynes@1015
    43
        {"resume", N_("Resume emulation"), CONFIG_TYPE_KEY},
nkeynes@1015
    44
        {"stop", N_("Stop emulation"), CONFIG_TYPE_KEY},
nkeynes@1015
    45
        {"reset", N_("Reset emulator"), CONFIG_TYPE_KEY},
nkeynes@1015
    46
        {"exit", N_("Exit emulator"), CONFIG_TYPE_KEY},
nkeynes@1015
    47
        {"save", N_("Save current quick save"), CONFIG_TYPE_KEY},
nkeynes@1015
    48
        {"load", N_("Load current quick save"), CONFIG_TYPE_KEY},
nkeynes@1015
    49
        {"state1", N_("Select quick save state 1"), CONFIG_TYPE_KEY},
nkeynes@1015
    50
        {"state2", N_("Select quick save state 2"), CONFIG_TYPE_KEY},
nkeynes@1015
    51
        {"state3", N_("Select quick save state 3"), CONFIG_TYPE_KEY},
nkeynes@1015
    52
        {"state4", N_("Select quick save state 4"), CONFIG_TYPE_KEY},
nkeynes@1015
    53
        {"state5", N_("Select quick save state 5"), CONFIG_TYPE_KEY},
nkeynes@1015
    54
        {"state6", N_("Select quick save state 6"), CONFIG_TYPE_KEY},
nkeynes@1015
    55
        {"state7", N_("Select quick save state 7"), CONFIG_TYPE_KEY},
nkeynes@1015
    56
        {"state8", N_("Select quick save state 8"), CONFIG_TYPE_KEY},
nkeynes@1015
    57
        {"state9", N_("Select quick save state 9"), CONFIG_TYPE_KEY},
nkeynes@1015
    58
        {NULL, CONFIG_TYPE_NONE}
nkeynes@1015
    59
};
nkeynes@1015
    60
nkeynes@1015
    61
void hotkeys_init() {
nkeynes@1015
    62
nkeynes@1015
    63
    char *home = getenv("HOME");
nkeynes@1015
    64
    char *save_path = g_strdup_printf("%s/.lxdream", home);
nkeynes@1015
    65
    mkdir(save_path, S_IRWXU);
nkeynes@1015
    66
    g_free(save_path);
nkeynes@1015
    67
nkeynes@1015
    68
    set_current_state_filename(1);
nkeynes@1015
    69
nkeynes@1015
    70
    hotkeys_register_keys();
nkeynes@1015
    71
}
nkeynes@1015
    72
nkeynes@1015
    73
void hotkeys_register_keys()
nkeynes@1015
    74
{
nkeynes@1015
    75
    input_register_key(hotkeys_config[0].value, &hotkey_resume_callback, NULL, 0);
nkeynes@1015
    76
    input_register_key(hotkeys_config[1].value, &hotkey_stop_callback, NULL, 0);
nkeynes@1015
    77
    input_register_key(hotkeys_config[2].value, &hotkey_reset_callback, NULL, 0);
nkeynes@1015
    78
    input_register_key(hotkeys_config[3].value, &hotkey_exit_callback, NULL, 0);
nkeynes@1015
    79
    input_register_key(hotkeys_config[4].value, &hotkey_state_save_callback, NULL, 0);
nkeynes@1015
    80
    input_register_key(hotkeys_config[5].value, &hotkey_state_load_callback, NULL, 0);
nkeynes@1015
    81
    for (int i = 0; i < 9; i++)
nkeynes@1015
    82
    {
nkeynes@1015
    83
        input_register_key(hotkeys_config[6 + i].value, &hotkey_state_select_callback, NULL, i);
nkeynes@1015
    84
    }
nkeynes@1015
    85
}
nkeynes@1015
    86
nkeynes@1015
    87
void hotkeys_unregister_keys()
nkeynes@1015
    88
{
nkeynes@1015
    89
    input_unregister_key(hotkeys_config[0].value, &hotkey_resume_callback, NULL, 0);
nkeynes@1015
    90
    input_unregister_key(hotkeys_config[1].value, &hotkey_stop_callback, NULL, 0);
nkeynes@1015
    91
    input_unregister_key(hotkeys_config[2].value, &hotkey_reset_callback, NULL, 0);
nkeynes@1015
    92
    input_unregister_key(hotkeys_config[3].value, &hotkey_exit_callback, NULL, 0);
nkeynes@1015
    93
    input_unregister_key(hotkeys_config[4].value, &hotkey_state_save_callback, NULL, 0);
nkeynes@1015
    94
    input_unregister_key(hotkeys_config[5].value, &hotkey_state_load_callback, NULL, 0);
nkeynes@1015
    95
    for (int i = 0; i < 9; i++)
nkeynes@1015
    96
    {
nkeynes@1015
    97
        input_unregister_key(hotkeys_config[6 + i].value, &hotkey_state_select_callback, NULL, i);
nkeynes@1015
    98
    }
nkeynes@1015
    99
}
nkeynes@1015
   100
nkeynes@1015
   101
lxdream_config_entry_t hotkeys_get_config()
nkeynes@1015
   102
{
nkeynes@1015
   103
    return hotkeys_config;
nkeynes@1015
   104
}
nkeynes@1015
   105
nkeynes@1015
   106
nkeynes@1015
   107
static void hotkey_resume_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   108
{
nkeynes@1015
   109
    if (isKeyDown && !dreamcast_is_running() ) {
nkeynes@1019
   110
        gui_do_later(dreamcast_run);
nkeynes@1015
   111
    }
nkeynes@1015
   112
}
nkeynes@1015
   113
nkeynes@1015
   114
static void hotkey_stop_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   115
{
nkeynes@1015
   116
    if (isKeyDown && dreamcast_is_running() ) {
nkeynes@1019
   117
        gui_do_later(dreamcast_stop);
nkeynes@1015
   118
    }
nkeynes@1015
   119
}
nkeynes@1015
   120
nkeynes@1015
   121
static void hotkey_reset_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   122
{
nkeynes@1015
   123
    if (isKeyDown) {
nkeynes@1015
   124
        dreamcast_reset();
nkeynes@1015
   125
    }
nkeynes@1015
   126
}
nkeynes@1015
   127
nkeynes@1015
   128
static void hotkey_exit_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   129
{
nkeynes@1015
   130
    if (isKeyDown) {
nkeynes@1015
   131
        dreamcast_shutdown();
nkeynes@1015
   132
    }
nkeynes@1015
   133
    exit(0);
nkeynes@1015
   134
}
nkeynes@1015
   135
nkeynes@1015
   136
static void hotkey_state_select_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   137
{
nkeynes@1015
   138
    if (isKeyDown) {
nkeynes@1015
   139
        INFO("state select callback called (%d)", value);
nkeynes@1015
   140
        assert(value > 0 && value <= 9);
nkeynes@1015
   141
        set_current_state_filename(value);
nkeynes@1015
   142
    }
nkeynes@1015
   143
}
nkeynes@1015
   144
nkeynes@1015
   145
static void hotkey_state_save_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   146
{
nkeynes@1015
   147
    if (isKeyDown) {
nkeynes@1015
   148
        if (current_save_state != NULL)
nkeynes@1015
   149
            dreamcast_save_state(current_save_state);
nkeynes@1015
   150
    }
nkeynes@1015
   151
}
nkeynes@1015
   152
nkeynes@1015
   153
static void hotkey_state_load_callback( void *mdev, uint32_t value, uint32_t pressure, gboolean isKeyDown )
nkeynes@1015
   154
{
nkeynes@1015
   155
    if (isKeyDown) {
nkeynes@1015
   156
        if (current_save_state != NULL)
nkeynes@1015
   157
            dreamcast_load_state(current_save_state);
nkeynes@1015
   158
    }
nkeynes@1015
   159
}
nkeynes@1015
   160
nkeynes@1015
   161
static char *set_current_state_filename(int filenum)
nkeynes@1015
   162
{
nkeynes@1015
   163
    char *home = getenv("HOME");
nkeynes@1015
   164
    if (current_save_state != NULL)
nkeynes@1015
   165
        g_free(current_save_state);
nkeynes@1015
   166
    current_save_state = g_strdup_printf("%s/.lxdream/quicksave%d.dst", home, filenum);
nkeynes@1015
   167
}
nkeynes@1015
   168
.