aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/corvid-msg/main.go22
-rw-r--r--cmd/corvid/main.go51
-rw-r--r--srv/corvidServer.go39
-rw-r--r--srv/notifServer.go76
-rw-r--r--srv/server.go88
5 files changed, 145 insertions, 131 deletions
diff --git a/cmd/corvid-msg/main.go b/cmd/corvid-msg/main.go
deleted file mode 100644
index abb87a5..0000000
--- a/cmd/corvid-msg/main.go
+++ /dev/null
@@ -1,22 +0,0 @@
-package main
-
-import (
- "log"
-
- "github.com/godbus/dbus/v5"
-)
-
-func main() {
- const CORVID_DBUS_OBJECT = "/sh/cxl/Corvid"
- const CORVID_DBUS_NAME = "sh.cxl.Corvid"
-
- conn, err := dbus.SessionBus()
- if err != nil {
- log.Fatal(err)
- }
-
- call := conn.Object(CORVID_DBUS_NAME, CORVID_DBUS_OBJECT).Call(CORVID_DBUS_NAME+".Test", 0, uint32(13))
- if call.Err != nil {
- log.Fatal(call.Err)
- }
-}
diff --git a/cmd/corvid/main.go b/cmd/corvid/main.go
index 9064ad6..53c84aa 100644
--- a/cmd/corvid/main.go
+++ b/cmd/corvid/main.go
@@ -1,10 +1,61 @@
package main
import (
+ "fmt"
+ "os"
+ "strconv"
+
"github.com/CartConnoisseur/corvid/srv"
+ "github.com/godbus/dbus/v5"
)
func main() {
+ if len(os.Args) < 2 {
+ server()
+ }
+
+ switch os.Args[1] {
+ case "server":
+ server()
+ case "dismiss":
+ if len(os.Args) < 3 {
+ fmt.Println("dismiss command requires positional argument 'id'")
+ os.Exit(1)
+ }
+
+ id, err := strconv.ParseInt(os.Args[2], 0, 32)
+ if err != nil || id <= 0 {
+ fmt.Printf("invalid value for positional argument 'id' (must be u64): %s\n", os.Args[2])
+ os.Exit(1)
+ }
+
+ call("Dismiss", uint32(id))
+ case "dismiss-all":
+ call("DismissAll")
+ default:
+ fmt.Printf("unknown subcommand: %s\n", os.Args[1])
+ os.Exit(1)
+ }
+}
+
+func server() {
srv.Start()
select {}
}
+
+func call(name string, args ...interface{}) error {
+ const CORVID_DBUS_OBJECT = "/sh/cxl/Corvid"
+ const CORVID_DBUS_NAME = "sh.cxl.Corvid"
+
+ conn, err := dbus.SessionBus()
+ if err != nil {
+ return err
+ }
+
+ call := conn.Object(CORVID_DBUS_NAME, CORVID_DBUS_OBJECT).Call(CORVID_DBUS_NAME+"."+name, 0, args...)
+ if call.Err != nil {
+ return call.Err
+ }
+
+ return nil
+}
diff --git a/srv/corvidServer.go b/srv/corvidServer.go
index 4a0556d..8b08a6e 100644
--- a/srv/corvidServer.go
+++ b/srv/corvidServer.go
@@ -1,39 +1,24 @@
package srv
import (
- "log"
-
"github.com/godbus/dbus/v5"
)
type corvidServer server
-func (s corvidServer) Test(param uint32) (e *dbus.Error) {
- log.Printf("Test called %d", param)
+func (s corvidServer) Dismiss(id uint32) (e *dbus.Error) {
+ // log.Print("Dismiss called")
+ server(s).close(id, CloseReasonDismissed)
+ server(s).output()
return nil
}
-// func (s corvidServer) GetCapabilities() (e *dbus.Error) {
-// log.Print("GetCapabilities called")
-// return nil
-// }
-
-// func (s corvidServer) GetServerInformation() (name, vendor, version, specVersion string, e *dbus.Error) {
-// // log.Print("GetServerInformation called")
-// return "corvid", "CartConnoisseur", "0.1.0", "1.2", nil
-// }
-
-// func (s corvidServer) CloseNotification(id uint32) (e *dbus.Error) {
-// // log.Printf("CloseNotification called: %d", id)
-// notification, ok := notifications.notifications[id]
-// if ok {
-// notification.close(CloseReasonClosed)
-// }
-
-// return nil
-// }
+func (s corvidServer) DismissAll() (e *dbus.Error) {
+ // log.Print("DismissAll called")
+ for _, notification := range s.notifications.notifications {
+ server(s).close(notification.Id, CloseReasonDismissed)
+ }
-// func (s corvidServer) Notify(appName string, replacesId uint32, appIcon string, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (id uint32, e *dbus.Error) {
-// // log.Print("Notify called")
-
-// }
+ server(s).output()
+ return nil
+}
diff --git a/srv/notifServer.go b/srv/notifServer.go
index 00038cc..d175055 100644
--- a/srv/notifServer.go
+++ b/srv/notifServer.go
@@ -1,23 +1,17 @@
package srv
import (
- "encoding/json"
- "fmt"
"image"
"image/png"
"log"
"os"
- "slices"
"strings"
"time"
"github.com/godbus/dbus/v5"
)
-type notifServer struct {
- notifications *notificationStack
- server
-}
+type notifServer server
func (s notifServer) GetCapabilities() (capabilities []string, e *dbus.Error) {
// log.Print("GetCapabilities called")
@@ -34,7 +28,8 @@ func (s notifServer) GetServerInformation() (name, vendor, version, specVersion
func (s notifServer) CloseNotification(id uint32) (e *dbus.Error) {
// log.Printf("CloseNotification called: %d", id)
- s.close(id, CloseReasonClosed)
+ server(s).close(id, CloseReasonClosed)
+ server(s).output()
return nil
}
@@ -124,72 +119,13 @@ func (s notifServer) Notify(appName string, replacesId uint32, appIcon string, s
if expireTimeout != 0 {
notification.timer = time.AfterFunc(time.Duration(expireTimeout)*time.Millisecond, func() {
- s.close(notification.Id, CloseReasonExpire)
+ server(s).close(notification.Id, CloseReasonExpire)
+ server(s).output()
})
}
s.notifications.notifications[id] = notification
- s.output()
+ server(s).output()
return id, nil
}
-
-func (s notifServer) close(id uint32, reason closeReason) {
- s.notifications.mutex.Lock()
- defer s.notifications.mutex.Unlock()
-
- n, ok := s.notifications.notifications[id]
- if !ok {
- return
- }
-
- if n.timer != nil {
- n.timer.Stop()
- }
-
- if n.Image != "" {
- os.Remove(n.Image)
- }
-
- delete(s.notifications.notifications, n.Id)
- s.output()
-
- err := s.conn.Emit(s.object, s.name+".NotificationClosed", n.Id, reason)
- if err != nil {
- log.Print(err)
- }
-}
-
-// TODO: relocate to cmd/corvid
-func (s notifServer) output() {
- arr := make([]notification, len(s.notifications.notifications))
-
- i := 0
- for _, notification := range s.notifications.notifications {
- arr[i] = notification
- i++
- }
-
- slices.SortFunc(arr, func(a, b notification) int {
- if a.Timestamp > b.Timestamp {
- return SORT_DIRECTION
- } else if a.Timestamp < b.Timestamp {
- return -SORT_DIRECTION
- } else {
- if a.Id > b.Id {
- return SORT_DIRECTION
- } else if a.Id < b.Id {
- return -SORT_DIRECTION
- }
- }
-
- return 0
- })
-
- j, err := json.Marshal(arr)
- if err != nil {
- log.Fatalln(err)
- }
-
- fmt.Println(string(j))
-}
diff --git a/srv/server.go b/srv/server.go
index 673081b..27ced3f 100644
--- a/srv/server.go
+++ b/srv/server.go
@@ -1,8 +1,11 @@
package srv
import (
+ "encoding/json"
"fmt"
"log"
+ "os"
+ "slices"
"sync"
"github.com/godbus/dbus/v5"
@@ -11,10 +14,70 @@ import (
const DEFAULT_EXPIRATION = 5000
const SORT_DIRECTION = 1 // 1 = newest first, -1 = oldest first
-type server = struct {
- conn *dbus.Conn
- object dbus.ObjectPath
- name string
+type server struct {
+ notifications *notificationStack
+ conn *dbus.Conn
+ object dbus.ObjectPath
+ name string
+}
+
+func (s server) close(id uint32, reason closeReason) {
+ s.notifications.mutex.Lock()
+ defer s.notifications.mutex.Unlock()
+
+ n, ok := s.notifications.notifications[id]
+ if !ok {
+ return
+ }
+
+ if n.timer != nil {
+ n.timer.Stop()
+ }
+
+ if n.Image != "" {
+ os.Remove(n.Image)
+ }
+
+ delete(s.notifications.notifications, n.Id)
+
+ err := s.conn.Emit(s.object, s.name+".NotificationClosed", n.Id, reason)
+ if err != nil {
+ log.Print(err)
+ }
+}
+
+// TODO: relocate to cmd/corvid
+func (s server) output() {
+ arr := make([]notification, len(s.notifications.notifications))
+
+ i := 0
+ for _, notification := range s.notifications.notifications {
+ arr[i] = notification
+ i++
+ }
+
+ slices.SortFunc(arr, func(a, b notification) int {
+ if a.Timestamp > b.Timestamp {
+ return SORT_DIRECTION
+ } else if a.Timestamp < b.Timestamp {
+ return -SORT_DIRECTION
+ } else {
+ if a.Id > b.Id {
+ return SORT_DIRECTION
+ } else if a.Id < b.Id {
+ return -SORT_DIRECTION
+ }
+ }
+
+ return 0
+ })
+
+ j, err := json.Marshal(arr)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ fmt.Println(string(j))
}
func Start() {
@@ -37,9 +100,10 @@ func Start() {
err = startDBusServer(
conn,
corvidServer{
- conn: conn,
- object: CORVID_DBUS_OBJECT,
- name: CORVID_DBUS_NAME,
+ notifications: &notifications,
+ conn: conn,
+ object: CORVID_DBUS_OBJECT,
+ name: CORVID_DBUS_NAME,
},
CORVID_DBUS_OBJECT,
CORVID_DBUS_NAME,
@@ -52,11 +116,9 @@ func Start() {
conn,
notifServer{
notifications: &notifications,
- server: server{
- conn: conn,
- object: NOTIF_DBUS_OBJECT,
- name: NOTIF_DBUS_NAME,
- },
+ conn: conn,
+ object: NOTIF_DBUS_OBJECT,
+ name: NOTIF_DBUS_NAME,
},
NOTIF_DBUS_OBJECT,
NOTIF_DBUS_NAME,
@@ -64,6 +126,8 @@ func Start() {
if err != nil {
log.Fatal(err)
}
+
+ fmt.Println("[]")
}
func startDBusServer(conn *dbus.Conn, v interface{}, object dbus.ObjectPath, name string) error {