gimp but for windows: a wayland compositor
git clone https://github.com/m-col/wimp
Files | Refs | Readme

Commit 136ce650d2a24c163a7bd3f41fded1e2e1fcbc03
Parent: b885455b2bf3142bbcece998ed581abd00d92e2a
Author: mcol <mcol@posteo.net>
Date: 2021-03-07 11:05:56 +0000
Committer: mcol <mcol@posteo.net>
Committed: 2021-03-07 11:05:56 +0000

move command interface parsing to parse.{c,h}

src/types.h Modified

@@ -237,4 +237,9 @@
     struct wlr_box geo; // negative values represent a percentage of the output's geometry
 };
 
+struct value_map {
+    const char *name;
+    const int value;
+};
+
 #endif

src/parse.h Added

@@ -0,0 +1,16 @@
+#ifndef WIMP_PARSE_H
+#define WIMP_PARSE_H
+
+#include "types.h"
+
+int _get(struct value_map *values, const int len, const char *name);
+#define get(arr, name) _get(arr, sizeof(arr) / sizeof(arr[0]), name)
+
+bool wlr_box_from_str(char* str, struct wlr_box *box);
+bool dir_handler(void **data, char *args);
+bool str_handler(void **data, char *args);
+bool motion_handler(void **data, char *args);
+bool scratchpad_handler(void **data, char *args);
+bool box_handler(void **data, char *args);
+
+#endif

src/parse.c Added

@@ -0,0 +1,176 @@
+#include <wlr/types/wlr_box.h>
+
+#include "parse.h"
+#include "types.h"
+
+
+/* To get the length of these arrays we need to calculate that before passing
+ * them to the getter so the macro in the header hides this. */
+int _get(struct value_map *values, const int len, const char *name) {
+    for (int i = 0; i < len; i++) {
+	if (strcasecmp(values[i].name, name) == 0)
+	    return values[i].value;
+    }
+    return 0;
+}
+
+
+bool wlr_box_from_str(char* str, struct wlr_box *box) {
+    // turns e.g. 1920x1800+500+500 into a wlr_box
+    char *w, *h, *x, *y;
+    if (str) {
+	if ((w = strtok(str, "x")) && is_number_perc(w)) {
+	    if ((h = strtok(NULL, "+")) && is_number_perc(h)) {
+		if ((x = strtok(NULL, "+")) && is_number_perc(x)) {
+		    if ((y = strtok(NULL, " \t\n\r")) && is_number_perc(y)) {
+			if (is_number(w)) {
+			    box->width = atoi(w);
+			} else {
+			    if ((w = strtok(w, "%"))) {
+				box->width = - atoi(w);
+			    } else {
+				return false;
+			    }
+			}
+			if (is_number(h)) {
+			    box->height = atoi(h);
+			} else {
+			    if ((h = strtok(h, "%"))) {
+				box->height = - atoi(h);
+			    } else {
+				return false;
+			    }
+			}
+			if (is_number(x)) {
+			    box->x = atoi(x);
+			} else {
+			    if ((x = strtok(x, "%"))) {
+				box->x = - atoi(x);
+			    } else {
+				return false;
+			    }
+			}
+			if (is_number(y)) {
+			    box->y = atoi(y);
+			} else {
+			    if ((y = strtok(y, "%"))) {
+				box->y = - atoi(y);
+			    } else {
+				return false;
+			    }
+			}
+			return true;
+		    }
+		}
+	    }
+	}
+    }
+    return false;
+}
+
+
+static struct value_map dirs[] = {
+    { "up", UP },
+    { "right", RIGHT },
+    { "down", DOWN },
+    { "left", LEFT },
+};
+
+
+bool dir_handler(void **data, char *args) {
+    *data = calloc(1, sizeof(enum direction));
+    *(enum direction *)(*data) = get(dirs, strtok(args, " \t\n\r"));
+    return true;
+}
+
+
+bool str_handler(void **data, char *args) {
+    if (!args) {
+	return false;
+    }
+    if (is_number(args)) {
+	*data = calloc(1, sizeof(double));
+	*(double *)(*data) = strtod(args, NULL);
+    } else {
+	*data = calloc(strlen(args), sizeof(char));
+	strncpy(*data, args, strlen(args));
+    }
+    return true;
+}
+
+
+bool motion_handler(void **data, char *args) {
+    char *s;
+    if (!args) {
+	return false;
+    }
+
+    s = strtok(args, " \t\n\r");
+    if (!is_number(s) || !args) {
+	return false;
+    }
+    int x = atoi(s);
+
+    s = strtok(NULL, " \t\n\r");
+    if (!is_number(s)) {
+	return false;
+    }
+    int y = atoi(s);
+
+    struct motion motion = {
+	.dx = x,
+	.dy = y,
+	.is_percentage = true,
+    };
+    *data = calloc(1, sizeof(struct motion));
+    *(struct motion *)(*data) = motion;
+    return true;
+}
+
+
+static int _scratchpad_id = 0;
+
+
+bool scratchpad_handler(void **data, char *args) {
+    char *geo, *command;
+    struct wlr_box box;
+    if (!args) {
+	return false;
+    }
+    if (!(geo = strtok(args, " \t\n\r"))) {
+	return false;
+    }
+    if (!(command = strtok(NULL, "\n\r"))) {
+	return false;
+    }
+    if (!wlr_box_from_str(geo, &box)) {
+	return false;
+    }
+
+    struct scratchpad *scratchpad = calloc(1, sizeof(struct scratchpad));
+    wl_list_insert(wimp.scratchpads.prev, &scratchpad->link);
+    scratchpad->command = strdup(command);
+    scratchpad->id = _scratchpad_id;
+    scratchpad->is_mapped = false;
+    scratchpad->view = NULL;
+    scratchpad->geo = box;
+
+    *data = calloc(1, sizeof(int));
+    *(int *)(*data) = _scratchpad_id;
+    _scratchpad_id++;
+    return true;
+}
+
+
+bool box_handler(void **data, char *args) {
+    char *geo;
+    if (!(geo = strtok(args, " \t\n\r"))) {
+	return false;
+    }
+    *data = calloc(1, sizeof(struct wlr_box));
+    if (!wlr_box_from_str(geo, *data)) {
+	free(*data);
+	return false;
+    }
+    return true;
+}

src/keybind.c Modified

@@ -1,25 +1,8 @@
 #include "action.h"
 #include "keybind.h"
+#include "parse.h"
 #include "types.h"
 
-struct value_map {
-    const char *name;
-    const int value;
-};
-
-
-/* To get the length of these arrays we need to calculate that before passing
- * them to the getter so the macro hides this. */
-static int _get(struct value_map *values, const int len, const char *name) {
-    for (int i = 0; i < len; i++) {
-	if (strcasecmp(values[i].name, name) == 0)
-	    return values[i].value;
-    }
-    return 0;
-}
-
-#define get(arr, name) _get(arr, sizeof(arr) / sizeof(arr[0]), name)
-
 
 static struct value_map mods[] = {
     { "shift", WLR_MODIFIER_SHIFT },
@@ -33,14 +16,6 @@
 };
 
 
-static struct value_map dirs[] = {
-    { "up", UP },
-    { "right", RIGHT },
-    { "down", DOWN },
-    { "left", LEFT },
-};
-
-
 static struct value_map mouse_keys[] = {
     { "motion", MOTION },
     { "scroll", SCROLL },
@@ -51,163 +26,10 @@
 };
 
 
-static bool dir_handler(struct binding *kb, char *data) {
-    kb->data = calloc(1, sizeof(enum direction));
-    *(enum direction *)(kb->data) = get(dirs, strtok(data, " \t\n\r"));
-    return true;
-}
-
-
-static bool str_handler(struct binding *kb, char *data) {
-    if (!data) {
-	return false;
-    }
-    if (is_number(data)) {
-	kb->data = calloc(1, sizeof(double));
-	*(double *)(kb->data) = strtod(data, NULL);
-    } else {
-	kb->data = calloc(strlen(data), sizeof(char));
-	strncpy(kb->data, data, strlen(data));
-    }
-    return true;
-}
-
-
-static bool motion_handler(struct binding *kb, char *data) {
-    char *s;
-    if (!data) {
-	return false;
-    }
-
-    s = strtok(data, " \t\n\r");
-    if (!is_number(s) || !data) {
-	return false;
-    }
-    int x = atoi(s);
-
-    s = strtok(NULL, " \t\n\r");
-    if (!is_number(s)) {
-	return false;
-    }
-    int y = atoi(s);
-
-    struct motion motion = {
-	.dx = x,
-	.dy = y,
-	.is_percentage = true,
-    };
-    kb->data = calloc(1, sizeof(struct motion));
-    *(struct motion *)(kb->data) = motion;
-    return true;
-}
-
-
-bool wlr_box_from_str(char* str, struct wlr_box *box) {
-    // turns e.g. 1920x1800+500+500 into a wlr_box
-    char *w, *h, *x, *y;
-    if (str) {
-	if ((w = strtok(str, "x")) && is_number_perc(w)) {
-	    if ((h = strtok(NULL, "+")) && is_number_perc(h)) {
-		if ((x = strtok(NULL, "+")) && is_number_perc(x)) {
-		    if ((y = strtok(NULL, " \t\n\r")) && is_number_perc(y)) {
-			if (is_number(w)) {
-			    box->width = atoi(w);
-			} else {
-			    if ((w = strtok(w, "%"))) {
-				box->width = - atoi(w);
-			    } else {
-				return false;
-			    }
-			}
-			if (is_number(h)) {
-			    box->height = atoi(h);
-			} else {
-			    if ((h = strtok(h, "%"))) {
-				box->height = - atoi(h);
-			    } else {
-				return false;
-			    }
-			}
-			if (is_number(x)) {
-			    box->x = atoi(x);
-			} else {
-			    if ((x = strtok(x, "%"))) {
-				box->x = - atoi(x);
-			    } else {
-				return false;
-			    }
-			}
-			if (is_number(y)) {
-			    box->y = atoi(y);
-			} else {
-			    if ((y = strtok(y, "%"))) {
-				box->y = - atoi(y);
-			    } else {
-				return false;
-			    }
-			}
-			return true;
-		    }
-		}
-	    }
-	}
-    }
-    return false;
-}
-
-
-static int _scratchpad_id = 0;
-
-
-static bool scratchpad_handler(struct binding *kb, char *data) {
-    char *geo, *command;
-    struct wlr_box box;
-    if (!data) {
-	return false;
-    }
-    if (!(geo = strtok(data, " \t\n\r"))) {
-	return false;
-    }
-    if (!(command = strtok(NULL, "\n\r"))) {
-	return false;
-    }
-    if (!wlr_box_from_str(geo, &box)) {
-	return false;
-    }
-
-    struct scratchpad *scratchpad = calloc(1, sizeof(struct scratchpad));
-    wl_list_insert(wimp.scratchpads.prev, &scratchpad->link);
-    scratchpad->command = strdup(command);
-    scratchpad->id = _scratchpad_id;
-    scratchpad->is_mapped = false;
-    scratchpad->view = NULL;
-    scratchpad->geo = box;
-
-    kb->data = calloc(1, sizeof(int));
-    *(int *)(kb->data) = _scratchpad_id;
-    _scratchpad_id++;
-    return true;
-}
-
-
-static bool box_handler(struct binding *kb, char *data) {
-    char *geo;
-    if (!(geo = strtok(data, " \t\n\r"))) {
-	return false;
-    }
-    struct wlr_box *box = calloc(1, sizeof(struct wlr_box));
-    if (!wlr_box_from_str(geo, box)) {
-	return false;
-    }
-    kb->data = box;
-    return true;
-}
-
-
 static struct {
     const char *name;
     const action action;
-    bool (*data_handler)(struct binding *kb, char *data);
+    bool (*data_handler)(void **data, char *args);
 } action_map[] = {
     { "terminate", &terminate, NULL },
     { "exec", &exec_command, &str_handler },
@@ -248,7 +70,7 @@
 };
 
 
-static bool assign_action(const char *name, char *data, struct binding *kb, char * response) {
+static bool assign_action(const char *name, char *args, struct binding *kb, char * response) {
     kb->action = NULL;
     kb->data = NULL;
     size_t i;
@@ -276,7 +98,7 @@
 		if (strcmp(action_map[i].name, name) == 0) {
 		    kb->action = action_map[i].action;
 		    if (action_map[i].data_handler != NULL) {
-			if (!action_map[i].data_handler(kb, data)){
+			if (!action_map[i].data_handler(&kb->data, args)){
 			    sprintf(response, "Command malformed/incomplete.");
 			    return false;
 			}