libnotify_actor/src/libnotify_actor.nim

72 lines
2.0 KiB
Nim

# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
include ./protocol
import std/[asyncdispatch]
from std/strutils import normalize
import syndicate
{.passC: staticExec("pkg-config --cflags libnotify").}
{.passL: staticExec("pkg-config --libs libnotify").}
const notifyHeader = "<libnotify/notify.h>"
{.pragma: notify, header: notifyHeader, importc: "notify_$1".}
{.pragma: notification, header: notifyHeader, importc: "notify_notification_$1".}
type
Notification {.importc: "NotifyNotification".} = ptr object
discard
GError {.importc.} = ptr object
message: cstring
proc init(appName: cstring): bool {.notify.}
# proc uninit() {.notify.}
proc notification_new(summary, body, icon: cstring = nil): Notification {.notify.}
proc show(n: Notification; err: ptr GError): bool {.notification.}
proc set_app_name(n: Notification; s: string) {.notification.}
proc set_category(n: Notification; s: string) {.notification.}
type NotifyUrgency {.importc.} = enum Low, Normal, Critical
proc set_urgency(n: Notification; u: NotifyUrgency) {.notification.}
proc show(n: Notification) =
if not show(n, nil):
stderr.writeLine "show failed"
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)
case getOrDefault(attrs, Symbol"urgency").normalize
of "high", "critical":
set_urgency(n, Critical)
of "low":
set_urgency(n, Low)
else: discard
let app_name = getOrDefault(attrs, Symbol"app_name")
if app_name != "":
set_app_name(n, app_name)
let category = getOrDefault(attrs, Symbol"category")
if category != "":
set_category(n, category)
show(n)
runForever()