quakeforge/libs/video/targets/in_x11.c

699 lines
13 KiB
C
Raw Normal View History

/*
in_x11.c
general x11 input driver
Copyright (C) 1996-1997 Id Software, Inc.
Copyright (C) 2000 Marcus Sundberg [mackan@stacken.kth.se]
Copyright (C) 1999,2000 contributors of the QuakeForge project
Please see the file "AUTHORS" for a list of contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#define _BSD
#include <ctype.h>
#include <errno.h>
2001-08-27 01:00:03 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#ifdef HAVE_DGA
# include <X11/extensions/XShm.h>
# include <X11/extensions/xf86dga.h>
#endif
#include "QF/cdaudio.h"
#include "QF/console.h"
#include "QF/cmd.h"
#include "QF/cvar.h"
#include "QF/input.h"
#include "QF/joystick.h"
#include "QF/keys.h"
#include "QF/mathlib.h"
#include "QF/qargs.h"
#include "QF/sound.h"
#include "QF/sys.h"
#include "QF/vid.h"
#include "compat.h"
#include "context_x11.h"
#include "dga_check.h"
2002-08-21 00:04:35 +00:00
cvar_t *in_snd_block;
cvar_t *in_dga;
static qboolean dga_avail;
static qboolean dga_active;
static keydest_t old_key_dest = key_none;
static int p_mouse_x, p_mouse_y;
#define KEY_MASK (KeyPressMask | KeyReleaseMask)
#define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
#define FOCUS_MASK (FocusChangeMask | EnterWindowMask)
#define INPUT_MASK (KEY_MASK | MOUSE_MASK | FOCUS_MASK)
2002-08-21 00:04:35 +00:00
static void
dga_on (void)
{
#ifdef HAVE_DGA
if (dga_avail && !dga_active) {
XF86DGADirectVideo (x_disp, DefaultScreen (x_disp),
XF86DGADirectMouse);
dga_active = true;
}
#endif
}
static void
dga_off (void)
{
#ifdef HAVE_DGA
if (dga_avail && dga_active) {
XF86DGADirectVideo (x_disp, DefaultScreen (x_disp), 0);
dga_active = false;
}
#endif
}
static void
in_dga_f (cvar_t *var)
{
if (in_grab && in_grab->int_val) {
if (var->int_val) {
dga_on ();
} else {
dga_off ();
}
}
}
2001-08-27 01:00:03 +00:00
static void
in_paste_buffer_f (void)
{
Atom property;
if (XGetSelectionOwner (x_disp, XA_PRIMARY) == None)
return;
property = XInternAtom (x_disp, "GETCLIPBOARDDATA_PROP", False);
XConvertSelection (x_disp, XA_PRIMARY, XA_STRING, property, x_win, x_time);
XFlush (x_disp);
}
static void
selection_notify (XEvent *event)
{
unsigned char *data, *p;
unsigned long num_bytes;
unsigned long tmp, len;
int format;
Atom type, property;
x_time = event->xselection.time;
if ((property = event->xselection.property) == None)
return;
XGetWindowProperty (x_disp, x_win, property, 0, 0, False, AnyPropertyType,
&type, &format, &len, &num_bytes, &data);
if (num_bytes <= 0)
return;
if (XGetWindowProperty (x_disp, x_win, property, 0, num_bytes, True,
2002-08-21 00:04:35 +00:00
AnyPropertyType, &type, &format, &len, &tmp, &data)
!= Success) {
2002-08-21 05:59:00 +00:00
XFree (data); // FIXME is this correct for this instance?
return;
}
// get bytes to keys.c
for (p = data; num_bytes && *p; p++, num_bytes--) {
Key_Event (QFK_UNKNOWN, *p, 1);
Key_Event (QFK_UNKNOWN, 0, 0);
}
XFree (data);
}
static void
enter_notify (XEvent *event)
{
x_time = event->xcrossing.time;
p_mouse_x = event->xmotion.x;
p_mouse_y = event->xmotion.y;
}
static void
XLateKey (XKeyEvent * ev, int *k, int *u)
{
2002-08-21 00:04:35 +00:00
unsigned char buffer[4];
int bytes, unicode;
int key = 0;
2001-08-17 19:47:51 +00:00
KeySym keysym, shifted_keysym;
XComposeStatus compose;
2001-08-17 19:47:51 +00:00
keysym = XLookupKeysym (ev, 0);
2001-08-27 01:00:03 +00:00
bytes = XLookupString (ev, buffer, sizeof(buffer), &shifted_keysym,
&compose);
unicode = buffer[0];
switch (keysym) {
case XK_KP_Page_Up:
2001-10-28 04:23:37 +00:00
key = QFK_KP9;
break;
case XK_Page_Up:
2001-10-28 04:23:37 +00:00
key = QFK_PAGEUP;
break;
case XK_KP_Page_Down:
2001-10-28 04:23:37 +00:00
key = QFK_KP3;
break;
case XK_Page_Down:
2001-10-28 04:23:37 +00:00
key = QFK_PAGEDOWN;
break;
case XK_KP_Home:
2001-10-28 04:23:37 +00:00
key = QFK_KP7;
break;
case XK_Home:
2001-10-28 04:23:37 +00:00
key = QFK_HOME;
break;
case XK_KP_End:
2001-10-28 04:23:37 +00:00
key = QFK_KP1;
break;
case XK_End:
2001-10-28 04:23:37 +00:00
key = QFK_END;
break;
case XK_KP_Left:
2001-10-28 04:23:37 +00:00
key = QFK_KP4;
break;
case XK_Left:
2001-10-28 04:23:37 +00:00
key = QFK_LEFT;
break;
case XK_KP_Right:
2001-10-28 04:23:37 +00:00
key = QFK_KP6;
break;
case XK_Right:
2001-10-28 04:23:37 +00:00
key = QFK_RIGHT;
break;
case XK_KP_Down:
2001-10-28 04:23:37 +00:00
key = QFK_KP2;
break;
case XK_Down:
2001-10-28 04:23:37 +00:00
key = QFK_DOWN;
break;
case XK_KP_Up:
2001-10-28 04:23:37 +00:00
key = QFK_KP8;
break;
case XK_Up:
2001-10-28 04:23:37 +00:00
key = QFK_UP;
break;
case XK_Escape:
2001-10-28 04:23:37 +00:00
key = QFK_ESCAPE;
break;
case XK_KP_Enter:
2001-10-28 04:23:37 +00:00
key = QFK_KP_ENTER;
break;
case XK_Return:
2001-10-28 04:23:37 +00:00
key = QFK_RETURN;
break;
case XK_Tab:
2001-10-28 04:23:37 +00:00
key = QFK_TAB;
break;
case XK_F1:
2001-10-28 04:23:37 +00:00
key = QFK_F1;
break;
case XK_F2:
2001-10-28 04:23:37 +00:00
key = QFK_F2;
break;
case XK_F3:
2001-10-28 04:23:37 +00:00
key = QFK_F3;
break;
case XK_F4:
2001-10-28 04:23:37 +00:00
key = QFK_F4;
break;
case XK_F5:
2001-10-28 04:23:37 +00:00
key = QFK_F5;
break;
case XK_F6:
2001-10-28 04:23:37 +00:00
key = QFK_F6;
break;
case XK_F7:
2001-10-28 04:23:37 +00:00
key = QFK_F7;
break;
case XK_F8:
2001-10-28 04:23:37 +00:00
key = QFK_F8;
break;
case XK_F9:
2001-10-28 04:23:37 +00:00
key = QFK_F9;
break;
case XK_F10:
2001-10-28 04:23:37 +00:00
key = QFK_F10;
break;
case XK_F11:
2001-10-28 04:23:37 +00:00
key = QFK_F11;
break;
case XK_F12:
2001-10-28 04:23:37 +00:00
key = QFK_F12;
break;
case XK_BackSpace:
2001-10-28 04:23:37 +00:00
key = QFK_BACKSPACE;
break;
case XK_KP_Delete:
2001-10-28 04:23:37 +00:00
key = QFK_KP_PERIOD;
break;
case XK_Delete:
2001-10-28 04:23:37 +00:00
key = QFK_DELETE;
break;
case XK_Pause:
2001-10-28 04:23:37 +00:00
key = QFK_PAUSE;
break;
case XK_Shift_L:
2001-10-28 04:23:37 +00:00
key = QFK_LSHIFT;
break;
case XK_Shift_R:
2001-10-28 04:23:37 +00:00
key = QFK_RSHIFT;
break;
case XK_Execute:
case XK_Control_L:
2001-10-28 04:23:37 +00:00
key = QFK_LCTRL;
break;
case XK_Control_R:
2002-08-08 05:53:58 +00:00
key = QFK_RCTRL;
break;
case XK_Mode_switch:
case XK_Alt_L:
2001-10-28 04:23:37 +00:00
key = QFK_LALT;
break;
case XK_Meta_L:
2001-10-28 04:23:37 +00:00
key = QFK_LMETA;
break;
case XK_Alt_R:
2001-10-28 04:23:37 +00:00
key = QFK_RALT;
break;
case XK_Meta_R:
2001-10-28 04:23:37 +00:00
key = QFK_RMETA;
break;
case XK_Multi_key:
key = QFK_COMPOSE;
break;
case XK_Menu:
key = QFK_MENU;
break;
case XK_Caps_Lock:
2001-10-28 04:23:37 +00:00
key = QFK_CAPSLOCK;
break;
2002-08-08 05:53:58 +00:00
case XK_KP_Begin:
2001-10-28 04:23:37 +00:00
key = QFK_KP5;
break;
2002-04-27 00:07:47 +00:00
case XK_Print:
key = QFK_PRINT;
break;
case XK_Scroll_Lock:
2001-10-28 04:23:37 +00:00
key = QFK_SCROLLOCK;
break;
case XK_Num_Lock:
2001-10-28 04:23:37 +00:00
key = QFK_NUMLOCK;
break;
case XK_Insert:
2001-10-28 04:23:37 +00:00
key = QFK_INSERT;
break;
case XK_KP_Insert:
2001-10-28 04:23:37 +00:00
key = QFK_KP0;
break;
case XK_KP_Multiply:
2001-10-28 04:23:37 +00:00
key = QFK_KP_MULTIPLY;
break;
case XK_KP_Add:
2001-10-28 04:23:37 +00:00
key = QFK_KP_PLUS;
break;
case XK_KP_Subtract:
2001-10-28 04:23:37 +00:00
key = QFK_KP_MINUS;
break;
case XK_KP_Divide:
2001-10-28 04:23:37 +00:00
key = QFK_KP_DIVIDE;
break;
2002-08-21 00:04:35 +00:00
// For Sun keyboards
case XK_F27:
2001-10-28 04:23:37 +00:00
key = QFK_HOME;
break;
case XK_F29:
2001-10-28 04:23:37 +00:00
key = QFK_PAGEUP;
break;
case XK_F33:
2001-10-28 04:23:37 +00:00
key = QFK_END;
break;
case XK_F35:
2001-10-28 04:23:37 +00:00
key = QFK_PAGEDOWN;
break;
2002-08-21 00:04:35 +00:00
// Some high ASCII symbols, for azerty keymaps
case XK_twosuperior:
2001-10-28 04:23:37 +00:00
key = QFK_WORLD_18;
break;
case XK_eacute:
2001-10-28 04:23:37 +00:00
key = QFK_WORLD_63;
break;
case XK_section:
2001-10-28 04:23:37 +00:00
key = QFK_WORLD_7;
break;
case XK_egrave:
2001-10-28 04:23:37 +00:00
key = QFK_WORLD_72;
break;
case XK_ccedilla:
2001-10-28 04:23:37 +00:00
key = QFK_WORLD_71;
break;
case XK_agrave:
2001-10-28 04:23:37 +00:00
key = QFK_WORLD_64;
break;
default:
2002-08-21 00:04:35 +00:00
if (keysym < 128) { // ASCII keys
key = keysym;
if ((key >= 'A') && (key <= 'Z')) {
key = key + ('a' - 'A');
}
}
break;
}
*k = key;
*u = unicode;
}
static void
event_key (XEvent *event)
{
int key, unicode;
x_time = event->xkey.time;
if (old_key_dest != key_dest) {
old_key_dest = key_dest;
if (key_dest == key_game) {
XAutoRepeatOff (x_disp);
} else {
XAutoRepeatOn (x_disp);
}
}
XLateKey (&event->xkey, &key, &unicode);
Key_Event (key, unicode, event->type == KeyPress);
}
static void
event_button (XEvent *event)
{
int but;
x_time = event->xbutton.time;
but = event->xbutton.button;
if (but == 2)
but = 3;
else if (but == 3)
but = 2;
switch (but) {
case 1:
case 2:
case 3:
2001-10-28 04:23:37 +00:00
Key_Event (QFM_BUTTON1 + but - 1, 0, event->type == ButtonPress);
break;
case 4:
2001-10-28 04:23:37 +00:00
Key_Event (QFM_WHEEL_UP, 0, event->type == ButtonPress);
break;
case 5:
2001-10-28 04:23:37 +00:00
Key_Event (QFM_WHEEL_DOWN, 0, event->type == ButtonPress);
break;
}
}
static void
event_focusout (XEvent *event)
{
x_have_focus = false;
XAutoRepeatOn (x_disp);
if (in_snd_block->int_val) {
S_BlockSound ();
CDAudio_Pause ();
}
X11_RestoreGamma ();
}
static void
event_focusin (XEvent *event)
{
x_have_focus = true;
if (key_dest == key_game)
XAutoRepeatOff (x_disp);
if (in_snd_block->int_val) {
S_UnblockSound ();
CDAudio_Resume ();
}
VID_UpdateGamma (vid_gamma);
}
static void
center_pointer (void)
{
XEvent event;
event.type = MotionNotify;
event.xmotion.display = x_disp;
event.xmotion.window = x_win;
event.xmotion.x = vid.width / 2;
event.xmotion.y = vid.height / 2;
XSendEvent (x_disp, x_win, False, PointerMotionMask, &event);
XWarpPointer (x_disp, None, x_win, 0, 0, 0, 0,
vid.width / 2, vid.height / 2);
}
static void
event_motion (XEvent *event)
{
x_time = event->xmotion.time;
if (dga_active) {
in_mouse_x += event->xmotion.x_root;
in_mouse_y += event->xmotion.y_root;
} else {
if (vid_fullscreen->int_val || in_grab->int_val) {
if (!event->xmotion.send_event) {
in_mouse_x += (event->xmotion.x - p_mouse_x);
in_mouse_y += (event->xmotion.y - p_mouse_y);
2001-08-27 01:00:03 +00:00
if (abs (vid.width / 2 - event->xmotion.x) > vid.width / 4 ||
abs (vid.height / 2 - event->xmotion.y) > vid.height / 4) {
center_pointer ();
}
}
} else {
in_mouse_x += (event->xmotion.x - p_mouse_x);
in_mouse_y += (event->xmotion.y - p_mouse_y);
}
p_mouse_x = event->xmotion.x;
p_mouse_y = event->xmotion.y;
}
}
static void
grab_error (int code, const char *device)
{
const char *reason;
switch (code) {
case AlreadyGrabbed:
reason = "already grabbed";
break;
case GrabNotViewable:
reason = "grab not viewable";
break;
case GrabFrozen:
reason = "grab frozen";
break;
case GrabInvalidTime:
reason = "grab invalid time";
break;
default:
reason = "unknown reason";
break;
}
2003-04-09 16:38:17 +00:00
Con_Printf ("failed to grab %s: %s\n", device, reason);
}
void
IN_LL_Grab_Input (int grab)
{
static int input_grabbed = 0;
if (!x_disp || !x_win)
return;
if (vid_fullscreen)
grab = grab || vid_fullscreen->int_val;
if ((input_grabbed && grab) || (!input_grabbed && !grab))
return;
if (grab) {
int ret;
ret = XGrabPointer (x_disp, x_win, True, MOUSE_MASK, GrabModeAsync,
GrabModeAsync, x_win, None, CurrentTime);
if (ret != GrabSuccess) {
grab_error (ret, "mouse");
return;
}
ret = XGrabKeyboard (x_disp, x_win, 1, GrabModeAsync, GrabModeAsync,
CurrentTime);
if (ret != GrabSuccess) {
XUngrabPointer (x_disp, CurrentTime);
grab_error (ret, "keyboard");
return;
}
input_grabbed = 1;
if (in_dga)
dga_on ();
} else {
XUngrabPointer (x_disp, CurrentTime);
XUngrabKeyboard (x_disp, CurrentTime);
input_grabbed = 0;
if (in_dga)
dga_off ();
}
}
void
IN_LL_ProcessEvents (void)
{
2002-08-21 05:59:00 +00:00
X11_ProcessEvents (); // Get events from X server.
}
void
IN_LL_Shutdown (void)
{
Con_Printf ("IN_LL_Shutdown\n");
in_mouse_avail = 0;
if (x_disp) {
XAutoRepeatOn (x_disp);
dga_off ();
}
X11_CloseDisplay ();
}
void
IN_LL_Init (void)
{
// open the display
if (!x_disp)
Sys_Error ("IN: No display!!");
if (!x_win)
Sys_Error ("IN: No window!!");
X11_OpenDisplay (); // call to increment the reference counter
{
int attribmask = CWEventMask;
XWindowAttributes attribs_1;
XSetWindowAttributes attribs_2;
XGetWindowAttributes (x_disp, x_win, &attribs_1);
attribs_2.event_mask = attribs_1.your_event_mask | INPUT_MASK;
XChangeWindowAttributes (x_disp, x_win, attribmask, &attribs_2);
}
X11_AddEvent (KeyPress, &event_key);
X11_AddEvent (KeyRelease, &event_key);
X11_AddEvent (FocusIn, &event_focusin);
X11_AddEvent (FocusOut, &event_focusout);
X11_AddEvent (SelectionNotify, &selection_notify);
X11_AddEvent (EnterNotify, &enter_notify);
if (!COM_CheckParm ("-nomouse")) {
dga_avail = VID_CheckDGA (x_disp, NULL, NULL, NULL);
X11_AddEvent (ButtonPress, &event_button);
X11_AddEvent (ButtonRelease, &event_button);
X11_AddEvent (MotionNotify, &event_motion);
2001-07-06 00:17:09 +00:00
in_mouse_avail = 1;
}
Cmd_AddCommand ("in_paste_buffer", in_paste_buffer_f,
"Paste the contents of X's C&P buffer to the console");
}
void
IN_LL_Init_Cvars (void)
{
2002-08-21 00:04:35 +00:00
in_snd_block = Cvar_Get ("in_snd_block", "0", CVAR_ARCHIVE, NULL,
"block sound output on window focus loss");
in_dga = Cvar_Get ("in_dga", "1", CVAR_ARCHIVE, in_dga_f,
2002-08-21 00:04:35 +00:00
"DGA Input support");
}
void
IN_LL_ClearStates (void)
{
}