someblocks

modular status bar for dwlb
git clone https://git.awy.one/someblocks
Log | Files | Refs | README | LICENSE

someblocks.c (8825B) - View raw


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#ifdef __OpenBSD__
#define SIGPLUS SIGUSR1 + 1
#define SIGMINUS SIGUSR1 - 1
#else
#define SIGPLUS SIGRTMIN
#define SIGMINUS SIGRTMIN
#endif
#define LENGTH(X) (sizeof (X) / sizeof (X[0]))
#define CMDLENGTH 512
#define MIN(a, b) ((a < b) ? a : b)
#define STATUSLENGTH (LENGTH (blocks) * CMDLENGTH + 1)

typedef struct
{
  char *icon;
  char *command;
  unsigned int interval;
  unsigned int signal;
  char *lclick;
  char *rclick;
  char *mclick;
  char *upscroll;
  char *downscroll;
} Block;
#ifndef __OpenBSD__
void dummysighandler (int num);
#endif
void sighandler (int num);
void getcmds (int time);
void getsigcmds (unsigned int signal);
void setupsignals ();
void sighandler (int signum);
int getstatus (char *str, char *last);
void statusloop ();
void termhandler (int signum);
void pstdout ();
void psomebar ();
static void (*writestatus) () = psomebar;

#include "blocks.h"

static char statusbar[LENGTH (blocks)][CMDLENGTH] = { 0 };
static char statusstr[2][STATUSLENGTH];
static int statusContinue = 1;
static int returnStatus = 0;
static char somebarPath[128];
static int somebarFd = -1;

// opens process *cmd and stores output in *output
void
getcmd (const Block *block, char *output)
{
  char tmp[CMDLENGTH] = { 0 };

  // Run module command
  FILE *cmdf = popen (block->command, "r");
  if (!cmdf)
    {
      output[0] = '\0';
      return;
    }

  fgets (tmp, sizeof (tmp) - delimLen, cmdf);
  pclose (cmdf);

  int len = strlen (tmp);
  if (len && tmp[len - 1] == '\n')
    tmp[len - 1] = '\0';

  // Skip output if module prints nothing and has no icon
  if (len == 0 && (!block->icon || block->icon[0] == '\0'))
    {
      output[0] = '\0';
      return;
    }

  // Start and end mouse tags
  char start_tags[512] = "";
  char end_tags[128] = "";

  if (block->lclick && block->lclick[0] != '\0')
    {
      strncat (start_tags, "^lm(sh -c '",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, block->lclick,
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, "')",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (end_tags, "^lm()", sizeof (end_tags) - strlen (end_tags) - 1);
    }

  if (block->rclick && block->rclick[0] != '\0')
    {
      strncat (start_tags, "^rm(sh -c '",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, block->rclick,
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, "')",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (end_tags, "^rm()", sizeof (end_tags) - strlen (end_tags) - 1);
    }

  if (block->mclick && block->mclick[0] != '\0')
    {
      strncat (start_tags, "^mm(sh -c '",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, block->mclick,
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, "')",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (end_tags, "^mm()", sizeof (end_tags) - strlen (end_tags) - 1);
    }

  if (block->upscroll && block->upscroll[0] != '\0')
    {
      strncat (start_tags, "^us(sh -c '",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, block->upscroll,
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, "')",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (end_tags, "^us()", sizeof (end_tags) - strlen (end_tags) - 1);
    }

  if (block->downscroll && block->downscroll[0] != '\0')
    {
      strncat (start_tags, "^ds(sh -c '",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, block->downscroll,
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (start_tags, "')",
               sizeof (start_tags) - strlen (start_tags) - 1);
      strncat (end_tags, "^ds()", sizeof (end_tags) - strlen (end_tags) - 1);
    }

  // Compose final output
  snprintf (output, CMDLENGTH, "%s%s%s%s", start_tags,
            block->icon ? block->icon : "", tmp, end_tags);

  // Add delimiter only if module produced visible output
  if (strlen (output) > 0)
    strncat (output, delim, CMDLENGTH - strlen (output) - 1);
  else
    output[0] = '\0';
}

void
getcmds (int time)
{
  const Block *current;
  for (unsigned int i = 0; i < LENGTH (blocks); i++)
    {
      current = blocks + i;
      if ((current->interval != 0 && time % current->interval == 0)
          || time == -1)
        getcmd (current, statusbar[i]);
    }
}

void
getsigcmds (unsigned int signal)
{
  const Block *current;
  for (unsigned int i = 0; i < LENGTH (blocks); i++)
    {
      current = blocks + i;
      if (current->signal == signal)
        getcmd (current, statusbar[i]);
    }
}

void
setupsignals ()
{
  struct sigaction sa = { 0 };
#ifndef __OpenBSD__
  /* initialize all real time signals with dummy handler */
  sa.sa_handler = dummysighandler;
  for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
    sigaction (i, &sa, NULL);
#endif

  sa.sa_handler = sighandler;
  for (unsigned int i = 0; i < LENGTH (blocks); i++)
    {
      if (blocks[i].signal > 0)
        sigaction (SIGMINUS + blocks[i].signal, &sa, NULL);
    }
}

int
getstatus (char *str, char *last)
{
  strcpy (last, str);
  str[0] = '\0';
  for (unsigned int i = 0; i < LENGTH (blocks); i++)
    strcat (str, statusbar[i]);
  str[strlen (str) - strlen (delim)] = '\0';
  return strcmp (str, last); // 0 if they are the same
}

void
pstdout ()
{
  if (!getstatus (statusstr[0],
                  statusstr[1])) // Only write out if text has changed.
    return;
  printf ("%s\n", statusstr[0]);
  fflush (stdout);
}

void
psomebar ()
{
  if (!getstatus (statusstr[0],
                  statusstr[1])) // Only write out if text has changed.
    return;
  if (somebarFd < 0)
    {
      somebarFd = open (somebarPath, O_WRONLY | O_CLOEXEC);
      if (somebarFd < 0 && errno == ENOENT)
        {
          // assume somebar is not ready yet
          sleep (1);
          somebarFd = open (somebarPath, O_WRONLY | O_CLOEXEC);
        }
      if (somebarFd < 0)
        {
          perror ("open");
          return;
        }
    }
  dprintf (somebarFd, "status %s\n", statusstr[0]);
}

void
pdwlb ()
{
  int dwlbSock = -1;
  struct sockaddr_un sockaddr;

  if (!getstatus (statusstr[0],
                  statusstr[1])) // Only write out if text has changed.
    return;

  if ((dwlbSock = socket (AF_UNIX, SOCK_STREAM, 1)) == -1)
    {
      perror ("socket");
      return;
    }
  snprintf (sockaddr.sun_path, sizeof (sockaddr.sun_path), "%s", somebarPath);
  sockaddr.sun_family = AF_UNIX;

  /* Try a couple times dwlb may not be ready */
  for (int i = 0; i < 5; i++)
    {
      if (connect (dwlbSock, (struct sockaddr *)&sockaddr,
                   sizeof (struct sockaddr_un))
          == -1)
        {
          sleep (1);
          continue;
        }
      break;
    }
  dprintf (dwlbSock, "all status %s", statusstr[0]);
  close (dwlbSock);
}

void
statusloop ()
{
  setupsignals ();
  int i = 0;
  getcmds (-1);
  while (1)
    {
      getcmds (i++);
      writestatus ();
      if (!statusContinue)
        break;
      sleep (1.0);
    }
}

#ifndef __OpenBSD__
/* this signal handler should do nothing */
void
dummysighandler (int signum)
{
  return;
}
#endif

void
sighandler (int signum)
{
  getsigcmds (signum - SIGPLUS);
  writestatus ();
}

void
termhandler (int signum)
{
  statusContinue = 0;
}

void
sigpipehandler (int signum)
{
  close (somebarFd);
  somebarFd = -1;
}

int
main (int argc, char **argv)
{
  for (int i = 0; i < argc; i++)
    { // Handle command line arguments
      if (!strcmp ("-d", argv[i]))
        {
          strncpy (delim, argv[++i], delimLen);
        }
      else if (!strcmp ("-p", argv[i]))
        {
          writestatus = pstdout;
        }
      else if (!strcmp ("-b", argv[i]))
        {
          writestatus = pdwlb;
          strcpy (somebarPath, getenv ("XDG_RUNTIME_DIR"));
          strcat (somebarPath, "/dwlb/dwlb-0");
        }
      else if (!strcmp ("-s", argv[i]))
        {
          strcpy (somebarPath, argv[++i]);
        }
    }

  if (!strlen (somebarPath))
    {
      strcpy (somebarPath, getenv ("XDG_RUNTIME_DIR"));
      strcat (somebarPath, "/somebar-0");
    }

  delimLen = MIN (delimLen, strlen (delim));
  delim[delimLen++] = '\0';
  signal (SIGTERM, termhandler);
  signal (SIGINT, termhandler);
  signal (SIGPIPE, sigpipehandler);
  statusloop ();
  return 0;
}