Skip to content

Commit

Permalink
Make GlkRunner async where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii committed Apr 17, 2023
1 parent c0c5eea commit 7f8286c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 83 deletions.
20 changes: 7 additions & 13 deletions FrankenDrift.GlkRunner/FrankenDrift.GlkRunner.AsyncGlk/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ internal static partial class AsyncGlk_imports
internal static partial void glk_request_char_event(IntPtr winId);
[JSImport("glk_request_hyperlink_event", "main.js")]
internal static partial void glk_request_hyperlink_event(IntPtr winId);
[JSImport("glk_request_line_event", "main.js")]
internal static partial void glk_request_line_event(IntPtr win, [JSMarshalAs<JSType.MemoryView>] Span<Byte> buf, int initlen);
[JSImport("glk_request_line_event_uni", "main.js")]
internal static partial void glk_request_line_event_uni(IntPtr win, [JSMarshalAs<JSType.MemoryView>] Span<Int32> buf, int initlen);
[JSImport("glk_request_timer_events", "main.js")]
Expand All @@ -55,8 +53,6 @@ internal static partial class AsyncGlk_imports
internal static partial void glk_set_window(IntPtr winId);
[JSImport("glk_stream_open_file", "main.js")]
internal static partial IntPtr glk_stream_open_file(IntPtr fileref, int fmode, int rock);
[JSImport("glk_stream_open_memory", "main.js")]
internal static partial IntPtr glk_stream_open_memory([JSMarshalAs<JSType.MemoryView>] Span<Byte> buf, int mode, int rock);
[JSImport("glk_stream_set_position", "main.js")]
internal static partial void glk_stream_set_position(IntPtr stream, int pos, int seekMode);
[JSImport("glk_stylehint_set", "main.js")]
Expand Down Expand Up @@ -125,8 +121,7 @@ public uint glk_image_get_info(uint imageId, ref uint width, ref uint height)
public void glk_put_buffer_uni(uint[] s, uint len) => AsyncGlk_imports.glk_put_buffer_uni(new Span<Int32>((int[])(object) s, 0, (int) len));
public void glk_request_char_event(WindowHandle winId) => AsyncGlk_imports.glk_request_char_event(winId);
public void glk_request_hyperlink_event(WindowHandle winId) => AsyncGlk_imports.glk_request_hyperlink_event(winId);
public unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen) => AsyncGlk_imports.glk_request_line_event(win, new Span<Byte>(buf, (int) maxlen), (int) initlen);
public unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen) => AsyncGlk_imports.glk_request_line_event_uni(win, new Span<Int32>(buf, (int) maxlen), (int) initlen);
public unsafe void glk_request_line_event_uni(WindowHandle win, uint[] buf, uint maxlen, uint initlen) => AsyncGlk_imports.glk_request_line_event_uni(win, new Span<Int32>((int[])(object) buf, 0, (int) maxlen), (int) initlen);
public void glk_request_timer_events(uint millisecs) => AsyncGlk_imports.glk_request_timer_events((int) millisecs);
public SoundChannel glk_schannel_create(uint rock) => 0;
public void glk_schannel_destroy(SoundChannel chan) {}
Expand All @@ -136,17 +131,10 @@ public void glk_schannel_pause(SoundChannel chan) {}
public void glk_schannel_set_volume(SoundChannel chan, uint vol) {}
public void glk_schannel_stop(SoundChannel chan) {}
public void glk_schannel_unpause(SoundChannel chan) {}
public void glk_select(ref Event ev)
{
Task t = AsyncGlk_imports.glk_select();
t.Wait(-1);
fill_event(ref ev);
}
public void glk_set_hyperlink(uint linkval) => AsyncGlk_imports.glk_set_hyperlink((int) linkval);
public void glk_set_style(Style s) => AsyncGlk_imports.glk_set_style((int) s);
public void glk_set_window(WindowHandle winId) => AsyncGlk_imports.glk_set_window(winId);
public StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock) => AsyncGlk_imports.glk_stream_open_file(fileref, (int) fmode, (int) rock);
public unsafe StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock) => AsyncGlk_imports.glk_stream_open_memory(new Span<Byte>(buf, (int) buflen), (int) mode, (int) rock);
public void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode) => AsyncGlk_imports.glk_stream_set_position(stream, (int) pos, (int) seekMode);
public void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val) => AsyncGlk_imports.glk_stylehint_set((int) wintype, (int) styl, (int) hint, (int) val);
public uint glk_style_measure(WindowHandle winid, Style styl, StyleHint hint, ref uint result) => 0;
Expand All @@ -173,6 +161,12 @@ public void glk_window_get_size(WindowHandle winId, out uint width, out uint hei
public void garglk_set_zcolors(uint fg, uint bg) {}
public string? glkunix_fileref_get_name(FileRefHandle fileref) => AsyncGlk_imports.glkunix_fileref_get_name(fileref);

public async Task<Event> GetEvent() {
await AsyncGlk_imports.glk_select();
Event ev = new() { type = EventType.None };
fill_event(ref ev);
return ev;
}
public void SetGameName(string game) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class FrankenDrift {
},
],
})
//.withVirtualWorkingDirectory('/frankendrift')
.create()

runtime.setModuleImports('main.js', {
Expand Down Expand Up @@ -100,7 +101,6 @@ export class FrankenDrift {
glk_put_buffer_uni: val => Glk.glk_put_buffer_uni(val._unsafe_create_view()),
glk_request_char_event: winId => Glk.glk_request_char_event(ids_to_objects[winId]),
glk_request_hyperlink_event: winId => Glk.glk_request_hyperlink_event(ids_to_objects[winId]),
glk_request_line_event: (winId, buf, initlen) => Glk.glk_request_line_event(ids_to_objects[winId], buf._unsafe_create_view(), initlen),
glk_request_line_event_uni: (winId, buf, initlen) => Glk.glk_request_line_event_uni(ids_to_objects[winId], buf._unsafe_create_view(), initlen),
glk_request_timer_events: msecs => Glk.glk_request_timer_events(msecs),
glk_select: async () => {
Expand All @@ -123,13 +123,6 @@ export class FrankenDrift {
}
return 0
},
glk_stream_open_memory: (buf, mode, rock) => {
const str = Glk.glk_stream_open_memory(buf._unsafe_create_view(), mode, rock)
if (str) {
return register_object(str)
}
return 0
},
glk_stream_set_position: (stream, pos, seekMode) => Glk.glk_stream_set_position(ids_to_objects[stream], pos, seekMode),
glk_stylehint_set: (wintype, styl, hint, val) => Glk.glk_stylehint_set(wintype, styl, hint, val),
glk_tick: () => Glk.glk_tick(),
Expand Down
16 changes: 7 additions & 9 deletions FrankenDrift.GlkRunner/FrankenDrift.GlkRunner.Gargoyle/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ static class Garglk_Pinvoke
[DllImport("libgarglk")]
internal static extern void glk_request_hyperlink_event(WindowHandle winId);
[DllImport("libgarglk")]
internal static extern unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen);
[DllImport("libgarglk")]
internal static extern unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen);
internal static extern unsafe void glk_request_line_event_uni(WindowHandle win, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] uint[] buf, uint maxlen, uint initlen);
[DllImport("libgarglk")]
internal static extern SoundChannel glk_schannel_create(uint rock);
[DllImport("libgarglk")]
Expand Down Expand Up @@ -69,8 +67,6 @@ static class Garglk_Pinvoke
[DllImport("libgarglk")]
internal static extern StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock);
[DllImport("libgarglk")]
internal static unsafe extern StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock);
[DllImport("libgarglk")]
internal static extern void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode);
[DllImport("libgarglk")]
internal static extern void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val);
Expand Down Expand Up @@ -133,8 +129,7 @@ class GarGlk: IGlk
public void glk_put_buffer_uni(uint[] s, uint len) => Garglk_Pinvoke.glk_put_buffer_uni(s, len);
public void glk_request_char_event(WindowHandle winId) => Garglk_Pinvoke.glk_request_char_event(winId);
public void glk_request_hyperlink_event(WindowHandle winId) => Garglk_Pinvoke.glk_request_hyperlink_event(winId);
public unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen) => Garglk_Pinvoke.glk_request_line_event(win, buf, maxlen, initlen);
public unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen) => Garglk_Pinvoke.glk_request_line_event_uni(win, buf, maxlen, initlen);
public unsafe void glk_request_line_event_uni(WindowHandle win, uint[] buf, uint maxlen, uint initlen) => Garglk_Pinvoke.glk_request_line_event_uni(win, buf, maxlen, initlen);
public SoundChannel glk_schannel_create(uint rock) => Garglk_Pinvoke.glk_schannel_create(rock);
public void glk_schannel_destroy(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_destroy(chan);
public void glk_schannel_pause(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_pause(chan);
Expand All @@ -143,12 +138,10 @@ class GarGlk: IGlk
public void glk_schannel_set_volume(SoundChannel chan, uint vol) => Garglk_Pinvoke.glk_schannel_set_volume(chan, vol);
public void glk_schannel_stop(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_stop(chan);
public void glk_schannel_unpause(SoundChannel chan) => Garglk_Pinvoke.glk_schannel_unpause(chan);
public void glk_select(ref Event ev) => Garglk_Pinvoke.glk_select(ref ev);
public void glk_set_hyperlink(uint linkval) => Garglk_Pinvoke.glk_set_hyperlink(linkval);
public void glk_set_style(Style s) => Garglk_Pinvoke.glk_set_style(s);
public void glk_set_window(WindowHandle winId) => Garglk_Pinvoke.glk_set_window(winId);
public StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock) => Garglk_Pinvoke.glk_stream_open_file(fileref, fmode, rock);
public unsafe StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock) => Garglk_Pinvoke.glk_stream_open_memory(buf, buflen, mode, rock);
public void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode) => Garglk_Pinvoke.glk_stream_set_position(stream, pos, seekMode);
public void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val) => Garglk_Pinvoke.glk_stylehint_set(wintype, styl, hint, val);
public uint glk_style_measure(WindowHandle winid, Style styl, StyleHint hint, ref uint result) => Garglk_Pinvoke.glk_style_measure(winid, styl, hint, ref result);
Expand All @@ -166,6 +159,11 @@ class GarGlk: IGlk
public unsafe uint glk_gestalt_ext(Gestalt sel, uint val, uint* arr, uint arrlen) => Garglk_Pinvoke.glk_gestalt_ext(sel, val, arr, arrlen);
public void glk_request_timer_events(uint millisecs) => Garglk_Pinvoke.glk_request_timer_events(millisecs);

public Task<Event> GetEvent() {
Event ev = new() { type = EventType.None };
Garglk_Pinvoke.glk_select(ref ev);
return Task.FromResult(ev);
}
public void SetGameName(string game) => Garglk_Pinvoke.garglk_set_story_name(game);

internal void GarglkInit(string[] argv)
Expand Down
15 changes: 7 additions & 8 deletions FrankenDrift.GlkRunner/FrankenDrift.GlkRunner.WinGlk/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ internal static class Winglk_Pinvoke
[DllImport("Glk")]
internal static extern void glk_request_hyperlink_event(WindowHandle winId);
[DllImport("Glk")]
internal static extern unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen);
[DllImport("Glk")]
internal static extern unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen);
internal static extern unsafe void glk_request_line_event_uni(WindowHandle win, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] uint[] buf, uint maxlen, uint initlen);
[DllImport("Glk")]
internal static extern SoundChannel glk_schannel_create(uint rock);
[DllImport("Glk")]
Expand Down Expand Up @@ -68,8 +66,6 @@ internal static class Winglk_Pinvoke
[DllImport("Glk")]
internal static extern StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock);
[DllImport("Glk")]
internal static unsafe extern StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock);
[DllImport("Glk")]
internal static extern void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode);
[DllImport("Glk")]
internal static extern void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val);
Expand Down Expand Up @@ -126,8 +122,7 @@ class WindowsGlk : IGlk
public void glk_put_buffer_uni(uint[] s, uint len) => Winglk_Pinvoke.glk_put_buffer_uni(s, len);
public void glk_request_char_event(WindowHandle winId) => Winglk_Pinvoke.glk_request_char_event(winId);
public void glk_request_hyperlink_event(WindowHandle winId) => Winglk_Pinvoke.glk_request_hyperlink_event(winId);
public unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen) => Winglk_Pinvoke.glk_request_line_event(win, buf, maxlen, initlen);
public unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen) => Winglk_Pinvoke.glk_request_line_event_uni(win, buf, maxlen, initlen);
public unsafe void glk_request_line_event_uni(WindowHandle win, uint[] buf, uint maxlen, uint initlen) => Winglk_Pinvoke.glk_request_line_event_uni(win, buf, maxlen, initlen);
public SoundChannel glk_schannel_create(uint rock) => Winglk_Pinvoke.glk_schannel_create(rock);
public void glk_schannel_destroy(SoundChannel chan) => Winglk_Pinvoke.glk_schannel_destroy(chan);
public void glk_schannel_pause(SoundChannel chan) => Winglk_Pinvoke.glk_schannel_pause(chan);
Expand All @@ -141,7 +136,6 @@ class WindowsGlk : IGlk
public void glk_set_style(Style s) => Winglk_Pinvoke.glk_set_style(s);
public void glk_set_window(WindowHandle winId) => Winglk_Pinvoke.glk_set_window(winId);
public StreamHandle glk_stream_open_file(FileRefHandle fileref, Glk.FileMode fmode, uint rock) => Winglk_Pinvoke.glk_stream_open_file(fileref, fmode, rock);
public unsafe StreamHandle glk_stream_open_memory(byte* buf, uint buflen, Glk.FileMode mode, uint rock) => Winglk_Pinvoke.glk_stream_open_memory(buf, buflen, mode, rock);
public void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode) => Winglk_Pinvoke.glk_stream_set_position(stream, pos, seekMode);
public void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val) => Winglk_Pinvoke.glk_stylehint_set(wintype, styl, hint, val);
public uint glk_style_measure(WindowHandle winid, Style styl, StyleHint hint, ref uint result) => Winglk_Pinvoke.glk_style_measure(winid, styl, hint, ref result);
Expand All @@ -159,6 +153,11 @@ class WindowsGlk : IGlk
public unsafe uint glk_gestalt_ext(Gestalt sel, uint val, uint* arr, uint arrlen) => Winglk_Pinvoke.glk_gestalt_ext(sel, val, arr, arrlen);
public void glk_request_timer_events(uint millisecs) => Winglk_Pinvoke.glk_request_timer_events(millisecs);

public Task<Event> GetEvent() {
Event ev = new() { type = EventType.None };
Winglk_Pinvoke.glk_select(ref ev);
return Task.FromResult(ev);
}
public void SetGameName(string game) => Winglk_Pinvoke.winglk_window_set_title(game);
}

Expand Down
6 changes: 2 additions & 4 deletions FrankenDrift.GlkRunner/FrankenDrift.GlkRunner/GlkApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ public interface IGlk
void glk_put_buffer_uni(uint[] s, uint len);
void glk_request_char_event(WindowHandle winId);
void glk_request_hyperlink_event(WindowHandle winId);
unsafe void glk_request_line_event(WindowHandle win, byte* buf, uint maxlen, uint initlen);
unsafe void glk_request_line_event_uni(WindowHandle win, uint* buf, uint maxlen, uint initlen);
unsafe void glk_request_line_event_uni(WindowHandle win, uint[] buf, uint maxlen, uint initlen);
SoundChannel glk_schannel_create(uint rock);
void glk_schannel_destroy(SoundChannel chan);
void glk_schannel_pause(SoundChannel chan);
Expand All @@ -260,12 +259,10 @@ public interface IGlk
void glk_schannel_set_volume(SoundChannel chan, uint vol);
void glk_schannel_stop(SoundChannel chan);
void glk_schannel_unpause(SoundChannel chan);
void glk_select(ref Event ev);
void glk_set_hyperlink(uint linkval);
void glk_set_style(Style s);
void glk_set_window(WindowHandle winId);
StreamHandle glk_stream_open_file(FileRefHandle fileref, FileMode fmode, uint rock);
unsafe StreamHandle glk_stream_open_memory(byte* buf, uint buflen, FileMode mode, uint rock);
void glk_stream_set_position(StreamHandle stream, int pos, SeekMode seekMode);
void glk_stylehint_set(WinType wintype, Style styl, StyleHint hint, int val);
uint glk_style_measure(WindowHandle winid, Style styl, StyleHint hint, ref uint result);
Expand All @@ -284,6 +281,7 @@ public interface IGlk
void glk_request_timer_events(uint millisecs);

// And some extra functions we want that could have different implementations
Task<Event> GetEvent();
void SetGameName(string game);
#pragma warning restore IDE1006 // Naming Styles
}
Expand Down
Loading

0 comments on commit 7f8286c

Please sign in to comment.