summaryrefslogtreecommitdiff
path: root/IvyCursor/IvyCursor.cpp
blob: 5b1a3d403fb91d08a39d5f7b8fb8c37691bbe4a2 (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
// IvyCursor.cpp : définit le point d'entrée pour l'application.
//

#include "stdafx.h"
#include "Ivy.h"
#include <winuser.h>
#include <crtdbg.h>
#include <shellapi.h>
#include <ostream>

#include "options.h"

#include "IvyCursor.h"
#define MAX_LOADSTRING 100

#define WM_ICONTRAY_NOTIFY (WM_USER+150)
// Variables globales :
HINSTANCE hInst;								// instance actuelle
TCHAR szTitle[MAX_LOADSTRING];					// Le texte de la barre de titre
TCHAR szWindowClass[MAX_LOADSTRING];			// le nom de la classe de fenêtre principale
HMENU hMenu;									// handle menu icontray
HMENU hmenuTrackPopup;

// Ivy 
Ivy* bus;

// Pré-déclarations des fonctions incluses dans ce module de code :
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

bool lock = false;

void ivyCursorMouve(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	UINT nb;
	INPUT Inputs;
	int x,y;
	double xf = atof( *argv++ );
	double yf = atof( *argv++ );
	x = (int)(65535 * xf / 100.0 + 0.5);
	y = (int)(65535 * yf  / 100.0 + 0.5);
	Inputs.type = INPUT_MOUSE;
	Inputs.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
	Inputs.mi.time = 0; // system provide it
	Inputs.mi.dx = x;
	Inputs.mi.dy = y;
	nb = SendInput( 1, &Inputs, sizeof(INPUT));

	if ( nb != 1 )
	{
		_RPT3(_CRT_WARN,"ivyCursorMouve can move to x=%d y=%d error=%d\n",x,y,GetLastError());
	}
}
void ivyCursorClick(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	UINT nb;
	INPUT Inputs[2];
	int x,y;
	double xf = atof( *argv++ );
	double yf = atof( *argv++ );
	x = (int)(65535 * xf / 100.0 + 0.5);
	y = (int)(65535 * yf  / 100.0 + 0.5);
	Inputs[0].type = INPUT_MOUSE;
	Inputs[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE;
	Inputs[0].mi.time = 0; // system provide it
	Inputs[0].mi.dx = x;
	Inputs[0].mi.dy = y;
	Inputs[1].type = INPUT_MOUSE;
	Inputs[1].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE;
	Inputs[1].mi.time = 0; // system provide it
	Inputs[1].mi.dx = x;
	Inputs[1].mi.dy = y;
	nb = SendInput( 2, Inputs, sizeof(INPUT));

	if ( nb != 2 )
	{
		_RPT3(_CRT_WARN,"ivyCursorClick can click to x=%d y=%d error=%d\n",x,y,GetLastError());
	}
}
void SendInput ( DWORD flags, double x, double y , int wheel )
{
	UINT nb;
	INPUT Inputs;

	if ( lock ) return;
	
	Inputs.type = INPUT_MOUSE;
	Inputs.mi.dwFlags = flags;
	Inputs.mi.mouseData = wheel;
	Inputs.mi.time = 0; // system provide it
	Inputs.mi.dx = (int)(65535 * x );
	Inputs.mi.dy = (int)(65535 * y );
	
	nb = SendInput( 1, &Inputs, sizeof(INPUT));

	if ( nb != 1 )
	{
		_RPT3(_CRT_WARN,"ivyPointerEvent can move to x=%d y=%d error=%d\n",x,y,GetLastError());
	}
}
void ivyPointerEvent(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	int x = atoi(*argv++);
	int y = atoi(*argv++);
	int presure = atoi(*argv++);
	int tilt_x = atoi(*argv++);
	int tilt_y = atoi(*argv++);
	int wheel = atoi(*argv++);
	int predicted_x = atoi(*argv++);
	int predicted_y = atoi(*argv++);
	const char *type = *argv++;
	int serial_number = atoi(*argv++);
	double time = atof(*argv++);
	double hires_x = atof(*argv++); 
	double hires_y = atof(*argv++);
	const char *proximity = *argv++; // (In|Out|unchanged)

	// hack for tablet PC
	if ( serial_number ==0 )
	{
		hires_x *= 86800 / 1400;
		hires_y *= 65000 / 1050;
	}

	SendInput ( MOUSEEVENTF_WHEEL | MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, hires_x, hires_y , wheel );
	
    
   
}
UINT TabletToMouseButton( int button , bool down )
{
	UINT mouse_bttton;
	if ( down )
	{
		switch ( button )
		{
		case 1:
			mouse_bttton = MOUSEEVENTF_LEFTDOWN;
			break;
		case 2:
			mouse_bttton = MOUSEEVENTF_MIDDLEDOWN;
			break;
		case 3:
			mouse_bttton = MOUSEEVENTF_RIGHTDOWN;
			break;
		default:
			mouse_bttton = MOUSEEVENTF_LEFTDOWN;
			break;
		}
	}
	else
	{
		switch ( button )
		{
		case 1:
			mouse_bttton = MOUSEEVENTF_LEFTUP;
			break;
		case 2:
			mouse_bttton = MOUSEEVENTF_MIDDLEUP;
			break;
		case 3:
			mouse_bttton = MOUSEEVENTF_RIGHTUP;
			break;
		default:
			mouse_bttton = MOUSEEVENTF_LEFTUP;
			break;
		}
	}
	return button;
}
void ivyButtonEvent(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	static int bt_down = 0;
	int button = atoi(*argv++);
	const char *status = *argv++ ; //(up|down) 
	int x = atoi(*argv++);
	int y = atoi(*argv++);
	int presure = atoi(*argv++);
	int tilt_x = atoi(*argv++);
	int tilt_y = atoi(*argv++);
	int wheel = atoi(*argv++);
	int predicted_x = atoi(*argv++);
	int predicted_y = atoi(*argv++);
	const char *type = *argv++;
	int serial_number = atoi(*argv++);
	double time = atof(*argv++);
	double hires_x = atof(*argv++); 
	double hires_y = atof(*argv++);
	const char *proximity = *argv++; // (In|Out|unchanged)
	
	
	// hack for tablet PC
	if ( serial_number ==0 )
	{
		hires_x *= 86800 / 1400;
		hires_y *= 65000 / 1050;
	}
	if ( hires_x < 0 || hires_y < 0 || hires_x > 1.0 || hires_y > 1.0 )
	{
		if ( *status == 'u' && bt_down )
		{
			SendInput ( TabletToMouseButton( button,false ), hires_x, hires_y , wheel );
			bt_down = 0;
		}
		
	}
	else
	{
	if ( (*status) == 'd' )
	{
	SendInput ( MOUSEEVENTF_WHEEL | MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | TabletToMouseButton( button,true ) , hires_x, hires_y , wheel );
	bt_down = 1;
	}
	else
	{
	SendInput ( MOUSEEVENTF_WHEEL | MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | TabletToMouseButton( button,false ) , hires_x, hires_y , wheel );
	bt_down = 0;
	}
	}
	
}
void ivyPadEvent(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	int button = atoi(*argv++);
	const char *status = *argv++ ; //(up|down) 
	int time = atoi(*argv++);
   
}
void ivySliderEvent(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	 int value = atoi(*argv++);
	 const char* side = *argv++ ; // (left|right)
	 int time = atoi(*argv++);
}  

void ivyLockEvent(IvyApplication *app, void *user_data, int argc, const char **argv)
{
	 const char* status = *argv++ ; // (lock|unlock)
	 lock =  ( *status == 'l' );
}  

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	MSG msg;
	int  optchar;
       const char * optarg;
       const char * ivybus = NULL;
	   const char * acc = "bordeaux";
	   const char * wp = "WP1";
	   const char * role = "TC";
       
       int  errors = 0, ngroups = 0;
	// read command line
	
    static const char * optv[] = {
		"H|help",
		"a:acc  <string>",
		"w:wp	<string>",
		"r:role <string>",
		"b:bus  <string>",
       NULL
    } ;
	Options  opts("ivycursor", optv);
	OptStrTokIter  iter(lpCmdLine);

	while( optchar = opts(iter, optarg) ) {
	   switch (optchar) {
	   case 'H' :
		   opts.usage(std::cout, "ivycursor ...");
		  exit(0);
		  break;
	   case 'a' :
		  acc = optarg; break;
	   case 'w' :
		  wp = optarg; break;
	   case 'r' :
		  role = optarg; break;
	   case 'b' :
		  ivybus = optarg; break;
	   default :  ++errors; break;
	   } //switch
	}

	if (errors) {
	   opts.usage(std::cerr, "ivycursor ...");
	   exit(1);
	}

	// Initialise les chaînes globales
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_IVYCURSOR, szWindowClass, MAX_LOADSTRING);
	hMenu =  LoadMenu(hInstance,  MAKEINTRESOURCE( IDR_MENU1  ) );
	
	// TrackPopupMenu cannot display the menu bar so get 
    // a handle to the first shortcut menu. 
 
    hmenuTrackPopup = GetSubMenu(hMenu, 0); 

	if ( hmenuTrackPopup == NULL )
	{
		return FALSE;
	}
	MyRegisterClass(hInstance);

	// Effectue l'initialisation de l'application :
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}
	// Initoiailize ivy
	bus = new Ivy("IvyCursor", "IvyCursor Ready", 0, FALSE );
	bus->BindMsg(BUS_CALLBACK(ivyCursorMouve,0),"^Cigale SetCursorPosition x=([0-9.]+) y=([0-9.]+)");
	bus->BindMsg(BUS_CALLBACK(ivyCursorClick,0),"^Cigale CursorSelectEvent x=([0-9.]+) y=([0-9.]+)");

	bus->BindMsg(BUS_CALLBACK(ivyPointerEvent,0), "^pointer_event device_id=%s:%s:%s x=([-0-9]+) y=([-0-9]+) presure=([-0-9]+) tilt_x=([-0-9]+) tilt_y=([-0-9]+) wheel=([-0-9]+) predicted_x=([-0-9]+) predicted_y=([-0-9]+) type=(.*) serial_number=([-0-9]+) time=([-.0-9]+) hires_x=([-.0-9]+) hires_y=([-.0-9]+) proximity=(In|Out|unchanged)", acc, wp, role );
    bus->BindMsg(BUS_CALLBACK(ivyButtonEvent,0), "^button_event device_id=%s:%s:%s button=([-.0-9]+) status=(up|down) x=([-0-9]+) y=([-0-9]+) presure=([-0-9]+) tilt_x=([-0-9]+) tilt_y=([-0-9]+) wheel=([-0-9]+) predicted_x=([-0-9]+) predicted_y=([-0-9]+) type=(.*) serial_number=([-0-9]+) time=([-0-9]+) hires_x=([-.0-9]+) hires_y=([-.0-9]+) proximity=(In|Out|unchanged)", acc, wp, role );
    bus->BindMsg(BUS_CALLBACK(ivyPadEvent,0), "^pad_event device_id=%s:%s:%s button=([0-9]+) status=(up|down) time=([-0-9]+)", acc, wp, role);
    bus->BindMsg(BUS_CALLBACK(ivySliderEvent,0), "^slider_event device_id=%s:%s:%s value=([-0-9]+) side=(left|right) time=([-0-9]+)", acc, wp, role);
    bus->BindMsg(BUS_CALLBACK(ivyLockEvent,0), "^lock_event device_id=%s:%s:%s status=(lock|unlock)", acc, wp, role);
    
	bus->start( ivybus );

	// Boucle de messages principale :
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}



//
//  FONCTION : MyRegisterClass()
//
//  BUT : inscrit la classe de fenêtre.
//
//  COMMENTAIRES :
//
//    Cette fonction et son utilisation sont nécessaires uniquement si vous souhaitez que ce code
//    soit compatible avec les systèmes Win32 avant la fonction 'RegisterClassEx'
//    qui a été ajoutée à Windows 95. Il est important d'appeler cette fonction
//    afin que l'application dispose des petites icônes correctes qui lui sont
//    associées.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_IVYCURSOR);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FONCTION : InitInstance(HANDLE, int)
//
//   BUT : enregistre le handle de l'instance et crée une fenêtre principale
//
//   COMMENTAIRES :
//
//        Dans cette fonction, nous enregistrons le handle de l'instance dans une variable globale, puis
//        créons et affichons la fenêtre principale du programme.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   NOTIFYICONDATA icodata;


   hInst = hInstance; // Stocke le handle d'instance dans la variable globale

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

	icodata.cbSize = sizeof(NOTIFYICONDATA);
	icodata.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ;
	icodata.hWnd = hWnd;
	icodata.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_IVYCURSOR);
	icodata.uID = 0;
	icodata.uVersion = NOTIFYICON_VERSION;
	icodata.uCallbackMessage = WM_ICONTRAY_NOTIFY;
	strcpy ( icodata.szTip, "IvyCursor Event redirector");
	
	if (! Shell_NotifyIcon(  NIM_ADD, &icodata) )
	{
		return FALSE;
	}
	if (! Shell_NotifyIcon(  NIM_SETVERSION, &icodata) )
	{
		return FALSE;
	}
	icodata.uFlags =  NIF_INFO;
	// Info
	icodata.dwInfoFlags = NIIF_INFO;
	strcpy ( icodata.szInfoTitle, "IvyCursor Event redirector");
	strcpy (icodata.szInfo , "IvyCursor Event redirector" );
	icodata.uTimeout =  10000; // 10 seconds 
	if (! Shell_NotifyIcon(  NIM_MODIFY, &icodata) )
	{
		return FALSE;
	}
	
   return TRUE;
}
void RemoveTrayIcon(HWND hWnd)
{
	NOTIFYICONDATA icodata;
	icodata.cbSize = sizeof( icodata );
	icodata.uFlags = 0;
	icodata.hWnd = hWnd;
	icodata.uID = 0;
	Shell_NotifyIcon(  NIM_DELETE, &icodata);
}
void ReturnFocus(HWND hWnd)
{
	NOTIFYICONDATA icodata;
	icodata.cbSize = sizeof( icodata );
	icodata.uFlags = 0;
	icodata.hWnd = hWnd;
	icodata.uID = 0;
	Shell_NotifyIcon(  NIM_SETFOCUS, &icodata);
}
void Quitting( HWND hWnd )
{
	RemoveTrayIcon(hWnd);
	DestroyMenu(hMenu);
	DestroyWindow(hWnd);
	bus->stop();
}
//
//  FONCTION : WndProc(HWND, unsigned, WORD, LONG)
//
//  BUT :  traite les messages pour la fenêtre principale.
//
//  WM_COMMAND	- traite le menu de l'application
//  WM_PAINT	- dessine la fenêtre principale
//  WM_DESTROY	- génère un message d'arrêt et retourne
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	int xPos,yPos; 
	POINT point;

	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Analyse les sélections de menu :
		switch (wmId)
		{
		case IDM_ABOUT:
			//DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
			break;
		case IDM_EXIT:
		case ID_QUIT:
			Quitting(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_CONTEXTMENU:
			// Display the shortcut menu. Track the right mouse 
			// button. 
			xPos = LOWORD(lParam); 
			yPos = HIWORD(lParam); 

			TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
			xPos, yPos, 0, hWnd, NULL); 
			PostMessage(hWnd, WM_NULL, 0, 0);

		break;
	case WM_ICONTRAY_NOTIFY:
		switch ( lParam )
		{
		case WM_LBUTTONDOWN:
			break;
		case WM_RBUTTONDOWN:
			GetCursorPos(&point);
			TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
			point.x, point.y, 0, hWnd, NULL); 
			PostMessage(hWnd, WM_NULL, 0, 0);
			break;
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Gestionnaire de messages pour la boîte de dialogue À propos de.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	return FALSE;
}