From 2da337b8a952c2d0aea3383536a4e7c411d09372 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 18 Apr 2023 19:56:59 +0200 Subject: [PATCH] added windowContext to ProcessHandle --- src/kernel/ps/ProcessHandle.hx | 28 +++++++++++++++++++++++++++- src/kernel/ps/ProcessManager.hx | 32 +++++++++++++++++++++++++++++--- src/kernel/ui/WindowManager.hx | 11 +++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/kernel/ps/ProcessHandle.hx b/src/kernel/ps/ProcessHandle.hx index 933de50..fa21511 100644 --- a/src/kernel/ps/ProcessHandle.hx +++ b/src/kernel/ps/ProcessHandle.hx @@ -1,5 +1,8 @@ package kernel.ps; +import kernel.ps.ProcessManager.PID; +import kernel.ui.WindowContext; +import kernel.ui.WindowManager; import haxe.ds.ReadOnlyArray; using tink.CoreApi; @@ -12,13 +15,17 @@ typedef HandleConfig = { class ProcessHandle { public var args(get,null): ReadOnlyArray; + private final pid: PID; private final config:HandleConfig; private final closeFuture: Future; private var closeFutureResolev: Bool -> Void; + private final windowContexts: Array = []; @:allow(kernel.ps.ProcessManager) - private function new(config: HandleConfig) { + private function new(config: HandleConfig,pid: PID) { this.config = config; + this.pid = pid; + this.closeFuture = new Future((trigger)->{ this.closeFutureResolev = trigger; return null; @@ -27,6 +34,8 @@ class ProcessHandle { if (this.config.onExit != null) { this.closeFuture.handle(this.config.onExit); } + + this.closeFuture.eager(); } public function onExit(): Future { @@ -36,6 +45,7 @@ class ProcessHandle { public function close(success: Bool = true): Void { this.dispose(); this.closeFutureResolev(success); + ProcessManager.removeProcess(this.pid); } public function write(message: String): Void { @@ -46,6 +56,22 @@ class ProcessHandle { this.write(message + "\n"); } + public function createBufferdWindowContext(): WindowContext { + var ctx = WindowManager.instance.createNewContext(); + this.windowContexts.push(ctx); + return ctx; + } + + public function createStatelessWindowContext(): {ctx:WindowContext, setRenderFunc: (() -> Void) -> Void, requestRender:() -> Void} { + var ctx = WindowManager.instance.createNewStatelessContext(); + this.windowContexts.push(ctx.ctx); + return ctx; + } + + public function getWindowContexts(): ReadOnlyArray { + return this.windowContexts; + } + private function dispose() { // TODO } diff --git a/src/kernel/ps/ProcessManager.hx b/src/kernel/ps/ProcessManager.hx index 1534659..77500f8 100644 --- a/src/kernel/ps/ProcessManager.hx +++ b/src/kernel/ps/ProcessManager.hx @@ -2,10 +2,36 @@ package kernel.ps; import kernel.ps.ProcessHandle.HandleConfig; +typedef PID = Int; + class ProcessManager { - public static function run(process:Process, config: HandleConfig):ProcessHandle { - var handle = new ProcessHandle(config); + private static final processList = new Map(); + + public static function run(process:Process, config: HandleConfig):PID { + var pid = createPID(); + var handle = new ProcessHandle(config, pid); + + processList.set(pid, handle); + process.run(handle); - return handle; + + return pid; + } + + private static function createPID(): PID { + // TODO: better PID generation + + // generate a random PID + return Math.ceil(Math.random() * 1000000); + } + + @:allow(kernel.ui.WindowManager) + private static function getProcess(pid:PID):Null{ + return processList.get(pid); + } + + @:allow(kernel.ps.ProcessHandle) + private static function removeProcess(pid:PID):Void { + processList.remove(pid); } } diff --git a/src/kernel/ui/WindowManager.hx b/src/kernel/ui/WindowManager.hx index b1dabda..8f13fb6 100644 --- a/src/kernel/ui/WindowManager.hx +++ b/src/kernel/ui/WindowManager.hx @@ -1,5 +1,7 @@ package kernel.ui; +import kernel.ps.ProcessManager; +import kernel.ps.ProcessManager.PID; import haxe.ds.ReadOnlyArray; import kernel.ui.TermWriteable; import kernel.peripherals.Peripherals.Peripheral; @@ -162,4 +164,13 @@ class WindowManager { context.setTarget(target); context.enable(); } + + public function getContextByPID(pid: PID): ReadOnlyArray { + var handle = ProcessManager.getProcess(pid); + if (handle == null) { + return []; + } + + return handle.getWindowContexts(); + } }