aboutsummaryrefslogtreecommitdiff
path: root/src/ivypointer.c
blob: 47ab6aca44182abd74d32ab6944cbcfa3cfc2dfa (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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
 *	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
 */

//#define _MPX_

#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>
//#	include<X11/extensions/XInput.h>
//#	include<X11/extensions/shape.h>
#	define XK_MISCELLANY
#	define XK_XKB_KEYS
#	include <X11/keysymdef.h>
#   ifndef  IsXExtensionKeyboard
# 		define IsXExtensionKeyboard 3
#	endif
#   ifndef  IsXExtensionPointer
# 		define IsXExtensionPointer 4
#	endif
#	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_SCREEN "^screen_geometry_event device_id=%s x0=(.*) y0=(.*) x1=(.*) y1=(.*)"
#define REGEXP_CHANGE_CROP "^crop_event device_id=%s x0=(.*) y0=(.*) x1=(.*) y1=(.*)"
#define REGEXP_LOCK_UNLOCK "^lock_event device_id=%s status=(lock|unlock)"

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

#define CHAMP_X_TELEPOINTEUR 1
#define CHAMP_Y_TELEPOINTEUR 2

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

#define CHAMP_X_WACOM 12
#define CHAMP_Y_WACOM 13
#define CHAMP_TIME_WACOM 11
#define CHAMP_PRESSURE_WACOM 3

#define CHAMP_X_WACOM_BUTTON 14
#define CHAMP_Y_WACOM_BUTTON 15

#define CHAMP_X_WACOM_PREDICTIVE 7
#define CHAMP_Y_WACOM_PREDICTIVE 8

#define CHAMP_X_WACOM_PREDICTIVE_BUTTON 9
#define CHAMP_Y_WACOM_PREDICTIVE_BUTTON 10

#define REGEXP_POINTER_2 "^InputMoveEvent %s Abs_X=(\\S*) Abs_Y=(\\S*) Abs_Pressure=(\\S*).* time=(.*)"
#define REGEXP_BUTTON_WACOM_2 "^InputButtonEvent %s num=(.*) state=(.*) Abs_X=(.*) Abs_Y=(.*) Abs_Pressure=(\\S*).* time=(.*)"

#define CHAMP_X_WACOM_2 1
#define CHAMP_Y_WACOM_2 2
#define CHAMP_TIME_XINPUT_2 4
#define CHAMP_PRESSURE_XINPUT_2 3

#define CHAMP_X_WACOM_BUTTON_2 3
#define CHAMP_Y_WACOM_BUTTON_2 4

#define TIME_CLICK 200
#define TIME_BETWEEN_CLICK 200

enum input_type {
   WACOM,
   TELEPOINTER,
   XINPUT2
};

static void printHelpMsg(const char *name);

static Display* display = NULL;
#ifdef _MPX_
static XDevice* dev = NULL;
//#else
//# error plop
#endif
static float width,height;
static float coords_input[4] = {0.0,0.0,100.0,100.0};
static float width_input,height_input, offsetx, offsety;
static float horiz_ratio; // width / width_input
static float vert_ratio; // height / height_input

static int dragged = 0;
static int relative = 0;
static int switch_mode = 0;

static int locked = 0;

static int predictive_mode = 0;

static int champX;
static int champY;
static int champXButton;
static int champYButton;
static int champTime;
static int champPressure;

static float old_x = 0;
static float old_y = 0;

static int device_input_id_given = 0;

static 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 (!XQueryExtension(display, XTestExtensionName,
						&majorOpcode, &firstEvent, &firstError)) {
		printf("XTEST extension not available");
		XCloseDisplay(display);
		return NULL;
	}
	XTestGrabControl(display, True);
#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;
}

#ifdef _MPX_
static XDevice *openDevice(Display * dpy, int id){
	return XOpenDevice(dpy, id);
}
#endif

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

static int valid(float x, float y) {
	return !locked && x>=coords_input[0] && x<=coords_input[2] && y>=coords_input[1] && y<=coords_input[3];
}

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

static void
fakeMouseButton(const unsigned int xButton, Bool press, int x, int y)
{
	if (xButton != 0) {
#ifndef _MPX_
		XTestFakeButtonEvent(display, xButton, press ? True : False, CurrentTime);
#else
		int axes[2] = {x,y};
		XTestFakeDeviceButtonEvent(display, dev, xButton, press ? True : False, axes, 2, CurrentTime);
#endif
		XFlush(display);
	}
}

static void
fakeMouseMove(int x, int y)
{
//	printf("x = %d,y = %d;\n ",x,y);
#ifndef _MPX_
	XTestFakeMotionEvent(display, DefaultScreen(display), x, y, CurrentTime);
#else
	int axes[2] = {x,y};
	XTestFakeDeviceMotionEvent(display, dev, False, 0, axes, 2, CurrentTime);
#endif
	//XWarpPointer(display, None, RootWindow(display, 0), 0, 0, 0, 0, x, y);
	XFlush(display);
}

static void
fakeMouseRelativeMove(int dx, int dy)
{
#ifndef _MPX_
	XTestFakeRelativeMotionEvent(display, dx, dy, CurrentTime);
#else
	int axes[2] = {dx,dy};
	XTestFakeDeviceMotionEvent(display, dev, True, 0, axes, 2, CurrentTime);
#endif
	//XWarpPointer(display, None, None, 0, 0, 0, 0, dx, dy);
	XFlush(display);
}

// --------------------------------------------------------------------------
// Geometry callback
// --------------------------------------------------------------------------

static void CallbackScreen(IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	float x0 = atof(argv[1-device_input_id_given]);
	float y0 = atof(argv[2-device_input_id_given]);
	float x1 = atof(argv[3-device_input_id_given]);
	float y1 = atof(argv[4-device_input_id_given]);
	if (x1 > x0 && y1 > y0 ){
		offsetx = x0;
		offsety = y0;
		width = x1-x0;
		height = y1-y0;
		horiz_ratio = width / width_input;
		vert_ratio = height / height_input;
		printf("screen : %f+%f x %f+%f\n",offsetx,width,offsety,height);
	} else {
		printf("bad coords received : %.2f,%.2f %.2f,%.2f\n",x0,y0,x1,y1);
	}
}

// --------------------------------------------------------------------------
// Crop callback
// --------------------------------------------------------------------------

static void CallbackCrop(IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	float x0 = atof(argv[1-device_input_id_given]);
	float y0 = atof(argv[2-device_input_id_given]);
	float x1 = atof(argv[3-device_input_id_given]);
	float y1 = atof(argv[4-device_input_id_given]);
	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];
		horiz_ratio = width / width_input;
		vert_ratio = height / height_input;
		printf("crop : %f %f %f %f\n",x0, y0, x1, y1);
	} else {
		printf("bad coords received : %.2f,%.2f %.2f,%.2f\n",x0,y0,x1,y1);
	}
}

// --------------------------------------------------------------------------
// Lock/Unlock callback
// --------------------------------------------------------------------------

static void CallbackLockUnlock(IvyClientPtr app, void *user_data, int argc, char *argv[]) {
	locked = strcmp(argv[1-device_input_id_given],"unlock");
}

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

static 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]);
	float x = atof(argv[champX]);
	float y = atof(argv[champY]);
	if (!valid(x,y)){
		// ignore outside events
		return;
	}
	x = offsetx + (x - coords_input[0]) * horiz_ratio;
	y = offsety + (y - coords_input[1]) * vert_ratio;
//	printf("x = %f,y = %f\n",x,y);
	old_x = x;
	old_y = y;
	fakeMouseMove((int)x,(int)y);
	if (dragged){
		fakeMouseButton(1,0,x,y);
		dragged = 0;
	}
}


static void CallbackDrag (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	float x = atof(argv[champX]);
	float y = atof(argv[champY]);
	if (!valid(x,y)){
		// ignore outside events
		return;
	}
	x = offsetx + (x - coords_input[0]) * horiz_ratio;
	y = offsety + (y - coords_input[1]) * vert_ratio;
//	printf("x = %f,y = %f\n",x,y);
	old_x = x;
	old_y = y;
	fakeMouseMove((int)x,(int)y);
	if (!dragged){
		fakeMouseButton(1,1,x,y);
		dragged = 1;
	}
}

static void CallbackRelMove (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	float dx = atof(argv[1-device_input_id_given]);
	float dy = atof(argv[2-device_input_id_given]);
	fakeMouseRelativeMove((int)dx,(int)dy);
}

static void CallbackButton (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	int b = atoi(argv[1-device_input_id_given]);
	int press = atoi(argv[2-device_input_id_given]);
	if (press && !valid(old_x,old_y)){
		// ignore outside release events
		return;
	}
	fakeMouseButton(b,press,0,0);
}

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

static void CallbackPointer (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
	//	char *id = argv[0];
	float x = atof(argv[champX]); // predicted position
	float y = atof(argv[champY]); // predicted position
	int time = atoi(argv[champTime]);
	int pressure = atoi(argv[champPressure]);
	static int old_time = 0;
	static int should_click = 0;
	static int clicked = 0;
	if (!valid(x,y)){
		// ignore outside events
		return;
	}

	if (!predictive_mode) {
		// in predictive mode, we don't allow the geometry to be changed
		x = offsetx + (x - coords_input[0]) * horiz_ratio;
		y = offsety + (y - coords_input[1]) * vert_ratio;
	}

	if (!relative) {
		//absolute mode
		fakeMouseMove((int)x,(int)y);
	} else {
		//relative mode
		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 (pressure > 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,x,y);
				}
//				printf("cli\n");
				clicked = 1;
				old_time = time;
				fakeMouseButton(1,1,x,y);
			}
			should_click = 0;
			if (clicked) {
				if (dt > TIME_BETWEEN_CLICK){
//					printf("ck\n");
					fakeMouseButton(1,0,x,y);
					clicked = 0;
				}
			} else {
				old_time = 0;
			}
		}
	}

	// release (common of both modes)
	if (dragged && pressure==0){
		fakeMouseButton(1,0,x,y);
		dragged = 0;
	}

	old_x = x;
	old_y = y;
}

static void CallbackPad (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
	if (!valid(old_x,old_y)){
		// ignore outside events
		return;
	}
	int b = atoi(argv[1-device_input_id_given]);
	int press = 0;
//	if (!b)
//		return
	if (strcmp(argv[2-device_input_id_given],"up")){
		press = 1;
	}
//	printf("Pad button=%d pressed%d event received\n",b, press);
	if (switch_mode && (b==11 || b==15 || (press && (b==12 || b==16)))){
		//inversion du mode
		relative = !relative;
	} else {
		fakeMouseButton(b,press,0,0);
	}
}

static 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);
}

static void CallbackButtonWacom (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	int b = atoi(argv[1-device_input_id_given]);
	if (b == 1 && relative) // gestion propre du bouton 1
		return;
	int press = strcmp(argv[2-device_input_id_given],"up");
	float x = atof(argv[champXButton]);
	float y = atof(argv[champYButton]);
	if (press && !valid(x,y)){
		// ignore outside release events
		return;
	}

	fakeMouseButton(b,press,0,0);
}

static void CallbackButtonXInput2 (IvyClientPtr app, void *user_data, int argc, char *argv[]) {
//	char *id = argv[0];
	int b = atoi(argv[1-device_input_id_given]);
	if (b == 1 && relative) // gestion propre du bouton 1
		return;
	int press = !!strcmp(argv[2-device_input_id_given],"released");
	float x = atof(argv[champXButton]);
	float y = atof(argv[champYButton]);
	if (press && !valid(x,y)){
		// ignore outside release events
		return;
	}

	fakeMouseButton(b,press,0,0);
}

static int extractValues(char optarg[], float* output, const char name[]){
	int i = 0;
	int j = 0;
	int last = 0;
	float coupures[4] = {0,0,0,0};

	while(optarg[i]){
		if (optarg[i] == ','){
			coupures[j] = i;
			j++;
			if (j > 3) {
				printHelpMsg(name);
				exit(1);
			}
		}
		i++;
	}
	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];
		}
		output[i] = atof(buf);
		last = coupures[i]+1;
	}
	return 1;
}

static void printHelpMsg(const char *name){
	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-2\t\tuse data from xinput2 instead of xinput_wacom\n"
	  "\t-t\t\tuse data from ModeManager instead of xinput_wacom\n"
	  "\t-C x0,y0,x1,y1\tspecify the coords of the screen used\n"
	  "\t-d device\tuse \"device\" as the source of the events\n"
	  "\t-r\tuse relative mode by default\n"
	  "\t-s\tenable the ability to dynamically switch between relative and absolute\n"
	  "\t-p\tuse predictive mode (disable geometry)\n"
	  "\t-v\t\tprints the ivy relase number\n\n"
	;
	printf("usage: %s %s",name,helpmsg);
}

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

int main(int argc, char *argv[]) {
	const char* bus = 0;
	char busbuf [1024] = "";
	char device_input_id [1024] = "(.*)";
	char ivyName [1024] = "IvyPointer";
	enum input_type input_type = WACOM;
	float user_coordsO = 0;
	float coordsO[4] = {0,0,0,0};
	int c;
	int id = -1;
	while ((c = getopt(argc, argv, "vb:2td:C:rpi:sc:")) != 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 'r':
				relative = 1;
				break;
			case 'd':
				strcpy (device_input_id, optarg);
				device_input_id_given = 1;
				break;
			case '2':
				input_type = XINPUT2;
				break;
			case 't':
				input_type = TELEPOINTER;
				break;
			case 'p':
				predictive_mode = 1;
				break;
			case 'C':
				user_coordsO = extractValues(optarg, coordsO, argv[0]);
				break;
			case 'i':
				id = atoi(optarg);
				break;
			case 's':
				switch_mode = 1;
				break;
			case 'c':
				printf("option '-c' disapeared since 0.0.7\n");
				break;
			default:
				printf("bad option : %s\n",(char*)&c);
				printHelpMsg(argv[0]);
				exit(1);
			}
	Display* d = openDisplay(NULL);
	if (!d){
		return 1;
	}
#ifdef _MPX_
	if (id >=0){
		printf("opening device id=%d\n",id);
		dev = openDevice(d,id);
		if (!dev) {
			printf("unable to open device id=%d\n",id);
		}
	}
#endif

	if (!predictive_mode && user_coordsO){
		offsetx = coordsO[0];
		offsety = coordsO[1];
		width = coordsO[2]-coordsO[0];
		height = coordsO[3]-coordsO[1];
	} else {
		offsetx = 0;
		offsety = 0;
		width = DisplayWidth(d, DefaultScreen(d));
		height = DisplayHeight(d, DefaultScreen(d));
	}

	printf("screen : %.2f+%.2f x %.2f+%.2f\n",offsetx,width,offsety,height);

	// verifications
	if (width < 2 || height < 2 ){
		printf("screen geometry : %.2f %.2f\n",width,height);
		printHelpMsg(argv[0]);
		exit(1);
	}

	switch (input_type) {
	case WACOM:
		if (predictive_mode){
			coords_input[2] = width;
			coords_input[3] = height;
		} else {
			coords_input[2] = 1.0;
			coords_input[3] = 1.0;
		}
		//~ print_coords_input();
		if (!predictive_mode) {
			champX = CHAMP_X_WACOM;
			champY = CHAMP_Y_WACOM;
			champXButton = CHAMP_X_WACOM_BUTTON;
			champYButton = CHAMP_Y_WACOM_BUTTON;
		} else {
			champX = CHAMP_X_WACOM_PREDICTIVE;
			champY = CHAMP_Y_WACOM_PREDICTIVE;
			champXButton = CHAMP_X_WACOM_PREDICTIVE_BUTTON;
			champYButton = CHAMP_Y_WACOM_PREDICTIVE_BUTTON;
		}
		champTime = CHAMP_TIME_WACOM;
		champPressure = CHAMP_PRESSURE_WACOM;
		break;
	case XINPUT2:
		coords_input[2] = 104480.0;
		coords_input[3] = 65600.0;
		champX = CHAMP_X_WACOM_2;
		champY = CHAMP_Y_WACOM_2;
		champXButton = CHAMP_X_WACOM_BUTTON_2;
		champYButton = CHAMP_Y_WACOM_BUTTON_2;
		champTime = CHAMP_TIME_XINPUT_2;
		champPressure = CHAMP_PRESSURE_XINPUT_2;
		break;
	default:
		champX = CHAMP_X_TELEPOINTEUR;
		champY = CHAMP_Y_TELEPOINTEUR;
	}

	champX -= device_input_id_given;
	champY -= device_input_id_given;
	champXButton -= device_input_id_given;
	champYButton -= device_input_id_given;
	champTime -= device_input_id_given;
	champPressure -= device_input_id_given;

	width_input  = coords_input[2]-coords_input[0];
	height_input = coords_input[3]-coords_input[1];

	horiz_ratio = width / width_input;
	vert_ratio = height / height_input;

	MsgRcvPtr ptrMove,ptrRelMove,ptrButton,ptrGeometry,ptrLockUnlock;

	if (device_input_id_given) {
		sprintf(ivyName, "IvyPointer:%s", device_input_id);
	}

	IvyInit(ivyName,"IvyPointer Ready",NULL,NULL,NULL,NULL);

	char regexp[2048] = "";
	switch (input_type) {
	case TELEPOINTER:
	// telepointer callbacks

		sprintf(regexp, REGEXP_MOVE, device_input_id);
		ptrMove=IvyBindMsg(CallbackMove,&ptrMove,REGEXP_MOVE, device_input_id);
		printf("bound to %s\n",regexp);

		sprintf(regexp, REGEXP_DRAG, device_input_id);
		ptrMove=IvyBindMsg(CallbackDrag,&ptrMove, REGEXP_DRAG, device_input_id);
		printf("bound to %s\n",regexp);

		sprintf(regexp, REGEXP_REL_MOVE, device_input_id);
		ptrMove=IvyBindMsg(CallbackRelMove,&ptrRelMove, REGEXP_REL_MOVE, device_input_id);
		printf("bound to %s\n",regexp);

		sprintf(regexp, REGEXP_BUTTON, device_input_id);
		ptrMove=IvyBindMsg(CallbackButton,&ptrButton, REGEXP_BUTTON, device_input_id);
		printf("bound to %s\n",regexp);
		break;
	case WACOM:
	//wacom callbacks
		sprintf(regexp, REGEXP_POINTER, device_input_id);
		ptrMove=IvyBindMsg(CallbackPointer,&ptrMove, REGEXP_POINTER, device_input_id);
		printf("bound to %s\n",regexp);

//	   ptrMove=IvyBindMsg(CallbackSlider,&ptrMove,REGEXP_SLIDER);
//	   printf("bound to %s\n",REGEXP_SLIDER);

		sprintf(regexp, REGEXP_PAD, device_input_id);
		ptrMove=IvyBindMsg(CallbackPad,&ptrButton, REGEXP_PAD, device_input_id);
		printf("bound to %s\n",regexp);

		sprintf(regexp, REGEXP_BUTTON_WACOM, device_input_id);
		ptrMove=IvyBindMsg(CallbackButtonWacom,&ptrButton, REGEXP_BUTTON_WACOM, device_input_id);
		printf("bound to %s\n",regexp);
		break;
	case XINPUT2:
		sprintf(regexp, REGEXP_POINTER_2, device_input_id);
		ptrMove=IvyBindMsg(CallbackPointer,&ptrMove, REGEXP_POINTER_2, device_input_id);
		printf("bound to %s\n",regexp);

		sprintf(regexp, REGEXP_BUTTON_WACOM_2, device_input_id);
		ptrMove=IvyBindMsg(CallbackButtonXInput2,&ptrButton, REGEXP_BUTTON_WACOM_2, device_input_id);
		printf("bound to %s\n",regexp);
		break;
	}

	if (!predictive_mode) {
		sprintf(regexp, REGEXP_CHANGE_GEOMETRY_SCREEN, device_input_id);
		ptrGeometry=IvyBindMsg(CallbackScreen,&ptrGeometry, REGEXP_CHANGE_GEOMETRY_SCREEN, device_input_id);
		printf("bound to %s\n",regexp);
	}

	sprintf(regexp, REGEXP_CHANGE_CROP, device_input_id);
	ptrGeometry=IvyBindMsg(CallbackCrop,&ptrGeometry, REGEXP_CHANGE_CROP, device_input_id);
	printf("bound to %s\n",regexp);

	sprintf(regexp, REGEXP_LOCK_UNLOCK, device_input_id);
	ptrLockUnlock=IvyBindMsg(CallbackLockUnlock,&ptrLockUnlock, REGEXP_LOCK_UNLOCK, device_input_id);
	printf("bound to %s\n",regexp);

	IvyStart(bus);
#if (IVYMAJOR_VERSION == 3) && (IVYMINOR_VERSION < 9)
	IvyMainLoop(NULL, NULL);
#else
	IvyMainLoop();
#endif
  return 0;
}