summaryrefslogtreecommitdiff
path: root/plugins/FingerprintNG/src/fingerprint.cpp
blob: 57613220d0fb66f27e975f908dac9adc99d2e14a (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
/*
Fingerprint NG (client version) icons module for Miranda NG
Copyright © 2006-24 ghazan, mataes, HierOS, FYR, Bio, nullbie, faith_healer and all respective contributors.

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.
*/

//Start of header
#include "stdafx.h"

KN_FP_MASK *g_pNotFound, *g_pUnknown;
LIST<KN_FP_MASK> g_arCommon(10), g_overlay1(10), g_overlay2(10), g_overlay3(10), g_overlay4(10);
wchar_t g_szSkinLib[MAX_PATH];

static UINT g_LPCodePage;
static HANDLE hExtraIcon = nullptr;
static HANDLE hFolderChanged = nullptr, hIconFolder = nullptr;

static int CompareFI(const FOUNDINFO *p1, const FOUNDINFO *p2)
{
	if (p1->iBase != p2->iBase)
		return p1->iBase - p2->iBase;
	return p1->iOverlay - p2->iOverlay;
}

static OBJLIST<FOUNDINFO> arFI(50, CompareFI);

static LIST<void> arMonitoredWindows(3, PtrKeySortT);

/*	GetIconsIndexes
*	Retrieves Icons indexes by Mirver
*/

static void GetIconsIndexes(LPWSTR wszMirVer, KN_FP_MASK *&base, KN_FP_MASK *&overlay1, KN_FP_MASK *&overlay2, KN_FP_MASK *&overlay3, KN_FP_MASK *&overlay4)
{
	base = overlay1 = overlay2 = overlay3 = overlay4 = nullptr;

	if (mir_wstrcmp(wszMirVer, L"?") == 0) {
		base = g_pUnknown;
		return;
	}

	LPWSTR wszMirVerUp = NEWWSTR_ALLOCA(wszMirVer);
	_wcsupr(wszMirVerUp);

	for (auto &it : g_arCommon) {
		if (it->hIcolibItem == nullptr)
			continue;

		if (!WildCompare(wszMirVerUp, it->szMaskUpper))
			continue;

		base = it;
		if (it->iIconIndex != IDI_NOTFOUND && it->iIconIndex != IDI_UNKNOWN && it->iIconIndex != IDI_UNDETECTED) {
			wchar_t destfile[MAX_PATH];
			wcsncpy_s(destfile, g_szSkinLib, _TRUNCATE);

			struct _stat64i32 stFileInfo;
			if (_wstat(destfile, &stFileInfo) == -1)
				base = g_pNotFound;
		}
		break;
	}

	if (base == nullptr)
		base = g_pUnknown;

	else if (!base->fNotUseOverlay) {
		for (auto &it : g_overlay1) {
			if (!WildCompare(wszMirVerUp, it->szMaskUpper))
				continue;

			struct _stat64i32 stFileInfo;
			if (_wstat(g_szSkinLib, &stFileInfo) != -1) {
				overlay1 = it;
				break;
			}
		}

		for (auto &it : g_overlay2) {
			if (WildCompare(wszMirVerUp, it->szMaskUpper)) {
				overlay2 = it;
				break;
			}
		}

		for (auto &it : g_overlay3) {
			if (WildCompare(wszMirVerUp, it->szMaskUpper)) {
				overlay3 = it;
				break;
			}
		}

		for (auto &it : g_overlay4) {
			if (it->hIcolibItem == nullptr)
				continue;

			if (WildCompare(wszMirVerUp, it->szMaskUpper)) {
				overlay4 = it;
				break;
			}
		}
	}
}

/*
* CreateIconFromIndexes
* returns hIcon of joined icon by given indexes
*/

HICON __fastcall CreateIconFromIndexes(KN_FP_MASK *base, KN_FP_MASK *overlay1, KN_FP_MASK *overlay2, KN_FP_MASK *overlay3, KN_FP_MASK *overlay4)
{
	HICON hIcon = nullptr;	// returned HICON
	HICON hTmp = nullptr;
	HICON icOverlay = nullptr;
	HICON icOverlay2 = nullptr;
	HICON icOverlay3 = nullptr;
	HICON icOverlay4 = nullptr;

	HICON icMain = IcoLib_GetIconByHandle(base->hIcolibItem);
	if (icMain) {
		icOverlay = (overlay1 == nullptr) ? nullptr : IcoLib_GetIconByHandle(overlay1->hIcolibItem);
		icOverlay2 = (overlay2 == nullptr) ? nullptr : IcoLib_GetIconByHandle(overlay2->hIcolibItem);
		icOverlay3 = (overlay3 == nullptr) ? nullptr : IcoLib_GetIconByHandle(overlay3->hIcolibItem);
		icOverlay4 = (overlay4 == nullptr) ? nullptr : IcoLib_GetIconByHandle(overlay4->hIcolibItem);

		hIcon = icMain;

		if (overlay1)
			hTmp = hIcon = CreateJoinedIcon(hIcon, icOverlay);

		if (overlay2) {
			hIcon = CreateJoinedIcon(hIcon, icOverlay2);
			if (hTmp) DestroyIcon(hTmp);
			hTmp = hIcon;
		}

		if (overlay3) {
			hIcon = CreateJoinedIcon(hIcon, icOverlay3);
			if (hTmp) DestroyIcon(hTmp);
			hTmp = hIcon;
		}

		if (overlay4) {
			hIcon = CreateJoinedIcon(hIcon, icOverlay4);
			if (hTmp) DestroyIcon(hTmp);
		}
	}

	if (hIcon == icMain)
		hIcon = CopyIcon(icMain);

	IcoLib_ReleaseIcon(icMain);
	IcoLib_ReleaseIcon(icOverlay);
	IcoLib_ReleaseIcon(icOverlay2);
	IcoLib_ReleaseIcon(icOverlay3);
	IcoLib_ReleaseIcon(icOverlay4);
	return hIcon;
}

/****************************************************************************************
*	ServiceGetClientIcon
*	MS_FP_GETCLIENTICONW service implementation.
*	wParam - LPWSTR MirVer value to get client for.
*	lParam - int noCopy - if wParam is equal to "1"	will return icon handler without copiing icon.
*	ICON IS ALWAYS COPIED!!!
*/

static INT_PTR ServiceGetClientIcon(WPARAM wParam, LPARAM)
{
	LPWSTR wszMirVer = (LPWSTR)wParam;			// MirVer value to get client for.
	if (wszMirVer == nullptr)
		return 0;

	KN_FP_MASK *base, *overlay1, *overlay2, *overlay3, *overlay4;
	GetIconsIndexes(wszMirVer, base, overlay1, overlay2, overlay3, overlay4);

	HICON hIcon = nullptr;			// returned HICON
	if (base != nullptr)
		hIcon = CreateIconFromIndexes(base, overlay1, overlay2, overlay3, overlay4);

	return (INT_PTR)hIcon;
}

/****************************************************************************************
*	 ApplyFingerprintImage
*	 1)Try to find appropriate mask
*	 2)Register icon in extraimage list if not yet registered (EMPTY_EXTRA_ICON)
*	 3)Set ExtraImage for contact
*/

static void SetSrmmIcon(MCONTACT hContact, LPTSTR ptszMirver)
{
	if (mir_wstrlen(ptszMirver))
		Srmm_ModifyIcon(hContact, MODULENAME, 1, (HICON)ServiceGetClientIcon((WPARAM)ptszMirver, TRUE), ptszMirver);
	else
		Srmm_SetIconFlags(hContact, MODULENAME, 1, MBF_HIDDEN);
}

int __fastcall ApplyFingerprintImage(MCONTACT hContact, LPTSTR szMirVer)
{
	if (hContact == NULL)
		return 0;

	HANDLE hImage = INVALID_HANDLE_VALUE;
	if (szMirVer)
		hImage = GetIconIndexFromFI(szMirVer);

	ExtraIcon_SetIcon(hExtraIcon, hContact, hImage);

	if (arMonitoredWindows.getIndex((HANDLE)hContact) != -1)
		SetSrmmIcon(hContact, szMirVer);

	return 0;
}

int OnExtraIconClick(WPARAM wParam, LPARAM, LPARAM)
{
	CallService(MS_USERINFO_SHOWDIALOG, wParam, NULL);
	return 0;
}

/*
*	WildCompare
*	Compare 'name' string with 'mask' strings.
*	Masks can contain '*' or '?' wild symbols
*	Asterics '*' symbol covers 'empty' symbol too e.g WildCompare("Tst","T*st*"), returns TRUE
*	In order to handle situation 'at least one any sybol' use "?*" combination:
*	e.g WildCompare("Tst","T?*st*"), returns FALSE, but both WildCompare("Test","T?*st*") and
*	WildCompare("Teeest","T?*st*") return TRUE.
*
*	Function is case sensitive! so convert input or modify func to use _qtoupper()
*
*	Mask can contain several submasks. In this case each submask (including first)
*	should start from '|' e.g: "|first*submask|second*mask".
*
*	Dec 25, 2006 by FYR:
*	Added Exception to masks: the mask "|^mask3|mask2|mask1" means:
*	if NOT according to mask 3 AND (mask1 OR mask2)
*	EXCEPTION should be BEFORE main mask:
*		IF Exception match - the comparing stops as FALSE
*		IF Exception does not match - comparing continue
*		IF Mask match - comparing stops as TRUE
*		IF Mask does not not match comparing continue
*/
BOOL __fastcall WildCompare(LPWSTR wszName, LPWSTR wszMask)
{
	if (wszMask == nullptr)
		return NULL;

	if (*wszMask != L'|')
		return wildcmpw(wszName, wszMask);

	size_t s = 1, e = 1;
	LPWSTR wszTemp = (LPWSTR)_alloca(mir_wstrlen(wszMask) * sizeof(wchar_t) + sizeof(wchar_t));
	BOOL bExcept;

	while (wszMask[e] != L'\0')
	{
		s = e;
		while (wszMask[e] != L'\0' && wszMask[e] != L'|') e++;

		// exception mask
		bExcept = (*(wszMask + s) == L'^');
		if (bExcept) s++;

		memcpy(wszTemp, wszMask + s, (e - s) * sizeof(wchar_t));
		wszTemp[e - s] = L'\0';

		if (wildcmpw(wszName, wszTemp))
			return !bExcept;

		if (wszMask[e] != L'\0')
			e++;
		else
			return FALSE;
	}
	return FALSE;
}

/******************************************************************************
 *	Futher routines is for creating joined 'overlay' icons.
 ******************************************************************************/

 /*
 *	CreateBitmap32 - Create DIB 32 bitmap with sizes cx*cy
 */

HBITMAP __inline CreateBitmap32(int cx, int cy)
{
	return CreateBitmap32Point(cx, cy, nullptr);
}

/*
*	CreateBitmap32 - Create DIB 32 bitmap with sizes cx*cy and put reference
*				to new bitmap pixel image memory area to void ** bits
*/
HBITMAP __fastcall CreateBitmap32Point(int cx, int cy, LPVOID* bits)
{
	LPVOID ptPixels = nullptr;

	if (cx < 0 || cy < 0) return nullptr;

	BITMAPINFO bmpi = {};
	bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpi.bmiHeader.biWidth = cx;
	bmpi.bmiHeader.biHeight = cy;
	bmpi.bmiHeader.biPlanes = 1;
	bmpi.bmiHeader.biBitCount = 32;
	HBITMAP DirectBitmap = CreateDIBSection(nullptr, &bmpi, DIB_RGB_COLORS, &ptPixels, nullptr, 0);

	GdiFlush();
	if (ptPixels) memset(ptPixels, 0, cx * cy * 4);
	if (bits != nullptr) *bits = ptPixels;

	return DirectBitmap;
}

/*
*	 checkHasAlfa - checks if image has at least one uint8_t in alpha channel
*				 that is not a 0. (is image real 32 bit or just 24 bit)
*/
BOOL __fastcall checkHasAlfa(LPBYTE from, int width, int height)
{
	LPDWORD pt = (LPDWORD)from;
	LPDWORD lim = pt + width * height;
	while (pt < lim)
	{
		if (*pt & 0xFF000000)
			return TRUE;
		pt++;
	}

	return FALSE;
}

/*
*	 checkMaskUsed - checks if mask image has at least one that is not a 0.
*	 Not sure is it required or not
*/
BOOL __fastcall checkMaskUsed(LPBYTE from)
{
	int i;
	for (i = 0; i < 16 * 16 / 8; i++)
	{
		if (from[i] != 0) return TRUE;
	}
	return FALSE;
}

/*
*	 GetMaskBit - return value of apropriate mask bit in line at x position
*/
BOOL __inline GetMaskBit(LPBYTE line, int x)
{
	return ((*(line + (x >> 3))) & (0x01 << (7 - (x & 0x07)))) != 0;
}

/*
*	blend	- alpha blend ARGB values of 2 pixels. X1 - underlaying,
*	 X2 - overlaying points.
*/
uint32_t __fastcall blend(uint32_t X1, uint32_t X2)
{
	RGBA* q1 = (RGBA*)&X1;
	RGBA* q2 = (RGBA*)&X2;
	uint8_t a_1 = ~q1->a;
	uint8_t a_2 = ~q2->a;
	uint16_t am = q1->a * a_2;

	uint16_t ar = q1->a + ((a_1 * q2->a) / 255);
	// if a2 more than 0 than result should be more
	// or equal (if a1==0) to a2, else in combination
	// with mask we can get here black points

	ar = (q2->a > ar) ? q2->a : ar;

	if (ar == 0) return 0;

	{
		uint16_t arm = ar * 255;
		uint16_t rr = ((q1->r * am + q2->r * q2->a * 255)) / arm;
		uint16_t gr = ((q1->g * am + q2->g * q2->a * 255)) / arm;
		uint16_t br = ((q1->b * am + q2->b * q2->a * 255)) / arm;
		return (ar << 24) | (rr << 16) | (gr << 8) | br;
	}
}

/*
*	CreateJoinedIcon	- creates new icon by drawing hTop over hBottom.
*/
HICON __fastcall CreateJoinedIcon(HICON hBottom, HICON hTop)
{
	BOOL drawn = FALSE;
	HDC tempDC, tempDC2, tempDC3;
	HICON res = nullptr;
	HBITMAP oImage, nImage;
	HBITMAP nMask, hbm, obmp, obmp2;
	LPBYTE ptPixels = nullptr;
	ICONINFO iNew = { 0 };
	uint8_t p[32] = { 0 };

	tempDC = CreateCompatibleDC(nullptr);
	nImage = CreateBitmap32Point(16, 16, (LPVOID*)&ptPixels);
	oImage = (HBITMAP)SelectObject(tempDC, nImage);

	ICONINFO iciBottom = { 0 };
	ICONINFO iciTop = { 0 };

	BITMAP bmp_top = { 0 };
	BITMAP bmp_top_mask = { 0 };

	BITMAP bmp_bottom = { 0 };
	BITMAP bmp_bottom_mask = { 0 };

	GetIconInfo(hBottom, &iciBottom);
	GetObject(iciBottom.hbmColor, sizeof(BITMAP), &bmp_bottom);
	GetObject(iciBottom.hbmMask, sizeof(BITMAP), &bmp_bottom_mask);

	GetIconInfo(hTop, &iciTop);
	GetObject(iciTop.hbmColor, sizeof(BITMAP), &bmp_top);
	GetObject(iciTop.hbmMask, sizeof(BITMAP), &bmp_top_mask);

	if (bmp_bottom.bmBitsPixel == 32 && bmp_top.bmBitsPixel == 32)
	{
		LPBYTE BottomBuffer, TopBuffer, BottomMaskBuffer, TopMaskBuffer;
		LPBYTE bb, tb, bmb, tmb;
		LPBYTE db = ptPixels;
		int vstep_d = 16 * 4;
		int vstep_b = bmp_bottom.bmWidthBytes;
		int vstep_t = bmp_top.bmWidthBytes;
		int vstep_bm = bmp_bottom_mask.bmWidthBytes;
		int vstep_tm = bmp_top_mask.bmWidthBytes;

		if (bmp_bottom.bmBits)
			bb = BottomBuffer = (LPBYTE)bmp_bottom.bmBits;
		else
		{
			BottomBuffer = (LPBYTE)_alloca(bmp_bottom.bmHeight * bmp_bottom.bmWidthBytes);
			GetBitmapBits(iciBottom.hbmColor, bmp_bottom.bmHeight * bmp_bottom.bmWidthBytes, BottomBuffer);
			bb = BottomBuffer + vstep_b * (bmp_bottom.bmHeight - 1);
			vstep_b = -vstep_b;
		}
		if (bmp_top.bmBits)
			tb = TopBuffer = (LPBYTE)bmp_top.bmBits;
		else
		{
			TopBuffer = (LPBYTE)_alloca(bmp_top.bmHeight * bmp_top.bmWidthBytes);
			GetBitmapBits(iciTop.hbmColor, bmp_top.bmHeight * bmp_top.bmWidthBytes, TopBuffer);
			tb = TopBuffer + vstep_t * (bmp_top.bmHeight - 1);
			vstep_t = -vstep_t;
		}
		if (bmp_bottom_mask.bmBits)
			bmb = BottomMaskBuffer = (LPBYTE)bmp_bottom_mask.bmBits;
		else
		{
			BottomMaskBuffer = (LPBYTE)_alloca(bmp_bottom_mask.bmHeight * bmp_bottom_mask.bmWidthBytes);
			GetBitmapBits(iciBottom.hbmMask, bmp_bottom_mask.bmHeight * bmp_bottom_mask.bmWidthBytes, BottomMaskBuffer);
			bmb = BottomMaskBuffer + vstep_bm * (bmp_bottom_mask.bmHeight - 1);
			vstep_bm = -vstep_bm;
		}
		if (bmp_top_mask.bmBits)
			tmb = TopMaskBuffer = (LPBYTE)bmp_top_mask.bmBits;
		else
		{
			TopMaskBuffer = (LPBYTE)_alloca(bmp_top_mask.bmHeight * bmp_top_mask.bmWidthBytes);
			GetBitmapBits(iciTop.hbmMask, bmp_top_mask.bmHeight * bmp_top_mask.bmWidthBytes, TopMaskBuffer);
			tmb = TopMaskBuffer + vstep_tm * (bmp_top_mask.bmHeight - 1);
			vstep_tm = -vstep_tm;
		}
		{
			int x; int y;
			BOOL topHasAlpha = checkHasAlfa(TopBuffer, bmp_top.bmWidth, bmp_top.bmHeight);
			BOOL bottomHasAlpha = checkHasAlfa(BottomBuffer, bmp_bottom.bmWidth, bmp_bottom.bmHeight);
			BOOL topMaskUsed = !topHasAlpha && checkMaskUsed(TopMaskBuffer);
			BOOL bottomMaskUsed = !bottomHasAlpha && checkMaskUsed(BottomMaskBuffer);

			for (y = 0; y < 16; y++)
			{
				for (x = 0; x < 16; x++)
				{
					uint32_t bottom_d = ((LPDWORD)bb)[x];
					uint32_t top_d = ((LPDWORD)tb)[x];

					if (topMaskUsed)
					{
						if (GetMaskBit(tmb, x))
							top_d &= 0x00FFFFFF;
						else
							top_d |= 0xFF000000;
					}
					else if (!topHasAlpha)
						top_d |= 0xFF000000;

					if (bottomMaskUsed)
					{
						if (GetMaskBit(bmb, x))
							bottom_d &= 0x00FFFFFF;
						else
							bottom_d |= 0xFF000000;
					}
					else if (!bottomHasAlpha)
						bottom_d |= 0xFF000000;

					((LPDWORD)db)[x] = blend(bottom_d, top_d);
				}
				bb += vstep_b;
				tb += vstep_t;
				bmb += vstep_bm;
				tmb += vstep_tm;
				db += vstep_d;
			}
		}

		drawn = TRUE;
	}

	DeleteObject(iciBottom.hbmColor);
	DeleteObject(iciTop.hbmColor);
	DeleteObject(iciBottom.hbmMask);
	DeleteObject(iciTop.hbmMask);

	if (!drawn) {
		DrawIconEx(tempDC, 0, 0, hBottom, 16, 16, 0, nullptr, DI_NORMAL);
		DrawIconEx(tempDC, 0, 0, hTop, 16, 16, 0, nullptr, DI_NORMAL);
	}

	nMask = CreateBitmap(16, 16, 1, 1, p);
	tempDC2 = CreateCompatibleDC(nullptr);
	tempDC3 = CreateCompatibleDC(nullptr);
	hbm = CreateCompatibleBitmap(tempDC3, 16, 16);
	obmp = (HBITMAP)SelectObject(tempDC2, nMask);
	obmp2 = (HBITMAP)SelectObject(tempDC3, hbm);
	DrawIconEx(tempDC2, 0, 0, hBottom, 16, 16, 0, nullptr, DI_MASK);
	DrawIconEx(tempDC3, 0, 0, hTop, 16, 16, 0, nullptr, DI_MASK);
	BitBlt(tempDC2, 0, 0, 16, 16, tempDC3, 0, 0, SRCAND);

	GdiFlush();

	SelectObject(tempDC2, obmp);
	DeleteDC(tempDC2);

	SelectObject(tempDC3, obmp2);
	DeleteDC(tempDC3);

	SelectObject(tempDC, oImage);
	DeleteDC(tempDC);

	DeleteObject(hbm);

	iNew.fIcon = TRUE;
	iNew.hbmColor = nImage;
	iNew.hbmMask = nMask;
	res = CreateIconIndirect(&iNew);

	DeleteObject(nImage);
	DeleteObject(nMask);

	return res;
}

HANDLE __fastcall GetIconIndexFromFI(LPTSTR szMirVer)
{
	KN_FP_MASK *base, *overlay1, *overlay2, *overlay3, *overlay4;
	GetIconsIndexes(szMirVer, base, overlay1, overlay2, overlay3, overlay4);
	if (base == nullptr)
		return INVALID_HANDLE_VALUE;

	// MAX: 256 + 64 + 64 + 64 + 64
	FOUNDINFO tmp = { base->idx, 0 };
	if (overlay1) tmp.iOverlay += ((overlay1->idx & 0xFF) << 18);
	if (overlay2) tmp.iOverlay += ((overlay2->idx & 0x3F) << 12);
	if (overlay3) tmp.iOverlay += ((overlay3->idx & 0x3F) << 6);
	if (overlay4) tmp.iOverlay += (overlay4->idx & 0x3F);

	auto *F = arFI.find(&tmp);
	if (F != nullptr)
		return F->hRegisteredImage;

	// not found - then add
	F = new FOUNDINFO(tmp);
	HICON hIcon = CreateIconFromIndexes(base, overlay1, overlay2, overlay3, overlay4);
	if (hIcon != nullptr) {
		F->hRegisteredImage = ExtraIcon_AddIcon(hIcon);
		DestroyIcon(hIcon);
	}
	else F->hRegisteredImage = INVALID_HANDLE_VALUE;
	
	arFI.insert(F);

	return F->hRegisteredImage;
}

VOID ClearFI()
{
	arFI.destroy();
}

/****************************************************************************************
 *	 ServiceGetClientDescrW
 *	 MS_FP_GETCLIENTDESCRW service implementation.
 *	 wParam - LPCWSTR MirVer value
 *	 lParam - (NULL) unused
 *	 returns LPCWSTR: client description (do not destroy) or NULL
 */

static INT_PTR ServiceGetClientDescr(WPARAM wParam, LPARAM)
{
	LPWSTR wszMirVer = (LPWSTR)wParam;  // MirVer value to get client for.
	if (wszMirVer == nullptr)
		return 0;

	LPWSTR wszMirVerUp = NEWWSTR_ALLOCA(wszMirVer); _wcsupr(wszMirVerUp);
	if (mir_wstrcmp(wszMirVerUp, L"?") == 0)
		return (INT_PTR)g_pUnknown->szClientDescription;

	for (auto &it : g_arCommon)
		if (WildCompare(wszMirVerUp, it->szMaskUpper))
			return (INT_PTR)it->szClientDescription;

	return NULL;
}

/****************************************************************************************
 *	 ServiceSameClientW
 *	 MS_FP_SAMECLIENTSW service implementation.
 *	 wParam - LPWSTR first MirVer value
 *	 lParam - LPWSTR second MirVer value
 *	 returns LPCWSTR: client description (do not destroy) if clients are same or NULL
 */

static INT_PTR ServiceSameClients(WPARAM wParam, LPARAM lParam)
{
	if (!wParam || !lParam)
		return NULL; //one of its is not null

	INT_PTR res1 = ServiceGetClientDescr(wParam, 0);
	INT_PTR res2 = ServiceGetClientDescr(lParam, 0);
	return (res1 == res2 && res1 != 0) ? res1 : NULL;
}

/****************************************************************************************
*	OnExtraIconListRebuild
*	Set all registered indexes in array to EMPTY_EXTRA_ICON (unregistered icon)
*/

static int OnExtraIconListRebuild(WPARAM, LPARAM)
{
	ClearFI();
	return 0;
}

/****************************************************************************************
*	OnIconsChanged
*/

static int OnIconsChanged(WPARAM, LPARAM)
{
	ClearFI();
	return 0;
}

/****************************************************************************************
*	 OnExtraImageApply
*	 Try to get MirVer value from db for contact and if success calls ApplyFingerprintImage
*/

int OnExtraImageApply(WPARAM hContact, LPARAM)
{
	if (hContact == NULL)
		return 0;

	ptrW tszMirver;
	char *szProto = Proto_GetBaseAccountName(hContact);
	if (szProto != nullptr)
		tszMirver = db_get_wsa(hContact, szProto, "MirVer");

	ApplyFingerprintImage(hContact, tszMirver);
	return 0;
}

/****************************************************************************************
*	 OnContactSettingChanged
*	 if contact settings changed apply new image or remove it
*/

static int OnContactSettingChanged(WPARAM hContact, LPARAM lParam)
{
	if (hContact == NULL)
		return 0;

	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING*)lParam;
	if (cws && cws->szSetting && !strcmp(cws->szSetting, "MirVer")) {
		switch (cws->value.type) {
		case DBVT_UTF8:
			ApplyFingerprintImage(hContact, ptrW(mir_utf8decodeW(cws->value.pszVal)));
			break;
		case DBVT_ASCIIZ:
			ApplyFingerprintImage(hContact, _A2T(cws->value.pszVal));
			break;
		case DBVT_WCHAR:
			ApplyFingerprintImage(hContact, cws->value.pwszVal);
			break;
		default:
			ApplyFingerprintImage(hContact, nullptr);
		}
	}
	return 0;
}

/****************************************************************************************
*	OnSrmmWindowEvent
*	Monitors SRMM window's creation to draw a statusbar icon
*/

static int OnSrmmWindowEvent(WPARAM uType, LPARAM lParam)
{
	if (!g_plugin.getByte("StatusBarIcon", 1))
		return 0;

	auto *pDlg = (CSrmmBaseDialog *)lParam;

	if (uType == MSG_WINDOW_EVT_OPEN) {
		ptrW ptszMirVer;
		char *szProto = Proto_GetBaseAccountName(pDlg->m_hContact);
		if (szProto != nullptr)
			ptszMirVer = db_get_wsa(pDlg->m_hContact, szProto, "MirVer");
		SetSrmmIcon(pDlg->m_hContact, ptszMirVer);
		arMonitoredWindows.insert((HANDLE)pDlg->m_hContact);
	}
	else if (uType == MSG_WINDOW_EVT_CLOSE)
		arMonitoredWindows.remove((HANDLE)pDlg->m_hContact);

	return 0;
}

/****************************************************************************************
*	OnModulesLoaded
*	Hook necessary events here
*/

int OnModulesLoaded(WPARAM, LPARAM)
{
	g_LPCodePage = Langpack_GetDefaultCodePage();

	// Hook necessary events
	HookEvent(ME_SKIN_ICONSCHANGED, OnIconsChanged);
	HookEvent(ME_MSG_WINDOWEVENT, OnSrmmWindowEvent);

	HookEvent(ME_MC_DEFAULTTCHANGED, OnExtraImageApply);

	PathToAbsoluteW(DEFAULT_SKIN_FOLDER, g_szSkinLib);

	RegisterIcons();

	hExtraIcon = ExtraIcon_RegisterCallback("Client", LPGEN("Fingerprint"), "client_Miranda_unknown",
		OnExtraIconListRebuild, OnExtraImageApply, OnExtraIconClick);

	if (g_plugin.getByte("StatusBarIcon", 1)) {
		StatusIconData sid = {};
		sid.szModule = MODULENAME;
		sid.flags = MBF_HIDDEN;
		sid.dwId = 1;
		Srmm_AddIcon(&sid, &g_plugin);
	}

	return 0;
}

void InitFingerModule()
{
	HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
	HookEvent(ME_OPT_INITIALISE, OnOptInitialise);
	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, OnContactSettingChanged);

	CreateServiceFunction("Fingerprint/SameClients", ServiceSameClients);
	CreateServiceFunction("Fingerprint/GetClientDescr", ServiceGetClientDescr);
	CreateServiceFunction("Fingerprint/GetClientIcon", ServiceGetClientIcon);
}