Compare commits

...

3 Commits

Author SHA1 Message Date
Emery Hemingway bbd122a2f5 Use !assert_built 2023-05-06 20:25:55 +01:00
Emery Hemingway 29fa33b1c6 Publish server info 2023-05-06 20:25:44 +01:00
Emery Hemingway 1b62a1e571 Use runActor proc 2023-05-06 20:25:34 +01:00
5 changed files with 56 additions and 28 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20230405"
version = "20230506"
author = "Emery Hemingway"
description = "Actor for sending notifications with Libnotify"
license = "Unlicense"

View File

@ -1,3 +1,4 @@
version 1.
Attrs = {symbol: string ...:...} .
Notify = <notify @summary string @attrs Attrs> .
ServerInfo = <server-info @name string @vendor string @version string @specVersion string> .

View File

@ -1,3 +1,4 @@
include_rules
: foreach ../*.prs |> !preserves_schema_nim |> {schema}
: libnotify_actor.nim | {schema} $(SYNDICATE_PROTOCOL) |> !nim_bin |>
: libnotify_actor.nim | {schema} $(SYNDICATE_PROTOCOL) |> !nim_bin |> {bin}
: {bin} |> !assert_built |>

View File

@ -21,7 +21,9 @@ type
message: cstring
proc init(appName: cstring): bool {.notify.}
# proc uninit() {.notify.}
proc uninit() {.notify.}
proc get_server_info(name, vendor, version, specVersion: ptr cstring): bool {.notify.}
proc notification_new(summary, body, icon: cstring = nil): Notification {.notify.}
proc show(n: Notification; err: ptr GError): bool {.notification.}
@ -41,31 +43,49 @@ const appName = "libnotify_actor"
if not init(appName):
quit "failed to initialize libnotify"
bootDataspace(appName) do (ds: Ref; turn: var Turn):
connectStdio(ds, turn)
#during(turn, ds, grab()) do (config: Config):
# discard init(config.app_name)
onMessage(turn, ds, ?Notify) do (summary: string, attrs: Attrs):
let
body = getOrDefault(attrs, Symbol"body")
icon = getOrDefault(attrs, Symbol"icon")
n = notification_new(summary, body, icon)
proc assertServerInfo(turn: var Turn; ds: Ref) =
var name, vendor, version, specVersion: cstring
if get_server_info(addr name, addr vendor, addr version, addr specVersion):
var a = ServerInfo(
name: $name,
vendor: $vendor,
version: $version,
specVersion: $specVersion,
)
discard publish(turn, ds, a)
case getOrDefault(attrs, Symbol"urgency").normalize
of "high", "critical":
set_urgency(n, Critical)
of "low":
set_urgency(n, Low)
else: discard
type Args {.preservesDictionary.} = object
dataspace: Ref
let app_name = getOrDefault(attrs, Symbol"app_name")
if app_name != "":
set_app_name(n, app_name)
runActor("main") do (root: Ref; turn: var Turn):
connectStdio(root, turn)
during(turn, root, ?Args) do (ds: Ref):
if not init(appName):
stderr.writeLine "failed to initialize libnotify"
else:
assertServerInfo(turn, ds)
onMessage(turn, ds, ?Notify) do (summary: string, attrs: Attrs):
let
body = getOrDefault(attrs, Symbol"body")
icon = getOrDefault(attrs, Symbol"icon")
n = notification_new(summary, body, icon)
let category = getOrDefault(attrs, Symbol"category")
if category != "":
set_category(n, category)
case getOrDefault(attrs, Symbol"urgency").normalize
of "high", "critical":
set_urgency(n, Critical)
of "low":
set_urgency(n, Low)
else: discard
show(n)
let app_name = getOrDefault(attrs, Symbol"app_name")
if app_name != "":
set_app_name(n, app_name)
runForever()
let category = getOrDefault(attrs, Symbol"category")
if category != "":
set_category(n, category)
show(n)
do:
uninit()

View File

@ -8,8 +8,14 @@ type
`summary`*: string
`attrs`*: Attrs
proc `$`*(x: Attrs | Notify): string =
ServerInfo* {.preservesRecord: "server-info".} = object
`name`*: string
`vendor`*: string
`version`*: string
`specVersion`*: string
proc `$`*(x: Attrs | Notify | ServerInfo): string =
`$`(toPreserve(x))
proc encode*(x: Attrs | Notify): seq[byte] =
proc encode*(x: Attrs | Notify | ServerInfo): seq[byte] =
encode(toPreserve(x))