From 9af56405d1d74adda6ff069637f79a9cdde98578 Mon Sep 17 00:00:00 2001 From: GNUser Date: Sun, 20 Oct 2019 11:16:00 -0400 Subject: Allow noninteractive icon (#4) --- README.md | 21 +++++++++++++-------- mktrayicon.c | 54 +++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index db15ad8..c84a99c 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,12 @@ `mktrayicon` is a simple proxy program that lets you create and modify system tray icons without having to deal with a graphical toolkit like GTK. -`mktrayicon` expects to be given a name pipe (FIFO) file path when it is -started, and you control your status icon by writing to this named pipe. *Note -that the pipe should already be created before you call `mktrayicon`*. +`mktrayicon` can be used two ways: To create an icon that is controlled by a +named pipe or, more simply, to create a non-interactive icon. + +If a FIFO is not provided, mktrayicon will run until killed (e.g., `pkill -f +'mktrayicon.*'`). If you are using a named pipe (FIFO) to control the +icon, *the pipe should already be created before you call `mktrayicon`*. Every line written to the pipe should contain a single letter specifying what operation to perform, optionally followed by a space and a parameter to the @@ -13,10 +16,12 @@ command. Each command should be terminated by a newline. The following commands are supported: - `q`: Terminate `mktrayicon` and remove the tray icon - - `i `: Set the graphic to use for the tray icon; it can be a stock icon name (see `/usr/share/icons`) or path to a custom icon + - `i `: Set the graphic to use for the tray icon; it can be a stock icon + name (see `/usr/share/icons`) or path to a custom icon - `t `: Set the text to display in the icon tooltip - `t`: Remove the icon tooltip - - `c `: Set the command to be execute when the user clicks the icon (`cmnd` is passed to `/bin/sh -c`) + - `c `: Set the command to be execute when the user clicks the icon + (`cmnd` is passed to `/bin/sh -c`) - `c`: Remove the click handler - `h`: Hide the tray icon - `s`: Show the tray icon @@ -24,9 +29,9 @@ are supported: By default, the `none` tooltip icon is used. To change this, pass `-i ` or `-i ` when running `mktrayicon`. -Note that any script communicating with `mktrayicon` **must**, for the time -being, send `q` when they are done. Just removing the FIFO file will **not** -cause the tray icon to be removed. +Note that any script communicating with `mktrayicon` via the pipe **must**, for +the time being, send `q` when they are done. Just removing the FIFO file will +**not** cause the tray icon to be removed. ## Why? diff --git a/mktrayicon.c b/mktrayicon.c index d793c89..4ac4779 100644 --- a/mktrayicon.c +++ b/mktrayicon.c @@ -10,6 +10,7 @@ #include #include #include +#include GtkStatusIcon *icon; char *onclick = NULL; @@ -55,12 +56,10 @@ gboolean set_icon(gpointer data) #ifdef DEBUG printf("Setting icon to '%s'\n", p); #endif - if (strchr(p, '/')) - { + if (strchr(p, '/')) { gtk_status_icon_set_from_file(icon, p); } - else - { + else { gtk_status_icon_set_from_icon_name(icon, p); } free(data); @@ -196,12 +195,10 @@ static GtkStatusIcon *create_tray_icon(char *start_icon) { GtkStatusIcon *tray_icon; - if (strchr(start_icon, '/')) - { + if (strchr(start_icon, '/')) { tray_icon = gtk_status_icon_new_from_file(start_icon); } - else - { + else { tray_icon = gtk_status_icon_new_from_icon_name(start_icon); } g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(tray_icon_on_click), NULL); @@ -214,28 +211,55 @@ static GtkStatusIcon *create_tray_icon(char *start_icon) int main(int argc, char **argv) { char *start_icon = "none"; + char *tooltip = NULL; + char *pipe = NULL; FILE *fifo; GThread *reader; XInitThreads(); /* see http://stackoverflow.com/a/18690540/472927 */ gtk_init(&argc, &argv); - if (argc == 4 && strcmp(argv[1], "-i") == 0) { - start_icon = argv[2]; - } - if (argc == 1) { - printf("Usage: %s [-i ICON] FIFO\n", *argv); - printf("Listen to FIFO for system tray icon specifications\n"); + printf("Usage: %s [-i ICON] [-t TOOLTIP] [FIFO]\n", *argv); + printf("Create a system tray icon as specified\n"); printf("\n"); printf(" -i ICON\tUse the specified ICON when initializing\n"); + printf(" -t TOOLTIP\tUse the specified TOOLTIP when initializing\n"); printf("\n"); + printf("If a FIFO is not provided, mktrayicon will run until killed\n"); printf("Report bugs at https://github.com/jonhoo/mktrayicon\n"); return 0; } + int c; + while ((c = getopt (argc, argv, "i:t:")) != -1) + switch (c) + { + case 'i': + start_icon = optarg; + break; + case 't': + tooltip = optarg; + break; + case '?': + fprintf(stderr, "Unknown option: %c\n", optopt); + return 1; + } + icon = create_tray_icon(start_icon); - reader = g_thread_new("watch_fifo", watch_fifo, argv[argc-1]); + + if (tooltip) { + gtk_status_icon_set_tooltip_text(icon, tooltip); + } + + /* optind holds the index of the next argument to be parsed */ + /* getopt moved positional arguments (if there were any) to the end of the argv array, without parsing them */ + /* so if there were only non-positional arguments, all arguments have been parsed and optind will be equal to argc */ + if (optind < argc) { + pipe = argv[optind]; + reader = g_thread_new("watch_fifo", watch_fifo, pipe); + } + gtk_main(); return 0; } -- cgit