aboutsummaryrefslogtreecommitdiff
path: root/src/ivypointer.c
blob: 984ec436a4d7801ccbd1bd26175abae9e7453e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 *	Ivypointer
 *
 *	Copyright (C) 2007
 *	DGAC / DSNA / DTI / R&D
 *
 * 	Main and only file
 *
 *	Authors: Benjamin Tissoires <tissoire@cena.fr>
 *
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if X_DISPLAY_MISSING
#	error X11 is required to build xtest
#else
#	include <X11/Xlib.h>
#	include <X11/X.h>
#	include <X11/Xutil.h>
#	define XK_MISCELLANY
#	define XK_XKB_KEYS
#	include <X11/keysymdef.h>
#	if HAVE_X11_EXTENSIONS_XTEST_H
#		include <X11/extensions/XTest.h>
//#	else
//#		error The XTest extension is required to build xtest
#	endif
#	if HAVE_X11_EXTENSIONS_XINERAMA_H
		// Xinerama.h may lack extern "C" for inclusion by C++
		extern "C" {
#		include <X11/extensions/Xinerama.h>
		}
#	endif
#	if HAVE_XKB_EXTENSION
#		include <X11/XKBlib.h>
#	endif
#endif

#include <Ivy/ivyloop.h>
#include <Ivy/ivy.h>
#include <Ivy/version.h>


#include "getopt.h"

#define REGEXP_CHANGE_GEOMETRY_INPUT "^geometry_event device_id=(.*) x0=(.*) y0=(.*) x1=(.*) y1=(.*)"

#define REGEXP_MOVE "^TelepointerMotion Id=(.*) X=(.*) Y=(.*)"
#define REGEXP_DRAG "^TelepointerDrag Id=(.*) X=(.*) Y=(.*)"
#define REGEXP_REL_MOVE "^TelepointerRelativeMotion Id=(.*) X=(.*) Y=(.*)"
#define REGEXP_BUTTON "^TelepointerButton Id=(.*) Button=(.*) status=(.*)"

#define REGEXP_POINTER "^pointer_event device_id=(.*) x=(.*) y=(.*) presure=(.*) tilt_x=(.*) tilt_y=(.*) wheel=(.*) predicted_x=(.*) predicted_y=(.*) type=(.*) serial_number=(.*) time=(.*)"
#define REGEXP_PAD "^pad_event device_id=(.*) button=(.*) status=(.*) time=(.*)"
#define REGEXP_SLIDER "^slider_event device_id=(.*) value=(.*) side=(.*) time=(.*)"

#define WIDTH_WACOM 1600.0
#define HEIGHT_WACOM 1200.0
#define TIME_CLICK 200
#define TIME_BETWEEN_CLICK 200

Display* display = NULL;
int width,height;
float coords_input[4] = {0.0,0.0,100.0,100.0};
int width_input,height_input;

int dragged = 0;
int relative = 1;

Display*
openDisplay(const char* displayName)
{
	// get the DISPLAY
	if (displayName == NULL) {
		displayName = getenv("DISPLAY");
		if (displayName == NULL) {
			displayName = ":0.0";
		}
	}

	// open the display
	printf("XOpenDisplay(\"%s\")\n", displayName);
	display = XOpenDisplay(displayName);
	if (display == NULL) {
	    printf("EE : can't open display \"%s\"\n", displayName);
	    return NULL;
//		throw XScreenUnavailable(60.0);
	}

	// verify the availability of the XTest extension
    int majorOpcode, firstEvent, firstError;
#if HAVE_X11_EXTENSIONS_XTEST_H
	if (!XQueryExtension(display, XTestExtensionName,
						&majorOpcode, &firstEvent, &firstError)) {
		printf("XTEST extension not available");
		XCloseDisplay(display);
	    return NULL;
	}
#endif
#if HAVE_XKB_EXTENSION
	{
		m_xkb = false;
		int major = XkbMajorVersion, minor = XkbMinorVersion;
		if (XkbLibraryVersion(&major, &minor)) {
			int opcode, firstError;
			if (XkbQueryExtension(display, &opcode, &m_xkbEventBase,
								&firstError, &major, &minor)) {
				m_xkb = true;
				XkbSelectEvents(display, XkbUseCoreKbd,
								XkbMapNotifyMask, XkbMapNotifyMask);
				XkbSelectEventDetails(display, XkbUseCoreKbd,
								XkbStateNotifyMask,
								XkbGroupStateMask, XkbGroupStateMask);
			}
		}
	}
#endif

	return display;
}

// --------------------------------------------------------------------------
// tools
// --------------------------------------------------------------------------

void print_coords_input(void){
    printf("coords of input : %.0f,%.0f %.0f,%.0f\n", coords_input[0], coords_input[1], coords_input[2], coords_input[3]);
}

int valid(float x, float y) {
    if (x<coords_input[0]) {
//	printf("x < x0\n");
        return 0;
    }
    if (x>coords_input[2]) {
//        printf("x > x1\n");
        return 0;
    }
    if (y<coords_input[1]){
//        printf("y < y0\n");
        return 0;
    }
    if (y>coords_input[3]){
//        printf("y < y1\n");
        return 0;
    }
    return 1;
}

// --------------------------------------------------------------------------
// X11 functions
// --------------------------------------------------------------------------

void
fakeMouseButton(const unsigned int xButton, Bool press)
{
#if HAVE_X11_EXTENSIONS_XTEST_H
	if (xButton != 0) {
		XTestFakeButtonEvent(display, xButton, press ? True : False, 0);
		XFlush(display);
	}
#endif
}

void
fakeMouseMove(int x, int y)
{
	XWarpPointer(display, None, RootWindow(display, 0), 0, 0, 0, 0, x, y);
	XFlush(display);
}

void
fakeMouseRelativeMove(int dx, int dy)
{
	XWarpPointer(display, None, None, 0, 0, 0, 0, dx, dy);
	XFlush(display);
}

// --------------------------------------------------------------------------
// Geometry callback
// --------------------------------------------------------------------------
void CallbackGeometry(IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//    char *id = argv[0];
    float x0 = atof(argv[1]);
    float y0 = atof(argv[2]);
    float x1 = atof(argv[3]);
    float y1 = atof(argv[4]);
    if (x1 > x0 && y1 > y0 ){
        coords_input[0] = x0;
        coords_input[1] = y0;
        coords_input[2] = x1;
        coords_input[3] = y1;
        width_input  = coords_input[2]-coords_input[0];
        height_input = coords_input[3]-coords_input[1];
        print_coords_input();
    } else {
        printf("bad coords received : %.0f,%.0f %.0f,%.0f\n",x0,y0,x1,y1);
    }
}

// --------------------------------------------------------------------------
// Telepointer callbacks
// --------------------------------------------------------------------------

void CallbackMove (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//    printf ("%s sent (size : %d)",IvyGetApplicationName(app),argc);
//    for  (i = 0; i < argc; i++)
//			printf(" '%s'",argv[i]);
    if (dragged){
        fakeMouseButton(1,0);
        dragged = 0;
    }
//    char *id = argv[0];
    float x = atof(argv[1]);
    float y = atof(argv[2]);
//    printf("x = %f,y = %f; ",x,y);
    x = x*width/100.0;
    y = y*height/100.0;
//    printf("x = %f,y = %f\n",x,y);
    if (valid(x,y))
        fakeMouseMove((int)x,(int)y);
//  IvyUnbindMsg(*ptr);
}


void CallbackDrag (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
    if (!dragged){
        fakeMouseButton(1,1);
        dragged = 1;
    }
//    char *id = argv[0];
    float x = atof(argv[1]);
    float y = atof(argv[2]);
    x *= width/100.0;
    y *= height/100.0;
//    printf("x = %f,y = %f\n",x,y);
    if (valid(x,y))
        fakeMouseMove((int)x,(int)y);
}

void CallbackRelMove (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//    char *id = argv[0];
    float x = atof(argv[1]);
    float y = atof(argv[2]);
    x *= width/100.0;
    y *= height/100.0;
    fakeMouseRelativeMove((int)x,(int)y);
}

void CallbackButton (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//    char *id = argv[0];
    int b = atoi(argv[1]);
    int press = atoi(argv[2]);
//    if (!b)
//        return
    fakeMouseButton(b,press);
}

// --------------------------------------------------------------------------
// Wacom callbacks
// --------------------------------------------------------------------------

void CallbackPointer (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//    char *id = argv[0];
    float x = atof(argv[1]);
    float y = atof(argv[2]);
    int time = atoi(argv[11]);
    int presure = atoi(argv[3]);
    static float old_x = 0;
    static float old_y = 0;
    static int old_time = 0;
    static int should_click = 0;
    static int clicked = 0;
    if (dragged && presure==0){
       	fakeMouseButton(1,0);
       	dragged = 0;
    }
    if (!relative) {
    	if (!dragged && presure>0){
        	fakeMouseButton(1,1);
	        dragged = 1;
    	}
    }
//    printf("x = %f,y = %f; ",x,y);
    if (valid(x,y)){
        x = (x-coords_input[0])*width/width_input;
        y = (y-coords_input[1])*height/height_input;
//        printf("x = %f,y = %f\n",x,y);
        if (!relative) {
            fakeMouseMove((int)x,(int)y);
        } else {
	    int dx = (int)(x-old_x);
            int dy = (int)(y-old_y);
	    int dt = time-old_time;
//	    if (old_time)
//        	printf("time=%d, old_time=%d, dt=%d\n",time, old_time, dt);
            if (presure > 0) {
		if (!should_click){
		    old_time = time;
		    dt = 0;
		    should_click = 1;
//		    printf("armement du timeout\n");
		} else {
		    if (should_click ==1 && dt > TIME_CLICK){
			should_click = 2;
//			printf("timeout\n");
		    }
		}
                fakeMouseRelativeMove(dx,dy);
            } else {
		if (should_click == 1) {
//		    printf("dt = %d\n",dt);
		    if (clicked) {
			//double click
//			printf("ck\n");
                        fakeMouseButton(1,0);
		    }
//		    printf("cli\n");
		    clicked = 1;
		    old_time = time;
		    fakeMouseButton(1,1);
 		}
       		should_click = 0;
		if (clicked) {
			if (dt > TIME_BETWEEN_CLICK){
//                            printf("ck\n");
                            fakeMouseButton(1,0);
			    clicked = 0;
			}
                } else {
		    old_time = 0;
		}
	    }
	}
        old_x = x;
        old_y = y;
//    } else {
//	printf("bad event\n");
    }
}

void CallbackPad (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//    char *id = argv[0];
    int b = atoi(argv[1]);
    int press = 0;
//    if (!b)
//        return
    if (strcmp(argv[2],"up")){
        press = 1;
    }
//    printf("Pad button=%d pressed%d event received\n",b, press);
    if (b==11 || b==15){
        //mode relatif
        relative = !press;
    } else {
        fakeMouseButton(b,press);
    }
}

void CallbackSlider (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
    //TODO
//    char *id = argv[0];
//    int b = atoi(argv[1]);
//    int press = atoi(argv[2]);
//    if (!b)
//        return
//    fakeMouseButton(b,press);
}

// --------------------------------------------------------------------------
// Main
// --------------------------------------------------------------------------

int main(int argc, char *argv[]) {
	const char* bus = 0;
	char busbuf [1024] = "";
	const char* helpmsg =
	  "[options] [regexps]\n\t-b bus\t\tdefines the Ivy bus to which to connect to, defaults to 127:2010\n"
	  "\t-w\t\tuse data from xinput_wacom instead of ModeManager\n"
	  "\t-c x0,y0,x1,y1\tspecify the coords of the window used (only used with wacom)\n"
	  "\t-v\t\tprints the ivy relase number\n\n"
    ;
    int wacom = 0;
    int user_coords = 0;
    int coords[4] = {0,0,0,0};
	int c;
	int i = 0;
	int j = 0;
	int coupures[4] = {0,0,0,0};
	int last = 0;
	while ((c = getopt(argc, argv, "vb:wc:")) != EOF)
			switch (c) {
			case 'b':
				strcpy (busbuf, optarg);
				bus = busbuf;
				break;
			case 'v':
				printf("ivy c library version %d.%d\n",IVYMAJOR_VERSION,IVYMINOR_VERSION);
				break;
			case 'w':
				wacom = 1;
				break;
			case 'c':
			    while(optarg[i]){
			        if (optarg[i] == ','){
			            coupures[j] = i;
			            j++;
			        }
			        i++;
			    }
			    if (j != 3) {
			        printf("usage: %s %s",argv[0],helpmsg);
				    exit(1);
			    } else {
			        coupures[j] = i;
			    }
			    j = 0;
			    last = 0;
			    for (i=0; i< 4; i++){
    			    int length = coupures[i]-last;
			        char buf [1024] = "";
			        int k;
			        for (k = 0; k < length; k++) {
			            buf[k] = optarg[last+k];
			        }
			        coords[i] = atoi(buf);
			        last = coupures[i]+1;
			    }
			    user_coords = 1;
				break;
			default:
				printf("usage: %s %s",argv[0],helpmsg);
				exit(1);
			}
    Display* d = openDisplay(NULL);
    if (!d){
        return 1;
    }
    
    if (wacom) {
        if (user_coords){
            int i;
            for (i=0;i<4;i++) {
                coords_input[i] = coords[i];
            }
        } else {
            coords_input[2] = WIDTH_WACOM;
            coords_input[3] = HEIGHT_WACOM;
        }        
        print_coords_input();
        width_input  = coords_input[2]-coords_input[0];
        height_input = coords_input[3]-coords_input[1];
    }
    
    width = DisplayWidth(d, DefaultScreen(d));
    height = DisplayHeight(d, DefaultScreen(d));
    printf("screen : %dx%d\n",width,height);
    
    MsgRcvPtr ptrMove,ptrRelMove,ptrButton;
    IvyInit("IvyPointer","IvyPointer Ready",NULL,NULL,NULL,NULL);
    
    if (!wacom) {
    // telepointer callbacks
        ptrMove=IvyBindMsg(CallbackMove,&ptrMove,REGEXP_MOVE);
        printf("bound to %s\n",REGEXP_MOVE);
        ptrMove=IvyBindMsg(CallbackDrag,&ptrMove,REGEXP_DRAG);
        printf("bound to %s\n",REGEXP_DRAG);
        ptrMove=IvyBindMsg(CallbackRelMove,&ptrRelMove,REGEXP_REL_MOVE);
        printf("bound to %s\n",REGEXP_REL_MOVE);
        ptrMove=IvyBindMsg(CallbackButton,&ptrButton,REGEXP_BUTTON);
        printf("bound to %s\n",REGEXP_BUTTON);
    } else {
    //wacom callbacks
        ptrMove=IvyBindMsg(CallbackGeometry,&ptrMove,REGEXP_CHANGE_GEOMETRY_INPUT);
        printf("bound to %s\n",REGEXP_CHANGE_GEOMETRY_INPUT);
        ptrMove=IvyBindMsg(CallbackPointer,&ptrMove,REGEXP_POINTER);
        printf("bound to %s\n",REGEXP_POINTER);
//       ptrMove=IvyBindMsg(CallbackSlider,&ptrMove,REGEXP_SLIDER);
//       printf("bound to %s\n",REGEXP_SLIDER);
        ptrMove=IvyBindMsg(CallbackPad,&ptrButton,REGEXP_PAD);
        printf("bound to %s\n",REGEXP_PAD);
    }
    IvyStart(bus);
#if (IVYMAJOR_VERSION == 3) && (IVYMINOR_VERSION < 9)
    IvyMainLoop(NULL, NULL);
#else
    IvyMainLoop();
#endif
  return 0;
}