jacket/examples/test_port_connect_cb.nim

62 lines
1.6 KiB
Nim
Raw Normal View History

2022-10-09 09:43:13 +02:00
import std/[logging, os]
import jacket
var jclient: ClientTPtr
var status: cint
2022-10-09 09:43:13 +02:00
var log = newConsoleLogger(lvlInfo)
proc errorCb(msg: cstring) {.cdecl.} =
# Suppress verbose JACK error messages when server is not available by
# default. Pass ``lvlAll`` when creating the logger to enable them.
debug "JACK error: " & $msg
proc cleanup() {.noconv.} =
2022-10-09 09:43:13 +02:00
debug "Cleaning up..."
if jclient != nil:
2022-10-09 09:43:13 +02:00
discard jclient.deactivate()
discard jclient.clientClose()
jclient = nil
2022-10-09 09:43:13 +02:00
quit QuitSuccess
proc portConnected(portA: PortIdT; portB: PortIdT; connect: cint; arg: pointer) {.cdecl.} =
2022-10-09 09:43:13 +02:00
let portAPtr = jclient.portById(portA)
let portBPtr = jclient.portById(portB)
if portAPtr != nil:
2022-10-09 09:43:13 +02:00
echo("Port A: ", portName(portAPtr))
else:
echo "Port A: <unknown>"
if portAPtr != nil:
2022-10-09 09:43:13 +02:00
echo("Port B: ", portName(portBPtr))
else:
echo "Port B: <unknown>"
2022-10-09 09:43:13 +02:00
echo("Action: ", if connect > 0: "connect" else: "disconnect")
2022-10-09 09:43:13 +02:00
addHandler(log)
setErrorFunction(errorCb)
jclient = clientOpen("jacket_port_register", NoStartServer.ord, status.addr)
debug "Server status: " & $status
if jclient == nil:
2022-10-09 09:43:13 +02:00
error getJackStatusErrorString(status)
quit 1
setControlCHook(cleanup)
2022-10-09 09:43:13 +02:00
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
2022-10-09 09:43:13 +02:00
if jclient.setPortConnectCallback(portConnected, nil) != 0:
error "Error: could not set port connection callback."
2022-10-09 09:43:13 +02:00
if jclient.activate() == 0:
while true:
sleep(50)
cleanup()