user_windows.odin 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package os2
  2. import "base:runtime"
  3. @(require) import win32 "core:sys/windows"
  4. _local_appdata :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  5. guid := win32.FOLDERID_LocalAppData
  6. return _get_known_folder_path(&guid, allocator)
  7. }
  8. _local_appdata_or_roaming :: proc(allocator: runtime.Allocator, roaming: bool) -> (dir: string, err: Error) {
  9. guid := win32.FOLDERID_LocalAppData
  10. if roaming {
  11. guid = win32.FOLDERID_RoamingAppData
  12. }
  13. return _get_known_folder_path(&guid, allocator)
  14. }
  15. _user_config_dir :: _local_appdata_or_roaming
  16. _user_data_dir :: _local_appdata_or_roaming
  17. _user_state_dir :: _local_appdata
  18. _user_log_dir :: _local_appdata
  19. _user_cache_dir :: _local_appdata
  20. _user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  21. guid := win32.FOLDERID_Profile
  22. return _get_known_folder_path(&guid, allocator)
  23. }
  24. _user_music_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  25. guid := win32.FOLDERID_Music
  26. return _get_known_folder_path(&guid, allocator)
  27. }
  28. _user_desktop_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  29. guid := win32.FOLDERID_Desktop
  30. return _get_known_folder_path(&guid, allocator)
  31. }
  32. _user_documents_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  33. guid := win32.FOLDERID_Documents
  34. return _get_known_folder_path(&guid, allocator)
  35. }
  36. _user_downloads_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  37. guid := win32.FOLDERID_Downloads
  38. return _get_known_folder_path(&guid, allocator)
  39. }
  40. _user_pictures_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  41. guid := win32.FOLDERID_Pictures
  42. return _get_known_folder_path(&guid, allocator)
  43. }
  44. _user_public_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  45. guid := win32.FOLDERID_Public
  46. return _get_known_folder_path(&guid, allocator)
  47. }
  48. _user_videos_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
  49. guid := win32.FOLDERID_Videos
  50. return _get_known_folder_path(&guid, allocator)
  51. }
  52. _get_known_folder_path :: proc(rfid: win32.REFKNOWNFOLDERID, allocator: runtime.Allocator) -> (dir: string, err: Error) {
  53. // https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
  54. // See also `known_folders.odin` in `core:sys/windows` for the GUIDs.
  55. path_w: win32.LPWSTR
  56. res := win32.SHGetKnownFolderPath(rfid, 0, nil, &path_w)
  57. defer win32.CoTaskMemFree(path_w)
  58. if res != 0 {
  59. return "", .Invalid_Path
  60. }
  61. dir, _ = win32.wstring_to_utf8(path_w, -1, allocator)
  62. return
  63. }