Compare commits
8 Commits
d58dfbb322
...
7dd9a3bddc
Author | SHA1 | Date | |
---|---|---|---|
|
7dd9a3bddc | ||
|
6a97f4d0c5 | ||
|
647080cbed | ||
|
098184d0c3 | ||
|
f1c037037d | ||
|
39dc44cf89 | ||
|
be46e8a8bd | ||
|
cdc992cc41 |
2
.clang-format
Normal file
2
.clang-format
Normal file
@ -0,0 +1,2 @@
|
||||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
24
Makefile
24
Makefile
@ -1,19 +1,21 @@
|
||||
.POSIX:
|
||||
|
||||
PREFIX = /usr/local
|
||||
CC = gcc
|
||||
CFLAGS = -Ofast
|
||||
LDLIBS = -lX11
|
||||
|
||||
BIN = dwmblocks
|
||||
|
||||
$(BIN): main.o
|
||||
$(CC) $^ -o $@ $(LDLIBS)
|
||||
|
||||
dwmblocks: main.o
|
||||
$(CC) main.o -lX11 -Ofast -o dwmblocks
|
||||
main.o: main.c config.h
|
||||
$(CC) -Ofast -c main.c
|
||||
clean:
|
||||
rm -f *.o *.gch dwmblocks
|
||||
install: dwmblocks
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin
|
||||
cp -f dwmblocks $(DESTDIR)$(PREFIX)/bin
|
||||
chmod 755 $(DESTDIR)$(PREFIX)/bin/dwmblocks
|
||||
$(RM) *.o $(BIN)
|
||||
|
||||
install: $(BIN)
|
||||
install -D -m 755 $(BIN) $(DESTDIR)$(PREFIX)/bin/$(BIN)
|
||||
|
||||
uninstall:
|
||||
rm -f $(DESTDIR)$(PREFIX)/bin/dwmblocks
|
||||
$(RM) $(DESTDIR)$(PREFIX)/bin/$(BIN)
|
||||
|
||||
.PHONY: clean install uninstall
|
||||
|
@ -125,8 +125,6 @@ To use this feature, define the `CLICKABLE_BLOCKS` feature macro in your `config
|
||||
|
||||
Apart from that, you need `dwm` to be patched with [statuscmd](https://dwm.suckless.org/patches/statuscmd/).
|
||||
|
||||
> Earlier, `dwmblocks-async` used to require a patch to be applied to `dwm`. However, the code has been redone so there's no need to apply that patch anymore.
|
||||
|
||||
|
||||
## Credits
|
||||
This work would not have been possible without [Luke's build of dwmblocks](https://github.com/LukeSmithxyz/dwmblocks) and [Daniel Bylinka's statuscmd patch](https://dwm.suckless.org/patches/statuscmd/).
|
||||
|
87
main.c
87
main.c
@ -15,7 +15,7 @@
|
||||
{ "echo \"$(" cmd ")\"", interval, signal }
|
||||
|
||||
typedef const struct {
|
||||
const char* command;
|
||||
const char *command;
|
||||
const unsigned int interval;
|
||||
const unsigned int signal;
|
||||
} Block;
|
||||
@ -35,7 +35,7 @@ typedef const struct {
|
||||
#define LEADING_DELIMITER 0
|
||||
#endif
|
||||
|
||||
static Display* dpy;
|
||||
static Display *dpy;
|
||||
static int screen;
|
||||
static Window root;
|
||||
static unsigned short statusContinue = 1;
|
||||
@ -48,7 +48,10 @@ static int execLock = 0;
|
||||
|
||||
// Longest UTF-8 character is 4 bytes long
|
||||
static char outputs[LEN(blocks)][CMDLENGTH * 4 + 1 + CLICKABLE_BLOCKS];
|
||||
static char statusBar[2][LEN(blocks) * (LEN(outputs[0]) - 1) + (LEN(blocks) - 1 + LEADING_DELIMITER) * (LEN(DELIMITER) - 1) + 1];
|
||||
static char
|
||||
statusBar[2]
|
||||
[LEN(blocks) * (LEN(outputs[0]) - 1) +
|
||||
(LEN(blocks) - 1 + LEADING_DELIMITER) * (LEN(DELIMITER) - 1) + 1];
|
||||
|
||||
void (*writeStatus)();
|
||||
|
||||
@ -62,15 +65,14 @@ int gcd(int a, int b) {
|
||||
return a;
|
||||
}
|
||||
|
||||
void closePipe(int* pipe) {
|
||||
void closePipe(int *pipe) {
|
||||
close(pipe[0]);
|
||||
close(pipe[1]);
|
||||
}
|
||||
|
||||
void execBlock(int i, const char* button) {
|
||||
void execBlock(int i, const char *button) {
|
||||
// Ensure only one child process exists per block at an instance
|
||||
if (execLock & 1 << i)
|
||||
return;
|
||||
if (execLock & 1 << i) return;
|
||||
// Lock execution of block until current instance finishes execution
|
||||
execLock |= 1 << i;
|
||||
|
||||
@ -79,20 +81,20 @@ void execBlock(int i, const char* button) {
|
||||
dup2(pipes[i][1], STDOUT_FILENO);
|
||||
close(pipes[i][1]);
|
||||
|
||||
if (button)
|
||||
setenv("BLOCK_BUTTON", button, 1);
|
||||
execl("/bin/sh", "sh", "-c", blocks[i].command, (char*)NULL);
|
||||
_exit(1);
|
||||
if (button) setenv("BLOCK_BUTTON", button, 1);
|
||||
execl("/bin/sh", "sh", "-c", blocks[i].command, (char *)NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void execBlocks(unsigned int time) {
|
||||
for (int i = 0; i < LEN(blocks); i++)
|
||||
if (time == 0 || (blocks[i].interval != 0 && time % blocks[i].interval == 0))
|
||||
if (time == 0 ||
|
||||
(blocks[i].interval != 0 && time % blocks[i].interval == 0))
|
||||
execBlock(i, NULL);
|
||||
}
|
||||
|
||||
int getStatus(char* new, char* old) {
|
||||
int getStatus(char *new, char *old) {
|
||||
strcpy(old, new);
|
||||
new[0] = '\0';
|
||||
|
||||
@ -109,7 +111,7 @@ int getStatus(char* new, char* old) {
|
||||
}
|
||||
|
||||
void updateBlock(int i) {
|
||||
char* output = outputs[i];
|
||||
char *output = outputs[i];
|
||||
char buffer[LEN(outputs[0]) - CLICKABLE_BLOCKS];
|
||||
int bytesRead = read(pipes[i][0], buffer, LEN(buffer));
|
||||
|
||||
@ -118,11 +120,10 @@ void updateBlock(int i) {
|
||||
while (buffer[j] != '\n' && count < CMDLENGTH) {
|
||||
count++;
|
||||
|
||||
// Skip continuation bytes, if any.
|
||||
// Skip continuation bytes, if any
|
||||
char ch = buffer[j];
|
||||
int skip = 1;
|
||||
while ((ch & 0xc0) > 0x80)
|
||||
ch <<= 1, skip++;
|
||||
while ((ch & 0xc0) > 0x80) ch <<= 1, skip++;
|
||||
j += skip;
|
||||
}
|
||||
|
||||
@ -131,8 +132,7 @@ void updateBlock(int i) {
|
||||
buffer[j] = ' ';
|
||||
|
||||
// Trim trailing spaces
|
||||
while (j >= 0 && buffer[j] == ' ')
|
||||
j--;
|
||||
while (j >= 0 && buffer[j] == ' ') j--;
|
||||
buffer[j + 1] = 0;
|
||||
|
||||
// Clear the pipe
|
||||
@ -156,25 +156,27 @@ void updateBlock(int i) {
|
||||
|
||||
void debug() {
|
||||
// Only write out if text has changed
|
||||
if (!getStatus(statusBar[0], statusBar[1]))
|
||||
return;
|
||||
if (!getStatus(statusBar[0], statusBar[1])) return;
|
||||
|
||||
write(STDOUT_FILENO, statusBar[0], strlen(statusBar[0]));
|
||||
write(STDOUT_FILENO, "\n", 1);
|
||||
}
|
||||
|
||||
void setRoot() {
|
||||
// Only set root if text has changed
|
||||
if (!getStatus(statusBar[0], statusBar[1]))
|
||||
return;
|
||||
int setupX() {
|
||||
dpy = XOpenDisplay(NULL);
|
||||
if (!dpy) return 1;
|
||||
|
||||
Display* d = XOpenDisplay(NULL);
|
||||
if (d)
|
||||
dpy = d;
|
||||
screen = DefaultScreen(dpy);
|
||||
root = RootWindow(dpy, screen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setRoot() {
|
||||
// Only set root if text has changed
|
||||
if (!getStatus(statusBar[0], statusBar[1])) return;
|
||||
|
||||
XStoreName(dpy, root, statusBar[0]);
|
||||
XCloseDisplay(dpy);
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
void signalHandler() {
|
||||
@ -199,16 +201,15 @@ void signalHandler() {
|
||||
|
||||
for (int j = 0; j < LEN(blocks); j++) {
|
||||
if (blocks[j].signal == signal - SIGRTMIN) {
|
||||
char button[] = {'0' + info.ssi_int & 0xff, 0};
|
||||
char button[4]; // value can't be more than 255;
|
||||
sprintf(button, "%d", info.ssi_int & 0xff);
|
||||
execBlock(j, button);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void termHandler() {
|
||||
statusContinue = 0;
|
||||
}
|
||||
void termHandler() { statusContinue = 0; }
|
||||
|
||||
void setupSignals() {
|
||||
sigset_t handledSignals;
|
||||
@ -227,8 +228,7 @@ void setupSignals() {
|
||||
epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event);
|
||||
|
||||
// Block all realtime and handled signals
|
||||
for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
|
||||
sigaddset(&handledSignals, i);
|
||||
for (int i = SIGRTMIN; i <= SIGRTMAX; i++) sigaddset(&handledSignals, i);
|
||||
sigprocmask(SIG_BLOCK, &handledSignals, NULL);
|
||||
|
||||
// Handle termination signals
|
||||
@ -258,8 +258,7 @@ void statusLoop() {
|
||||
signalHandler();
|
||||
}
|
||||
|
||||
if (eventCount != -1)
|
||||
writeStatus();
|
||||
if (eventCount != -1) writeStatus();
|
||||
}
|
||||
}
|
||||
|
||||
@ -283,19 +282,23 @@ void init() {
|
||||
setupSignals();
|
||||
}
|
||||
|
||||
int main(const int argc, const char* argv[]) {
|
||||
int main(const int argc, const char *argv[]) {
|
||||
if (setupX()) {
|
||||
fprintf(stderr, "dwmblocks: Failed to open display\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
writeStatus = setRoot;
|
||||
for (int i = 0; i < argc; i++)
|
||||
if (!strcmp("-d", argv[i]))
|
||||
writeStatus = debug;
|
||||
if (!strcmp("-d", argv[i])) writeStatus = debug;
|
||||
|
||||
init();
|
||||
statusLoop();
|
||||
|
||||
XCloseDisplay(dpy);
|
||||
close(epollFD);
|
||||
close(signalFD);
|
||||
for (int i = 0; i < LEN(pipes); i++)
|
||||
closePipe(pipes[i]);
|
||||
for (int i = 0; i < LEN(pipes); i++) closePipe(pipes[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user