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

Commit 507c5bd988a9e0f8f246373c6c23ed67804679e4
Parent: a8d0f418fed1d9b87eec9260797c9218c1fc1d6b
Author: mcol <mcol@posteo.net>
Date: 2021-02-18 20:48:17 +0000
Committer: mcol <mcol@posteo.net>
Committed: 2021-02-18 20:48:17 +0000

add to_region action

wimp.conf Modified

@@ -99,6 +99,8 @@
 # A percentage of the used output can also be requested:
 wimptool bind	Ctrl	t		scratchpad 50%+50%+0+0 footclient tmux
 
+wimptool bind		s		exec wimptool to_region \`slurp -f %wx%h+%x+%y\`
+
 # These two actions can be used to mark and go to set positions. Each one, once
 # pressed, waits for the next key press and uses that as the 'mark', e.g.
 # hitting 'mod + m' then 'a' will save the current position, then hitting 'mod

src/ipc.c Modified

@@ -27,7 +27,7 @@
 	ssize_t len = recv(fd, buffer, sizeof(buffer) - 1, 0);
 	if (len > 0) {
 	    buffer[len] = '\0';
-	    parse_message(buffer);
+	    handle_message(buffer);
 	}
 	close(fd);
     }

src/config.h Modified

@@ -3,8 +3,9 @@
 
 #include "types.h"
 
+bool wlr_box_from_str(char* str, struct wlr_box *box);
 void assign_colour(char *hex, float dest[4]);
-void parse_message(char *buffer);
+void handle_message(char *buffer);
 void schedule_startup();
 void set_up_defaults();
 

src/config.c Modified

@@ -125,7 +125,7 @@
 }
 
 
-static bool wlr_box_from_str(char* str, struct wlr_box *box) {
+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) {
@@ -217,6 +217,23 @@
 }
 
 
+static bool box_handler(struct binding *kb, char *data) {
+    char *geo;
+    if (!(geo = strtok(data, " \t\n\r"))) {
+	goto err;
+    }
+    struct wlr_box *box = calloc(1, sizeof(struct wlr_box));
+    if (!wlr_box_from_str(geo, box)) {
+	goto err;
+    }
+    kb->data = box;
+    return true;
+err:
+    wlr_log(WLR_ERROR, "Command malformed/incomplete.");
+    return false;
+}
+
+
 static struct {
     const char *name;
     const action action;
@@ -239,6 +256,7 @@
     { "maximize", &maximize, NULL },
     { "send_to_desk", &send_to_desk, &str_handler },
     { "scratchpad", &toggle_scratchpad, &scratchpad_handler },
+    { "to_region", &to_region, &box_handler },
 };
 
 
@@ -460,7 +478,7 @@
 }
 
 
-void parse_message(char *message) {
+void handle_message(char *message) {
     char *s;
     if (!(s = strtok(message, " \t\n\r"))) {
 	return;
@@ -595,7 +613,9 @@
 
     // other non-empty, non-commented lines
     else {
-        wlr_log(WLR_ERROR, "Unknown command '%s'.", s);
+	if(!handle_do_action(s)) {
+	    wlr_log(WLR_ERROR, "Unknown command '%s'.", s);
+	}
     }
 }
 
@@ -655,7 +675,7 @@
 	"bind Ctrl Escape shutdown",
     };
     for (size_t i = 0; i < sizeof(defaults) / sizeof(defaults[0]); i++) {
-	parse_message(defaults[i]);
+	handle_message(defaults[i]);
     }
     setup_vt_switching();
 }

src/action.h Modified

@@ -3,6 +3,7 @@
 
 #include "types.h"
 
+bool handle_do_action(char *command);
 void shutdown(void *data);
 void exec_command(void *data);
 void change_vt(void *data);
@@ -26,5 +27,6 @@
 void maximize(void *data);
 void send_to_desk(void *data);
 void toggle_scratchpad(void *data);
+void to_region(void *data);
 
 #endif

src/action.c Modified

@@ -12,6 +12,21 @@
 #include "shell.h"
 #include "types.h"
 
+bool handle_do_action(char *command) {
+    char *s;
+
+    if (!strcasecmp(command, "to_region")) {
+	if ((s = strtok(NULL, " \t\n\r"))) {
+	    struct wlr_box box;
+	    if (wlr_box_from_str(s, &box)) {
+		to_region(&box);
+	    }
+	}
+	return true;
+    }
+    return false;
+}
+
 
 void shutdown(void *data) {
     wl_display_terminate(wimp.display);
@@ -447,3 +462,31 @@
 	wlr_log(WLR_DEBUG, "Launching scratchpad: %s", scratchpad->command);
     }
 }
+
+
+void to_region(void *data) {
+    struct wlr_box *box = (struct wlr_box *)data;
+    struct wlr_box *extents = wlr_output_layout_get_box(wimp.output_layout, NULL);
+
+    struct motion motion = {
+	.dx = - (extents->width / 2) + (box->width / 2) + box->x,
+	.dy = - (extents->height / 2) + (box->height / 2) + box->y,
+	.is_percentage = false,
+    };
+    pan_desk(&motion);
+
+    double f;
+    if (extents->width / extents->height > box->width / box->height) {
+	f = wimp.current_desk->zoom / ((double)box->width / (double)extents->width);
+    } else {
+	f = wimp.current_desk->zoom / ((double)box->height / (double)extents->height);
+    }
+    double dz = 100 * f / wimp.current_desk->zoom - 100;
+    double cx = wimp.cursor->x;
+    double cy = wimp.cursor->y;
+    wimp.cursor->x = extents->width / 2;
+    wimp.cursor->y = extents->height / 2;
+    zoom(&dz);
+    wimp.cursor->x = cx;
+    wimp.cursor->y = cy;
+}