You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dunstify.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include <glib.h>
  2. #include <libnotify/notify.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. static gchar *appname = "dunstify";
  7. static gchar *summary = NULL;
  8. static gchar *body = NULL;
  9. static NotifyUrgency urgency = NOTIFY_URGENCY_NORMAL;
  10. static gchar *urgency_str = NULL;
  11. static gchar **hint_strs = NULL;
  12. static gchar **action_strs = NULL;
  13. static gint timeout = NOTIFY_EXPIRES_DEFAULT;
  14. static gchar *icon = NULL;
  15. static gboolean capabilities = false;
  16. static gboolean serverinfo = false;
  17. static gboolean printid = false;
  18. static guint32 replace_id = 0;
  19. static guint32 close_id = 0;
  20. static gboolean block = false;
  21. static GOptionEntry entries[] =
  22. {
  23. { "appname", 'a', 0, G_OPTION_ARG_STRING, &appname, "Name of your application", "NAME" },
  24. { "urgency", 'u', 0, G_OPTION_ARG_STRING, &urgency_str, "The urgency of this notification", "URG" },
  25. { "hints", 'h', 0, G_OPTION_ARG_STRING_ARRAY, &hint_strs, "User specified hints", "HINT" },
  26. { "action", 'A', 0, G_OPTION_ARG_STRING_ARRAY, &action_strs, "Actions the user can invoke", "ACTION" },
  27. { "timeout", 't', 0, G_OPTION_ARG_INT, &timeout, "The time until the notification expires", "TIMEOUT" },
  28. { "icon", 'i', 0, G_OPTION_ARG_STRING, &icon, "An Icon that should be displayed with the notification", "ICON" },
  29. { "capabilities", 'c', 0, G_OPTION_ARG_NONE, &capabilities, "Print the server capabilities and exit", NULL},
  30. { "serverinfo", 's', 0, G_OPTION_ARG_NONE, &serverinfo, "Print server information and exit", NULL},
  31. { "printid", 'p', 0, G_OPTION_ARG_NONE, &printid, "Print id, which can be used to update/replace this notification", NULL},
  32. { "replace", 'r', 0, G_OPTION_ARG_INT, &replace_id, "Set id of this notification.", "ID"},
  33. { "close", 'C', 0, G_OPTION_ARG_INT, &close_id, "Set id of this notification.", "ID"},
  34. { "block", 'b', 0, G_OPTION_ARG_NONE, &block, "Block until notification is closed and print close reason", NULL},
  35. { NULL }
  36. };
  37. void die(int exit_value)
  38. {
  39. if (notify_is_initted())
  40. notify_uninit();
  41. exit(exit_value);
  42. }
  43. void print_capabilities(void)
  44. {
  45. GList *caps = notify_get_server_caps();
  46. for (GList *iter = caps; iter; iter = iter->next) {
  47. if (strlen(iter->data) > 0) {
  48. g_print("%s\n", iter->data);
  49. }
  50. }
  51. }
  52. void print_serverinfo(void)
  53. {
  54. char *name;
  55. char *vendor;
  56. char *version;
  57. char *spec_version;
  58. if (!notify_get_server_info(&name, &vendor, &version, &spec_version)) {
  59. g_printerr("Unable to get server information");
  60. exit(1);
  61. }
  62. g_print("name:%s\nvendor:%s\nversion:%s\nspec_version:%s\n", name,
  63. vendor,
  64. version,
  65. spec_version);
  66. }
  67. void parse_commandline(int argc, char *argv[])
  68. {
  69. GError *error = NULL;
  70. GOptionContext *context;
  71. context = g_option_context_new("- Dunstify");
  72. g_option_context_add_main_entries(context, entries, NULL);
  73. if (!g_option_context_parse(context, &argc, &argv, &error)){
  74. g_printerr("Invalid commandline: %s\n", error->message);
  75. exit(1);
  76. }
  77. g_option_context_free(context);
  78. if (capabilities) {
  79. print_capabilities();
  80. die(0);
  81. }
  82. if (serverinfo) {
  83. print_serverinfo();
  84. die(0);
  85. }
  86. if (argc < 2 && close_id < 1) {
  87. g_printerr("I need at least a summary\n");
  88. die(1);
  89. } else if (argc < 2) {
  90. summary = g_strdup("These are not the summaries you are looking for");
  91. } else {
  92. summary = g_strdup(argv[1]);
  93. }
  94. if (argc > 2) {
  95. body = g_strdup(argv[2]);
  96. }
  97. if (urgency_str) {
  98. switch (urgency_str[0]) {
  99. case 'l':
  100. case 'L':
  101. case '0':
  102. urgency = NOTIFY_URGENCY_LOW;
  103. break;
  104. case 'n':
  105. case 'N':
  106. case '1':
  107. urgency = NOTIFY_URGENCY_NORMAL;
  108. break;
  109. case 'c':
  110. case 'C':
  111. case '2':
  112. urgency = NOTIFY_URGENCY_CRITICAL;
  113. break;
  114. default:
  115. g_printerr("Unknown urgency: %s\n", urgency_str);
  116. g_printerr("Assuming normal urgency\n");
  117. break;
  118. }
  119. }
  120. }
  121. typedef struct _NotifyNotificationPrivate
  122. {
  123. guint32 id;
  124. char *app_name;
  125. char *summary;
  126. char *body;
  127. /* NULL to use icon data. Anything else to have server lookup icon */
  128. char *icon_name;
  129. /*
  130. * -1 = use server default
  131. * 0 = never timeout
  132. * > 0 = Number of milliseconds before we timeout
  133. */
  134. gint timeout;
  135. GSList *actions;
  136. GHashTable *action_map;
  137. GHashTable *hints;
  138. gboolean has_nondefault_actions;
  139. gboolean updates_pending;
  140. gulong proxy_signal_handler;
  141. gint closed_reason;
  142. } knickers;
  143. int get_id(NotifyNotification *n)
  144. {
  145. knickers *kn = n->priv;
  146. /* I'm sorry for taking a peek */
  147. return kn->id;
  148. }
  149. int put_id(NotifyNotification *n, guint32 id)
  150. {
  151. knickers *kn = n->priv;
  152. /* And know I'm putting stuff into
  153. * your knickers. I'm sorry.
  154. * I'm so sorry.
  155. * */
  156. kn->id = id;
  157. }
  158. void actioned(NotifyNotification *n, char *a, gpointer foo)
  159. {
  160. notify_notification_close(n, NULL);
  161. g_print("%s\n", a);
  162. die(0);
  163. }
  164. void closed(NotifyNotification *n, gpointer foo)
  165. {
  166. g_print("%d\n", notify_notification_get_closed_reason(n));
  167. die(0);
  168. }
  169. void add_action(NotifyNotification *n, char *str)
  170. {
  171. char *action = str;
  172. char *label = strstr(str, ",");
  173. if (!label || *(label+1) == '\0') {
  174. g_printerr("Malformed action. Excpected \"action,label\", got \"%s\"", str);
  175. return;
  176. }
  177. *label = '\0';
  178. label++;
  179. notify_notification_add_action(n, action, label, actioned, NULL, NULL);
  180. }
  181. void add_hint(NotifyNotification *n, char *str)
  182. {
  183. char *type = str;
  184. char *name = strstr(str, ":");
  185. if (!name || *(name+1) == '\0') {
  186. g_printerr("Malformed hint. Expected \"type:name:value\", got \"%s\"", str);
  187. return;
  188. }
  189. *name = '\0';
  190. name++;
  191. char *value = strstr(name, ":");
  192. if (!value || *(value+1) == '\0') {
  193. g_printerr("Malformed hint. Expected \"type:name:value\", got \"%s\"", str);
  194. return;
  195. }
  196. *value = '\0';
  197. value++;
  198. if (strcmp(type, "int") == 0)
  199. notify_notification_set_hint_int32(n, name, atoi(value));
  200. else if (strcmp(type, "double") == 0)
  201. notify_notification_set_hint_double(n, name, atof(value));
  202. else if (strcmp(type, "string") == 0)
  203. notify_notification_set_hint_string(n, name, value);
  204. else if (strcmp(type, "byte") == 0) {
  205. gint h_byte = g_ascii_strtoull(value, NULL, 10);
  206. if (h_byte < 0 || h_byte > 0xFF)
  207. g_printerr("Not a byte: \"%s\"", value);
  208. else
  209. notify_notification_set_hint_byte(n, name, (guchar) h_byte);
  210. } else
  211. g_printerr("Malformed hint. Expected a type of int, double, string or byte, got %s\n", type);
  212. }
  213. int main(int argc, char *argv[])
  214. {
  215. #if !GLIB_CHECK_VERSION(2,35,0)
  216. g_type_init();
  217. #endif
  218. parse_commandline(argc, argv);
  219. if (!notify_init(appname)) {
  220. g_printerr("Unable to initialize libnotify\n");
  221. die(1);
  222. }
  223. NotifyNotification *n;
  224. n = notify_notification_new(summary, body, icon);
  225. notify_notification_set_timeout(n, timeout);
  226. notify_notification_set_urgency(n, urgency);
  227. GError *err = NULL;
  228. if (close_id > 0) {
  229. put_id(n, close_id);
  230. notify_notification_close(n, &err);
  231. if (err) {
  232. g_printerr("Unable to close notification: %s\n", err->message);
  233. die(1);
  234. }
  235. die(0);
  236. }
  237. if (replace_id > 0) {
  238. put_id(n, replace_id);
  239. }
  240. GMainLoop *l = NULL;
  241. if (block || action_strs)
  242. l = g_main_loop_new(NULL, false);
  243. g_signal_connect(n, "closed", G_CALLBACK(closed), NULL);
  244. if (action_strs)
  245. for (int i = 0; action_strs[i]; i++) {
  246. add_action(n, action_strs[i]);
  247. }
  248. if (hint_strs)
  249. for (int i = 0; hint_strs[i]; i++) {
  250. add_hint(n, hint_strs[i]);
  251. }
  252. notify_notification_show(n, &err);
  253. if (err) {
  254. g_printerr("Unable to send notification: %s\n", err->message);
  255. die(1);
  256. }
  257. if (block || action_strs)
  258. g_main_loop_run(l);
  259. if (printid) {
  260. g_print("%d\n", get_id(n));
  261. }
  262. die(0);
  263. }