window.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. } EventType;
  16. typedef enum {
  17. Show = 0,
  18. Hide = 1,
  19. Expose = 2,
  20. Move = 3,
  21. Resize = 4,
  22. Minimize= 5,
  23. Maximize= 6,
  24. Restore = 7,
  25. Enter = 8,
  26. Leave = 9,
  27. Focus = 10,
  28. Blur = 11,
  29. Close = 12
  30. } WindowStateChange;
  31. typedef enum {
  32. Hidden = 0x000001,
  33. Resizable = 0x000002
  34. } WindowFlags;
  35. typedef struct {
  36. hl_type *t;
  37. EventType type;
  38. int mouseX;
  39. int mouseY;
  40. int button;
  41. int wheelDelta;
  42. WindowStateChange state;
  43. int keyCode;
  44. int scanCode;
  45. bool keyRepeat;
  46. int controller;
  47. int value;
  48. } dx_event;
  49. typedef struct {
  50. dx_event events[MAX_EVENTS];
  51. DWORD normal_style;
  52. double opacity;
  53. int min_width;
  54. int min_height;
  55. int max_width;
  56. int max_height;
  57. int event_count;
  58. int next_event;
  59. bool is_over;
  60. } dx_events;
  61. typedef struct HWND__ dx_window;
  62. static dx_window *cur_clip_cursor_window = NULL;
  63. typedef HCURSOR dx_cursor;
  64. static dx_cursor cur_cursor = NULL;
  65. static bool show_cursor = true;
  66. static dx_events *get_events(HWND wnd) {
  67. return (dx_events*)GetWindowLongPtr(wnd,GWLP_USERDATA);
  68. }
  69. static void updateClipCursor(HWND wnd) {
  70. if (cur_clip_cursor_window == wnd) {
  71. RECT rect;
  72. GetClientRect(wnd, &rect);
  73. ClientToScreen(wnd, (LPPOINT)& rect.left);
  74. ClientToScreen(wnd, (LPPOINT)& rect.right);
  75. ClipCursor(&rect);
  76. }
  77. }
  78. static dx_event *addEvent( HWND wnd, EventType type ) {
  79. dx_events *buf = get_events(wnd);
  80. dx_event *e;
  81. if( buf->event_count == MAX_EVENTS )
  82. e = &buf->events[MAX_EVENTS-1];
  83. else
  84. e = &buf->events[buf->event_count++];
  85. e->type = type;
  86. return e;
  87. }
  88. #define addMouse(etype,but) { \
  89. e = addEvent(wnd,etype); \
  90. e->button = but; \
  91. e->mouseX = (short)LOWORD(lparam); \
  92. e->mouseY = (short)HIWORD(lparam); \
  93. }
  94. #define addState(wstate) { e = addEvent(wnd,WindowState); e->state = wstate; }
  95. static bool shift_downs[] = { false, false };
  96. static LRESULT CALLBACK WndProc( HWND wnd, UINT umsg, WPARAM wparam, LPARAM lparam ) {
  97. dx_event *e = NULL;
  98. switch(umsg) {
  99. case WM_DESTROY:
  100. PostQuitMessage(0);
  101. return 0;
  102. case WM_CREATE:
  103. SetWindowLongPtr(wnd,GWLP_USERDATA,(LONG_PTR)((CREATESTRUCT*)lparam)->lpCreateParams);
  104. break;
  105. case WM_SIZE:
  106. {
  107. dx_events *buf = get_events(wnd);
  108. if( buf->event_count > buf->next_event && buf->events[buf->event_count-1].type == WindowState && buf->events[buf->event_count-1].state == Resize )
  109. buf->event_count--;
  110. }
  111. addState(Resize);
  112. break;
  113. case WM_LBUTTONDOWN: addMouse(MouseDown,1); break;
  114. case WM_LBUTTONUP: addMouse(MouseUp,1); break;
  115. case WM_MBUTTONDOWN: addMouse(MouseDown,2); break;
  116. case WM_MBUTTONUP: addMouse(MouseUp,2); break;
  117. case WM_RBUTTONDOWN: addMouse(MouseDown,3); break;
  118. case WM_RBUTTONUP: addMouse(MouseUp,3); break;
  119. case WM_XBUTTONDOWN: addMouse(MouseDown,3 + HIWORD(wparam)); break;
  120. case WM_XBUTTONUP: addMouse(MouseUp,3 + HIWORD(wparam)); break;
  121. case WM_MOUSEWHEEL: addMouse(MouseWheel,0); e->wheelDelta = ((int)(short)HIWORD(wparam)) / 120; break;
  122. case WM_SYSCOMMAND:
  123. if( wparam == SC_KEYMENU && (lparam>>16) <= 0 )
  124. return 0;
  125. break;
  126. case WM_MOUSEMOVE:
  127. {
  128. dx_events *evt = get_events(wnd);
  129. if( !evt->is_over ) {
  130. TRACKMOUSEEVENT ev;
  131. ev.cbSize = sizeof(ev);
  132. ev.dwFlags = TME_LEAVE;
  133. ev.hwndTrack = wnd;
  134. TrackMouseEvent(&ev);
  135. evt->is_over = true;
  136. addState(Enter);
  137. }
  138. addMouse(MouseMove,0);
  139. }
  140. break;
  141. case WM_MOUSELEAVE:
  142. addState(Leave);
  143. get_events(wnd)->is_over = false;
  144. break;
  145. case WM_KEYDOWN:
  146. case WM_SYSKEYDOWN:
  147. case WM_KEYUP:
  148. case WM_SYSKEYUP:
  149. // right alt has triggered a control !
  150. if( wparam == VK_MENU && lparam & (1<<24) ) {
  151. dx_events *buf = get_events(wnd);
  152. 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) ) {
  153. buf->event_count--;
  154. //printf("CANCEL\n");
  155. }
  156. }
  157. bool repeat = (umsg == WM_KEYDOWN || umsg == WM_SYSKEYDOWN) && (lparam & 0x40000000) != 0;
  158. // see
  159. e = addEvent(wnd,(umsg == WM_KEYUP || umsg == WM_SYSKEYUP) ? KeyUp : KeyDown);
  160. e->keyCode = (int)wparam;
  161. e->scanCode = (lparam >> 16) & 0xFF;
  162. e->keyRepeat = repeat;
  163. // L/R location
  164. if( e->keyCode == VK_SHIFT ) {
  165. bool right = MapVirtualKey((lparam >> 16) & 0xFF, MAPVK_VSC_TO_VK_EX) == VK_RSHIFT;
  166. e->keyCode |= right ? 512 : 256;
  167. e->keyRepeat = false;
  168. shift_downs[right?1:0] = e->type == KeyDown;
  169. }
  170. if( e->keyCode == VK_SHIFT || e->keyCode == VK_CONTROL || e->keyCode == VK_MENU )
  171. e->keyCode |= (lparam & (1<<24)) ? 512 : 256;
  172. if( e->keyCode == 13 && (lparam & 0x1000000) )
  173. e->keyCode = 108; // numpad enter
  174. //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" : "");
  175. if( (e->keyCode & 0xFF) == VK_MENU )
  176. return 0;
  177. break;
  178. case WM_TIMER:
  179. // bugfix for shifts being considered as a single key (one single WM_KEYUP is received when both are down)
  180. if( shift_downs[0] && GetKeyState(VK_LSHIFT) >= 0 ) {
  181. //printf("LSHIFT RELEASED\n");
  182. shift_downs[0] = false;
  183. e = addEvent(wnd,KeyUp);
  184. e->keyCode = VK_SHIFT | 256;
  185. }
  186. if( shift_downs[1] && GetKeyState(VK_RSHIFT) >= 0 ) {
  187. //printf("RSHIFT RELEASED\n");
  188. shift_downs[1] = false;
  189. e = addEvent(wnd,KeyUp);
  190. e->keyCode = VK_SHIFT | 512;
  191. }
  192. break;
  193. case WM_CHAR:
  194. e = addEvent(wnd,TextInput);
  195. e->keyCode = (int)wparam;
  196. e->keyRepeat = (lparam & 0xFFFF) != 0;
  197. break;
  198. case WM_SETFOCUS:
  199. updateClipCursor(wnd);
  200. addState(Focus);
  201. break;
  202. case WM_KILLFOCUS:
  203. shift_downs[0] = false;
  204. shift_downs[1] = false;
  205. addState(Blur);
  206. break;
  207. case WM_WINDOWPOSCHANGED:
  208. updateClipCursor(wnd);
  209. break;
  210. case WM_GETMINMAXINFO:
  211. {
  212. dx_events *buf = get_events(wnd);
  213. if( buf ) {
  214. long resizable_flags = (WS_MAXIMIZEBOX | WS_THICKFRAME);
  215. if( (buf->normal_style & resizable_flags) == resizable_flags ) {
  216. if( buf->min_width != 0 || buf->min_height != 0 ||
  217. buf->max_width != 0 || buf->max_height != 0 ) {
  218. MINMAXINFO *info = (MINMAXINFO*)lparam;
  219. RECT r;
  220. GetClientRect(wnd, &r);
  221. int curr_width = r.right;
  222. int curr_height = r.bottom;
  223. // remove curr_width/height which contain non-client dimensions
  224. bool apply_max = false;
  225. int min_width = buf->min_width - curr_width;
  226. int min_height = buf->min_height - curr_height;
  227. int max_width = buf->max_width;
  228. int max_height = buf->max_height;
  229. if( max_width && max_height ) {
  230. max_width -= curr_width;
  231. max_height -= curr_height;
  232. apply_max = true;
  233. }
  234. // fix curr_width/height to contain only client size
  235. {
  236. RECT size;
  237. LONG style = GetWindowLong(wnd, GWL_STYLE);
  238. BOOL menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(wnd) != NULL);
  239. size.top = 0;
  240. size.left = 0;
  241. size.bottom = curr_height;
  242. size.right = curr_width;
  243. AdjustWindowRectEx(&size, style, menu, 0);
  244. curr_width = size.right - size.left;
  245. curr_height = size.bottom - size.top;
  246. }
  247. // apply curr_width/height without client size
  248. info->ptMinTrackSize.x = min_width + curr_width;
  249. info->ptMinTrackSize.y = min_height + curr_height;
  250. if( apply_max ) {
  251. info->ptMaxTrackSize.x = max_width + curr_width;
  252. info->ptMaxTrackSize.y = max_height + curr_height;
  253. }
  254. return 0;
  255. }
  256. }
  257. }
  258. break;
  259. }
  260. case WM_SETCURSOR:
  261. if( LOWORD(lparam) == HTCLIENT ) {
  262. if( show_cursor )
  263. SetCursor(cur_cursor != NULL ? cur_cursor : LoadCursor(NULL, IDC_ARROW));
  264. else
  265. SetCursor(NULL);
  266. return TRUE;
  267. }
  268. break;
  269. case WM_CLOSE:
  270. addEvent(wnd, Quit);
  271. return 0;
  272. }
  273. return DefWindowProc(wnd, umsg, wparam, lparam);
  274. }
  275. HL_PRIM dx_window *HL_NAME(win_create_ex)( int x, int y, int width, int height, WindowFlags windowFlags ) {
  276. static bool wnd_class_reg = false;
  277. HINSTANCE hinst = GetModuleHandle(NULL);
  278. if( !wnd_class_reg ) {
  279. WNDCLASSEX wc;
  280. wchar_t fileName[1024];
  281. GetModuleFileName(hinst,fileName,1024);
  282. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  283. wc.lpfnWndProc = WndProc;
  284. wc.cbClsExtra = 0;
  285. wc.cbWndExtra = 0;
  286. wc.hInstance = hinst;
  287. wc.hIcon = ExtractIcon(hinst, fileName, 0);
  288. wc.hIconSm = wc.hIcon;
  289. wc.hCursor = NULL;
  290. wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  291. wc.lpszMenuName = NULL;
  292. wc.lpszClassName = USTR("HL_WIN");
  293. wc.cbSize = sizeof(WNDCLASSEX);
  294. RegisterClassEx(&wc);
  295. }
  296. RECT r;
  297. DWORD style = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  298. if( !(windowFlags & Resizable) ) {
  299. style &= ~( WS_MAXIMIZEBOX | WS_THICKFRAME );
  300. }
  301. r.left = r.top = 0;
  302. r.right = width;
  303. r.bottom = height;
  304. AdjustWindowRect(&r,style,false);
  305. dx_events *event_buffer = (dx_events*)malloc(sizeof(dx_events));
  306. memset(event_buffer,0, sizeof(dx_events));
  307. event_buffer->normal_style = style;
  308. event_buffer->opacity = 1.0;
  309. 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);
  310. SetTimer(win,0,10,NULL);
  311. if( !(windowFlags & Hidden) ) {
  312. ShowWindow(win, SW_SHOW);
  313. }
  314. SetForegroundWindow(win);
  315. SetFocus(win);
  316. return win;
  317. }
  318. HL_PRIM dx_window *HL_NAME(win_create)( int width, int height ) {
  319. return HL_NAME(win_create_ex)(CW_USEDEFAULT, CW_USEDEFAULT, width, height, Resizable);
  320. }
  321. HL_PRIM void HL_NAME(win_set_title)(dx_window *win, vbyte *title) {
  322. SetWindowText(win,(LPCWSTR)title);
  323. }
  324. HL_PRIM void HL_NAME(win_set_size)(dx_window *win, int width, int height) {
  325. RECT r;
  326. GetWindowRect(win,&r);
  327. r.left = r.top = 0;
  328. r.right = width;
  329. r.bottom = height;
  330. AdjustWindowRectEx(&r,GetWindowLong(win,GWL_STYLE),GetMenu(win) != NULL,GetWindowLong(win,GWL_EXSTYLE));
  331. SetWindowPos(win,NULL,0,0,r.right - r.left,r.bottom - r.top,SWP_NOMOVE|SWP_NOOWNERZORDER);
  332. }
  333. HL_PRIM void HL_NAME(win_get_size)(dx_window *win, int *width, int *height) {
  334. RECT r;
  335. GetClientRect(win,&r);
  336. if( width ) *width = r.right;
  337. if( height ) *height = r.bottom;
  338. }
  339. HL_PRIM void HL_NAME(win_set_min_size)(dx_window *win, int width, int height) {
  340. dx_events *buf = get_events(win);
  341. if( width < 0 ) width = 0;
  342. if( height < 0 ) height = 0;
  343. if( buf->max_width != 0 && width > buf->max_width ) width = buf->max_width;
  344. if( buf->max_height != 0 && height > buf->max_height ) height = buf->max_height;
  345. if( buf->min_width != width || buf->min_height != height ) {
  346. buf->min_width = width;
  347. buf->min_height = height;
  348. int curr_width = 0;
  349. int curr_height = 0;
  350. HL_NAME(win_get_size)(win, &curr_width, &curr_height);
  351. if( curr_width < width || curr_height < height )
  352. HL_NAME(win_set_size)(win, max(curr_width, width), max(curr_height, height));
  353. }
  354. }
  355. HL_PRIM void HL_NAME(win_get_min_size)(dx_window *win, int *width, int *height) {
  356. dx_events *buf = get_events(win);
  357. if( width ) *width = buf->min_width;
  358. if( height ) *height = buf->min_height;
  359. }
  360. HL_PRIM void HL_NAME(win_set_max_size)(dx_window *win, int width, int height) {
  361. dx_events *buf = get_events(win);
  362. if( width == 0 && height == 0 ) {
  363. buf->max_width = 0;
  364. buf->max_height = 0;
  365. } else {
  366. if( width < buf->min_width ) width = buf->min_width;
  367. if( height < buf->min_height ) height = buf->min_height;
  368. if( width == 0 ) width = 1;
  369. if( height == 0 ) height = 1;
  370. if( buf->max_width != width || buf->max_height != height ) {
  371. buf->max_width = width;
  372. buf->max_height = height;
  373. int curr_width = 0;
  374. int curr_height = 0;
  375. HL_NAME(win_get_size)(win, &curr_width, &curr_height);
  376. if( curr_width > width || curr_height > height )
  377. HL_NAME(win_set_size)(win, min(curr_width, width), min(curr_height, height));
  378. }
  379. }
  380. }
  381. HL_PRIM void HL_NAME(win_get_max_size)(dx_window *win, int *width, int *height) {
  382. dx_events *buf = get_events(win);
  383. if( width ) *width = buf->max_width;
  384. if( height ) *height = buf->max_height;
  385. }
  386. HL_PRIM void HL_NAME(win_get_position)(dx_window *win, int *x, int *y) {
  387. RECT r;
  388. GetWindowRect(win,&r);
  389. if( x ) *x = r.left;
  390. if( y ) *y = r.top;
  391. }
  392. HL_PRIM void HL_NAME(win_set_position)(dx_window *win, int x, int y) {
  393. SetWindowPos(win,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
  394. }
  395. // initially written with the intent to center on closest monitor; however, SDL centers to primary, so both options are provided
  396. HL_PRIM void HL_NAME(win_center)(dx_window *win, bool centerPrimary) {
  397. int scnX = 0;
  398. int scnY = 0;
  399. int scnWidth = -1;
  400. int scnHeight = -1;
  401. if( centerPrimary ) {
  402. scnWidth = GetSystemMetrics(SM_CXSCREEN);
  403. scnHeight = GetSystemMetrics(SM_CYSCREEN);
  404. } else {
  405. HMONITOR m = MonitorFromWindow(win, MONITOR_DEFAULTTONEAREST);
  406. if( m != NULL ) {
  407. MONITORINFO info;
  408. info.cbSize = sizeof(MONITORINFO);
  409. GetMonitorInfo(m, &info);
  410. RECT screen = info.rcMonitor; // "rcMonitor" to match SM_CXSCREEN/SM_CYSCREEN measurements
  411. scnX = screen.left;
  412. scnY = screen.top;
  413. scnWidth = (screen.right - scnX);
  414. scnHeight = (screen.bottom - scnY);
  415. }
  416. }
  417. if( scnWidth >= 0 && scnHeight >= 0 ) {
  418. int winWidth = 0;
  419. int winHeight = 0;
  420. HL_NAME(win_get_size)(win, &winWidth, &winHeight);
  421. HL_NAME(win_set_position)(win, scnX + ((scnWidth - winWidth) / 2), scnY + ((scnHeight - winHeight) / 2));
  422. }
  423. }
  424. HL_PRIM void HL_NAME(win_resize)(dx_window *win, int mode) {
  425. switch( mode ) {
  426. case 0:
  427. ShowWindow(win, SW_MAXIMIZE);
  428. break;
  429. case 1:
  430. ShowWindow(win, SW_MINIMIZE);
  431. break;
  432. case 2:
  433. ShowWindow(win, SW_RESTORE);
  434. break;
  435. case 3:
  436. ShowWindow(win, SW_HIDE);
  437. break;
  438. case 4:
  439. ShowWindow(win, SW_SHOW);
  440. break;
  441. default:
  442. break;
  443. }
  444. }
  445. HL_PRIM void HL_NAME(win_set_fullscreen)(dx_window *win, bool fs) {
  446. if( fs ) {
  447. MONITORINFO mi = { sizeof(mi) };
  448. GetMonitorInfo(MonitorFromWindow(win,MONITOR_DEFAULTTOPRIMARY), &mi);
  449. SetWindowLong(win,GWL_STYLE,WS_POPUP | WS_VISIBLE);
  450. 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);
  451. } else {
  452. dx_events *buf = get_events(win);
  453. SetWindowLong(win,GWL_STYLE,buf->normal_style);
  454. SetWindowPos(win,NULL,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOOWNERZORDER|SWP_FRAMECHANGED|SWP_SHOWWINDOW);
  455. }
  456. }
  457. HL_PRIM double HL_NAME(win_get_opacity)(dx_window *win) {
  458. dx_events *buf = get_events(win);
  459. return buf->opacity;
  460. }
  461. HL_PRIM bool HL_NAME(win_set_opacity)(dx_window *win, double opacity) {
  462. if( opacity > 1.0 )
  463. opacity = 1.0;
  464. else if( opacity < 0.0 )
  465. opacity = 0.0;
  466. dx_events *buf = get_events(win);
  467. if( buf->opacity != opacity ) {
  468. buf->opacity = opacity;
  469. LONG style = GetWindowLong(win, GWL_EXSTYLE);
  470. bool layered = (style & WS_EX_LAYERED) != 0;
  471. if( opacity >= 1.0 ) {
  472. if( layered )
  473. if( SetWindowLong(win, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0 )
  474. return false;
  475. } else {
  476. if( !layered )
  477. if( SetWindowLong(win, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0 )
  478. return false;
  479. if( SetLayeredWindowAttributes(win, 0, (BYTE)((int)(opacity * 255.0)), LWA_ALPHA) == 0 )
  480. return false;
  481. }
  482. }
  483. return true;
  484. }
  485. HL_PRIM void HL_NAME(win_destroy)(dx_window *win) {
  486. if (cur_clip_cursor_window == win) {
  487. cur_clip_cursor_window = NULL;
  488. ClipCursor(NULL);
  489. }
  490. dx_events *buf = get_events(win);
  491. free(buf);
  492. SetWindowLongPtr(win,GWLP_USERDATA,0);
  493. DestroyWindow(win);
  494. }
  495. HL_PRIM bool HL_NAME(win_get_next_event)( dx_window *win, dx_event *e ) {
  496. dx_events *buf = get_events(win);
  497. hl_type *save;
  498. if( !buf ) {
  499. e->type = Quit;
  500. return true;
  501. }
  502. if( buf->next_event == buf->event_count ) {
  503. buf->next_event = buf->event_count = 0;
  504. return false;
  505. }
  506. save = e->t;
  507. memcpy(e,&buf->events[buf->next_event++],sizeof(dx_event));
  508. e->t = save;
  509. return true;
  510. }
  511. HL_PRIM void HL_NAME(win_clip_cursor)(dx_window *win) {
  512. cur_clip_cursor_window = win;
  513. if (win)
  514. updateClipCursor(win);
  515. else
  516. ClipCursor(NULL);
  517. }
  518. HL_PRIM int HL_NAME(get_screen_width)() {
  519. return GetSystemMetrics(SM_CXSCREEN);
  520. }
  521. HL_PRIM int HL_NAME(get_screen_height)() {
  522. return GetSystemMetrics(SM_CYSCREEN);
  523. }
  524. #define TWIN _ABSTRACT(dx_window)
  525. DEFINE_PRIM(TWIN, win_create_ex, _I32 _I32 _I32 _I32 _I32);
  526. DEFINE_PRIM(TWIN, win_create, _I32 _I32);
  527. DEFINE_PRIM(_VOID, win_set_fullscreen, TWIN _BOOL);
  528. DEFINE_PRIM(_VOID, win_resize, TWIN _I32);
  529. DEFINE_PRIM(_VOID, win_set_title, TWIN _BYTES);
  530. DEFINE_PRIM(_VOID, win_set_size, TWIN _I32 _I32);
  531. DEFINE_PRIM(_VOID, win_set_min_size, TWIN _I32 _I32);
  532. DEFINE_PRIM(_VOID, win_set_max_size, TWIN _I32 _I32);
  533. DEFINE_PRIM(_VOID, win_set_position, TWIN _I32 _I32);
  534. DEFINE_PRIM(_VOID, win_center, TWIN _BOOL);
  535. DEFINE_PRIM(_VOID, win_get_size, TWIN _REF(_I32) _REF(_I32));
  536. DEFINE_PRIM(_VOID, win_get_min_size, TWIN _REF(_I32) _REF(_I32));
  537. DEFINE_PRIM(_VOID, win_get_max_size, TWIN _REF(_I32) _REF(_I32));
  538. DEFINE_PRIM(_VOID, win_get_position, TWIN _REF(_I32) _REF(_I32));
  539. DEFINE_PRIM(_F64, win_get_opacity, TWIN);
  540. DEFINE_PRIM(_BOOL, win_set_opacity, TWIN _F64);
  541. DEFINE_PRIM(_VOID, win_destroy, TWIN);
  542. DEFINE_PRIM(_BOOL, win_get_next_event, TWIN _DYN);
  543. DEFINE_PRIM(_VOID, win_clip_cursor, TWIN);
  544. DEFINE_PRIM(_I32, get_screen_width, _NO_ARG);
  545. DEFINE_PRIM(_I32, get_screen_height, _NO_ARG);
  546. HL_PRIM dx_cursor HL_NAME(load_cursor)( int res ) {
  547. return LoadCursor(NULL,MAKEINTRESOURCE(res));
  548. }
  549. HL_PRIM dx_cursor HL_NAME(create_cursor)( int width, int height, vbyte *data, int hotX, int hotY ) {
  550. int pad = sizeof(void*) << 3;
  551. HICON hicon;
  552. HDC hdc = GetDC(NULL);
  553. BITMAPV4HEADER bmh;
  554. void *pixels;
  555. void *maskbits;
  556. int maskbitslen;
  557. ICONINFO ii;
  558. ZeroMemory(&bmh,sizeof(bmh));
  559. bmh.bV4Size = sizeof(bmh);
  560. bmh.bV4Width = width;
  561. bmh.bV4Height = -height;
  562. bmh.bV4Planes = 1;
  563. bmh.bV4BitCount = 32;
  564. bmh.bV4V4Compression = BI_BITFIELDS;
  565. bmh.bV4AlphaMask = 0xFF000000;
  566. bmh.bV4RedMask = 0x00FF0000;
  567. bmh.bV4GreenMask = 0x0000FF00;
  568. bmh.bV4BlueMask = 0x000000FF;
  569. maskbitslen = ((width + (-width)%pad) >> 3) * height;
  570. maskbits = malloc(maskbitslen);
  571. if( maskbits == NULL )
  572. return NULL;
  573. memset(maskbits,0xFF,maskbitslen);
  574. memset(&ii,0,sizeof(ii));
  575. ii.fIcon = FALSE;
  576. ii.xHotspot = (DWORD)hotX;
  577. ii.yHotspot = (DWORD)hotY;
  578. ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
  579. ii.hbmMask = CreateBitmap(width, height, 1, 1, maskbits);
  580. ReleaseDC(NULL, hdc);
  581. free(maskbits);
  582. memcpy(pixels, data, height * width * 4);
  583. hicon = CreateIconIndirect(&ii);
  584. DeleteObject(ii.hbmColor);
  585. DeleteObject(ii.hbmMask);
  586. return hicon;
  587. }
  588. HL_PRIM void HL_NAME(destroy_cursor)( dx_cursor c ) {
  589. DestroyIcon(c);
  590. }
  591. HL_PRIM void HL_NAME(set_cursor)( dx_cursor c ) {
  592. cur_cursor = c;
  593. if( show_cursor )
  594. SetCursor(c);
  595. }
  596. HL_PRIM void HL_NAME(show_cursor)( bool visible ) {
  597. show_cursor = visible;
  598. SetCursor(visible ? cur_cursor : NULL);
  599. }
  600. HL_PRIM bool HL_NAME(is_cursor_visible)() {
  601. return show_cursor;
  602. }
  603. #define TCURSOR _ABSTRACT(dx_cursor)
  604. DEFINE_PRIM(TCURSOR, load_cursor, _I32);
  605. DEFINE_PRIM(TCURSOR, create_cursor, _I32 _I32 _BYTES _I32 _I32);
  606. DEFINE_PRIM(_VOID, destroy_cursor, TCURSOR);
  607. DEFINE_PRIM(_VOID, set_cursor, TCURSOR);
  608. DEFINE_PRIM(_VOID, show_cursor, _BOOL);
  609. DEFINE_PRIM(_BOOL, is_cursor_visible, _NO_ARG);
  610. HL_PRIM vbyte *HL_NAME(detect_keyboard_layout)() {
  611. char q = MapVirtualKey(0x10, MAPVK_VSC_TO_VK);
  612. char w = MapVirtualKey(0x11, MAPVK_VSC_TO_VK);
  613. char y = MapVirtualKey(0x15, MAPVK_VSC_TO_VK);
  614. if (q == 'Q' && w == 'W' && y == 'Y') return "qwerty";
  615. if (q == 'A' && w == 'Z' && y == 'Y') return "azerty";
  616. if (q == 'Q' && w == 'W' && y == 'Z') return "qwertz";
  617. if (q == 'Q' && w == 'Z' && y == 'Y') return "qzerty";
  618. return "unknown";
  619. }
  620. DEFINE_PRIM(_BYTES, detect_keyboard_layout, _NO_ARG);