windows.odin 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. when ODIN_OS == "windows" {
  2. foreign import "system:kernel32.lib"
  3. foreign import "system:user32.lib"
  4. foreign import "system:gdi32.lib"
  5. foreign import "system:winmm.lib"
  6. foreign import "system:shell32.lib"
  7. }
  8. Handle :: rawptr;
  9. Hwnd :: Handle;
  10. Hdc :: Handle;
  11. Hinstance :: Handle;
  12. Hicon :: Handle;
  13. Hcursor :: Handle;
  14. Hmenu :: Handle;
  15. Hbrush :: Handle;
  16. Hgdiobj :: Handle;
  17. Hmodule :: Handle;
  18. Hmonitor :: Handle;
  19. Hrawinput :: Handle;
  20. HKL :: Handle;
  21. Wparam :: uint;
  22. Lparam :: int;
  23. Lresult :: int;
  24. Wnd_Proc :: #type proc "c" (Hwnd, u32, Wparam, Lparam) -> Lresult;
  25. Long_Ptr :: int;
  26. Bool :: i32;
  27. FALSE: Bool : 0;
  28. TRUE: Bool : 1;
  29. Point :: struct #ordered {
  30. x, y: i32,
  31. }
  32. Wnd_Class_Ex_A :: struct #ordered {
  33. size, style: u32,
  34. wnd_proc: Wnd_Proc,
  35. cls_extra, wnd_extra: i32,
  36. instance: Hinstance,
  37. icon: Hicon,
  38. cursor: Hcursor,
  39. background: Hbrush,
  40. menu_name, class_name: ^u8,
  41. sm: Hicon,
  42. }
  43. Wnd_Class_Ex_W :: struct #ordered {
  44. size, style: u32,
  45. wnd_proc: Wnd_Proc,
  46. cls_extra, wnd_extra: i32,
  47. instance: Hinstance,
  48. icon: Hicon,
  49. cursor: Hcursor,
  50. background: Hbrush,
  51. menu_name, class_name: ^u16,
  52. sm: Hicon,
  53. }
  54. Msg :: struct #ordered {
  55. hwnd: Hwnd,
  56. message: u32,
  57. wparam: Wparam,
  58. lparam: Lparam,
  59. time: u32,
  60. pt: Point,
  61. }
  62. Rect :: struct #ordered {
  63. left: i32,
  64. top: i32,
  65. right: i32,
  66. bottom: i32,
  67. }
  68. Filetime :: struct #ordered {
  69. lo, hi: u32,
  70. }
  71. Systemtime :: struct #ordered {
  72. year, month: u16,
  73. day_of_week, day: u16,
  74. hour, minute, second, millisecond: u16,
  75. }
  76. By_Handle_File_Information :: struct #ordered {
  77. file_attributes: u32,
  78. creation_time,
  79. last_access_time,
  80. last_write_time: Filetime,
  81. volume_serial_number,
  82. file_size_high,
  83. file_size_low,
  84. number_of_links,
  85. file_index_high,
  86. file_index_low: u32,
  87. }
  88. File_Attribute_Data :: struct #ordered {
  89. file_attributes: u32,
  90. creation_time,
  91. last_access_time,
  92. last_write_time: Filetime,
  93. file_size_high,
  94. file_size_low: u32,
  95. }
  96. Find_Data :: struct #ordered{
  97. file_attributes: u32,
  98. creation_time: Filetime,
  99. last_access_time: Filetime,
  100. last_write_time: Filetime,
  101. file_size_high: u32,
  102. file_size_low: u32,
  103. reserved0: u32,
  104. reserved1: u32,
  105. file_name: [MAX_PATH]u8,
  106. alternate_file_name: [14]u8,
  107. }
  108. Security_Attributes :: struct #ordered {
  109. length: u32,
  110. security_descriptor: rawptr,
  111. inherit_handle: Bool,
  112. }
  113. Pixel_Format_Descriptor :: struct #ordered {
  114. size,
  115. version,
  116. flags: u32,
  117. pixel_type,
  118. color_bits,
  119. red_bits,
  120. red_shift,
  121. green_bits,
  122. green_shift,
  123. blue_bits,
  124. blue_shift,
  125. alpha_bits,
  126. alpha_shift,
  127. accum_bits,
  128. accum_red_bits,
  129. accum_green_bits,
  130. accum_blue_bits,
  131. accum_alpha_bits,
  132. depth_bits,
  133. stencil_bits,
  134. aux_buffers,
  135. layer_type,
  136. reserved: u8,
  137. layer_mask,
  138. visible_mask,
  139. damage_mask: u32,
  140. }
  141. Critical_Section :: struct #ordered {
  142. debug_info: ^Critical_Section_Debug,
  143. lock_count: i32,
  144. recursion_count: i32,
  145. owning_thread: Handle,
  146. lock_semaphore: Handle,
  147. spin_count: ^u32,
  148. }
  149. Critical_Section_Debug :: struct #ordered {
  150. typ: u16,
  151. creator_back_trace_index: u16,
  152. critical_section: ^Critical_Section,
  153. process_locks_list: ^List_Entry,
  154. entry_count: u32,
  155. contention_count: u32,
  156. flags: u32,
  157. creator_back_trace_index_high: u16,
  158. spare_word: u16,
  159. }
  160. List_Entry :: struct #ordered {flink, blink: ^List_Entry};
  161. Raw_Input_Device :: struct #ordered {
  162. usage_page: u16,
  163. usage: u16,
  164. flags: u32,
  165. wnd_target: Hwnd,
  166. }
  167. Raw_Input_Header :: struct #ordered {
  168. kind: u32,
  169. size: u32,
  170. device: Handle,
  171. wparam: Wparam,
  172. }
  173. Raw_HID :: struct #ordered {
  174. size_hid: u32,
  175. count: u32,
  176. raw_data: [1]u8,
  177. }
  178. Raw_Keyboard :: struct #ordered {
  179. make_code: u16,
  180. flags: u16,
  181. reserved: u16,
  182. vkey: u16,
  183. message: u32,
  184. extra_information: u32,
  185. }
  186. Raw_Mouse :: struct #ordered {
  187. flags: u16,
  188. using data: struct #raw_union {
  189. buttons: u32,
  190. using _: struct #ordered {
  191. button_flags: u16,
  192. button_data: u16,
  193. },
  194. },
  195. raw_buttons: u32,
  196. last_x: i32,
  197. last_y: i32,
  198. extra_information: u32,
  199. }
  200. Raw_Input :: struct #ordered {
  201. using header: Raw_Input_Header,
  202. data: struct #raw_union {
  203. mouse: Raw_Mouse,
  204. keyboard: Raw_Keyboard,
  205. hid: Raw_HID,
  206. },
  207. }
  208. MAPVK_VK_TO_VSC :: 0;
  209. MAPVK_VSC_TO_VK :: 1;
  210. MAPVK_VK_TO_CHAR :: 2;
  211. MAPVK_VSC_TO_VK_EX :: 3;
  212. INVALID_HANDLE :: Handle(~uintptr(0));
  213. CREATE_SUSPENDED :: 0x00000004;
  214. STACK_SIZE_PARAM_IS_A_RESERVATION :: 0x00010000;
  215. WAIT_ABANDONED :: 0x00000080;
  216. WAIT_OBJECT_0 :: 0;
  217. WAIT_TIMEOUT :: 0x00000102;
  218. WAIT_FAILED :: 0xffffffff;
  219. CS_VREDRAW :: 0x0001;
  220. CS_HREDRAW :: 0x0002;
  221. CS_OWNDC :: 0x0020;
  222. CW_USEDEFAULT :: -0x80000000;
  223. WS_OVERLAPPED :: 0;
  224. WS_MAXIMIZEBOX :: 0x00010000;
  225. WS_MINIMIZEBOX :: 0x00020000;
  226. WS_THICKFRAME :: 0x00040000;
  227. WS_SYSMENU :: 0x00080000;
  228. WS_BORDER :: 0x00800000;
  229. WS_CAPTION :: 0x00C00000;
  230. WS_VISIBLE :: 0x10000000;
  231. WS_POPUP :: 0x80000000;
  232. WS_MAXIMIZE :: 0x01000000;
  233. WS_MINIMIZE :: 0x20000000;
  234. WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX;
  235. WS_POPUPWINDOW :: WS_POPUP | WS_BORDER | WS_SYSMENU;
  236. WM_ACTIVATE :: 0x0006;
  237. WM_ACTIVATEAPP :: 0x001C;
  238. WM_CHAR :: 0x0102;
  239. WM_CLOSE :: 0x0010;
  240. WM_CREATE :: 0x0001;
  241. WM_DESTROY :: 0x0002;
  242. WM_INPUT :: 0x00ff;
  243. WM_KEYDOWN :: 0x0100;
  244. WM_KEYUP :: 0x0101;
  245. WM_KILLFOCUS :: 0x0008;
  246. WM_QUIT :: 0x0012;
  247. WM_SETCURSOR :: 0x0020;
  248. WM_SETFOCUS :: 0x0007;
  249. WM_SIZE :: 0x0005;
  250. WM_SIZING :: 0x0214;
  251. WM_SYSKEYDOWN :: 0x0104;
  252. WM_SYSKEYUP :: 0x0105;
  253. WM_USER :: 0x0400;
  254. WM_WINDOWPOSCHANGED :: 0x0047;
  255. WM_MOUSEWHEEL :: 0x020A;
  256. WM_MOUSEMOVE :: 0x0200;
  257. WM_LBUTTONDOWN :: 0x0201;
  258. WM_LBUTTONUP :: 0x0202;
  259. WM_LBUTTONDBLCLK :: 0x0203;
  260. WM_RBUTTONDOWN :: 0x0204;
  261. WM_RBUTTONUP :: 0x0205;
  262. WM_RBUTTONDBLCLK :: 0x0206;
  263. WM_MBUTTONDOWN :: 0x0207;
  264. WM_MBUTTONUP :: 0x0208;
  265. WM_MBUTTONDBLCLK :: 0x0209;
  266. PM_NOREMOVE :: 0x0000;
  267. PM_REMOVE :: 0x0001;
  268. PM_NOYIELD :: 0x0002;
  269. BLACK_BRUSH :: 4;
  270. SM_CXSCREEN :: 0;
  271. SM_CYSCREEN :: 1;
  272. SW_SHOW :: 5;
  273. COLOR_BACKGROUND :: Hbrush(uintptr(1));
  274. INVALID_SET_FILE_POINTER :: ~u32(0);
  275. HEAP_ZERO_MEMORY :: 0x00000008;
  276. INFINITE :: 0xffffffff;
  277. GWL_EXSTYLE :: -20;
  278. GWLP_HINSTANCE :: -6;
  279. GWLP_ID :: -12;
  280. GWL_STYLE :: -16;
  281. GWLP_USERDATA :: -21;
  282. GWLP_WNDPROC :: -4;
  283. Hwnd_TOP :: Hwnd(uintptr(0));
  284. BI_RGB :: 0;
  285. DIB_RGB_COLORS :: 0x00;
  286. SRCCOPY: u32 : 0x00cc0020;
  287. MONITOR_DEFAULTTONULL :: 0x00000000;
  288. MONITOR_DEFAULTTOPRIMARY :: 0x00000001;
  289. MONITOR_DEFAULTTONEAREST :: 0x00000002;
  290. SWP_FRAMECHANGED :: 0x0020;
  291. SWP_NOOWNERZORDER :: 0x0200;
  292. SWP_NOZORDER :: 0x0004;
  293. SWP_NOSIZE :: 0x0001;
  294. SWP_NOMOVE :: 0x0002;
  295. // Raw Input
  296. RID_HEADER :: 0x10000005;
  297. RID_INPUT :: 0x10000003;
  298. RIDEV_APPKEYS :: 0x00000400;
  299. RIDEV_CAPTUREMOUSE :: 0x00000200;
  300. RIDEV_DEVNOTIFY :: 0x00002000;
  301. RIDEV_EXCLUDE :: 0x00000010;
  302. RIDEV_EXINPUTSINK :: 0x00001000;
  303. RIDEV_INPUTSINK :: 0x00000100;
  304. RIDEV_NOHOTKEYS :: 0x00000200;
  305. RIDEV_NOLEGACY :: 0x00000030;
  306. RIDEV_PAGEONLY :: 0x00000020;
  307. RIDEV_REMOVE :: 0x00000001;
  308. RIM_TYPEMOUSE :: 0;
  309. RIM_TYPEKEYBOARD :: 1;
  310. RIM_TYPEHID :: 2;
  311. MOUSE_ATTRIBUTES_CHANGED :: 0x04;
  312. MOUSE_MOVE_RELATIVE :: 0;
  313. MOUSE_MOVE_ABSOLUTE :: 1;
  314. MOUSE_VIRTUAL_DESKTOP :: 0x02;
  315. RI_MOUSE_BUTTON_1_DOWN :: 0x0001;
  316. RI_MOUSE_BUTTON_1_UP :: 0x0002;
  317. RI_MOUSE_BUTTON_2_DOWN :: 0x0004;
  318. RI_MOUSE_BUTTON_2_UP :: 0x0008;
  319. RI_MOUSE_BUTTON_3_DOWN :: 0x0010;
  320. RI_MOUSE_BUTTON_3_UP :: 0x0020;
  321. RI_MOUSE_BUTTON_4_DOWN :: 0x0040;
  322. RI_MOUSE_BUTTON_4_UP :: 0x0080;
  323. RI_MOUSE_BUTTON_5_DOWN :: 0x0100;
  324. RI_MOUSE_BUTTON_5_UP :: 0x0200;
  325. RI_MOUSE_LEFT_BUTTON_DOWN :: 0x0001;
  326. RI_MOUSE_LEFT_BUTTON_UP :: 0x0002;
  327. RI_MOUSE_MIDDLE_BUTTON_DOWN :: 0x0010;
  328. RI_MOUSE_MIDDLE_BUTTON_UP :: 0x0020;
  329. RI_MOUSE_RIGHT_BUTTON_DOWN :: 0x0004;
  330. RI_MOUSE_RIGHT_BUTTON_UP :: 0x0008;
  331. RI_MOUSE_WHEEL :: 0x0400;
  332. RI_KEY_MAKE :: 0x00;
  333. RI_KEY_BREAK :: 0x01;
  334. RI_KEY_E0 :: 0x02;
  335. RI_KEY_E1 :: 0x04;
  336. RI_KEY_TERMSRV_SET_LED :: 0x08;
  337. RI_KEY_TERMSRV_SHADOW :: 0x10;
  338. // Windows OpenGL
  339. PFD_TYPE_RGBA :: 0;
  340. PFD_TYPE_COLORINDEX :: 1;
  341. PFD_MAIN_PLANE :: 0;
  342. PFD_OVERLAY_PLANE :: 1;
  343. PFD_UNDERLAY_PLANE :: -1;
  344. PFD_DOUBLEBUFFER :: 1;
  345. PFD_STEREO :: 2;
  346. PFD_DRAW_TO_WINDOW :: 4;
  347. PFD_DRAW_TO_BITMAP :: 8;
  348. PFD_SUPPORT_GDI :: 16;
  349. PFD_SUPPORT_OPENGL :: 32;
  350. PFD_GENERIC_FORMAT :: 64;
  351. PFD_NEED_PALETTE :: 128;
  352. PFD_NEED_SYSTEM_PALETTE :: 0x00000100;
  353. PFD_SWAP_EXCHANGE :: 0x00000200;
  354. PFD_SWAP_COPY :: 0x00000400;
  355. PFD_SWAP_LAYER_BUFFERS :: 0x00000800;
  356. PFD_GENERIC_ACCELERATED :: 0x00001000;
  357. PFD_DEPTH_DONTCARE :: 0x20000000;
  358. PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000;
  359. PFD_STEREO_DONTCARE :: 0x80000000;
  360. GET_FILEEX_INFO_LEVELS :: i32;
  361. GetFileExInfoStandard: GET_FILEEX_INFO_LEVELS : 0;
  362. GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1;
  363. @(default_calling_convention = "std")
  364. foreign kernel32 {
  365. @(link_name="GetLastError") get_last_error :: proc() -> i32 ---;
  366. @(link_name="ExitProcess") exit_process :: proc(exit_code: u32) ---;
  367. @(link_name="GetModuleHandleA") get_module_handle_a :: proc(module_name: ^u8) -> Hinstance ---;
  368. @(link_name="GetModuleHandleW") get_module_handle_w :: proc(module_name: ^u16) -> Hinstance ---;
  369. @(link_name="Sleep") sleep :: proc(ms: i32) -> i32 ---;
  370. @(link_name="QueryPerformanceFrequency") query_performance_frequency :: proc(result: ^i64) -> i32 ---;
  371. @(link_name="QueryPerformanceCounter") query_performance_counter :: proc(result: ^i64) -> i32 ---;
  372. @(link_name="OutputDebugStringA") output_debug_string_a :: proc(c_str: ^u8) ---;
  373. @(link_name="GetCommandLineA") get_command_line_a :: proc() -> ^u8 ---;
  374. @(link_name="GetCommandLineW") get_command_line_w :: proc() -> ^u16 ---;
  375. @(link_name="GetSystemMetrics") get_system_metrics :: proc(index: i32) -> i32 ---;
  376. @(link_name="GetCurrentThreadId") get_current_thread_id :: proc() -> u32 ---;
  377. @(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) ---;
  378. @(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---;
  379. @(link_name="FileTimeToSystemTime") file_time_to_system_time :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---;
  380. @(link_name="SystemTimeToFileTime") system_time_to_file_time :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---;
  381. @(link_name="CloseHandle") close_handle :: proc(h: Handle) -> i32 ---;
  382. @(link_name="GetStdHandle") get_std_handle :: proc(h: i32) -> Handle ---;
  383. @(link_name="CreateFileA")
  384. create_file_a :: proc(filename: ^u8, desired_access, share_module: u32,
  385. security: rawptr,
  386. creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
  387. @(link_name="ReadFile") read_file :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---;
  388. @(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---;
  389. @(link_name="GetFileSizeEx") get_file_size_ex :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
  390. @(link_name="GetFileAttributesA") get_file_attributes_a :: proc(filename: ^u8) -> u32 ---;
  391. @(link_name="GetFileAttributesExA") get_file_attributes_ex_a :: proc(filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---;
  392. @(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
  393. @(link_name="GetFileType") get_file_type :: proc(file_handle: Handle) -> u32 ---;
  394. @(link_name="SetFilePointer") set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---;
  395. @(link_name="SetHandleInformation") set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool ---;
  396. @(link_name="FindFirstFileA") find_first_file_a :: proc(file_name : ^u8, data : ^Find_Data) -> Handle ---;
  397. @(link_name="FindNextFileA") find_next_file_a :: proc(file : Handle, data : ^Find_Data) -> Bool ---;
  398. @(link_name="FindClose") find_close :: proc(file : Handle) -> Bool ---;
  399. @(link_name="HeapAlloc") heap_alloc :: proc(h: Handle, flags: u32, bytes: int) -> rawptr ---;
  400. @(link_name="HeapReAlloc") heap_realloc :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---;
  401. @(link_name="HeapFree") heap_free :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool ---;
  402. @(link_name="GetProcessHeap") get_process_heap :: proc() -> Handle ---;
  403. @(link_name="CreateSemaphoreA") create_semaphore_a :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: ^u8) -> Handle ---;
  404. @(link_name="ReleaseSemaphore") release_semaphore :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---;
  405. @(link_name="WaitForSingleObject") wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 ---;
  406. }
  407. @(default_calling_convention = "c")
  408. foreign kernel32 {
  409. @(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 ---;
  410. @(link_name="InterlockedExchange") interlocked_exchange :: proc(dst: ^i32, desired: i32) -> i32 ---;
  411. @(link_name="InterlockedExchangeAdd") interlocked_exchange_add :: proc(dst: ^i32, desired: i32) -> i32 ---;
  412. @(link_name="InterlockedAnd") interlocked_and :: proc(dst: ^i32, desired: i32) -> i32 ---;
  413. @(link_name="InterlockedOr") interlocked_or :: proc(dst: ^i32, desired: i32) -> i32 ---;
  414. @(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 ---;
  415. @(link_name="InterlockedExchange64") interlocked_exchange64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  416. @(link_name="InterlockedExchangeAdd64") interlocked_exchange_add64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  417. @(link_name="InterlockedAnd64") interlocked_and64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  418. @(link_name="InterlockedOr64") interlocked_or64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  419. }
  420. @(default_calling_convention = "std")
  421. foreign kernel32 {
  422. @(link_name="_mm_pause") mm_pause :: proc() ---;
  423. @(link_name="ReadWriteBarrier") read_write_barrier :: proc() ---;
  424. @(link_name="WriteBarrier") write_barrier :: proc() ---;
  425. @(link_name="ReadBarrier") read_barrier :: proc() ---;
  426. @(link_name="CreateThread")
  427. create_thread :: proc(thread_attributes: ^Security_Attributes, stack_size: int, start_routine: rawptr,
  428. parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---;
  429. @(link_name="ResumeThread") resume_thread :: proc(thread: Handle) -> u32 ---;
  430. @(link_name="GetThreadPriority") get_thread_priority :: proc(thread: Handle) -> i32 ---;
  431. @(link_name="SetThreadPriority") set_thread_priority :: proc(thread: Handle, priority: i32) -> Bool ---;
  432. @(link_name="GetExitCodeThread") get_exit_code_thread :: proc(thread: Handle, exit_code: ^u32) -> Bool ---;
  433. @(link_name="InitializeCriticalSection") initialize_critical_section :: proc(critical_section: ^Critical_Section) ---;
  434. @(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) ---;
  435. @(link_name="DeleteCriticalSection") delete_critical_section :: proc(critical_section: ^Critical_Section) ---;
  436. @(link_name="SetCriticalSectionSpinCount") set_critical_section_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) -> u32 ---;
  437. @(link_name="TryEnterCriticalSection") try_enter_critical_section :: proc(critical_section: ^Critical_Section) -> Bool ---;
  438. @(link_name="EnterCriticalSection") enter_critical_section :: proc(critical_section: ^Critical_Section) ---;
  439. @(link_name="LeaveCriticalSection") leave_critical_section :: proc(critical_section: ^Critical_Section) ---;
  440. @(link_name="CreateEventA") create_event_a :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: ^u8) -> Handle ---;
  441. @(link_name="LoadLibraryA") load_library_a :: proc(c_str: ^u8) -> Hmodule ---;
  442. @(link_name="LoadLibraryW") load_library_a :: proc(c_str: ^u16) -> Hmodule ---;
  443. @(link_name="FreeLibrary") free_library :: proc(h: Hmodule) ---;
  444. @(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: ^u8) -> rawptr ---;
  445. }
  446. @(default_calling_convention = "std")
  447. foreign user32 {
  448. @(link_name="GetDesktopWindow") get_desktop_window :: proc() -> Hwnd ---;
  449. @(link_name="ShowCursor") show_cursor :: proc(show : Bool) ---;
  450. @(link_name="GetCursorPos") get_cursor_pos :: proc(p: ^Point) -> Bool ---;
  451. @(link_name="SetCursorPos") set_cursor_pos :: proc(x, y: i32) -> Bool ---;
  452. @(link_name="ScreenToClient") screen_to_client :: proc(h: Hwnd, p: ^Point) -> Bool ---;
  453. @(link_name="ClientToScreen") client_to_screen :: proc(h: Hwnd, p: ^Point) -> Bool ---;
  454. @(link_name="PostQuitMessage") post_quit_message :: proc(exit_code: i32) ---;
  455. @(link_name="SetWindowTextA") set_window_text_a :: proc(hwnd: Hwnd, c_string: ^u8) -> Bool ---;
  456. @(link_name="RegisterClassExA") register_class_ex_a :: proc(wc: ^Wnd_Class_Ex_A) -> i16 ---;
  457. @(link_name="RegisterClassExW") register_class_ex_w :: proc(wc: ^Wnd_Class_Ex_W) -> i16 ---;
  458. @(link_name="CreateWindowExA")
  459. create_window_ex_a :: proc(ex_style: u32,
  460. class_name, title: ^u8,
  461. style: u32,
  462. x, y, w, h: i32,
  463. parent: Hwnd, menu: Hmenu, instance: Hinstance,
  464. param: rawptr) -> Hwnd ---;
  465. @(link_name="CreateWindowExW")
  466. create_window_ex_w :: proc(ex_style: u32,
  467. class_name, title: ^u16,
  468. style: u32,
  469. x, y, w, h: i32,
  470. parent: Hwnd, menu: Hmenu, instance: Hinstance,
  471. param: rawptr) -> Hwnd ---;
  472. @(link_name="ShowWindow") show_window :: proc(hwnd: Hwnd, cmd_show: i32) -> Bool ---;
  473. @(link_name="TranslateMessage") translate_message :: proc(msg: ^Msg) -> Bool ---;
  474. @(link_name="DispatchMessageA") dispatch_message_a :: proc(msg: ^Msg) -> Lresult ---;
  475. @(link_name="DispatchMessageW") dispatch_message_w :: proc(msg: ^Msg) -> Lresult ---;
  476. @(link_name="UpdateWindow") update_window :: proc(hwnd: Hwnd) -> Bool ---;
  477. @(link_name="GetMessageA") get_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool ---;
  478. @(link_name="GetMessageW") get_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool ---;
  479. @(link_name="PeekMessageA") peek_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---;
  480. @(link_name="PeekMessageW") peek_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---;
  481. @(link_name="PostMessageA") post_message :: proc(hwnd: Hwnd, msg, wparam, lparam : u32) -> Bool ---;
  482. @(link_name="DefWindowProcA") def_window_proc_a :: proc(hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult ---;
  483. @(link_name="AdjustWindowRect") adjust_window_rect :: proc(rect: ^Rect, style: u32, menu: Bool) -> Bool ---;
  484. @(link_name="GetActiveWindow") get_active_window :: proc() -> Hwnd ---;
  485. @(link_name="DestroyWindow") destroy_window :: proc(wnd: Hwnd) -> Bool ---;
  486. @(link_name="DescribePixelFormat") describe_pixel_format :: proc(dc: Hdc, pixel_format: i32, bytes: u32, pfd: ^Pixel_Format_Descriptor) -> i32 ---;
  487. @(link_name="GetMonitor_InfoA") get_monitor_info_a :: proc(monitor: Hmonitor, mi: ^Monitor_Info) -> Bool ---;
  488. @(link_name="MonitorFromWindow") monitor_from_window :: proc(wnd: Hwnd, flags : u32) -> Hmonitor ---;
  489. @(link_name="SetWindowPos") set_window_pos :: proc(wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) ---;
  490. @(link_name="GetWindowPlacement") get_window_placement :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---;
  491. @(link_name="SetWindowPlacement") set_window_placement :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---;
  492. @(link_name="GetWindowRect") get_window_rect :: proc(wnd: Hwnd, rect: ^Rect) -> Bool ---;
  493. @(link_name="GetWindowLongPtrA") get_window_long_ptr_a :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---;
  494. @(link_name="SetWindowLongPtrA") set_window_long_ptr_a :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---;
  495. @(link_name="GetWindowLongPtrW") get_window_long_ptr_w :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---;
  496. @(link_name="SetWindowLongPtrW") set_window_long_ptr_w :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---;
  497. @(link_name="GetWindowText") get_window_text :: proc(wnd: Hwnd, str: ^u8, maxCount: i32) -> i32 ---;
  498. @(link_name="GetClientRect") get_client_rect :: proc(hwnd: Hwnd, rect: ^Rect) -> Bool ---;
  499. @(link_name="GetDC") get_dc :: proc(h: Hwnd) -> Hdc ---;
  500. @(link_name="ReleaseDC") release_dc :: proc(wnd: Hwnd, hdc: Hdc) -> i32 ---;
  501. @(link_name="MapVirtualKeyA") map_virtual_key_a :: proc(scancode : u32, map_type : u32) -> u32 ---;
  502. @(link_name="MapVirtualKeyW") map_virtual_key_w :: proc(scancode : u32, map_type : u32) -> u32 ---;
  503. @(link_name="GetKeyState") get_key_state :: proc(v_key: i32) -> i16 ---;
  504. @(link_name="GetAsyncKeyState") get_async_key_state :: proc(v_key: i32) -> i16 ---;
  505. @(link_name="SetForegroundWindow") set_foreground_window :: proc(h: Hwnd) -> Bool ---;
  506. @(link_name="SetFocus") set_focus :: proc(h: Hwnd) -> Hwnd ---;
  507. @(link_name="RegisterRawInputDevices") register_raw_input_devices :: proc(raw_input_device: ^Raw_Input_Device, num_devices, size: u32) -> Bool ---;
  508. @(link_name="GetRawInputData") get_raw_input_data :: proc(raw_input: Hrawinput, command: u32, data: rawptr, size: ^u32, size_header: u32) -> u32 ---;
  509. @(link_name="MapVirtualKeyExW") map_virtual_key_ex_w :: proc(code, map_type: u32, hkl: HKL) ---;
  510. @(link_name="MapVirtualKeyExA") map_virtual_key_ex_a :: proc(code, map_type: u32, hkl: HKL) ---;
  511. }
  512. @(default_calling_convention = "std")
  513. foreign gdi32 {
  514. @(link_name="GetStockObject") get_stock_object :: proc(fn_object: i32) -> Hgdiobj ---;
  515. @(link_name="StretchDIBits")
  516. stretch_dibits :: proc(hdc: Hdc,
  517. x_dst, y_dst, width_dst, height_dst: i32,
  518. x_src, y_src, width_src, header_src: i32,
  519. bits: rawptr, bits_info: ^Bitmap_Info,
  520. usage: u32,
  521. rop: u32) -> i32 ---;
  522. @(link_name="SetPixelFormat") set_pixel_format :: proc(hdc: Hdc, pixel_format: i32, pfd: ^Pixel_Format_Descriptor) -> Bool ---;
  523. @(link_name="ChoosePixelFormat") choose_pixel_format :: proc(hdc: Hdc, pfd: ^Pixel_Format_Descriptor) -> i32 ---;
  524. @(link_name="SwapBuffers") swap_buffers :: proc(hdc: Hdc) -> Bool ---;
  525. }
  526. @(default_calling_convention = "std")
  527. foreign shell32 {
  528. @(link_name="CommandLineToArgvW") command_line_to_argv_w :: proc(cmd_list: ^u16, num_args: ^i32) -> ^^u16 ---;
  529. }
  530. @(default_calling_convention = "std")
  531. foreign winmm {
  532. @(link_name="timeGetTime") time_get_time :: proc() -> u32 ---;
  533. }
  534. get_query_performance_frequency :: proc() -> i64 {
  535. r: i64;
  536. query_performance_frequency(&r);
  537. return r;
  538. }
  539. HIWORD :: proc(wParam: Wparam) -> u16 { return u16((u32(wParam) >> 16) & 0xffff); }
  540. HIWORD :: proc(lParam: Lparam) -> u16 { return u16((u32(lParam) >> 16) & 0xffff); }
  541. LOWORD :: proc(wParam: Wparam) -> u16 { return u16(wParam); }
  542. LOWORD :: proc(lParam: Lparam) -> u16 { return u16(lParam); }
  543. is_key_down :: inline proc(key: Key_Code) -> bool do return get_async_key_state(i32(key)) < 0;
  544. MAX_PATH :: 0x00000104;
  545. HANDLE_FLAG_INHERIT :: 1;
  546. HANDLE_FLAG_PROTECT_FROM_CLOSE :: 2;
  547. FILE_BEGIN :: 0;
  548. FILE_CURRENT :: 1;
  549. FILE_END :: 2;
  550. FILE_SHARE_READ :: 0x00000001;
  551. FILE_SHARE_WRITE :: 0x00000002;
  552. FILE_SHARE_DELETE :: 0x00000004;
  553. FILE_GENERIC_ALL :: 0x10000000;
  554. FILE_GENERIC_EXECUTE :: 0x20000000;
  555. FILE_GENERIC_WRITE :: 0x40000000;
  556. FILE_GENERIC_READ :: 0x80000000;
  557. FILE_APPEND_DATA :: 0x0004;
  558. STD_INPUT_HANDLE :: -10;
  559. STD_OUTPUT_HANDLE :: -11;
  560. STD_ERROR_HANDLE :: -12;
  561. CREATE_NEW :: 1;
  562. CREATE_ALWAYS :: 2;
  563. OPEN_EXISTING :: 3;
  564. OPEN_ALWAYS :: 4;
  565. TRUNCATE_EXISTING :: 5;
  566. INVALID_FILE_ATTRIBUTES :: -1;
  567. FILE_ATTRIBUTE_READONLY :: 0x00000001;
  568. FILE_ATTRIBUTE_HIDDEN :: 0x00000002;
  569. FILE_ATTRIBUTE_SYSTEM :: 0x00000004;
  570. FILE_ATTRIBUTE_DIRECTORY :: 0x00000010;
  571. FILE_ATTRIBUTE_ARCHIVE :: 0x00000020;
  572. FILE_ATTRIBUTE_DEVICE :: 0x00000040;
  573. FILE_ATTRIBUTE_NORMAL :: 0x00000080;
  574. FILE_ATTRIBUTE_TEMPORARY :: 0x00000100;
  575. FILE_ATTRIBUTE_SPARSE_FILE :: 0x00000200;
  576. FILE_ATTRIBUTE_REPARSE_Point :: 0x00000400;
  577. FILE_ATTRIBUTE_COMPRESSED :: 0x00000800;
  578. FILE_ATTRIBUTE_OFFLINE :: 0x00001000;
  579. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED :: 0x00002000;
  580. FILE_ATTRIBUTE_ENCRYPTED :: 0x00004000;
  581. FILE_TYPE_DISK :: 0x0001;
  582. FILE_TYPE_CHAR :: 0x0002;
  583. FILE_TYPE_PIPE :: 0x0003;
  584. Monitor_Info :: struct #ordered {
  585. size: u32,
  586. monitor: Rect,
  587. work: Rect,
  588. flags: u32,
  589. }
  590. Window_Placement :: struct #ordered {
  591. length: u32,
  592. flags: u32,
  593. show_cmd: u32,
  594. min_pos: Point,
  595. max_pos: Point,
  596. normal_pos: Rect,
  597. }
  598. Bitmap_Info_Header :: struct #ordered {
  599. size: u32,
  600. width, height: i32,
  601. planes, bit_count: i16,
  602. compression: u32,
  603. size_image: u32,
  604. x_pels_per_meter: i32,
  605. y_pels_per_meter: i32,
  606. clr_used: u32,
  607. clr_important: u32,
  608. }
  609. Bitmap_Info :: struct #ordered {
  610. using header: Bitmap_Info_Header,
  611. colors: [1]Rgb_Quad,
  612. }
  613. Rgb_Quad :: struct #ordered {blue, green, red, reserved: u8}
  614. Key_Code :: enum i32 {
  615. Unknown = 0x00,
  616. Lbutton = 0x01,
  617. Rbutton = 0x02,
  618. Cancel = 0x03,
  619. Mbutton = 0x04,
  620. Xbutton1 = 0x05,
  621. Xbutton2 = 0x06,
  622. Back = 0x08,
  623. Tab = 0x09,
  624. Clear = 0x0C,
  625. Return = 0x0D,
  626. Shift = 0x10,
  627. Control = 0x11,
  628. Menu = 0x12,
  629. Pause = 0x13,
  630. Capital = 0x14,
  631. Kana = 0x15,
  632. Hangeul = 0x15,
  633. Hangul = 0x15,
  634. Junja = 0x17,
  635. Final = 0x18,
  636. Hanja = 0x19,
  637. Kanji = 0x19,
  638. Escape = 0x1B,
  639. Convert = 0x1C,
  640. NonConvert = 0x1D,
  641. Accept = 0x1E,
  642. ModeChange = 0x1F,
  643. Space = 0x20,
  644. Prior = 0x21,
  645. Next = 0x22,
  646. End = 0x23,
  647. Home = 0x24,
  648. Left = 0x25,
  649. Up = 0x26,
  650. Right = 0x27,
  651. Down = 0x28,
  652. Select = 0x29,
  653. Print = 0x2A,
  654. Execute = 0x2B,
  655. Snapshot = 0x2C,
  656. Insert = 0x2D,
  657. Delete = 0x2E,
  658. Help = 0x2F,
  659. Num0 = '0',
  660. Num1 = '1',
  661. Num2 = '2',
  662. Num3 = '3',
  663. Num4 = '4',
  664. Num5 = '5',
  665. Num6 = '6',
  666. Num7 = '7',
  667. Num8 = '8',
  668. Num9 = '9',
  669. A = 'A',
  670. B = 'B',
  671. C = 'C',
  672. D = 'D',
  673. E = 'E',
  674. F = 'F',
  675. G = 'G',
  676. H = 'H',
  677. I = 'I',
  678. J = 'J',
  679. K = 'K',
  680. L = 'L',
  681. M = 'M',
  682. N = 'N',
  683. O = 'O',
  684. P = 'P',
  685. Q = 'Q',
  686. R = 'R',
  687. S = 'S',
  688. T = 'T',
  689. U = 'U',
  690. V = 'V',
  691. W = 'W',
  692. X = 'X',
  693. Y = 'Y',
  694. Z = 'Z',
  695. Lwin = 0x5B,
  696. Rwin = 0x5C,
  697. Apps = 0x5D,
  698. Numpad0 = 0x60,
  699. Numpad1 = 0x61,
  700. Numpad2 = 0x62,
  701. Numpad3 = 0x63,
  702. Numpad4 = 0x64,
  703. Numpad5 = 0x65,
  704. Numpad6 = 0x66,
  705. Numpad7 = 0x67,
  706. Numpad8 = 0x68,
  707. Numpad9 = 0x69,
  708. Multiply = 0x6A,
  709. Add = 0x6B,
  710. Separator = 0x6C,
  711. Subtract = 0x6D,
  712. Decimal = 0x6E,
  713. Divide = 0x6F,
  714. F1 = 0x70,
  715. F2 = 0x71,
  716. F3 = 0x72,
  717. F4 = 0x73,
  718. F5 = 0x74,
  719. F6 = 0x75,
  720. F7 = 0x76,
  721. F8 = 0x77,
  722. F9 = 0x78,
  723. F10 = 0x79,
  724. F11 = 0x7A,
  725. F12 = 0x7B,
  726. F13 = 0x7C,
  727. F14 = 0x7D,
  728. F15 = 0x7E,
  729. F16 = 0x7F,
  730. F17 = 0x80,
  731. F18 = 0x81,
  732. F19 = 0x82,
  733. F20 = 0x83,
  734. F21 = 0x84,
  735. F22 = 0x85,
  736. F23 = 0x86,
  737. F24 = 0x87,
  738. Numlock = 0x90,
  739. Scroll = 0x91,
  740. Lshift = 0xA0,
  741. Rshift = 0xA1,
  742. Lcontrol = 0xA2,
  743. Rcontrol = 0xA3,
  744. Lmenu = 0xA4,
  745. Rmenu = 0xA5,
  746. ProcessKey = 0xE5,
  747. Attn = 0xF6,
  748. Crsel = 0xF7,
  749. Exsel = 0xF8,
  750. Ereof = 0xF9,
  751. Play = 0xFA,
  752. Zoom = 0xFB,
  753. Noname = 0xFC,
  754. Pa1 = 0xFD,
  755. OemClear = 0xFE,
  756. }