00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <time.h>
00015 #include <math.h>
00016
00017 #include <xcb/xcb_atom.h>
00018 #include <xcb/xcb_icccm.h>
00019
00020 #include <X11/XKBlib.h>
00021
00022 #include "all.h"
00023
00024
00025 typedef enum { CLICK_BORDER = 0, CLICK_DECORATION = 1, CLICK_INSIDE = 2 } click_destination_t;
00026
00027
00028
00029
00030
00031
00032
00033 static bool tiling_resize_for_border(Con *con, border_t border, xcb_button_press_event_t *event) {
00034 DLOG("border = %d\n", border);
00035 char way = (border == BORDER_TOP || border == BORDER_LEFT ? 'p' : 'n');
00036 orientation_t orientation = (border == BORDER_TOP || border == BORDER_BOTTOM ? VERT : HORIZ);
00037
00038
00039 Con *first = NULL, *second = NULL;
00040 Con *resize_con = con;
00041 while (resize_con->type != CT_WORKSPACE &&
00042 resize_con->type != CT_FLOATING_CON &&
00043 resize_con->parent->orientation != orientation)
00044 resize_con = resize_con->parent;
00045
00046 if (resize_con->type != CT_WORKSPACE &&
00047 resize_con->type != CT_FLOATING_CON &&
00048 resize_con->parent->orientation == orientation) {
00049 first = resize_con;
00050 second = (way == 'n') ? TAILQ_NEXT(first, nodes) : TAILQ_PREV(first, nodes_head, nodes);
00051 if (second == TAILQ_END(&(first->nodes_head))) {
00052 second = NULL;
00053 }
00054 else if (way == 'p') {
00055 Con *tmp = first;
00056 first = second;
00057 second = tmp;
00058 }
00059 }
00060
00061 if (first == NULL || second == NULL) {
00062 DLOG("Resize not possible\n");
00063 return false;
00064 }
00065 else {
00066 assert(first != second);
00067 assert(first->parent == second->parent);
00068 resize_graphical_handler(first, second, orientation, event);
00069 }
00070
00071 DLOG("After resize handler, rendering\n");
00072 tree_render();
00073 return true;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 static bool floating_mod_on_tiled_client(Con *con, xcb_button_press_event_t *event) {
00085
00086
00087
00088 int to_right = con->rect.width - event->event_x,
00089 to_left = event->event_x,
00090 to_top = event->event_y,
00091 to_bottom = con->rect.height - event->event_y;
00092
00093 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n",
00094 to_right, to_left, to_top, to_bottom);
00095
00096 if (to_right < to_left &&
00097 to_right < to_top &&
00098 to_right < to_bottom)
00099 return tiling_resize_for_border(con, BORDER_RIGHT, event);
00100
00101 if (to_left < to_right &&
00102 to_left < to_top &&
00103 to_left < to_bottom)
00104 return tiling_resize_for_border(con, BORDER_LEFT, event);
00105
00106 if (to_top < to_right &&
00107 to_top < to_left &&
00108 to_top < to_bottom)
00109 return tiling_resize_for_border(con, BORDER_TOP, event);
00110
00111 if (to_bottom < to_right &&
00112 to_bottom < to_left &&
00113 to_bottom < to_top)
00114 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
00115
00116 return false;
00117 }
00118
00119
00120
00121
00122
00123 static bool tiling_resize(Con *con, xcb_button_press_event_t *event, click_destination_t dest) {
00124
00125 Rect bsr = con_border_style_rect(con);
00126 DLOG("BORDER x = %d, y = %d for con %p, window 0x%08x\n",
00127 event->event_x, event->event_y, con, event->event);
00128 DLOG("checks for right >= %d\n", con->window_rect.x + con->window_rect.width);
00129 if (dest == CLICK_DECORATION)
00130 return tiling_resize_for_border(con, BORDER_TOP, event);
00131
00132 if (event->event_x >= 0 && event->event_x <= bsr.x &&
00133 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
00134 return tiling_resize_for_border(con, BORDER_LEFT, event);
00135
00136 if (event->event_x >= (con->window_rect.x + con->window_rect.width) &&
00137 event->event_y >= bsr.y && event->event_y <= con->rect.height + bsr.height)
00138 return tiling_resize_for_border(con, BORDER_RIGHT, event);
00139
00140 if (event->event_y >= (con->window_rect.y + con->window_rect.height))
00141 return tiling_resize_for_border(con, BORDER_BOTTOM, event);
00142
00143 return false;
00144 }
00145
00146
00147
00148
00149
00150
00151 static int route_click(Con *con, xcb_button_press_event_t *event, bool mod_pressed, click_destination_t dest) {
00152 DLOG("--> click properties: mod = %d, destination = %d\n", mod_pressed, dest);
00153 DLOG("--> OUTCOME = %p\n", con);
00154 DLOG("type = %d, name = %s\n", con->type, con->name);
00155
00156
00157 if (con->parent->type == CT_DOCKAREA)
00158 goto done;
00159
00160
00161 Con *floatingcon = con_inside_floating(con);
00162 const bool proportional = (event->state & BIND_SHIFT);
00163 const bool in_stacked = (con->parent->layout == L_STACKED || con->parent->layout == L_TABBED);
00164
00165
00166 if (in_stacked &&
00167 dest == CLICK_DECORATION &&
00168 (event->detail == XCB_BUTTON_INDEX_4 ||
00169 event->detail == XCB_BUTTON_INDEX_5)) {
00170 DLOG("Scrolling on a window decoration\n");
00171 orientation_t orientation = (con->parent->layout == L_STACKED ? VERT : HORIZ);
00172 if (event->detail == XCB_BUTTON_INDEX_4)
00173 tree_next('p', orientation);
00174 else tree_next('n', orientation);
00175 goto done;
00176 }
00177
00178
00179 con_focus(con);
00180
00181
00182 if (floatingcon != NULL) {
00183 floating_raise_con(floatingcon);
00184
00185
00186 if (mod_pressed && event->detail == 1) {
00187 floating_drag_window(floatingcon, event);
00188 return 1;
00189 }
00190
00191
00192
00193
00194 if (mod_pressed && event->detail == 3) {
00195 DLOG("floating resize due to floatingmodifier\n");
00196 floating_resize_window(floatingcon, proportional, event);
00197 return 1;
00198 }
00199
00200 if (!in_stacked && dest == CLICK_DECORATION) {
00201
00202 DLOG("tiling resize with fallback\n");
00203 if (tiling_resize(con, event, dest))
00204 goto done;
00205 }
00206
00207 if (dest == CLICK_BORDER) {
00208 DLOG("floating resize due to border click\n");
00209 floating_resize_window(floatingcon, proportional, event);
00210 return 1;
00211 }
00212
00213
00214
00215 if (!in_stacked && dest == CLICK_DECORATION) {
00216 floating_drag_window(floatingcon, event);
00217 return 1;
00218 }
00219
00220 goto done;
00221 }
00222
00223 if (in_stacked) {
00224
00225
00226 con = con->parent;
00227 }
00228
00229
00230 if (mod_pressed && event->detail == 3) {
00231 if (floating_mod_on_tiled_client(con, event))
00232 return 1;
00233 }
00234
00235 else if (dest == CLICK_BORDER || dest == CLICK_DECORATION) {
00236 DLOG("Trying to resize (tiling)\n");
00237 tiling_resize(con, event, dest);
00238 }
00239
00240 done:
00241 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
00242 xcb_flush(conn);
00243 tree_render();
00244 return 0;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 int handle_button_press(xcb_button_press_event_t *event) {
00256 Con *con;
00257 DLOG("Button %d pressed on window 0x%08x\n", event->state, event->event);
00258
00259 const uint32_t mod = config.floating_modifier;
00260 bool mod_pressed = (mod != 0 && (event->state & mod) == mod);
00261 DLOG("floating_mod = %d, detail = %d\n", mod_pressed, event->detail);
00262 if ((con = con_by_window_id(event->event)))
00263 return route_click(con, event, mod_pressed, CLICK_INSIDE);
00264
00265 if (!(con = con_by_frame_id(event->event))) {
00266 ELOG("Clicked into unknown window?!\n");
00267 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
00268 xcb_flush(conn);
00269 return 0;
00270 }
00271
00272
00273 Con *child;
00274 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00275 if (!rect_contains(child->deco_rect, event->event_x, event->event_y))
00276 continue;
00277
00278 return route_click(child, event, mod_pressed, CLICK_DECORATION);
00279 }
00280
00281 return route_click(con, event, mod_pressed, CLICK_BORDER);
00282 }