RetroArch/input/drivers/x11_input.c

426 lines
10 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2015-01-07 17:46:50 +01:00
* Copyright (C) 2010-2015 - Hans-Kristian Arntzen
2011-03-13 04:51:09 +01:00
*
2012-04-21 23:13:50 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-03-13 04:51:09 +01: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 23:13:50 +02:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-03-13 04:51:09 +01: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 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-03-13 04:51:09 +01:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <boolean.h>
#include "../input_config.h"
#include "../input_joypad_driver.h"
2015-01-12 06:16:52 +01:00
#include "../input_keymaps.h"
2011-03-13 04:51:09 +01:00
#include "../../gfx/video_driver.h"
2015-11-29 03:34:09 +01:00
#include "../common/input_x11_common.h"
2015-01-12 06:16:52 +01:00
#include "../../driver.h"
#include "../../general.h"
2015-11-23 12:03:38 +01:00
#include "../../verbosity.h"
2012-09-28 22:38:42 +02:00
typedef struct x11_input
{
bool blocked;
const input_device_driver_t *joypad;
2012-09-28 22:38:42 +02:00
Display *display;
Window win;
char state[32];
2015-11-29 03:34:09 +01:00
bool mouse_l, mouse_r, mouse_m;
2012-09-28 22:38:42 +02:00
int mouse_x, mouse_y;
int mouse_last_x, mouse_last_y;
2013-03-29 18:53:07 +01:00
bool grab_mouse;
2012-09-28 22:38:42 +02:00
} x11_input_t;
2015-07-10 09:55:29 +02:00
2011-11-02 19:31:36 +01:00
static void *x_input_init(void)
2011-03-13 04:51:09 +01:00
{
2015-06-26 18:35:35 +02:00
x11_input_t *x11;
settings_t *settings = config_get_ptr();
2015-03-20 22:32:09 +01:00
if (video_driver_display_type_get() != RARCH_DISPLAY_X11)
2011-03-13 04:51:09 +01:00
{
2012-09-28 22:38:42 +02:00
RARCH_ERR("Currently active window is not an X11 window. Cannot use this driver.\n");
2011-03-13 04:51:09 +01:00
return NULL;
}
2015-06-26 18:35:35 +02:00
x11 = (x11_input_t*)calloc(1, sizeof(*x11));
2012-09-28 22:38:42 +02:00
if (!x11)
2011-03-13 04:51:09 +01:00
return NULL;
2014-09-09 18:15:17 +02:00
/* Borrow the active X window ... */
x11->display = (Display*)video_driver_display_get();
x11->win = (Window)video_driver_window_get();
2012-09-28 22:38:42 +02:00
x11->joypad = input_joypad_init_driver(settings->input.joypad_driver, x11);
input_keymaps_init_keyboard_lut(rarch_key_map_x11);
2011-03-13 12:02:06 +01:00
2011-03-13 04:51:09 +01:00
return x11;
}
static bool x_key_pressed(x11_input_t *x11, int key)
{
unsigned sym;
int keycode;
bool ret;
if (key >= RETROK_LAST)
return false;
2015-03-09 18:55:26 +01:00
sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key);
keycode = XKeysymToKeycode(x11->display, sym);
2015-03-09 18:55:26 +01:00
ret = x11->state[keycode >> 3] & (1 << (keycode & 7));
2011-03-13 12:02:06 +01:00
return ret;
2011-03-13 04:51:09 +01:00
}
2014-09-09 18:15:17 +02:00
static bool x_is_pressed(x11_input_t *x11,
const struct retro_keybind *binds, unsigned id)
2011-03-13 04:51:09 +01:00
{
2012-04-21 23:25:32 +02:00
if (id < RARCH_BIND_LIST_END)
2011-03-13 04:51:09 +01:00
{
2012-07-07 17:19:32 +02:00
const struct retro_keybind *bind = &binds[id];
2012-01-30 01:45:18 +01:00
return bind->valid && x_key_pressed(x11, binds[id].key);
2011-03-13 04:51:09 +01:00
}
2015-03-09 18:55:26 +01:00
2014-08-27 03:28:22 +02:00
return false;
2011-03-13 04:51:09 +01:00
}
2014-09-09 18:15:17 +02:00
static int16_t x_pressed_analog(x11_input_t *x11,
2014-10-20 20:31:00 +02:00
const struct retro_keybind *binds, unsigned idx, unsigned id)
{
int16_t pressed_minus = 0, pressed_plus = 0;
unsigned id_minus = 0;
unsigned id_plus = 0;
2014-10-20 20:31:00 +02:00
input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus);
if (x_is_pressed(x11, binds, id_minus))
pressed_minus = -0x7fff;
if (x_is_pressed(x11, binds, id_plus))
pressed_plus = 0x7fff;
return pressed_plus + pressed_minus;
}
2015-11-07 20:59:12 +01:00
static bool x_input_key_pressed(void *data, int key)
2011-03-13 04:51:09 +01:00
{
x11_input_t *x11 = (x11_input_t*)data;
settings_t *settings = config_get_ptr();
2015-11-07 20:59:12 +01:00
if (x_is_pressed(x11, settings->input.binds[0], key))
return true;
if (input_joypad_pressed(x11->joypad, 0, settings->input.binds[0], key))
return true;
2015-11-07 20:59:12 +01:00
return false;
}
2015-11-07 20:59:12 +01:00
static bool x_input_meta_key_pressed(void *data, int key)
2015-07-17 03:31:51 +02:00
{
return false;
}
2012-09-26 15:27:25 +02: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;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
2015-11-29 03:34:09 +01:00
return x_mouse_state_wheel(id);
2014-04-25 23:44:53 +02:00
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return x11->mouse_m;
2012-09-26 15:27:25 +02:00
}
2015-03-09 18:55:26 +01:00
return 0;
2012-09-26 15:27:25 +02:00
}
static int16_t x_mouse_state_screen(x11_input_t *x11, unsigned id)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return x11->mouse_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return x11->mouse_y;
default:
break;
}
return x_mouse_state(x11, id);
}
2014-09-09 18:15:17 +02:00
static int16_t x_pointer_state(x11_input_t *x11,
2014-10-20 20:31:00 +02:00
unsigned idx, unsigned id, bool screen)
{
bool valid, inside;
2015-10-08 08:19:38 +02:00
int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0;
2014-10-20 20:31:00 +02:00
if (idx != 0)
2012-12-27 12:26:13 +01:00
return 0;
valid = input_translate_coord_viewport(x11->mouse_x, x11->mouse_y,
2013-01-11 16:23:04 +01:00
&res_x, &res_y, &res_screen_x, &res_screen_y);
if (!valid)
return 0;
2013-01-11 16:23:04 +01:00
if (screen)
{
res_x = res_screen_x;
res_y = res_screen_y;
}
inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
if (!inside)
return 0;
switch (id)
{
case RETRO_DEVICE_ID_POINTER_X:
return res_x;
case RETRO_DEVICE_ID_POINTER_Y:
return res_y;
case RETRO_DEVICE_ID_POINTER_PRESSED:
return x11->mouse_l;
}
2015-03-09 18:55:26 +01:00
return 0;
}
2012-09-26 15:27:25 +02:00
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;
}
return 0;
2012-09-26 15:27:25 +02:00
}
2014-09-09 18:15:17 +02:00
static int16_t x_input_state(void *data,
const struct retro_keybind **binds, unsigned port,
2014-10-20 20:31:00 +02:00
unsigned device, unsigned idx, unsigned id)
2011-03-13 04:51:09 +01:00
{
int16_t ret;
x11_input_t *x11 = (x11_input_t*)data;
2011-03-13 04:51:09 +01:00
switch (device)
{
2012-04-07 11:55:37 +02:00
case RETRO_DEVICE_JOYPAD:
return x_is_pressed(x11, binds[port], id) ||
2013-04-26 14:36:36 +02:00
input_joypad_pressed(x11->joypad, port, binds[port], id);
2011-03-13 04:51:09 +01:00
case RETRO_DEVICE_KEYBOARD:
return x_key_pressed(x11, id);
2012-06-28 17:57:50 +02:00
case RETRO_DEVICE_ANALOG:
2014-10-20 20:31:00 +02:00
ret = x_pressed_analog(x11, binds[port], idx, id);
if (!ret)
2014-10-20 20:31:00 +02:00
ret = input_joypad_analog(x11->joypad, port, idx,
2014-09-09 18:15:17 +02:00
id, binds[port]);
return ret;
2012-06-28 17:57:50 +02:00
2012-09-26 15:27:25 +02:00
case RETRO_DEVICE_MOUSE:
return x_mouse_state(x11, id);
case RARCH_DEVICE_MOUSE_SCREEN:
return x_mouse_state_screen(x11, id);
2012-09-26 15:27:25 +02:00
case RETRO_DEVICE_POINTER:
2013-01-11 16:23:04 +01:00
case RARCH_DEVICE_POINTER_SCREEN:
2014-10-20 20:31:00 +02:00
return x_pointer_state(x11, idx, id,
2014-09-09 18:15:17 +02:00
device == RARCH_DEVICE_POINTER_SCREEN);
2012-09-26 15:27:25 +02:00
case RETRO_DEVICE_LIGHTGUN:
return x_lightgun_state(x11, id);
2011-03-13 04:51:09 +01:00
}
2015-03-09 18:55:26 +01:00
return 0;
2011-03-13 04:51:09 +01:00
}
static void x_input_free(void *data)
{
2011-12-24 13:46:12 +01:00
x11_input_t *x11 = (x11_input_t*)data;
if (!x11)
return;
2012-09-28 22:38:42 +02:00
if (x11->joypad)
x11->joypad->destroy();
free(x11);
2011-03-13 04:51:09 +01:00
}
2012-09-26 15:27:25 +02:00
static void x_input_poll_mouse(x11_input_t *x11)
{
unsigned mask;
2015-03-22 23:40:22 +01:00
int root_x, root_y, win_x, win_y;
Window root_win, child_win;
2012-09-26 15:27:25 +02:00
x11->mouse_last_x = x11->mouse_x;
x11->mouse_last_y = x11->mouse_y;
XQueryPointer(x11->display,
x11->win,
2012-09-26 15:27:25 +02:00
&root_win, &child_win,
&root_x, &root_y,
&win_x, &win_y,
&mask);
x11->mouse_x = win_x;
x11->mouse_y = win_y;
x11->mouse_l = mask & Button1Mask;
x11->mouse_m = mask & Button2Mask;
x11->mouse_r = mask & Button3Mask;
2013-03-29 18:53:07 +01:00
2014-09-09 18:15:17 +02:00
/* Somewhat hacky, but seem to do the job. */
2016-05-08 14:00:51 +02:00
if (x11->grab_mouse && video_driver_is_focused())
2013-03-29 18:53:07 +01:00
{
2015-03-22 21:28:50 +01:00
int mid_w, mid_h;
2015-02-14 05:52:05 +01:00
struct video_viewport vp = {0};
2014-08-15 20:51:59 +02:00
2016-05-08 14:00:51 +02:00
video_driver_get_viewport_info(&vp);
2015-03-22 21:28:50 +01:00
mid_w = vp.full_width >> 1;
mid_h = vp.full_height >> 1;
2013-03-29 19:30:09 +01:00
if (x11->mouse_x != mid_w || x11->mouse_y != mid_h)
{
XWarpPointer(x11->display, None,
x11->win, 0, 0, 0, 0,
mid_w, mid_h);
}
2013-03-29 18:53:07 +01:00
x11->mouse_last_x = mid_w;
x11->mouse_last_y = mid_h;
}
2012-09-26 15:27:25 +02:00
}
2011-03-13 04:51:09 +01:00
static void x_input_poll(void *data)
{
2011-12-24 13:46:12 +01:00
x11_input_t *x11 = (x11_input_t*)data;
2016-05-08 14:00:51 +02:00
if (video_driver_is_focused())
XQueryKeymap(x11->display, x11->state);
else
memset(x11->state, 0, sizeof(x11->state));
2012-09-26 15:27:25 +02:00
x_input_poll_mouse(x11);
if (x11->joypad)
x11->joypad->poll();
2011-03-13 04:51:09 +01:00
}
2013-03-29 18:53:07 +01:00
static void x_grab_mouse(void *data, bool state)
{
x11_input_t *x11 = (x11_input_t*)data;
if (x11)
x11->grab_mouse = state;
2013-03-29 18:53:07 +01:00
}
2014-09-09 18:15:17 +02:00
static bool x_set_rumble(void *data, unsigned port,
enum retro_rumble_effect effect, uint16_t strength)
{
x11_input_t *x11 = (x11_input_t*)data;
if (!x11)
return false;
return input_joypad_set_rumble(x11->joypad, port, effect, strength);
}
static const input_device_driver_t *x_get_joypad_driver(void *data)
2013-09-29 17:58:46 +02:00
{
x11_input_t *x11 = (x11_input_t*)data;
2015-03-09 18:55:26 +01:00
if (!x11)
return NULL;
2013-09-29 17:58:46 +02:00
return x11->joypad;
}
static uint64_t x_input_get_capabilities(void *data)
{
uint64_t caps = 0;
caps |= (1 << RETRO_DEVICE_JOYPAD);
caps |= (1 << RETRO_DEVICE_MOUSE);
caps |= (1 << RETRO_DEVICE_KEYBOARD);
caps |= (1 << RETRO_DEVICE_LIGHTGUN);
caps |= (1 << RETRO_DEVICE_POINTER);
caps |= (1 << RETRO_DEVICE_ANALOG);
return caps;
}
static bool x_keyboard_mapping_is_blocked(void *data)
{
x11_input_t *x11 = (x11_input_t*)data;
if (!x11)
return false;
return x11->blocked;
}
static void x_keyboard_mapping_set_block(void *data, bool value)
{
x11_input_t *x11 = (x11_input_t*)data;
if (!x11)
return;
x11->blocked = value;
}
2014-09-11 07:06:20 +02:00
input_driver_t input_x = {
2011-12-24 13:46:12 +01:00
x_input_init,
x_input_poll,
x_input_state,
x_input_key_pressed,
2015-07-17 03:31:51 +02:00
x_input_meta_key_pressed,
2011-12-24 13:46:12 +01:00
x_input_free,
NULL,
NULL,
x_input_get_capabilities,
2013-03-29 18:53:07 +01:00
"x",
x_grab_mouse,
NULL,
x_set_rumble,
2013-09-29 17:58:46 +02:00
x_get_joypad_driver,
2015-11-16 02:39:38 +01:00
NULL,
x_keyboard_mapping_is_blocked,
x_keyboard_mapping_set_block,
2011-03-13 04:51:09 +01:00
};