Initial freedesktop_notifier utility

This commit is contained in:
Emery Hemingway 2022-06-09 23:49:49 -05:00
parent cf0ac36d4b
commit ef15b583df
7 changed files with 135 additions and 2 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ http_translator
json_translator
msg
json_socket_translator
freedesktop_notifier

View File

@ -1,5 +1,9 @@
# Syndicate utils
## freedesktop_notifier
Does that desktop notification thing. See [notifications.prs](./notifications.prs) for the message schema.
## http_translator
Dispatches HTTP requests to registered handlers.

3
notifications.prs Normal file
View File

@ -0,0 +1,3 @@
version 1 .
Urgency = =Low / =Normal / =Critical .
Notify = <notify @summary string @body string @timeout int @urgency Urgency> .

View File

@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, strutils]
import syndicate
import ./private/libnotify
import ./notifications
bootDataspace("main") do (ds: Ref; turn: var Turn):
connectStdio(ds, turn)
let nc = newNotifyClient("syndicate")
onMessage(turn, ds, ?Notify) do (s: string, b: string, t: int, u: Urgency):
nc.send_new_notification(
summary = s,
body = b,
icon_fname = "",
timeout = t,
urgency = NotificationUrgency u)
runForever()

18
src/notifications.nim Normal file
View File

@ -0,0 +1,18 @@
import
std/typetraits, preserves
type
Notify* {.preservesRecord: "notify".} = object
`summary`*: string
`body`*: string
`timeout`*: int
`urgency`*: Urgency
`Urgency`* {.preservesOr, pure.} = enum
`Low`, `Normal`, `Critical`
proc `$`*(x: Notify): string =
`$`(toPreserve(x))
proc encode*(x: Notify): seq[byte] =
encode(toPreserve(x))

87
src/private/libnotify.nim Normal file
View File

@ -0,0 +1,87 @@
##
## A minimalistic wrapper for libnotify
## https://github.com/FedericoCeratto/nim-libnotify
##
## Wraps libnotify.so.4 https://developer.gnome.org/libnotify/
##
## Released under LGPLv3, see LICENSE file
## 2015 - Federico Ceratto <federico.ceratto@gmail.com>
##
{.deadCodeElim: on.}
import glib2
const libnotify_fn* = "libnotify.so.4"
type Notify = pointer
type NotificationUrgency* = enum
Low, Normal, Critical
proc notify_init*(name: cstring): bool
{.importc: "notify_init", dynlib: libnotify_fn.}
proc notify_uninit*(): void {.importc: "notify_uninit",
dynlib: libnotify_fn.}
proc notify_is_initted(): bool
{.importc: "notify_is_initted", dynlib: libnotify_fn.}
proc notify_get_app_name(): cstring
{.importc: "notify_get_app_name", dynlib: libnotify_fn.}
proc notify_set_app_name(name: cstring): void
{.importc: "notify_set_app_name", dynlib: libnotify_fn.}
proc notify_notification_new(a, b, c: cstring): Notify
{.importc: "notify_notification_new", dynlib: libnotify_fn.}
proc notify_notification_show(notification: Notify, gerror: cint): gboolean
{.importc: "notify_notification_show", dynlib: libnotify_fn.}
proc notify_notification_set_timeout(notification: Notify, timeout: cint): void
{.importc: "notify_notification_set_timeout", dynlib: libnotify_fn.}
proc notify_notification_set_urgency(notification: Notify, urgency: NotificationUrgency): void
{.importc: "notify_notification_set_urgency", dynlib: libnotify_fn.}
type NotifyClient* = ref object of RootObj
proc newNotifyClient*(app_name: string): NotifyClient =
## Initialize notification client
if notify_is_initted() == false:
doAssert notify_init(app_name)
doAssert notify_is_initted()
new(NotifyClient)
proc get_app_name*(self: NotifyClient): string =
## Get application name
return $notify_get_app_name()
proc set_app_name*(self: NotifyClient, app_name: string) =
## Set application name
notify_set_app_name(app_name.cstring)
proc send_new_notification*(self: NotifyClient, summary, body, icon_fname: string, timeout=0,
urgency=NotificationUrgency.Normal) =
## Show a notification. Optionally specify a timeout in milliseconds and an
## urgency as a NotificationUrgency.Low / Normal / Critical
let n = notify_notification_new(summary.cstring, body.cstring, icon_fname.cstring)
notify_notification_set_urgency(n, urgency)
if timeout != 0:
notify_notification_set_timeout(n, timeout.cint)
let error: cint = 0
doAssert notify_notification_show(n, error)
if error != 0:
raise newException(Exception, "Unhandled exception")
proc uninit*(self: NotifyClient) =
## Uninitialize the library
notify_uninit()

View File

@ -5,9 +5,9 @@ author = "Emery Hemingway"
description = "Utilites for Syndicated Actors and Synit"
license = "unlicense"
srcDir = "src"
bin = @["http_translator", "json_socket_translator", "json_translator", "msg"]
bin = @["http_translator", "json_socket_translator", "json_translator", "msg", "freedesktop_notifier"]
# Dependencies
requires "nim >= 1.6.6", "syndicate >= 0.3.1"
requires "nim >= 1.6.6", "syndicate >= 0.3.1", "gtk2 >= 1.3"