diff --git a/src/kernel/Log.hx b/src/kernel/Log.hx index d36bfd2..601d69b 100644 --- a/src/kernel/Log.hx +++ b/src/kernel/Log.hx @@ -2,12 +2,13 @@ package kernel; import kernel.ui.WindowContext; import kernel.ui.WindowManager; -import lib.TermWriteable; +import kernel.ui.TermWriteable; import lib.TermIO; #if webconsole import kernel.net.Net; import util.Debug; #end + /** Log messages to specified output. **/ @@ -29,34 +30,49 @@ class Log { } public static function info(msg:Dynamic, ?pos:haxe.PosInfos) { - writer.writeLn("[INFO][" + pos.className + "]: " + Std.string(msg)); + writer.writeLn(logLine("INFO",pos,msg)); #if webconsole - Debug.printWeb("[INFO][" + pos.className + "]: " + Std.string(msg)); + Debug.printWeb(logLine("INFO",pos,msg)); #end } public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) { - writer.writeLn("[WARN][" + pos.className + "]: " + Std.string(msg), Yellow); + writer.writeLn(logLine("WARN",pos,msg), Yellow); #if webconsole - Debug.printWeb("[WARN][" + pos.className + "]: " + Std.string(msg)); + Debug.printWeb(logLine("WARN",pos,msg)); #end } public static function error(msg:Dynamic, ?pos:haxe.PosInfos) { - writer.writeLn("[ERRO][" + pos.className + "]: " + Std.string(msg), Red); + writer.writeLn(logLine("ERRO",pos,msg), Red); #if webconsole - Debug.printWeb("[ERRO][" + pos.className + "]: " + Std.string(msg)); + Debug.printWeb(logLine("ERRO",pos,msg)); #end } public static function debug(msg:Dynamic, ?pos:haxe.PosInfos) { - writer.writeLn("[DEBG][" + pos.className + "]: " + Std.string(msg), Gray); + writer.writeLn(logLine("DEBG",pos,msg), Gray); #if webconsole - Debug.printWeb("[DEBG][" + pos.className + "]: " + Std.string(msg)); + Debug.printWeb(logLine("DEBG",pos,msg)); + #end + } + + public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) { + writer.writeLn(logLine("SILY",pos,msg), LightGrey); + #if webconsole + Debug.printWeb(logLine("SILY",pos,msg)); #end } public static function moveToOutput(addr:String) { WindowManager.instance.focusContextToOutput(context, addr); } + + private static function logLine(tag: String,pos: haxe.PosInfos,msg: Dynamic): String { + #if debug + return '[$tag][${pos.className}:${pos.lineNumber}]: ${Std.string(msg)}'; + #else + return '[$tag]: ${Std.string(msg)}'; + #end + } } diff --git a/src/kernel/MainTerm.hx b/src/kernel/MainTerm.hx index 58ac832..5784faf 100644 --- a/src/kernel/MainTerm.hx +++ b/src/kernel/MainTerm.hx @@ -2,7 +2,7 @@ package kernel; using tink.CoreApi; -import lib.TermWriteable; +import kernel.ui.TermWriteable; import cc.Term; import util.Vec.Vec2; import util.Color; diff --git a/src/kernel/net/Loopback.hx b/src/kernel/net/Loopback.hx index 2d469ae..bdd12a3 100644 --- a/src/kernel/net/Loopback.hx +++ b/src/kernel/net/Loopback.hx @@ -18,11 +18,13 @@ class Loopback implements INetworkInterface { } public function listen(chan:Int) { - // TODO + if (!this.openChans.contains(chan)){ + this.openChans.push(chan); + } } public function close(chan:Int) { - // TODO + this.openChans.remove(chan); } public function isListening(chan:Int):Bool { @@ -34,7 +36,11 @@ class Loopback implements INetworkInterface { } public function send(chan:Int, replyChan:Int, payload:Any) { - this.onMessageTrigger.trigger(payload); + if (this.openChans.contains(chan)){ + this.onMessageTrigger.trigger(payload); + }else{ + Log.silly("Loopback got package on non open channel"); + } } public function name():String { diff --git a/src/kernel/net/Package.hx b/src/kernel/net/Package.hx index 9509cc6..665ae57 100644 --- a/src/kernel/net/Package.hx +++ b/src/kernel/net/Package.hx @@ -22,22 +22,6 @@ enum PackageTypes { public final data:Dynamic; public var ttl: Int; - /** - Parse package from an `modem_message` event. - **/ - public static function fromEvent(params:Array):Package { - var payload = params[4]; - - return { - fromID: params[3], - toID: params[2], - msgID: payload.msgID, - type: payload.type, - data: payload.data, - ttl: payload.ttl, - }; - } - /** Create package that can be used as a response. **/ diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index a64a23e..7f58884 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -6,6 +6,7 @@ import kernel.peripherals.Screen; using lua.Table; using Lambda; +using tink.CoreApi; /** Class responseable for retrieving peripherals. @@ -54,6 +55,10 @@ class Peripheral { return this.modes; } + public function getModem(addr: String): Null { + return this.modes.find(item -> item.getAddr() == addr); + } + private function findModems():Void { var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem"); this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s)); diff --git a/src/kernel/peripherals/Screen.hx b/src/kernel/peripherals/Screen.hx index 57de206..e10fa88 100644 --- a/src/kernel/peripherals/Screen.hx +++ b/src/kernel/peripherals/Screen.hx @@ -1,23 +1,23 @@ package kernel.peripherals; -using tink.CoreApi; import cc.Term.TerminalSize; -import lib.TermWriteable; +import kernel.ui.TermWriteable; import util.Vec.Vec2; import util.Color; +using tink.CoreApi; + class Screen implements TermWriteable implements IPeripheral { private final nativ:cc.periphs.Monitor.Monitor; private final addr:String; - private final onResizeTrigger:SignalTrigger>; + private final onResizeTrigger:SignalTrigger> = Signal.trigger(); - public var onResize(default, null):Signal>; + public final onResize:Signal>; @:allow(kernel.peripherals) public function new(nativePeripherals:cc.periphs.Monitor.Monitor, addr:String) { - this.onResizeTrigger = Signal.trigger(); this.onResize = onResizeTrigger.asSignal(); this.nativ = nativePeripherals; @@ -73,7 +73,6 @@ class Screen implements TermWriteable implements IPeripheral { } public function getSize():Vec2 { - // FIXME: this will not compile. Has to be changes upstream var size:TerminalSize = nativ.getSize(); return { x: size.width, diff --git a/src/kernel/ui/TermBuffer.hx b/src/kernel/ui/TermBuffer.hx index d9f882a..80db7d5 100644 --- a/src/kernel/ui/TermBuffer.hx +++ b/src/kernel/ui/TermBuffer.hx @@ -1,11 +1,17 @@ package kernel.ui; -using tink.CoreApi; import util.Vec.Vec2; import util.Color; -import lib.TermWriteable; +import kernel.ui.TermWriteable; +using tink.CoreApi; + +/** + A virtual terminal. With this a GUI program can still write to its "screen" + even if its not displayed right now. When the GUI gets displayed again + this buffer will be written to the screen. +**/ class TermBuffer implements TermWriteable { /** format [y][x]. First index is the line. Second index the char in the line. @@ -15,14 +21,14 @@ class TermBuffer implements TermWriteable { private var cursorPos:Vec2 = {x: 0, y: 0}; private var currentTextColor:Color = White; private var currentBgColor:Color = Black; + private var cursorBlink: Bool = false; private var size:Vec2 = {x: 51, y: 19}; // Default size set to default size of the main terminal - public var onResize(default, null):Signal>; + public final onResize:Signal>; - private final onResizeTrigger:SignalTrigger>; + private final onResizeTrigger:SignalTrigger> = Signal.trigger(); - public function new() { - this.onResizeTrigger = Signal.trigger(); + private function new() { this.onResize = onResizeTrigger.asSignal(); initScreenBuffer(size); } @@ -126,11 +132,11 @@ class TermBuffer implements TermWriteable { } public function getCursorBlink():Bool { - throw new haxe.exceptions.NotImplementedException(); + return this.cursorBlink; } public function setCursorBlink(blink:Bool) { - // TODO + this.cursorBlink = blink; } public function getSize():Vec2 { @@ -164,7 +170,7 @@ class TermBuffer implements TermWriteable { } public function isColor():Bool { - throw new haxe.exceptions.NotImplementedException(); + return true; // Lets return true for now. } public function reset() { diff --git a/src/lib/TermWriteable.hx b/src/kernel/ui/TermWriteable.hx similarity index 91% rename from src/lib/TermWriteable.hx rename to src/kernel/ui/TermWriteable.hx index 9705ce1..3906c1b 100644 --- a/src/lib/TermWriteable.hx +++ b/src/kernel/ui/TermWriteable.hx @@ -1,4 +1,4 @@ -package lib; +package kernel.ui; using tink.CoreApi; @@ -34,8 +34,8 @@ interface TermWriteable { public function getBackgroundColor():Color; public function setBackgroundColor(color:Color):Void; public function isColor():Bool; - // setPaletteColor(...) - // getPaletteColor(colour) + // public function setPaletteColor(...); + // public function getPaletteColor(colour); /** Clears the screen, resetes the courser to (0,0) and resetes the color to Black and White. diff --git a/src/kernel/ui/VirtualTermWriter.hx b/src/kernel/ui/VirtualTermWriter.hx index d60ad9e..9487794 100644 --- a/src/kernel/ui/VirtualTermWriter.hx +++ b/src/kernel/ui/VirtualTermWriter.hx @@ -4,8 +4,12 @@ using tink.CoreApi; import util.Vec.Vec2; import util.Color; -import lib.TermWriteable; +import kernel.ui.TermWriteable; +/** + A term writer that can switch beetween its internal buffer and another termwriter. + The other target is most of the time a real screen +**/ class VirtualTermWriter implements TermWriteable extends TermBuffer { private static final defaultSize:Vec2 = {x: 50, y: 50}; @@ -13,7 +17,8 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer { private var enabled:Bool = false; private var onResizeLink:CallbackLink; - public function new(?target:TermWriteable) { + @:allow(kernel.ui) + private function new(?target:TermWriteable) { setTarget(target); if (enabled) { @@ -52,6 +57,8 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer { setSuperSize(newSize); }); + newTarget.reset(); + target = newTarget; } } @@ -95,11 +102,18 @@ class VirtualTermWriter implements TermWriteable extends TermBuffer { } public override function getCursorBlink():Bool { - throw new haxe.exceptions.NotImplementedException(); + if (isEnabled()){ + return target.getCursorBlink(); + }else{ + return super.getCursorBlink(); + } } public override function setCursorBlink(blink:Bool) { - // TODO + if (isEnabled()){ + target.setCursorBlink(blink); + } + super.setCursorBlink(blink); } public override function getSize():Vec2 { diff --git a/src/kernel/ui/WindowContext.hx b/src/kernel/ui/WindowContext.hx index 47a10d1..2cb8832 100644 --- a/src/kernel/ui/WindowContext.hx +++ b/src/kernel/ui/WindowContext.hx @@ -5,7 +5,7 @@ using tink.CoreApi; import util.Color; import kernel.ButtonType; import util.Vec.Vec2; -import lib.TermWriteable; +import kernel.ui.TermWriteable; /** The main object you interact with when writing anything to the screen. diff --git a/src/kernel/ui/WindowManager.hx b/src/kernel/ui/WindowManager.hx index 5199e2f..ee17284 100644 --- a/src/kernel/ui/WindowManager.hx +++ b/src/kernel/ui/WindowManager.hx @@ -1,8 +1,12 @@ package kernel.ui; -import lib.TermWriteable; +import kernel.ui.TermWriteable; import kernel.peripherals.Peripherals.Peripheral; +/** + Responsable for creating window context, forwarding UI events to the context + and switching context to real screens. +**/ class WindowManager { /** Depends on: KernelEvents, Peripheral @@ -45,9 +49,9 @@ class WindowManager { }); KernelEvents.instance.onMouseUp.handle(params -> { - // if (currentMainContext != null) { + if (currentMainContext != null) { currentMainContext.onMouseUpTrigger.trigger(params); - // } + } }); KernelEvents.instance.onPaste.handle(text -> { diff --git a/src/lib/TermIO.hx b/src/lib/TermIO.hx index 6cc457b..0f9528d 100644 --- a/src/lib/TermIO.hx +++ b/src/lib/TermIO.hx @@ -1,7 +1,7 @@ package lib; import util.Color; -import lib.TermWriteable; +import kernel.ui.TermWriteable; /** Helpfull class for writing onto a `TermWriteable`.