From 2c9bb4fe87ad9d2bf355140c9133a0eb658d7c6a Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Fri, 30 Sep 2022 19:35:50 -0400 Subject: improvements from rwp code review --- experimental/keyboard-leds-trayicons.c | 41 ++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/experimental/keyboard-leds-trayicons.c b/experimental/keyboard-leds-trayicons.c index f2c6a4a..676849e 100644 --- a/experimental/keyboard-leds-trayicons.c +++ b/experimental/keyboard-leds-trayicons.c @@ -1,7 +1,7 @@ /* * File: keyboard-leds-trayicons.c * Location: https://bgstack15.ddns.net/cgit/keyboard-leds-trayicons/ - * Author: bgstack15 + * Authors: bgstack15, jonhoo * Startdate: 2022-09-28-4 13:30 * SPDX-License-Identifier: GPL-3.0 * Title: Proof of Concept C utility for polling capslock and numlock @@ -20,6 +20,8 @@ * https://qnaplus.com/c-program-to-sleep-in-milliseconds/ * https://stackoverflow.com/questions/47107311/sending-signal-from-child-to-parent * https://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly + * https://cboard.cprogramming.com/c-programming/150795-signal-sigaction-conversion-printable-thread.html + * http://maemo.cloud-7.de/irclogs/freenode/_devuan-dev/_devuan-dev.2022-10-01.log.html * Improvements: * Use getopts, -h, -c conffile, -d (debug) * fix all return values at end @@ -30,19 +32,19 @@ * exit when both trayicons have exited */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* START inclusion of mktrayicon.c, 536 lines */ /* @@ -768,13 +770,12 @@ int main(int argc, char **argv) { } else { // parent 1 - signal(SIGPIPE, SIG_IGN); /* prevents breakage when C or N tray icon is closed. However, now this daemon will never quit without the closed_icon counter! */ - fd_C = open(config.caps_fifo, O_WRONLY); + fd_C = open(config.caps_fifo, O_RDWR); stream_C = fdopen(fd_C,"w"); - fd_N = open(config.num_fifo, O_WRONLY); + fd_N = open(config.num_fifo, O_RDWR); stream_N = fdopen(fd_N,"w"); strcpy(msg, "m quit,echo 'q' > "); strcat(msg, config.caps_fifo); @@ -797,8 +798,10 @@ int main(int argc, char **argv) { status_capslock = get_indicator(dpy, "Caps Lock"); status_numlock = get_indicator(dpy, "Num Lock"); //printf("Capslock: %d\tNumlock: %d\n",status_capslock,status_numlock); - if(signal(SIGUSR1,sig_usr) == SIG_ERR) - printf("Signal processed."); + struct sigaction san; + memset(&san,0,sizeof(san)); + san.sa_handler = sig_usr; + 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); -- cgit