├── LICENSE ├── README ├── examples └── single_notification │ ├── Makefile │ ├── main.c │ └── main.go └── notify.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012 LENORMAND Frank 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a fork of https://github.com/lenormf/go-notify go-notify with some minor corrections. 2 | 3 | Install : 4 | 5 | # install libnotify devel librairies, 6 | sudo apt-get install libnotify-dev # debian style 7 | 8 | # installing go-notify 9 | go get github.com/mqu/go-notify 10 | 11 | C Dependencies: libnotify (sudo apt-get install libnotify-dev) 12 | 13 | GO Dependencies: github.com/mattn/go-gtk/glib 14 | 15 | This package provides GO bindings for the C library libnotify. 16 | Although this package provides full retro-compatibility with the regular C 17 | library, it also provides OOP-like functions for the NotifyNotification object. 18 | 19 | The notify package depends on mattn's glib wrapper for the GO language. 20 | 21 | See example for usage. 22 | -------------------------------------------------------------------------------- /examples/single_notification/Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile for go-notify 3 | ## by lenorm_f 4 | ## 5 | 6 | include $(GOROOT)/src/Make.inc 7 | 8 | TARG = example_go 9 | 10 | GOFILES =\ 11 | main.go 12 | 13 | CLEANFILES += example_c 14 | 15 | include $(GOROOT)/src/Make.cmd 16 | 17 | example_c: main.c 18 | gcc -Wall -Wextra `pkg-config --cflags --libs libnotify` -o example_c main.c 19 | -------------------------------------------------------------------------------- /examples/single_notification/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c for go-notify 3 | * by lenorm_f 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #define DELAY 3000 11 | 12 | int main () { 13 | NotifyNotification *hello; 14 | 15 | notify_init("Hello World!"); 16 | hello = notify_notification_new("Hello World!", 17 | "This is an example notification.", NULL); 18 | if (!hello) 19 | return 1; 20 | notify_notification_set_timeout(hello, DELAY); 21 | 22 | if (!notify_notification_show(hello, NULL)) 23 | return 3; 24 | 25 | sleep(DELAY / 1000); 26 | notify_notification_close(hello, NULL); 27 | 28 | notify_uninit(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /examples/single_notification/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * main.go for go-notify 3 | * by lenorm_f 4 | */ 5 | 6 | package main 7 | 8 | import notify "github.com/mqu/go-notify" 9 | import ( 10 | "os" 11 | "fmt" 12 | "time" 13 | ) 14 | 15 | const ( 16 | DELAY = 3000; 17 | ) 18 | 19 | func main() { 20 | notify.Init("Hello World!") 21 | hello := notify.NotificationNew("Hello World!", 22 | "This is an example notification.", 23 | "") 24 | 25 | if hello == nil { 26 | fmt.Fprintf(os.Stderr, "Unable to create a new notification\n") 27 | return 28 | } 29 | // hello.SetTimeout(3000) 30 | notify.NotificationSetTimeout(hello, DELAY) 31 | 32 | // hello.Show() 33 | if e := notify.NotificationShow(hello); e != nil { 34 | fmt.Fprintf(os.Stderr, "%s\n", e.Message()) 35 | return 36 | } 37 | 38 | time.Sleep(DELAY * 1000000) 39 | // hello.Close() 40 | notify.NotificationClose(hello) 41 | 42 | notify.UnInit() 43 | } 44 | -------------------------------------------------------------------------------- /notify.go: -------------------------------------------------------------------------------- 1 | /* 2 | * notify.go for go-notify 3 | * by lenorm_f 4 | */ 5 | 6 | package notify 7 | 8 | /* 9 | #cgo pkg-config: libnotify 10 | 11 | #include 12 | #include 13 | */ 14 | import "C" 15 | import "unsafe" 16 | import glib "github.com/mattn/go-gtk/glib" 17 | 18 | /* 19 | * Exported Types 20 | */ 21 | type NotifyNotification struct { 22 | _notification *C.NotifyNotification 23 | } 24 | 25 | const ( 26 | NOTIFY_URGENCY_LOW = 0; 27 | NOTIFY_URGENCY_NORMAL = 1; 28 | NOTIFY_URGENCY_CRITICAL = 2; 29 | ) 30 | 31 | type NotifyUrgency int 32 | type NotifyActionCallback func (*NotifyNotification, string, interface {}) 33 | 34 | /* 35 | * Private Functions 36 | */ 37 | func new_notify_notification(cnotif *C.NotifyNotification) *NotifyNotification { 38 | return &NotifyNotification{cnotif} 39 | } 40 | 41 | /* 42 | * Exported Functions 43 | */ 44 | // Pure Functions 45 | func Init(app_name string) bool { 46 | papp_name := C.CString(app_name) 47 | defer C.free(unsafe.Pointer(papp_name)) 48 | 49 | return bool(C.notify_init(papp_name) != 0) 50 | } 51 | 52 | func UnInit() { 53 | C.notify_uninit() 54 | } 55 | 56 | func IsInitted() bool { 57 | return C.notify_is_initted() != 0 58 | } 59 | 60 | func GetAppName() string { 61 | return C.GoString(C.notify_get_app_name()) 62 | } 63 | 64 | func GetServerCaps() *glib.List { 65 | var gcaps *C.GList 66 | 67 | gcaps = C.notify_get_server_caps() 68 | 69 | return glib.ListFromNative(unsafe.Pointer(gcaps)) 70 | } 71 | 72 | func GetServerInfo(name, vendor, version, spec_version *string) bool { 73 | var cname *C.char 74 | var cvendor *C.char 75 | var cversion *C.char 76 | var cspec_version *C.char 77 | 78 | ret := C.notify_get_server_info(&cname, 79 | &cvendor, 80 | &cversion, 81 | &cspec_version) != 0 82 | *name = C.GoString(cname) 83 | *vendor = C.GoString(cvendor) 84 | *version = C.GoString(cversion) 85 | *spec_version = C.GoString(cspec_version) 86 | 87 | return ret 88 | } 89 | 90 | func NotificationNew(title, text, image string) *NotifyNotification { 91 | ptitle := C.CString(title) 92 | ptext := C.CString(text) 93 | pimage := C.CString(image) 94 | ntext := C.g_utf8_normalize((*C.gchar)(ptext), -1, C.G_NORMALIZE_DEFAULT) 95 | defer func() { 96 | C.free(unsafe.Pointer(ptitle)) 97 | C.free(unsafe.Pointer(ptext)) 98 | C.free(unsafe.Pointer(pimage)) 99 | 100 | if ntext != nil { 101 | C.free(unsafe.Pointer(ntext)) 102 | } 103 | }() 104 | 105 | return new_notify_notification(C.notify_notification_new(ptitle, (*C.char)(ntext), pimage)) 106 | } 107 | 108 | func NotificationUpdate(notif *NotifyNotification, summary, body, icon string) bool { 109 | psummary := C.CString(summary) 110 | pbody := C.CString(body) 111 | picon := C.CString(icon) 112 | defer func() { 113 | C.free(unsafe.Pointer(psummary)) 114 | C.free(unsafe.Pointer(pbody)) 115 | C.free(unsafe.Pointer(picon)) 116 | }() 117 | 118 | return C.notify_notification_update(notif._notification, psummary, pbody, picon) != 0 119 | } 120 | 121 | func NotificationShow(notif *NotifyNotification) *glib.Error { 122 | var err *C.GError 123 | C.notify_notification_show(notif._notification, &err) 124 | 125 | return glib.ErrorFromNative(unsafe.Pointer(err)) 126 | } 127 | 128 | func NotificationSetTimeout(notif *NotifyNotification, timeout int32) { 129 | C.notify_notification_set_timeout(notif._notification, C.gint(timeout)) 130 | } 131 | 132 | func NotificationSetCategory(notif *NotifyNotification, category string) { 133 | pcategory := C.CString(category) 134 | defer C.free(unsafe.Pointer(pcategory)) 135 | 136 | C.notify_notification_set_category(notif._notification, pcategory) 137 | } 138 | 139 | func NotificationSetUrgency(notif *NotifyNotification, urgency NotifyUrgency) { 140 | C.notify_notification_set_urgency(notif._notification, C.NotifyUrgency(urgency)) 141 | } 142 | 143 | func NotificationSetHintInt32(notif *NotifyNotification, key string, value int32) { 144 | pkey := C.CString(key) 145 | defer C.free(unsafe.Pointer(pkey)) 146 | 147 | C.notify_notification_set_hint_int32(notif._notification, pkey, C.gint(value)) 148 | } 149 | 150 | func NotificationSetHintDouble(notif *NotifyNotification, key string, value float64) { 151 | pkey := C.CString(key) 152 | defer C.free(unsafe.Pointer(pkey)) 153 | 154 | C.notify_notification_set_hint_double(notif._notification, pkey, C.gdouble(value)) 155 | } 156 | 157 | func NotificationSetHintString(notif *NotifyNotification, key string, value string) { 158 | pkey := C.CString(key) 159 | pvalue := C.CString(value) 160 | defer func() { 161 | C.free(unsafe.Pointer(pkey)) 162 | C.free(unsafe.Pointer(pvalue)) 163 | }() 164 | 165 | C.notify_notification_set_hint_string(notif._notification, pkey, pvalue) 166 | } 167 | 168 | func NotificationSetHintByte(notif *NotifyNotification, key string, value byte) { 169 | pkey := C.CString(key) 170 | defer C.free(unsafe.Pointer(pkey)) 171 | 172 | C.notify_notification_set_hint_byte(notif._notification, pkey, C.guchar(value)) 173 | } 174 | 175 | // FIXME: implement 176 | func NotificationSetHintByteArray(notif *NotifyNotification, key string, value []byte, len uint32) { 177 | pkey := C.CString(key) 178 | defer C.free(unsafe.Pointer(pkey)) 179 | 180 | // C.notify_notification_set_hint_byte_array(notif._notification, pkey, (*C.guchar)(value), C.gsize(len)) 181 | } 182 | 183 | func NotificationSetHint(notif *NotifyNotification, key string, value interface {}) { 184 | switch value.(type) { 185 | case int32: NotificationSetHintInt32(notif, key, value.(int32)) 186 | case float64: NotificationSetHintDouble(notif, key, value.(float64)) 187 | case string: NotificationSetHintString(notif, key, value.(string)) 188 | case byte: NotificationSetHintByte(notif, key, value.(byte)) 189 | } 190 | } 191 | 192 | func NotificationClearHints(notif *NotifyNotification) { 193 | C.notify_notification_clear_hints(notif._notification) 194 | } 195 | 196 | // FIXME: the C function is supposed to be allowing the user to pass another function than free 197 | func NotificationAddAction(notif *NotifyNotification, action, label string, callback NotifyActionCallback, user_data interface {}) { 198 | // C.notify_notification_add_action(notif._notification, paction, plabel, (C.NotifyActionCallback)(callback), user_data, C.free) 199 | } 200 | 201 | func NotificationClearActions(notif *NotifyNotification) { 202 | C.notify_notification_clear_actions(notif._notification) 203 | } 204 | 205 | func NotificationClose(notif *NotifyNotification) *glib.Error { 206 | var err *C.GError 207 | 208 | C.notify_notification_close(notif._notification, &err) 209 | 210 | return glib.ErrorFromNative(unsafe.Pointer(err)) 211 | } 212 | 213 | // Member Functions 214 | func (this *NotifyNotification) Update(summary, body, icon string) bool { 215 | return NotificationUpdate(this, summary, body, icon) 216 | } 217 | 218 | func (this *NotifyNotification) Show() *glib.Error { 219 | return NotificationShow(this) 220 | } 221 | 222 | func (this *NotifyNotification) SetTimeout(timeout int32) { 223 | NotificationSetTimeout(this, timeout) 224 | } 225 | 226 | func (this *NotifyNotification) SetCategory(category string) { 227 | NotificationSetCategory(this, category) 228 | } 229 | 230 | func (this *NotifyNotification) SetUrgency(urgency NotifyUrgency) { 231 | NotificationSetUrgency(this, urgency) 232 | } 233 | 234 | func (this *NotifyNotification) SetHintInt32(key string, value int32) { 235 | NotificationSetHintInt32(this, key, value) 236 | } 237 | 238 | func (this *NotifyNotification) SetHintDouble(key string, value float64) { 239 | NotificationSetHintDouble(this, key, value) 240 | } 241 | 242 | func (this *NotifyNotification) SetHintString(key string, value string) { 243 | NotificationSetHintString(this, key, value) 244 | } 245 | 246 | func (this *NotifyNotification) SetHintByte(key string, value byte) { 247 | NotificationSetHintByte(this, key, value) 248 | } 249 | 250 | func (this *NotifyNotification) SetHintByteArray(key string, value []byte, len uint32) { 251 | NotificationSetHintByteArray(this, key, value, len) 252 | } 253 | 254 | func (this *NotifyNotification) SetHint(key string, value interface {}) { 255 | NotificationSetHint(this, key, value) 256 | } 257 | 258 | func (this *NotifyNotification) ClearHints() { 259 | NotificationClearHints(this) 260 | } 261 | 262 | func (this *NotifyNotification) AddAction(action, label string, callback NotifyActionCallback, user_data interface {}) { 263 | NotificationAddAction(this, action, label, callback, user_data) 264 | } 265 | 266 | func (this *NotifyNotification) ClearActions() { 267 | NotificationClearActions(this) 268 | } 269 | 270 | func (this *NotifyNotification) Close() *glib.Error { 271 | return NotificationClose(this) 272 | } 273 | --------------------------------------------------------------------------------