aboutsummaryrefslogtreecommitdiff
path: root/experimental/keyboard-leds-trayicons.c
diff options
context:
space:
mode:
Diffstat (limited to 'experimental/keyboard-leds-trayicons.c')
-rw-r--r--experimental/keyboard-leds-trayicons.c167
1 files changed, 71 insertions, 96 deletions
diff --git a/experimental/keyboard-leds-trayicons.c b/experimental/keyboard-leds-trayicons.c
index 3dea691..4608b18 100644
--- a/experimental/keyboard-leds-trayicons.c
+++ b/experimental/keyboard-leds-trayicons.c
@@ -46,6 +46,21 @@
#include <X11/Xlib.h>
#include <signal.h>
+int debug = 0;
+Display* dpy;
+typedef struct {
+ const char* caps_on;
+ const char* caps_off;
+ const char* num_on;
+ const char* num_off;
+ int sleep_microseconds;
+ const char* caps_fifo;
+ const char* num_fifo;
+} configuration;
+char* conffile = "none";
+char msg[1000];
+int closed_icons = 0;
+
/* START inclusion of mktrayicon.c, 536 lines */
/*
* This function is made because it's needed to escape the '\' character
@@ -105,13 +120,11 @@ void tray_icon_on_middleclick(GtkStatusIcon *status_icon, GdkEventButton *event,
gpointer user_data) {
if (2 == event->button) {
if (onmiddleclick == NULL) {
-#ifdef DEBUG
+ if(debug)
printf("middleclick, but no command specified\n");
-#endif
} else {
-#ifdef DEBUG
+ if(debug)
printf("middleclick\n");
-#endif
if (onmiddleclick != NULL && fork() == 0) {
execl("/bin/sh", "sh", "-c", onmiddleclick, (char *)NULL);
}
@@ -134,9 +147,8 @@ void click_menu_item(GtkMenuItem *menuitem, gpointer user_data) {
void tray_icon_on_menu(GtkStatusIcon *status_icon, guint button,
guint activate_time, gpointer user_data) {
-#ifdef DEBUG
- printf("Popup menu\n");
-#endif
+ if(debug)
+ printf("Popup menu\n");
if (menusize) {
gtk_menu_popup_at_pointer((GtkMenu *)menu, NULL);
}
@@ -158,25 +170,22 @@ void tray_icon_on_scroll(GtkStatusIcon *status_icon, GdkEventScroll *event,
break;
}
if (i != NULL) {
-#ifdef DEBUG
+ if(debug)
printf("scroll %s\n",i);
-#endif
}
}
gboolean set_tooltip(gpointer data) {
char *p = (char *)data;
if (*p == '\0') {
-#ifdef DEBUG
+ if(debug)
printf("Removing tooltip\n");
-#endif
gtk_status_icon_set_has_tooltip(icon, FALSE);
return FALSE;
}
-#ifdef DEBUG
- printf("Setting tooltip to '%s'\n", p);
-#endif
+ if(debug)
+ printf("Setting tooltip to '%s'\n", p);
gtk_status_icon_set_tooltip_text(icon, p);
free(data);
return FALSE;
@@ -184,9 +193,8 @@ gboolean set_tooltip(gpointer data) {
gboolean set_icon(gpointer data) {
char *p = (char *)data;
-#ifdef DEBUG
- printf("Setting icon to '%s'\n", p);
-#endif
+ if(debug)
+ printf("Setting icon to '%s'\n", p);
if (strchr(p, '/')) {
gtk_status_icon_set_from_file(icon, p);
} else {
@@ -372,17 +380,15 @@ outer:
}
if (param != NULL && *param == '\0') {
-#ifdef DEBUG
- printf("Removing onclick handler\n");
-#endif
+ if(debug)
+ printf("Removing onclick handler\n");
free(param);
break;
}
onclick = param;
-#ifdef DEBUG
- printf("Setting onclick handler to '%s'\n", onclick);
-#endif
+ if(debug)
+ printf("Setting onclick handler to '%s'\n", onclick);
break;
case 'm': /* menu */
if (onmenu != NULL) {
@@ -404,9 +410,8 @@ outer:
if (!param) {
break;
} else if (*param == '\0') {
-#ifdef DEBUG
- printf("Removing onmenu handler\n");
-#endif
+ if(debug)
+ printf("Removing onmenu handler\n");
free(param);
break;
}
@@ -490,14 +495,12 @@ outer:
free(onscrollup);
}
if (!param || (*param == '\0')) {
-#ifdef DEBUG
- printf("Removing scrollup command\n");
+ if(debug)
+ printf("Removing scrollup command\n");
onscrollup = NULL;
-#endif
} else {
-#ifdef DEBUG
- printf("Setting scrollup command\n");
-#endif
+ if(debug)
+ printf("Setting scrollup command\n");
onscrollup = malloc(strlen(param));
strncpy(onscrollup, param, len + 1);
}
@@ -507,14 +510,12 @@ outer:
free(onscrolldown);
}
if (!param || (*param == '\0')) {
-#ifdef DEBUG
- printf("Removing scrolldown command\n");
+ if(debug)
+ printf("Removing scrolldown command\n");
onscrolldown = NULL;
-#endif
} else {
-#ifdef DEBUG
- printf("Setting scrolldown command\n");
-#endif
+ if(debug)
+ printf("Setting scrolldown command\n");
onscrolldown = malloc(strlen(param));
strncpy(onscrolldown, param, len + 1);
}
@@ -524,14 +525,12 @@ outer:
free(onmiddleclick);
}
if (!param || (*param == '\0')) {
-#ifdef DEBUG
- printf("Removing middle click command\n");
+ if(debug)
+ printf("Removing middle click command\n");
onmiddleclick = NULL;
-#endif
} else {
-#ifdef DEBUG
- printf("Setting middleclick command\n");
-#endif
+ if(debug)
+ printf("Setting middleclick command\n");
onmiddleclick = malloc(strlen(param));
strncpy(onmiddleclick, param, len + 1);
}
@@ -633,8 +632,6 @@ int fmain(int argc, char **argv) {
}
/* STOP inclusion of mktrayicon.c */
-Display* dpy;
-
int get_indicator(Display* dpy, char* indicator) {
// where indicator is one of ["Num Lock", "Caps Lock"]
Atom lockIndicator = XInternAtom(dpy, indicator, False);
@@ -643,20 +640,6 @@ int get_indicator(Display* dpy, char* indicator) {
return st;
}
-typedef struct {
- const char* caps_on;
- const char* caps_off;
- const char* num_on;
- const char* num_off;
- int sleep_microseconds;
- const char* caps_fifo;
- const char* num_fifo;
-} configuration;
-
-// this should be configurable with -c parameter (getopts?)
-char* conffile = "none";
-int debug = 0;
-
// slightly modified from example https://github.com/benhoyt/inih
static int handler(void* user, const char* section, const char* name, const char* value) {
// KLT does not need section information at all.
@@ -675,16 +658,12 @@ static int handler(void* user, const char* section, const char* name, const char
return 1;
}
-char msg[1000];
-
void *send_fifo_message(FILE *stream, const char *message) {
-#ifdef DEBUG
- printf("Msg: %s", message);
-#endif
+ if(debug)
+ printf("Msg: %s", message);
int response = fprintf(stream, message);
-#ifdef DEBUG
- printf("response: %d\n", response);
-#endif
+ if(debug)
+ printf("response: %d\n", response);
fflush(stream);
}
@@ -696,7 +675,6 @@ void *send_fifo_icon_message(FILE *stream, const char *iconfile) {
send_fifo_message(stream, msg);
}
-int closed_icons = 0;
void sig_usr(int signo) {
if(signo == SIGUSR1) {
printf("an icon has exited!\n");
@@ -716,12 +694,8 @@ int fprint_usage(char **argv) {
return 0;
}
-
int main(int argc, char **argv) {
-
- conffile = "none";
-
- // Step 0: parse parameters
+ // Step 1: parse parameters
int c;
while ((c = getopt(argc, argv, "c:dh")) != -1)
switch (c) {
@@ -741,30 +715,30 @@ int main(int argc, char **argv) {
// must reset this for the inner getopt/optint operations to proceed. This is somehow a globally-scoped variable.
optind = 0;
- // Step 1: load config
+ // Step 2: load config
configuration config;
if (ini_parse(conffile, handler, &config) < 0) {
printf("Error: cannot load %s\n",conffile);
return 1;
}
- #ifdef DEBUG
- printf("Config loaded from %s.\n", conffile);
- printf("caps_on=%s\n", config.caps_on);
- printf("caps_off=%s\n", config.caps_off);
- printf("num_on=%s\n", config.num_on);
- printf("num_off=%s\n", config.num_off);
- printf("sleep_us=%d\n", config.sleep_microseconds);
- printf("caps_fifo=%s\n", config.caps_fifo);
- printf("num_fifo=%s\n", config.num_fifo);
- #endif
-
- // Step 2: validate config
+ if(debug) {
+ printf("Config loaded from %s.\n", conffile);
+ printf("caps_on=%s\n", config.caps_on);
+ printf("caps_off=%s\n", config.caps_off);
+ printf("num_on=%s\n", config.num_on);
+ printf("num_off=%s\n", config.num_off);
+ printf("sleep_us=%d\n", config.sleep_microseconds);
+ printf("caps_fifo=%s\n", config.caps_fifo);
+ printf("num_fifo=%s\n", config.num_fifo);
+ }
+
+ // Step 3: validate config
// hardcoded min/max for microseconds, 0 and 10 seconds
if (config.sleep_microseconds < 0 || config.sleep_microseconds >= 10000000) {
printf("Warning: invalid sleep_microseconds: %d\n", config.sleep_microseconds);
};
- // Step 3: initialize variables
+ // Step 4: initialize variables
FILE *stream_N, *stream_C;
dpy = XOpenDisplay( NULL );
int status_capslock = get_indicator(dpy, "Caps Lock");
@@ -780,13 +754,13 @@ int main(int argc, char **argv) {
pid_t pid, pid_C, pid_N;
printf("Capslock: %d\tNumlock: %d\n",status_capslock,status_numlock);
- // Step 4: initiate fifo files
+ // Step 5: initiate fifo files
if (stat(config.caps_fifo, &st_C) != 0)
mkfifo(config.caps_fifo, 0666);
if (stat(config.num_fifo, &st_N) != 0)
mkfifo(config.num_fifo, 0666);
- // Step 5: start child proceses (tray icons)
+ // Step 6: start child proceses (tray icons)
// parent 1
pid = getpid();
if ((pid_C = fork()) == 0) {
@@ -821,10 +795,10 @@ int main(int argc, char **argv) {
strcat(msg, "\n");
send_fifo_message(stream_N, msg);
// still parent 1
- // Step 6: loop monitoring logic
+ // Step 7: loop monitoring logic
int status_caps_old = -1;
int status_num_old = -1;
- int kill_result = 500;
+ int kill_result = -1;
while (closed_icons < 2) {
// (1000000); 1 second
// ( 1000); 1 millisecond
@@ -839,7 +813,8 @@ int main(int argc, char **argv) {
sigaction(SIGUSR1,&san,NULL);
if(status_capslock != status_caps_old) {
kill_result = kill(pid_C, 0);
- printf("Checking for capslock change, pid %d, result %d\n", pid_C, kill_result);
+ // unfortunately, kill does not operate as expected. I expected kill_result <> 0 if the pid is already gone.
+ //printf("Checking for capslock change, pid %d, result %d\n", pid_C, kill_result);
if(0 == status_capslock) {
send_fifo_icon_message(stream_C, config.caps_off);
printf("capslock off\n");
@@ -863,9 +838,9 @@ int main(int argc, char **argv) {
};
}; // end while(1)
// Either CTRL+C was run twice, or both icons closed
- return 15;
+ return 0;
}; // end parent 1 loop
- return 26;
+ return 0;
}; // end parent 1 loop
- return 37;
+ return 0;
}
bgstack15