summaryrefslogtreecommitdiff
path: root/plugins/KeyboardNotify/src/flash.cpp
blob: 20c7888ba7b4e1ba3ef574649d05732f16133b59 (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
/*

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "stdafx.h"

struct FLASHING_SEQUENCE
{
	unsigned int size;
	unsigned int index;
	BYTE frame[MAX_PATH];
};


// Prototypes
FLASHING_SEQUENCE *getCustomSeq(void);
FLASHING_SEQUENCE *getPredefinedSeq(void);
FLASHING_SEQUENCE *getTrillianSeq(void);
void updateTrillianSeq(void);
static void __cdecl TestThread(void *param);
static void PreviewThread(void *);
FLASHING_SEQUENCE str2FS(wchar_t *);
BYTE KbdChar2Byte(char);
void countUnopenEvents(int *, int *, int *, int *);

#define Leds2Flash	((BYTE)(bFlashLed[2] + (bFlashLed[0]<<1) + (bFlashLed[1]<<2)))

// Flashing settings
FLASHING_SEQUENCE *pFS;
BOOL bTemporarilyUseExtern;
extern BYTE bFlashLed[3];
extern BYTE bFlashEffect; extern BYTE bSequenceOrder;
extern WORD wCustomTheme;
extern BYTE bTrillianLedsMsg; extern BYTE bTrillianLedsURL; extern BYTE bTrillianLedsFile; extern BYTE bTrillianLedsOther;
extern BYTE bEmulateKeypresses;

// TestThread/PreviewThread globals
extern int nWaitDelay; extern WORD wStartDelay;
BOOL bTestSemaphore, bPreviewSemaphore, bPreview;

void RestoreLEDState(void)
{
	if (bEmulateKeypresses)
		keypresses_RestoreLEDState();
	else
		ToggleKeyboardLights((BYTE)(LedState(VK_SCROLL) + (LedState(VK_NUMLOCK)<<1) + (LedState(VK_CAPITAL)<<2)));
}

BYTE getBlinkingLeds(void)
{
	if (!pFS->size)
		return (BYTE)(LedState(VK_SCROLL) + (LedState(VK_NUMLOCK)<<1) + (LedState(VK_CAPITAL)<<2));

	pFS->index %= pFS->size;
	if (bFlashEffect == FLASH_TRILLIAN && !bTemporarilyUseExtern && !pFS->index)
		updateTrillianSeq();
	return pFS->frame[pFS->index++];
}

void setFlashingSequence(void)
{
	switch (bFlashEffect) {
	case FLASH_CUSTOM:
		pFS = getCustomSeq();
		break;
	case FLASH_TRILLIAN:
		pFS = getTrillianSeq();
		break;
	default:
		pFS = getPredefinedSeq();
	}
	bTemporarilyUseExtern = FALSE;
}

FLASHING_SEQUENCE *getCustomSeq(void)
{
	ptrW tszSeq( db_get_wsa(NULL, MODULENAME, fmtDBSettingName("custom%d", wCustomTheme)));

	static FLASHING_SEQUENCE Custom = {0};
	Custom = str2FS((tszSeq != 0) ? tszSeq : L"");
	return &Custom;
}


FLASHING_SEQUENCE *getPredefinedSeq(void)
{
	static FLASHING_SEQUENCE Predefined = {0};

	FLASHING_SEQUENCE *pAux;
	FLASHING_SEQUENCE SameTime = {2, 0, {7, 0}};
	FLASHING_SEQUENCE InTurn = {2, 0, {3, 4}};
	FLASHING_SEQUENCE InSeq = {3, 0, {2, 4, 1}};
	FLASHING_SEQUENCE InSeqRev = {3, 0, {1, 4, 2}};
	FLASHING_SEQUENCE InSeqKIT = {4, 0, {2, 4, 1, 4}};


	if (Leds2Flash < 3 || Leds2Flash == 4)
		pAux = &SameTime;
	else
		switch (bFlashEffect) {
		default:
		case FLASH_SAMETIME:
			pAux = &SameTime;
			break;
		case FLASH_INTURN:
			if (Leds2Flash == 3)	// 3 = Num+Scroll
				pAux = &InSeq;
			else
				pAux = &InTurn;
			break;
		case FLASH_INSEQUENCE:
			switch (bSequenceOrder) {
			default:
			case SEQ_LEFT2RIGHT:
				pAux = &InSeq;
				break;
			case SEQ_RIGHT2LEFT:
				pAux = &InSeqRev;
				break;
			case SEQ_LIKEKIT:
				if (Leds2Flash != 7)	// 7 = Num+Caps+Scroll
					pAux = &InSeq;
				else
					pAux = &InSeqKIT;
				break;
			}
			break;
		}

	Predefined.size = Predefined.index = 0;
	for (pAux->index=0; pAux->index < pAux->size; pAux->index++)
		if (!pAux->frame[pAux->index] || (pAux->frame[pAux->index] & Leds2Flash)) {
			Predefined.size++;
			Predefined.frame[Predefined.size - 1] = pAux->frame[pAux->index] & Leds2Flash;
		}

	return &Predefined;
}


FLASHING_SEQUENCE *getTrillianSeq(void)
{
	static FLASHING_SEQUENCE Trillian = {2, 0, {0, 0}};

	Trillian.size = 2;
	Trillian.index = 0;

	return &Trillian;
}


void updateTrillianSeq(void)
{
	int i, msgCount=0, fileCount=0, urlCount=0, otherCount=0;

	pFS->size = 2;
	countUnopenEvents(&msgCount, &fileCount, &urlCount, &otherCount);

	if ((bTrillianLedsMsg & Leds2Flash) && (pFS->size + 2 * msgCount) <= MAX_PATH)
		for (i=0; i < msgCount; i++) {
			pFS->frame[pFS->size++] = bTrillianLedsMsg & Leds2Flash;
			pFS->frame[pFS->size++] = 0;
		}

	if ((bTrillianLedsFile & Leds2Flash) && (pFS->size + 2 * fileCount) <= MAX_PATH)
		for (i=0; i < fileCount; i++) {
			pFS->frame[pFS->size++] = bTrillianLedsFile & Leds2Flash;
			pFS->frame[pFS->size++] = 0;
		}

	if ((bTrillianLedsURL & Leds2Flash) && (pFS->size + 2 * urlCount) <= MAX_PATH)
		for (i=0; i < urlCount; i++) {
			pFS->frame[pFS->size++] = bTrillianLedsURL & Leds2Flash;
			pFS->frame[pFS->size++] = 0;
		}

	if ((bTrillianLedsOther & Leds2Flash) && (pFS->size + 2 * otherCount) <= MAX_PATH)
		for (i=0; i < otherCount; i++) {
			pFS->frame[pFS->size++] = bTrillianLedsOther & Leds2Flash;
			pFS->frame[pFS->size++] = 0;
		}
}

void useExternSequence(wchar_t *extStr)
{
	static FLASHING_SEQUENCE Extern = {0};

	wchar_t externStr[MAX_PATH+1];
	mir_wstrcpy(externStr, extStr);

	Extern = str2FS(normalizeCustomString(externStr));

	pFS = &Extern;
	bTemporarilyUseExtern = TRUE;
}


wchar_t *normalizeCustomString(wchar_t *customStr)
{
	int len=0, status=0;
	BOOL used[4];
	wchar_t strAux[MAX_PATH+1], *str;

	for (mir_wstrcpy(str=strAux, customStr); *str; str++) {
		switch (*str) {
		case '[':
			if (status == 0) {
				status = 1;
				customStr[len++] = *str;
				used[0] = used [1] = used[2] = used[3] = FALSE;
			}
			break;
		case ']':
			if (status == 1) {
				status = 0;
				customStr[len++] = *str;
			}
			break;
		case '0':
		case '1':
		case '2':
		case '3':
			if (status == 0)
				customStr[len++] = *str;
			else if (!used[*str - '0']) {
				customStr[len++] = *str;
				used[*str - '0'] = TRUE;
			}
			break;
		}
	}
	if (status == 1)
		customStr[len++] = ']';
	customStr[len] = '\0';

	return customStr;
}

wchar_t* getCurrentSequenceString(void)
{
	static wchar_t CurrentSeqString[MAX_PATH+1];
	wchar_t *str = CurrentSeqString;

	for (unsigned i=0; i < pFS->size; i++) {
		switch (pFS->frame[i]) {
		case 0:
			*(str++) = '0';
			break;
		case 1:
			*(str++) = '3';
			break;
		case 2:
			*(str++) = '1';
			break;
		case 3:
			*(str++) = '[';
			*(str++) = '1';
			*(str++) = '3';
			*(str++) = ']';
			break;
		case 4:
			*(str++) = '2';
			break;
		case 5:
			*(str++) = '[';
			*(str++) = '2';
			*(str++) = '3';
			*(str++) = ']';
			break;
		case 6:
			*(str++) = '[';
			*(str++) = '1';
			*(str++) = '2';
			*(str++) = ']';
			break;
		case 7:
			*(str++) = '[';
			*(str++) = '1';
			*(str++) = '2';
			*(str++) = '3';
			*(str++) = ']';
		}
	}
	*str = '\0';

	return CurrentSeqString;
}

void testSequence(wchar_t *testStr)
{
	if (bTestSemaphore) // we try to avoid concurrent test threads
		return;

	bTestSemaphore = TRUE;

	static FLASHING_SEQUENCE Test = {0};
	Test = str2FS(testStr);
	
	mir_forkthread(TestThread, &Test);
}


static void __cdecl TestThread(void *param)
{
	FLASHING_SEQUENCE *pTest = (FLASHING_SEQUENCE *)param;
	unsigned int testNum = (unsigned int)db_get_b(NULL, MODULENAME, "testnum", DEF_SETTING_TESTNUM);
	unsigned int testSecs = (unsigned int)db_get_b(NULL, MODULENAME, "testsecs", DEF_SETTING_TESTSECS);

	for (unsigned i=0, dwEndTest=GetTickCount()+testSecs*1000; i < testNum || GetTickCount() < dwEndTest; i++)
		for (pTest->index=0; pTest->index < pTest->size; pTest->index++) {
			ToggleKeyboardLights(pTest->frame[pTest->index]);
			Sleep(nWaitDelay);
		}

	RestoreLEDState();

	bTestSemaphore = FALSE;	
}


void previewFlashing(BOOL buttonState)
{
	bPreview = buttonState;
	if (!bPreview || bPreviewSemaphore) // turn off flashing or already running
		return;

	bPreviewSemaphore = TRUE;
	mir_forkthread(PreviewThread);
}


static void PreviewThread(void*)
{
	if (wStartDelay > 0)
		Sleep(wStartDelay * 1000);

	BYTE unchangedLeds = (BYTE)(LedState(VK_SCROLL) * !bFlashLed[2] + ((LedState(VK_NUMLOCK) * !bFlashLed[0])<<1) + ((LedState(VK_CAPITAL) * !bFlashLed[1])<<2));

	while (bPreview)
		for (unsigned i=0; bPreview && i < pFS->size; i++) {
			ToggleKeyboardLights((BYTE)(pFS->frame[i%pFS->size]|unchangedLeds));
			Sleep(nWaitDelay);				
		}

	RestoreLEDState();

	bPreviewSemaphore = FALSE;	
}


FLASHING_SEQUENCE str2FS(wchar_t *str)
{
	FLASHING_SEQUENCE Temp = {0};

	for (Temp.size=Temp.index=0; *str; str++) {
		Temp.size++;
		if (*str == '[') {
			Temp.frame[Temp.size - 1] = 0;
			for (str++; *str && *str != ']'; str++)
				Temp.frame[Temp.size - 1] += KbdChar2Byte(*str) & Leds2Flash;
			if (!*str) break;
		}
		else Temp.frame[Temp.size - 1] = KbdChar2Byte(*str) & Leds2Flash;
	}

	return Temp;
}


BYTE KbdChar2Byte(char kbdChar)
{
	switch (kbdChar) {
	case '1': //NumLock
		return 2;
	case '2': //CapsLock
		return 4;
	case '3': //ScrollLock
		return 1;
	}
	return 0;
}