src/spacenav-lib-hack.c
author František Kučera <franta-hg@frantovo.cz>
Fri, 08 Mar 2019 23:05:15 +0100
branchv_0
changeset 4 650e0a578dab
parent 3 d8793f61ee43
child 5 26e9fb705faf
permissions -rw-r--r--
documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     2
 * Spacenav lib hack
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
0
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
#define _GNU_SOURCE
0
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    20
#include <stdio.h>
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <stdint.h>
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
#include <dlfcn.h>
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    23
#include <stdlib.h>
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    24
#include <unistd.h>
0
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <X11/Xlib.h>
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <spnav.h>
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
4
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    29
/**
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    30
 * The purpose of this hack is to enable 3D mouse in applications that use X11 protocol
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    31
 * while our daemon is providing only domain socket and no X11.
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    32
 * 
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    33
 * Usage:
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    34
 * LD_PRELOAD=./spacenav-lib-hack/build/Debug/src/libspnav-lib-hack.so freecad
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    35
 * 
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    36
 * This hack:
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    37
 * 1) Pretends that spnav_x11_open() was successful.
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    38
 * 2) Starts a background process (TODO: also terminate it) which reads from the domain socket
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    39
 *    and translates spacenav events to X11 events and sends them to the application window.
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
 * 3) reimplements spnav_x11_event() which is needed, because Atom variables in original library
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
 *    were not initialized spnav_x11_open() and thus the library funcion is not working.
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
 * 
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
 * This solution works and allows using e.g. FreeCAD with domain socket instead of X11, but:
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
 *	- the background process never ends and must be terminated by: killall freecad
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
 *  - is very hackish and should be reimplemented as a separate process which will work partly
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
 *    as a libspnav client and partly emulate spacenavd daemon and will bridge the domain socket
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
 *    and th X11. Other solution is to emulate the hardware and keep spacenavd and libspnav untouched.
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
 */
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    49
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
// from proto_x11.c (spacenavd)
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
static Atom xa_event_motion, xa_event_bpress, xa_event_brelease, xa_event_cmd;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
static float x11_sens = 1.0;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
/**
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
 * This function is based on proto_x11.c (spacenavd) written by:
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
 * Copyright (C) 2007-2019 John Tsiombikas <nuclear@member.fsf.org>
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
 * and published under GNU GPLv3+
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
 * 
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
 * @param ev
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
 * @param dpy
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
 * @param win
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
 */
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
static void send_xevent(spnav_event *ev, Display* dpy, Window win) {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
	int i;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
	XEvent xevent;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
	if (!dpy) return;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
	// if(setjmp(jbuf)) return;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
	xevent.type = ClientMessage;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
	xevent.xclient.send_event = False;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
	xevent.xclient.display = dpy;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
	xevent.xclient.window = win;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
	switch (ev->type) {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
		case SPNAV_EVENT_MOTION:
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
			xevent.xclient.message_type = xa_event_motion;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
			xevent.xclient.format = 16;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
			for (i = 0; i < 6; i++) {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
				float val = (float) ev->motion.data[i] * x11_sens;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
				xevent.xclient.data.s[i + 2] = (short) val;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
			}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
			xevent.xclient.data.s[0] = xevent.xclient.data.s[1] = 0;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
			xevent.xclient.data.s[8] = ev->motion.period;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
			break;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
		case SPNAV_EVENT_BUTTON:
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
			xevent.xclient.message_type = ev->button.press ? xa_event_bpress : xa_event_brelease;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
			xevent.xclient.format = 16;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
			xevent.xclient.data.s[2] = ev->button.bnum;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
			break;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
		default:
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
			break;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
	}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    98
4
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    99
	XSendEvent(dpy, win, False, 0, &xevent);
650e0a578dab documentation
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   100
	XFlush(dpy);
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
}
0
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   103
static void spacenav_hack_translate_events(Window win) {
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
	spnav_event event;
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   105
	Display* dpy = XOpenDisplay(0);
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   106
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
	while (1) {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
		if (spnav_wait_event(&event)) {
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   109
			send_xevent(&event, dpy, win);
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   110
		}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   111
	}
3
d8793f61ee43 a bit cleaner version (but still with an infinite loop)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   112
	// FIXME: this process never ends and must be killed manually
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
int spnav_x11_open(Display* dpy, Window win) {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   116
	int result = spnav_open();
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   117
	fprintf(stderr, "spnav-lib-hack: instead of spnav_x11_open(%p, 0x%lx) calling spnav_open() = %d\n", dpy, win, result);
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   118
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   119
	xa_event_motion = XInternAtom(dpy, "MotionEvent", False);
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   120
	xa_event_bpress = XInternAtom(dpy, "ButtonPressEvent", False);
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
	xa_event_brelease = XInternAtom(dpy, "ButtonReleaseEvent", False);
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
	xa_event_cmd = XInternAtom(dpy, "CommandEvent", False);
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   123
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   124
	if (result == 0) {
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   125
		pid_t pid = fork();
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   126
		fprintf(stderr, "spnav-lib-hack: fork() = %d; PID=%d\n", pid, getpid());
3
d8793f61ee43 a bit cleaner version (but still with an infinite loop)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   127
		if (pid == -1) return 1; // error
d8793f61ee43 a bit cleaner version (but still with an infinite loop)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   128
		if (pid == 0) spacenav_hack_translate_events(win); // child
d8793f61ee43 a bit cleaner version (but still with an infinite loop)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   129
		else; // parent
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   130
	}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   131
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   132
	return result;
0
37aa134ae57b empty QT project linked to spnav library
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
}
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   134
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   135
/**
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   136
 * This function is based on proto_x11.c (spacenavd) written by:
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   137
 * Copyright (C) 2007-2019 John Tsiombikas <nuclear@member.fsf.org>
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   138
 * and published under GNU GPLv3+
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   139
 * 
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   140
 * @param dpy
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   141
 * @param win
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   142
 * @return 
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   143
 */
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   144
int spnav_x11_event(const XEvent* xev, spnav_event* event) {
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   145
	/*
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   146
	 * Because real spnav_x11_open() was not called, the library has not initialized xa_event_*
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   147
	 * variables and thus will return 0 (= not a spacenav event).
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   148
	 */
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   149
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   150
	int i;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   151
	int xmsg_type;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   152
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   153
	if (xev->type != ClientMessage) {
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   154
		return 0;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   155
	}
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   156
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   157
	xmsg_type = xev->xclient.message_type;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   158
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   159
	if (xmsg_type != xa_event_motion && xmsg_type != xa_event_bpress &&
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   160
			xmsg_type != xa_event_brelease) {
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   161
		return 0;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   162
	}
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   163
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   164
	if (xmsg_type == xa_event_motion) {
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   165
		event->type = SPNAV_EVENT_MOTION;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   166
		event->motion.data = &event->motion.x;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   167
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   168
		for (i = 0; i < 6; i++) {
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   169
			event->motion.data[i] = xev->xclient.data.s[i + 2];
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   170
		}
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   171
		event->motion.period = xev->xclient.data.s[8];
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   172
	} else {
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   173
		event->type = SPNAV_EVENT_BUTTON;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   174
		event->button.press = xmsg_type == xa_event_bpress ? 1 : 0;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   175
		event->button.bnum = xev->xclient.data.s[2];
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   176
	}
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   177
	return event->type;
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   178
3
d8793f61ee43 a bit cleaner version (but still with an infinite loop)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   179
	// real_spnav_x11_event() is not working (see comment above):
d8793f61ee43 a bit cleaner version (but still with an infinite loop)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   180
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   181
	/*
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   182
	static int (*real_spnav_x11_event)(const XEvent*, spnav_event*) = NULL;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   183
	if (!real_spnav_x11_event) real_spnav_x11_event = dlsym(RTLD_NEXT, "spnav_x11_event");
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   184
	int result = real_spnav_x11_event(xev, event);
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   185
	if (result == SPNAV_EVENT_MOTION || result == SPNAV_EVENT_BUTTON) fprintf(stderr, "spnav-lib-hack: spnav_x11_event() = %d\n", result);
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   186
	return result;
2
3ba27504be0e very hackish and… working
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   187
	 */
1
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   188
}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   189
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   190
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   191
/*
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   192
int spnav_x11_open(Display* dpy, Window win) {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   193
	static int (*real_spnav_x11_open)(Display*, Window) = NULL;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   194
	if (!real_spnav_x11_open) real_spnav_x11_open = dlsym(RTLD_NEXT, "spnav_x11_open");
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   195
	int result = real_spnav_x11_open(dpy, win);
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   196
	fprintf(stderr, "spnav-lib-hack: spnav_x11_open() = %d\n", result);
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   197
	return result;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   198
}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   199
 */
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   200
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   201
/*
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   202
int spnav_open() {
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   203
	static int (*real_spnav_open)() = NULL;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   204
	if (!real_spnav_open) real_spnav_open = dlsym(RTLD_NEXT, "spnav_open");
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   205
	int result = real_spnav_open();
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   206
	// fprintf(stderr, "spnav-lib-hack: spnav_open() = %d\n", result);
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   207
	return result;
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   208
}
ac5f3768ebb1 almost working, but there is an SIGABRT
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   209
 */