diff options
-rw-r--r-- | README.md | 21 | ||||
-rw-r--r-- | mktrayicon.c | 54 |
2 files changed, 52 insertions, 23 deletions
@@ -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.*<ICON>'`). 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 <icon>`: 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 <icon>`: 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 <text>`: Set the text to display in the icon tooltip - `t`: Remove the icon tooltip - - `c <cmnd>`: Set the command to be execute when the user clicks the icon (`cmnd` is passed to `/bin/sh -c`) + - `c <cmnd>`: 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 <stock_icon_name>` or `-i <path_to_custom_icon>` 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 <stdlib.h> #include <string.h> #include <sys/stat.h> +#include <unistd.h> 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; } |