added daemonization, output possibility to console (for other programs) and moved the code to set WM_NAME to its own function as it is needed two times

This commit is contained in:
Aaron Marcher 2016-09-16 23:31:24 +02:00 committed by Aaron Marcher (drkhsh)
parent 887b9bd3e3
commit 87eae6db03
2 changed files with 105 additions and 6 deletions

55
arg.h Normal file
View File

@ -0,0 +1,55 @@
/*
* Copy me if you can.
* by 20h
*/
#ifndef __ARG_H__
#define __ARG_H__
extern char *argv0;
#define USED(x) ((void)(x))
/* use main(int argc, char *argv[]) */
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
argv[0] && argv[0][1]\
&& argv[0][0] == '-';\
argc--, argv++) {\
char _argc;\
char **_argv;\
int brk;\
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
argv++;\
argc--;\
break;\
}\
for (brk = 0, argv[0]++, _argv = argv;\
argv[0][0] && !brk;\
argv[0]++) {\
if (_argv != argv)\
break;\
_argc = argv[0][0];\
switch (_argc)
#define ARGEND }\
USED(_argc);\
}\
USED(argv);\
USED(argc);
#define ARGC() _argc
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
((x), abort(), (char *)0) :\
(brk = 1, (argv[0][1] != '\0')?\
(&argv[0][1]) :\
(argc--, argv++, argv[0])))
#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
(char *)0 :\
(brk = 1, (argv[0][1] != '\0')?\
(&argv[0][1]) :\
(argc--, argv++, argv[0])))
#endif

View File

@ -26,6 +26,7 @@
#undef strlcat #undef strlcat
#undef strlcpy #undef strlcpy
#include "arg.h"
#include "strlcat.h" #include "strlcat.h"
#include "strlcpy.h" #include "strlcpy.h"
#include "concat.h" #include "concat.h"
@ -64,9 +65,12 @@ static char *username(void);
static char *vol_perc(const char *); static char *vol_perc(const char *);
static char *wifi_perc(const char *); static char *wifi_perc(const char *);
static char *wifi_essid(const char *); static char *wifi_essid(const char *);
static void set_status(const char *);
static void sighandler(const int); static void sighandler(const int);
static void usage(void);
static unsigned short int delay, done; char *argv0;
static unsigned short int delay, done, dflag, oflag;
static Display *dpy; static Display *dpy;
#include "config.h" #include "config.h"
@ -579,6 +583,13 @@ wifi_essid(const char *wificard)
return smprintf("%s", (char *)wreq.u.essid.pointer); return smprintf("%s", (char *)wreq.u.essid.pointer);
} }
static void
set_status(const char *str)
{
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
static void static void
sighandler(const int signo) sighandler(const int signo)
{ {
@ -586,8 +597,20 @@ sighandler(const int signo)
done = 1; done = 1;
} }
static void
usage(void)
{
fprintf(stderr,
"slstatus (c) 2016, drkhsh\n"
"usage: %s [-dho]\n",
argv0);
exit(1);
}
int int
main(void) main(int argc, char *argv[])
{ {
unsigned short int i; unsigned short int i;
char status_string[4096]; char status_string[4096];
@ -595,6 +618,22 @@ main(void)
struct arg argument; struct arg argument;
struct sigaction act; struct sigaction act;
ARGBEGIN {
case 'd':
dflag = 1;
break;
case 'o':
oflag = 1;
break;
default:
usage();
} ARGEND
if (dflag && oflag)
usage();
if (dflag)
(void)daemon(1, 1);
memset(&act, 0, sizeof(act)); memset(&act, 0, sizeof(act));
act.sa_handler = sighandler; act.sa_handler = sighandler;
sigaction(SIGINT, &act, 0); sigaction(SIGINT, &act, 0);
@ -604,6 +643,7 @@ main(void)
while (!done) { while (!done) {
status_string[0] = '\0'; status_string[0] = '\0';
for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
argument = args[i]; argument = args[i];
if (argument.args == NULL) if (argument.args == NULL)
@ -619,8 +659,12 @@ main(void)
free(res); free(res);
free(element); free(element);
} }
XStoreName(dpy, DefaultRootWindow(dpy), status_string);
XSync(dpy, False); if (!oflag)
set_status(status_string);
else
printf("%s\n", status_string);
/* /*
* subtract delay time spend in function * subtract delay time spend in function
* calls from the actual global delay time * calls from the actual global delay time
@ -629,8 +673,8 @@ main(void)
delay = 0; delay = 0;
} }
XStoreName(dpy, DefaultRootWindow(dpy), NULL); if (!oflag)
XSync(dpy, False); set_status(NULL);
XCloseDisplay(dpy); XCloseDisplay(dpy);