i3
|
00001 /* 00002 * vim:ts=8:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * 00006 * © 2009-2010 Michael Stapelberg and contributors 00007 * 00008 * See file LICENSE for license information. 00009 * 00010 * src/click.c: Contains the handlers for button press (mouse click) events 00011 * because they are quite large. 00012 * 00013 */ 00014 #include <stdio.h> 00015 #include <assert.h> 00016 #include <string.h> 00017 #include <stdlib.h> 00018 #include <time.h> 00019 #include <stdbool.h> 00020 #include <math.h> 00021 00022 #include <xcb/xcb.h> 00023 #include <xcb/xcb_atom.h> 00024 #include <xcb/xcb_icccm.h> 00025 00026 #include <X11/XKBlib.h> 00027 00028 #include "i3.h" 00029 #include "queue.h" 00030 #include "table.h" 00031 #include "config.h" 00032 #include "util.h" 00033 #include "xcb.h" 00034 #include "client.h" 00035 #include "workspace.h" 00036 #include "commands.h" 00037 #include "floating.h" 00038 #include "resize.h" 00039 #include "log.h" 00040 #include "randr.h" 00041 00042 static struct Stack_Window *get_stack_window(xcb_window_t window_id) { 00043 struct Stack_Window *current; 00044 00045 SLIST_FOREACH(current, &stack_wins, stack_windows) { 00046 if (current->window != window_id) 00047 continue; 00048 00049 return current; 00050 } 00051 00052 return NULL; 00053 } 00054 00055 /* 00056 * Checks if the button press was on a stack window, handles focus setting and returns true 00057 * if so, or false otherwise. 00058 * 00059 */ 00060 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) { 00061 struct Stack_Window *stack_win; 00062 00063 /* If we find a corresponding stack window, we can handle the event */ 00064 if ((stack_win = get_stack_window(event->event)) == NULL) 00065 return false; 00066 00067 /* A stack window was clicked, we check if it was button4 or button5 00068 which are scroll up / scroll down. */ 00069 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) { 00070 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN); 00071 focus_window_in_container(conn, CUR_CELL, direction); 00072 return true; 00073 } 00074 00075 /* It was no scrolling, so we calculate the destination client by 00076 dividing the Y position of the event through the height of a window 00077 decoration and then set the focus to this client. */ 00078 i3Font *font = load_font(conn, config.font); 00079 int decoration_height = (font->height + 2 + 2); 00080 int destination = (event->event_y / decoration_height), 00081 c = 0, 00082 num_clients = 0; 00083 Client *client; 00084 Container *container = stack_win->container; 00085 00086 CIRCLEQ_FOREACH(client, &(container->clients), clients) 00087 num_clients++; 00088 00089 /* If we don’t have any clients in this container, we cannot do 00090 * anything useful anyways. */ 00091 if (num_clients == 0) 00092 return true; 00093 00094 if (container->mode == MODE_TABBED) 00095 destination = (event->event_x / (container->width / num_clients)); 00096 else if (container->mode == MODE_STACK && 00097 container->stack_limit != STACK_LIMIT_NONE) { 00098 if (container->stack_limit == STACK_LIMIT_COLS) { 00099 int wrap = ceil((float)num_clients / container->stack_limit_value); 00100 int clicked_column = (event->event_x / (stack_win->rect.width / container->stack_limit_value)); 00101 int clicked_row = (event->event_y / decoration_height); 00102 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row); 00103 destination = (wrap * clicked_column) + clicked_row; 00104 } else { 00105 int width = (stack_win->rect.width / ceil((float)num_clients / container->stack_limit_value)); 00106 int clicked_column = (event->event_x / width); 00107 int clicked_row = (event->event_y / decoration_height); 00108 DLOG("clicked on column %d, row %d\n", clicked_column, clicked_row); 00109 destination = (container->stack_limit_value * clicked_column) + clicked_row; 00110 } 00111 } 00112 00113 DLOG("Click on stack_win for client %d\n", destination); 00114 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients) 00115 if (c++ == destination) { 00116 set_focus(conn, client, true); 00117 return true; 00118 } 00119 00120 return true; 00121 } 00122 00123 /* 00124 * Checks if the button press was on a bar, switches to the workspace and returns true 00125 * if so, or false otherwise. 00126 * 00127 */ 00128 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) { 00129 Output *output; 00130 TAILQ_FOREACH(output, &outputs, outputs) { 00131 if (output->bar != event->event) 00132 continue; 00133 00134 DLOG("Click on a bar\n"); 00135 00136 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */ 00137 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) { 00138 Workspace *ws = c_ws; 00139 if (event->detail == XCB_BUTTON_INDEX_5) { 00140 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) { 00141 if (ws->output == output) { 00142 workspace_show(conn, ws->num + 1); 00143 return true; 00144 } 00145 } 00146 } else { 00147 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) { 00148 if (ws->output == output) { 00149 workspace_show(conn, ws->num + 1); 00150 return true; 00151 } 00152 } 00153 } 00154 return true; 00155 } 00156 int drawn = 0; 00157 /* Because workspaces can be on different outputs, we need to loop 00158 through all of them and decide to count it based on its ->output */ 00159 Workspace *ws; 00160 TAILQ_FOREACH(ws, workspaces, workspaces) { 00161 if (ws->output != output) 00162 continue; 00163 DLOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n", 00164 ws->num, drawn, ws->text_width); 00165 if (event->event_x > (drawn + 1) && 00166 event->event_x <= (drawn + 1 + ws->text_width + 5 + 5)) { 00167 workspace_show(conn, ws->num + 1); 00168 return true; 00169 } 00170 00171 drawn += ws->text_width + 5 + 5 + 2; 00172 } 00173 return true; 00174 } 00175 00176 return false; 00177 } 00178 00179 /* 00180 * Called when the user clicks using the floating_modifier, but the client is in 00181 * tiling layout. 00182 * 00183 * Returns false if it does not do anything (that is, the click should be sent 00184 * to the client). 00185 * 00186 */ 00187 static bool floating_mod_on_tiled_client(xcb_connection_t *conn, Client *client, 00188 xcb_button_press_event_t *event) { 00189 /* Only the right mouse button is interesting for us at the moment */ 00190 if (event->detail != 3) 00191 return false; 00192 00193 /* The client is in tiling layout. We can still 00194 * initiate a resize with the right mouse button, 00195 * by chosing the border which is the most near one 00196 * to the position of the mouse pointer */ 00197 int to_right = client->rect.width - event->event_x, 00198 to_left = event->event_x, 00199 to_top = event->event_y, 00200 to_bottom = client->rect.height - event->event_y; 00201 resize_orientation_t orientation = O_VERTICAL; 00202 Container *con = client->container; 00203 Workspace *ws = con->workspace; 00204 int first = 0, second = 0; 00205 00206 DLOG("click was %d px to the right, %d px to the left, %d px to top, %d px to bottom\n", 00207 to_right, to_left, to_top, to_bottom); 00208 00209 if (to_right < to_left && 00210 to_right < to_top && 00211 to_right < to_bottom) { 00212 /* …right border */ 00213 first = con->col + (con->colspan - 1); 00214 DLOG("column %d\n", first); 00215 00216 if (!cell_exists(ws, first, con->row) || 00217 (first == (ws->cols-1))) 00218 return false; 00219 00220 second = first + 1; 00221 } else if (to_left < to_right && 00222 to_left < to_top && 00223 to_left < to_bottom) { 00224 /* …left border */ 00225 if (con->col == 0) 00226 return false; 00227 00228 first = con->col - 1; 00229 second = con->col; 00230 } else if (to_top < to_right && 00231 to_top < to_left && 00232 to_top < to_bottom) { 00233 /* This was a press on the top border */ 00234 if (con->row == 0) 00235 return false; 00236 first = con->row - 1; 00237 second = con->row; 00238 orientation = O_HORIZONTAL; 00239 } else if (to_bottom < to_right && 00240 to_bottom < to_left && 00241 to_bottom < to_top) { 00242 /* …bottom border */ 00243 first = con->row + (con->rowspan - 1); 00244 if (!cell_exists(ws, con->col, first) || 00245 (first == (ws->rows-1))) 00246 return false; 00247 00248 second = first + 1; 00249 orientation = O_HORIZONTAL; 00250 } 00251 00252 return resize_graphical_handler(conn, ws, first, second, orientation, event); 00253 } 00254 00255 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) { 00256 DLOG("Button %d pressed\n", event->state); 00257 /* This was either a focus for a client’s parent (= titlebar)… */ 00258 Client *client = table_get(&by_child, event->event); 00259 bool border_click = false; 00260 if (client == NULL) { 00261 client = table_get(&by_parent, event->event); 00262 border_click = true; 00263 } 00264 /* See if this was a click with the configured modifier. If so, we need 00265 * to move around the client if it was floating. if not, we just process 00266 * as usual. */ 00267 if (config.floating_modifier != 0 && 00268 (event->state & config.floating_modifier) == config.floating_modifier) { 00269 if (client == NULL) { 00270 DLOG("Not handling, floating_modifier was pressed and no client found\n"); 00271 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time); 00272 xcb_flush(conn); 00273 return 1; 00274 } 00275 if (client->fullscreen) { 00276 DLOG("Not handling, client is in fullscreen mode\n"); 00277 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time); 00278 xcb_flush(conn); 00279 return 1; 00280 } 00281 if (client_is_floating(client)) { 00282 DLOG("button %d pressed\n", event->detail); 00283 if (event->detail == 1) { 00284 DLOG("left mouse button, dragging\n"); 00285 floating_drag_window(conn, client, event); 00286 } else if (event->detail == 3) { 00287 bool proportional = (event->state & BIND_SHIFT); 00288 DLOG("right mouse button\n"); 00289 floating_resize_window(conn, client, proportional, event); 00290 } 00291 return 1; 00292 } 00293 00294 if (!floating_mod_on_tiled_client(conn, client, event)) { 00295 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time); 00296 xcb_flush(conn); 00297 } 00298 00299 return 1; 00300 } 00301 00302 if (client == NULL) { 00303 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */ 00304 if (button_press_stackwin(conn, event)) 00305 return 1; 00306 00307 /* Or on a bar? */ 00308 if (button_press_bar(conn, event)) 00309 return 1; 00310 00311 DLOG("Could not handle this button press\n"); 00312 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time); 00313 xcb_flush(conn); 00314 return 1; 00315 } 00316 00317 /* Set focus in any case */ 00318 set_focus(conn, client, false); 00319 00320 /* Let’s see if this was on the borders (= resize). If not, we’re done */ 00321 DLOG("press button on x=%d, y=%d\n", event->event_x, event->event_y); 00322 resize_orientation_t orientation = O_VERTICAL; 00323 Container *con = client->container; 00324 int first, second; 00325 00326 if (client->dock) { 00327 DLOG("dock. done.\n"); 00328 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time); 00329 xcb_flush(conn); 00330 return 1; 00331 } 00332 00333 DLOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width); 00334 00335 /* Some clients (xfontsel for example) seem to pass clicks on their 00336 * window to the parent window, thus we receive an event here which in 00337 * reality is a border_click. Check for the position and fix state. */ 00338 if (border_click && 00339 event->event_x >= client->child_rect.x && 00340 event->event_x <= (client->child_rect.x + client->child_rect.width) && 00341 event->event_y >= client->child_rect.y && 00342 event->event_y <= (client->child_rect.y + client->child_rect.height)) { 00343 DLOG("Fixing border_click = false because of click in child\n"); 00344 border_click = false; 00345 } 00346 00347 if (!border_click) { 00348 DLOG("client. done.\n"); 00349 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time); 00350 /* Floating clients should be raised on click */ 00351 if (client_is_floating(client)) 00352 xcb_raise_window(conn, client->frame); 00353 xcb_flush(conn); 00354 return 1; 00355 } 00356 00357 /* Don’t handle events inside the titlebar, only borders are interesting */ 00358 i3Font *font = load_font(conn, config.font); 00359 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) { 00360 DLOG("click on titlebar\n"); 00361 00362 /* Floating clients can be dragged by grabbing their titlebar */ 00363 if (client_is_floating(client)) { 00364 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */ 00365 xcb_raise_window(conn, client->frame); 00366 xcb_flush(conn); 00367 00368 floating_drag_window(conn, client, event); 00369 } 00370 return 1; 00371 } 00372 00373 if (client_is_floating(client)) 00374 return floating_border_click(conn, client, event); 00375 00376 Workspace *ws = con->workspace; 00377 00378 if (event->event_y < 2) { 00379 /* This was a press on the top border */ 00380 if (con->row == 0) 00381 return 1; 00382 first = con->row - 1; 00383 second = con->row; 00384 orientation = O_HORIZONTAL; 00385 } else if (event->event_y >= (client->rect.height - 2)) { 00386 /* …bottom border */ 00387 first = con->row + (con->rowspan - 1); 00388 if (!cell_exists(ws, con->col, first) || 00389 (first == (ws->rows-1))) 00390 return 1; 00391 00392 second = first + 1; 00393 orientation = O_HORIZONTAL; 00394 } else if (event->event_x <= 2) { 00395 /* …left border */ 00396 if (con->col == 0) 00397 return 1; 00398 00399 first = con->col - 1; 00400 second = con->col; 00401 } else if (event->event_x > 2) { 00402 /* …right border */ 00403 first = con->col + (con->colspan - 1); 00404 DLOG("column %d\n", first); 00405 00406 if (!cell_exists(ws, first, con->row) || 00407 (first == (ws->cols-1))) 00408 return 1; 00409 00410 second = first + 1; 00411 } 00412 00413 return resize_graphical_handler(conn, ws, first, second, orientation, event); 00414 }