+ struct swm_geometry g = r->g;
+ int rt, lt, tp, bm, bw;
+
+ bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
+
+ /*
+ * Perpendicular distance of each side of the window to the respective
+ * side of the region boundary. Positive values indicate the side of
+ * the window has passed beyond the region boundary.
+ */
+ rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
+ lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
+ bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
+ tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
+
+ DNPRINTF(SWM_D_MISC, "region_containment: win 0x%x, rt: %d, lt: %d, "
+ "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
+ lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
+ YESNO(opts & SWM_CW_HARDBOUNDARY));
+
+ /*
+ * Disable containment if any of the flagged sides went beyond the
+ * containment boundary, or if containment is disabled.
+ */
+ if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
+ (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
+ /* Make sure window has at least 1 pixel in the region */
+ g.x += 1 - WIDTH(win);
+ g.y += 1 - HEIGHT(win);
+ g.w += 2 * WIDTH(win) - 2;
+ g.h += 2 * HEIGHT(win) - 2;
+ }
+
+ constrain_window(win, &g, &opts);
+}
+
+/* Move or resize a window so that flagged side(s) fit into the supplied box. */
+void
+constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
+{
+ DNPRINTF(SWM_D_MISC, "constrain_window: window: 0x%x, (x,y) w x h: "
+ "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
+ "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
+ Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
+ YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
+ YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
+ YESNO(*opts & SWM_CW_RESIZABLE));
+
+ if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
+ if (*opts & SWM_CW_RESIZABLE)
+ WIDTH(win) = b->x + b->w - X(win) - BORDER(win);