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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimppickbutton-win32.c
* Copyright (C) 2022 Luca Bacci <luca.bacci@outlook.com>
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpcolor/gimpcolor.h"
#include "gimpwidgetstypes.h"
#include "gimppickbutton.h"
#include "gimppickbutton-win32.h"
#include <sdkddkver.h>
#include <windows.h>
#include <windowsx.h>
#include <stdint.h>
/*
* NOTES:
*
* This implementation is based on gtk/gtkcolorpickerwin32.c from GTK.
*
* We install a low-level mouse hook so that color picking continues
* even when switching active windows.
*
* Beyond that, we also create keep-above, input-only HWNDs and place
* them on the screen to cover each monitor. This is done to show our
* custom cursor and to avoid giving input to other windows: that way
* the desktop appears "frozen" while picking the color.
*
* Finally, we also set up a low-level keyboard hook to dismiss color-
* picking mode whenever the user presses <ESC>.
*
* Note that low-level hooks for mouse and keyboard do not use any DLL
* injection and are thus non-invasive.
*
* For GTK4: consider using an appropriate GDK surface for input-only
* windows. This'd enable us to also get Wintab input when CXO_SYSTEM
* is not set.
*/
typedef struct
{
HMONITOR handle;
wchar_t *device;
HDC hdc;
POINT screen_origin;
int logical_width;
int logical_height;
int physical_width;
int physical_height;
} MonitorData;
GList *pickers;
HHOOK mouse_hook;
HHOOK keyboard_hook;
ATOM notif_window_class;
HWND notif_window_handle;
GArray *monitors;
ATOM input_window_class;
GArray *input_window_handles;
/* Utils */
static HMODULE this_module (void);
static MonitorData * monitors_find_for_logical_point (POINT logical);
static MonitorData * monitors_find_for_physical_point (POINT physical);
static POINT logical_to_physical (MonitorData *data,
POINT logical);
static void destroy_window (gpointer ptr);
/* Mouse cursor */
static HCURSOR create_cursor_from_rgba32_pixbuf (GdkPixbuf *pixbuf,
POINT hotspot);
static HCURSOR create_cursor (void);
/* Low-level mouse hook */
static LRESULT CALLBACK mouse_procedure (int nCode,
WPARAM wParam,
LPARAM lParam);
static gboolean ensure_mouse_hook (void);
static void remove_mouse_hook (void);
/* Low-level keyboard hook */
static LRESULT CALLBACK keyboard_procedure (int nCode,
WPARAM wParam,
LPARAM lParam);
static gboolean ensure_keyboard_hook (void);
static void remove_keyboard_hook (void);
/* Input-only window */
static LRESULT CALLBACK input_window_procedure (HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
static gboolean ensure_input_window_class (void);
static void remove_input_window_class (void);
static HWND create_input_window (POINT origin,
int width,
int height);
/* Hidden notification window */
static LRESULT CALLBACK notif_window_procedure (HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
static gboolean ensure_notif_window_class (void);
static void remove_notif_window_class (void);
static gboolean ensure_notif_window (void);
static void remove_notif_window (void);
/* Monitor enumeration and discovery */
static void monitor_data_free (gpointer ptr);
static BOOL CALLBACK enum_monitor_callback (HMONITOR hMonitor,
HDC hDC,
RECT *pRect,
LPARAM lParam);
static GArray* enumerate_monitors (void);
/* GimpPickButtonWin32 */
static void ensure_input_windows (void);
static void remove_input_windows (void);
static void ensure_monitors (void);
static void remove_monitors (void);
static void ensure_screen_data (void);
static void remove_screen_data (void);
static void screen_changed (void);
static void ensure_screen_tracking (void);
static void remove_screen_tracking (void);
static GimpRGB pick_color_with_gdi (POINT physical_point);
static void user_picked (MonitorData *monitor,
POINT physical_point);
void _gimp_pick_button_win32_pick (GimpPickButton *button);
static void stop_picking (void);
/* {{{ Utils */
/* Gets a handle to the module containing this code.
* Works regardless if we're building a shared or
* static library */
static HMODULE
this_module (void)
{
extern IMAGE_DOS_HEADER __ImageBase;
return (HMODULE) &__ImageBase;
}
static MonitorData*
monitors_find_for_logical_point (POINT logical)
{
HMONITOR monitor_handle;
guint i;
monitor_handle = MonitorFromPoint (logical, MONITOR_DEFAULTTONULL);
if (!monitor_handle)
return NULL;
ensure_monitors ();
for (i = 0; i < monitors->len; i++)
{
MonitorData *data = &g_array_index (monitors, MonitorData, i);
if (data->handle == monitor_handle)
return data;
}
return NULL;
}
static MonitorData*
monitors_find_for_physical_point (POINT physical)
{
guint i;
ensure_monitors ();
for (i = 0; i < monitors->len; i++)
{
MonitorData *data = &g_array_index (monitors, MonitorData, i);
RECT physical_rect;
physical_rect.left = data->screen_origin.x;
physical_rect.top = data->screen_origin.y;
physical_rect.right = physical_rect.left + data->physical_width;
physical_rect.bottom = physical_rect.top + data->physical_height;
/* TODO: tolerance */
if (PtInRect (&physical_rect, physical))
return data;
}
return NULL;
}
static POINT
logical_to_physical (MonitorData *data,
POINT logical)
{
POINT physical = logical;
if (data &&
(data->logical_width != data->physical_width) &&
data->logical_width > 0 && data->physical_width > 0)
{
double dpi_scale = (double) data->physical_width /
(double) data->logical_width;
physical.x = logical.x * dpi_scale;
physical.y = logical.y * dpi_scale;
}
return physical;
}
static void
destroy_window (gpointer ptr)
{
HWND *hwnd = (HWND*) ptr;
DestroyWindow (*hwnd);
}
/* }}}
* {{{ Mouse cursor */
static HCURSOR
create_cursor_from_rgba32_pixbuf (GdkPixbuf *pixbuf,
POINT hotspot)
{
struct {
BITMAPV5HEADER header;
RGBQUAD colors[2];
} info;
HBITMAP bitmap = NULL;
uint8_t *bitmap_data = NULL;
HBITMAP mask = NULL;
uint8_t *mask_data = NULL;
unsigned int mask_stride;
HDC hdc = NULL;
int width;
int height;
int size;
int stride;
const uint8_t *pixbuf_data = NULL;
int i_offset;
int j_offset;
int i;
int j;
ICONINFO icon_info;
HICON icon = NULL;
if (gdk_pixbuf_get_n_channels (pixbuf) != 4)
goto cleanup;
hdc = GetDC (NULL);
if (!hdc)
goto cleanup;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
stride = gdk_pixbuf_get_rowstride (pixbuf);
size = MAX (width, height);
pixbuf_data = gdk_pixbuf_read_pixels (pixbuf);
memset (&info, 0, sizeof (info));
info.header.bV5Size = sizeof (info.header);
info.header.bV5Width = size;
info.header.bV5Height = size;
/* Since Windows XP the OS supports mouse cursors as 32bpp
* bitmaps with alpha channel that are laid out as BGRA32
* (assuming little-endian) */
info.header.bV5Planes = 1;
info.header.bV5BitCount = 32;
info.header.bV5Compression = BI_BITFIELDS;
info.header.bV5BlueMask = 0x000000FF;
info.header.bV5GreenMask = 0x0000FF00;
info.header.bV5RedMask = 0x00FF0000;
info.header.bV5AlphaMask = 0xFF000000;
bitmap = CreateDIBSection (hdc, (BITMAPINFO*) &info, DIB_RGB_COLORS,
(void**) &bitmap_data, NULL, 0);
if (!bitmap || !bitmap_data)
{
g_warning ("CreateDIBSection failed with error code %u",
(unsigned) GetLastError ());
goto cleanup;
}
/* We also need to provide a proper mask HBITMAP, otherwise
* CreateIconIndirect() fails with ERROR_INVALID_PARAMETER.
* This mask bitmap is a bitfield indicating whether the */
memset (&info, 0, sizeof (info));
info.header.bV5Size = sizeof (info.header);
info.header.bV5Width = size;
info.header.bV5Height = size;
info.header.bV5Planes = 1;
info.header.bV5BitCount = 1;
info.header.bV5Compression = BI_RGB;
info.colors[0] = (RGBQUAD){0x00, 0x00, 0x00};
info.colors[1] = (RGBQUAD){0xFF, 0xFF, 0xFF};
mask = CreateDIBSection (hdc, (BITMAPINFO*) &info, DIB_RGB_COLORS,
(void**) &mask_data, NULL, 0);
if (!mask || !mask_data)
{
g_warning ("CreateDIBSection failed with error code %u",
(unsigned) GetLastError ());
goto cleanup;
}
/* MSDN says mask rows are aligned to LONG boundaries */
mask_stride = (((size + 31) & ~31) >> 3);
if (width > height)
{
i_offset = 0;
j_offset = (width - height) / 2;
}
else
{
i_offset = (height - width) / 2;
j_offset = 0;
}
for (j = 0; j < height; j++) /* loop over all the bitmap rows */
{
uint8_t *bitmap_row = bitmap_data + 4 * ((j + j_offset) * size + i_offset);
uint8_t *mask_byte = mask_data + (j + j_offset) * mask_stride + i_offset / 8;
unsigned int mask_bit = (0x80 >> (i_offset % 8));
const uint8_t *pixbuf_row = pixbuf_data + (height - j - 1) * stride;
for (i = 0; i < width; i++) /* loop over the current bitmap row */
{
uint8_t *bitmap_pixel = bitmap_row + 4 * i;
const uint8_t *pixbuf_pixel = pixbuf_row + 4 * i;
/* Assign to destination pixel from source pixel
* by also swapping channels as appropriate */
bitmap_pixel[0] = pixbuf_pixel[2];
bitmap_pixel[1] = pixbuf_pixel[1];
bitmap_pixel[2] = pixbuf_pixel[0];
bitmap_pixel[3] = pixbuf_pixel[3];
if (pixbuf_pixel[3] == 0)
mask_byte[0] |= mask_bit; /* turn ON bit */
else
mask_byte[0] &= ~mask_bit; /* turn OFF bit */
mask_bit >>= 1;
if (mask_bit == 0)
{
mask_bit = 0x80;
mask_byte++;
}
}
}
memset (&icon_info, 0, sizeof (icon_info));
icon_info.fIcon = FALSE;
icon_info.xHotspot = hotspot.x;
icon_info.yHotspot = hotspot.y;
icon_info.hbmMask = mask;
icon_info.hbmColor = bitmap;
icon = CreateIconIndirect (&icon_info);
if (!icon)
{
g_warning ("CreateIconIndirect failed with error code %u",
(unsigned) GetLastError ());
goto cleanup;
}
cleanup:
if (mask)
DeleteObject (mask);
if (bitmap)
DeleteObject (bitmap);
if (hdc)
ReleaseDC (NULL, hdc);
return (HCURSOR) icon;
}
static HCURSOR
create_cursor (void)
{
GdkPixbuf *pixbuf = NULL;
GError *error = NULL;
HCURSOR cursor = NULL;
pixbuf = gdk_pixbuf_new_from_resource ("/org/gimp/color-picker-cursors/cursor-color-picker.png",
&error);
if (!pixbuf)
{
g_critical ("gdk_pixbuf_new_from_resource failed: %s",
error ? error->message : "unknown error");
goto cleanup;
}
g_clear_error (&error);
cursor = create_cursor_from_rgba32_pixbuf (pixbuf, (POINT){ 1, 30 });
cleanup:
g_clear_error (&error);
g_clear_object (&pixbuf);
return cursor;
}
/* }}}
* {{{ Low-level mouse hook */
/* This mouse procedure can detect clicks made on any
* application window. Countrary to mouse capture, this
* method continues to work even after switching active
* windows. */
static LRESULT CALLBACK
mouse_procedure (int nCode,
WPARAM wParam,
LPARAM lParam)
{
if (nCode == HC_ACTION)
{
MSLLHOOKSTRUCT *info = (MSLLHOOKSTRUCT*) lParam;
switch (wParam)
{
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_XBUTTONDOWN:
{
POINT physical;
MonitorData *data;
if (pickers == NULL)
break;
/* A low-level mouse hook always receives points in
* per-monitor DPI-aware screen coordinates, regardless of
* the DPI awareness setting of the application. */
physical = info->pt;
data = monitors_find_for_physical_point (physical);
if (!data)
g_message ("Captured point (%ld, %ld) doesn't belong to any monitor",
(long) physical.x, (long) physical.y);
user_picked (data, physical);
/* It's safe to remove a hook from within its callback.
* Anyway this can even be called from an idle callback,
* as the hook does nothing if there are no pickers.
* (In that case also the ensure functions have to be
* scheduled in an idle callback) */
stop_picking ();
return (wParam == WM_XBUTTONDOWN) ? TRUE : 0;
}
break;
default:
break;
}
}
return CallNextHookEx (NULL, nCode, wParam, lParam);
}
static gboolean
ensure_mouse_hook (void)
{
if (!mouse_hook)
{
mouse_hook = SetWindowsHookEx (WH_MOUSE_LL, mouse_procedure, this_module (), 0);
if (!mouse_hook)
{
g_warning ("SetWindowsHookEx failed with error code %u",
(unsigned) GetLastError ());
return FALSE;
}
}
return TRUE;
}
static void
remove_mouse_hook (void)
{
if (mouse_hook)
{
if (!UnhookWindowsHookEx (mouse_hook))
{
g_warning ("UnhookWindowsHookEx failed with error code %u",
(unsigned) GetLastError ());
return;
}
mouse_hook = NULL;
}
}
/* }}}
* {{{ Low-level keyboard hook */
/* This is used to stop picking anytime the user presses ESC */
static LRESULT CALLBACK
keyboard_procedure (int nCode,
WPARAM wParam,
LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT *info = (KBDLLHOOKSTRUCT*) lParam;
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (info->vkCode == VK_ESCAPE)
{
stop_picking ();
return 1;
}
break;
default:
break;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
static gboolean
ensure_keyboard_hook (void)
{
if (!keyboard_hook)
{
keyboard_hook = SetWindowsHookEx (WH_KEYBOARD_LL, keyboard_procedure, this_module (), 0);
if (!keyboard_hook)
{
g_warning ("SetWindowsHookEx failed with error code %u",
(unsigned) GetLastError ());
return FALSE;
}
}
return TRUE;
}
static void
remove_keyboard_hook (void)
{
if (keyboard_hook)
{
if (!UnhookWindowsHookEx (keyboard_hook))
{
g_warning ("UnhookWindowsHookEx failed with error code %u",
(unsigned) GetLastError ());
return;
}
keyboard_hook = NULL;
}
}
/* }}}
* {{{ Input-only windows */
/* Those input-only windows are placed to cover each monitor on
* the screen and serve two purposes: display our custom mouse
* cursor and freeze the desktop by avoiding interaction of the
* mouse with other applications */
static LRESULT CALLBACK
input_window_procedure (HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_NCCREATE:
/* The shell automatically hides the taskbar when a window
* covers the entire area of a monitor (fullscreened). In
* order to avoid that, we can set a special property on
* the window
* See the docs for ITaskbarList2::MarkFullscreenWindow()
* on MSDN for more informations */
if (!SetPropW (hwnd, L"NonRudeHWND", (HANDLE) TRUE))
g_warning_once ("SetPropW failed with error code %u",
(unsigned) GetLastError ());
break;
case WM_NCDESTROY:
/* We have to remove window properties manually before the
* window gets destroyed */
if (!RemovePropW (hwnd, L"NonRudeHWND"))
g_warning_once ("SetPropW failed with error code %u",
(unsigned) GetLastError ());
break;
/* Avoid any drawing at all. Here we block processing of the
* default window procedure, although we set the NULL_BRUSH
* as the window class's background brush, so even the default
* window procedure wouldn't draw anything at all */
case WM_ERASEBKGND:
return 1;
case WM_PAINT:
{
#if 1
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
#else
UINT flags = RDW_VALIDATE |
RDW_NOFRAME |
RDW_NOERASE;
RedrawWindow (hwnd, NULL, NULL, flags);
#endif
}
return 0;
#if 0
case WM_SYNCPAINT:
return 0;
#endif
case WM_MOUSEACTIVATE:
/* This can be useful in case we want to avoid
* using a mouse hook */
return MA_NOACTIVATE;
/* Anytime we detect a mouse click, pick the color. Note that
* this is rarely used as the mouse hook would process the
* clicks before that */
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_XBUTTONDOWN:
{
POINT logical = (POINT){GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam)};
MonitorData *data = monitors_find_for_logical_point (logical);
POINT physical = logical_to_physical (data, logical);
if (!data)
g_message ("Captured point (%ld, %ld) doesn't belong to any monitor",
(long) logical.x, (long) logical.y);
user_picked (data, physical);
stop_picking ();
}
return (uMsg == WM_XBUTTONDOWN) ? TRUE : 0;
default:
break;
}
return DefWindowProcW (hwnd, uMsg, wParam, lParam);
}
static gboolean
ensure_input_window_class (void)
{
if (!input_window_class)
{
WNDCLASSEXW wndclassex;
HCURSOR cursor = create_cursor ();
memset (&wndclassex, 0, sizeof (wndclassex));
wndclassex.cbSize = sizeof (wndclassex);
wndclassex.hInstance = this_module ();
wndclassex.lpszClassName = L"GimpPickButtonInputWindowClass";
wndclassex.lpfnWndProc = input_window_procedure;
wndclassex.hbrBackground = GetStockObject (NULL_BRUSH);
wndclassex.hCursor = cursor ?
cursor :
LoadImageW (NULL,
(LPCWSTR)(guintptr)IDC_CROSS,
IMAGE_CURSOR, 0, 0,
LR_DEFAULTSIZE | LR_SHARED);
input_window_class = RegisterClassExW (&wndclassex);
if (input_window_class == 0)
{
g_warning ("RegisterClassExW failed with error code %u",
(unsigned) GetLastError ());
if (cursor)
DestroyCursor (cursor);
return FALSE;
}
}
return TRUE;
}
static void
remove_input_window_class (void)
{
if (input_window_class)
{
LPCWSTR name = (LPCWSTR)(guintptr)input_window_class;
UnregisterClassW (name, this_module ());
input_window_class = 0;
}
}
/* create_input_window expects logical screen coordinates */
static HWND
create_input_window (POINT origin,
int width,
int height)
{
DWORD stylex = WS_EX_NOACTIVATE | WS_EX_TOPMOST;
LPCWSTR wclass;
LPCWSTR title = L"Gimp Input Window";
DWORD style = WS_POPUP;
HWND hwnd;
if (!ensure_input_window_class ())
return FALSE;
/* This must go after the ensure_input_window_class */
wclass = (LPCWSTR)(guintptr)input_window_class;
hwnd = CreateWindowExW (stylex, wclass, title, style,
origin.x, origin.y, width, height,
NULL, NULL, this_module (), NULL);
if (hwnd == NULL)
{
g_warning_once ("CreateWindowExW failed with error code %u",
(unsigned) GetLastError ());
return FALSE;
}
ShowWindow (hwnd, SW_SHOWNOACTIVATE);
return hwnd;
}
/* }}}
* {{{ Hidden notification window */
/* We setup a hidden window to listen for WM_DISPLAYCAHNGE
* messages and reposition the input-only windows on the
* screen */
static LRESULT CALLBACK
notif_window_procedure (HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_DISPLAYCHANGE:
screen_changed ();
break;
default:
break;
}
return DefWindowProcW (hwnd, uMsg, wParam, lParam);
}
static gboolean
ensure_notif_window_class (void)
{
if (!notif_window_class)
{
WNDCLASSEXW wndclassex;
memset (&wndclassex, 0, sizeof (wndclassex));
wndclassex.cbSize = sizeof (wndclassex);
wndclassex.hInstance = this_module ();
wndclassex.lpszClassName = L"GimpPickButtonNotifWindowClass";
wndclassex.lpfnWndProc = notif_window_procedure;
notif_window_class = RegisterClassExW (&wndclassex);
if (notif_window_class == 0)
{
g_warning ("RegisterClassExW failed with error code %u",
(unsigned) GetLastError ());
return FALSE;
}
}
return TRUE;
}
static void
remove_notif_window_class (void)
{
if (notif_window_class)
{
LPCWSTR name = (LPCWSTR)(guintptr)notif_window_class;
UnregisterClassW (name, this_module ());
notif_window_class = 0;
}
}
static gboolean
ensure_notif_window (void)
{
if (!ensure_notif_window_class ())
return FALSE;
if (!notif_window_handle)
{
DWORD stylex = 0;
LPCWSTR wclass = (LPCWSTR)(guintptr)notif_window_class;
LPCWSTR title = L"Gimp Notifications Window";
DWORD style = WS_POPUP;
notif_window_handle = CreateWindowExW (stylex, wclass,
title, style,
0, 0, 1, 1,
NULL, NULL,
this_module (),
NULL);
if (notif_window_handle == NULL)
{
g_warning_once ("CreateWindowExW failed with error code %u",
(unsigned) GetLastError ());
return FALSE;
}
}
return TRUE;
}
static void
remove_notif_window (void)
{
if (notif_window_handle)
{
DestroyWindow (notif_window_handle);
notif_window_handle = NULL;
}
remove_notif_window_class ();
}
/* }}}
* {{{ Monitor enumeration and discovery */
static void
monitor_data_free (gpointer ptr)
{
MonitorData *data = (MonitorData*) ptr;
if (data->device)
free (data->device);
}
static BOOL CALLBACK
enum_monitor_callback (HMONITOR hMonitor,
HDC hDC,
RECT *pRect,
LPARAM lParam)
{
GArray *result = (GArray*) lParam;
MonitorData data;
MONITORINFOEXW info;
DEVMODEW devmode;
const BOOL CALLBACK_CONTINUE = TRUE;
const BOOL CALLBACK_STOP = FALSE;
if (!pRect)
return CALLBACK_CONTINUE;
if (IsRectEmpty (pRect))
return CALLBACK_CONTINUE;
memset (&data, 0, sizeof (data));
data.handle = hMonitor;
data.hdc = hDC;
data.screen_origin.x = pRect->left;
data.screen_origin.y = pRect->top;
data.logical_width = pRect->right - pRect->left;
data.logical_height = pRect->bottom - pRect->top;
memset (&info, 0, sizeof (info));
info.cbSize = sizeof (info);
if (!GetMonitorInfoW (hMonitor, (MONITORINFO*) &info))
{
g_warning_once ("GetMonitorInfo failed with error code %u",
(unsigned) GetLastError ());
return CALLBACK_CONTINUE;
}
data.device = _wcsdup (info.szDevice);
memset (&devmode, 0, sizeof (devmode));
devmode.dmSize = sizeof (devmode);
if (!EnumDisplaySettingsExW (info.szDevice,
ENUM_CURRENT_SETTINGS,
&devmode, EDS_ROTATEDMODE))
{
g_warning_once ("EnumDisplaySettingsEx failed with error code %u",
(unsigned) GetLastError ());
return CALLBACK_CONTINUE;
}
if (devmode.dmPelsWidth)
data.physical_width = devmode.dmPelsWidth;
if (devmode.dmPelsHeight)
data.physical_height = devmode.dmPelsHeight;
g_array_append_val (result, data);
if (result->len >= 100)
return CALLBACK_STOP;
return CALLBACK_CONTINUE;
}
static GArray*
enumerate_monitors (void)
{
int count_monitors;
guint length_hint;
GArray *result;
count_monitors = GetSystemMetrics (SM_CMONITORS);
if (count_monitors > 0 && count_monitors < 100)
length_hint = (guint) count_monitors;
else
length_hint = 1;
result = g_array_sized_new (FALSE, TRUE, sizeof (MonitorData), length_hint);
g_array_set_clear_func (result, monitor_data_free);
if (!EnumDisplayMonitors (NULL, NULL, enum_monitor_callback, (LPARAM) result))
{
g_warning ("EnumDisplayMonitors failed with error code %u",
(unsigned) GetLastError ());
}
return result;
}
static void
ensure_input_windows (void)
{
ensure_monitors ();
if (!input_window_handles)
{
guint i;
input_window_handles = g_array_sized_new (FALSE, TRUE,
sizeof (HWND),
monitors->len);
g_array_set_clear_func (input_window_handles, destroy_window);
for (i = 0; i < monitors->len; i++)
{
MonitorData *data = &g_array_index (monitors, MonitorData, i);
HWND hwnd = create_input_window (data->screen_origin,
data->logical_width,
data->logical_height);
if (hwnd)
g_array_append_val (input_window_handles, hwnd);
}
}
}
static void
remove_input_windows (void)
{
if (input_window_handles)
{
g_array_free (input_window_handles, TRUE);
input_window_handles = NULL;
}
}
static void
ensure_monitors (void)
{
if (!monitors)
monitors = enumerate_monitors ();
}
static void
remove_monitors (void)
{
if (monitors)
{
g_array_free (monitors, TRUE);
monitors = NULL;
}
}
static void
ensure_screen_data (void)
{
ensure_monitors ();
ensure_input_windows ();
}
static void
remove_screen_data (void)
{
remove_input_windows ();
remove_monitors ();
}
static void
screen_changed (void)
{
remove_screen_data ();
ensure_screen_data ();
}
static void
ensure_screen_tracking (void)
{
ensure_notif_window ();
screen_changed ();
}
static void
remove_screen_tracking (void)
{
remove_notif_window ();
remove_screen_data ();
}
/* }}}
* {{{ GimpPickButtonWin32 */
/* pick_color_with_gdi is based on the GDI GetPixel() function.
* Note that GDI only returns 8bit per-channel color data, but
* as of today there's no documented method to retrieve colors
* at higher bit depths. */
static GimpRGB
pick_color_with_gdi (POINT physical_point)
{
GimpRGB rgb;
COLORREF color;
HDC hdc;
hdc = GetDC (HWND_DESKTOP);
if (!(GetDeviceCaps (hdc, RASTERCAPS) & RC_BITBLT))
g_warning_once ("RC_BITBLT capability missing from device context");
color = GetPixel (hdc, physical_point.x, physical_point.y);
gimp_rgba_set_uchar (&rgb, GetRValue (color), GetGValue (color), GetBValue (color), 255);
ReleaseDC (HWND_DESKTOP, hdc);
return rgb;
}
static void
user_picked (MonitorData *monitor,
POINT physical_point)
{
GimpRGB rgb;
GList *l;
/* Currently unused */
(void) monitor;
rgb = pick_color_with_gdi (physical_point);
for (l = pickers; l != NULL; l = l->next)
{
GimpPickButton *button = GIMP_PICK_BUTTON (l->data);
g_signal_emit_by_name (button, "color-picked", &rgb);
}
}
/* entry point to this file, called from gimppickbutton.c */
void
_gimp_pick_button_win32_pick (GimpPickButton *button)
{
ensure_mouse_hook ();
ensure_keyboard_hook ();
ensure_screen_tracking ();
if (g_list_find (pickers, button))
return;
pickers = g_list_prepend (pickers,
g_object_ref (button));
}
static void
stop_picking (void)
{
remove_screen_tracking ();
remove_keyboard_hook ();
remove_mouse_hook ();
g_list_free_full (pickers, g_object_unref);
pickers = NULL;
}
|