Handle block output excess properly

This commit is contained in:
Utkarsh Verma 2022-01-03 10:34:20 +05:30
parent 978229b265
commit 7df6de59b5
No known key found for this signature in database
GPG Key ID: 817656CF818EFCCC

27
main.c
View File

@ -12,8 +12,9 @@
#define POLL_INTERVAL 50 #define POLL_INTERVAL 50
#define LEN(arr) (sizeof(arr) / sizeof(arr[0])) #define LEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define BLOCK(cmd, interval, signal) {"echo \"$(" cmd ")\"", interval, signal} #define BLOCK(cmd, interval, signal) \
typedef struct { { "echo \"$(" cmd ")\"", interval, signal }
typedef const struct {
const char* command; const char* command;
const unsigned int interval; const unsigned int interval;
const unsigned int signal; const unsigned int signal;
@ -24,16 +25,22 @@ typedef struct {
#undef CLICKABLE_BLOCKS #undef CLICKABLE_BLOCKS
#define CLICKABLE_BLOCKS 1 #define CLICKABLE_BLOCKS 1
#else #else
#undef CLICKABLE_BLOCKS
#define CLICKABLE_BLOCKS 0 #define CLICKABLE_BLOCKS 0
#endif #endif
#ifdef LEADING_DELIMITER
#undef LEADING_DELIMITER
#define LEADING_DELIMITER 1
#else
#define LEADING_DELIMITER 0
#endif
static Display* dpy; static Display* dpy;
static int screen; static int screen;
static Window root; static Window root;
static unsigned short int statusContinue = 1; static unsigned short int statusContinue = 1;
static char outputs[LEN(blocks)][CMDLENGTH + 1 + CLICKABLE_BLOCKS]; static char outputs[LEN(blocks)][CMDLENGTH + 1 + CLICKABLE_BLOCKS];
static char statusBar[2][LEN(blocks) * ((LEN(outputs[0]) - 1) + (LEN(DELIMITER) - 1)) + 1]; static char statusBar[2][LEN(blocks) * (LEN(outputs[0]) - 1) + (LEN(blocks) - 1 + LEADING_DELIMITER) * (LEN(DELIMITER) - 1) + 1];
static struct epoll_event event, events[LEN(blocks) + 2]; static struct epoll_event event, events[LEN(blocks) + 2];
static int pipes[LEN(blocks)][2]; static int pipes[LEN(blocks)][2];
static int timerPipe[2]; static int timerPipe[2];
@ -79,7 +86,7 @@ int getStatus(char* new, char* old) {
new[0] = '\0'; new[0] = '\0';
for (int i = 0; i < LEN(blocks); i++) { for (int i = 0; i < LEN(blocks); i++) {
#ifdef LEADING_DELIMITER #if LEADING_DELIMITER
if (strlen(outputs[i])) if (strlen(outputs[i]))
#else #else
if (strlen(new) && strlen(outputs[i])) if (strlen(new) && strlen(outputs[i]))
@ -92,9 +99,14 @@ int getStatus(char* new, char* old) {
void updateBlock(int i) { void updateBlock(int i) {
char* output = outputs[i]; char* output = outputs[i];
char buffer[LEN(outputs[0])]; char buffer[LEN(outputs[0]) - CLICKABLE_BLOCKS];
int bytesRead = read(pipes[i][0], buffer, LEN(buffer)); int bytesRead = read(pipes[i][0], buffer, LEN(buffer));
if (bytesRead == 1) {
output[0] = '\0';
return;
}
// Trim UTF-8 characters properly // Trim UTF-8 characters properly
int j = bytesRead - 1; int j = bytesRead - 1;
while ((buffer[j] & 0b11000000) == 0x80) while ((buffer[j] & 0b11000000) == 0x80)
@ -111,9 +123,6 @@ void updateBlock(int i) {
char ch; char ch;
while (read(pipes[i][0], &ch, 1) == 1 && ch != '\n') while (read(pipes[i][0], &ch, 1) == 1 && ch != '\n')
; ;
} else if (bytesRead == 1) {
output[0] = '\0';
return;
} }
#if CLICKABLE_BLOCKS #if CLICKABLE_BLOCKS