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

#include "stdafx.h"
#include <winuser.h>
#include <crtdbg.h>
#include <shellapi.h>
#include "Ivy.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* ivy;

// 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);


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());
	}
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	MSG msg;
	// 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
	ivy = new Ivy("IvyCursor", "IvyCursor Ready", 0, FALSE );
	ivy->BindMsg(BUS_CALLBACK(ivyCursorMouve,0),"Cigale SetCursorPosition x=([0-9.]+) y=([0-9.]+)");
	ivy->BindMsg(BUS_CALLBACK(ivyCursorClick,0),"Cigale CursorSelectEvent x=([0-9.]+) y=([0-9.]+)");
	ivy->start( 0 );

	// 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);
	ivy->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;
}