-rw-r--r-- src/parse.c
1 #include <wlr/types/wlr_box.h> 2 3 #include "parse.h" 4 #include "types.h" 5 6 7 /* To get the length of these arrays we need to calculate that before passing 8 * them to the getter so the macro in the header hides this. */ 9 int _get(struct dict *values, const int len, const char *name) { 10 for (int i = 0; i < len; i++) { 11 if (strcasecmp(values[i].name, name) == 0) 12 return values[i].value; 13 } 14 return 0; 15 } 16 17 18 static bool wlr_box_from_str(char* str, struct wlr_box *box) { 19 // turns e.g. 1920x1800+500+500 into a wlr_box 20 char *w, *h, *x, *y; 21 if (str) { 22 if ((w = strtok(str, "x")) && is_number_perc(w)) { 23 if ((h = strtok(NULL, "+")) && is_number_perc(h)) { 24 if ((x = strtok(NULL, "+")) && is_number_perc(x)) { 25 if ((y = strtok(NULL, " \t\n\r")) && is_number_perc(y)) { 26 if (is_number(w)) { 27 box->width = atoi(w); 28 } else { 29 if ((w = strtok(w, "%"))) { 30 box->width = - atoi(w); 31 } else { 32 return false; 33 } 34 } 35 if (is_number(h)) { 36 box->height = atoi(h); 37 } else { 38 if ((h = strtok(h, "%"))) { 39 box->height = - atoi(h); 40 } else { 41 return false; 42 } 43 } 44 if (is_number(x)) { 45 box->x = atoi(x); 46 } else { 47 if ((x = strtok(x, "%"))) { 48 box->x = - atoi(x); 49 } else { 50 return false; 51 } 52 } 53 if (is_number(y)) { 54 box->y = atoi(y); 55 } else { 56 if ((y = strtok(y, "%"))) { 57 box->y = - atoi(y); 58 } else { 59 return false; 60 } 61 } 62 return true; 63 } 64 } 65 } 66 } 67 } 68 return false; 69 } 70 71 72 static struct dict dirs[] = { 73 { "up", UP }, 74 { "right", RIGHT }, 75 { "down", DOWN }, 76 { "left", LEFT }, 77 }; 78 79 80 bool dir_handler(void **data, char *args) { 81 *data = calloc(1, sizeof(enum direction)); 82 *(enum direction *)(*data) = get(dirs, strtok(args, " \t\n\r")); 83 return true; 84 } 85 86 87 bool str_handler(void **data, char *args) { 88 if (!args) { 89 return false; 90 } 91 if (is_number(args)) { 92 *data = calloc(1, sizeof(double)); 93 *(double *)(*data) = strtod(args, NULL); 94 } else { 95 *data = calloc(strlen(args), sizeof(char)); 96 strncpy(*data, args, strlen(args)); 97 } 98 return true; 99 } 100 101 102 bool motion_handler(void **data, char *args) { 103 char *s; 104 if (!args) { 105 return false; 106 } 107 108 s = strtok(args, " \t\n\r"); 109 if (!is_number(s) || !args) { 110 return false; 111 } 112 int x = atoi(s); 113 114 s = strtok(NULL, " \t\n\r"); 115 if (!is_number(s)) { 116 return false; 117 } 118 int y = atoi(s); 119 120 struct motion motion = { 121 .dx = x, 122 .dy = y, 123 .is_percentage = true, 124 }; 125 *data = calloc(1, sizeof(struct motion)); 126 *(struct motion *)(*data) = motion; 127 return true; 128 } 129 130 131 static int _scratchpad_id = 0; 132 133 134 bool scratchpad_handler(void **data, char *args) { 135 char *geo, *command; 136 struct wlr_box box; 137 if (!args) { 138 return false; 139 } 140 if (!(geo = strtok(args, " \t\n\r"))) { 141 return false; 142 } 143 if (!(command = strtok(NULL, "\n\r"))) { 144 return false; 145 } 146 if (!wlr_box_from_str(geo, &box)) { 147 return false; 148 } 149 150 struct scratchpad *scratchpad = calloc(1, sizeof(struct scratchpad)); 151 wl_list_insert(wimp.scratchpads.prev, &scratchpad->link); 152 scratchpad->command = strdup(command); 153 scratchpad->id = _scratchpad_id; 154 scratchpad->is_mapped = false; 155 scratchpad->view = NULL; 156 scratchpad->geo = box; 157 158 *data = calloc(1, sizeof(int)); 159 *(int *)(*data) = _scratchpad_id; 160 _scratchpad_id++; 161 return true; 162 } 163 164 165 bool box_handler(void **data, char *args) { 166 char *geo; 167 if (!(geo = strtok(args, " \t\n\r"))) { 168 return false; 169 } 170 *data = calloc(1, sizeof(struct wlr_box)); 171 if (!wlr_box_from_str(geo, *data)) { 172 free(*data); 173 return false; 174 } 175 return true; 176 } 177