2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - 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;
|
2013-03-29 17:53:07 +00:00
|
|
|
|
|
|
|
bool grab_mouse;
|
2012-09-28 20:38:42 +00:00
|
|
|
} x11_input_t;
|
|
|
|
|
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;
|
|
|
|
|
2013-05-04 08:22:31 +00:00
|
|
|
x11->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
|
2012-12-08 12:25:42 +00:00
|
|
|
input_init_keyboard_lut(rarch_key_map_x11);
|
2011-03-13 11:02:06 +00:00
|
|
|
|
2011-03-13 03:51:09 +00:00
|
|
|
return x11;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool x_key_pressed(x11_input_t *x11, int key)
|
|
|
|
{
|
2012-07-28 01:21:37 +00:00
|
|
|
if (key >= RETROK_LAST)
|
|
|
|
return false;
|
|
|
|
|
2012-12-08 12:25:42 +00:00
|
|
|
unsigned sym = input_translate_rk_to_keysym((enum retro_key)key);
|
|
|
|
int keycode = XKeysymToKeycode(x11->display, sym);
|
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
|
|
|
}
|
|
|
|
|
2013-11-09 00:01:18 +00:00
|
|
|
static int16_t x_pressed_analog(x11_input_t *x11, const struct retro_keybind *binds, unsigned index, unsigned id)
|
|
|
|
{
|
|
|
|
unsigned id_minus = 0;
|
|
|
|
unsigned id_plus = 0;
|
|
|
|
input_conv_analog_id_to_bind_id(index, id, &id_minus, &id_plus);
|
|
|
|
|
|
|
|
int16_t pressed_minus = x_is_pressed(x11, binds, id_minus) ? -0x7fff : 0;
|
|
|
|
int16_t pressed_plus = x_is_pressed(x11, binds, id_plus) ? 0x7fff : 0;
|
|
|
|
return pressed_plus + pressed_minus;
|
|
|
|
}
|
|
|
|
|
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) ||
|
2013-04-26 12:36:36 +00:00
|
|
|
input_joypad_pressed(x11->joypad, 0, g_settings.input.binds[0], key);
|
2012-09-15 09:41:28 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 15:23:04 +00:00
|
|
|
static int16_t x_pointer_state(x11_input_t *x11, unsigned index, unsigned id, bool screen)
|
2012-10-27 22:38:31 +00:00
|
|
|
{
|
2012-12-27 11:26:13 +00:00
|
|
|
if (index != 0)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-11 15:23:04 +00:00
|
|
|
int16_t res_x = 0, res_y = 0, res_screen_x = 0, res_screen_y = 0;
|
|
|
|
bool valid = input_translate_coord_viewport(x11->mouse_x, x11->mouse_y,
|
|
|
|
&res_x, &res_y, &res_screen_x, &res_screen_y);
|
2012-10-27 22:38:31 +00:00
|
|
|
|
|
|
|
if (!valid)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-11 15:23:04 +00:00
|
|
|
if (screen)
|
|
|
|
{
|
|
|
|
res_x = res_screen_x;
|
|
|
|
res_y = res_screen_y;
|
|
|
|
}
|
|
|
|
|
2013-04-20 07:01:38 +00:00
|
|
|
bool inside = (res_x >= -0x7fff) && (res_y >= -0x7fff);
|
2012-10-27 22:38:31 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:27:25 +00: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;
|
|
|
|
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;
|
2013-11-09 00:01:18 +00:00
|
|
|
int16_t ret;
|
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) ||
|
2013-04-26 12:36:36 +00:00
|
|
|
input_joypad_pressed(x11->joypad, port, binds[port], id);
|
2011-03-13 03:51:09 +00:00
|
|
|
|
2012-07-28 01:21:37 +00:00
|
|
|
case RETRO_DEVICE_KEYBOARD:
|
|
|
|
return x_key_pressed(x11, id);
|
|
|
|
|
2012-06-28 15:57:50 +00:00
|
|
|
case RETRO_DEVICE_ANALOG:
|
2013-11-09 10:02:41 +00:00
|
|
|
ret = x_pressed_analog(x11, binds[port], index, id);
|
2013-11-09 00:01:18 +00:00
|
|
|
if (!ret)
|
2013-11-09 10:02:41 +00:00
|
|
|
ret = input_joypad_analog(x11->joypad, port, index, id, binds[port]);
|
2013-11-09 00:01:18 +00:00
|
|
|
return ret;
|
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);
|
|
|
|
|
2012-10-27 22:38:31 +00:00
|
|
|
case RETRO_DEVICE_POINTER:
|
2013-01-11 15:23:04 +00:00
|
|
|
case RARCH_DEVICE_POINTER_SCREEN:
|
|
|
|
return x_pointer_state(x11, index, id, device == RARCH_DEVICE_POINTER_SCREEN);
|
2012-10-27 22:38:31 +00:00
|
|
|
|
2012-09-26 13:27:25 +00:00
|
|
|
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-15 23:27:32 +00:00
|
|
|
|
2012-09-28 20:38:42 +00:00
|
|
|
if (x11->joypad)
|
|
|
|
x11->joypad->destroy();
|
2012-09-15 23:27:32 +00:00
|
|
|
|
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,
|
2012-10-13 17:53:04 +00:00
|
|
|
x11->win,
|
2012-09-26 13:27:25 +00:00
|
|
|
&root_win, &child_win,
|
|
|
|
&root_x, &root_y,
|
|
|
|
&win_x, &win_y,
|
|
|
|
&mask);
|
|
|
|
|
2012-10-27 22:38:31 +00:00
|
|
|
x11->mouse_x = win_x;
|
|
|
|
x11->mouse_y = win_y;
|
2012-09-26 13:27:25 +00:00
|
|
|
x11->mouse_l = mask & Button1Mask;
|
|
|
|
x11->mouse_m = mask & Button2Mask;
|
|
|
|
x11->mouse_r = mask & Button3Mask;
|
2013-03-29 17:53:07 +00:00
|
|
|
|
|
|
|
// Somewhat hacky, but seem to do the job.
|
2013-03-29 18:03:55 +00:00
|
|
|
if (x11->grab_mouse && video_focus_func())
|
2013-03-29 17:53:07 +00:00
|
|
|
{
|
|
|
|
struct rarch_viewport vp = {0};
|
|
|
|
video_viewport_info_func(&vp);
|
2013-03-29 18:30:09 +00:00
|
|
|
int mid_w = vp.full_width >> 1;
|
|
|
|
int mid_h = vp.full_height >> 1;
|
|
|
|
|
|
|
|
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 17:53:07 +00:00
|
|
|
x11->mouse_last_x = mid_w;
|
|
|
|
x11->mouse_last_y = mid_h;
|
|
|
|
}
|
2012-09-26 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2012-09-15 23:27:32 +00:00
|
|
|
|
2012-10-13 17:53:04 +00:00
|
|
|
if (video_focus_func())
|
2012-09-15 23:27:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-29 17:53:07 +00:00
|
|
|
static void x_grab_mouse(void *data, bool state)
|
|
|
|
{
|
|
|
|
x11_input_t *x11 = (x11_input_t*)data;
|
|
|
|
x11->grab_mouse = state;
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:55:05 +00:00
|
|
|
static bool x_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength)
|
2013-09-25 20:59:05 +00:00
|
|
|
{
|
|
|
|
x11_input_t *x11 = (x11_input_t*)data;
|
2013-09-26 10:55:05 +00:00
|
|
|
return input_joypad_set_rumble(x11->joypad, port, effect, strength);
|
2013-09-25 20:59:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-29 15:58:46 +00:00
|
|
|
static const rarch_joypad_driver_t *x_get_joypad_driver(void *data)
|
|
|
|
{
|
|
|
|
x11_input_t *x11 = (x11_input_t*)data;
|
|
|
|
return x11->joypad;
|
|
|
|
}
|
|
|
|
|
2013-11-02 20:16:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
2013-03-13 15:33:39 +00:00
|
|
|
NULL,
|
2013-11-02 23:27:58 +00:00
|
|
|
NULL,
|
2013-11-02 20:16:57 +00:00
|
|
|
x_input_get_capabilities,
|
2013-03-29 17:53:07 +00:00
|
|
|
"x",
|
|
|
|
x_grab_mouse,
|
2013-09-25 20:59:05 +00:00
|
|
|
x_set_rumble,
|
2013-09-29 15:58:46 +00:00
|
|
|
x_get_joypad_driver,
|
2011-03-13 03:51:09 +00:00
|
|
|
};
|
|
|
|
|