— Scoopta 4 days ago
Added hot CSS reloading
1 files changed, 110 insertions(+), 103 deletions(-)

M src/main.c
M src/main.c +110 -103
@@ 31,6 31,9 @@
 static char* COLORS_LOCATION;
 static struct map* config;
 static char** _argv;
+static char* config_path;
+static char* stylesheet;
+static char* color_path;
 
 static void print_usage(char** argv) {
 	char* slash = strrchr(argv[0], '/');

          
@@ 57,6 60,108 @@
 return FALSE;
 }
 
+static gboolean reload_css(gpointer data) {
+	(void) data;
+	if(access(stylesheet, R_OK) == 0) {
+		FILE* file = fopen(stylesheet, "r");
+		fseek(file, 0, SEEK_END);
+		ssize_t size = ftell(file);
+		fseek(file, 0, SEEK_SET);
+		char* data = malloc(size + 1);
+		fread(data, 1, size, file);
+		fclose(file);
+
+		data[size] = 0;
+		struct wl_list lines;
+		struct node {
+			char* line;
+			struct wl_list link;
+		};
+		wl_list_init(&lines);
+		if(access(color_path, R_OK) == 0) {
+			file = fopen(color_path, "r");
+			char* line = NULL;
+			size_t line_size = 0;
+			ssize_t line_l = 0;
+			while((line_l = getline(&line, &line_size, file)) != -1) {
+				struct node* entry = malloc(sizeof(struct node));
+				line[line_l - 1] = 0;
+				entry->line = malloc(line_l + 1);
+				strcpy(entry->line, line);
+				wl_list_insert(&lines, &entry->link);
+			}
+			fclose(file);
+			free(line);
+		}
+
+		ssize_t count = wl_list_length(&lines) - 1;
+		if(count > 99) {
+			fprintf(stderr, "Woah there that's a lot of colors. Try having no more than 99, thanks\n");
+			exit(1);
+		}
+		struct node* node;
+		wl_list_for_each(node, &lines, link) {
+			//Do --rootbar-color replace
+			const char* color = node->line;
+			const char* rootbar_color = "--rootbar-color";
+			char count_str[3];
+			snprintf(count_str, 3, "%lu", count--);
+			char* needle = utils_concat(2, rootbar_color, count_str);
+			size_t color_len = strlen(color);
+			size_t needle_len = strlen(needle);
+			if(color_len > needle_len) {
+				free(needle);
+				fprintf(stderr, "What color format is this, try #FFFFFF\n");
+				continue;
+			}
+			char* replace = strstr(data, needle);
+			while(replace != NULL) {
+				memcpy(replace, color, color_len);
+				memset(replace + color_len, ' ', needle_len - color_len);
+				replace = strstr(data, needle);
+			}
+			free(needle);
+
+
+			//Do --rootbar-rgb-color replace
+			if(color_len < 7) {
+				fprintf(stderr, "What color format is this, try #FFFFFF\n");
+				continue;
+			}
+			const char* rootbar_rgb_color = "--rootbar-rgb-color";
+			needle = utils_concat(2, rootbar_rgb_color, count_str);
+			needle_len = strlen(needle);
+			replace = strstr(data, needle);
+			while(replace != NULL) {
+				char r[3];
+				char g[3];
+				char b[3];
+				memcpy(r, color + 1, 2);
+				memcpy(g, color + 3, 2);
+				memcpy(b, color + 5, 2);
+				r[2] = 0;
+				g[2] = 0;
+				b[2] = 0;
+				char rgb[14];
+				snprintf(rgb, 14, "%ld, %ld, %ld", strtol(r, NULL, 16), strtol(g, NULL, 16), strtol(b, NULL, 16));
+				size_t rgb_len = strlen(rgb);
+				memcpy(replace, rgb, rgb_len);
+				memset(replace + rgb_len, ' ', needle_len - rgb_len);
+				replace = strstr(data, needle);
+			}
+			free(needle);
+		}
+		GtkCssProvider* css = gtk_css_provider_new();
+#ifdef GTK4
+		gtk_css_provider_load_from_data(css, data, strlen(data));
+#else
+		gtk_css_provider_load_from_data(css, data, strlen(data), NULL);
+#endif
+		gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+	}
+	return FALSE;
+}
+
 void sig(int32_t signum) {
 	switch(signum) {
 	case SIGINT:

          
@@ 65,6 170,9 @@
 case SIGUSR1:
 		g_idle_add(G_SOURCE_FUNC(reload), NULL);
 		break;
+	case SIGUSR2:
+		g_idle_add(G_SOURCE_FUNC(reload_css), NULL);
+		break;
 	}
 }
 

          
@@ 168,7 276,6 @@
 config = map_init();
 
 	//Check if --conf was specified
-	char* config_path;
 	if(config_str == NULL) {
 		const char* config_f = "/config";
 		config_path = utils_concat(2, CONFIG_LOCATION, config_f);

          
@@ 184,7 291,6 @@
 free(config_path);
 
 	//Check if --style was specified
-	char* stylesheet;
 	if(style_str == NULL) {
 		style_str = map_get(config, "stylesheet");
 		if(style_str == NULL) {

          
@@ 202,7 308,6 @@
 }
 
 	//Check if --color was specified
-	char* color_path;
 	if(color_str == NULL) {
 		color_str = map_get(config, "colors");
 		if(color_str == NULL) {

          
@@ 226,106 331,7 @@
 gtk_init(&argc, &argv);
 #endif
 
-	if(access(stylesheet, R_OK) == 0) {
-		FILE* file = fopen(stylesheet, "r");
-		fseek(file, 0, SEEK_END);
-		ssize_t size = ftell(file);
-		fseek(file, 0, SEEK_SET);
-		char* data = malloc(size + 1);
-		fread(data, 1, size, file);
-		fclose(file);
-
-		data[size] = 0;
-		struct wl_list lines;
-		struct node {
-			char* line;
-			struct wl_list link;
-		};
-		wl_list_init(&lines);
-		if(access(color_path, R_OK) == 0) {
-			file = fopen(color_path, "r");
-			char* line = NULL;
-			size_t line_size = 0;
-			ssize_t line_l = 0;
-			while((line_l = getline(&line, &line_size, file)) != -1) {
-				struct node* entry = malloc(sizeof(struct node));
-				line[line_l - 1] = 0;
-				entry->line = malloc(line_l + 1);
-				strcpy(entry->line, line);
-				wl_list_insert(&lines, &entry->link);
-			}
-			fclose(file);
-			free(line);
-		}
-
-		ssize_t count = wl_list_length(&lines) - 1;
-		if(count > 99) {
-			fprintf(stderr, "Woah there that's a lot of colors. Try having no more than 99, thanks\n");
-			exit(1);
-		}
-		struct node* node;
-		wl_list_for_each(node, &lines, link) {
-			//Do --rootbar-color replace
-			const char* color = node->line;
-			const char* rootbar_color = "--rootbar-color";
-			char count_str[3];
-			snprintf(count_str, 3, "%lu", count--);
-			char* needle = utils_concat(2, rootbar_color, count_str);
-			size_t color_len = strlen(color);
-			size_t needle_len = strlen(needle);
-			if(color_len > needle_len) {
-				free(needle);
-				fprintf(stderr, "What color format is this, try #FFFFFF\n");
-				continue;
-			}
-			char* replace = strstr(data, needle);
-			while(replace != NULL) {
-				memcpy(replace, color, color_len);
-				memset(replace + color_len, ' ', needle_len - color_len);
-				replace = strstr(data, needle);
-			}
-			free(needle);
-
-
-			//Do --rootbar-rgb-color replace
-			if(color_len < 7) {
-				fprintf(stderr, "What color format is this, try #FFFFFF\n");
-				continue;
-			}
-			const char* rootbar_rgb_color = "--rootbar-rgb-color";
-			needle = utils_concat(2, rootbar_rgb_color, count_str);
-			needle_len = strlen(needle);
-			replace = strstr(data, needle);
-			while(replace != NULL) {
-				char r[3];
-				char g[3];
-				char b[3];
-				memcpy(r, color + 1, 2);
-				memcpy(g, color + 3, 2);
-				memcpy(b, color + 5, 2);
-				r[2] = 0;
-				g[2] = 0;
-				b[2] = 0;
-				char rgb[14];
-				snprintf(rgb, 14, "%ld, %ld, %ld", strtol(r, NULL, 16), strtol(g, NULL, 16), strtol(b, NULL, 16));
-				size_t rgb_len = strlen(rgb);
-				memcpy(replace, rgb, rgb_len);
-				memset(replace + rgb_len, ' ', needle_len - rgb_len);
-				replace = strstr(data, needle);
-			}
-			free(needle);
-		}
-		GtkCssProvider* css = gtk_css_provider_new();
-#ifdef GTK4
-		gtk_css_provider_load_from_data(css, data, strlen(data));
-#else
-		gtk_css_provider_load_from_data(css, data, strlen(data), NULL);
-#endif
-		gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-	}
-
-	free(stylesheet);
-	free(color_path);
+	reload_css(NULL);
 
 	if(strcmp(bar_name, "all") == 0) {
 		char* bars = map_get(config, "bars");

          
@@ 346,6 352,7 @@
 sigact.sa_handler = sig;
 	sigaction(SIGINT, &sigact, NULL);
 	sigaction(SIGUSR1, &sigact, NULL);
+	sigaction(SIGUSR2, &sigact, NULL);
 	gtk_main();
 	return 0;
 }