RetroArch/input/x11_input.c

316 lines
7.9 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2011-03-13 03:51:09 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-03-13 03:51:09 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-03-13 03:51:09 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-03-13 03:51:09 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2012-09-22 07:57:04 +00:00
#include "input_common.h"
2011-03-13 03:51:09 +00:00
2012-09-28 20:38:42 +00:00
#include "../driver.h"
#include "../boolean.h"
#include "../general.h"
#include <stdint.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
typedef struct x11_input
{
const rarch_joypad_driver_t *joypad;
Display *display;
Window win;
char state[32];
bool mouse_l, mouse_r, mouse_m;
int mouse_x, mouse_y;
int mouse_last_x, mouse_last_y;
} x11_input_t;
2011-03-13 11:02:06 +00:00
struct key_bind
{
unsigned x;
enum retro_key sk;
2011-03-13 11:02:06 +00:00
};
static unsigned keysym_lut[RETROK_LAST];
2011-03-13 11:02:06 +00:00
static const struct key_bind lut_binds[] = {
{ XK_Left, RETROK_LEFT },
{ XK_Right, RETROK_RIGHT },
{ XK_Up, RETROK_UP },
{ XK_Down, RETROK_DOWN },
{ XK_Return, RETROK_RETURN },
{ XK_Tab, RETROK_TAB },
{ XK_Insert, RETROK_INSERT },
{ XK_Home, RETROK_HOME },
{ XK_End, RETROK_END },
{ XK_Page_Up, RETROK_PAGEUP },
{ XK_Page_Down, RETROK_PAGEDOWN },
{ XK_Delete, RETROK_DELETE },
{ XK_Shift_R, RETROK_RSHIFT },
{ XK_Shift_L, RETROK_LSHIFT },
{ XK_Control_L, RETROK_LCTRL },
{ XK_Alt_L, RETROK_LALT },
{ XK_space, RETROK_SPACE },
{ XK_Escape, RETROK_ESCAPE },
{ XK_BackSpace, RETROK_BACKSPACE },
{ XK_KP_Enter, RETROK_KP_ENTER },
{ XK_KP_Add, RETROK_KP_PLUS },
{ XK_KP_Subtract, RETROK_KP_MINUS },
{ XK_KP_Multiply, RETROK_KP_MULTIPLY },
{ XK_KP_Divide, RETROK_KP_DIVIDE },
{ XK_grave, RETROK_BACKQUOTE },
{ XK_Pause, RETROK_PAUSE },
{ XK_KP_0, RETROK_KP0 },
{ XK_KP_1, RETROK_KP1 },
{ XK_KP_2, RETROK_KP2 },
{ XK_KP_3, RETROK_KP3 },
{ XK_KP_4, RETROK_KP4 },
{ XK_KP_5, RETROK_KP5 },
{ XK_KP_6, RETROK_KP6 },
{ XK_KP_7, RETROK_KP7 },
{ XK_KP_8, RETROK_KP8 },
{ XK_KP_9, RETROK_KP9 },
{ XK_0, RETROK_0 },
{ XK_1, RETROK_1 },
{ XK_2, RETROK_2 },
{ XK_3, RETROK_3 },
{ XK_4, RETROK_4 },
{ XK_5, RETROK_5 },
{ XK_6, RETROK_6 },
{ XK_7, RETROK_7 },
{ XK_8, RETROK_8 },
{ XK_9, RETROK_9 },
{ XK_F1, RETROK_F1 },
{ XK_F2, RETROK_F2 },
{ XK_F3, RETROK_F3 },
{ XK_F4, RETROK_F4 },
{ XK_F5, RETROK_F5 },
{ XK_F6, RETROK_F6 },
{ XK_F7, RETROK_F7 },
{ XK_F8, RETROK_F8 },
{ XK_F9, RETROK_F9 },
{ XK_F10, RETROK_F10 },
{ XK_F11, RETROK_F11 },
{ XK_F12, RETROK_F12 },
{ XK_a, RETROK_a },
{ XK_b, RETROK_b },
{ XK_c, RETROK_c },
{ XK_d, RETROK_d },
{ XK_e, RETROK_e },
{ XK_f, RETROK_f },
{ XK_g, RETROK_g },
{ XK_h, RETROK_h },
{ XK_i, RETROK_i },
{ XK_j, RETROK_j },
{ XK_k, RETROK_k },
{ XK_l, RETROK_l },
{ XK_m, RETROK_m },
{ XK_n, RETROK_n },
{ XK_o, RETROK_o },
{ XK_p, RETROK_p },
{ XK_q, RETROK_q },
{ XK_r, RETROK_r },
{ XK_s, RETROK_s },
{ XK_t, RETROK_t },
{ XK_u, RETROK_u },
{ XK_v, RETROK_v },
{ XK_w, RETROK_w },
{ XK_x, RETROK_x },
{ XK_y, RETROK_y },
{ XK_z, RETROK_z },
2011-03-13 11:02:06 +00:00
};
static void init_lut(void)
{
memset(keysym_lut, 0, sizeof(keysym_lut));
for (unsigned i = 0; i < sizeof(lut_binds) / sizeof(lut_binds[0]); i++)
keysym_lut[lut_binds[i].sk] = lut_binds[i].x;
2011-03-13 11:02:06 +00:00
}
2011-11-02 18:31:36 +00:00
static void *x_input_init(void)
2011-03-13 03:51:09 +00:00
{
2012-09-28 20:38:42 +00:00
if (driver.display_type != RARCH_DISPLAY_X11)
2011-03-13 03:51:09 +00:00
{
2012-09-28 20:38:42 +00:00
RARCH_ERR("Currently active window is not an X11 window. Cannot use this driver.\n");
2011-03-13 03:51:09 +00:00
return NULL;
}
2012-09-28 20:38:42 +00:00
x11_input_t *x11 = (x11_input_t*)calloc(1, sizeof(*x11));
if (!x11)
2011-03-13 03:51:09 +00:00
return NULL;
2012-09-28 20:38:42 +00:00
// Borrow the active X window ...
x11->display = (Display*)driver.video_display;
x11->win = (Window)driver.video_window;
x11->joypad = input_joypad_init_first();
2011-03-13 11:02:06 +00:00
init_lut();
2011-03-13 03:51:09 +00:00
return x11;
}
static bool x_key_pressed(x11_input_t *x11, int key)
{
if (key >= RETROK_LAST)
return false;
key = keysym_lut[key];
2011-03-13 03:51:09 +00:00
int keycode = XKeysymToKeycode(x11->display, key);
2011-03-13 11:02:06 +00:00
bool ret = x11->state[keycode >> 3] & (1 << (keycode & 7));
return ret;
2011-03-13 03:51:09 +00:00
}
2012-07-07 15:19:32 +00:00
static bool x_is_pressed(x11_input_t *x11, const struct retro_keybind *binds, unsigned id)
2011-03-13 03:51:09 +00:00
{
2012-04-21 21:25:32 +00:00
if (id < RARCH_BIND_LIST_END)
2011-03-13 03:51:09 +00:00
{
2012-07-07 15:19:32 +00:00
const struct retro_keybind *bind = &binds[id];
2012-01-30 00:45:18 +00:00
return bind->valid && x_key_pressed(x11, binds[id].key);
2011-03-13 03:51:09 +00:00
}
2012-01-30 00:45:18 +00:00
else
return false;
2011-03-13 03:51:09 +00:00
}
static bool x_bind_button_pressed(void *data, int key)
{
2011-12-24 12:46:12 +00:00
x11_input_t *x11 = (x11_input_t*)data;
2012-01-30 00:45:18 +00:00
return x_is_pressed(x11, g_settings.input.binds[0], key) ||
2012-09-28 22:34:44 +00:00
input_joypad_pressed(x11->joypad, 0, &g_settings.input.binds[0][key]);
}
2012-09-26 13:27:25 +00:00
static int16_t x_mouse_state(x11_input_t *x11, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return x11->mouse_x - x11->mouse_last_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return x11->mouse_y - x11->mouse_last_y;
case RETRO_DEVICE_ID_MOUSE_LEFT:
return x11->mouse_l;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
return x11->mouse_r;
default:
return 0;
}
}
static int16_t x_lightgun_state(x11_input_t *x11, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_LIGHTGUN_X:
return x11->mouse_x - x11->mouse_last_x;
case RETRO_DEVICE_ID_LIGHTGUN_Y:
return x11->mouse_y - x11->mouse_last_y;
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
return x11->mouse_l;
case RETRO_DEVICE_ID_LIGHTGUN_CURSOR:
return x11->mouse_m;
case RETRO_DEVICE_ID_LIGHTGUN_TURBO:
return x11->mouse_r;
case RETRO_DEVICE_ID_LIGHTGUN_START:
return x11->mouse_m && x11->mouse_r;
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
return x11->mouse_m && x11->mouse_l;
default:
return 0;
}
}
2012-07-07 15:19:32 +00:00
static int16_t x_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned index, unsigned id)
2011-03-13 03:51:09 +00:00
{
2011-12-24 12:46:12 +00:00
x11_input_t *x11 = (x11_input_t*)data;
2011-03-13 03:51:09 +00:00
switch (device)
{
2012-04-07 09:55:37 +00:00
case RETRO_DEVICE_JOYPAD:
return x_is_pressed(x11, binds[port], id) ||
2012-09-28 20:38:42 +00:00
input_joypad_pressed(x11->joypad, port, &binds[port][id]);
2011-03-13 03:51:09 +00:00
case RETRO_DEVICE_KEYBOARD:
return x_key_pressed(x11, id);
2012-06-28 15:57:50 +00:00
case RETRO_DEVICE_ANALOG:
2012-09-28 20:38:42 +00:00
return input_joypad_analog(x11->joypad, port, index, id, binds[port]);
2012-06-28 15:57:50 +00:00
2012-09-26 13:27:25 +00:00
case RETRO_DEVICE_MOUSE:
return x_mouse_state(x11, id);
case RETRO_DEVICE_LIGHTGUN:
return x_lightgun_state(x11, id);
2011-03-13 03:51:09 +00:00
default:
return 0;
}
}
static void x_input_free(void *data)
{
2011-12-24 12:46:12 +00:00
x11_input_t *x11 = (x11_input_t*)data;
2012-09-28 20:38:42 +00:00
if (x11->joypad)
x11->joypad->destroy();
2011-03-13 03:51:09 +00:00
free(data);
}
2012-09-26 13:27:25 +00:00
static void x_input_poll_mouse(x11_input_t *x11)
{
Window root_win, child_win;
int root_x, root_y, win_x, win_y;
unsigned mask;
x11->mouse_last_x = x11->mouse_x;
x11->mouse_last_y = x11->mouse_y;
XQueryPointer(x11->display,
x11->win,
2012-09-26 13:27:25 +00:00
&root_win, &child_win,
&root_x, &root_y,
&win_x, &win_y,
&mask);
x11->mouse_x = root_x;
x11->mouse_y = root_y;
x11->mouse_l = mask & Button1Mask;
x11->mouse_m = mask & Button2Mask;
x11->mouse_r = mask & Button3Mask;
}
2011-03-13 03:51:09 +00:00
static void x_input_poll(void *data)
{
2011-12-24 12:46:12 +00:00
x11_input_t *x11 = (x11_input_t*)data;
if (video_focus_func())
XQueryKeymap(x11->display, x11->state);
else
memset(x11->state, 0, sizeof(x11->state));
2012-09-26 13:27:25 +00:00
x_input_poll_mouse(x11);
2012-09-28 20:38:42 +00:00
input_joypad_poll(x11->joypad);
2011-03-13 03:51:09 +00:00
}
const input_driver_t input_x = {
2011-12-24 12:46:12 +00:00
x_input_init,
x_input_poll,
x_input_state,
x_bind_button_pressed,
x_input_free,
"x"
2011-03-13 03:51:09 +00:00
};