2021-12-20 00:55:30 +00:00
|
|
|
package kernel;
|
|
|
|
|
2022-02-21 14:17:38 +00:00
|
|
|
import util.Vec.Vec2;
|
|
|
|
import haxe.Exception;
|
|
|
|
using tink.CoreApi;
|
|
|
|
|
2021-12-20 00:55:30 +00:00
|
|
|
import cc.OS;
|
|
|
|
|
|
|
|
using lua.Table;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Class for interacting with the native pullEvent system.
|
|
|
|
**/
|
|
|
|
class KernelEvents{
|
2022-02-21 00:50:19 +00:00
|
|
|
public static var instance:KernelEvents;
|
|
|
|
|
2022-02-21 14:17:38 +00:00
|
|
|
public final onAlarm: Signal<Int>;
|
|
|
|
public final onChar: Signal<String>;
|
|
|
|
public final onDisk: Signal<String>;
|
|
|
|
public final onDiskEject: Signal<String>;
|
|
|
|
public final onHttpCheck: Signal<{url:String,success:Bool,failReason:Any}>;
|
|
|
|
public final onHttpFailure: Signal<{url:String,failReason:String,handle:Any}>;
|
|
|
|
public final onHttpSuccess: Signal<{url:String,handle:Any}>;
|
|
|
|
public final onKey: Signal<{keyCode: Int,isHeld:Bool}>;
|
|
|
|
public final onKeyUp: Signal<Int>;
|
|
|
|
public final onModemMessage: Signal<{addr: String,channel:Int, replyChannel:Int,message:Dynamic,distance:Int}>;
|
|
|
|
public final onMonitorResize: Signal<String>;
|
|
|
|
public final onMonitorTouch: Signal<{addr:String,pos: Vec2<Int>}>;
|
|
|
|
public final onMouseClick: Signal<{button:ButtonType,pos: Vec2<Int>}>;
|
|
|
|
public final onMouseDrag: Signal<{button:ButtonType,pos: Vec2<Int>}>;
|
|
|
|
public final onMouseScroll: Signal<{dir:Int,pos: Vec2<Int>}>;
|
|
|
|
public final onMouseUp: Signal<{button:ButtonType,pos: Vec2<Int>}>;
|
|
|
|
public final onPaste: Signal<String>;
|
|
|
|
public final onPeripheral: Signal<String>;
|
|
|
|
public final onPeripheralDetach: Signal<String>;
|
|
|
|
public final onRednetMessage: Signal<{sender:Int,message:Any,protocol:Any}>;
|
|
|
|
public final onRedstone: Signal<Noise>;
|
|
|
|
public final onSpeakerAudioEmpty: Signal<String>;
|
|
|
|
public final onTaskComplete: Signal<{id:Int,success:Bool,failedReason:String}>;
|
|
|
|
public final onTermResize: Signal<Noise>;
|
|
|
|
public final onTerminate: Signal<Noise>;
|
|
|
|
public final onTimer: Signal<Int>;
|
|
|
|
public final onTurtleInventory: Signal<Noise>;
|
|
|
|
public final onWebsocketClose: Signal<String>;
|
|
|
|
public final onWebsocketFailure: Signal<{url:String,failReason:String}>;
|
|
|
|
public final onWebsocketMessage: Signal<{url:String,message:String,isBinary:Bool}>;
|
|
|
|
public final onWebsocketSuccess: Signal<{url:String,handle:Any}>;
|
|
|
|
|
|
|
|
private final onAlarmTrigger: SignalTrigger<Int> = Signal.trigger();
|
|
|
|
private final onCharTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onDiskTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onDiskEjectTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onHttpCheckTrigger: SignalTrigger<{url:String,success:Bool,failReason:Any}> = Signal.trigger();
|
|
|
|
private final onHttpFailureTrigger: SignalTrigger<{url:String,failReason:String,handle:Any}> = Signal.trigger();
|
|
|
|
private final onHttpSuccessTrigger: SignalTrigger<{url:String,handle:Any}> = Signal.trigger();
|
|
|
|
private final onKeyTrigger: SignalTrigger<{keyCode: Int,isHeld:Bool}> = Signal.trigger();
|
|
|
|
private final onKeyUpTrigger: SignalTrigger<Int> = Signal.trigger();
|
|
|
|
private final onModemMessageTrigger: SignalTrigger<{addr: String,channel:Int, replyChannel:Int,message:Dynamic,distance:Int}> = Signal.trigger();
|
|
|
|
private final onMonitorResizeTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onMonitorTouchTrigger: SignalTrigger<{addr:String,pos: Vec2<Int>}> = Signal.trigger();
|
|
|
|
private final onMouseClickTrigger: SignalTrigger<{button:ButtonType,pos: Vec2<Int>}> = Signal.trigger();
|
|
|
|
private final onMouseDragTrigger: SignalTrigger<{button:ButtonType,pos: Vec2<Int>}> = Signal.trigger();
|
|
|
|
private final onMouseScrollTrigger: SignalTrigger<{dir:Int,pos: Vec2<Int>}> = Signal.trigger();
|
|
|
|
private final onMouseUpTrigger: SignalTrigger<{button:ButtonType,pos: Vec2<Int>}> = Signal.trigger();
|
|
|
|
private final onPasteTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onPeripheralTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onPeripheralDetachTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onRednetMessageTrigger: SignalTrigger<{sender:Int,message:Any,protocol:Any}> = Signal.trigger();
|
|
|
|
private final onRedstoneTrigger: SignalTrigger<Noise> = Signal.trigger();
|
|
|
|
private final onSpeakerAudioEmptyTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onTaskCompleteTrigger: SignalTrigger<{id:Int,success:Bool,failedReason:String}> = Signal.trigger();
|
|
|
|
private final onTermResizeTrigger: SignalTrigger<Noise> = Signal.trigger();
|
|
|
|
private final onTerminateTrigger: SignalTrigger<Noise> = Signal.trigger();
|
|
|
|
private final onTimerTrigger: SignalTrigger<Int> = Signal.trigger();
|
|
|
|
private final onTurtleInventoryTrigger: SignalTrigger<Noise> = Signal.trigger();
|
|
|
|
private final onWebsocketCloseTrigger: SignalTrigger<String> = Signal.trigger();
|
|
|
|
private final onWebsocketFailureTrigger: SignalTrigger<{url:String,failReason:String}> = Signal.trigger();
|
|
|
|
private final onWebsocketMessageTrigger: SignalTrigger<{url:String,message:String,isBinary:Bool}> = Signal.trigger();
|
|
|
|
private final onWebsocketSuccessTrigger: SignalTrigger<{url:String,handle:Any}> = Signal.trigger();
|
2021-12-20 00:55:30 +00:00
|
|
|
|
2022-02-21 14:17:38 +00:00
|
|
|
@:allow(kernel.Init)
|
|
|
|
private function new () {
|
|
|
|
this.onAlarm = onAlarmTrigger.asSignal();
|
|
|
|
this.onChar = onCharTrigger.asSignal();
|
|
|
|
this.onDisk = onDiskTrigger.asSignal();
|
|
|
|
this.onDiskEject = onDiskEjectTrigger.asSignal();
|
|
|
|
this.onHttpCheck = onHttpCheckTrigger.asSignal();
|
|
|
|
this.onHttpFailure = onHttpFailureTrigger.asSignal();
|
|
|
|
this.onHttpSuccess = onHttpSuccessTrigger.asSignal();
|
|
|
|
this.onKey = onKeyTrigger.asSignal();
|
|
|
|
this.onKeyUp = onKeyUpTrigger.asSignal();
|
|
|
|
this.onModemMessage = onModemMessageTrigger.asSignal();
|
|
|
|
this.onMonitorResize = onMonitorResizeTrigger.asSignal();
|
|
|
|
this.onMonitorTouch = onMonitorTouchTrigger.asSignal();
|
|
|
|
this.onMouseClick = onMouseClickTrigger.asSignal();
|
|
|
|
this.onMouseDrag = onMouseDragTrigger.asSignal();
|
|
|
|
this.onMouseScroll = onMouseScrollTrigger.asSignal();
|
|
|
|
this.onMouseUp = onMouseUpTrigger.asSignal();
|
|
|
|
this.onPaste = onPasteTrigger.asSignal();
|
|
|
|
this.onPeripheral = onPeripheralTrigger.asSignal();
|
|
|
|
this.onPeripheralDetach = onPeripheralDetachTrigger.asSignal();
|
|
|
|
this.onRednetMessage = onRednetMessageTrigger.asSignal();
|
|
|
|
this.onRedstone = onRedstoneTrigger.asSignal();
|
|
|
|
this.onSpeakerAudioEmpty = onSpeakerAudioEmptyTrigger.asSignal();
|
|
|
|
this.onTaskComplete = onTaskCompleteTrigger.asSignal();
|
|
|
|
this.onTermResize = onTermResizeTrigger.asSignal();
|
|
|
|
this.onTerminate = onTerminateTrigger.asSignal();
|
|
|
|
this.onTimer = onTimerTrigger.asSignal();
|
|
|
|
this.onTurtleInventory = onTurtleInventoryTrigger.asSignal();
|
|
|
|
this.onWebsocketClose = onWebsocketCloseTrigger.asSignal();
|
|
|
|
this.onWebsocketFailure = onWebsocketFailureTrigger.asSignal();
|
|
|
|
this.onWebsocketMessage = onWebsocketMessageTrigger.asSignal();
|
|
|
|
this.onWebsocketSuccess = onWebsocketSuccessTrigger.asSignal();
|
|
|
|
}
|
2021-12-20 00:55:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Start pulling events. Blocking.
|
|
|
|
**/
|
|
|
|
public function startEventLoop() {
|
|
|
|
while (true){
|
|
|
|
var event:Table<Int, Dynamic> = OS.pullEventRaw();
|
|
|
|
|
|
|
|
var eventName:String = event[1];
|
|
|
|
|
2022-02-21 14:17:38 +00:00
|
|
|
switch eventName {
|
|
|
|
case "alarm":
|
|
|
|
this.onAlarmTrigger.trigger(event[2]);
|
|
|
|
case "char":
|
|
|
|
this.onCharTrigger.trigger(event[2]);
|
|
|
|
case "disk":
|
|
|
|
this.onDiskTrigger.trigger(event[2]);
|
|
|
|
case "disk_eject":
|
|
|
|
this.onDiskEjectTrigger.trigger(event[2]);
|
|
|
|
case "http_check":
|
|
|
|
this.onHttpCheckTrigger.trigger({url: event[2],success: event[3],failReason: event[4]});
|
|
|
|
case "http_failure":
|
|
|
|
this.onHttpFailureTrigger.trigger({url: event[2],failReason: event[3],handle: event[4]});
|
|
|
|
case "http_success":
|
|
|
|
this.onHttpSuccessTrigger.trigger({url: event[2],handle: event[3]});
|
|
|
|
case "key":
|
|
|
|
this.onKeyTrigger.trigger({keyCode: event[2],isHeld: event[3]});
|
|
|
|
case "key_up":
|
|
|
|
this.onKeyUpTrigger.trigger(event[2]);
|
|
|
|
case "modem_message":
|
|
|
|
this.onModemMessageTrigger.trigger({addr: event[2],channel: event[3],replyChannel: event[4],message: event[5],distance: event[6]});
|
|
|
|
case "monitor_resize":
|
|
|
|
this.onMonitorResizeTrigger.trigger(event[2]);
|
|
|
|
case "monitor_touch":
|
|
|
|
this.onMonitorTouchTrigger.trigger({addr: event[2],pos:{x: (event[3]:Int) - 1,y: (event[4]:Int) - 1}});
|
|
|
|
case "mouse_click":
|
|
|
|
this.onMouseClickTrigger.trigger({button: ccButtonToEnum(event[2]),pos:{ x: (event[3]:Int) - 1,y: (event[4]:Int) - 1}});
|
|
|
|
case "mouse_drag":
|
|
|
|
this.onMouseDragTrigger.trigger({button: ccButtonToEnum(event[2]),pos:{x: (event[3]:Int) - 1,y: (event[4]:Int) - 1}});
|
|
|
|
case "mouse_scroll":
|
|
|
|
this.onMouseScrollTrigger.trigger({dir: event[2],pos:{x:(event[3]:Int) - 1,y: (event[4]:Int) - 1}});
|
|
|
|
case "mouse_up":
|
|
|
|
this.onMouseUpTrigger.trigger({button: ccButtonToEnum(event[2]),pos:{x: (event[3]:Int) - 1,y: (event[4]:Int) - 1}});
|
|
|
|
case "paste":
|
|
|
|
this.onPasteTrigger.trigger(event[2]);
|
|
|
|
case "peripheral":
|
|
|
|
this.onPeripheralTrigger.trigger(event[2]);
|
|
|
|
case "peripheral_detach":
|
|
|
|
this.onPeripheralDetachTrigger.trigger(event[2]);
|
|
|
|
case "rednet_message":
|
|
|
|
this.onRednetMessageTrigger.trigger({sender: event[2],message: event[3],protocol: event[4]});
|
|
|
|
case "redstone":
|
|
|
|
this.onRedstoneTrigger.trigger(null);
|
|
|
|
case "speaker_audio_empty":
|
|
|
|
this.onSpeakerAudioEmptyTrigger.trigger(event[2]);
|
|
|
|
case "task_complete":
|
|
|
|
this.onTaskCompleteTrigger.trigger({id: event[2],success: event[3],failedReason: event[4]});
|
|
|
|
case "term_resize":
|
|
|
|
this.onTermResizeTrigger.trigger(null);
|
|
|
|
case "terminate":
|
|
|
|
this.onTerminateTrigger.trigger(null);
|
|
|
|
case "timer":
|
|
|
|
this.onTimerTrigger.trigger(event[2]);
|
|
|
|
case "turtle_inventory":
|
|
|
|
this.onTurtleInventoryTrigger.trigger(null);
|
|
|
|
case "websocket_closed":
|
|
|
|
this.onWebsocketCloseTrigger.trigger(event[2]);
|
|
|
|
case "websocket_failure":
|
|
|
|
this.onWebsocketFailureTrigger.trigger({url: event[2],failReason: event[3]});
|
|
|
|
case "websocket_message":
|
|
|
|
this.onWebsocketMessageTrigger.trigger({url: event[2],message: event[3],isBinary: event[4]});
|
|
|
|
case "websocket_success":
|
|
|
|
this.onWebsocketSuccessTrigger.trigger({url: event[2],handle: event[3]});
|
|
|
|
default:
|
|
|
|
Log.error("Unknown cc event: " + eventName);
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:17:38 +00:00
|
|
|
private static function ccButtonToEnum(button: Dynamic): ButtonType {
|
|
|
|
switch button {
|
|
|
|
case 1:
|
|
|
|
return Left;
|
|
|
|
case 2:
|
|
|
|
return Middle;
|
|
|
|
case 3:
|
|
|
|
return Right;
|
|
|
|
default:
|
|
|
|
throw new Exception("Invalid input");
|
|
|
|
}
|
2021-12-20 00:55:30 +00:00
|
|
|
}
|
|
|
|
}
|