win32drv.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /**
  2. * @file win32drv.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "win32drv.h"
  9. #if USE_WIN32DRV
  10. #include <windowsx.h>
  11. #include <process.h>
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. /*********************
  15. * DEFINES
  16. *********************/
  17. #define WINDOW_EX_STYLE \
  18. WS_EX_CLIENTEDGE
  19. #define WINDOW_STYLE \
  20. (WS_OVERLAPPEDWINDOW & ~(WS_SIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME))
  21. #ifndef WIN32DRV_MONITOR_ZOOM
  22. #define WIN32DRV_MONITOR_ZOOM 1
  23. #endif
  24. #ifndef USER_DEFAULT_SCREEN_DPI
  25. #define USER_DEFAULT_SCREEN_DPI 96
  26. #endif
  27. /**********************
  28. * TYPEDEFS
  29. **********************/
  30. typedef struct _WINDOW_THREAD_PARAMETER
  31. {
  32. HANDLE window_mutex;
  33. HINSTANCE instance_handle;
  34. HICON icon_handle;
  35. lv_coord_t hor_res;
  36. lv_coord_t ver_res;
  37. int show_window_mode;
  38. } WINDOW_THREAD_PARAMETER, * PWINDOW_THREAD_PARAMETER;
  39. /**********************
  40. * STATIC PROTOTYPES
  41. **********************/
  42. /**
  43. * @brief Creates a B8G8R8A8 frame buffer.
  44. * @param WindowHandle A handle to the window for the creation of the frame
  45. * buffer. If this value is NULL, the entire screen will be
  46. * referenced.
  47. * @param Width The width of the frame buffer.
  48. * @param Height The height of the frame buffer.
  49. * @param PixelBuffer The raw pixel buffer of the frame buffer you created.
  50. * @param PixelBufferSize The size of the frame buffer you created.
  51. * @return If the function succeeds, the return value is a handle to the device
  52. * context (DC) for the frame buffer. If the function fails, the return
  53. * value is NULL, and PixelBuffer parameter is NULL.
  54. */
  55. static HDC lv_win32_create_frame_buffer(
  56. _In_opt_ HWND WindowHandle,
  57. _In_ LONG Width,
  58. _In_ LONG Height,
  59. _Out_ UINT32** PixelBuffer,
  60. _Out_ SIZE_T* PixelBufferSize);
  61. /**
  62. * @brief Enables WM_DPICHANGED message for child window for the associated
  63. * window.
  64. * @param WindowHandle The window you want to enable WM_DPICHANGED message for
  65. * child window.
  66. * @return If the function succeeds, the return value is non-zero. If the
  67. * function fails, the return value is zero.
  68. * @remarks You need to use this function in Windows 10 Threshold 1 or Windows
  69. * 10 Threshold 2.
  70. */
  71. static BOOL lv_win32_enable_child_window_dpi_message(
  72. _In_ HWND WindowHandle);
  73. /**
  74. * @brief Registers a window as being touch-capable.
  75. * @param hWnd The handle of the window being registered.
  76. * @param ulFlags A set of bit flags that specify optional modifications.
  77. * @return If the function succeeds, the return value is nonzero. If the
  78. * function fails, the return value is zero.
  79. * @remark For more information, see RegisterTouchWindow.
  80. */
  81. static BOOL lv_win32_register_touch_window(
  82. HWND hWnd,
  83. ULONG ulFlags);
  84. /**
  85. * @brief Retrieves detailed information about touch inputs associated with a
  86. * particular touch input handle.
  87. * @param hTouchInput The touch input handle received in the LPARAM of a touch
  88. * message.
  89. * @param cInputs The number of structures in the pInputs array.
  90. * @param pInputs A pointer to an array of TOUCHINPUT structures to receive
  91. * information about the touch points associated with the
  92. * specified touch input handle.
  93. * @param cbSize The size, in bytes, of a single TOUCHINPUT structure.
  94. * @return If the function succeeds, the return value is nonzero. If the
  95. * function fails, the return value is zero.
  96. * @remark For more information, see GetTouchInputInfo.
  97. */
  98. static BOOL lv_win32_get_touch_input_info(
  99. HTOUCHINPUT hTouchInput,
  100. UINT cInputs,
  101. PTOUCHINPUT pInputs,
  102. int cbSize);
  103. /**
  104. * @brief Closes a touch input handle, frees process memory associated with it,
  105. and invalidates the handle.
  106. * @param hTouchInput The touch input handle received in the LPARAM of a touch
  107. * message.
  108. * @return If the function succeeds, the return value is nonzero. If the
  109. * function fails, the return value is zero.
  110. * @remark For more information, see CloseTouchInputHandle.
  111. */
  112. static BOOL lv_win32_close_touch_input_handle(
  113. HTOUCHINPUT hTouchInput);
  114. /**
  115. * @brief Returns the dots per inch (dpi) value for the associated window.
  116. * @param WindowHandle The window you want to get information about.
  117. * @return The DPI for the window.
  118. */
  119. static UINT lv_win32_get_dpi_for_window(
  120. _In_ HWND WindowHandle);
  121. static void lv_win32_display_driver_flush_callback(
  122. lv_disp_drv_t* disp_drv,
  123. const lv_area_t* area,
  124. lv_color_t* color_p);
  125. static void lv_win32_display_refresh_handler(
  126. lv_timer_t* param);
  127. static void lv_win32_pointer_driver_read_callback(
  128. lv_indev_drv_t* indev_drv,
  129. lv_indev_data_t* data);
  130. static void lv_win32_keypad_driver_read_callback(
  131. lv_indev_drv_t* indev_drv,
  132. lv_indev_data_t* data);
  133. static void lv_win32_encoder_driver_read_callback(
  134. lv_indev_drv_t* indev_drv,
  135. lv_indev_data_t* data);
  136. static LRESULT CALLBACK lv_win32_window_message_callback(
  137. HWND hWnd,
  138. UINT uMsg,
  139. WPARAM wParam,
  140. LPARAM lParam);
  141. static unsigned int __stdcall lv_win32_window_thread_entrypoint(
  142. void* raw_parameter);
  143. /**********************
  144. * GLOBAL VARIABLES
  145. **********************/
  146. EXTERN_C bool lv_win32_quit_signal = false;
  147. EXTERN_C lv_indev_t* lv_win32_pointer_device_object = NULL;
  148. EXTERN_C lv_indev_t* lv_win32_keypad_device_object = NULL;
  149. EXTERN_C lv_indev_t* lv_win32_encoder_device_object = NULL;
  150. /**********************
  151. * STATIC VARIABLES
  152. **********************/
  153. static HWND g_window_handle = NULL;
  154. static HDC g_buffer_dc_handle = NULL;
  155. static UINT32* g_pixel_buffer = NULL;
  156. static SIZE_T g_pixel_buffer_size = 0;
  157. static lv_disp_t* g_display = NULL;
  158. static bool volatile g_display_refreshing = false;
  159. static bool volatile g_mouse_pressed = false;
  160. static LPARAM volatile g_mouse_value = 0;
  161. static bool volatile g_mousewheel_pressed = false;
  162. static int16_t volatile g_mousewheel_value = 0;
  163. static bool volatile g_keyboard_pressed = false;
  164. static WPARAM volatile g_keyboard_value = 0;
  165. static int volatile g_dpi_value = USER_DEFAULT_SCREEN_DPI;
  166. /**********************
  167. * MACROS
  168. **********************/
  169. /**********************
  170. * GLOBAL FUNCTIONS
  171. **********************/
  172. EXTERN_C void lv_win32_add_all_input_devices_to_group(
  173. lv_group_t* group)
  174. {
  175. if (!group)
  176. {
  177. LV_LOG_WARN(
  178. "The group object is NULL. Get the default group object instead.");
  179. group = lv_group_get_default();
  180. if (!group)
  181. {
  182. LV_LOG_WARN(
  183. "The default group object is NULL. Create a new group object "
  184. "and set it to default instead.");
  185. group = lv_group_create();
  186. if (group)
  187. {
  188. lv_group_set_default(group);
  189. }
  190. }
  191. }
  192. LV_ASSERT_MSG(group, "Cannot obtain an available group object.");
  193. lv_indev_set_group(lv_win32_pointer_device_object, group);
  194. lv_indev_set_group(lv_win32_keypad_device_object, group);
  195. lv_indev_set_group(lv_win32_encoder_device_object, group);
  196. }
  197. EXTERN_C bool lv_win32_init(
  198. HINSTANCE instance_handle,
  199. int show_window_mode,
  200. lv_coord_t hor_res,
  201. lv_coord_t ver_res,
  202. HICON icon_handle)
  203. {
  204. PWINDOW_THREAD_PARAMETER parameter =
  205. (PWINDOW_THREAD_PARAMETER)malloc(sizeof(WINDOW_THREAD_PARAMETER));
  206. parameter->window_mutex = CreateEventExW(NULL, NULL, 0, EVENT_ALL_ACCESS);
  207. parameter->instance_handle = instance_handle;
  208. parameter->icon_handle = icon_handle;
  209. parameter->hor_res = hor_res;
  210. parameter->ver_res = ver_res;
  211. parameter->show_window_mode = show_window_mode;
  212. _beginthreadex(
  213. NULL,
  214. 0,
  215. lv_win32_window_thread_entrypoint,
  216. parameter,
  217. 0,
  218. NULL);
  219. WaitForSingleObjectEx(parameter->window_mutex, INFINITE, FALSE);
  220. static lv_disp_draw_buf_t display_buffer;
  221. #if (LV_COLOR_DEPTH == 32) || \
  222. (LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP == 0) || \
  223. (LV_COLOR_DEPTH == 8) || \
  224. (LV_COLOR_DEPTH == 1)
  225. lv_disp_draw_buf_init(
  226. &display_buffer,
  227. (lv_color_t*)g_pixel_buffer,
  228. NULL,
  229. hor_res * ver_res);
  230. #else
  231. lv_disp_draw_buf_init(
  232. &display_buffer,
  233. (lv_color_t*)malloc(hor_res * ver_res * sizeof(lv_color_t)),
  234. NULL,
  235. hor_res * ver_res);
  236. #endif
  237. static lv_disp_drv_t display_driver;
  238. lv_disp_drv_init(&display_driver);
  239. display_driver.hor_res = hor_res;
  240. display_driver.ver_res = ver_res;
  241. display_driver.flush_cb = lv_win32_display_driver_flush_callback;
  242. display_driver.draw_buf = &display_buffer;
  243. display_driver.direct_mode = 1;
  244. g_display = lv_disp_drv_register(&display_driver);
  245. lv_timer_del(g_display->refr_timer);
  246. g_display->refr_timer = NULL;
  247. lv_timer_create(lv_win32_display_refresh_handler, 0, NULL);
  248. static lv_indev_drv_t pointer_driver;
  249. lv_indev_drv_init(&pointer_driver);
  250. pointer_driver.type = LV_INDEV_TYPE_POINTER;
  251. pointer_driver.read_cb = lv_win32_pointer_driver_read_callback;
  252. lv_win32_pointer_device_object = lv_indev_drv_register(&pointer_driver);
  253. static lv_indev_drv_t keypad_driver;
  254. lv_indev_drv_init(&keypad_driver);
  255. keypad_driver.type = LV_INDEV_TYPE_KEYPAD;
  256. keypad_driver.read_cb = lv_win32_keypad_driver_read_callback;
  257. lv_win32_keypad_device_object = lv_indev_drv_register(&keypad_driver);
  258. static lv_indev_drv_t encoder_driver;
  259. lv_indev_drv_init(&encoder_driver);
  260. encoder_driver.type = LV_INDEV_TYPE_ENCODER;
  261. encoder_driver.read_cb = lv_win32_encoder_driver_read_callback;
  262. lv_win32_encoder_device_object = lv_indev_drv_register(&encoder_driver);
  263. return true;
  264. }
  265. /**********************
  266. * STATIC FUNCTIONS
  267. **********************/
  268. static HDC lv_win32_create_frame_buffer(
  269. HWND WindowHandle,
  270. LONG Width,
  271. LONG Height,
  272. UINT32** PixelBuffer,
  273. SIZE_T* PixelBufferSize)
  274. {
  275. HDC hFrameBufferDC = NULL;
  276. if (PixelBuffer && PixelBufferSize)
  277. {
  278. HDC hWindowDC = GetDC(WindowHandle);
  279. if (hWindowDC)
  280. {
  281. hFrameBufferDC = CreateCompatibleDC(hWindowDC);
  282. ReleaseDC(WindowHandle, hWindowDC);
  283. }
  284. if (hFrameBufferDC)
  285. {
  286. #if LV_COLOR_DEPTH == 32
  287. BITMAPINFO BitmapInfo = { 0 };
  288. #elif LV_COLOR_DEPTH == 16
  289. typedef struct _BITMAPINFO_16BPP {
  290. BITMAPINFOHEADER bmiHeader;
  291. DWORD bmiColorMask[3];
  292. } BITMAPINFO_16BPP, *PBITMAPINFO_16BPP;
  293. BITMAPINFO_16BPP BitmapInfo = { 0 };
  294. #elif LV_COLOR_DEPTH == 8
  295. typedef struct _BITMAPINFO_8BPP {
  296. BITMAPINFOHEADER bmiHeader;
  297. RGBQUAD bmiColors[256];
  298. } BITMAPINFO_8BPP, *PBITMAPINFO_8BPP;
  299. BITMAPINFO_8BPP BitmapInfo = { 0 };
  300. #elif LV_COLOR_DEPTH == 1
  301. typedef struct _BITMAPINFO_1BPP {
  302. BITMAPINFOHEADER bmiHeader;
  303. RGBQUAD bmiColors[2];
  304. } BITMAPINFO_1BPP, *PBITMAPINFO_1BPP;
  305. BITMAPINFO_1BPP BitmapInfo = { 0 };
  306. #else
  307. BITMAPINFO BitmapInfo = { 0 };
  308. #endif
  309. BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  310. BitmapInfo.bmiHeader.biWidth = Width;
  311. BitmapInfo.bmiHeader.biHeight = -Height;
  312. BitmapInfo.bmiHeader.biPlanes = 1;
  313. #if LV_COLOR_DEPTH == 32
  314. BitmapInfo.bmiHeader.biBitCount = 32;
  315. BitmapInfo.bmiHeader.biCompression = BI_RGB;
  316. #elif LV_COLOR_DEPTH == 16
  317. BitmapInfo.bmiHeader.biBitCount = 16;
  318. BitmapInfo.bmiHeader.biCompression = BI_BITFIELDS;
  319. BitmapInfo.bmiColorMask[0] = 0xF800;
  320. BitmapInfo.bmiColorMask[1] = 0x07E0;
  321. BitmapInfo.bmiColorMask[2] = 0x001F;
  322. #elif LV_COLOR_DEPTH == 8
  323. BitmapInfo.bmiHeader.biBitCount = 8;
  324. BitmapInfo.bmiHeader.biCompression = BI_RGB;
  325. for (size_t i = 0; i < 256; ++i)
  326. {
  327. lv_color8_t color;
  328. color.full = i;
  329. BitmapInfo.bmiColors[i].rgbRed = LV_COLOR_GET_R(color) * 36;
  330. BitmapInfo.bmiColors[i].rgbGreen = LV_COLOR_GET_G(color) * 36;
  331. BitmapInfo.bmiColors[i].rgbBlue = LV_COLOR_GET_B(color) * 85;
  332. BitmapInfo.bmiColors[i].rgbReserved = 0xFF;
  333. }
  334. #elif LV_COLOR_DEPTH == 1
  335. BitmapInfo.bmiHeader.biBitCount = 8;
  336. BitmapInfo.bmiHeader.biCompression = BI_RGB;
  337. BitmapInfo.bmiHeader.biClrUsed = 2;
  338. BitmapInfo.bmiHeader.biClrImportant = 2;
  339. BitmapInfo.bmiColors[0].rgbRed = 0x00;
  340. BitmapInfo.bmiColors[0].rgbGreen = 0x00;
  341. BitmapInfo.bmiColors[0].rgbBlue = 0x00;
  342. BitmapInfo.bmiColors[0].rgbReserved = 0xFF;
  343. BitmapInfo.bmiColors[1].rgbRed = 0xFF;
  344. BitmapInfo.bmiColors[1].rgbGreen = 0xFF;
  345. BitmapInfo.bmiColors[1].rgbBlue = 0xFF;
  346. BitmapInfo.bmiColors[1].rgbReserved = 0xFF;
  347. #else
  348. BitmapInfo.bmiHeader.biBitCount = 32;
  349. BitmapInfo.bmiHeader.biCompression = BI_RGB;
  350. #endif
  351. HBITMAP hBitmap = CreateDIBSection(
  352. hFrameBufferDC,
  353. (PBITMAPINFO)(&BitmapInfo),
  354. DIB_RGB_COLORS,
  355. (void**)PixelBuffer,
  356. NULL,
  357. 0);
  358. if (hBitmap)
  359. {
  360. #if LV_COLOR_DEPTH == 32
  361. *PixelBufferSize = Width * Height * sizeof(UINT32);
  362. #elif LV_COLOR_DEPTH == 16
  363. *PixelBufferSize = Width * Height * sizeof(UINT16);
  364. #elif LV_COLOR_DEPTH == 8
  365. *PixelBufferSize = Width * Height * sizeof(UINT8);
  366. #elif LV_COLOR_DEPTH == 1
  367. *PixelBufferSize = Width * Height * sizeof(UINT8);
  368. #else
  369. *PixelBufferSize = Width * Height * sizeof(UINT32);
  370. #endif
  371. DeleteObject(SelectObject(hFrameBufferDC, hBitmap));
  372. DeleteObject(hBitmap);
  373. }
  374. else
  375. {
  376. DeleteDC(hFrameBufferDC);
  377. hFrameBufferDC = NULL;
  378. }
  379. }
  380. }
  381. return hFrameBufferDC;
  382. }
  383. static BOOL lv_win32_enable_child_window_dpi_message(
  384. HWND WindowHandle)
  385. {
  386. // This hack is only for Windows 10 TH1/TH2 only.
  387. // We don't need this hack if the Per Monitor Aware V2 is existed.
  388. OSVERSIONINFOEXW OSVersionInfoEx = { 0 };
  389. OSVersionInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
  390. OSVersionInfoEx.dwMajorVersion = 10;
  391. OSVersionInfoEx.dwMinorVersion = 0;
  392. OSVersionInfoEx.dwBuildNumber = 14393;
  393. if (!VerifyVersionInfoW(
  394. &OSVersionInfoEx,
  395. VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER,
  396. VerSetConditionMask(
  397. VerSetConditionMask(
  398. VerSetConditionMask(
  399. 0,
  400. VER_MAJORVERSION,
  401. VER_GREATER_EQUAL),
  402. VER_MINORVERSION,
  403. VER_GREATER_EQUAL),
  404. VER_BUILDNUMBER,
  405. VER_LESS)))
  406. {
  407. return FALSE;
  408. }
  409. HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
  410. if (!ModuleHandle)
  411. {
  412. return FALSE;
  413. }
  414. typedef BOOL(WINAPI* FunctionType)(HWND, BOOL);
  415. FunctionType pFunction = (FunctionType)(
  416. GetProcAddress(ModuleHandle, "EnableChildWindowDpiMessage"));
  417. if (!pFunction)
  418. {
  419. return FALSE;
  420. }
  421. return pFunction(WindowHandle, TRUE);
  422. }
  423. static BOOL lv_win32_register_touch_window(
  424. HWND hWnd,
  425. ULONG ulFlags)
  426. {
  427. HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
  428. if (!ModuleHandle)
  429. {
  430. return FALSE;
  431. }
  432. typedef BOOL(WINAPI* FunctionType)(HWND, ULONG);
  433. FunctionType pFunction = (FunctionType)(
  434. GetProcAddress(ModuleHandle, "RegisterTouchWindow"));
  435. if (!pFunction)
  436. {
  437. return FALSE;
  438. }
  439. return pFunction(hWnd, ulFlags);
  440. }
  441. static BOOL lv_win32_get_touch_input_info(
  442. HTOUCHINPUT hTouchInput,
  443. UINT cInputs,
  444. PTOUCHINPUT pInputs,
  445. int cbSize)
  446. {
  447. HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
  448. if (!ModuleHandle)
  449. {
  450. return FALSE;
  451. }
  452. typedef BOOL(WINAPI* FunctionType)(HTOUCHINPUT, UINT, PTOUCHINPUT, int);
  453. FunctionType pFunction = (FunctionType)(
  454. GetProcAddress(ModuleHandle, "GetTouchInputInfo"));
  455. if (!pFunction)
  456. {
  457. return FALSE;
  458. }
  459. return pFunction(hTouchInput, cInputs, pInputs, cbSize);
  460. }
  461. static BOOL lv_win32_close_touch_input_handle(
  462. HTOUCHINPUT hTouchInput)
  463. {
  464. HMODULE ModuleHandle = GetModuleHandleW(L"user32.dll");
  465. if (!ModuleHandle)
  466. {
  467. return FALSE;
  468. }
  469. typedef BOOL(WINAPI* FunctionType)(HTOUCHINPUT);
  470. FunctionType pFunction = (FunctionType)(
  471. GetProcAddress(ModuleHandle, "CloseTouchInputHandle"));
  472. if (!pFunction)
  473. {
  474. return FALSE;
  475. }
  476. return pFunction(hTouchInput);
  477. }
  478. static UINT lv_win32_get_dpi_for_window(
  479. _In_ HWND WindowHandle)
  480. {
  481. UINT Result = (UINT)(-1);
  482. HMODULE ModuleHandle = LoadLibraryW(L"SHCore.dll");
  483. if (ModuleHandle)
  484. {
  485. typedef enum MONITOR_DPI_TYPE_PRIVATE {
  486. MDT_EFFECTIVE_DPI = 0,
  487. MDT_ANGULAR_DPI = 1,
  488. MDT_RAW_DPI = 2,
  489. MDT_DEFAULT = MDT_EFFECTIVE_DPI
  490. } MONITOR_DPI_TYPE_PRIVATE;
  491. typedef HRESULT(WINAPI* FunctionType)(
  492. HMONITOR, MONITOR_DPI_TYPE_PRIVATE, UINT*, UINT*);
  493. FunctionType pFunction = (FunctionType)(
  494. GetProcAddress(ModuleHandle, "GetDpiForMonitor"));
  495. if (pFunction)
  496. {
  497. HMONITOR MonitorHandle = MonitorFromWindow(
  498. WindowHandle,
  499. MONITOR_DEFAULTTONEAREST);
  500. UINT dpiX = 0;
  501. UINT dpiY = 0;
  502. if (SUCCEEDED(pFunction(
  503. MonitorHandle,
  504. MDT_EFFECTIVE_DPI,
  505. &dpiX,
  506. &dpiY)))
  507. {
  508. Result = dpiX;
  509. }
  510. }
  511. FreeLibrary(ModuleHandle);
  512. }
  513. if (Result == (UINT)(-1))
  514. {
  515. HDC hWindowDC = GetDC(WindowHandle);
  516. if (hWindowDC)
  517. {
  518. Result = GetDeviceCaps(hWindowDC, LOGPIXELSX);
  519. ReleaseDC(WindowHandle, hWindowDC);
  520. }
  521. }
  522. if (Result == (UINT)(-1))
  523. {
  524. Result = USER_DEFAULT_SCREEN_DPI;
  525. }
  526. return Result;
  527. }
  528. static void lv_win32_display_driver_flush_callback(
  529. lv_disp_drv_t* disp_drv,
  530. const lv_area_t* area,
  531. lv_color_t* color_p)
  532. {
  533. if (lv_disp_flush_is_last(disp_drv))
  534. {
  535. #if (LV_COLOR_DEPTH == 32) || \
  536. (LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP == 0) || \
  537. (LV_COLOR_DEPTH == 8) || \
  538. (LV_COLOR_DEPTH == 1)
  539. UNREFERENCED_PARAMETER(color_p);
  540. #elif (LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP != 0)
  541. SIZE_T count = g_pixel_buffer_size / sizeof(UINT16);
  542. PUINT16 source = (PUINT16)color_p;
  543. PUINT16 destination = (PUINT16)g_pixel_buffer;
  544. for (SIZE_T i = 0; i < count; ++i)
  545. {
  546. UINT16 current = *source;
  547. *destination = (LOBYTE(current) << 8) | HIBYTE(current);
  548. ++source;
  549. ++destination;
  550. }
  551. #else
  552. for (int y = area->y1; y <= area->y2; ++y)
  553. {
  554. for (int x = area->x1; x <= area->x2; ++x)
  555. {
  556. g_pixel_buffer[y * disp_drv->hor_res + x] =
  557. lv_color_to32(*color_p);
  558. color_p++;
  559. }
  560. }
  561. #endif
  562. InvalidateRect(g_window_handle, NULL, FALSE);
  563. }
  564. lv_disp_flush_ready(disp_drv);
  565. }
  566. static void lv_win32_display_refresh_handler(
  567. lv_timer_t* param)
  568. {
  569. UNREFERENCED_PARAMETER(param);
  570. if (!g_display_refreshing)
  571. {
  572. _lv_disp_refr_timer(NULL);
  573. }
  574. }
  575. static void lv_win32_pointer_driver_read_callback(
  576. lv_indev_drv_t* indev_drv,
  577. lv_indev_data_t* data)
  578. {
  579. UNREFERENCED_PARAMETER(indev_drv);
  580. data->state = (lv_indev_state_t)(
  581. g_mouse_pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL);
  582. data->point.x = MulDiv(
  583. GET_X_LPARAM(g_mouse_value),
  584. USER_DEFAULT_SCREEN_DPI,
  585. WIN32DRV_MONITOR_ZOOM * g_dpi_value);
  586. data->point.y = MulDiv(
  587. GET_Y_LPARAM(g_mouse_value),
  588. USER_DEFAULT_SCREEN_DPI,
  589. WIN32DRV_MONITOR_ZOOM * g_dpi_value);
  590. if (data->point.x < 0)
  591. {
  592. data->point.x = 0;
  593. }
  594. if (data->point.x > g_display->driver->hor_res - 1)
  595. {
  596. data->point.x = g_display->driver->hor_res - 1;
  597. }
  598. if (data->point.y < 0)
  599. {
  600. data->point.y = 0;
  601. }
  602. if (data->point.y > g_display->driver->ver_res - 1)
  603. {
  604. data->point.y = g_display->driver->ver_res - 1;
  605. }
  606. }
  607. static void lv_win32_keypad_driver_read_callback(
  608. lv_indev_drv_t* indev_drv,
  609. lv_indev_data_t* data)
  610. {
  611. UNREFERENCED_PARAMETER(indev_drv);
  612. data->state = (lv_indev_state_t)(
  613. g_keyboard_pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL);
  614. WPARAM KeyboardValue = g_keyboard_value;
  615. switch (KeyboardValue)
  616. {
  617. case VK_UP:
  618. data->key = LV_KEY_UP;
  619. break;
  620. case VK_DOWN:
  621. data->key = LV_KEY_DOWN;
  622. break;
  623. case VK_LEFT:
  624. data->key = LV_KEY_LEFT;
  625. break;
  626. case VK_RIGHT:
  627. data->key = LV_KEY_RIGHT;
  628. break;
  629. case VK_ESCAPE:
  630. data->key = LV_KEY_ESC;
  631. break;
  632. case VK_DELETE:
  633. data->key = LV_KEY_DEL;
  634. break;
  635. case VK_BACK:
  636. data->key = LV_KEY_BACKSPACE;
  637. break;
  638. case VK_RETURN:
  639. data->key = LV_KEY_ENTER;
  640. break;
  641. case VK_NEXT:
  642. data->key = LV_KEY_NEXT;
  643. break;
  644. case VK_PRIOR:
  645. data->key = LV_KEY_PREV;
  646. break;
  647. case VK_HOME:
  648. data->key = LV_KEY_HOME;
  649. break;
  650. case VK_END:
  651. data->key = LV_KEY_END;
  652. break;
  653. default:
  654. if (KeyboardValue >= 'A' && KeyboardValue <= 'Z')
  655. {
  656. KeyboardValue += 0x20;
  657. }
  658. data->key = (uint32_t)KeyboardValue;
  659. break;
  660. }
  661. }
  662. static void lv_win32_encoder_driver_read_callback(
  663. lv_indev_drv_t* indev_drv,
  664. lv_indev_data_t* data)
  665. {
  666. UNREFERENCED_PARAMETER(indev_drv);
  667. data->state = (lv_indev_state_t)(
  668. g_mousewheel_pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL);
  669. data->enc_diff = g_mousewheel_value;
  670. g_mousewheel_value = 0;
  671. }
  672. static LRESULT CALLBACK lv_win32_window_message_callback(
  673. HWND hWnd,
  674. UINT uMsg,
  675. WPARAM wParam,
  676. LPARAM lParam)
  677. {
  678. switch (uMsg)
  679. {
  680. case WM_MOUSEMOVE:
  681. case WM_LBUTTONDOWN:
  682. case WM_LBUTTONUP:
  683. case WM_MBUTTONDOWN:
  684. case WM_MBUTTONUP:
  685. {
  686. g_mouse_value = lParam;
  687. if (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP)
  688. {
  689. g_mouse_pressed = (uMsg == WM_LBUTTONDOWN);
  690. }
  691. else if (uMsg == WM_MBUTTONDOWN || uMsg == WM_MBUTTONUP)
  692. {
  693. g_mousewheel_pressed = (uMsg == WM_MBUTTONDOWN);
  694. }
  695. return 0;
  696. }
  697. case WM_KEYDOWN:
  698. case WM_KEYUP:
  699. {
  700. g_keyboard_pressed = (uMsg == WM_KEYDOWN);
  701. g_keyboard_value = wParam;
  702. break;
  703. }
  704. case WM_MOUSEWHEEL:
  705. {
  706. g_mousewheel_value = -(GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA);
  707. break;
  708. }
  709. case WM_TOUCH:
  710. {
  711. UINT cInputs = LOWORD(wParam);
  712. HTOUCHINPUT hTouchInput = (HTOUCHINPUT)(lParam);
  713. PTOUCHINPUT pInputs = malloc(cInputs * sizeof(TOUCHINPUT));
  714. if (pInputs)
  715. {
  716. if (lv_win32_get_touch_input_info(
  717. hTouchInput,
  718. cInputs,
  719. pInputs,
  720. sizeof(TOUCHINPUT)))
  721. {
  722. for (UINT i = 0; i < cInputs; ++i)
  723. {
  724. POINT Point;
  725. Point.x = TOUCH_COORD_TO_PIXEL(pInputs[i].x);
  726. Point.y = TOUCH_COORD_TO_PIXEL(pInputs[i].y);
  727. if (!ScreenToClient(hWnd, &Point))
  728. {
  729. continue;
  730. }
  731. uint16_t x = (uint16_t)(Point.x & 0xffff);
  732. uint16_t y = (uint16_t)(Point.y & 0xffff);
  733. DWORD MousePressedMask =
  734. TOUCHEVENTF_MOVE | TOUCHEVENTF_DOWN;
  735. g_mouse_value = (y << 16) | x;
  736. g_mouse_pressed = (pInputs[i].dwFlags & MousePressedMask);
  737. }
  738. }
  739. free(pInputs);
  740. }
  741. lv_win32_close_touch_input_handle(hTouchInput);
  742. break;
  743. }
  744. case WM_DPICHANGED:
  745. {
  746. g_dpi_value = HIWORD(wParam);
  747. LPRECT SuggestedRect = (LPRECT)lParam;
  748. SetWindowPos(
  749. hWnd,
  750. NULL,
  751. SuggestedRect->left,
  752. SuggestedRect->top,
  753. SuggestedRect->right,
  754. SuggestedRect->bottom,
  755. SWP_NOZORDER | SWP_NOACTIVATE);
  756. RECT ClientRect;
  757. GetClientRect(hWnd, &ClientRect);
  758. int WindowWidth = MulDiv(
  759. g_display->driver->hor_res * WIN32DRV_MONITOR_ZOOM,
  760. g_dpi_value,
  761. USER_DEFAULT_SCREEN_DPI);
  762. int WindowHeight = MulDiv(
  763. g_display->driver->ver_res * WIN32DRV_MONITOR_ZOOM,
  764. g_dpi_value,
  765. USER_DEFAULT_SCREEN_DPI);
  766. SetWindowPos(
  767. hWnd,
  768. NULL,
  769. SuggestedRect->left,
  770. SuggestedRect->top,
  771. SuggestedRect->right + (WindowWidth - ClientRect.right),
  772. SuggestedRect->bottom + (WindowHeight - ClientRect.bottom),
  773. SWP_NOZORDER | SWP_NOACTIVATE);
  774. break;
  775. }
  776. case WM_PAINT:
  777. {
  778. g_display_refreshing = true;
  779. PAINTSTRUCT ps;
  780. HDC hdc = BeginPaint(hWnd, &ps);
  781. if (g_display)
  782. {
  783. SetStretchBltMode(hdc, HALFTONE);
  784. StretchBlt(
  785. hdc,
  786. ps.rcPaint.left,
  787. ps.rcPaint.top,
  788. ps.rcPaint.right - ps.rcPaint.left,
  789. ps.rcPaint.bottom - ps.rcPaint.top,
  790. g_buffer_dc_handle,
  791. 0,
  792. 0,
  793. MulDiv(
  794. ps.rcPaint.right - ps.rcPaint.left,
  795. USER_DEFAULT_SCREEN_DPI,
  796. WIN32DRV_MONITOR_ZOOM * g_dpi_value),
  797. MulDiv(
  798. ps.rcPaint.bottom - ps.rcPaint.top,
  799. USER_DEFAULT_SCREEN_DPI,
  800. WIN32DRV_MONITOR_ZOOM * g_dpi_value),
  801. SRCCOPY);
  802. }
  803. EndPaint(hWnd, &ps);
  804. g_display_refreshing = false;
  805. break;
  806. }
  807. case WM_DESTROY:
  808. PostQuitMessage(0);
  809. break;
  810. default:
  811. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  812. }
  813. return 0;
  814. }
  815. static unsigned int __stdcall lv_win32_window_thread_entrypoint(
  816. void* raw_parameter)
  817. {
  818. PWINDOW_THREAD_PARAMETER parameter =
  819. (PWINDOW_THREAD_PARAMETER)raw_parameter;
  820. WNDCLASSEXW window_class;
  821. window_class.cbSize = sizeof(WNDCLASSEXW);
  822. window_class.style = 0;
  823. window_class.lpfnWndProc = lv_win32_window_message_callback;
  824. window_class.cbClsExtra = 0;
  825. window_class.cbWndExtra = 0;
  826. window_class.hInstance = parameter->instance_handle;
  827. window_class.hIcon = parameter->icon_handle;
  828. window_class.hCursor = LoadCursorW(NULL, IDC_ARROW);
  829. window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  830. window_class.lpszMenuName = NULL;
  831. window_class.lpszClassName = L"lv_sim_visual_studio";
  832. window_class.hIconSm = parameter->icon_handle;
  833. if (!RegisterClassExW(&window_class))
  834. {
  835. return 0;
  836. }
  837. HWND window_handle = CreateWindowExW(
  838. WINDOW_EX_STYLE,
  839. window_class.lpszClassName,
  840. L"LVGL Simulator for Windows Desktop",
  841. WINDOW_STYLE,
  842. CW_USEDEFAULT,
  843. 0,
  844. CW_USEDEFAULT,
  845. 0,
  846. NULL,
  847. NULL,
  848. parameter->instance_handle,
  849. NULL);
  850. if (!window_handle)
  851. {
  852. return 0;
  853. }
  854. g_dpi_value = lv_win32_get_dpi_for_window(window_handle);
  855. RECT window_size;
  856. window_size.left = 0;
  857. window_size.right = MulDiv(
  858. parameter->hor_res * WIN32DRV_MONITOR_ZOOM,
  859. g_dpi_value,
  860. USER_DEFAULT_SCREEN_DPI);
  861. window_size.top = 0;
  862. window_size.bottom = MulDiv(
  863. parameter->ver_res * WIN32DRV_MONITOR_ZOOM,
  864. g_dpi_value,
  865. USER_DEFAULT_SCREEN_DPI);
  866. AdjustWindowRectEx(
  867. &window_size,
  868. WINDOW_STYLE,
  869. FALSE,
  870. WINDOW_EX_STYLE);
  871. OffsetRect(
  872. &window_size,
  873. -window_size.left,
  874. -window_size.top);
  875. SetWindowPos(
  876. window_handle,
  877. NULL,
  878. 0,
  879. 0,
  880. window_size.right,
  881. window_size.bottom,
  882. SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE);
  883. lv_win32_register_touch_window(window_handle, 0);
  884. lv_win32_enable_child_window_dpi_message(window_handle);
  885. HDC hNewBufferDC = lv_win32_create_frame_buffer(
  886. window_handle,
  887. parameter->hor_res,
  888. parameter->ver_res,
  889. &g_pixel_buffer,
  890. &g_pixel_buffer_size);
  891. DeleteDC(g_buffer_dc_handle);
  892. g_buffer_dc_handle = hNewBufferDC;
  893. ShowWindow(window_handle, parameter->show_window_mode);
  894. UpdateWindow(window_handle);
  895. g_window_handle = window_handle;
  896. SetEvent(parameter->window_mutex);
  897. MSG message;
  898. while (GetMessageW(&message, NULL, 0, 0))
  899. {
  900. TranslateMessage(&message);
  901. DispatchMessageW(&message);
  902. }
  903. lv_win32_quit_signal = true;
  904. return 0;
  905. }
  906. #endif /*USE_WIN32DRV*/