| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  | const ext = @cImport({
 | 
					
						
							|  |  |  |     @cInclude("SDL2/SDL.h");
 | 
					
						
							|  |  |  | });
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  | const io = @import("./io.zig");
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  | const mem = @import("./mem.zig");
 | 
					
						
							|  |  |  | const stack = @import("./stack.zig");
 | 
					
						
							|  |  |  | const std = @import("std");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | pub const EventLoop = packed struct {
 | 
					
						
							|  |  |  |     current_request: ?*Request = null,
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// [OpenError.NotFound] is used as a catch-all for any hardware or software-specific reason for
 | 
					
						
							|  |  |  |     /// failing to open a given file. This includes file-system restrictions surrounding a specific
 | 
					
						
							|  |  |  |     /// file as well as it simply not existing.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// [OpenError.OutOfFiles] occurs when there are no more resources available to open further
 | 
					
						
							|  |  |  |     /// files. As a result, some open files must be closed before more may be opened.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub const OpenError = error {
 | 
					
						
							|  |  |  |         NotFound,
 | 
					
						
							|  |  |  |         OutOfFiles,
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// Indicates what kind of access the consumer logic has to a file.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// [OpenMode.read] is for reading from an existing file from the start.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// [OpenMode.overwrite] is for deleting the contents of a file, or creating an empty one if no
 | 
					
						
							|  |  |  |     /// such file exists, and writing to it from the start.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// [OpenMode.append] is for writing additional contents to a file, creating an empty one if no
 | 
					
						
							|  |  |  |     /// such file exists, on the end of whatever it already contains.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub const OpenMode = enum {
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |         readonly,
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |         overwrite,
 | 
					
						
							|  |  |  |         append,
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Request = struct {
 | 
					
						
							|  |  |  |         next: ?*Request = null,
 | 
					
						
							|  |  |  |         frame: anyframe,
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         message: union(enum) {
 | 
					
						
							|  |  |  |             close: struct {
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |                 file: *FileAccess,
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |             },
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             open: struct {
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |                 file_system: FileSystem,
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |                 path: *const Path,
 | 
					
						
							|  |  |  |                 mode: OpenMode,
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |                 file: OpenError!*FileAccess = error.OutOfFiles,
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |             },
 | 
					
						
							|  |  |  |         },
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const max_files = 512;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// Asynchronously closes `file_access` via `event_loop`.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// *Note* that `file_access` must have been opened by `event_loop` for it to be closed by it,
 | 
					
						
							|  |  |  |     /// otherwise it will cause undefined behavior.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub fn close(event_loop: *EventLoop, file_access: *FileAccess) void {
 | 
					
						
							|  |  |  |         var request = Request{
 | 
					
						
							|  |  |  |             .frame = @frame(),
 | 
					
						
							|  |  |  |             .message = .{.close = @ptrCast(*ext.SDL_RWops, file_access)},
 | 
					
						
							|  |  |  |         };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |         suspend if (event_loop.current_request) |current_request| {
 | 
					
						
							|  |  |  |             current_request.next = &request;
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             event_loop.current_request = &request;
 | 
					
						
							|  |  |  |         };
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |     pub fn open(event_loop: *EventLoop, file_system: FileSystem,
 | 
					
						
							|  |  |  |         path: Path, mode: OpenMode) OpenError!*FileAccess {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |         var request = Request{
 | 
					
						
							|  |  |  |             .frame = @frame(),
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             .message = .{
 | 
					
						
							|  |  |  |                 .open = .{
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |                     .file_system = file_system,
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |                     .path = &path,
 | 
					
						
							|  |  |  |                     .mode = mode,
 | 
					
						
							|  |  |  |                 },
 | 
					
						
							|  |  |  |             },
 | 
					
						
							|  |  |  |         };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |         suspend if (event_loop.current_request) |current_request| {
 | 
					
						
							|  |  |  |             current_request.next = &request;
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             event_loop.current_request = &request;
 | 
					
						
							|  |  |  |         };
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return @ptrCast(*FileAccess, try request.message.open.file);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub fn tick(event_loop: *EventLoop) void {
 | 
					
						
							|  |  |  |         while (event_loop.current_request) |request| {
 | 
					
						
							|  |  |  |             switch (request.message) {
 | 
					
						
							|  |  |  |                 .close => |*close| {
 | 
					
						
							|  |  |  |                     // Swallow file close errors.
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |                     _ = ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops, @alignCast(@alignOf(ext.SDL_RWops), close.file)));
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |                 },
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 .open => |*open| {
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |                     switch (open.file_system) {
 | 
					
						
							|  |  |  |                         .data => {
 | 
					
						
							|  |  |  |                             var zeroed_path_buffer = std.mem.zeroes([4096]u8);
 | 
					
						
							|  |  |  |                             var zeroed_path = stack.Fixed(u8){.buffer = &zeroed_path_buffer};
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             zeroed_path.push('.') catch continue;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             _ = open.path.write(zeroed_path.asWriter());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             zeroed_path.push(0) catch continue;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             // TODO: Access TAR file.
 | 
					
						
							|  |  |  |                         },
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         .user => {
 | 
					
						
							|  |  |  |                             var zeroed_path_buffer = std.mem.zeroes([4096]u8);
 | 
					
						
							|  |  |  |                             var zeroed_path = stack.Fixed(u8){.buffer = &zeroed_path_buffer};
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             zeroed_path.pushAll("/home/kayomn/.local/share") catch continue;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             _ = open.path.write(zeroed_path.asWriter());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             zeroed_path.push(0) catch continue;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             open.file = @ptrCast(?*FileAccess, ext.SDL_RWFromFile(&zeroed_path_buffer, switch (open.mode) {
 | 
					
						
							|  |  |  |                                 .readonly => "rb",
 | 
					
						
							|  |  |  |                                 .overwrite => "wb",
 | 
					
						
							|  |  |  |                                 .append => "ab",
 | 
					
						
							|  |  |  |                             })) orelse error.NotFound;
 | 
					
						
							|  |  |  |                         },
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |                 },
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             resume request.frame;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             event_loop.current_request = request.next;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  | ///
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | pub const FileAccess = opaque {};
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// With files typically being backed by a block device, they can produce a variety of errors -
 | 
					
						
							|  |  |  | /// from physical to virtual errors - these are all encapsulated by the API as general
 | 
					
						
							|  |  |  | /// [Error.Inaccessible] errors.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | pub const FileError = error {
 | 
					
						
							|  |  |  |     Inaccessible,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  | ///
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | pub const FileSystem = enum {
 | 
					
						
							|  |  |  |     data,
 | 
					
						
							|  |  |  |     user,
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  | };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// Platform-agnostic mechanism for accessing files on any of the virtual file-systems supported by
 | 
					
						
							|  |  |  | /// Ona.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | pub const Path = struct {
 | 
					
						
							|  |  |  |     length: u16,
 | 
					
						
							|  |  |  |     buffer: [max]u8,
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |     pub const Error = error {
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |         TooLong,
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// Returns `true` if the length of `path` is empty, otherwise `false`.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub fn isEmpty(path: Path) bool {
 | 
					
						
							|  |  |  |         return (path.length == 0);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// Returns `true` if `this` is equal to `that`, otherwise `false`.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub fn equals(this: Path, that: Path) bool {
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |         return std.mem.eql(u8, this.buffer[0 .. this.length], that.buffer[0 .. that.length]);
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// The maximum possible byte-length of a [Path].
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     /// Note that paths are encoded using UTF-8, meaning that a character may be bigger than one
 | 
					
						
							|  |  |  |     /// byte. Because of this, it is not safe to asume that a path may hold [max] individual
 | 
					
						
							|  |  |  |     /// characters.
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     pub const max = 1000;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |     pub fn parseJoins(path: Path, joins: []const []const u8) Error!Path {
 | 
					
						
							|  |  |  |         _ = path;
 | 
					
						
							|  |  |  |         _ = joins;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Path{
 | 
					
						
							|  |  |  |             .buffer = std.mem.zeroes([max]u8),
 | 
					
						
							|  |  |  |             .length = 0
 | 
					
						
							|  |  |  |         };
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |     pub fn write(path: Path, writer: io.Writer) bool {
 | 
					
						
							|  |  |  |         return (writer.write(path.buffer[0 .. path.length]) == path.length);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							|  |  |  |     ///
 | 
					
						
							| 
									
										
										
										
											2022-09-27 21:45:32 +01:00
										 |  |  |     pub const root = Path{
 | 
					
						
							|  |  |  |         .buffer = [_]u8{'/'} ** max,
 | 
					
						
							|  |  |  |         .length = 1,
 | 
					
						
							|  |  |  |     };
 | 
					
						
							| 
									
										
										
										
											2022-09-24 23:09:02 +01:00
										 |  |  | };
 |