feat: implement proper signal handling in examples
This commit is contained in:
parent
e68223f1e6
commit
ba5ebc4f4d
|
@ -0,0 +1,16 @@
|
||||||
|
import system/ansi_c
|
||||||
|
|
||||||
|
export SIG_DFL, SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM
|
||||||
|
|
||||||
|
when not defined(windows):
|
||||||
|
export SIGPIPE
|
||||||
|
var
|
||||||
|
SIG_IGN* {.importc: "SIG_IGN", header: "<signal.h>".}: cint
|
||||||
|
SIGHUP* {.importc: "SIGHUP", header: "<signal.h>".}: cint
|
||||||
|
SIGQUIT* {.importc: "SIGQUIT", header: "<signal.h>".}: cint
|
||||||
|
|
||||||
|
type CSighandlerT = proc (a: cint) {.noconv.}
|
||||||
|
|
||||||
|
proc setSignalProc* (`proc`: CSighandlerT, signals: varargs[cint]) =
|
||||||
|
for sig in signals:
|
||||||
|
discard c_signal(sig, `proc`)
|
|
@ -1,16 +1,17 @@
|
||||||
import std/[logging, os]
|
import std/[logging, os]
|
||||||
import jacket
|
import jacket
|
||||||
|
import signal
|
||||||
|
|
||||||
var jclient: ClientTPtr
|
var jclient: ClientTPtr
|
||||||
var status: cint
|
var status: cint
|
||||||
var log = newConsoleLogger(lvlInfo)
|
var log = newConsoleLogger(lvlDebug)
|
||||||
|
|
||||||
proc errorCb(msg: cstring) {.cdecl.} =
|
proc errorCb(msg: cstring) {.cdecl.} =
|
||||||
# Suppress verbose JACK error messages when server is not available by
|
# Suppress verbose JACK error messages when server is not available by
|
||||||
# default. Pass ``lvlAll`` when creating the logger to enable them.
|
# default. Pass ``lvlAll`` when creating the logger to enable them.
|
||||||
debug "JACK error: " & $msg
|
debug "JACK error: " & $msg
|
||||||
|
|
||||||
proc cleanup() {.noconv.} =
|
proc cleanup(sig: cint) {.noconv.} =
|
||||||
debug "Cleaning up..."
|
debug "Cleaning up..."
|
||||||
|
|
||||||
if jclient != nil:
|
if jclient != nil:
|
||||||
|
@ -46,7 +47,10 @@ if jclient == nil:
|
||||||
error getJackStatusErrorString(status)
|
error getJackStatusErrorString(status)
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
setControlCHook(cleanup)
|
when defined(windows):
|
||||||
|
setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
|
||||||
|
else:
|
||||||
|
setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
|
||||||
|
|
||||||
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
|
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
|
||||||
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
|
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
|
||||||
|
@ -58,4 +62,4 @@ if jclient.activate() == 0:
|
||||||
while true:
|
while true:
|
||||||
sleep(50)
|
sleep(50)
|
||||||
|
|
||||||
cleanup()
|
cleanup()
|
|
@ -1,22 +1,23 @@
|
||||||
import std/[logging, os]
|
import std/[logging, os]
|
||||||
|
import signal
|
||||||
import jacket
|
import jacket
|
||||||
|
|
||||||
var jclient: ClientTPtr
|
var jclient: ClientTPtr
|
||||||
var status: cint
|
var status: cint
|
||||||
var log = newConsoleLogger(lvlInfo)
|
var log = newConsoleLogger(lvlDebug)
|
||||||
|
|
||||||
proc errorCb(msg: cstring) {.cdecl.} =
|
proc errorCb(msg: cstring) {.cdecl.} =
|
||||||
# Suppress verbose JACK error messages when server is not available by
|
# Suppress verbose JACK error messages when server is not available by
|
||||||
# default. Pass ``lvlAll`` when creating the logger to enable them.
|
# default. Pass ``lvlAll`` when creating the logger to enable them.
|
||||||
debug "JACK error: " & $msg
|
debug "JACK error: " & $msg
|
||||||
|
|
||||||
proc cleanup() {.noconv.} =
|
proc cleanup(sig: cint = 0) {.noconv.} =
|
||||||
debug "Cleaning up..."
|
debug "Cleaning up..."
|
||||||
|
|
||||||
if jclient != nil:
|
if jclient != nil:
|
||||||
discard jclient.clientClose
|
discard jclient.clientClose
|
||||||
jclient = nil
|
jclient = nil
|
||||||
|
|
||||||
quit QuitSuccess
|
quit QuitSuccess
|
||||||
|
|
||||||
addHandler(log)
|
addHandler(log)
|
||||||
|
@ -28,7 +29,10 @@ if jclient == nil:
|
||||||
error getJackStatusErrorString(status)
|
error getJackStatusErrorString(status)
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
setControlCHook(cleanup)
|
when defined(windows):
|
||||||
|
setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM)
|
||||||
|
else:
|
||||||
|
setSignalProc(cleanup, SIGHUP, SIGINT, SIGQUIT, SIGTERM)
|
||||||
|
|
||||||
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
|
discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0)
|
||||||
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
|
discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0)
|
||||||
|
@ -36,4 +40,4 @@ discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord,
|
||||||
while true:
|
while true:
|
||||||
sleep(50)
|
sleep(50)
|
||||||
|
|
||||||
cleanup()
|
cleanup() # normally not reached
|
||||||
|
|
Loading…
Reference in New Issue