Skip to content

Commit

Permalink
IPC: Add event for bidirectional communication between the game and t…
Browse files Browse the repository at this point in the history
…he projector (#2257)

* IPC: Add event for bidirectional communication between the game and the projector

* Test Generic Event parsing and writing
  • Loading branch information
soywiz committed Jul 2, 2024
1 parent 4010991 commit c009580
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
19 changes: 19 additions & 0 deletions korge-core/src/korlibs/event/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,25 @@ data class TouchEvent(
val isEnd get() = type == Type.END
}

class GenericEvent(
override var type: Type,
var kind: String,
var data: ByteArray
) : Event(), TEvent<GenericEvent> {
enum class Type : EventType<GenericEvent> {
PROJECTOR_TO_GAME, GAME_TO_PROJECTOR;
companion object {
val ALL = Type.entries
}
}

fun copyFrom(other: GenericEvent) {
this.type = other.type
this.kind = other.kind
this.data = other.data
}
}

data class KeyEvent constructor(
override var type: Type = Type.UP,
var id: Int = 0,
Expand Down
16 changes: 16 additions & 0 deletions korge-ipc/src/main/kotlin/korlibs/korge/ipc/KorgeIPCSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ class IPCPacket(
val KEY_UP = 0x0302
val KEY_TYPE = 0x0303

val EVENT_GAME_TO_PROJECTOR = 0x0401
val EVENT_PROJECTOR_TO_GAME = 0x0402

val REQUEST_NODE_CHILDREN = 0x7701
val REQUEST_NODE_PROPS = 0x7702
val REQUEST_NODE_SET_PROP = 0x7703
Expand Down Expand Up @@ -239,6 +242,19 @@ class IPCPacket(
}
}

fun IPCPacket.Companion.genericEventGen(kind: String, eventData: ByteArray): ByteArray = MemorySyncStreamToByteArray(16 + kind.length + 4 + eventData.size + 4) {
val kindBytes = kind.encodeToByteArray()
writeU_VL(kindBytes.size); writeBytes(kindBytes)
writeU_VL(eventData.size); writeBytes(eventData)
}

fun IPCPacket.Companion.genericEventParse(data: ByteArray): Pair<String, ByteArray> {
val s = data.openFastStream()
val type = s.readBytes(s.readU_VL()).decodeToString()
val data = s.readBytes(s.readU_VL())
return type to data
}

private operator fun IntBuffer.set(index: Int, value: Int) {
this.put(index, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ class KorgeIPCServerSocketTest {

assertEquals("HELLO", bytes.decodeToString())
}

@Test
fun testGenericEvent() {
val bytes = IPCPacket.genericEventGen("hello", byteArrayOf(1, 2, 3))
val (kind, data) = IPCPacket.genericEventParse(bytes)
assertEquals("hello", kind)
assertEquals(byteArrayOf(1, 2, 3).toList(), data.toList())
}
}
10 changes: 9 additions & 1 deletion korge/src@jvm/korlibs/korge/IPCViewsCompleter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package korlibs.korge

import korlibs.event.*
import korlibs.graphics.*
import korlibs.io.stream.*
import korlibs.korge.ipc.*
import korlibs.korge.view.*
import korlibs.korge.view.property.*
Expand All @@ -17,6 +18,10 @@ class IPCViewsCompleter : ViewsCompleter {

val viewsNodeId = ViewsNodeId(views)

views.onEvent(GenericEvent.Type.GAME_TO_PROJECTOR) { e ->
ipc.writeEvent(IPCPacket(IPCPacket.EVENT_GAME_TO_PROJECTOR, IPCPacket.genericEventGen(e.kind, e.data)))
}

views.onBeforeRender {
while (true) {
val e = ipc.tryReadEvent() ?: break
Expand Down Expand Up @@ -44,7 +49,10 @@ class IPCViewsCompleter : ViewsCompleter {
str = null,
)
}

IPCPacket.EVENT_PROJECTOR_TO_GAME -> {
val (type, data) = IPCPacket.genericEventParse(e.data)
views.dispatch(GenericEvent(GenericEvent.Type.PROJECTOR_TO_GAME, type, data))
}
IPCPacket.MOUSE_MOVE, IPCPacket.MOUSE_DOWN, IPCPacket.MOUSE_UP, IPCPacket.MOUSE_CLICK, IPCPacket.MOUSE_SCROLL -> {
val x = e.buffer.getInt()
val y = e.buffer.getInt()
Expand Down

0 comments on commit c009580

Please sign in to comment.