-
Notifications
You must be signed in to change notification settings - Fork 282
/
win32drv.c
1763 lines (1539 loc) · 50 KB
/
win32drv.c
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
/**
* @file win32drv.c
*
*/
/*********************
* INCLUDES
*********************/
#include "win32drv.h"
#include <windowsx.h>
#include <malloc.h>
#include <process.h>
#include <stdbool.h>
#include <stdint.h>
#pragma comment(lib, "Imm32.lib")
/*********************
* DEFINES
*********************/
#define LV_WINDOWS_ZOOM_LEVEL 100
#define LV_WINDOWS_SIMULATOR_MODE 0
#define LV_WINDOWS_ALLOW_DPI_OVERRIDE 0
#define WINDOW_EX_STYLE \
WS_EX_CLIENTEDGE
#define WINDOW_STYLE \
WS_OVERLAPPEDWINDOW //(WS_OVERLAPPEDWINDOW & ~(WS_SIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME))
#define LV_WINDOWS_ZOOM_BASE_LEVEL 100
#ifndef LV_WINDOWS_ZOOM_LEVEL
#define LV_WINDOWS_ZOOM_LEVEL LV_WINDOWS_ZOOM_BASE_LEVEL
#endif
#ifndef USER_DEFAULT_SCREEN_DPI
#define USER_DEFAULT_SCREEN_DPI 96
#endif
/**********************
* TYPEDEFS
**********************/
typedef struct _WINDOW_THREAD_PARAMETER
{
HANDLE window_mutex;
HINSTANCE instance_handle;
HICON icon_handle;
int32_t hor_res;
int32_t ver_res;
int show_window_mode;
} WINDOW_THREAD_PARAMETER, * PWINDOW_THREAD_PARAMETER;
/**********************
* STATIC PROTOTYPES
**********************/
/**
* @brief Creates a B8G8R8A8 frame buffer.
* @param WindowHandle A handle to the window for the creation of the frame
* buffer. If this value is NULL, the entire screen will be
* referenced.
* @param Width The width of the frame buffer.
* @param Height The height of the frame buffer.
* @param PixelBuffer The raw pixel buffer of the frame buffer you created.
* @param PixelBufferSize The size of the frame buffer you created.
* @return If the function succeeds, the return value is a handle to the device
* context (DC) for the frame buffer. If the function fails, the return
* value is NULL, and PixelBuffer parameter is NULL.
*/
static HDC lv_windows_create_frame_buffer(
_In_opt_ HWND WindowHandle,
_In_ LONG Width,
_In_ LONG Height,
_Out_ UINT32** PixelBuffer,
_Out_ SIZE_T* PixelBufferSize);
/**
* @brief Enables WM_DPICHANGED message for child window for the associated
* window.
* @param WindowHandle The window you want to enable WM_DPICHANGED message for
* child window.
* @return If the function succeeds, the return value is non-zero. If the
* function fails, the return value is zero.
* @remarks You need to use this function in Windows 10 Threshold 1 or Windows
* 10 Threshold 2.
*/
static BOOL lv_windows_enable_child_window_dpi_message(
_In_ HWND WindowHandle);
/**
* @brief Registers a window as being touch-capable.
* @param hWnd The handle of the window being registered.
* @param ulFlags A set of bit flags that specify optional modifications.
* @return If the function succeeds, the return value is nonzero. If the
* function fails, the return value is zero.
* @remark For more information, see RegisterTouchWindow.
*/
static BOOL lv_windows_register_touch_window(
HWND hWnd,
ULONG ulFlags);
/**
* @brief Retrieves detailed information about touch inputs associated with a
* particular touch input handle.
* @param hTouchInput The touch input handle received in the LPARAM of a touch
* message.
* @param cInputs The number of structures in the pInputs array.
* @param pInputs A pointer to an array of TOUCHINPUT structures to receive
* information about the touch points associated with the
* specified touch input handle.
* @param cbSize The size, in bytes, of a single TOUCHINPUT structure.
* @return If the function succeeds, the return value is nonzero. If the
* function fails, the return value is zero.
* @remark For more information, see GetTouchInputInfo.
*/
static BOOL lv_windows_get_touch_input_info(
HTOUCHINPUT hTouchInput,
UINT cInputs,
PTOUCHINPUT pInputs,
int cbSize);
/**
* @brief Closes a touch input handle, frees process memory associated with it,
and invalidates the handle.
* @param hTouchInput The touch input handle received in the LPARAM of a touch
* message.
* @return If the function succeeds, the return value is nonzero. If the
* function fails, the return value is zero.
* @remark For more information, see CloseTouchInputHandle.
*/
static BOOL lv_windows_close_touch_input_handle(
HTOUCHINPUT hTouchInput);
/**
* @brief Returns the dots per inch (dpi) value for the associated window.
* @param WindowHandle The window you want to get information about.
* @return The DPI for the window.
*/
static UINT lv_windows_get_dpi_for_window(
_In_ HWND WindowHandle);
static void lv_windows_display_driver_flush_callback(
lv_disp_t* disp_drv,
const lv_area_t* area,
uint8_t* px_map);
static void lv_windows_pointer_driver_read_callback(
lv_indev_t* indev,
lv_indev_data_t* data);
static void lv_windows_keypad_driver_read_callback(
lv_indev_t* indev,
lv_indev_data_t* data);
static void lv_windows_encoder_driver_read_callback(
lv_indev_t* indev,
lv_indev_data_t* data);
static lv_windows_window_context_t* lv_windows_get_display_context(
lv_disp_t* display);
static LRESULT CALLBACK lv_windows_window_message_callback(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
static unsigned int __stdcall lv_windows_window_thread_entrypoint(
void* raw_parameter);
static void lv_windows_push_key_to_keyboard_queue(
lv_windows_window_context_t* context,
uint32_t key,
lv_indev_state_t state)
{
lv_windows_keypad_queue_item_t* current = (lv_windows_keypad_queue_item_t*)(
_lv_ll_ins_tail(&context->keypad.queue));
if (current)
{
current->key = key;
current->state = state;
}
}
/**********************
* GLOBAL VARIABLES
**********************/
/**********************
* STATIC VARIABLES
**********************/
static HWND g_window_handle = NULL;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
EXTERN_C lv_windows_window_context_t* lv_windows_get_window_context(
HWND window_handle)
{
return (lv_windows_window_context_t*)(
GetPropW(window_handle, L"LVGL.Window.Context"));
}
static void lv_windows_check_display_existence_timer_callback(lv_timer_t* timer)
{
if (!lv_display_get_next(NULL))
{
// Don't use lv_deinit() due to it will cause exception when parallel
// rendering is enabled.
exit(0);
}
}
EXTERN_C bool lv_windows_init_window_class()
{
lv_timer_create(
lv_windows_check_display_existence_timer_callback,
200,
NULL);
// Try to ensure the default group exists.
{
lv_group_t* default_group = lv_group_get_default();
if (!default_group)
{
default_group = lv_group_create();
if (default_group)
{
lv_group_set_default(default_group);
}
}
}
WNDCLASSEXW window_class;
window_class.cbSize = sizeof(WNDCLASSEXW);
window_class.style = 0;
window_class.lpfnWndProc = lv_windows_window_message_callback;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = NULL;
window_class.hIcon = NULL;
window_class.hCursor = LoadCursorW(NULL, IDC_ARROW);
window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
window_class.lpszMenuName = NULL;
window_class.lpszClassName = LVGL_SIMULATOR_WINDOW_CLASS;
window_class.hIconSm = NULL;
return RegisterClassExW(&window_class);
}
EXTERN_C HWND lv_windows_create_display_window(
const wchar_t* window_title,
int32_t hor_res,
int32_t ver_res,
HINSTANCE instance_handle,
HICON icon_handle,
int show_window_mode)
{
HWND display_window_handle = CreateWindowExW(
WINDOW_EX_STYLE,
LVGL_SIMULATOR_WINDOW_CLASS,
window_title,
WINDOW_STYLE,
CW_USEDEFAULT,
0,
hor_res,
ver_res,
NULL,
NULL,
instance_handle,
NULL);
if (display_window_handle)
{
SendMessageW(
display_window_handle,
WM_SETICON,
TRUE,
(LPARAM)icon_handle);
SendMessageW(
display_window_handle,
WM_SETICON,
FALSE,
(LPARAM)icon_handle);
ShowWindow(display_window_handle, show_window_mode);
UpdateWindow(display_window_handle);
}
return display_window_handle;
}
EXTERN_C bool lv_windows_init(
HINSTANCE instance_handle,
int show_window_mode,
int32_t hor_res,
int32_t ver_res,
HICON icon_handle)
{
if (!lv_windows_init_window_class())
{
return false;
}
PWINDOW_THREAD_PARAMETER parameter =
(PWINDOW_THREAD_PARAMETER)malloc(sizeof(WINDOW_THREAD_PARAMETER));
parameter->window_mutex = CreateEventExW(NULL, NULL, 0, EVENT_ALL_ACCESS);
parameter->instance_handle = instance_handle;
parameter->icon_handle = icon_handle;
parameter->hor_res = hor_res;
parameter->ver_res = ver_res;
parameter->show_window_mode = show_window_mode;
_beginthreadex(
NULL,
0,
lv_windows_window_thread_entrypoint,
parameter,
0,
NULL);
WaitForSingleObjectEx(parameter->window_mutex, INFINITE, FALSE);
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_window_context(g_window_handle));
if (!context)
{
return false;
}
return true;
}
static void lv_windows_release_pointer_device_event_callback(lv_event_t* e)
{
lv_indev_t* indev = (lv_indev_t*)lv_event_get_user_data(e);
if (!indev)
{
return;
}
lv_display_t* display = lv_indev_get_disp(indev);
if (!display)
{
return;
}
HWND window_handle = (HWND)lv_display_get_driver_data(display);
if (!window_handle)
{
return;
}
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_window_context(
window_handle));
if (!context)
{
return;
}
context->pointer.state = LV_INDEV_STATE_RELEASED;
context->pointer.point.x = 0;
context->pointer.point.y = 0;
context->pointer.indev = NULL;
}
EXTERN_C lv_indev_t* lv_windows_acquire_pointer_device(
lv_display_t* display)
{
HWND window_handle = (HWND)lv_display_get_driver_data(display);
if (!window_handle)
{
return NULL;
}
lv_windows_window_context_t* context =
(lv_windows_window_context_t*)(lv_windows_get_window_context(
window_handle));
if (!context)
{
return NULL;
}
if (!context->pointer.indev)
{
context->pointer.state = LV_INDEV_STATE_RELEASED;
context->pointer.point.x = 0;
context->pointer.point.y = 0;
context->pointer.indev = lv_indev_create();
if (context->pointer.indev)
{
lv_indev_set_type(
context->pointer.indev,
LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(
context->pointer.indev,
lv_windows_pointer_driver_read_callback);
lv_indev_set_disp(
context->pointer.indev,
context->display_device_object);
lv_indev_add_event_cb(
context->pointer.indev,
lv_windows_release_pointer_device_event_callback,
LV_EVENT_DELETE,
context->pointer.indev);
lv_indev_set_group(
context->pointer.indev,
lv_group_get_default());
}
}
return context->pointer.indev;
}
static void lv_windows_release_keypad_device_event_callback(lv_event_t* e)
{
lv_indev_t* indev = (lv_indev_t*)lv_event_get_user_data(e);
if (!indev)
{
return;
}
lv_display_t* display = lv_indev_get_disp(indev);
if (!display)
{
return;
}
HWND window_handle = (HWND)lv_display_get_driver_data(display);
if (!window_handle)
{
return;
}
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_window_context(
window_handle));
if (!context)
{
return;
}
DeleteCriticalSection(&context->keypad.mutex);
_lv_ll_clear(&context->keypad.queue);
context->keypad.utf16_high_surrogate = 0;
context->keypad.utf16_low_surrogate = 0;
context->keypad.indev = NULL;
}
EXTERN_C lv_indev_t* lv_windows_acquire_keypad_device(
lv_display_t* display)
{
HWND window_handle = (HWND)lv_display_get_driver_data(display);
if (!window_handle)
{
return NULL;
}
lv_windows_window_context_t* context =
(lv_windows_window_context_t*)(lv_windows_get_window_context(
window_handle));
if (!context)
{
return NULL;
}
if (!context->keypad.indev)
{
InitializeCriticalSection(&context->keypad.mutex);
_lv_ll_init(
&context->keypad.queue,
sizeof(lv_windows_keypad_queue_item_t));
context->keypad.utf16_high_surrogate = 0;
context->keypad.utf16_low_surrogate = 0;
context->keypad.indev = lv_indev_create();
if (context->keypad.indev)
{
lv_indev_set_type(
context->keypad.indev,
LV_INDEV_TYPE_KEYPAD);
lv_indev_set_read_cb(
context->keypad.indev,
lv_windows_keypad_driver_read_callback);
lv_indev_set_disp(
context->keypad.indev,
context->display_device_object);
lv_indev_add_event_cb(
context->keypad.indev,
lv_windows_release_keypad_device_event_callback,
LV_EVENT_DELETE,
context->keypad.indev);
lv_indev_set_group(
context->keypad.indev,
lv_group_get_default());
}
}
return context->keypad.indev;
}
static void lv_windows_release_encoder_device_event_callback(lv_event_t* e)
{
lv_indev_t* indev = (lv_indev_t*)lv_event_get_user_data(e);
if (!indev)
{
return;
}
lv_display_t* display = lv_indev_get_disp(indev);
if (!display)
{
return;
}
HWND window_handle = (HWND)lv_display_get_driver_data(display);
if (!window_handle)
{
return;
}
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_window_context(
window_handle));
if (!context)
{
return;
}
context->encoder.state = LV_INDEV_STATE_RELEASED;
context->encoder.enc_diff = 0;
context->encoder.indev = NULL;
}
EXTERN_C lv_indev_t* lv_windows_acquire_encoder_device(
lv_display_t* display)
{
HWND window_handle = (HWND)lv_display_get_driver_data(display);
if (!window_handle)
{
return NULL;
}
lv_windows_window_context_t* context =
(lv_windows_window_context_t*)(lv_windows_get_window_context(
window_handle));
if (!context)
{
return NULL;
}
if (!context->encoder.indev)
{
context->encoder.state = LV_INDEV_STATE_RELEASED;
context->encoder.enc_diff = 0;
context->encoder.indev = lv_indev_create();
if (context->encoder.indev)
{
lv_indev_set_type(
context->encoder.indev,
LV_INDEV_TYPE_ENCODER);
lv_indev_set_read_cb(
context->encoder.indev,
lv_windows_encoder_driver_read_callback);
lv_indev_set_disp(
context->encoder.indev,
context->display_device_object);
lv_indev_add_event_cb(
context->encoder.indev,
lv_windows_release_encoder_device_event_callback,
LV_EVENT_DELETE,
context->encoder.indev);
lv_indev_set_group(
context->encoder.indev,
lv_group_get_default());
}
}
return context->encoder.indev;
}
/**********************
* STATIC FUNCTIONS
**********************/
static HDC lv_windows_create_frame_buffer(
HWND WindowHandle,
LONG Width,
LONG Height,
UINT32** PixelBuffer,
SIZE_T* PixelBufferSize)
{
HDC hFrameBufferDC = NULL;
if (PixelBuffer && PixelBufferSize)
{
HDC hWindowDC = GetDC(WindowHandle);
if (hWindowDC)
{
hFrameBufferDC = CreateCompatibleDC(hWindowDC);
ReleaseDC(WindowHandle, hWindowDC);
}
if (hFrameBufferDC)
{
#if (LV_COLOR_DEPTH == 32) || (LV_COLOR_DEPTH == 24)
BITMAPINFO BitmapInfo = { 0 };
#elif (LV_COLOR_DEPTH == 16)
typedef struct _BITMAPINFO_16BPP {
BITMAPINFOHEADER bmiHeader;
DWORD bmiColorMask[3];
} BITMAPINFO_16BPP, *PBITMAPINFO_16BPP;
BITMAPINFO_16BPP BitmapInfo = { 0 };
#else
#error [lv_windows] Unsupported LV_COLOR_DEPTH.
#endif
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = Width;
BitmapInfo.bmiHeader.biHeight = -Height;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = lv_color_format_get_bpp(
LV_COLOR_FORMAT_NATIVE);
#if (LV_COLOR_DEPTH == 32) || (LV_COLOR_DEPTH == 24)
BitmapInfo.bmiHeader.biCompression = BI_RGB;
#elif (LV_COLOR_DEPTH == 16)
BitmapInfo.bmiHeader.biCompression = BI_BITFIELDS;
BitmapInfo.bmiColorMask[0] = 0xF800;
BitmapInfo.bmiColorMask[1] = 0x07E0;
BitmapInfo.bmiColorMask[2] = 0x001F;
#else
#error [lv_windows] Unsupported LV_COLOR_DEPTH.
#endif
HBITMAP hBitmap = CreateDIBSection(
hFrameBufferDC,
(PBITMAPINFO)(&BitmapInfo),
DIB_RGB_COLORS,
(void**)PixelBuffer,
NULL,
0);
if (hBitmap)
{
*PixelBufferSize = Width * Height;
*PixelBufferSize *= lv_color_format_get_size(
LV_COLOR_FORMAT_NATIVE);
DeleteObject(SelectObject(hFrameBufferDC, hBitmap));
DeleteObject(hBitmap);
}
else
{
DeleteDC(hFrameBufferDC);
hFrameBufferDC = NULL;
}
}
}
return hFrameBufferDC;
}
static BOOL lv_windows_enable_child_window_dpi_message(
HWND WindowHandle)
{
// The private Per-Monitor DPI Awareness support extension is Windows 10
// only. We don't need the private Per-Monitor DPI Awareness support
// extension if the Per-Monitor (V2) DPI Awareness exists.
OSVERSIONINFOEXW OSVersionInfoEx = { 0 };
OSVersionInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
OSVersionInfoEx.dwMajorVersion = 10;
OSVersionInfoEx.dwMinorVersion = 0;
OSVersionInfoEx.dwBuildNumber = 14986;
if (!VerifyVersionInfoW(
&OSVersionInfoEx,
VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER,
VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(
0,
VER_MAJORVERSION,
VER_GREATER_EQUAL),
VER_MINORVERSION,
VER_GREATER_EQUAL),
VER_BUILDNUMBER,
VER_LESS)))
{
return FALSE;
}
HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
if (!ModuleHandle)
{
return FALSE;
}
typedef BOOL(WINAPI* FunctionType)(HWND, BOOL);
FunctionType pFunction = (FunctionType)(
GetProcAddress(ModuleHandle, "EnableChildWindowDpiMessage"));
if (!pFunction)
{
return FALSE;
}
return pFunction(WindowHandle, TRUE);
}
static BOOL lv_windows_register_touch_window(
HWND hWnd,
ULONG ulFlags)
{
HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
if (!ModuleHandle)
{
return FALSE;
}
typedef BOOL(WINAPI* FunctionType)(HWND, ULONG);
FunctionType pFunction = (FunctionType)(
GetProcAddress(ModuleHandle, "RegisterTouchWindow"));
if (!pFunction)
{
return FALSE;
}
return pFunction(hWnd, ulFlags);
}
static BOOL lv_windows_get_touch_input_info(
HTOUCHINPUT hTouchInput,
UINT cInputs,
PTOUCHINPUT pInputs,
int cbSize)
{
HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
if (!ModuleHandle)
{
return FALSE;
}
typedef BOOL(WINAPI* FunctionType)(HTOUCHINPUT, UINT, PTOUCHINPUT, int);
FunctionType pFunction = (FunctionType)(
GetProcAddress(ModuleHandle, "GetTouchInputInfo"));
if (!pFunction)
{
return FALSE;
}
return pFunction(hTouchInput, cInputs, pInputs, cbSize);
}
static BOOL lv_windows_close_touch_input_handle(
HTOUCHINPUT hTouchInput)
{
HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
if (!ModuleHandle)
{
return FALSE;
}
typedef BOOL(WINAPI* FunctionType)(HTOUCHINPUT);
FunctionType pFunction = (FunctionType)(
GetProcAddress(ModuleHandle, "CloseTouchInputHandle"));
if (!pFunction)
{
return FALSE;
}
return pFunction(hTouchInput);
}
static UINT lv_windows_get_dpi_for_window(
_In_ HWND WindowHandle)
{
UINT Result = (UINT)(-1);
HMODULE ModuleHandle = LoadLibraryW(L"SHCore.dll");
if (ModuleHandle)
{
typedef enum MONITOR_DPI_TYPE_PRIVATE {
MDT_EFFECTIVE_DPI = 0,
MDT_ANGULAR_DPI = 1,
MDT_RAW_DPI = 2,
MDT_DEFAULT = MDT_EFFECTIVE_DPI
} MONITOR_DPI_TYPE_PRIVATE;
typedef HRESULT(WINAPI* FunctionType)(
HMONITOR, MONITOR_DPI_TYPE_PRIVATE, UINT*, UINT*);
FunctionType pFunction = (FunctionType)(
GetProcAddress(ModuleHandle, "GetDpiForMonitor"));
if (pFunction)
{
HMONITOR MonitorHandle = MonitorFromWindow(
WindowHandle,
MONITOR_DEFAULTTONEAREST);
UINT dpiX = 0;
UINT dpiY = 0;
if (SUCCEEDED(pFunction(
MonitorHandle,
MDT_EFFECTIVE_DPI,
&dpiX,
&dpiY)))
{
Result = dpiX;
}
}
FreeLibrary(ModuleHandle);
}
if (Result == (UINT)(-1))
{
HDC hWindowDC = GetDC(WindowHandle);
if (hWindowDC)
{
Result = GetDeviceCaps(hWindowDC, LOGPIXELSX);
ReleaseDC(WindowHandle, hWindowDC);
}
}
if (Result == (UINT)(-1))
{
Result = USER_DEFAULT_SCREEN_DPI;
}
return Result;
}
static int32_t lv_windows_zoom_to_logical(int32_t physical, int32_t zoom_level)
{
return MulDiv(physical, LV_WINDOWS_ZOOM_BASE_LEVEL, zoom_level);
}
static int32_t lv_windows_zoom_to_physical(int32_t logical, int32_t zoom_level)
{
return MulDiv(logical, zoom_level, LV_WINDOWS_ZOOM_BASE_LEVEL);
}
static int32_t lv_windows_dpi_to_logical(int32_t physical, int32_t dpi)
{
return MulDiv(physical, USER_DEFAULT_SCREEN_DPI, dpi);
}
static int32_t lv_windows_dpi_to_physical(int32_t logical, int32_t dpi)
{
return MulDiv(logical, dpi, USER_DEFAULT_SCREEN_DPI);
}
static void lv_windows_display_driver_flush_callback(
lv_disp_t* disp_drv,
const lv_area_t* area,
uint8_t* px_map)
{
HWND window_handle = (HWND)lv_display_get_driver_data(disp_drv);
if (!window_handle)
{
lv_display_flush_ready(disp_drv);
return;
}
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_window_context(window_handle));
if (!context)
{
lv_display_flush_ready(disp_drv);
return;
}
if (lv_display_flush_is_last(disp_drv))
{
#if (LV_COLOR_DEPTH == 32) || \
(LV_COLOR_DEPTH == 24) || \
(LV_COLOR_DEPTH == 16)
UNREFERENCED_PARAMETER(px_map);
#else
#error [lv_windows] Unsupported LV_COLOR_DEPTH.
#endif
HDC hdc = GetDC(window_handle);
if (hdc)
{
SetStretchBltMode(hdc, HALFTONE);
RECT client_rect;
GetClientRect(window_handle, &client_rect);
int32_t width = lv_windows_zoom_to_logical(
client_rect.right - client_rect.left,
context->zoom_level);
int32_t height = lv_windows_zoom_to_logical(
client_rect.bottom - client_rect.top,
context->zoom_level);
if (context->simulator_mode)
{
width = lv_windows_dpi_to_logical(width, context->window_dpi);
height = lv_windows_dpi_to_logical(height, context->window_dpi);
}
StretchBlt(
hdc,
client_rect.left,
client_rect.top,
client_rect.right - client_rect.left,
client_rect.bottom - client_rect.top,
context->display_framebuffer_context_handle,
0,
0,
width,
height,
SRCCOPY);
ReleaseDC(window_handle, hdc);
}
}
lv_display_flush_ready(disp_drv);
}
static void lv_windows_pointer_driver_read_callback(
lv_indev_t* indev,
lv_indev_data_t* data)
{
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_display_context(lv_indev_get_disp(indev)));
if (!context)
{
return;
}
data->state = context->pointer.state;
data->point = context->pointer.point;
}
static void lv_windows_keypad_driver_read_callback(
lv_indev_t* indev,
lv_indev_data_t* data)
{
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_display_context(lv_indev_get_disp(indev)));
if (!context)
{
return;
}
EnterCriticalSection(&context->keypad.mutex);
lv_windows_keypad_queue_item_t* current = (lv_windows_keypad_queue_item_t*)(
_lv_ll_get_head(&context->keypad.queue));
if (current)
{
data->key = current->key;
data->state = current->state;
_lv_ll_remove(&context->keypad.queue, current);
lv_free(current);
data->continue_reading = true;
}
LeaveCriticalSection(&context->keypad.mutex);
}
static void lv_windows_encoder_driver_read_callback(
lv_indev_t* indev,
lv_indev_data_t* data)
{
lv_windows_window_context_t* context = (lv_windows_window_context_t*)(
lv_windows_get_display_context(lv_indev_get_disp(indev)));
if (!context)
{
return;
}
data->state = context->encoder.state;
data->enc_diff = context->encoder.enc_diff;
context->encoder.enc_diff = 0;
}