diff --git a/core/os/os2/user.odin b/core/os/os2/user.odin new file mode 100644 index 000000000..a4b958921 --- /dev/null +++ b/core/os/os2/user.odin @@ -0,0 +1,66 @@ +package os2 + +import "base:runtime" + +@(require_results) +user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_home_dir(allocator) +} + +// application caches, logs, temporary files +@(require_results) +user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_cache_dir(allocator) +} + +// application assets +@(require_results) +user_data_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_data_dir(allocator) +} + +// application history, ui layout state, logs +@(require_results) +user_state_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_state_dir(allocator) +} + +@(require_results) +user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_config_dir(allocator) +} + +@(require_results) +user_music_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_music_dir(allocator) +} + +@(require_results) +user_desktop_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_desktop_dir(allocator) +} + +@(require_results) +user_documents_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_documents_dir(allocator) +} + +@(require_results) +user_downloads_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_downloads_dir(allocator) +} + +@(require_results) +user_pictures_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_pictures_dir(allocator) +} + +@(require_results) +user_public_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_public_dir(allocator) +} + +@(require_results) +user_videos_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + return _user_videos_dir(allocator) +} diff --git a/core/os/os2/user_posix.odin b/core/os/os2/user_posix.odin index c49d51bb3..ce37015ab 100644 --- a/core/os/os2/user_posix.odin +++ b/core/os/os2/user_posix.odin @@ -3,60 +3,260 @@ package os2 import "base:runtime" -@(require_results) -user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { +_user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) #partial switch ODIN_OS { case .Darwin: dir = get_env("HOME", temp_allocator) - if dir != "" { - dir = concatenate({dir, "/Library/Caches"}, allocator) or_return + if dir == "" { + return "", .Invalid_Path } + dir = concatenate({dir, "/Library/Caches"}, allocator) or_return case: // All other UNIX systems dir = get_env("XDG_CACHE_HOME", allocator) if dir == "" { dir = get_env("HOME", temp_allocator) if dir == "" { - return + return "", .Invalid_Path } dir = concatenate({dir, "/.cache"}, allocator) or_return } } - if dir == "" { - err = .Invalid_Path - } return } -@(require_results) -user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { +_user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) #partial switch ODIN_OS { case .Darwin: dir = get_env("HOME", temp_allocator) - if dir != "" { - dir = concatenate({dir, "/.config"}, allocator) or_return + if dir == "" { + return "", .Invalid_Path } + dir = concatenate({dir, "/.config"}, allocator) or_return case: // All other UNIX systems dir = get_env("XDG_CONFIG_HOME", temp_allocator) if dir == "" { dir = get_env("HOME", temp_allocator) if dir == "" { - return + return "", .Invalid_Path } dir = concatenate({dir, "/.config"}, allocator) or_return } } - if dir == "" { - err = .Invalid_Path + return +} + +_user_state_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/.local/state"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_STATE_HOME", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/.local/state"}, allocator) or_return + } } return } -@(require_results) -user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { +_user_data_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/.local/share"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_DATA_HOME", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/.local/share"}, allocator) or_return + } + } + return +} + +_user_music_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Music"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_MUSIC_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Music"}, allocator) or_return + } + } + return +} + +_user_desktop_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Desktop"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_DESKTOP_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Desktop"}, allocator) or_return + } + } + return +} + +_user_documents_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Documents"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_DOCUMENTS_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Documents"}, allocator) or_return + } + } + return +} + +_user_downloads_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Downloads"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_DOWNLOAD_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Downloads"}, allocator) or_return + } + } + return +} + +_user_pictures_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Pictures"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_PICTURES_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Pictures"}, allocator) or_return + } + } + return +} + +_user_public_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Public"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_PUBLIC_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Public"}, allocator) or_return + } + } + return +} + +_user_videos_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator }) + + #partial switch ODIN_OS { + case .Darwin: + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Movies"}, allocator) or_return + case: // All other UNIX systems + dir = get_env("XDG_VIDEOS_DIR", temp_allocator) + if dir == "" { + dir = get_env("HOME", temp_allocator) + if dir == "" { + return "", .Invalid_Path + } + dir = concatenate({dir, "/Videos"}, allocator) or_return + } + } + return +} + +_user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { if v := get_env("HOME", allocator); v != "" { return v, nil } diff --git a/core/os/os2/user_windows.odin b/core/os/os2/user_windows.odin index fd007af03..94ac3c566 100644 --- a/core/os/os2/user_windows.odin +++ b/core/os/os2/user_windows.odin @@ -3,25 +3,60 @@ package os2 import "base:runtime" @(require) import win32 "core:sys/windows" -@(require_results) -user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { +_user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { guid := win32.FOLDERID_LocalAppData return _get_known_folder_path(&guid, allocator) } -@(require_results) -user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { +_user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { guid := win32.FOLDERID_RoamingAppData return _get_known_folder_path(&guid, allocator) } -@(require_results) -user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { +_user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { guid := win32.FOLDERID_Profile return _get_known_folder_path(&guid, allocator) } -@(require_results) +_user_data_dir :: _user_config_dir + +_user_state_dir :: _user_cache_dir + +_user_music_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Music + return _get_known_folder_path(&guid, allocator) +} + +_user_desktop_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Desktop + return _get_known_folder_path(&guid, allocator) +} + +_user_documents_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Documents + return _get_known_folder_path(&guid, allocator) +} + +_user_downloads_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Downloads + return _get_known_folder_path(&guid, allocator) +} + +_user_pictures_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Pictures + return _get_known_folder_path(&guid, allocator) +} + +_user_public_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Public + return _get_known_folder_path(&guid, allocator) +} + +_user_videos_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { + guid := win32.FOLDERID_Videos + return _get_known_folder_path(&guid, allocator) +} + _get_known_folder_path :: proc(rfid: win32.REFKNOWNFOLDERID, allocator: runtime.Allocator) -> (dir: string, err: Error) { // https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath // See also `known_folders.odin` in `core:sys/windows` for the GUIDs.