diff --git a/build.bat b/build.bat index c01424057..91dc44c8f 100644 --- a/build.bat +++ b/build.bat @@ -42,7 +42,7 @@ del *.ilk > NUL 2> NUL cl %compiler_settings% "src\main.cpp" ^ /link %linker_settings% -OUT:%exe_name% ^ - && odin run examples/demo.odin + && odin check examples/demo del *.obj > NUL 2> NUL diff --git a/core/atomics.odin b/core/atomics/atomics_windows.odin similarity index 99% rename from core/atomics.odin rename to core/atomics/atomics_windows.odin index a95f046f7..1a510290d 100644 --- a/core/atomics.odin +++ b/core/atomics/atomics_windows.odin @@ -1,3 +1,5 @@ +package atomics + // TODO(bill): Use assembly instead here to implement atomics // Inline vs external file? diff --git a/core/bits.odin b/core/bits/bits.odin similarity index 99% rename from core/bits.odin rename to core/bits/bits.odin index ed7d7a3cf..0e0f21db2 100644 --- a/core/bits.odin +++ b/core/bits/bits.odin @@ -1,3 +1,5 @@ +package bits + U8_MIN :: u8(0); U16_MIN :: u16(0); U32_MIN :: u32(0); diff --git a/core/c.odin b/core/c/c.odin similarity index 98% rename from core/c.odin rename to core/c/c.odin index 40be8acfb..a9507194f 100644 --- a/core/c.odin +++ b/core/c/c.odin @@ -1,3 +1,5 @@ +package c + CHAR_BIT :: 8; c_bool :: bool; diff --git a/core/decimal.odin b/core/decimal/decimal.odin similarity index 99% rename from core/decimal.odin rename to core/decimal/decimal.odin index 7d324ddcc..497c51918 100644 --- a/core/decimal.odin +++ b/core/decimal/decimal.odin @@ -1,5 +1,6 @@ // Multiple precision decimal numbers // NOTE: This is only for floating point printing and nothing else +package decimal Decimal :: struct { digits: [384]byte, // big-endian digits diff --git a/core/fmt.odin b/core/fmt/fmt.odin similarity index 99% rename from core/fmt.odin rename to core/fmt/fmt.odin index 743c43222..47b30661b 100644 --- a/core/fmt.odin +++ b/core/fmt/fmt.odin @@ -1,9 +1,11 @@ -import "core:os.odin" -import "core:mem.odin" -import "core:utf8.odin" -import "core:types.odin" -import "core:strconv.odin" -import "core:raw.odin" +package fmt + +import "core:os" +import "core:mem" +import "core:unicode/utf8" +import "core:types" +import "core:strconv" +import "core:raw" _BUFFER_SIZE :: 1<<12; diff --git a/core/hash.odin b/core/hash/hash.odin similarity index 99% rename from core/hash.odin rename to core/hash/hash.odin index 4f1664040..6addcd7f8 100644 --- a/core/hash.odin +++ b/core/hash/hash.odin @@ -1,4 +1,6 @@ -import "core:mem.odin" +package hash + +import "core:mem" adler32 :: proc(data: []byte) -> u32 { ADLER_CONST :: 65521; diff --git a/core/math.odin b/core/math/math.odin similarity index 99% rename from core/math.odin rename to core/math/math.odin index c67cc731b..e895ceb92 100644 --- a/core/math.odin +++ b/core/math/math.odin @@ -1,3 +1,5 @@ +package math + TAU :: 6.28318530717958647692528676655900576; PI :: 3.14159265358979323846264338327950288; diff --git a/core/math/rand.odin b/core/math/rand/rand.odin similarity index 99% rename from core/math/rand.odin rename to core/math/rand/rand.odin index 4862ad496..f6756b537 100644 --- a/core/math/rand.odin +++ b/core/math/rand/rand.odin @@ -1,3 +1,5 @@ +package rand + Rand :: struct { state: u64, inc: u64, diff --git a/core/mem.odin b/core/mem/mem.odin similarity index 96% rename from core/mem.odin rename to core/mem/mem.odin index 9bb34c1b7..c125495b2 100644 --- a/core/mem.odin +++ b/core/mem/mem.odin @@ -1,5 +1,6 @@ -import "core:raw.odin" -import "core:mem.odin" +package mem + +import "core:raw" foreign __llvm_core { @(link_name = "llvm.bswap.i16") swap16 :: proc(b: u16) -> u16 ---; @@ -85,11 +86,11 @@ AllocationHeader :: struct {size: int}; allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: int) { header.size = size; - ptr := cast(^uint)(mem.ptr_offset(header, 1)); - n := mem.ptr_sub(cast(^uint)data, ptr); + ptr := cast(^uint)(ptr_offset(header, 1)); + n := ptr_sub(cast(^uint)data, ptr); for i in 0..n { - mem.ptr_offset(ptr, i)^ = ~uint(0); + ptr_offset(ptr, i)^ = ~uint(0); } } allocation_header :: proc(data: rawptr) -> ^AllocationHeader { diff --git a/core/opengl.odin b/core/opengl/opengl.odin similarity index 98% rename from core/opengl.odin rename to core/opengl/opengl.odin index 6a2794d1f..29845fba2 100644 --- a/core/opengl.odin +++ b/core/opengl/opengl.odin @@ -1,7 +1,8 @@ +package opengl + when ODIN_OS == "windows" { foreign import lib "system:opengl32.lib" - import win32 "core:sys/windows.odin" - import "core:sys/wgl.odin" + import win32 "core:sys/win32" } else when ODIN_OS == "linux" { foreign import lib "system:gl" } @@ -48,7 +49,7 @@ get_gl_proc_address :: proc(name: string) -> rawptr { } // NOTE(bill): null terminated assert((&name[0] + len(name))^ == 0); - res := wgl.get_gl_proc_address(cstring(&name[0])); + res := win32.get_gl_proc_address(cstring(&name[0])); if res == nil { res = win32.get_proc_address(_libgl, cstring(&name[0])); } diff --git a/core/opengl_constants.odin b/core/opengl/opengl_constants.odin similarity index 99% rename from core/opengl_constants.odin rename to core/opengl/opengl_constants.odin index 9b768409f..72babb358 100644 --- a/core/opengl_constants.odin +++ b/core/opengl/opengl_constants.odin @@ -1,3 +1,5 @@ +package opengl + FALSE :: 0; TRUE :: 1; diff --git a/core/os.odin b/core/os/os.odin similarity index 83% rename from core/os.odin rename to core/os/os.odin index 6fbbabd94..34be2437f 100644 --- a/core/os.odin +++ b/core/os/os.odin @@ -1,9 +1,6 @@ -when ODIN_OS == "windows" do export "core:os/windows.odin"; -when ODIN_OS == "osx" do export "core:os/osx.odin"; -when ODIN_OS == "linux" do export "core:os/linux.odin"; -when ODIN_OS == "essence" do export "core:os/essence.odin"; +package os -import "mem.odin"; +import "core:mem" write_string :: proc(fd: Handle, str: string) -> (int, Errno) { return write(fd, cast([]byte)str); diff --git a/core/os/essence.odin b/core/os/os_essence.odin similarity index 97% rename from core/os/essence.odin rename to core/os/os_essence.odin index 501a68e99..b417ff0b1 100644 --- a/core/os/essence.odin +++ b/core/os/os_essence.odin @@ -1,3 +1,5 @@ +package os + foreign import api "system:api" Handle :: distinct int; @@ -17,8 +19,8 @@ ERROR_PATH_NOT_WITHIN_MOUNTED_VOLUME : Errno : -14; ERROR_PATH_NOT_FOUND : Errno : -15; ERROR_FILE_EXISTS : Errno : -19; ERROR_FILE_NOT_FOUND : Errno : -20; -ERROR_DRIVE_ERROR_FILE_DAMAGED : Errno : -21; -ERROR_ACCESS_NOT_WITHIN_FILE_BOUNDS : Errno : -22; +ERROR_DRIVE_ERROR_FILE_DAMAGED : Errno : -21; +ERROR_ACCESS_NOT_WITHIN_FILE_BOUNDS : Errno : -22; ERROR_ACCESS_DENIED : Errno : -23; ERROR_FILE_IN_EXCLUSIVE_USE : Errno : -24; ERROR_FILE_CANNOT_GET_EXCLUSIVE_USE : Errno : -25; diff --git a/core/os/linux.odin b/core/os/os_linux.odin similarity index 99% rename from core/os/linux.odin rename to core/os/os_linux.odin index 8d765df0a..e3c9de55b 100644 --- a/core/os/linux.odin +++ b/core/os/os_linux.odin @@ -1,8 +1,10 @@ +package os + foreign import dl "system:dl" foreign import libc "system:c" -import "core:strings.odin" -import "core:mem.odin" +import "core:strings" +import "core:mem" Handle :: distinct i32; File_Time :: distinct u64; diff --git a/core/os/osx.odin b/core/os/os_osx.odin similarity index 99% rename from core/os/osx.odin rename to core/os/os_osx.odin index 579aab073..e45cfb264 100644 --- a/core/os/osx.odin +++ b/core/os/os_osx.odin @@ -1,8 +1,10 @@ +package os + foreign import dl "system:dl" foreign import libc "system:c" -import "core:strings.odin" -import "core:mem.odin" +import "core:strings" +import "core:mem" Handle :: distinct i32; File_Time :: distinct u64; diff --git a/core/os/windows.odin b/core/os/os_windows.odin similarity index 99% rename from core/os/windows.odin rename to core/os/os_windows.odin index 3bdc8a5d0..4b776c7f4 100644 --- a/core/os/windows.odin +++ b/core/os/os_windows.odin @@ -1,5 +1,7 @@ -import win32 "core:sys/windows.odin" -import "core:mem.odin" +package os + +import "core:sys/win32" +import "core:mem" Handle :: distinct uintptr; File_Time :: distinct u64; diff --git a/core/raw.odin b/core/raw/raw.odin similarity index 98% rename from core/raw.odin rename to core/raw/raw.odin index fc3876eed..94019f95b 100644 --- a/core/raw.odin +++ b/core/raw/raw.odin @@ -1,3 +1,5 @@ +package raw + Any :: struct { data: rawptr, typeid: typeid, diff --git a/core/_preload.odin b/core/runtime/_preload.odin similarity index 99% rename from core/_preload.odin rename to core/runtime/_preload.odin index dfacf8959..b774350ee 100644 --- a/core/_preload.odin +++ b/core/runtime/_preload.odin @@ -1,10 +1,10 @@ -#shared_global_scope +package runtime -import "core:os.odin" -// import "core:fmt.odin" // TODO(bill): Remove the need for `fmt` here -import "core:utf8.odin" -import "core:raw.odin" -import "core:mem.odin" +import "core:os" +// import "core:fmt" // TODO(bill): Remove the need for `fmt` here +import "core:unicode/utf8" +import "core:raw" +import "core:mem" // Naming Conventions: // In general, Ada_Case for types and snake_case for values diff --git a/core/_soft_numbers.odin b/core/runtime/_soft_numbers.odin similarity index 99% rename from core/_soft_numbers.odin rename to core/runtime/_soft_numbers.odin index 7fb5986c4..92ec2bfec 100644 --- a/core/_soft_numbers.odin +++ b/core/runtime/_soft_numbers.odin @@ -1,4 +1,4 @@ -#shared_global_scope +package runtime /* @(link_name="__multi3") diff --git a/core/sort.odin b/core/sort/sort.odin similarity index 99% rename from core/sort.odin rename to core/sort/sort.odin index e5f690066..f2e349b61 100644 --- a/core/sort.odin +++ b/core/sort/sort.odin @@ -1,3 +1,5 @@ +package sort + bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) { assert(f != nil); count := len(array); diff --git a/core/strconv.odin b/core/strconv/strconv.odin similarity index 99% rename from core/strconv.odin rename to core/strconv/strconv.odin index bc2c34ca1..5e9d3ff70 100644 --- a/core/strconv.odin +++ b/core/strconv/strconv.odin @@ -1,4 +1,6 @@ -using import "core:decimal.odin" +package strconv + +using import "core:decimal" Int_Flag :: enum { Prefix = 1<<0, diff --git a/core/strings.odin b/core/strings/strings.odin similarity index 92% rename from core/strings.odin rename to core/strings/strings.odin index 0bfcccfbc..9cee48bc8 100644 --- a/core/strings.odin +++ b/core/strings/strings.odin @@ -1,5 +1,7 @@ -import "core:mem.odin" -import "core:raw.odin" +package strings + +import "core:mem" +import "core:raw" new_string :: proc(s: string) -> string { c := make([]byte, len(s)+1); diff --git a/core/sync.odin b/core/sync/sync.odin similarity index 100% rename from core/sync.odin rename to core/sync/sync.odin diff --git a/core/sync_linux.odin b/core/sync/sync_linux.odin similarity index 100% rename from core/sync_linux.odin rename to core/sync/sync_linux.odin diff --git a/core/sync_windows.odin b/core/sync/sync_windows.odin similarity index 100% rename from core/sync_windows.odin rename to core/sync/sync_windows.odin diff --git a/core/sys/wgl.odin b/core/sys/win32/wgl.odin similarity index 98% rename from core/sys/wgl.odin rename to core/sys/win32/wgl.odin index b95fba414..75d845406 100644 --- a/core/sys/wgl.odin +++ b/core/sys/win32/wgl.odin @@ -1,6 +1,7 @@ +package win32 + when ODIN_OS == "windows" { foreign import "system:opengl32.lib" - using import "core:sys/windows.odin" } diff --git a/core/sys/windows.odin b/core/sys/win32/windows.odin similarity index 97% rename from core/sys/windows.odin rename to core/sys/win32/windows.odin index 6b6d0c56e..124674122 100644 --- a/core/sys/windows.odin +++ b/core/sys/win32/windows.odin @@ -1,1091 +1,1093 @@ -when ODIN_OS == "windows" { - foreign import "system:kernel32.lib" - foreign import "system:user32.lib" - foreign import "system:gdi32.lib" - foreign import "system:winmm.lib" - foreign import "system:shell32.lib" -} - -Handle :: distinct rawptr; -Hwnd :: distinct Handle; -Hdc :: distinct Handle; -Hinstance :: distinct Handle; -Hicon :: distinct Handle; -Hcursor :: distinct Handle; -Hmenu :: distinct Handle; -Hbrush :: distinct Handle; -Hgdiobj :: distinct Handle; -Hmodule :: distinct Handle; -Hmonitor :: distinct Handle; -Hrawinput :: distinct Handle; -HKL :: distinct Handle; -Wparam :: distinct uint; -Lparam :: distinct int; -Lresult :: distinct int; -Wnd_Proc :: distinct #type proc "c" (Hwnd, u32, Wparam, Lparam) -> Lresult; - -Long_Ptr :: distinct int; - -Bool :: distinct b32; - -Wstring :: ^u16; - -Point :: struct { - x, y: i32, -} - -Wnd_Class_Ex_A :: struct { - size, style: u32, - wnd_proc: Wnd_Proc, - cls_extra, wnd_extra: i32, - instance: Hinstance, - icon: Hicon, - cursor: Hcursor, - background: Hbrush, - menu_name, class_name: cstring, - sm: Hicon, -} - -Wnd_Class_Ex_W :: struct { - size, style: u32, - wnd_proc: Wnd_Proc, - cls_extra, wnd_extra: i32, - instance: Hinstance, - icon: Hicon, - cursor: Hcursor, - background: Hbrush, - menu_name, class_name: Wstring, - sm: Hicon, -} - - -Msg :: struct { - hwnd: Hwnd, - message: u32, - wparam: Wparam, - lparam: Lparam, - time: u32, - pt: Point, -} - -Rect :: struct { - left: i32, - top: i32, - right: i32, - bottom: i32, -} - -Filetime :: struct { - lo, hi: u32, -} - -Systemtime :: struct { - year, month: u16, - day_of_week, day: u16, - hour, minute, second, millisecond: u16, -} - -By_Handle_File_Information :: struct { - file_attributes: u32, - creation_time, - last_access_time, - last_write_time: Filetime, - volume_serial_number, - file_size_high, - file_size_low, - number_of_links, - file_index_high, - file_index_low: u32, -} - -File_Attribute_Data :: struct { - file_attributes: u32, - creation_time, - last_access_time, - last_write_time: Filetime, - file_size_high, - file_size_low: u32, -} - -Find_Data_W :: struct{ - file_attributes: u32, - creation_time: Filetime, - last_access_time: Filetime, - last_write_time: Filetime, - file_size_high: u32, - file_size_low: u32, - reserved0: u32, - reserved1: u32, - file_name: [MAX_PATH]u16, - alternate_file_name: [14]u16, -} - -Find_Data_A :: struct{ - file_attributes: u32, - creation_time: Filetime, - last_access_time: Filetime, - last_write_time: Filetime, - file_size_high: u32, - file_size_low: u32, - reserved0: u32, - reserved1: u32, - file_name: [MAX_PATH]byte, - alternate_file_name: [14]byte, -} - -Security_Attributes :: struct { - length: u32, - security_descriptor: rawptr, - inherit_handle: Bool, -} - -Process_Information :: struct { - process: Handle, - thread: Handle, - process_id: u32, - thread_id: u32 -} - -Startup_Info :: struct { - cb: u32, - reserved: Wstring, - desktop: Wstring, - title: Wstring, - x: u32, - y: u32, - x_size: u32, - y_size: u32, - x_count_chars: u32, - y_count_chars: u32, - fill_attribute: u32, - flags: u32, - show_window: u16, - _: u16, - _: cstring, - stdin: Handle, - stdout: Handle, - stderr: Handle, -} - -Pixel_Format_Descriptor :: struct { - size, - version, - flags: u32, - - pixel_type, - color_bits, - red_bits, - red_shift, - green_bits, - green_shift, - blue_bits, - blue_shift, - alpha_bits, - alpha_shift, - accum_bits, - accum_red_bits, - accum_green_bits, - accum_blue_bits, - accum_alpha_bits, - depth_bits, - stencil_bits, - aux_buffers, - layer_type, - reserved: byte, - - layer_mask, - visible_mask, - damage_mask: u32, -} - -Critical_Section :: struct { - debug_info: ^Critical_Section_Debug, - - lock_count: i32, - recursion_count: i32, - owning_thread: Handle, - lock_semaphore: Handle, - spin_count: ^u32, -} - -Critical_Section_Debug :: struct { - typ: u16, - creator_back_trace_index: u16, - critical_section: ^Critical_Section, - process_locks_list: ^List_Entry, - entry_count: u32, - contention_count: u32, - flags: u32, - creator_back_trace_index_high: u16, - spare_word: u16, -} - -List_Entry :: struct {flink, blink: ^List_Entry}; - - -Raw_Input_Device :: struct { - usage_page: u16, - usage: u16, - flags: u32, - wnd_target: Hwnd, -} - -Raw_Input_Header :: struct { - kind: u32, - size: u32, - device: Handle, - wparam: Wparam, -} - -Raw_HID :: struct { - size_hid: u32, - count: u32, - raw_data: [1]byte, -} - -Raw_Keyboard :: struct { - make_code: u16, - flags: u16, - reserved: u16, - vkey: u16, - message: u32, - extra_information: u32, -} - -Raw_Mouse :: struct { - flags: u16, - using data: struct #raw_union { - buttons: u32, - using _: struct { - button_flags: u16, - button_data: u16, - }, - }, - raw_buttons: u32, - last_x: i32, - last_y: i32, - extra_information: u32, -} - -Raw_Input :: struct { - using header: Raw_Input_Header, - data: struct #raw_union { - mouse: Raw_Mouse, - keyboard: Raw_Keyboard, - hid: Raw_HID, - }, -} - - -Overlapped :: struct { - internal: ^u64, - internal_high: ^u64, - using _: struct #raw_union { - using _: struct { - offset: u32, - offset_high: u32, - }, - pointer: rawptr, - }, - event: Handle, -} - -File_Notify_Information :: struct { - next_entry_offset: u32, - action: u32, - file_name_length: u32, - file_name: [1]u16, -} - -MAPVK_VK_TO_VSC :: 0; -MAPVK_VSC_TO_VK :: 1; -MAPVK_VK_TO_CHAR :: 2; -MAPVK_VSC_TO_VK_EX :: 3; - - - - -INVALID_HANDLE :: Handle(~uintptr(0)); - -CREATE_SUSPENDED :: 0x00000004; -STACK_SIZE_PARAM_IS_A_RESERVATION :: 0x00010000; -WAIT_ABANDONED :: 0x00000080; -WAIT_OBJECT_0 :: 0; -WAIT_TIMEOUT :: 0x00000102; -WAIT_FAILED :: 0xffffffff; - -CS_VREDRAW :: 0x0001; -CS_HREDRAW :: 0x0002; -CS_OWNDC :: 0x0020; -CW_USEDEFAULT :: -0x80000000; - -WS_OVERLAPPED :: 0; -WS_MAXIMIZEBOX :: 0x00010000; -WS_MINIMIZEBOX :: 0x00020000; -WS_THICKFRAME :: 0x00040000; -WS_SYSMENU :: 0x00080000; -WS_BORDER :: 0x00800000; -WS_CAPTION :: 0x00C00000; -WS_VISIBLE :: 0x10000000; -WS_POPUP :: 0x80000000; -WS_MAXIMIZE :: 0x01000000; -WS_MINIMIZE :: 0x20000000; -WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX; -WS_POPUPWINDOW :: WS_POPUP | WS_BORDER | WS_SYSMENU; - -WM_ACTIVATE :: 0x0006; -WM_ACTIVATEAPP :: 0x001C; -WM_CHAR :: 0x0102; -WM_CLOSE :: 0x0010; -WM_CREATE :: 0x0001; -WM_DESTROY :: 0x0002; -WM_INPUT :: 0x00ff; -WM_KEYDOWN :: 0x0100; -WM_KEYUP :: 0x0101; -WM_KILLFOCUS :: 0x0008; -WM_QUIT :: 0x0012; -WM_SETCURSOR :: 0x0020; -WM_SETFOCUS :: 0x0007; -WM_SIZE :: 0x0005; -WM_SIZING :: 0x0214; -WM_SYSKEYDOWN :: 0x0104; -WM_SYSKEYUP :: 0x0105; -WM_USER :: 0x0400; -WM_WINDOWPOSCHANGED :: 0x0047; - -WM_MOUSEWHEEL :: 0x020A; -WM_MOUSEMOVE :: 0x0200; -WM_LBUTTONDOWN :: 0x0201; -WM_LBUTTONUP :: 0x0202; -WM_LBUTTONDBLCLK :: 0x0203; -WM_RBUTTONDOWN :: 0x0204; -WM_RBUTTONUP :: 0x0205; -WM_RBUTTONDBLCLK :: 0x0206; -WM_MBUTTONDOWN :: 0x0207; -WM_MBUTTONUP :: 0x0208; -WM_MBUTTONDBLCLK :: 0x0209; - -PM_NOREMOVE :: 0x0000; -PM_REMOVE :: 0x0001; -PM_NOYIELD :: 0x0002; - -BLACK_BRUSH :: 4; - -SM_CXSCREEN :: 0; -SM_CYSCREEN :: 1; - -SW_SHOW :: 5; - -COLOR_BACKGROUND :: Hbrush(uintptr(1)); - -INVALID_SET_FILE_POINTER :: ~u32(0); -HEAP_ZERO_MEMORY :: 0x00000008; -INFINITE :: 0xffffffff; -GWL_EXSTYLE :: -20; -GWLP_HINSTANCE :: -6; -GWLP_ID :: -12; -GWL_STYLE :: -16; -GWLP_USERDATA :: -21; -GWLP_WNDPROC :: -4; -Hwnd_TOP :: Hwnd(uintptr(0)); - -BI_RGB :: 0; -DIB_RGB_COLORS :: 0x00; -SRCCOPY: u32 : 0x00cc0020; - - -MONITOR_DEFAULTTONULL :: 0x00000000; -MONITOR_DEFAULTTOPRIMARY :: 0x00000001; -MONITOR_DEFAULTTONEAREST :: 0x00000002; - -SWP_FRAMECHANGED :: 0x0020; -SWP_NOOWNERZORDER :: 0x0200; -SWP_NOZORDER :: 0x0004; -SWP_NOSIZE :: 0x0001; -SWP_NOMOVE :: 0x0002; - - -// Raw Input - - -RID_HEADER :: 0x10000005; -RID_INPUT :: 0x10000003; - - -RIDEV_APPKEYS :: 0x00000400; -RIDEV_CAPTUREMOUSE :: 0x00000200; -RIDEV_DEVNOTIFY :: 0x00002000; -RIDEV_EXCLUDE :: 0x00000010; -RIDEV_EXINPUTSINK :: 0x00001000; -RIDEV_INPUTSINK :: 0x00000100; -RIDEV_NOHOTKEYS :: 0x00000200; -RIDEV_NOLEGACY :: 0x00000030; -RIDEV_PAGEONLY :: 0x00000020; -RIDEV_REMOVE :: 0x00000001; - - -RIM_TYPEMOUSE :: 0; -RIM_TYPEKEYBOARD :: 1; -RIM_TYPEHID :: 2; - - -MOUSE_ATTRIBUTES_CHANGED :: 0x04; -MOUSE_MOVE_RELATIVE :: 0; -MOUSE_MOVE_ABSOLUTE :: 1; -MOUSE_VIRTUAL_DESKTOP :: 0x02; - - - -RI_MOUSE_BUTTON_1_DOWN :: 0x0001; -RI_MOUSE_BUTTON_1_UP :: 0x0002; -RI_MOUSE_BUTTON_2_DOWN :: 0x0004; -RI_MOUSE_BUTTON_2_UP :: 0x0008; -RI_MOUSE_BUTTON_3_DOWN :: 0x0010; -RI_MOUSE_BUTTON_3_UP :: 0x0020; -RI_MOUSE_BUTTON_4_DOWN :: 0x0040; -RI_MOUSE_BUTTON_4_UP :: 0x0080; -RI_MOUSE_BUTTON_5_DOWN :: 0x0100; -RI_MOUSE_BUTTON_5_UP :: 0x0200; -RI_MOUSE_LEFT_BUTTON_DOWN :: 0x0001; -RI_MOUSE_LEFT_BUTTON_UP :: 0x0002; -RI_MOUSE_MIDDLE_BUTTON_DOWN :: 0x0010; -RI_MOUSE_MIDDLE_BUTTON_UP :: 0x0020; -RI_MOUSE_RIGHT_BUTTON_DOWN :: 0x0004; -RI_MOUSE_RIGHT_BUTTON_UP :: 0x0008; -RI_MOUSE_WHEEL :: 0x0400; - - -RI_KEY_MAKE :: 0x00; -RI_KEY_BREAK :: 0x01; -RI_KEY_E0 :: 0x02; -RI_KEY_E1 :: 0x04; -RI_KEY_TERMSRV_SET_LED :: 0x08; -RI_KEY_TERMSRV_SHADOW :: 0x10; - -// Windows OpenGL - -PFD_TYPE_RGBA :: 0; -PFD_TYPE_COLORINDEX :: 1; -PFD_MAIN_PLANE :: 0; -PFD_OVERLAY_PLANE :: 1; -PFD_UNDERLAY_PLANE :: -1; -PFD_DOUBLEBUFFER :: 1; -PFD_STEREO :: 2; -PFD_DRAW_TO_WINDOW :: 4; -PFD_DRAW_TO_BITMAP :: 8; -PFD_SUPPORT_GDI :: 16; -PFD_SUPPORT_OPENGL :: 32; -PFD_GENERIC_FORMAT :: 64; -PFD_NEED_PALETTE :: 128; -PFD_NEED_SYSTEM_PALETTE :: 0x00000100; -PFD_SWAP_EXCHANGE :: 0x00000200; -PFD_SWAP_COPY :: 0x00000400; -PFD_SWAP_LAYER_BUFFERS :: 0x00000800; -PFD_GENERIC_ACCELERATED :: 0x00001000; -PFD_DEPTH_DONTCARE :: 0x20000000; -PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000; -PFD_STEREO_DONTCARE :: 0x80000000; - -GET_FILEEX_INFO_LEVELS :: distinct i32; -GetFileExInfoStandard: GET_FILEEX_INFO_LEVELS : 0; -GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1; - -STARTF_USESHOWWINDOW :: 0x00000001; -STARTF_USESIZE :: 0x00000002; -STARTF_USEPOSITION :: 0x00000004; -STARTF_USECOUNTCHARS :: 0x00000008; -STARTF_USEFILLATTRIBUTE :: 0x00000010; -STARTF_RUNFULLSCREEN :: 0x00000020; // ignored for non-x86 platforms -STARTF_FORCEONFEEDBACK :: 0x00000040; -STARTF_FORCEOFFFEEDBACK :: 0x00000080; -STARTF_USESTDHANDLES :: 0x00000100; -STARTF_USEHOTKEY :: 0x00000200; -STARTF_TITLEISLINKNAME :: 0x00000800; -STARTF_TITLEISAPPID :: 0x00001000; -STARTF_PREVENTPINNING :: 0x00002000; -STARTF_UNTRUSTEDSOURCE :: 0x00008000; - - -MOVEFILE_REPLACE_EXISTING :: 0x00000001; -MOVEFILE_COPY_ALLOWED :: 0x00000002; -MOVEFILE_DELAY_UNTIL_REBOOT :: 0x00000004; -MOVEFILE_WRITE_THROUGH :: 0x00000008; -MOVEFILE_CREATE_HARDLINK :: 0x00000010; -MOVEFILE_FAIL_IF_NOT_TRACKABLE :: 0x00000020; - -FILE_NOTIFY_CHANGE_FILE_NAME :: 0x00000001; -FILE_NOTIFY_CHANGE_DIR_NAME :: 0x00000002; -FILE_NOTIFY_CHANGE_ATTRIBUTES :: 0x00000004; -FILE_NOTIFY_CHANGE_SIZE :: 0x00000008; -FILE_NOTIFY_CHANGE_LAST_WRITE :: 0x00000010; -FILE_NOTIFY_CHANGE_LAST_ACCESS :: 0x00000020; -FILE_NOTIFY_CHANGE_CREATION :: 0x00000040; -FILE_NOTIFY_CHANGE_SECURITY :: 0x00000100; - -FILE_FLAG_WRITE_THROUGH :: 0x80000000; -FILE_FLAG_OVERLAPPED :: 0x40000000; -FILE_FLAG_NO_BUFFERING :: 0x20000000; -FILE_FLAG_RANDOM_ACCESS :: 0x10000000; -FILE_FLAG_SEQUENTIAL_SCAN :: 0x08000000; -FILE_FLAG_DELETE_ON_CLOSE :: 0x04000000; -FILE_FLAG_BACKUP_SEMANTICS :: 0x02000000; -FILE_FLAG_POSIX_SEMANTICS :: 0x01000000; -FILE_FLAG_SESSION_AWARE :: 0x00800000; -FILE_FLAG_OPEN_REPARSE_POINT :: 0x00200000; -FILE_FLAG_OPEN_NO_RECALL :: 0x00100000; -FILE_FLAG_FIRST_PIPE_INSTANCE :: 0x00080000; - -FILE_ACTION_ADDED :: 0x00000001; -FILE_ACTION_REMOVED :: 0x00000002; -FILE_ACTION_MODIFIED :: 0x00000003; -FILE_ACTION_RENAMED_OLD_NAME :: 0x00000004; -FILE_ACTION_RENAMED_NEW_NAME :: 0x00000005; - -CP_ACP :: 0; // default to ANSI code page -CP_OEMCP :: 1; // default to OEM code page -CP_MACCP :: 2; // default to MAC code page -CP_THREAD_ACP :: 3; // current thread's ANSI code page -CP_SYMBOL :: 42; // SYMBOL translations -CP_UTF7 :: 65000; // UTF-7 translation -CP_UTF8 :: 65001; // UTF-8 translation - -@(default_calling_convention = "std") -foreign kernel32 { - @(link_name="GetLastError") get_last_error :: proc() -> i32 ---; - @(link_name="CreateProcessA") create_process_a :: proc(application_name, command_line: cstring, - process_attributes, thread_attributes: ^Security_Attributes, - inherit_handle: Bool, creation_flags: u32, environment: rawptr, - current_direcotry: cstring, startup_info: ^Startup_Info, - process_information: ^Process_Information) -> Bool ---; - @(link_name="CreateProcessW") create_process_w :: proc(application_name, command_line: Wstring, - process_attributes, thread_attributes: ^Security_Attributes, - inherit_handle: Bool, creation_flags: u32, environment: rawptr, - current_direcotry: cstring, startup_info: ^Startup_Info, - process_information: ^Process_Information) -> Bool ---; - @(link_name="GetExitCodeProcess") get_exit_code_process :: proc(process: Handle, exit: ^u32) -> Bool ---; - @(link_name="ExitProcess") exit_process :: proc(exit_code: u32) ---; - @(link_name="GetModuleHandleA") get_module_handle_a :: proc(module_name: cstring) -> Hinstance ---; - @(link_name="GetModuleHandleW") get_module_handle_w :: proc(module_name: Wstring) -> Hinstance ---; - @(link_name="Sleep") sleep :: proc(ms: i32) -> i32 ---; - @(link_name="QueryPerformanceFrequency") query_performance_frequency :: proc(result: ^i64) -> i32 ---; - @(link_name="QueryPerformanceCounter") query_performance_counter :: proc(result: ^i64) -> i32 ---; - @(link_name="OutputDebugStringA") output_debug_string_a :: proc(c_str: cstring) ---; - - @(link_name="GetCommandLineA") get_command_line_a :: proc() -> cstring ---; - @(link_name="GetCommandLineW") get_command_line_w :: proc() -> Wstring ---; - @(link_name="GetSystemMetrics") get_system_metrics :: proc(index: i32) -> i32 ---; - @(link_name="GetCurrentThreadId") get_current_thread_id :: proc() -> u32 ---; - - @(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) ---; - @(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---; - @(link_name="FileTimeToSystemTime") file_time_to_system_time :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---; - @(link_name="SystemTimeToFileTime") system_time_to_file_time :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---; - - @(link_name="CloseHandle") close_handle :: proc(h: Handle) -> i32 ---; - @(link_name="GetStdHandle") get_std_handle :: proc(h: i32) -> Handle ---; - - @(link_name="CreateFileA") - create_file_a :: proc(filename: cstring, desired_access, share_module: u32, - security: rawptr, - creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---; - - @(link_name="CreateFileW") - create_file_w :: proc(filename: Wstring, desired_access, share_module: u32, - security: rawptr, - creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---; - - - @(link_name="ReadFile") read_file :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---; - @(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---; - - @(link_name="GetFileSizeEx") get_file_size_ex :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---; - @(link_name="GetFileAttributesA") get_file_attributes_a :: proc(filename: cstring) -> u32 ---; - @(link_name="GetFileAttributesW") get_file_attributes_w :: proc(filename: Wstring) -> u32 ---; - @(link_name="GetFileAttributesExA") get_file_attributes_ex_a :: proc(filename: cstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---; - @(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---; - - @(link_name="CreateDirectoryA") create_directory_a :: proc(path: cstring, security_attributes: ^Security_Attributes) -> Bool ---; - @(link_name="CreateDirectoryW") create_directory_w :: proc(path: Wstring, security_attributes: ^Security_Attributes) -> Bool ---; - - @(link_name="GetFileType") get_file_type :: proc(file_handle: Handle) -> u32 ---; - @(link_name="SetFilePointer") set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---; - - @(link_name="SetHandleInformation") set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool ---; - - @(link_name="FindFirstFileA") find_first_file_a :: proc(file_name: cstring, data: ^Find_Data_A) -> Handle ---; - @(link_name="FindNextFileA") find_next_file_a :: proc(file: Handle, data: ^Find_Data_A) -> Bool ---; - - @(link_name="FindFirstFileW") find_first_file_w :: proc(file_name: Wstring, data: ^Find_Data_W) -> Handle ---; - @(link_name="FindNextFileW") find_next_file_w :: proc(file: Handle, data: ^Find_Data_W) -> Bool ---; - - @(link_name="FindClose") find_close :: proc(file: Handle) -> Bool ---; - - @(link_name="MoveFileExA") move_file_ex_a :: proc(existing, new: cstring, flags: u32) -> Bool ---; - @(link_name="DeleteFileA") delete_file_a :: proc(file_name: cstring) -> Bool ---; - @(link_name="CopyFileA") copy_file_a :: proc(existing, new: cstring, fail_if_exists: Bool) -> Bool ---; - - @(link_name="MoveFileExW") move_file_ex_w :: proc(existing, new: Wstring, flags: u32) -> Bool ---; - @(link_name="DeleteFileW") delete_file_w :: proc(file_name: Wstring) -> Bool ---; - @(link_name="CopyFileW") copy_file_w :: proc(existing, new: Wstring, fail_if_exists: Bool) -> Bool ---; - - @(link_name="HeapAlloc") heap_alloc :: proc(h: Handle, flags: u32, bytes: int) -> rawptr ---; - @(link_name="HeapReAlloc") heap_realloc :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---; - @(link_name="HeapFree") heap_free :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool ---; - @(link_name="GetProcessHeap") get_process_heap :: proc() -> Handle ---; - - @(link_name="LocalAlloc") local_alloc :: proc(flags: u32, bytes: int) -> rawptr ---; - @(link_name="LocalReAlloc") local_realloc :: proc(mem: rawptr, bytes: int, flags: uint) -> rawptr ---; - @(link_name="LocalFree") local_free :: proc(mem: rawptr) -> rawptr ---; - - @(link_name="FindFirstChangeNotificationA") find_first_change_notification_a :: proc(path: cstring, watch_subtree: Bool, filter: u32) -> Handle ---; - @(link_name="FindNextChangeNotification") find_next_change_notification :: proc(h: Handle) -> Bool ---; - @(link_name="FindCloseChangeNotification") find_close_change_notification :: proc(h: Handle) -> Bool ---; - - @(link_name="ReadDirectoryChangesW") read_directory_changes_w :: proc(dir: Handle, buf: rawptr, buf_length: u32, - watch_subtree: Bool, notify_filter: u32, - bytes_returned: ^u32, overlapped: ^Overlapped, - completion: rawptr) -> Bool ---; - - @(link_name="WideCharToMultiByte") wide_char_to_multi_byte :: proc(code_page: u32, flags: u32, - wchar_str: Wstring, wchar: i32, - multi_str: cstring, multi: i32, - default_char: cstring, used_default_char: ^Bool) -> i32 ---; - - @(link_name="MultiByteToWideChar") multi_byte_to_wide_char :: proc(code_page: u32, flags: u32, - mb_str: cstring, mb: i32, - wc_str: Wstring, wc: i32) -> i32 ---; - - @(link_name="CreateSemaphoreA") create_semaphore_a :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: cstring) -> Handle ---; - @(link_name="ReleaseSemaphore") release_semaphore :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---; - @(link_name="WaitForSingleObject") wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 ---; -} - -@(default_calling_convention = "c") -foreign kernel32 { - @(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 ---; - @(link_name="InterlockedExchange") interlocked_exchange :: proc(dst: ^i32, desired: i32) -> i32 ---; - @(link_name="InterlockedExchangeAdd") interlocked_exchange_add :: proc(dst: ^i32, desired: i32) -> i32 ---; - @(link_name="InterlockedAnd") interlocked_and :: proc(dst: ^i32, desired: i32) -> i32 ---; - @(link_name="InterlockedOr") interlocked_or :: proc(dst: ^i32, desired: i32) -> i32 ---; - - @(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 ---; - @(link_name="InterlockedExchange64") interlocked_exchange64 :: proc(dst: ^i64, desired: i64) -> i64 ---; - @(link_name="InterlockedExchangeAdd64") interlocked_exchange_add64 :: proc(dst: ^i64, desired: i64) -> i64 ---; - @(link_name="InterlockedAnd64") interlocked_and64 :: proc(dst: ^i64, desired: i64) -> i64 ---; - @(link_name="InterlockedOr64") interlocked_or64 :: proc(dst: ^i64, desired: i64) -> i64 ---; -} - -@(default_calling_convention = "std") -foreign kernel32 { - @(link_name="_mm_pause") mm_pause :: proc() ---; - @(link_name="ReadWriteBarrier") read_write_barrier :: proc() ---; - @(link_name="WriteBarrier") write_barrier :: proc() ---; - @(link_name="ReadBarrier") read_barrier :: proc() ---; - - @(link_name="CreateThread") - create_thread :: proc(thread_attributes: ^Security_Attributes, stack_size: int, start_routine: rawptr, - parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---; - @(link_name="ResumeThread") resume_thread :: proc(thread: Handle) -> u32 ---; - @(link_name="GetThreadPriority") get_thread_priority :: proc(thread: Handle) -> i32 ---; - @(link_name="SetThreadPriority") set_thread_priority :: proc(thread: Handle, priority: i32) -> Bool ---; - @(link_name="GetExitCodeThread") get_exit_code_thread :: proc(thread: Handle, exit_code: ^u32) -> Bool ---; - @(link_name="TerminateThread") terminate_thread :: proc(thread: Handle, exit_code: u32) -> Bool ---; - - @(link_name="InitializeCriticalSection") initialize_critical_section :: proc(critical_section: ^Critical_Section) ---; - @(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) ---; - @(link_name="DeleteCriticalSection") delete_critical_section :: proc(critical_section: ^Critical_Section) ---; - @(link_name="SetCriticalSectionSpinCount") set_critical_section_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) -> u32 ---; - @(link_name="TryEnterCriticalSection") try_enter_critical_section :: proc(critical_section: ^Critical_Section) -> Bool ---; - @(link_name="EnterCriticalSection") enter_critical_section :: proc(critical_section: ^Critical_Section) ---; - @(link_name="LeaveCriticalSection") leave_critical_section :: proc(critical_section: ^Critical_Section) ---; - - @(link_name="CreateEventA") create_event_a :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: cstring) -> Handle ---; - - @(link_name="LoadLibraryA") load_library_a :: proc(c_str: cstring) -> Hmodule ---; - @(link_name="LoadLibraryW") load_library_w :: proc(c_str: Wstring) -> Hmodule ---; - @(link_name="FreeLibrary") free_library :: proc(h: Hmodule) ---; - @(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---; - -} - -@(default_calling_convention = "std") -foreign user32 { - @(link_name="GetDesktopWindow") get_desktop_window :: proc() -> Hwnd ---; - @(link_name="ShowCursor") show_cursor :: proc(show: Bool) ---; - @(link_name="GetCursorPos") get_cursor_pos :: proc(p: ^Point) -> Bool ---; - @(link_name="SetCursorPos") set_cursor_pos :: proc(x, y: i32) -> Bool ---; - @(link_name="ScreenToClient") screen_to_client :: proc(h: Hwnd, p: ^Point) -> Bool ---; - @(link_name="ClientToScreen") client_to_screen :: proc(h: Hwnd, p: ^Point) -> Bool ---; - @(link_name="PostQuitMessage") post_quit_message :: proc(exit_code: i32) ---; - @(link_name="SetWindowTextA") set_window_text_a :: proc(hwnd: Hwnd, c_string: cstring) -> Bool ---; - @(link_name="RegisterClassExA") register_class_ex_a :: proc(wc: ^Wnd_Class_Ex_A) -> i16 ---; - @(link_name="RegisterClassExW") register_class_ex_w :: proc(wc: ^Wnd_Class_Ex_W) -> i16 ---; - - @(link_name="CreateWindowExA") - create_window_ex_a :: proc(ex_style: u32, - class_name, title: cstring, - style: u32, - x, y, w, h: i32, - parent: Hwnd, menu: Hmenu, instance: Hinstance, - param: rawptr) -> Hwnd ---; - - @(link_name="CreateWindowExW") - create_window_ex_w :: proc(ex_style: u32, - class_name, title: Wstring, - style: u32, - x, y, w, h: i32, - parent: Hwnd, menu: Hmenu, instance: Hinstance, - param: rawptr) -> Hwnd ---; - - @(link_name="ShowWindow") show_window :: proc(hwnd: Hwnd, cmd_show: i32) -> Bool ---; - @(link_name="TranslateMessage") translate_message :: proc(msg: ^Msg) -> Bool ---; - @(link_name="DispatchMessageA") dispatch_message_a :: proc(msg: ^Msg) -> Lresult ---; - @(link_name="DispatchMessageW") dispatch_message_w :: proc(msg: ^Msg) -> Lresult ---; - @(link_name="UpdateWindow") update_window :: proc(hwnd: Hwnd) -> Bool ---; - @(link_name="GetMessageA") get_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max: u32) -> Bool ---; - @(link_name="GetMessageW") get_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max: u32) -> Bool ---; - - @(link_name="PeekMessageA") peek_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---; - @(link_name="PeekMessageW") peek_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---; - - - @(link_name="PostMessageA") post_message :: proc(hwnd: Hwnd, msg, wparam, lparam: u32) -> Bool ---; - - @(link_name="DefWindowProcA") def_window_proc_a :: proc(hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult ---; - - @(link_name="AdjustWindowRect") adjust_window_rect :: proc(rect: ^Rect, style: u32, menu: Bool) -> Bool ---; - @(link_name="GetActiveWindow") get_active_window :: proc() -> Hwnd ---; - - @(link_name="DestroyWindow") destroy_window :: proc(wnd: Hwnd) -> Bool ---; - @(link_name="DescribePixelFormat") describe_pixel_format :: proc(dc: Hdc, pixel_format: i32, bytes: u32, pfd: ^Pixel_Format_Descriptor) -> i32 ---; - - @(link_name="GetMonitor_InfoA") get_monitor_info_a :: proc(monitor: Hmonitor, mi: ^Monitor_Info) -> Bool ---; - @(link_name="MonitorFromWindow") monitor_from_window :: proc(wnd: Hwnd, flags: u32) -> Hmonitor ---; - - @(link_name="SetWindowPos") set_window_pos :: proc(wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) ---; - - @(link_name="GetWindowPlacement") get_window_placement :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---; - @(link_name="SetWindowPlacement") set_window_placement :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---; - @(link_name="GetWindowRect") get_window_rect :: proc(wnd: Hwnd, rect: ^Rect) -> Bool ---; - - @(link_name="GetWindowLongPtrA") get_window_long_ptr_a :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---; - @(link_name="SetWindowLongPtrA") set_window_long_ptr_a :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---; - @(link_name="GetWindowLongPtrW") get_window_long_ptr_w :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---; - @(link_name="SetWindowLongPtrW") set_window_long_ptr_w :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---; - - @(link_name="GetWindowText") get_window_text :: proc(wnd: Hwnd, str: cstring, maxCount: i32) -> i32 ---; - - @(link_name="GetClientRect") get_client_rect :: proc(hwnd: Hwnd, rect: ^Rect) -> Bool ---; - - @(link_name="GetDC") get_dc :: proc(h: Hwnd) -> Hdc ---; - @(link_name="ReleaseDC") release_dc :: proc(wnd: Hwnd, hdc: Hdc) -> i32 ---; - - @(link_name="MapVirtualKeyA") map_virtual_key_a :: proc(scancode: u32, map_type: u32) -> u32 ---; - @(link_name="MapVirtualKeyW") map_virtual_key_w :: proc(scancode: u32, map_type: u32) -> u32 ---; - - @(link_name="GetKeyState") get_key_state :: proc(v_key: i32) -> i16 ---; - @(link_name="GetAsyncKeyState") get_async_key_state :: proc(v_key: i32) -> i16 ---; - - @(link_name="SetForegroundWindow") set_foreground_window :: proc(h: Hwnd) -> Bool ---; - @(link_name="SetFocus") set_focus :: proc(h: Hwnd) -> Hwnd ---; - - - @(link_name="LoadImageA") load_image_a :: proc(instance: Hinstance, name: cstring, type_: u32, x_desired, y_desired : i32, load : u32) -> Handle ---; - @(link_name="LoadIconA") load_icon_a :: proc(instance: Hinstance, icon_name: cstring) -> Hicon ---; - @(link_name="DestroyIcon") destroy_icon :: proc(icon: Hicon) -> Bool ---; - - @(link_name="LoadCursorA") load_cursor_a :: proc(instance: Hinstance, cursor_name: cstring) -> Hcursor ---; - @(link_name="GetCursor") get_cursor :: proc() -> Hcursor ---; - @(link_name="SetCursor") set_cursor :: proc(cursor: Hcursor) -> Hcursor ---; - - @(link_name="RegisterRawInputDevices") register_raw_input_devices :: proc(raw_input_device: ^Raw_Input_Device, num_devices, size: u32) -> Bool ---; - - @(link_name="GetRawInputData") get_raw_input_data :: proc(raw_input: Hrawinput, command: u32, data: rawptr, size: ^u32, size_header: u32) -> u32 ---; - - @(link_name="MapVirtualKeyExW") map_virtual_key_ex_w :: proc(code, map_type: u32, hkl: HKL) ---; - @(link_name="MapVirtualKeyExA") map_virtual_key_ex_a :: proc(code, map_type: u32, hkl: HKL) ---; -} - -@(default_calling_convention = "std") -foreign gdi32 { - @(link_name="GetStockObject") get_stock_object :: proc(fn_object: i32) -> Hgdiobj ---; - - @(link_name="StretchDIBits") - stretch_dibits :: proc(hdc: Hdc, - x_dst, y_dst, width_dst, height_dst: i32, - x_src, y_src, width_src, header_src: i32, - bits: rawptr, bits_info: ^Bitmap_Info, - usage: u32, - rop: u32) -> i32 ---; - - @(link_name="SetPixelFormat") set_pixel_format :: proc(hdc: Hdc, pixel_format: i32, pfd: ^Pixel_Format_Descriptor) -> Bool ---; - @(link_name="ChoosePixelFormat") choose_pixel_format :: proc(hdc: Hdc, pfd: ^Pixel_Format_Descriptor) -> i32 ---; - @(link_name="SwapBuffers") swap_buffers :: proc(hdc: Hdc) -> Bool ---; - -} - -@(default_calling_convention = "std") -foreign shell32 { - @(link_name="CommandLineToArgvW") command_line_to_argv_w :: proc(cmd_list: Wstring, num_args: ^i32) -> ^Wstring ---; -} - -@(default_calling_convention = "std") -foreign winmm { - @(link_name="timeGetTime") time_get_time :: proc() -> u32 ---; -} - - - -get_query_performance_frequency :: proc() -> i64 { - r: i64; - query_performance_frequency(&r); - return r; -} - -HIWORD_W :: proc(wParam: Wparam) -> u16 { return u16((u32(wParam) >> 16) & 0xffff); } -HIWORD_L :: proc(lParam: Lparam) -> u16 { return u16((u32(lParam) >> 16) & 0xffff); } -LOWORD_W :: proc(wParam: Wparam) -> u16 { return u16(wParam); } -LOWORD_L :: proc(lParam: Lparam) -> u16 { return u16(lParam); } - -is_key_down :: inline proc(key: Key_Code) -> bool { return get_async_key_state(i32(key)) < 0; } - - - - -MAX_PATH :: 0x00000104; - -HANDLE_FLAG_INHERIT :: 1; -HANDLE_FLAG_PROTECT_FROM_CLOSE :: 2; - -FILE_BEGIN :: 0; -FILE_CURRENT :: 1; -FILE_END :: 2; - -FILE_SHARE_READ :: 0x00000001; -FILE_SHARE_WRITE :: 0x00000002; -FILE_SHARE_DELETE :: 0x00000004; -FILE_GENERIC_ALL :: 0x10000000; -FILE_GENERIC_EXECUTE :: 0x20000000; -FILE_GENERIC_WRITE :: 0x40000000; -FILE_GENERIC_READ :: 0x80000000; - -FILE_APPEND_DATA :: 0x0004; - -STD_INPUT_HANDLE :: -10; -STD_OUTPUT_HANDLE :: -11; -STD_ERROR_HANDLE :: -12; - -CREATE_NEW :: 1; -CREATE_ALWAYS :: 2; -OPEN_EXISTING :: 3; -OPEN_ALWAYS :: 4; -TRUNCATE_EXISTING :: 5; - -INVALID_FILE_ATTRIBUTES :: -1; - -FILE_ATTRIBUTE_READONLY :: 0x00000001; -FILE_ATTRIBUTE_HIDDEN :: 0x00000002; -FILE_ATTRIBUTE_SYSTEM :: 0x00000004; -FILE_ATTRIBUTE_DIRECTORY :: 0x00000010; -FILE_ATTRIBUTE_ARCHIVE :: 0x00000020; -FILE_ATTRIBUTE_DEVICE :: 0x00000040; -FILE_ATTRIBUTE_NORMAL :: 0x00000080; -FILE_ATTRIBUTE_TEMPORARY :: 0x00000100; -FILE_ATTRIBUTE_SPARSE_FILE :: 0x00000200; -FILE_ATTRIBUTE_REPARSE_Point :: 0x00000400; -FILE_ATTRIBUTE_COMPRESSED :: 0x00000800; -FILE_ATTRIBUTE_OFFLINE :: 0x00001000; -FILE_ATTRIBUTE_NOT_CONTENT_INDEXED :: 0x00002000; -FILE_ATTRIBUTE_ENCRYPTED :: 0x00004000; - -FILE_TYPE_DISK :: 0x0001; -FILE_TYPE_CHAR :: 0x0002; -FILE_TYPE_PIPE :: 0x0003; - - -Monitor_Info :: struct { - size: u32, - monitor: Rect, - work: Rect, - flags: u32, -} - -Window_Placement :: struct { - length: u32, - flags: u32, - show_cmd: u32, - min_pos: Point, - max_pos: Point, - normal_pos: Rect, -} - -Bitmap_Info_Header :: struct { - size: u32, - width, height: i32, - planes, bit_count: i16, - compression: u32, - size_image: u32, - x_pels_per_meter: i32, - y_pels_per_meter: i32, - clr_used: u32, - clr_important: u32, -} -Bitmap_Info :: struct { - using header: Bitmap_Info_Header, - colors: [1]Rgb_Quad, -} - - -Rgb_Quad :: struct {blue, green, red, reserved: byte} - - -Key_Code :: enum i32 { - Unknown = 0x00, - - Lbutton = 0x01, - Rbutton = 0x02, - Cancel = 0x03, - Mbutton = 0x04, - Xbutton1 = 0x05, - Xbutton2 = 0x06, - Back = 0x08, - Tab = 0x09, - Clear = 0x0C, - Return = 0x0D, - - Shift = 0x10, - Control = 0x11, - Menu = 0x12, - Pause = 0x13, - Capital = 0x14, - Kana = 0x15, - Hangeul = 0x15, - Hangul = 0x15, - Junja = 0x17, - Final = 0x18, - Hanja = 0x19, - Kanji = 0x19, - Escape = 0x1B, - Convert = 0x1C, - NonConvert = 0x1D, - Accept = 0x1E, - ModeChange = 0x1F, - Space = 0x20, - Prior = 0x21, - Next = 0x22, - End = 0x23, - Home = 0x24, - Left = 0x25, - Up = 0x26, - Right = 0x27, - Down = 0x28, - Select = 0x29, - Print = 0x2A, - Execute = 0x2B, - Snapshot = 0x2C, - Insert = 0x2D, - Delete = 0x2E, - Help = 0x2F, - - Num0 = '0', - Num1 = '1', - Num2 = '2', - Num3 = '3', - Num4 = '4', - Num5 = '5', - Num6 = '6', - Num7 = '7', - Num8 = '8', - Num9 = '9', - A = 'A', - B = 'B', - C = 'C', - D = 'D', - E = 'E', - F = 'F', - G = 'G', - H = 'H', - I = 'I', - J = 'J', - K = 'K', - L = 'L', - M = 'M', - N = 'N', - O = 'O', - P = 'P', - Q = 'Q', - R = 'R', - S = 'S', - T = 'T', - U = 'U', - V = 'V', - W = 'W', - X = 'X', - Y = 'Y', - Z = 'Z', - - Lwin = 0x5B, - Rwin = 0x5C, - Apps = 0x5D, - - Numpad0 = 0x60, - Numpad1 = 0x61, - Numpad2 = 0x62, - Numpad3 = 0x63, - Numpad4 = 0x64, - Numpad5 = 0x65, - Numpad6 = 0x66, - Numpad7 = 0x67, - Numpad8 = 0x68, - Numpad9 = 0x69, - Multiply = 0x6A, - Add = 0x6B, - Separator = 0x6C, - Subtract = 0x6D, - Decimal = 0x6E, - Divide = 0x6F, - - F1 = 0x70, - F2 = 0x71, - F3 = 0x72, - F4 = 0x73, - F5 = 0x74, - F6 = 0x75, - F7 = 0x76, - F8 = 0x77, - F9 = 0x78, - F10 = 0x79, - F11 = 0x7A, - F12 = 0x7B, - F13 = 0x7C, - F14 = 0x7D, - F15 = 0x7E, - F16 = 0x7F, - F17 = 0x80, - F18 = 0x81, - F19 = 0x82, - F20 = 0x83, - F21 = 0x84, - F22 = 0x85, - F23 = 0x86, - F24 = 0x87, - - Numlock = 0x90, - Scroll = 0x91, - Lshift = 0xA0, - Rshift = 0xA1, - Lcontrol = 0xA2, - Rcontrol = 0xA3, - Lmenu = 0xA4, - Rmenu = 0xA5, - ProcessKey = 0xE5, - Attn = 0xF6, - Crsel = 0xF7, - Exsel = 0xF8, - Ereof = 0xF9, - Play = 0xFA, - Zoom = 0xFB, - Noname = 0xFC, - Pa1 = 0xFD, - OemClear = 0xFE, -} +package win32 + +when ODIN_OS == "windows" { + foreign import "system:kernel32.lib" + foreign import "system:user32.lib" + foreign import "system:gdi32.lib" + foreign import "system:winmm.lib" + foreign import "system:shell32.lib" +} + +Handle :: distinct rawptr; +Hwnd :: distinct Handle; +Hdc :: distinct Handle; +Hinstance :: distinct Handle; +Hicon :: distinct Handle; +Hcursor :: distinct Handle; +Hmenu :: distinct Handle; +Hbrush :: distinct Handle; +Hgdiobj :: distinct Handle; +Hmodule :: distinct Handle; +Hmonitor :: distinct Handle; +Hrawinput :: distinct Handle; +HKL :: distinct Handle; +Wparam :: distinct uint; +Lparam :: distinct int; +Lresult :: distinct int; +Wnd_Proc :: distinct #type proc "c" (Hwnd, u32, Wparam, Lparam) -> Lresult; + +Long_Ptr :: distinct int; + +Bool :: distinct b32; + +Wstring :: ^u16; + +Point :: struct { + x, y: i32, +} + +Wnd_Class_Ex_A :: struct { + size, style: u32, + wnd_proc: Wnd_Proc, + cls_extra, wnd_extra: i32, + instance: Hinstance, + icon: Hicon, + cursor: Hcursor, + background: Hbrush, + menu_name, class_name: cstring, + sm: Hicon, +} + +Wnd_Class_Ex_W :: struct { + size, style: u32, + wnd_proc: Wnd_Proc, + cls_extra, wnd_extra: i32, + instance: Hinstance, + icon: Hicon, + cursor: Hcursor, + background: Hbrush, + menu_name, class_name: Wstring, + sm: Hicon, +} + + +Msg :: struct { + hwnd: Hwnd, + message: u32, + wparam: Wparam, + lparam: Lparam, + time: u32, + pt: Point, +} + +Rect :: struct { + left: i32, + top: i32, + right: i32, + bottom: i32, +} + +Filetime :: struct { + lo, hi: u32, +} + +Systemtime :: struct { + year, month: u16, + day_of_week, day: u16, + hour, minute, second, millisecond: u16, +} + +By_Handle_File_Information :: struct { + file_attributes: u32, + creation_time, + last_access_time, + last_write_time: Filetime, + volume_serial_number, + file_size_high, + file_size_low, + number_of_links, + file_index_high, + file_index_low: u32, +} + +File_Attribute_Data :: struct { + file_attributes: u32, + creation_time, + last_access_time, + last_write_time: Filetime, + file_size_high, + file_size_low: u32, +} + +Find_Data_W :: struct{ + file_attributes: u32, + creation_time: Filetime, + last_access_time: Filetime, + last_write_time: Filetime, + file_size_high: u32, + file_size_low: u32, + reserved0: u32, + reserved1: u32, + file_name: [MAX_PATH]u16, + alternate_file_name: [14]u16, +} + +Find_Data_A :: struct{ + file_attributes: u32, + creation_time: Filetime, + last_access_time: Filetime, + last_write_time: Filetime, + file_size_high: u32, + file_size_low: u32, + reserved0: u32, + reserved1: u32, + file_name: [MAX_PATH]byte, + alternate_file_name: [14]byte, +} + +Security_Attributes :: struct { + length: u32, + security_descriptor: rawptr, + inherit_handle: Bool, +} + +Process_Information :: struct { + process: Handle, + thread: Handle, + process_id: u32, + thread_id: u32 +} + +Startup_Info :: struct { + cb: u32, + reserved: Wstring, + desktop: Wstring, + title: Wstring, + x: u32, + y: u32, + x_size: u32, + y_size: u32, + x_count_chars: u32, + y_count_chars: u32, + fill_attribute: u32, + flags: u32, + show_window: u16, + _: u16, + _: cstring, + stdin: Handle, + stdout: Handle, + stderr: Handle, +} + +Pixel_Format_Descriptor :: struct { + size, + version, + flags: u32, + + pixel_type, + color_bits, + red_bits, + red_shift, + green_bits, + green_shift, + blue_bits, + blue_shift, + alpha_bits, + alpha_shift, + accum_bits, + accum_red_bits, + accum_green_bits, + accum_blue_bits, + accum_alpha_bits, + depth_bits, + stencil_bits, + aux_buffers, + layer_type, + reserved: byte, + + layer_mask, + visible_mask, + damage_mask: u32, +} + +Critical_Section :: struct { + debug_info: ^Critical_Section_Debug, + + lock_count: i32, + recursion_count: i32, + owning_thread: Handle, + lock_semaphore: Handle, + spin_count: ^u32, +} + +Critical_Section_Debug :: struct { + typ: u16, + creator_back_trace_index: u16, + critical_section: ^Critical_Section, + process_locks_list: ^List_Entry, + entry_count: u32, + contention_count: u32, + flags: u32, + creator_back_trace_index_high: u16, + spare_word: u16, +} + +List_Entry :: struct {flink, blink: ^List_Entry}; + + +Raw_Input_Device :: struct { + usage_page: u16, + usage: u16, + flags: u32, + wnd_target: Hwnd, +} + +Raw_Input_Header :: struct { + kind: u32, + size: u32, + device: Handle, + wparam: Wparam, +} + +Raw_HID :: struct { + size_hid: u32, + count: u32, + raw_data: [1]byte, +} + +Raw_Keyboard :: struct { + make_code: u16, + flags: u16, + reserved: u16, + vkey: u16, + message: u32, + extra_information: u32, +} + +Raw_Mouse :: struct { + flags: u16, + using data: struct #raw_union { + buttons: u32, + using _: struct { + button_flags: u16, + button_data: u16, + }, + }, + raw_buttons: u32, + last_x: i32, + last_y: i32, + extra_information: u32, +} + +Raw_Input :: struct { + using header: Raw_Input_Header, + data: struct #raw_union { + mouse: Raw_Mouse, + keyboard: Raw_Keyboard, + hid: Raw_HID, + }, +} + + +Overlapped :: struct { + internal: ^u64, + internal_high: ^u64, + using _: struct #raw_union { + using _: struct { + offset: u32, + offset_high: u32, + }, + pointer: rawptr, + }, + event: Handle, +} + +File_Notify_Information :: struct { + next_entry_offset: u32, + action: u32, + file_name_length: u32, + file_name: [1]u16, +} + +MAPVK_VK_TO_VSC :: 0; +MAPVK_VSC_TO_VK :: 1; +MAPVK_VK_TO_CHAR :: 2; +MAPVK_VSC_TO_VK_EX :: 3; + + + + +INVALID_HANDLE :: Handle(~uintptr(0)); + +CREATE_SUSPENDED :: 0x00000004; +STACK_SIZE_PARAM_IS_A_RESERVATION :: 0x00010000; +WAIT_ABANDONED :: 0x00000080; +WAIT_OBJECT_0 :: 0; +WAIT_TIMEOUT :: 0x00000102; +WAIT_FAILED :: 0xffffffff; + +CS_VREDRAW :: 0x0001; +CS_HREDRAW :: 0x0002; +CS_OWNDC :: 0x0020; +CW_USEDEFAULT :: -0x80000000; + +WS_OVERLAPPED :: 0; +WS_MAXIMIZEBOX :: 0x00010000; +WS_MINIMIZEBOX :: 0x00020000; +WS_THICKFRAME :: 0x00040000; +WS_SYSMENU :: 0x00080000; +WS_BORDER :: 0x00800000; +WS_CAPTION :: 0x00C00000; +WS_VISIBLE :: 0x10000000; +WS_POPUP :: 0x80000000; +WS_MAXIMIZE :: 0x01000000; +WS_MINIMIZE :: 0x20000000; +WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX; +WS_POPUPWINDOW :: WS_POPUP | WS_BORDER | WS_SYSMENU; + +WM_ACTIVATE :: 0x0006; +WM_ACTIVATEAPP :: 0x001C; +WM_CHAR :: 0x0102; +WM_CLOSE :: 0x0010; +WM_CREATE :: 0x0001; +WM_DESTROY :: 0x0002; +WM_INPUT :: 0x00ff; +WM_KEYDOWN :: 0x0100; +WM_KEYUP :: 0x0101; +WM_KILLFOCUS :: 0x0008; +WM_QUIT :: 0x0012; +WM_SETCURSOR :: 0x0020; +WM_SETFOCUS :: 0x0007; +WM_SIZE :: 0x0005; +WM_SIZING :: 0x0214; +WM_SYSKEYDOWN :: 0x0104; +WM_SYSKEYUP :: 0x0105; +WM_USER :: 0x0400; +WM_WINDOWPOSCHANGED :: 0x0047; + +WM_MOUSEWHEEL :: 0x020A; +WM_MOUSEMOVE :: 0x0200; +WM_LBUTTONDOWN :: 0x0201; +WM_LBUTTONUP :: 0x0202; +WM_LBUTTONDBLCLK :: 0x0203; +WM_RBUTTONDOWN :: 0x0204; +WM_RBUTTONUP :: 0x0205; +WM_RBUTTONDBLCLK :: 0x0206; +WM_MBUTTONDOWN :: 0x0207; +WM_MBUTTONUP :: 0x0208; +WM_MBUTTONDBLCLK :: 0x0209; + +PM_NOREMOVE :: 0x0000; +PM_REMOVE :: 0x0001; +PM_NOYIELD :: 0x0002; + +BLACK_BRUSH :: 4; + +SM_CXSCREEN :: 0; +SM_CYSCREEN :: 1; + +SW_SHOW :: 5; + +COLOR_BACKGROUND :: Hbrush(uintptr(1)); + +INVALID_SET_FILE_POINTER :: ~u32(0); +HEAP_ZERO_MEMORY :: 0x00000008; +INFINITE :: 0xffffffff; +GWL_EXSTYLE :: -20; +GWLP_HINSTANCE :: -6; +GWLP_ID :: -12; +GWL_STYLE :: -16; +GWLP_USERDATA :: -21; +GWLP_WNDPROC :: -4; +Hwnd_TOP :: Hwnd(uintptr(0)); + +BI_RGB :: 0; +DIB_RGB_COLORS :: 0x00; +SRCCOPY: u32 : 0x00cc0020; + + +MONITOR_DEFAULTTONULL :: 0x00000000; +MONITOR_DEFAULTTOPRIMARY :: 0x00000001; +MONITOR_DEFAULTTONEAREST :: 0x00000002; + +SWP_FRAMECHANGED :: 0x0020; +SWP_NOOWNERZORDER :: 0x0200; +SWP_NOZORDER :: 0x0004; +SWP_NOSIZE :: 0x0001; +SWP_NOMOVE :: 0x0002; + + +// Raw Input + + +RID_HEADER :: 0x10000005; +RID_INPUT :: 0x10000003; + + +RIDEV_APPKEYS :: 0x00000400; +RIDEV_CAPTUREMOUSE :: 0x00000200; +RIDEV_DEVNOTIFY :: 0x00002000; +RIDEV_EXCLUDE :: 0x00000010; +RIDEV_EXINPUTSINK :: 0x00001000; +RIDEV_INPUTSINK :: 0x00000100; +RIDEV_NOHOTKEYS :: 0x00000200; +RIDEV_NOLEGACY :: 0x00000030; +RIDEV_PAGEONLY :: 0x00000020; +RIDEV_REMOVE :: 0x00000001; + + +RIM_TYPEMOUSE :: 0; +RIM_TYPEKEYBOARD :: 1; +RIM_TYPEHID :: 2; + + +MOUSE_ATTRIBUTES_CHANGED :: 0x04; +MOUSE_MOVE_RELATIVE :: 0; +MOUSE_MOVE_ABSOLUTE :: 1; +MOUSE_VIRTUAL_DESKTOP :: 0x02; + + + +RI_MOUSE_BUTTON_1_DOWN :: 0x0001; +RI_MOUSE_BUTTON_1_UP :: 0x0002; +RI_MOUSE_BUTTON_2_DOWN :: 0x0004; +RI_MOUSE_BUTTON_2_UP :: 0x0008; +RI_MOUSE_BUTTON_3_DOWN :: 0x0010; +RI_MOUSE_BUTTON_3_UP :: 0x0020; +RI_MOUSE_BUTTON_4_DOWN :: 0x0040; +RI_MOUSE_BUTTON_4_UP :: 0x0080; +RI_MOUSE_BUTTON_5_DOWN :: 0x0100; +RI_MOUSE_BUTTON_5_UP :: 0x0200; +RI_MOUSE_LEFT_BUTTON_DOWN :: 0x0001; +RI_MOUSE_LEFT_BUTTON_UP :: 0x0002; +RI_MOUSE_MIDDLE_BUTTON_DOWN :: 0x0010; +RI_MOUSE_MIDDLE_BUTTON_UP :: 0x0020; +RI_MOUSE_RIGHT_BUTTON_DOWN :: 0x0004; +RI_MOUSE_RIGHT_BUTTON_UP :: 0x0008; +RI_MOUSE_WHEEL :: 0x0400; + + +RI_KEY_MAKE :: 0x00; +RI_KEY_BREAK :: 0x01; +RI_KEY_E0 :: 0x02; +RI_KEY_E1 :: 0x04; +RI_KEY_TERMSRV_SET_LED :: 0x08; +RI_KEY_TERMSRV_SHADOW :: 0x10; + +// Windows OpenGL + +PFD_TYPE_RGBA :: 0; +PFD_TYPE_COLORINDEX :: 1; +PFD_MAIN_PLANE :: 0; +PFD_OVERLAY_PLANE :: 1; +PFD_UNDERLAY_PLANE :: -1; +PFD_DOUBLEBUFFER :: 1; +PFD_STEREO :: 2; +PFD_DRAW_TO_WINDOW :: 4; +PFD_DRAW_TO_BITMAP :: 8; +PFD_SUPPORT_GDI :: 16; +PFD_SUPPORT_OPENGL :: 32; +PFD_GENERIC_FORMAT :: 64; +PFD_NEED_PALETTE :: 128; +PFD_NEED_SYSTEM_PALETTE :: 0x00000100; +PFD_SWAP_EXCHANGE :: 0x00000200; +PFD_SWAP_COPY :: 0x00000400; +PFD_SWAP_LAYER_BUFFERS :: 0x00000800; +PFD_GENERIC_ACCELERATED :: 0x00001000; +PFD_DEPTH_DONTCARE :: 0x20000000; +PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000; +PFD_STEREO_DONTCARE :: 0x80000000; + +GET_FILEEX_INFO_LEVELS :: distinct i32; +GetFileExInfoStandard: GET_FILEEX_INFO_LEVELS : 0; +GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1; + +STARTF_USESHOWWINDOW :: 0x00000001; +STARTF_USESIZE :: 0x00000002; +STARTF_USEPOSITION :: 0x00000004; +STARTF_USECOUNTCHARS :: 0x00000008; +STARTF_USEFILLATTRIBUTE :: 0x00000010; +STARTF_RUNFULLSCREEN :: 0x00000020; // ignored for non-x86 platforms +STARTF_FORCEONFEEDBACK :: 0x00000040; +STARTF_FORCEOFFFEEDBACK :: 0x00000080; +STARTF_USESTDHANDLES :: 0x00000100; +STARTF_USEHOTKEY :: 0x00000200; +STARTF_TITLEISLINKNAME :: 0x00000800; +STARTF_TITLEISAPPID :: 0x00001000; +STARTF_PREVENTPINNING :: 0x00002000; +STARTF_UNTRUSTEDSOURCE :: 0x00008000; + + +MOVEFILE_REPLACE_EXISTING :: 0x00000001; +MOVEFILE_COPY_ALLOWED :: 0x00000002; +MOVEFILE_DELAY_UNTIL_REBOOT :: 0x00000004; +MOVEFILE_WRITE_THROUGH :: 0x00000008; +MOVEFILE_CREATE_HARDLINK :: 0x00000010; +MOVEFILE_FAIL_IF_NOT_TRACKABLE :: 0x00000020; + +FILE_NOTIFY_CHANGE_FILE_NAME :: 0x00000001; +FILE_NOTIFY_CHANGE_DIR_NAME :: 0x00000002; +FILE_NOTIFY_CHANGE_ATTRIBUTES :: 0x00000004; +FILE_NOTIFY_CHANGE_SIZE :: 0x00000008; +FILE_NOTIFY_CHANGE_LAST_WRITE :: 0x00000010; +FILE_NOTIFY_CHANGE_LAST_ACCESS :: 0x00000020; +FILE_NOTIFY_CHANGE_CREATION :: 0x00000040; +FILE_NOTIFY_CHANGE_SECURITY :: 0x00000100; + +FILE_FLAG_WRITE_THROUGH :: 0x80000000; +FILE_FLAG_OVERLAPPED :: 0x40000000; +FILE_FLAG_NO_BUFFERING :: 0x20000000; +FILE_FLAG_RANDOM_ACCESS :: 0x10000000; +FILE_FLAG_SEQUENTIAL_SCAN :: 0x08000000; +FILE_FLAG_DELETE_ON_CLOSE :: 0x04000000; +FILE_FLAG_BACKUP_SEMANTICS :: 0x02000000; +FILE_FLAG_POSIX_SEMANTICS :: 0x01000000; +FILE_FLAG_SESSION_AWARE :: 0x00800000; +FILE_FLAG_OPEN_REPARSE_POINT :: 0x00200000; +FILE_FLAG_OPEN_NO_RECALL :: 0x00100000; +FILE_FLAG_FIRST_PIPE_INSTANCE :: 0x00080000; + +FILE_ACTION_ADDED :: 0x00000001; +FILE_ACTION_REMOVED :: 0x00000002; +FILE_ACTION_MODIFIED :: 0x00000003; +FILE_ACTION_RENAMED_OLD_NAME :: 0x00000004; +FILE_ACTION_RENAMED_NEW_NAME :: 0x00000005; + +CP_ACP :: 0; // default to ANSI code page +CP_OEMCP :: 1; // default to OEM code page +CP_MACCP :: 2; // default to MAC code page +CP_THREAD_ACP :: 3; // current thread's ANSI code page +CP_SYMBOL :: 42; // SYMBOL translations +CP_UTF7 :: 65000; // UTF-7 translation +CP_UTF8 :: 65001; // UTF-8 translation + +@(default_calling_convention = "std") +foreign kernel32 { + @(link_name="GetLastError") get_last_error :: proc() -> i32 ---; + @(link_name="CreateProcessA") create_process_a :: proc(application_name, command_line: cstring, + process_attributes, thread_attributes: ^Security_Attributes, + inherit_handle: Bool, creation_flags: u32, environment: rawptr, + current_direcotry: cstring, startup_info: ^Startup_Info, + process_information: ^Process_Information) -> Bool ---; + @(link_name="CreateProcessW") create_process_w :: proc(application_name, command_line: Wstring, + process_attributes, thread_attributes: ^Security_Attributes, + inherit_handle: Bool, creation_flags: u32, environment: rawptr, + current_direcotry: cstring, startup_info: ^Startup_Info, + process_information: ^Process_Information) -> Bool ---; + @(link_name="GetExitCodeProcess") get_exit_code_process :: proc(process: Handle, exit: ^u32) -> Bool ---; + @(link_name="ExitProcess") exit_process :: proc(exit_code: u32) ---; + @(link_name="GetModuleHandleA") get_module_handle_a :: proc(module_name: cstring) -> Hinstance ---; + @(link_name="GetModuleHandleW") get_module_handle_w :: proc(module_name: Wstring) -> Hinstance ---; + @(link_name="Sleep") sleep :: proc(ms: i32) -> i32 ---; + @(link_name="QueryPerformanceFrequency") query_performance_frequency :: proc(result: ^i64) -> i32 ---; + @(link_name="QueryPerformanceCounter") query_performance_counter :: proc(result: ^i64) -> i32 ---; + @(link_name="OutputDebugStringA") output_debug_string_a :: proc(c_str: cstring) ---; + + @(link_name="GetCommandLineA") get_command_line_a :: proc() -> cstring ---; + @(link_name="GetCommandLineW") get_command_line_w :: proc() -> Wstring ---; + @(link_name="GetSystemMetrics") get_system_metrics :: proc(index: i32) -> i32 ---; + @(link_name="GetCurrentThreadId") get_current_thread_id :: proc() -> u32 ---; + + @(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) ---; + @(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---; + @(link_name="FileTimeToSystemTime") file_time_to_system_time :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---; + @(link_name="SystemTimeToFileTime") system_time_to_file_time :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---; + + @(link_name="CloseHandle") close_handle :: proc(h: Handle) -> i32 ---; + @(link_name="GetStdHandle") get_std_handle :: proc(h: i32) -> Handle ---; + + @(link_name="CreateFileA") + create_file_a :: proc(filename: cstring, desired_access, share_module: u32, + security: rawptr, + creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---; + + @(link_name="CreateFileW") + create_file_w :: proc(filename: Wstring, desired_access, share_module: u32, + security: rawptr, + creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---; + + + @(link_name="ReadFile") read_file :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---; + @(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---; + + @(link_name="GetFileSizeEx") get_file_size_ex :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---; + @(link_name="GetFileAttributesA") get_file_attributes_a :: proc(filename: cstring) -> u32 ---; + @(link_name="GetFileAttributesW") get_file_attributes_w :: proc(filename: Wstring) -> u32 ---; + @(link_name="GetFileAttributesExA") get_file_attributes_ex_a :: proc(filename: cstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---; + @(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---; + + @(link_name="CreateDirectoryA") create_directory_a :: proc(path: cstring, security_attributes: ^Security_Attributes) -> Bool ---; + @(link_name="CreateDirectoryW") create_directory_w :: proc(path: Wstring, security_attributes: ^Security_Attributes) -> Bool ---; + + @(link_name="GetFileType") get_file_type :: proc(file_handle: Handle) -> u32 ---; + @(link_name="SetFilePointer") set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---; + + @(link_name="SetHandleInformation") set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool ---; + + @(link_name="FindFirstFileA") find_first_file_a :: proc(file_name: cstring, data: ^Find_Data_A) -> Handle ---; + @(link_name="FindNextFileA") find_next_file_a :: proc(file: Handle, data: ^Find_Data_A) -> Bool ---; + + @(link_name="FindFirstFileW") find_first_file_w :: proc(file_name: Wstring, data: ^Find_Data_W) -> Handle ---; + @(link_name="FindNextFileW") find_next_file_w :: proc(file: Handle, data: ^Find_Data_W) -> Bool ---; + + @(link_name="FindClose") find_close :: proc(file: Handle) -> Bool ---; + + @(link_name="MoveFileExA") move_file_ex_a :: proc(existing, new: cstring, flags: u32) -> Bool ---; + @(link_name="DeleteFileA") delete_file_a :: proc(file_name: cstring) -> Bool ---; + @(link_name="CopyFileA") copy_file_a :: proc(existing, new: cstring, fail_if_exists: Bool) -> Bool ---; + + @(link_name="MoveFileExW") move_file_ex_w :: proc(existing, new: Wstring, flags: u32) -> Bool ---; + @(link_name="DeleteFileW") delete_file_w :: proc(file_name: Wstring) -> Bool ---; + @(link_name="CopyFileW") copy_file_w :: proc(existing, new: Wstring, fail_if_exists: Bool) -> Bool ---; + + @(link_name="HeapAlloc") heap_alloc :: proc(h: Handle, flags: u32, bytes: int) -> rawptr ---; + @(link_name="HeapReAlloc") heap_realloc :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---; + @(link_name="HeapFree") heap_free :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool ---; + @(link_name="GetProcessHeap") get_process_heap :: proc() -> Handle ---; + + @(link_name="LocalAlloc") local_alloc :: proc(flags: u32, bytes: int) -> rawptr ---; + @(link_name="LocalReAlloc") local_realloc :: proc(mem: rawptr, bytes: int, flags: uint) -> rawptr ---; + @(link_name="LocalFree") local_free :: proc(mem: rawptr) -> rawptr ---; + + @(link_name="FindFirstChangeNotificationA") find_first_change_notification_a :: proc(path: cstring, watch_subtree: Bool, filter: u32) -> Handle ---; + @(link_name="FindNextChangeNotification") find_next_change_notification :: proc(h: Handle) -> Bool ---; + @(link_name="FindCloseChangeNotification") find_close_change_notification :: proc(h: Handle) -> Bool ---; + + @(link_name="ReadDirectoryChangesW") read_directory_changes_w :: proc(dir: Handle, buf: rawptr, buf_length: u32, + watch_subtree: Bool, notify_filter: u32, + bytes_returned: ^u32, overlapped: ^Overlapped, + completion: rawptr) -> Bool ---; + + @(link_name="WideCharToMultiByte") wide_char_to_multi_byte :: proc(code_page: u32, flags: u32, + wchar_str: Wstring, wchar: i32, + multi_str: cstring, multi: i32, + default_char: cstring, used_default_char: ^Bool) -> i32 ---; + + @(link_name="MultiByteToWideChar") multi_byte_to_wide_char :: proc(code_page: u32, flags: u32, + mb_str: cstring, mb: i32, + wc_str: Wstring, wc: i32) -> i32 ---; + + @(link_name="CreateSemaphoreA") create_semaphore_a :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: cstring) -> Handle ---; + @(link_name="ReleaseSemaphore") release_semaphore :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---; + @(link_name="WaitForSingleObject") wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 ---; +} + +@(default_calling_convention = "c") +foreign kernel32 { + @(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 ---; + @(link_name="InterlockedExchange") interlocked_exchange :: proc(dst: ^i32, desired: i32) -> i32 ---; + @(link_name="InterlockedExchangeAdd") interlocked_exchange_add :: proc(dst: ^i32, desired: i32) -> i32 ---; + @(link_name="InterlockedAnd") interlocked_and :: proc(dst: ^i32, desired: i32) -> i32 ---; + @(link_name="InterlockedOr") interlocked_or :: proc(dst: ^i32, desired: i32) -> i32 ---; + + @(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 ---; + @(link_name="InterlockedExchange64") interlocked_exchange64 :: proc(dst: ^i64, desired: i64) -> i64 ---; + @(link_name="InterlockedExchangeAdd64") interlocked_exchange_add64 :: proc(dst: ^i64, desired: i64) -> i64 ---; + @(link_name="InterlockedAnd64") interlocked_and64 :: proc(dst: ^i64, desired: i64) -> i64 ---; + @(link_name="InterlockedOr64") interlocked_or64 :: proc(dst: ^i64, desired: i64) -> i64 ---; +} + +@(default_calling_convention = "std") +foreign kernel32 { + @(link_name="_mm_pause") mm_pause :: proc() ---; + @(link_name="ReadWriteBarrier") read_write_barrier :: proc() ---; + @(link_name="WriteBarrier") write_barrier :: proc() ---; + @(link_name="ReadBarrier") read_barrier :: proc() ---; + + @(link_name="CreateThread") + create_thread :: proc(thread_attributes: ^Security_Attributes, stack_size: int, start_routine: rawptr, + parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---; + @(link_name="ResumeThread") resume_thread :: proc(thread: Handle) -> u32 ---; + @(link_name="GetThreadPriority") get_thread_priority :: proc(thread: Handle) -> i32 ---; + @(link_name="SetThreadPriority") set_thread_priority :: proc(thread: Handle, priority: i32) -> Bool ---; + @(link_name="GetExitCodeThread") get_exit_code_thread :: proc(thread: Handle, exit_code: ^u32) -> Bool ---; + @(link_name="TerminateThread") terminate_thread :: proc(thread: Handle, exit_code: u32) -> Bool ---; + + @(link_name="InitializeCriticalSection") initialize_critical_section :: proc(critical_section: ^Critical_Section) ---; + @(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) ---; + @(link_name="DeleteCriticalSection") delete_critical_section :: proc(critical_section: ^Critical_Section) ---; + @(link_name="SetCriticalSectionSpinCount") set_critical_section_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) -> u32 ---; + @(link_name="TryEnterCriticalSection") try_enter_critical_section :: proc(critical_section: ^Critical_Section) -> Bool ---; + @(link_name="EnterCriticalSection") enter_critical_section :: proc(critical_section: ^Critical_Section) ---; + @(link_name="LeaveCriticalSection") leave_critical_section :: proc(critical_section: ^Critical_Section) ---; + + @(link_name="CreateEventA") create_event_a :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: cstring) -> Handle ---; + + @(link_name="LoadLibraryA") load_library_a :: proc(c_str: cstring) -> Hmodule ---; + @(link_name="LoadLibraryW") load_library_w :: proc(c_str: Wstring) -> Hmodule ---; + @(link_name="FreeLibrary") free_library :: proc(h: Hmodule) ---; + @(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---; + +} + +@(default_calling_convention = "std") +foreign user32 { + @(link_name="GetDesktopWindow") get_desktop_window :: proc() -> Hwnd ---; + @(link_name="ShowCursor") show_cursor :: proc(show: Bool) ---; + @(link_name="GetCursorPos") get_cursor_pos :: proc(p: ^Point) -> Bool ---; + @(link_name="SetCursorPos") set_cursor_pos :: proc(x, y: i32) -> Bool ---; + @(link_name="ScreenToClient") screen_to_client :: proc(h: Hwnd, p: ^Point) -> Bool ---; + @(link_name="ClientToScreen") client_to_screen :: proc(h: Hwnd, p: ^Point) -> Bool ---; + @(link_name="PostQuitMessage") post_quit_message :: proc(exit_code: i32) ---; + @(link_name="SetWindowTextA") set_window_text_a :: proc(hwnd: Hwnd, c_string: cstring) -> Bool ---; + @(link_name="RegisterClassExA") register_class_ex_a :: proc(wc: ^Wnd_Class_Ex_A) -> i16 ---; + @(link_name="RegisterClassExW") register_class_ex_w :: proc(wc: ^Wnd_Class_Ex_W) -> i16 ---; + + @(link_name="CreateWindowExA") + create_window_ex_a :: proc(ex_style: u32, + class_name, title: cstring, + style: u32, + x, y, w, h: i32, + parent: Hwnd, menu: Hmenu, instance: Hinstance, + param: rawptr) -> Hwnd ---; + + @(link_name="CreateWindowExW") + create_window_ex_w :: proc(ex_style: u32, + class_name, title: Wstring, + style: u32, + x, y, w, h: i32, + parent: Hwnd, menu: Hmenu, instance: Hinstance, + param: rawptr) -> Hwnd ---; + + @(link_name="ShowWindow") show_window :: proc(hwnd: Hwnd, cmd_show: i32) -> Bool ---; + @(link_name="TranslateMessage") translate_message :: proc(msg: ^Msg) -> Bool ---; + @(link_name="DispatchMessageA") dispatch_message_a :: proc(msg: ^Msg) -> Lresult ---; + @(link_name="DispatchMessageW") dispatch_message_w :: proc(msg: ^Msg) -> Lresult ---; + @(link_name="UpdateWindow") update_window :: proc(hwnd: Hwnd) -> Bool ---; + @(link_name="GetMessageA") get_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max: u32) -> Bool ---; + @(link_name="GetMessageW") get_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max: u32) -> Bool ---; + + @(link_name="PeekMessageA") peek_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---; + @(link_name="PeekMessageW") peek_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---; + + + @(link_name="PostMessageA") post_message :: proc(hwnd: Hwnd, msg, wparam, lparam: u32) -> Bool ---; + + @(link_name="DefWindowProcA") def_window_proc_a :: proc(hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult ---; + + @(link_name="AdjustWindowRect") adjust_window_rect :: proc(rect: ^Rect, style: u32, menu: Bool) -> Bool ---; + @(link_name="GetActiveWindow") get_active_window :: proc() -> Hwnd ---; + + @(link_name="DestroyWindow") destroy_window :: proc(wnd: Hwnd) -> Bool ---; + @(link_name="DescribePixelFormat") describe_pixel_format :: proc(dc: Hdc, pixel_format: i32, bytes: u32, pfd: ^Pixel_Format_Descriptor) -> i32 ---; + + @(link_name="GetMonitor_InfoA") get_monitor_info_a :: proc(monitor: Hmonitor, mi: ^Monitor_Info) -> Bool ---; + @(link_name="MonitorFromWindow") monitor_from_window :: proc(wnd: Hwnd, flags: u32) -> Hmonitor ---; + + @(link_name="SetWindowPos") set_window_pos :: proc(wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) ---; + + @(link_name="GetWindowPlacement") get_window_placement :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---; + @(link_name="SetWindowPlacement") set_window_placement :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---; + @(link_name="GetWindowRect") get_window_rect :: proc(wnd: Hwnd, rect: ^Rect) -> Bool ---; + + @(link_name="GetWindowLongPtrA") get_window_long_ptr_a :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---; + @(link_name="SetWindowLongPtrA") set_window_long_ptr_a :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---; + @(link_name="GetWindowLongPtrW") get_window_long_ptr_w :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---; + @(link_name="SetWindowLongPtrW") set_window_long_ptr_w :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---; + + @(link_name="GetWindowText") get_window_text :: proc(wnd: Hwnd, str: cstring, maxCount: i32) -> i32 ---; + + @(link_name="GetClientRect") get_client_rect :: proc(hwnd: Hwnd, rect: ^Rect) -> Bool ---; + + @(link_name="GetDC") get_dc :: proc(h: Hwnd) -> Hdc ---; + @(link_name="ReleaseDC") release_dc :: proc(wnd: Hwnd, hdc: Hdc) -> i32 ---; + + @(link_name="MapVirtualKeyA") map_virtual_key_a :: proc(scancode: u32, map_type: u32) -> u32 ---; + @(link_name="MapVirtualKeyW") map_virtual_key_w :: proc(scancode: u32, map_type: u32) -> u32 ---; + + @(link_name="GetKeyState") get_key_state :: proc(v_key: i32) -> i16 ---; + @(link_name="GetAsyncKeyState") get_async_key_state :: proc(v_key: i32) -> i16 ---; + + @(link_name="SetForegroundWindow") set_foreground_window :: proc(h: Hwnd) -> Bool ---; + @(link_name="SetFocus") set_focus :: proc(h: Hwnd) -> Hwnd ---; + + + @(link_name="LoadImageA") load_image_a :: proc(instance: Hinstance, name: cstring, type_: u32, x_desired, y_desired : i32, load : u32) -> Handle ---; + @(link_name="LoadIconA") load_icon_a :: proc(instance: Hinstance, icon_name: cstring) -> Hicon ---; + @(link_name="DestroyIcon") destroy_icon :: proc(icon: Hicon) -> Bool ---; + + @(link_name="LoadCursorA") load_cursor_a :: proc(instance: Hinstance, cursor_name: cstring) -> Hcursor ---; + @(link_name="GetCursor") get_cursor :: proc() -> Hcursor ---; + @(link_name="SetCursor") set_cursor :: proc(cursor: Hcursor) -> Hcursor ---; + + @(link_name="RegisterRawInputDevices") register_raw_input_devices :: proc(raw_input_device: ^Raw_Input_Device, num_devices, size: u32) -> Bool ---; + + @(link_name="GetRawInputData") get_raw_input_data :: proc(raw_input: Hrawinput, command: u32, data: rawptr, size: ^u32, size_header: u32) -> u32 ---; + + @(link_name="MapVirtualKeyExW") map_virtual_key_ex_w :: proc(code, map_type: u32, hkl: HKL) ---; + @(link_name="MapVirtualKeyExA") map_virtual_key_ex_a :: proc(code, map_type: u32, hkl: HKL) ---; +} + +@(default_calling_convention = "std") +foreign gdi32 { + @(link_name="GetStockObject") get_stock_object :: proc(fn_object: i32) -> Hgdiobj ---; + + @(link_name="StretchDIBits") + stretch_dibits :: proc(hdc: Hdc, + x_dst, y_dst, width_dst, height_dst: i32, + x_src, y_src, width_src, header_src: i32, + bits: rawptr, bits_info: ^Bitmap_Info, + usage: u32, + rop: u32) -> i32 ---; + + @(link_name="SetPixelFormat") set_pixel_format :: proc(hdc: Hdc, pixel_format: i32, pfd: ^Pixel_Format_Descriptor) -> Bool ---; + @(link_name="ChoosePixelFormat") choose_pixel_format :: proc(hdc: Hdc, pfd: ^Pixel_Format_Descriptor) -> i32 ---; + @(link_name="SwapBuffers") swap_buffers :: proc(hdc: Hdc) -> Bool ---; + +} + +@(default_calling_convention = "std") +foreign shell32 { + @(link_name="CommandLineToArgvW") command_line_to_argv_w :: proc(cmd_list: Wstring, num_args: ^i32) -> ^Wstring ---; +} + +@(default_calling_convention = "std") +foreign winmm { + @(link_name="timeGetTime") time_get_time :: proc() -> u32 ---; +} + + + +get_query_performance_frequency :: proc() -> i64 { + r: i64; + query_performance_frequency(&r); + return r; +} + +HIWORD_W :: proc(wParam: Wparam) -> u16 { return u16((u32(wParam) >> 16) & 0xffff); } +HIWORD_L :: proc(lParam: Lparam) -> u16 { return u16((u32(lParam) >> 16) & 0xffff); } +LOWORD_W :: proc(wParam: Wparam) -> u16 { return u16(wParam); } +LOWORD_L :: proc(lParam: Lparam) -> u16 { return u16(lParam); } + +is_key_down :: inline proc(key: Key_Code) -> bool { return get_async_key_state(i32(key)) < 0; } + + + + +MAX_PATH :: 0x00000104; + +HANDLE_FLAG_INHERIT :: 1; +HANDLE_FLAG_PROTECT_FROM_CLOSE :: 2; + +FILE_BEGIN :: 0; +FILE_CURRENT :: 1; +FILE_END :: 2; + +FILE_SHARE_READ :: 0x00000001; +FILE_SHARE_WRITE :: 0x00000002; +FILE_SHARE_DELETE :: 0x00000004; +FILE_GENERIC_ALL :: 0x10000000; +FILE_GENERIC_EXECUTE :: 0x20000000; +FILE_GENERIC_WRITE :: 0x40000000; +FILE_GENERIC_READ :: 0x80000000; + +FILE_APPEND_DATA :: 0x0004; + +STD_INPUT_HANDLE :: -10; +STD_OUTPUT_HANDLE :: -11; +STD_ERROR_HANDLE :: -12; + +CREATE_NEW :: 1; +CREATE_ALWAYS :: 2; +OPEN_EXISTING :: 3; +OPEN_ALWAYS :: 4; +TRUNCATE_EXISTING :: 5; + +INVALID_FILE_ATTRIBUTES :: -1; + +FILE_ATTRIBUTE_READONLY :: 0x00000001; +FILE_ATTRIBUTE_HIDDEN :: 0x00000002; +FILE_ATTRIBUTE_SYSTEM :: 0x00000004; +FILE_ATTRIBUTE_DIRECTORY :: 0x00000010; +FILE_ATTRIBUTE_ARCHIVE :: 0x00000020; +FILE_ATTRIBUTE_DEVICE :: 0x00000040; +FILE_ATTRIBUTE_NORMAL :: 0x00000080; +FILE_ATTRIBUTE_TEMPORARY :: 0x00000100; +FILE_ATTRIBUTE_SPARSE_FILE :: 0x00000200; +FILE_ATTRIBUTE_REPARSE_Point :: 0x00000400; +FILE_ATTRIBUTE_COMPRESSED :: 0x00000800; +FILE_ATTRIBUTE_OFFLINE :: 0x00001000; +FILE_ATTRIBUTE_NOT_CONTENT_INDEXED :: 0x00002000; +FILE_ATTRIBUTE_ENCRYPTED :: 0x00004000; + +FILE_TYPE_DISK :: 0x0001; +FILE_TYPE_CHAR :: 0x0002; +FILE_TYPE_PIPE :: 0x0003; + + +Monitor_Info :: struct { + size: u32, + monitor: Rect, + work: Rect, + flags: u32, +} + +Window_Placement :: struct { + length: u32, + flags: u32, + show_cmd: u32, + min_pos: Point, + max_pos: Point, + normal_pos: Rect, +} + +Bitmap_Info_Header :: struct { + size: u32, + width, height: i32, + planes, bit_count: i16, + compression: u32, + size_image: u32, + x_pels_per_meter: i32, + y_pels_per_meter: i32, + clr_used: u32, + clr_important: u32, +} +Bitmap_Info :: struct { + using header: Bitmap_Info_Header, + colors: [1]Rgb_Quad, +} + + +Rgb_Quad :: struct {blue, green, red, reserved: byte} + + +Key_Code :: enum i32 { + Unknown = 0x00, + + Lbutton = 0x01, + Rbutton = 0x02, + Cancel = 0x03, + Mbutton = 0x04, + Xbutton1 = 0x05, + Xbutton2 = 0x06, + Back = 0x08, + Tab = 0x09, + Clear = 0x0C, + Return = 0x0D, + + Shift = 0x10, + Control = 0x11, + Menu = 0x12, + Pause = 0x13, + Capital = 0x14, + Kana = 0x15, + Hangeul = 0x15, + Hangul = 0x15, + Junja = 0x17, + Final = 0x18, + Hanja = 0x19, + Kanji = 0x19, + Escape = 0x1B, + Convert = 0x1C, + NonConvert = 0x1D, + Accept = 0x1E, + ModeChange = 0x1F, + Space = 0x20, + Prior = 0x21, + Next = 0x22, + End = 0x23, + Home = 0x24, + Left = 0x25, + Up = 0x26, + Right = 0x27, + Down = 0x28, + Select = 0x29, + Print = 0x2A, + Execute = 0x2B, + Snapshot = 0x2C, + Insert = 0x2D, + Delete = 0x2E, + Help = 0x2F, + + Num0 = '0', + Num1 = '1', + Num2 = '2', + Num3 = '3', + Num4 = '4', + Num5 = '5', + Num6 = '6', + Num7 = '7', + Num8 = '8', + Num9 = '9', + A = 'A', + B = 'B', + C = 'C', + D = 'D', + E = 'E', + F = 'F', + G = 'G', + H = 'H', + I = 'I', + J = 'J', + K = 'K', + L = 'L', + M = 'M', + N = 'N', + O = 'O', + P = 'P', + Q = 'Q', + R = 'R', + S = 'S', + T = 'T', + U = 'U', + V = 'V', + W = 'W', + X = 'X', + Y = 'Y', + Z = 'Z', + + Lwin = 0x5B, + Rwin = 0x5C, + Apps = 0x5D, + + Numpad0 = 0x60, + Numpad1 = 0x61, + Numpad2 = 0x62, + Numpad3 = 0x63, + Numpad4 = 0x64, + Numpad5 = 0x65, + Numpad6 = 0x66, + Numpad7 = 0x67, + Numpad8 = 0x68, + Numpad9 = 0x69, + Multiply = 0x6A, + Add = 0x6B, + Separator = 0x6C, + Subtract = 0x6D, + Decimal = 0x6E, + Divide = 0x6F, + + F1 = 0x70, + F2 = 0x71, + F3 = 0x72, + F4 = 0x73, + F5 = 0x74, + F6 = 0x75, + F7 = 0x76, + F8 = 0x77, + F9 = 0x78, + F10 = 0x79, + F11 = 0x7A, + F12 = 0x7B, + F13 = 0x7C, + F14 = 0x7D, + F15 = 0x7E, + F16 = 0x7F, + F17 = 0x80, + F18 = 0x81, + F19 = 0x82, + F20 = 0x83, + F21 = 0x84, + F22 = 0x85, + F23 = 0x86, + F24 = 0x87, + + Numlock = 0x90, + Scroll = 0x91, + Lshift = 0xA0, + Rshift = 0xA1, + Lcontrol = 0xA2, + Rcontrol = 0xA3, + Lmenu = 0xA4, + Rmenu = 0xA5, + ProcessKey = 0xE5, + Attn = 0xF6, + Crsel = 0xF7, + Exsel = 0xF8, + Ereof = 0xF9, + Play = 0xFA, + Zoom = 0xFB, + Noname = 0xFC, + Pa1 = 0xFD, + OemClear = 0xFE, +} diff --git a/core/thread.odin b/core/thread/thread.odin similarity index 98% rename from core/thread.odin rename to core/thread/thread.odin index 71701a5a1..5c471cbfe 100644 --- a/core/thread.odin +++ b/core/thread/thread.odin @@ -1,3 +1,5 @@ +package thread + #assert(ODIN_OS == "windows"); when ODIN_OS == "windows" { @@ -77,4 +79,4 @@ destroy :: proc(thread: ^Thread) { terminate :: proc(using thread : ^Thread, exit_code : u32) { win32.terminate_thread(win32_thread, exit_code); -} \ No newline at end of file +} diff --git a/core/types.odin b/core/types/types.odin similarity index 99% rename from core/types.odin rename to core/types/types.odin index 0ba87f14d..ff892199d 100644 --- a/core/types.odin +++ b/core/types/types.odin @@ -1,3 +1,5 @@ +package types + are_types_identical :: proc(a, b: ^Type_Info) -> bool { if a == b do return true; diff --git a/core/utf16.odin b/core/unicode/utf16/utf16.odin similarity index 97% rename from core/utf16.odin rename to core/unicode/utf16/utf16.odin index 2a677ee06..44517820e 100644 --- a/core/utf16.odin +++ b/core/unicode/utf16/utf16.odin @@ -1,4 +1,6 @@ -import "utf8.odin" +package utf8 + +import "core:unicode/utf8" REPLACEMENT_CHAR :: '\uFFFD'; MAX_RUNE :: '\U0010FFFF'; diff --git a/core/utf8.odin b/core/unicode/utf8/utf8.odin similarity index 99% rename from core/utf8.odin rename to core/unicode/utf8/utf8.odin index d108fec91..cfdadd790 100644 --- a/core/utf8.odin +++ b/core/unicode/utf8/utf8.odin @@ -1,3 +1,5 @@ +package utf8 + RUNE_ERROR :: '\ufffd'; RUNE_SELF :: 0x80; RUNE_BOM :: 0xfeff; diff --git a/src/common.cpp b/src/common.cpp index ac73cf40e..194f1e386 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -734,3 +734,103 @@ String path_to_full_path(gbAllocator a, String path) { char *fullpath = gb_path_get_full_name(a, path_c); return make_string_c(fullpath); } + + + +struct FileInfo { + String name; + String fullpath; + i64 size; + bool is_dir; +}; + +enum ReadDirectoryError { + ReadDirectory_None, + + ReadDirectory_InvalidPath, + ReadDirectory_NotDir, + ReadDirectory_EOF, + ReadDirectory_Unknown, + + ReadDirectory_COUNT, +}; + +#if defined(GB_SYSTEM_WINDOWS) + +ReadDirectoryError read_directory(String path, Array *fi) { + GB_ASSERT(fi != nullptr); + + while (path.len > 0) { + Rune end = path[path.len-1]; + if (end == '/') { + path.len -= 1; + } else if (end == '\\') { + path.len -= 1; + } else { + break; + } + } + + if (path.len == 0) { + return ReadDirectory_InvalidPath; + } + if (!path_is_directory(path)) { + return ReadDirectory_NotDir; + } + + gbAllocator a = heap_allocator(); + + char *new_path = gb_alloc_array(a, char, path.len+3); + defer (gb_free(a, new_path)); + + gb_memmove(new_path, path.text, path.len); + gb_memmove(new_path+path.len, "/*", 2); + new_path[path.len+2] = 0; + + String np = make_string(cast(u8 *)new_path, path.len+2); + String16 wstr = string_to_string16(a, np); + defer (gb_free(a, wstr.text)); + + WIN32_FIND_DATAW file_data = {}; + HANDLE find_file = FindFirstFileW(wstr.text, &file_data); + if (find_file == INVALID_HANDLE_VALUE) { + return ReadDirectory_Unknown; + } + defer (FindClose(find_file)); + + array_init(fi, a, 0, 100); + + do { + wchar_t *filename_w = file_data.cFileName; + i64 size = cast(i64)file_data.nFileSizeLow; + size |= (cast(i64)file_data.nFileSizeHigh) << 32; + String name = string16_to_string(a, make_string16_c(filename_w)); + if (name == "." || name == "..") { + gb_free(a, name.text); + continue; + } + + String filepath = {}; + filepath.len = path.len+1+name.len; + filepath.text = gb_alloc_array(a, u8, filepath.len+1); + defer (gb_free(a, filepath.text)); + gb_memmove(filepath.text, path.text, path.len); + gb_memmove(filepath.text+path.len, "/", 1); + gb_memmove(filepath.text+path.len+1, name.text, name.len); + + FileInfo info = {}; + info.name = name; + info.fullpath = path_to_full_path(a, filepath); + info.size = size; + info.is_dir = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + array_add(fi, info); + + } while (FindNextFileW(find_file, &file_data)); + + + return ReadDirectory_None; +} + +#else +#error Implement read_directory +#endif diff --git a/src/docs.cpp b/src/docs.cpp index c0a32287c..bfc34232e 100644 --- a/src/docs.cpp +++ b/src/docs.cpp @@ -93,15 +93,15 @@ void print_declaration(AstNode *decl) { } void generate_documentation(Parser *parser) { - for_array(file_index, parser->files) { - AstFile *file = parser->files[file_index]; - Tokenizer *tokenizer = &file->tokenizer; - String fullpath = tokenizer->fullpath; - gb_printf("%.*s\n", LIT(fullpath)); + // for_array(file_index, parser->files) { + // AstFile *file = parser->files[file_index]; + // Tokenizer *tokenizer = &file->tokenizer; + // String fullpath = tokenizer->fullpath; + // gb_printf("%.*s\n", LIT(fullpath)); - for_array(decl_index, file->decls) { - AstNode *decl = file->decls[decl_index]; - print_declaration(decl); - } - } + // for_array(decl_index, file->decls) { + // AstNode *decl = file->decls[decl_index]; + // print_declaration(decl); + // } + // } } diff --git a/src/main.cpp b/src/main.cpp index d528de0b9..3d56d73b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,11 +12,13 @@ #include "checker.hpp" #include "parser.cpp" +#if 0 #include "docs.cpp" #include "checker.cpp" #include "ir.cpp" #include "ir_opt.cpp" #include "ir_print.cpp" +#endif // NOTE(bill): 'name' is used in debugging and profiling modes i32 system_exec_command_line_app(char *name, bool is_silent, char *fmt, ...) { @@ -772,8 +774,9 @@ int main(int arg_count, char **arg_ptr) { print_usage_line(0, "%s 32-bit is not yet supported", args[0]); return 1; } +#if 0 init_universal_scope(); - +#endif // TODO(bill): prevent compiling without a linker timings_start_section(&timings, str_lit("parse files")); @@ -784,10 +787,11 @@ int main(int arg_count, char **arg_ptr) { } defer (destroy_parser(&parser)); - if (parse_files(&parser, init_filename) != ParseFile_None) { + if (parse_packages(&parser, init_filename) != ParseFile_None) { return 1; } +#if 0 if (build_context.generate_docs) { generate_documentation(&parser); return 0; @@ -1054,6 +1058,6 @@ int main(int arg_count, char **arg_ptr) { system_exec_command_line_app("odin run", false, "%.*s", LIT(output_base)); } #endif - +#endif return 0; } diff --git a/src/parser.cpp b/src/parser.cpp index 1fcc7140d..4321ec3f3 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3712,17 +3712,18 @@ AstNode *parse_stmt(AstFile *f) { Token name = expect_token(f, Token_Ident); String tag = name.string; - if (tag == "shared_global_scope") { - if (f->curr_proc == nullptr) { - f->is_global_scope = true; - s = ast_empty_stmt(f, f->curr_token); - } else { - syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope"); - s = ast_bad_decl(f, token, f->curr_token); - } - expect_semicolon(f, s); - return s; - } else if (tag == "bounds_check") { + // if (tag == "shared_global_scope") { + // if (f->curr_proc == nullptr) { + // f->is_global_scope = true; + // s = ast_empty_stmt(f, f->curr_token); + // } else { + // syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope"); + // s = ast_bad_decl(f, token, f->curr_token); + // } + // expect_semicolon(f, s); + // return s; + // } else + if (tag == "bounds_check") { s = parse_stmt(f); s->stmt_state_flags |= StmtStateFlag_bounds_check; if ((s->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) { @@ -3830,6 +3831,7 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) { isize init_token_cap = cast(isize)gb_max(next_pow2(cast(i64)(file_size/2ll)), 16); array_init(&f->tokens, heap_allocator(), 0, gb_max(init_token_cap, 16)); + if (err == TokenizerInit_Empty) { Token token = {Token_EOF}; token.pos.file = fullpath; @@ -3881,8 +3883,9 @@ void destroy_ast_file(AstFile *f) { bool init_parser(Parser *p) { GB_ASSERT(p != nullptr); - array_init(&p->files, heap_allocator()); + map_init(&p->packages, heap_allocator()); array_init(&p->imports, heap_allocator()); + array_init(&p->files, heap_allocator()); gb_mutex_init(&p->file_add_mutex); gb_mutex_init(&p->file_decl_mutex); return true; @@ -3921,8 +3924,8 @@ bool try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos) } } - ImportedFile item = {}; - item.kind = ImportedFile_Normal; + ImportedPackage item = {}; + item.kind = ImportedPackage_Normal; item.path = path; item.rel_path = rel_path; item.pos = pos; @@ -4164,28 +4167,33 @@ void parse_file(Parser *p, AstFile *f) { comsume_comment_groups(f, f->prev_token); + + f->package_token = expect_token(f, Token_package); + Token package_name = expect_token_after(f, Token_Ident, "package"); + if (package_name.kind == Token_Ident) { + if (package_name.string == "_") { + error(package_name, "Invalid package name '_'"); + } else if (f->package->kind != ImportedPackage_Runtime && package_name.string == "runtime") { + error(package_name, "Use of reserved package name '%.*s'", LIT(package_name.string)); + } + } + f->package_name = package_name.string; + f->decls = parse_stmt_list(f); parse_setup_file_decls(p, f, base_dir, f->decls); } - -ParseFileError parse_import(Parser *p, ImportedFile imported_file) { - String import_path = imported_file.path; - String import_rel_path = imported_file.rel_path; - TokenPos pos = imported_file.pos; +ParseFileError parse_imported_file(Parser *p, AstPackage *package, FileInfo *fi, TokenPos pos) { AstFile *file = gb_alloc_item(heap_allocator(), AstFile); - file->file_kind = imported_file.kind; - if (file->file_kind == ImportedFile_Shared) { - file->is_global_scope = true; - } + file->package = package; TokenPos err_pos = {0}; - ParseFileError err = init_ast_file(file, import_path, &err_pos); + ParseFileError err = init_ast_file(file, fi->fullpath, &err_pos); if (err != ParseFile_None) { if (err == ParseFile_EmptyFile) { - if (import_path == p->init_fullpath) { + if (fi->fullpath == p->init_fullpath) { gb_printf_err("Initial file is empty - %.*s\n", LIT(p->init_fullpath)); gb_exit(1); } @@ -4195,7 +4203,7 @@ ParseFileError parse_import(Parser *p, ImportedFile imported_file) { if (pos.line != 0) { gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column); } - gb_printf_err("Failed to parse file: %.*s\n\t", LIT(import_rel_path)); + gb_printf_err("Failed to parse file: %.*s\n\t", LIT(fi->name)); switch (err) { case ParseFile_WrongExtension: gb_printf_err("Invalid file extension: File must have the extension '.odin'"); @@ -4207,7 +4215,7 @@ ParseFileError parse_import(Parser *p, ImportedFile imported_file) { gb_printf_err("File permissions problem"); break; case ParseFile_NotFound: - gb_printf_err("File cannot be found ('%.*s')", LIT(import_path)); + gb_printf_err("File cannot be found ('%.*s')", LIT(fi->fullpath)); break; case ParseFile_InvalidToken: gb_printf_err("Invalid token found in file at (%td:%td)", err_pos.line, err_pos.column); @@ -4230,8 +4238,17 @@ skip: parse_file(p, file); gb_mutex_lock(&p->file_add_mutex); - file->id = imported_file.index; + // file->id = imported_package.index; + HashKey key = hash_string(fi->fullpath); + map_set(&package->files, key, file); array_add(&p->files, file); + + if (package->name.len == 0) { + package->name = file->package_name; + } else if (file->tokens.count > 0 && package->name != file->package_name) { + error(file->package_token, "Different package name, expected '%.*s', got '%.*s'", LIT(package->name), LIT(file->package_name)); + } + p->total_line_count += file->tokenizer.line_count; p->total_token_count += file->tokens.count; gb_mutex_unlock(&p->file_add_mutex); @@ -4239,12 +4256,74 @@ skip: return ParseFile_None; } + +ParseFileError parse_import(Parser *p, ImportedPackage imported_package) { + String import_path = imported_package.path; + String import_rel_path = imported_package.rel_path; + TokenPos pos = imported_package.pos; + + HashKey path_key = hash_string(import_path); + if (map_get(&p->packages, path_key) != nullptr) { + return ParseFile_None; + } + + + Array list = {}; + ReadDirectoryError rd_err = read_directory(import_path, &list); + defer (array_free(&list)); + + if (rd_err != ReadDirectory_EOF && rd_err != ReadDirectory_None && pos.line != 0) { + gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column); + } + + switch (rd_err) { + case ReadDirectory_InvalidPath: + gb_printf_err("Invalid path: %.*s\n", LIT(import_rel_path)); + gb_mutex_lock(&global_error_collector.mutex); + global_error_collector.count++; + gb_mutex_unlock(&global_error_collector.mutex); + return ParseFile_InvalidFile; + case ReadDirectory_NotDir: + gb_printf_err("Expected a directory for a package, got a file: %.*s\n", LIT(import_rel_path)); + gb_mutex_lock(&global_error_collector.mutex); + global_error_collector.count++; + gb_mutex_unlock(&global_error_collector.mutex); + return ParseFile_InvalidFile; + case ReadDirectory_Unknown: + gb_printf_err("Unknown error whilst reading directory"); + gb_mutex_lock(&global_error_collector.mutex); + global_error_collector.count++; + gb_mutex_unlock(&global_error_collector.mutex); + return ParseFile_InvalidFile; + case ReadDirectory_EOF: + break; + } + + AstPackage *package = gb_alloc_item(heap_allocator(), AstPackage); + package->kind = imported_package.kind; + package->fullpath = import_path; + map_init(&package->files, heap_allocator()); + + // TODO(bill): Fix concurrency + for_array(i, list) { + FileInfo *fi = &list[i]; + if (string_ends_with(fi->name, str_lit(".odin"))) { + ParseFileError err = parse_imported_file(p, package, fi, pos); + if (err != ParseFile_None) { + return err; + } + } + } + + return ParseFile_None; +} + GB_THREAD_PROC(parse_worker_file_proc) { if (thread == nullptr) return 0; auto *p = cast(Parser *)thread->user_data; isize index = thread->user_index; - ImportedFile imported_file = p->imports[index]; - ParseFileError err = parse_import(p, imported_file); + ImportedPackage imported_package = p->imports[index]; + ParseFileError err = parse_import(p, imported_package); return cast(isize)err; } @@ -4254,29 +4333,24 @@ struct ParserThreadWork { isize import_index; }; -ParseFileError parse_files(Parser *p, String init_filename) { +ParseFileError parse_packages(Parser *p, String init_filename) { GB_ASSERT(init_filename.text[init_filename.len] == 0); char *fullpath_str = gb_path_get_full_name(heap_allocator(), cast(char *)&init_filename[0]); String init_fullpath = string_trim_whitespace(make_string_c(fullpath_str)); + TokenPos init_pos = {}; - ImportedFile init_imported_file = {ImportedFile_Init, init_fullpath, init_fullpath, init_pos}; + ImportedPackage init_imported_package = {ImportedPackage_Init, init_fullpath, init_fullpath, init_pos}; isize shared_file_count = 0; if (!build_context.generate_docs) { - String s = get_fullpath_core(heap_allocator(), str_lit("_preload.odin")); - ImportedFile runtime_file = {ImportedFile_Shared, s, s, init_pos}; - array_add(&p->imports, runtime_file); - shared_file_count++; - } - if (!build_context.generate_docs) { - String s = get_fullpath_core(heap_allocator(), str_lit("_soft_numbers.odin")); - ImportedFile runtime_file = {ImportedFile_Shared, s, s, init_pos}; - array_add(&p->imports, runtime_file); + String s = get_fullpath_core(heap_allocator(), str_lit("runtime")); + ImportedPackage runtime_package = {ImportedPackage_Runtime, s, s, init_pos}; + array_add(&p->imports, runtime_package); shared_file_count++; } - array_add(&p->imports, init_imported_file); + array_add(&p->imports, init_imported_package); p->init_fullpath = init_fullpath; // IMPORTANT TODO(bill): Figure out why this doesn't work on *nix sometimes diff --git a/src/parser.hpp b/src/parser.hpp index 0fb12a460..d99562d2e 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -3,6 +3,8 @@ struct Scope; struct Type; struct Entity; struct DeclInfo; +struct AstFile; +struct AstPackage; enum ParseFileError { @@ -23,23 +25,23 @@ struct CommentGroup { }; -enum ImportedFileKind { - ImportedFile_Normal, - ImportedFile_Shared, - ImportedFile_Init, +enum ImportedPackageKind { + ImportedPackage_Normal, + ImportedPackage_Runtime, + ImportedPackage_Init, }; -struct ImportedFile { - ImportedFileKind kind; - String path; - String rel_path; - TokenPos pos; // import - isize index; +struct ImportedPackage { + ImportedPackageKind kind; + String path; + String rel_path; + TokenPos pos; // import + isize index; }; - struct AstFile { - isize id; + AstPackage * package; + // isize id; String fullpath; gbArena arena; Tokenizer tokenizer; @@ -47,6 +49,8 @@ struct AstFile { isize curr_token_index; Token curr_token; Token prev_token; // previous non-comment + Token package_token; + String package_name; // >= 0: In Expression // < 0: In Control Clause @@ -58,8 +62,6 @@ struct AstFile { isize when_level; Array decls; - ImportedFileKind file_kind; - bool is_global_scope; Array imports_and_exports; // 'import' 'using import' 'export' @@ -81,14 +83,23 @@ struct AstFile { }; +struct AstPackage { + ImportedPackageKind kind; + String name; + String fullpath; + Map files; // Key: String (names) +}; + + struct Parser { - String init_fullpath; - Array files; - Array imports; - isize total_token_count; - isize total_line_count; - gbMutex file_add_mutex; - gbMutex file_decl_mutex; + String init_fullpath; + Map packages; // Key: String (fullpath) + Array files; + Array imports; + isize total_token_count; + isize total_line_count; + gbMutex file_add_mutex; + gbMutex file_decl_mutex; }; enum ProcInlining { @@ -526,3 +537,4 @@ gb_inline bool is_ast_node_when_stmt(AstNode *node) { return node->kind == AstNode_WhenStmt; } + diff --git a/src/string.cpp b/src/string.cpp index 470530298..16ba3c951 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -82,6 +82,10 @@ gb_inline String make_string_c(char *text) { return make_string(cast(u8 *)cast(void *)text, gb_strlen(text)); } +gb_inline String16 make_string16_c(wchar_t *text) { + return make_string16(text, string16_len(text)); +} + String substring(String const &s, isize lo, isize hi) { isize max = s.len; GB_ASSERT_MSG(lo <= hi && hi <= max, "%td..%td..%td", lo, hi, max); diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 3890ae000..ca8a2ba9f 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -85,6 +85,7 @@ TOKEN_KIND(Token__KeywordBegin, ""), \ TOKEN_KIND(Token_import, "import"), \ TOKEN_KIND(Token_export, "export"), \ TOKEN_KIND(Token_foreign, "foreign"), \ + TOKEN_KIND(Token_package, "package"), \ TOKEN_KIND(Token_type, "type"), \ TOKEN_KIND(Token_when, "when"), \ TOKEN_KIND(Token_if, "if"), \