window.c 31 KB

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