window.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. #define HL_NAME(n) directx_##n
  2. #include <hl.h>
  3. #define MAX_EVENTS 1024
  4. typedef enum {
  5. Quit = 0,
  6. MouseMove = 1,
  7. MouseLeave = 2,
  8. MouseDown = 3,
  9. MouseUp = 4,
  10. MouseWheel = 5,
  11. WindowState = 6,
  12. KeyDown = 7,
  13. KeyUp = 8,
  14. TextInput = 9,
  15. DropStart = 10,
  16. DropFile = 11,
  17. DropEnd = 12,
  18. KeyMapChanged = 13,
  19. } EventType;
  20. typedef enum {
  21. Show = 0,
  22. Hide = 1,
  23. Expose = 2,
  24. Move = 3,
  25. Resize = 4,
  26. Minimize= 5,
  27. Maximize= 6,
  28. Restore = 7,
  29. Enter = 8,
  30. Leave = 9,
  31. Focus = 10,
  32. Blur = 11,
  33. Close = 12
  34. } WindowStateChange;
  35. typedef enum {
  36. Hidden = 0x000001,
  37. Resizable = 0x000002
  38. } WindowFlags;
  39. typedef struct {
  40. hl_type *t;
  41. EventType type;
  42. int mouseX;
  43. int mouseY;
  44. int mouseXRel;
  45. int mouseYRel;
  46. int button;
  47. int wheelDelta;
  48. WindowStateChange state;
  49. int keyCode;
  50. int scanCode;
  51. bool keyRepeat;
  52. int controller;
  53. int value;
  54. vbyte* dropFile;
  55. } dx_event;
  56. typedef struct {
  57. dx_event events[MAX_EVENTS];
  58. DWORD normal_style;
  59. double opacity;
  60. int min_width;
  61. int min_height;
  62. int max_width;
  63. int max_height;
  64. int event_count;
  65. int next_event;
  66. bool is_over;
  67. bool is_focused;
  68. } dx_events;
  69. typedef struct HWND__ dx_window;
  70. static dx_window *cur_clip_cursor_window = NULL;
  71. static DWORD capture_refresh_time = 0;
  72. typedef enum {
  73. SkipUpdate = 1,
  74. InTitleClick = 2,
  75. LButton = 4,
  76. RButton = 8,
  77. MButton = 16,
  78. XButton1 = 32,
  79. XButton2 = 64,
  80. NotForeground = 128
  81. } mouse_capture_flags;
  82. static int disable_capture = 0;
  83. static bool capture_mouse = false;
  84. static bool relative_mouse = false;
  85. typedef HCURSOR dx_cursor;
  86. static dx_cursor cur_cursor = NULL;
  87. static bool show_cursor = true;
  88. #define CURSOR_VISIBLE show_cursor && !relative_mouse
  89. static dx_events *get_events(HWND wnd) {
  90. return (dx_events*)GetWindowLongPtr(wnd,GWLP_USERDATA);
  91. }
  92. static void updateClipCursor(HWND wnd) {
  93. if ( disable_capture ) {
  94. ClipCursor(NULL);
  95. capture_refresh_time = GetTickCount();
  96. return;
  97. }
  98. if ( !capture_mouse && !relative_mouse ) {
  99. cur_clip_cursor_window = NULL;
  100. ClipCursor(NULL);
  101. } else {
  102. cur_clip_cursor_window = wnd;
  103. RECT rect;
  104. if ( GetClientRect(wnd, &rect) && !IsRectEmpty(&rect) ) {
  105. ClientToScreen(wnd, (LPPOINT)&rect.left);
  106. ClientToScreen(wnd, (LPPOINT)&rect.right);
  107. ClipCursor(&rect);
  108. }
  109. }
  110. capture_refresh_time = GetTickCount();
  111. }
  112. static void checkCaptureFlags( HWND wnd ) {
  113. if ( !(GetAsyncKeyState(VK_LBUTTON) & 0x8000) )
  114. disable_capture &= ~LButton;
  115. if ( !(GetAsyncKeyState(VK_RBUTTON) & 0x8000) )
  116. disable_capture &= ~RButton;
  117. if ( !(GetAsyncKeyState(VK_MBUTTON) & 0x8000) )
  118. disable_capture &= ~MButton;
  119. if ( !(GetAsyncKeyState(VK_XBUTTON1) & 0x8000) )
  120. disable_capture &= ~XButton1;
  121. if ( !(GetAsyncKeyState(VK_XBUTTON2) & 0x8000) )
  122. disable_capture &= ~XButton2;
  123. }
  124. static bool setRelativeMode( HWND wnd, bool enabled ) {
  125. RAWINPUTDEVICE mouse = { 0x01, 0x02, 0, NULL }; /* Mouse: UsagePage = 1, Usage = 2 */
  126. if ( relative_mouse == enabled ) return true;
  127. if ( !enabled ) {
  128. mouse.dwFlags |= RIDEV_REMOVE;
  129. if ( show_cursor ) SetCursor(cur_cursor);
  130. } else {
  131. SetCursor(NULL);
  132. }
  133. relative_mouse = enabled;
  134. updateClipCursor(wnd);
  135. return RegisterRawInputDevices(&mouse, 1, sizeof(RAWINPUTDEVICE)) || !enabled;
  136. }
  137. static dx_event *addEvent( HWND wnd, EventType type ) {
  138. dx_events *buf = get_events(wnd);
  139. dx_event *e;
  140. if( buf->event_count == MAX_EVENTS )
  141. e = &buf->events[MAX_EVENTS-1];
  142. else
  143. e = &buf->events[buf->event_count++];
  144. e->type = type;
  145. return e;
  146. }
  147. #define addMouse(etype,but) { \
  148. e = addEvent(wnd,etype); \
  149. e->button = but; \
  150. e->mouseX = (short)LOWORD(lparam); \
  151. e->mouseY = (short)HIWORD(lparam); \
  152. }
  153. #define addState(wstate) { e = addEvent(wnd,WindowState); e->state = wstate; }
  154. static bool shift_downs[] = { false, false };
  155. static LRESULT CALLBACK WndProc( HWND wnd, UINT umsg, WPARAM wparam, LPARAM lparam ) {
  156. dx_event *e = NULL;
  157. switch(umsg) {
  158. case WM_DESTROY:
  159. PostQuitMessage(0);
  160. return 0;
  161. case WM_CREATE:
  162. SetWindowLongPtr(wnd,GWLP_USERDATA,(LONG_PTR)((CREATESTRUCT*)lparam)->lpCreateParams);
  163. break;
  164. case WM_SIZE:
  165. {
  166. dx_events *buf = get_events(wnd);
  167. if( buf->event_count > buf->next_event && buf->events[buf->event_count-1].type == WindowState && buf->events[buf->event_count-1].state == Resize )
  168. buf->event_count--;
  169. }
  170. addState(Resize);
  171. break;
  172. case WM_LBUTTONDOWN: addMouse(MouseDown,1); break;
  173. case WM_LBUTTONUP: addMouse(MouseUp,1); break;
  174. case WM_MBUTTONDOWN: addMouse(MouseDown,2); break;
  175. case WM_MBUTTONUP: addMouse(MouseUp,2); break;
  176. case WM_RBUTTONDOWN: addMouse(MouseDown,3); break;
  177. case WM_RBUTTONUP: addMouse(MouseUp,3); break;
  178. case WM_XBUTTONDOWN: addMouse(MouseDown,3 + HIWORD(wparam)); break;
  179. case WM_XBUTTONUP: addMouse(MouseUp,3 + HIWORD(wparam)); break;
  180. case WM_MOUSEWHEEL: addMouse(MouseWheel,0); e->wheelDelta = ((int)(short)HIWORD(wparam)) / 120; break;
  181. case WM_SYSCOMMAND:
  182. if( wparam == SC_KEYMENU && (lparam>>16) <= 0 )
  183. return 0;
  184. break;
  185. case WM_MOUSEMOVE:
  186. {
  187. dx_events *evt = get_events(wnd);
  188. if( !evt->is_over ) {
  189. TRACKMOUSEEVENT ev;
  190. ev.cbSize = sizeof(ev);
  191. ev.dwFlags = TME_LEAVE;
  192. ev.hwndTrack = wnd;
  193. TrackMouseEvent(&ev);
  194. evt->is_over = true;
  195. addState(Enter);
  196. }
  197. if ( !relative_mouse )
  198. addMouse(MouseMove, 0);
  199. }
  200. break;
  201. case WM_INPUT:
  202. {
  203. dx_events* evt = get_events(wnd);
  204. if ( evt->is_focused ) {
  205. // Only handle raw input when window is active
  206. HRAWINPUT hRawInput = (HRAWINPUT)lparam;
  207. RAWINPUT inp;
  208. UINT size = sizeof(inp);
  209. GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
  210. // Ignore pseudo-movement from touch events.
  211. if ( inp.header.dwType == RIM_TYPEMOUSE ) {
  212. RAWMOUSE* mouse = &inp.data.mouse;
  213. if ( (mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE ) {
  214. if ( mouse->lLastX != 0 || mouse->lLastY != 0 ) {
  215. e = addEvent(wnd, MouseMove);
  216. e->mouseX = 0;
  217. e->mouseY = 0;
  218. e->button = 0;
  219. e->mouseXRel = mouse->lLastX;
  220. e->mouseYRel = mouse->lLastY;
  221. }
  222. } else if ( mouse->lLastX || mouse->lLastY ) {
  223. // Absolute movement - simulate relative movement
  224. static POINT lastMousePos;
  225. bool virtual_desktop = mouse->usFlags & MOUSE_VIRTUAL_DESKTOP;
  226. int w = GetSystemMetrics(virtual_desktop ? SM_CXVIRTUALSCREEN : SM_CXSCREEN);
  227. int h = GetSystemMetrics(virtual_desktop ? SM_CYVIRTUALSCREEN : SM_CYSCREEN);
  228. int x = (int)(((float)mouse->lLastX / 65535.0f) * w);
  229. int y = (int)(((float)mouse->lLastY / 65535.0f) * h);
  230. if (lastMousePos.x == 0 && lastMousePos.y == 0) {
  231. lastMousePos.x = x;
  232. lastMousePos.y = y;
  233. } else {
  234. e = addEvent(wnd, MouseMove);
  235. e->mouseX = 0;
  236. e->mouseY = 0;
  237. e->button = 0;
  238. e->mouseXRel = x - lastMousePos.x;
  239. e->mouseYRel = y - lastMousePos.y;
  240. lastMousePos.x = x;
  241. lastMousePos.y = y;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. break;
  248. case WM_MOUSELEAVE:
  249. addState(Leave);
  250. get_events(wnd)->is_over = false;
  251. break;
  252. case WM_KEYDOWN:
  253. case WM_SYSKEYDOWN:
  254. case WM_KEYUP:
  255. case WM_SYSKEYUP:
  256. // right alt has triggered a control !
  257. if( wparam == VK_MENU && lparam & (1<<24) ) {
  258. dx_events *buf = get_events(wnd);
  259. if( buf->event_count > buf->next_event && buf->events[buf->event_count-1].type == (umsg == WM_KEYUP ? KeyUp : KeyDown) && buf->events[buf->event_count-1].keyCode == (VK_CONTROL|256) ) {
  260. buf->event_count--;
  261. //printf("CANCEL\n");
  262. }
  263. }
  264. bool repeat = (umsg == WM_KEYDOWN || umsg == WM_SYSKEYDOWN) && (lparam & 0x40000000) != 0;
  265. // see
  266. e = addEvent(wnd,(umsg == WM_KEYUP || umsg == WM_SYSKEYUP) ? KeyUp : KeyDown);
  267. e->keyCode = (int)wparam;
  268. e->scanCode = (lparam >> 16) & 0xFF;
  269. e->keyRepeat = repeat;
  270. // L/R location
  271. if( e->keyCode == VK_SHIFT ) {
  272. bool right = MapVirtualKey((lparam >> 16) & 0xFF, MAPVK_VSC_TO_VK_EX) == VK_RSHIFT;
  273. e->keyCode |= right ? 512 : 256;
  274. e->keyRepeat = false;
  275. shift_downs[right?1:0] = e->type == KeyDown;
  276. }
  277. if( e->keyCode == VK_SHIFT || e->keyCode == VK_CONTROL || e->keyCode == VK_MENU )
  278. e->keyCode |= (lparam & (1<<24)) ? 512 : 256;
  279. if( e->keyCode == 13 && (lparam & 0x1000000) )
  280. e->keyCode = 108; // numpad enter
  281. //printf("%.8X - %d[%s]%s%s\n",lparam,e->keyCode&255,e->type == KeyUp ? "UP":"DOWN",e->keyRepeat?" REPEAT":"",(e->keyCode&256) ? " LEFT" : (e->keyCode & 512) ? " RIGHT" : "");
  282. if( (e->keyCode & 0xFF) == VK_MENU )
  283. return 0;
  284. break;
  285. case WM_TIMER:
  286. // bugfix for shifts being considered as a single key (one single WM_KEYUP is received when both are down)
  287. if( shift_downs[0] && GetKeyState(VK_LSHIFT) >= 0 ) {
  288. //printf("LSHIFT RELEASED\n");
  289. shift_downs[0] = false;
  290. e = addEvent(wnd,KeyUp);
  291. e->keyCode = VK_SHIFT | 256;
  292. }
  293. if( shift_downs[1] && GetKeyState(VK_RSHIFT) >= 0 ) {
  294. //printf("RSHIFT RELEASED\n");
  295. shift_downs[1] = false;
  296. e = addEvent(wnd,KeyUp);
  297. e->keyCode = VK_SHIFT | 512;
  298. }
  299. if ( ( capture_mouse || relative_mouse ) && get_events(wnd)->is_focused ) {
  300. // Refresh the cursor capture every 3s in case some app hijacks it.
  301. if ( disable_capture ) {
  302. checkCaptureFlags(wnd);
  303. if (!disable_capture || disable_capture == SkipUpdate) {
  304. disable_capture = 0;
  305. updateClipCursor(wnd);
  306. }
  307. } else if ( GetTickCount() - capture_refresh_time >= 3000 ) {
  308. disable_capture &= ~SkipUpdate;
  309. updateClipCursor(wnd);
  310. }
  311. }
  312. break;
  313. case WM_CHAR:
  314. e = addEvent(wnd,TextInput);
  315. e->keyCode = (int)wparam;
  316. e->keyRepeat = (lparam & 0xFFFF) != 0;
  317. break;
  318. case WM_NCACTIVATE:
  319. // Allow user to interact with the titlebar without clipping.
  320. disable_capture |= SkipUpdate;
  321. ClipCursor(NULL);
  322. break;
  323. case WM_ACTIVATE:
  324. // HIWORD(wparam) = minimized flag
  325. if (!(bool)HIWORD(wparam) && LOWORD(wparam) != WA_INACTIVE) {
  326. if ( LOWORD(wparam) == WA_CLICKACTIVE ) {
  327. if ( GetAsyncKeyState(VK_LBUTTON) )
  328. disable_capture |= LButton;
  329. if ( GetAsyncKeyState(VK_RBUTTON) )
  330. disable_capture |= RButton;
  331. if ( GetAsyncKeyState(VK_MBUTTON) )
  332. disable_capture |= MButton;
  333. if ( GetAsyncKeyState(VK_XBUTTON1) )
  334. disable_capture |= XButton1;
  335. if ( GetAsyncKeyState(VK_XBUTTON2) )
  336. disable_capture |= XButton2;
  337. }
  338. checkCaptureFlags(wnd);
  339. updateClipCursor(wnd);
  340. } else {
  341. ClipCursor(NULL);
  342. }
  343. break;
  344. case WM_NCLBUTTONDOWN:
  345. disable_capture |= InTitleClick;
  346. ClipCursor(NULL);
  347. break;
  348. case WM_CAPTURECHANGED:
  349. disable_capture &= ~InTitleClick;
  350. checkCaptureFlags(wnd);
  351. updateClipCursor(wnd);
  352. break;
  353. case WM_SETFOCUS:
  354. get_events(wnd)->is_focused = true;
  355. updateClipCursor(wnd);
  356. addState(Focus);
  357. break;
  358. case WM_KILLFOCUS:
  359. shift_downs[0] = false;
  360. shift_downs[1] = false;
  361. if ( capture_mouse || relative_mouse ) ClipCursor(NULL);
  362. get_events(wnd)->is_focused = false;
  363. addState(Blur);
  364. break;
  365. case WM_WINDOWPOSCHANGED:
  366. {
  367. HWND wndFg = GetForegroundWindow();
  368. if( wndFg != wnd ) {
  369. disable_capture |= NotForeground;
  370. } else {
  371. disable_capture &= ~NotForeground;
  372. }
  373. updateClipCursor(wnd);
  374. break;
  375. }
  376. case WM_GETMINMAXINFO:
  377. {
  378. dx_events *buf = get_events(wnd);
  379. if( buf ) {
  380. long resizable_flags = (WS_MAXIMIZEBOX | WS_THICKFRAME);
  381. if( (buf->normal_style & resizable_flags) == resizable_flags ) {
  382. if( buf->min_width != 0 || buf->min_height != 0 ||
  383. buf->max_width != 0 || buf->max_height != 0 ) {
  384. MINMAXINFO *info = (MINMAXINFO*)lparam;
  385. RECT r;
  386. GetClientRect(wnd, &r);
  387. int curr_width = r.right;
  388. int curr_height = r.bottom;
  389. // remove curr_width/height which contain non-client dimensions
  390. bool apply_max = false;
  391. int min_width = buf->min_width - curr_width;
  392. int min_height = buf->min_height - curr_height;
  393. int max_width = buf->max_width;
  394. int max_height = buf->max_height;
  395. if( max_width && max_height ) {
  396. max_width -= curr_width;
  397. max_height -= curr_height;
  398. apply_max = true;
  399. }
  400. // fix curr_width/height to contain only client size
  401. {
  402. RECT size;
  403. LONG style = GetWindowLong(wnd, GWL_STYLE);
  404. BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(wnd) != NULL);
  405. size.top = 0;
  406. size.left = 0;
  407. size.bottom = curr_height;
  408. size.right = curr_width;
  409. AdjustWindowRectEx(&size, style, menu, 0);
  410. curr_width = size.right - size.left;
  411. curr_height = size.bottom - size.top;
  412. }
  413. // apply curr_width/height without client size
  414. info->ptMinTrackSize.x = min_width + curr_width;
  415. info->ptMinTrackSize.y = min_height + curr_height;
  416. if( apply_max ) {
  417. info->ptMaxTrackSize.x = max_width + curr_width;
  418. info->ptMaxTrackSize.y = max_height + curr_height;
  419. }
  420. return 0;
  421. }
  422. }
  423. }
  424. break;
  425. }
  426. case WM_SETCURSOR:
  427. if( LOWORD(lparam) == HTCLIENT ) {
  428. if( CURSOR_VISIBLE )
  429. SetCursor(cur_cursor != NULL ? cur_cursor : LoadCursor(NULL, IDC_ARROW));
  430. else
  431. SetCursor(NULL);
  432. return TRUE;
  433. }
  434. break;
  435. case WM_DROPFILES:
  436. {
  437. HDROP drop = (HDROP)wparam;
  438. UINT count = DragQueryFileW(drop, 0xFFFFFFFF, NULL, 0);
  439. POINT dragPoint;
  440. if ( !DragQueryPoint(drop, &dragPoint) ) {
  441. dragPoint.x = 0L;
  442. dragPoint.y = 0L;
  443. }
  444. e = addEvent(wnd, DropStart);
  445. e->value = count;
  446. e->mouseX = (int)dragPoint.x;
  447. e->mouseY = (int)dragPoint.y;
  448. for ( UINT i = 0; i < count; i++ ) {
  449. UINT size = DragQueryFileW(drop, i, NULL, 0) + 1; // + zero terminator
  450. // We have to make a temporary unmanaged buffer copy due to async nature of event being
  451. // processed and collected by Haxe event loop, and using GC-allocated buffer risks
  452. // resulting in garbage data if GC is ran at any point inbetween due to freed buffer.
  453. // As a consequence, this event requires checks during fetching of the next event
  454. // (and window destruction) in order to ensure Haxe side gets proper buffer without memory leaks.
  455. vbyte* buffer = malloc(size * sizeof(WCHAR));
  456. if ( DragQueryFileW(drop, i, (LPWSTR)buffer, size) ) {
  457. e = addEvent(wnd, DropFile);
  458. e->value = size * sizeof(WCHAR);
  459. e->dropFile = buffer;
  460. e->mouseX = (int)dragPoint.x;
  461. e->mouseY = (int)dragPoint.y;
  462. } else {
  463. free(buffer);
  464. }
  465. }
  466. e = addEvent(wnd, DropEnd);
  467. e->value = count;
  468. e->mouseX = (int)dragPoint.x;
  469. e->mouseY = (int)dragPoint.y;
  470. DragFinish(drop);
  471. break;
  472. }
  473. case WM_INPUTLANGCHANGE:
  474. e = addEvent(wnd,KeyMapChanged);
  475. break;
  476. case WM_CLOSE:
  477. addState(Close);
  478. return 0;
  479. }
  480. return DefWindowProc(wnd, umsg, wparam, lparam);
  481. }
  482. HL_PRIM dx_window *HL_NAME(win_create_ex)( int x, int y, int width, int height, WindowFlags windowFlags ) {
  483. static bool wnd_class_reg = false;
  484. HINSTANCE hinst = GetModuleHandle(NULL);
  485. if( !wnd_class_reg ) {
  486. WNDCLASSEX wc;
  487. wchar_t fileName[1024];
  488. GetModuleFileName(hinst,fileName,1024);
  489. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  490. wc.lpfnWndProc = WndProc;
  491. wc.cbClsExtra = 0;
  492. wc.cbWndExtra = 0;
  493. wc.hInstance = hinst;
  494. wc.hIcon = ExtractIcon(hinst, fileName, 0);
  495. wc.hIconSm = wc.hIcon;
  496. wc.hCursor = NULL;
  497. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  498. wc.lpszMenuName = NULL;
  499. wc.lpszClassName = USTR("HL_WIN");
  500. wc.cbSize = sizeof(WNDCLASSEX);
  501. RegisterClassEx(&wc);
  502. }
  503. RECT r;
  504. DWORD style = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  505. if( !(windowFlags & Resizable) ) {
  506. style &= ~( WS_MAXIMIZEBOX | WS_THICKFRAME );
  507. }
  508. r.left = r.top = 0;
  509. r.right = width;
  510. r.bottom = height;
  511. AdjustWindowRect(&r,style,false);
  512. dx_events *event_buffer = (dx_events*)malloc(sizeof(dx_events));
  513. memset(event_buffer,0, sizeof(dx_events));
  514. event_buffer->normal_style = style;
  515. event_buffer->opacity = 1.0;
  516. dx_window *win = CreateWindowEx(WS_EX_APPWINDOW, USTR("HL_WIN"), USTR(""), style, x, y, r.right - r.left, r.bottom - r.top, NULL, NULL, hinst, event_buffer);
  517. SetTimer(win,0,10,NULL);
  518. if( !(windowFlags & Hidden) ) {
  519. ShowWindow(win, SW_SHOW);
  520. }
  521. SetForegroundWindow(win);
  522. SetFocus(win);
  523. return win;
  524. }
  525. HL_PRIM dx_window *HL_NAME(win_create)( int width, int height ) {
  526. return HL_NAME(win_create_ex)(CW_USEDEFAULT, CW_USEDEFAULT, width, height, Resizable);
  527. }
  528. HL_PRIM void HL_NAME(win_set_title)(dx_window *win, vbyte *title) {
  529. SetWindowText(win,(LPCWSTR)title);
  530. }
  531. HL_PRIM void HL_NAME(win_set_size)(dx_window *win, int width, int height) {
  532. RECT r;
  533. GetWindowRect(win,&r);
  534. r.left = r.top = 0;
  535. r.right = width;
  536. r.bottom = height;
  537. AdjustWindowRectEx(&r,GetWindowLong(win,GWL_STYLE),GetMenu(win) != NULL,GetWindowLong(win,GWL_EXSTYLE));
  538. SetWindowPos(win,NULL,0,0,r.right - r.left,r.bottom - r.top,SWP_NOMOVE|SWP_NOOWNERZORDER);
  539. }
  540. HL_PRIM void HL_NAME(win_get_size)(dx_window *win, int *width, int *height) {
  541. RECT r;
  542. GetClientRect(win,&r);
  543. if( width ) *width = r.right;
  544. if( height ) *height = r.bottom;
  545. }
  546. HL_PRIM void HL_NAME(win_set_min_size)(dx_window *win, int width, int height) {
  547. dx_events *buf = get_events(win);
  548. if( width < 0 ) width = 0;
  549. if( height < 0 ) height = 0;
  550. if( buf->max_width != 0 && width > buf->max_width ) width = buf->max_width;
  551. if( buf->max_height != 0 && height > buf->max_height ) height = buf->max_height;
  552. if( buf->min_width != width || buf->min_height != height ) {
  553. buf->min_width = width;
  554. buf->min_height = height;
  555. int curr_width = 0;
  556. int curr_height = 0;
  557. HL_NAME(win_get_size)(win, &curr_width, &curr_height);
  558. if( curr_width < width || curr_height < height )
  559. HL_NAME(win_set_size)(win, max(curr_width, width), max(curr_height, height));
  560. }
  561. }
  562. HL_PRIM void HL_NAME(win_get_min_size)(dx_window *win, int *width, int *height) {
  563. dx_events *buf = get_events(win);
  564. if( width ) *width = buf->min_width;
  565. if( height ) *height = buf->min_height;
  566. }
  567. HL_PRIM void HL_NAME(win_set_max_size)(dx_window *win, int width, int height) {
  568. dx_events *buf = get_events(win);
  569. if( width == 0 && height == 0 ) {
  570. buf->max_width = 0;
  571. buf->max_height = 0;
  572. } else {
  573. if( width < buf->min_width ) width = buf->min_width;
  574. if( height < buf->min_height ) height = buf->min_height;
  575. if( width == 0 ) width = 1;
  576. if( height == 0 ) height = 1;
  577. if( buf->max_width != width || buf->max_height != height ) {
  578. buf->max_width = width;
  579. buf->max_height = height;
  580. int curr_width = 0;
  581. int curr_height = 0;
  582. HL_NAME(win_get_size)(win, &curr_width, &curr_height);
  583. if( curr_width > width || curr_height > height )
  584. HL_NAME(win_set_size)(win, min(curr_width, width), min(curr_height, height));
  585. }
  586. }
  587. }
  588. HL_PRIM void HL_NAME(win_get_max_size)(dx_window *win, int *width, int *height) {
  589. dx_events *buf = get_events(win);
  590. if( width ) *width = buf->max_width;
  591. if( height ) *height = buf->max_height;
  592. }
  593. HL_PRIM void HL_NAME(win_get_position)(dx_window *win, int *x, int *y) {
  594. RECT r;
  595. GetWindowRect(win,&r);
  596. if( x ) *x = r.left;
  597. if( y ) *y = r.top;
  598. }
  599. HL_PRIM void HL_NAME(win_set_position)(dx_window *win, int x, int y) {
  600. SetWindowPos(win,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
  601. }
  602. // initially written with the intent to center on closest monitor; however, SDL centers to primary, so both options are provided
  603. HL_PRIM void HL_NAME(win_center)(dx_window *win, bool centerPrimary) {
  604. int scnX = 0;
  605. int scnY = 0;
  606. int scnWidth = -1;
  607. int scnHeight = -1;
  608. if( centerPrimary ) {
  609. scnWidth = GetSystemMetrics(SM_CXSCREEN);
  610. scnHeight = GetSystemMetrics(SM_CYSCREEN);
  611. } else {
  612. HMONITOR m = MonitorFromWindow(win, MONITOR_DEFAULTTONEAREST);
  613. if( m != NULL ) {
  614. MONITORINFO info;
  615. info.cbSize = sizeof(MONITORINFO);
  616. GetMonitorInfo(m, &info);
  617. RECT screen = info.rcMonitor; // "rcMonitor" to match SM_CXSCREEN/SM_CYSCREEN measurements
  618. scnX = screen.left;
  619. scnY = screen.top;
  620. scnWidth = (screen.right - scnX);
  621. scnHeight = (screen.bottom - scnY);
  622. }
  623. }
  624. if( scnWidth >= 0 && scnHeight >= 0 ) {
  625. int winWidth = 0;
  626. int winHeight = 0;
  627. HL_NAME(win_get_size)(win, &winWidth, &winHeight);
  628. HL_NAME(win_set_position)(win, scnX + ((scnWidth - winWidth) / 2), scnY + ((scnHeight - winHeight) / 2));
  629. }
  630. }
  631. HL_PRIM void HL_NAME(win_resize)(dx_window *win, int mode) {
  632. switch( mode ) {
  633. case 0:
  634. ShowWindow(win, SW_MAXIMIZE);
  635. break;
  636. case 1:
  637. ShowWindow(win, SW_MINIMIZE);
  638. break;
  639. case 2:
  640. ShowWindow(win, SW_RESTORE);
  641. break;
  642. case 3:
  643. ShowWindow(win, SW_HIDE);
  644. break;
  645. case 4:
  646. ShowWindow(win, SW_SHOW);
  647. break;
  648. default:
  649. break;
  650. }
  651. }
  652. HL_PRIM void HL_NAME(win_set_fullscreen)(dx_window *win, bool fs) {
  653. if( fs ) {
  654. MONITORINFO mi = { sizeof(mi) };
  655. GetMonitorInfo(MonitorFromWindow(win,MONITOR_DEFAULTTOPRIMARY), &mi);
  656. SetWindowLong(win,GWL_STYLE,WS_POPUP | WS_VISIBLE);
  657. SetWindowPos(win,NULL,mi.rcMonitor.left,mi.rcMonitor.top,mi.rcMonitor.right - mi.rcMonitor.left,mi.rcMonitor.bottom - mi.rcMonitor.top,SWP_NOOWNERZORDER|SWP_FRAMECHANGED|SWP_SHOWWINDOW);
  658. } else {
  659. dx_events *buf = get_events(win);
  660. SetWindowLong(win,GWL_STYLE,buf->normal_style);
  661. SetWindowPos(win,NULL,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOOWNERZORDER|SWP_FRAMECHANGED|SWP_SHOWWINDOW);
  662. }
  663. }
  664. HL_PRIM double HL_NAME(win_get_opacity)(dx_window *win) {
  665. dx_events *buf = get_events(win);
  666. return buf->opacity;
  667. }
  668. HL_PRIM bool HL_NAME(win_set_opacity)(dx_window *win, double opacity) {
  669. if( opacity > 1.0 )
  670. opacity = 1.0;
  671. else if( opacity < 0.0 )
  672. opacity = 0.0;
  673. dx_events *buf = get_events(win);
  674. if( buf->opacity != opacity ) {
  675. buf->opacity = opacity;
  676. LONG style = GetWindowLong(win, GWL_EXSTYLE);
  677. bool layered = (style & WS_EX_LAYERED) != 0;
  678. if( opacity >= 1.0 ) {
  679. if( layered )
  680. if( SetWindowLong(win, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0 )
  681. return false;
  682. } else {
  683. if( !layered )
  684. if( SetWindowLong(win, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0 )
  685. return false;
  686. if( SetLayeredWindowAttributes(win, 0, (BYTE)((int)(opacity * 255.0)), LWA_ALPHA) == 0 )
  687. return false;
  688. }
  689. }
  690. return true;
  691. }
  692. HL_PRIM void HL_NAME(win_destroy)(dx_window *win) {
  693. if (cur_clip_cursor_window == win) {
  694. cur_clip_cursor_window = NULL;
  695. ClipCursor(NULL);
  696. }
  697. dx_events *buf = get_events(win);
  698. DestroyWindow(win);
  699. // See WM_DROPFILES comment regarding GC
  700. for ( int i = buf->next_event; i < buf->event_count; i++ ) {
  701. if ( buf->events[i].dropFile != NULL )
  702. free(buf->events[i].dropFile);
  703. }
  704. free(buf);
  705. }
  706. HL_PRIM bool HL_NAME(win_get_next_event)( dx_window *win, dx_event *e ) {
  707. dx_events *buf = get_events(win);
  708. hl_type *save;
  709. if( !buf ) {
  710. e->type = Quit;
  711. return true;
  712. }
  713. if( buf->next_event == buf->event_count ) {
  714. buf->next_event = buf->event_count = 0;
  715. return false;
  716. }
  717. save = e->t;
  718. memcpy(e,&buf->events[buf->next_event++],sizeof(dx_event));
  719. if ( e->type == DropFile ) {
  720. // See WM_DROPFILES comment regarding GC
  721. vbyte* unmanaged = e->dropFile;
  722. e->dropFile = hl_copy_bytes(unmanaged, e->value);
  723. free(unmanaged);
  724. buf->events[buf->next_event - 1].dropFile = NULL;
  725. }
  726. e->t = save;
  727. return true;
  728. }
  729. HL_PRIM void HL_NAME(win_clip_cursor)(dx_window *win, bool enable) {
  730. capture_mouse = enable;
  731. updateClipCursor(win);
  732. }
  733. HL_PRIM bool HL_NAME(set_cursor_pos)( int x, int y ) {
  734. return SetCursorPos(x, y);
  735. }
  736. HL_PRIM bool HL_NAME(win_set_cursor_pos)( dx_window *wnd, int x, int y) {
  737. if ( wnd ) {
  738. POINT pt;
  739. pt.x = x;
  740. pt.y = y;
  741. ClientToScreen(wnd, &pt);
  742. return SetCursorPos(pt.x, pt.y);
  743. }
  744. return false;
  745. }
  746. HL_PRIM bool HL_NAME(win_set_relative_mouse_mode)( dx_window *wnd, bool enabled) {
  747. return setRelativeMode(wnd, enabled);
  748. }
  749. HL_PRIM bool HL_NAME(win_get_relative_mouse_mode)() {
  750. return relative_mouse;
  751. }
  752. HL_PRIM void HL_NAME(win_set_drag_accept_files)( dx_window* wnd, bool enabled ) {
  753. DragAcceptFiles(wnd, enabled);
  754. }
  755. HL_PRIM int HL_NAME(get_screen_width)() {
  756. return GetSystemMetrics(SM_CXSCREEN);
  757. }
  758. HL_PRIM int HL_NAME(get_screen_height)() {
  759. return GetSystemMetrics(SM_CYSCREEN);
  760. }
  761. typedef struct {
  762. int idx;
  763. varray* arr;
  764. } get_monitors_data;
  765. BOOL CALLBACK on_get_monitors(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM param) {
  766. get_monitors_data *data = (get_monitors_data*)param;
  767. varray* arr = data->arr;
  768. MONITORINFOEXW info;
  769. info.cbSize = sizeof(MONITORINFOEXW);
  770. GetMonitorInfoW(monitor, (LPMONITORINFO)&info);
  771. vdynamic* dynobj = (vdynamic*)hl_alloc_dynobj();
  772. hl_dyn_seti(dynobj, hl_hash_utf8("left"), &hlt_i32, rect->left);
  773. hl_dyn_seti(dynobj, hl_hash_utf8("right"), &hlt_i32, rect->right);
  774. hl_dyn_seti(dynobj, hl_hash_utf8("top"), &hlt_i32, rect->top);
  775. hl_dyn_seti(dynobj, hl_hash_utf8("bottom"), &hlt_i32, rect->bottom);
  776. hl_dyn_setp(dynobj, hl_hash_utf8("name"), &hlt_bytes, hl_copy_bytes((vbyte*)info.szDevice, (int)(wcslen(info.szDevice)+1)*2));
  777. hl_aptr(arr, vdynobj*)[data->idx++] = (vdynobj*)dynobj;
  778. return TRUE;
  779. }
  780. HL_PRIM varray* HL_NAME(win_get_monitors)() {
  781. get_monitors_data data;
  782. data.idx = 0;
  783. data.arr = hl_alloc_array(&hlt_dynobj, 64);
  784. EnumDisplayMonitors(NULL, NULL, on_get_monitors, (LPARAM)&data);
  785. data.arr->size = data.idx;
  786. return data.arr;
  787. }
  788. HL_PRIM vbyte* HL_NAME(win_get_monitor_from_window)(HWND wnd) {
  789. HMONITOR handle = MonitorFromWindow(wnd, MONITOR_DEFAULTTOPRIMARY);
  790. MONITORINFOEX info;
  791. info.cbSize = sizeof(MONITORINFOEX);
  792. if (!GetMonitorInfo(handle, (LPMONITORINFO)&info))
  793. return NULL;
  794. return hl_copy_bytes((vbyte*)info.szDevice, (int)(wcslen(info.szDevice) + 1) * 2);
  795. }
  796. HL_PRIM varray* HL_NAME(win_get_display_settings)(wchar_t* device) {
  797. DEVMODEW ds;
  798. ds.dmSize = sizeof(DEVMODEW);
  799. int len = 0;
  800. while (EnumDisplaySettingsW(device, len, &ds))
  801. len++;
  802. varray* arr = hl_alloc_array(&hlt_dynobj, len);
  803. for (int i = 0; EnumDisplaySettingsW(device, i, &ds); i++) {
  804. vdynamic* dynobj = (vdynamic*) hl_alloc_dynobj();
  805. hl_dyn_seti(dynobj, hl_hash_utf8("width"), &hlt_i32, ds.dmPelsWidth);
  806. hl_dyn_seti(dynobj, hl_hash_utf8("height"), &hlt_i32, ds.dmPelsHeight);
  807. hl_dyn_seti(dynobj, hl_hash_utf8("framerate"), &hlt_i32, ds.dmDisplayFrequency);
  808. hl_aptr(arr, vdynobj*)[i] = (vdynobj*) dynobj;
  809. }
  810. return arr;
  811. }
  812. HL_PRIM vdynamic* HL_NAME(win_get_current_display_setting)(wchar_t* device, bool registry) {
  813. DEVMODEW ds;
  814. ds.dmSize = sizeof(DEVMODEW);
  815. EnumDisplaySettingsW(device, registry ? ENUM_REGISTRY_SETTINGS : ENUM_CURRENT_SETTINGS, &ds);
  816. vdynamic* dynobj = (vdynamic*) hl_alloc_dynobj();
  817. hl_dyn_seti(dynobj, hl_hash_utf8("width"), &hlt_i32, ds.dmPelsWidth);
  818. hl_dyn_seti(dynobj, hl_hash_utf8("height"), &hlt_i32, ds.dmPelsHeight);
  819. hl_dyn_seti(dynobj, hl_hash_utf8("framerate"), &hlt_i32, ds.dmDisplayFrequency);
  820. return dynobj;
  821. }
  822. HL_PRIM int HL_NAME(win_change_display_setting)(wchar_t* device, vdynamic* ds) {
  823. bool found = false;
  824. DEVMODEW devMode;
  825. devMode.dmSize = sizeof(DEVMODEW);
  826. if (ds != NULL) {
  827. int width = hl_dyn_geti(ds, hl_hash_utf8("width"), &hlt_i32);
  828. int height = hl_dyn_geti(ds, hl_hash_utf8("height"), &hlt_i32);
  829. int framerate = hl_dyn_geti(ds, hl_hash_utf8("framerate"), &hlt_i32);
  830. for (int i = 0; EnumDisplaySettingsW(device, i, &devMode); i++) {
  831. if (devMode.dmPelsWidth == width && devMode.dmPelsHeight == height && devMode.dmDisplayFrequency == framerate) {
  832. found = true;
  833. break;
  834. }
  835. }
  836. }
  837. return ChangeDisplaySettingsExW(device, found ? &devMode : NULL, NULL, found ? CDS_FULLSCREEN : 0, NULL);
  838. }
  839. #define TWIN _ABSTRACT(dx_window)
  840. DEFINE_PRIM(TWIN, win_create_ex, _I32 _I32 _I32 _I32 _I32);
  841. DEFINE_PRIM(TWIN, win_create, _I32 _I32);
  842. DEFINE_PRIM(_VOID, win_set_fullscreen, TWIN _BOOL);
  843. DEFINE_PRIM(_VOID, win_resize, TWIN _I32);
  844. DEFINE_PRIM(_VOID, win_set_title, TWIN _BYTES);
  845. DEFINE_PRIM(_VOID, win_set_size, TWIN _I32 _I32);
  846. DEFINE_PRIM(_VOID, win_set_min_size, TWIN _I32 _I32);
  847. DEFINE_PRIM(_VOID, win_set_max_size, TWIN _I32 _I32);
  848. DEFINE_PRIM(_VOID, win_set_position, TWIN _I32 _I32);
  849. DEFINE_PRIM(_VOID, win_center, TWIN _BOOL);
  850. DEFINE_PRIM(_VOID, win_get_size, TWIN _REF(_I32) _REF(_I32));
  851. DEFINE_PRIM(_VOID, win_get_min_size, TWIN _REF(_I32) _REF(_I32));
  852. DEFINE_PRIM(_VOID, win_get_max_size, TWIN _REF(_I32) _REF(_I32));
  853. DEFINE_PRIM(_VOID, win_get_position, TWIN _REF(_I32) _REF(_I32));
  854. DEFINE_PRIM(_F64, win_get_opacity, TWIN);
  855. DEFINE_PRIM(_BOOL, win_set_opacity, TWIN _F64);
  856. DEFINE_PRIM(_VOID, win_destroy, TWIN);
  857. DEFINE_PRIM(_BOOL, win_get_next_event, TWIN _DYN);
  858. DEFINE_PRIM(_VOID, win_clip_cursor, TWIN _BOOL);
  859. DEFINE_PRIM(_BOOL, set_cursor_pos, _I32 _I32);
  860. DEFINE_PRIM(_BOOL, win_set_cursor_pos, TWIN _I32 _I32);
  861. DEFINE_PRIM(_BOOL, win_set_relative_mouse_mode, TWIN _BOOL);
  862. DEFINE_PRIM(_BOOL, win_get_relative_mouse_mode, _NO_ARG);
  863. DEFINE_PRIM(_VOID, win_set_drag_accept_files, TWIN _BOOL);
  864. DEFINE_PRIM(_ARR, win_get_display_settings, _BYTES);
  865. DEFINE_PRIM(_DYN, win_get_current_display_setting, _BYTES _BOOL);
  866. DEFINE_PRIM(_I32, win_change_display_setting, _BYTES _DYN);
  867. DEFINE_PRIM(_ARR, win_get_monitors, _NO_ARG);
  868. DEFINE_PRIM(_BYTES, win_get_monitor_from_window, TWIN);
  869. DEFINE_PRIM(_I32, get_screen_width, _NO_ARG);
  870. DEFINE_PRIM(_I32, get_screen_height, _NO_ARG);
  871. HL_PRIM dx_cursor HL_NAME(load_cursor)( int res ) {
  872. return LoadCursor(NULL,MAKEINTRESOURCE(res));
  873. }
  874. HL_PRIM dx_cursor HL_NAME(create_cursor)( int width, int height, vbyte *data, int hotX, int hotY ) {
  875. int pad = sizeof(void*) << 3;
  876. HICON hicon;
  877. HDC hdc = GetDC(NULL);
  878. BITMAPV4HEADER bmh;
  879. void *pixels;
  880. void *maskbits;
  881. int maskbitslen;
  882. ICONINFO ii;
  883. ZeroMemory(&bmh,sizeof(bmh));
  884. bmh.bV4Size = sizeof(bmh);
  885. bmh.bV4Width = width;
  886. bmh.bV4Height = -height;
  887. bmh.bV4Planes = 1;
  888. bmh.bV4BitCount = 32;
  889. bmh.bV4V4Compression = BI_BITFIELDS;
  890. bmh.bV4AlphaMask = 0xFF000000;
  891. bmh.bV4RedMask = 0x00FF0000;
  892. bmh.bV4GreenMask = 0x0000FF00;
  893. bmh.bV4BlueMask = 0x000000FF;
  894. maskbitslen = ((width + (-width)%pad) >> 3) * height;
  895. maskbits = malloc(maskbitslen);
  896. if( maskbits == NULL )
  897. return NULL;
  898. memset(maskbits,0xFF,maskbitslen);
  899. memset(&ii,0,sizeof(ii));
  900. ii.fIcon = FALSE;
  901. ii.xHotspot = (DWORD)hotX;
  902. ii.yHotspot = (DWORD)hotY;
  903. ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
  904. ii.hbmMask = CreateBitmap(width, height, 1, 1, maskbits);
  905. ReleaseDC(NULL, hdc);
  906. free(maskbits);
  907. memcpy(pixels, data, height * width * 4);
  908. hicon = CreateIconIndirect(&ii);
  909. DeleteObject(ii.hbmColor);
  910. DeleteObject(ii.hbmMask);
  911. return hicon;
  912. }
  913. HL_PRIM void HL_NAME(destroy_cursor)( dx_cursor c ) {
  914. DestroyIcon(c);
  915. }
  916. HL_PRIM void HL_NAME(set_cursor)( dx_cursor c ) {
  917. cur_cursor = c;
  918. if( CURSOR_VISIBLE )
  919. SetCursor(c);
  920. }
  921. HL_PRIM void HL_NAME(show_cursor)( bool visible ) {
  922. show_cursor = visible;
  923. SetCursor(CURSOR_VISIBLE ? cur_cursor : NULL);
  924. }
  925. HL_PRIM bool HL_NAME(is_cursor_visible)() {
  926. return show_cursor;
  927. }
  928. #define TCURSOR _ABSTRACT(dx_cursor)
  929. DEFINE_PRIM(TCURSOR, load_cursor, _I32);
  930. DEFINE_PRIM(TCURSOR, create_cursor, _I32 _I32 _BYTES _I32 _I32);
  931. DEFINE_PRIM(_VOID, destroy_cursor, TCURSOR);
  932. DEFINE_PRIM(_VOID, set_cursor, TCURSOR);
  933. DEFINE_PRIM(_VOID, show_cursor, _BOOL);
  934. DEFINE_PRIM(_BOOL, is_cursor_visible, _NO_ARG);
  935. HL_PRIM vbyte *HL_NAME(detect_keyboard_layout)() {
  936. char q = MapVirtualKey(0x10, MAPVK_VSC_TO_VK);
  937. char w = MapVirtualKey(0x11, MAPVK_VSC_TO_VK);
  938. char y = MapVirtualKey(0x15, MAPVK_VSC_TO_VK);
  939. if (q == 'Q' && w == 'W' && y == 'Y') return "qwerty";
  940. if (q == 'A' && w == 'Z' && y == 'Y') return "azerty";
  941. if (q == 'Q' && w == 'W' && y == 'Z') return "qwertz";
  942. if (q == 'Q' && w == 'Z' && y == 'Y') return "qzerty";
  943. return "unknown";
  944. }
  945. DEFINE_PRIM(_BYTES, detect_keyboard_layout, _NO_ARG);