dotfiles

My beautiful configs and dotfiles managed by Nix' home-manager
Log | Files | Refs | README | LICENSE

slstatus-notify.diff (3183B)


      1 diff --git a/config.def.h b/config.def.h
      2 index d805331..c37c675 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -6,6 +6,9 @@ const unsigned int interval = 1000;
      6  /* text to show if no value can be retrieved */
      7  static const char unknown_str[] = "n/a";
      8  
      9 +/* if message is displayed, last max cycles, set to 0 to disable listening */
     10 +const static int maxmsgcycle = 5;
     11 +
     12  /* maximum output string length */
     13  #define MAXLEN 2048
     14  
     15 diff --git a/slstatus.c b/slstatus.c
     16 index fd31313..b9f8784 100644
     17 --- a/slstatus.c
     18 +++ b/slstatus.c
     19 @@ -1,10 +1,13 @@
     20  /* See LICENSE file for copyright and license details. */
     21  #include <errno.h>
     22 +#include <fcntl.h>
     23  #include <signal.h>
     24  #include <stdio.h>
     25  #include <stdlib.h>
     26  #include <string.h>
     27  #include <time.h>
     28 +#include <unistd.h>
     29 +#include <poll.h>
     30  #include <X11/Xlib.h>
     31  
     32  #include "arg.h"
     33 @@ -13,6 +16,7 @@
     34  
     35  struct arg {
     36  	const char *(*func)(const char *);
     37 +	int (*doenable)(const char *);
     38  	const char *fmt;
     39  	const char *args;
     40  };
     41 @@ -21,6 +25,16 @@ char buf[1024];
     42  static volatile sig_atomic_t done;
     43  static Display *dpy;
     44  
     45 +int always(const char *unused) {
     46 +	(void)unused;
     47 +	return 1;
     48 +}
     49 +
     50 +int never(const char *unused) {
     51 +	(void)unused;
     52 +	return 0;
     53 +}
     54 +
     55  #include "config.h"
     56  
     57  static void
     58 @@ -38,10 +52,25 @@ difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
     59  	               (a->tv_nsec < b->tv_nsec) * 1E9;
     60  }
     61  
     62 +static int
     63 +getnotify(int fifo, char* buffer, size_t size)
     64 +{
     65 +	struct pollfd fds = { fifo, POLLIN, 0 };
     66 +
     67 +	if (poll(&fds, 1, 0) <= 0)
     68 +		return 0;
     69 +
     70 +	int len = 0;
     71 +	while (read(fifo, buffer + len, 1) > 0 && buffer[len] != '\n' && len < size)
     72 +		len++;
     73 +	buffer[len] = '\0';
     74 +	return len;
     75 +}
     76 +
     77  static void
     78  usage(void)
     79  {
     80 -	die("usage: %s [-v] [-s] [-1]", argv0);
     81 +	die("usage: %s [-v] [-s] [-1] [-p fifo]", argv0);
     82  }
     83  
     84  int
     85 @@ -51,8 +80,11 @@ main(int argc, char *argv[])
     86  	struct timespec start, current, diff, intspec, wait;
     87  	size_t i, len;
     88  	int sflag, ret;
     89 +	int fifo, msgcycle = 0;
     90  	char status[MAXLEN];
     91  	const char *res;
     92 +	const char *fifopath = NULL;
     93 +	char *nl;
     94  
     95  	sflag = 0;
     96  	ARGBEGIN {
     97 @@ -64,6 +96,9 @@ main(int argc, char *argv[])
     98  	case 's':
     99  		sflag = 1;
    100  		break;
    101 +	case 'p':
    102 +		fifopath = EARGF(usage());
    103 +		break;
    104  	default:
    105  		usage();
    106  	} ARGEND
    107 @@ -78,6 +113,9 @@ main(int argc, char *argv[])
    108  	act.sa_flags |= SA_RESTART;
    109  	sigaction(SIGUSR1, &act, NULL);
    110  
    111 +	if (fifopath && (fifo = open(fifopath, O_RDONLY)) == -1)
    112 +		die("open(fifo): ");
    113 +
    114  	if (!sflag && !(dpy = XOpenDisplay(NULL)))
    115  		die("XOpenDisplay: Failed to open display");
    116  
    117 @@ -85,8 +123,19 @@ main(int argc, char *argv[])
    118  		if (clock_gettime(CLOCK_MONOTONIC, &start) < 0)
    119  			die("clock_gettime:");
    120  
    121 +		if (msgcycle > 0) {
    122 +			msgcycle--;
    123 +		} else if (fifopath && getnotify(fifo, status, sizeof(status)) > 0) {
    124 +			if (!*status)
    125 +				continue;
    126 +		
    127 +			msgcycle = maxmsgcycle;
    128 +		} else {
    129  		status[0] = '\0';
    130  		for (i = len = 0; i < LEN(args); i++) {
    131 +				if (!args[i].doenable(args[i].args))
    132 +					continue;
    133 +
    134  			if (!(res = args[i].func(args[i].args)))
    135  				res = unknown_str;
    136  
    137 @@ -95,6 +144,7 @@ main(int argc, char *argv[])
    138  				break;
    139  
    140  			len += ret;
    141 +			}
    142  		}
    143  
    144  		if (sflag) {