status.c (2012B)
1 #include "status.h" 2 3 #include <stdbool.h> 4 #include <stdio.h> 5 #include <string.h> 6 7 #include "block.h" 8 #include "config.h" 9 #include "util.h" 10 #include "x11.h" 11 12 static bool has_status_changed(const status *const status) { 13 return strcmp(status->current, status->previous) != 0; 14 } 15 16 status status_new(const block *const blocks, 17 const unsigned short block_count) { 18 status status = { 19 .current = {[0] = '\0'}, 20 .previous = {[0] = '\0'}, 21 22 .blocks = blocks, 23 .block_count = block_count, 24 }; 25 26 return status; 27 } 28 29 bool status_update(status *const status) { 30 (void)strncpy(status->previous, status->current, LEN(status->current)); 31 status->current[0] = '\0'; 32 33 for (unsigned short i = 0; i < status->block_count; ++i) { 34 const block *const block = &status->blocks[i]; 35 36 if (strlen(block->output) > 0) { 37 #if LEADING_DELIMITER 38 (void)strncat(status->current, DELIMITER, LEN(DELIMITER)); 39 #else 40 if (status->current[0] != '\0') { 41 (void)strncat(status->current, DELIMITER, LEN(DELIMITER)); 42 } 43 #endif 44 45 #if CLICKABLE_BLOCKS 46 if (block->signal > 0) { 47 const char signal[] = {(char)block->signal, '\0'}; 48 (void)strncat(status->current, signal, LEN(signal)); 49 } 50 #endif 51 52 (void)strncat(status->current, block->icon, LEN(block->output)); 53 (void)strncat(status->current, block->output, LEN(block->output)); 54 } 55 } 56 57 #if TRAILING_DELIMITER 58 if (status->current[0] != '\0') { 59 (void)strncat(status->current, DELIMITER, LEN(DELIMITER)); 60 } 61 #endif 62 63 return has_status_changed(status); 64 } 65 66 int status_write(const status *const status, const bool is_debug_mode, 67 x11_connection *const connection) { 68 if (is_debug_mode) { 69 (void)printf("%s\n", status->current); 70 return 0; 71 } 72 73 if (x11_set_root_name(connection, status->current) != 0) { 74 return 1; 75 } 76 77 return 0; 78 }