/* * event.c - event handlers * * Copyright © 2007-2008 Julien Danjou * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * */ #include #include #include #include "screen.h" #include "event.h" #include "tag.h" #include "statusbar.h" #include "window.h" #include "mouse.h" #include "ewmh.h" #include "client.h" #include "widget.h" #include "titlebar.h" #include "keygrabber.h" #include "lua.h" #include "systray.h" #include "layouts/floating.h" #include "common/xscreen.h" #include "common/xutil.h" extern awesome_t globalconf; /** Handle mouse button press. * \param c The client on which the event happened or NULL. * \param button Button number * \param state Modkeys state * \param buttons Buttons list to check for */ static void event_handle_mouse_button_press(client_t *c, unsigned int button, unsigned int state, button_t *buttons) { button_t *b; for(b = buttons; b; b = b->next) if(button == b->button && CLEANMASK(state) == b->mod && b->fct) { if(c) { luaA_client_userdata_new(c); luaA_dofunction(globalconf.L, b->fct, 1); } else luaA_dofunction(globalconf.L, b->fct, 0); } } /** The button press event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_buttonpress(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_button_press_event_t *ev) { int screen, tmp; const int nb_screen = xcb_setup_roots_length(xcb_get_setup(connection)); client_t *c; widget_node_t *w; statusbar_t *statusbar; for(screen = 0; screen < globalconf.screens_info->nscreen; screen++) for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next) if(statusbar->sw && (statusbar->sw->window == ev->event || statusbar->sw->window == ev->child)) { /* If the statusbar is child, then x,y are * relative to root window */ if(statusbar->sw->window == ev->child) { ev->event_x -= statusbar->sw->geometry.x; ev->event_y -= statusbar->sw->geometry.y; } /* Need to transform coordinates like it was * top/bottom */ switch(statusbar->position) { case Right: tmp = ev->event_y; ev->event_y = statusbar->height - ev->event_x; ev->event_x = tmp; break; case Left: tmp = ev->event_y; ev->event_y = ev->event_x; ev->event_x = statusbar->width - tmp; break; default: break; } for(w = statusbar->widgets; w; w = w->next) if(ev->event_x >= w->area.x && ev->event_x < w->area.x + w->area.width && ev->event_y >= w->area.y && ev->event_y < w->area.y + w->area.height) { w->widget->button_press(w, ev, statusbar->screen, statusbar, AWESOME_TYPE_STATUSBAR); return 0; } /* return if no widget match */ return 0; } if((c = client_getbytitlebarwin(ev->event))) { /* Need to transform coordinates like it was * top/bottom */ switch(c->titlebar->position) { case Right: tmp = ev->event_y; ev->event_y = c->titlebar->height - ev->event_x; ev->event_x = tmp; break; case Left: tmp = ev->event_y; ev->event_y = ev->event_x; ev->event_x = c->titlebar->width - tmp; break; default: break; } for(w = c->titlebar->widgets; w; w = w->next) if(ev->event_x >= w->area.x && ev->event_x < w->area.x + w->area.width && ev->event_y >= w->area.y && ev->event_y < w->area.y + w->area.height) { w->widget->button_press(w, ev, c->screen, c->titlebar, AWESOME_TYPE_TITLEBAR); return 0; } /* return if no widget match */ return 0; } if((c = client_getbywin(ev->event))) { event_handle_mouse_button_press(c, ev->detail, ev->state, c->buttons); xcb_allow_events(globalconf.connection, XCB_ALLOW_REPLAY_POINTER, XCB_CURRENT_TIME); } else for(screen = 0; screen < nb_screen; screen++) if(xcb_aux_get_screen(connection, screen)->root == ev->event) { event_handle_mouse_button_press(NULL, ev->detail, ev->state, globalconf.buttons.root); return 0; } return 0; } /** The configure event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_configurerequest(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_configure_request_event_t *ev) { client_t *c; area_t geometry; if((c = client_getbywin(ev->window))) { geometry = c->geometry; if(ev->value_mask & XCB_CONFIG_WINDOW_X) geometry.x = ev->x; if(ev->value_mask & XCB_CONFIG_WINDOW_Y) geometry.y = ev->y; if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) geometry.width = ev->width; if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT) geometry.height = ev->height; if(geometry.x != c->geometry.x || geometry.y != c->geometry.y || geometry.width != c->geometry.width || geometry.height != c->geometry.height) { if(c->isfloating || layout_get_current(c->screen) == layout_floating) client_resize(c, geometry, false); else { globalconf.screens[c->screen].need_arrange = true; /* If we do not resize the client, at least tell it that it * has its new configuration. That fixes at least * gnome-terminal */ window_configure(c->win, c->geometry, c->border); } } else { titlebar_update_geometry_floating(c); window_configure(c->win, geometry, c->border); } } else { uint16_t config_win_mask = 0; uint32_t config_win_vals[7]; unsigned short i = 0; if(ev->value_mask & XCB_CONFIG_WINDOW_X) { config_win_mask |= XCB_CONFIG_WINDOW_X; config_win_vals[i++] = ev->x; } if(ev->value_mask & XCB_CONFIG_WINDOW_Y) { config_win_mask |= XCB_CONFIG_WINDOW_Y; config_win_vals[i++] = ev->y; } if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) { config_win_mask |= XCB_CONFIG_WINDOW_WIDTH; config_win_vals[i++] = ev->width; } if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT) { config_win_mask |= XCB_CONFIG_WINDOW_HEIGHT; config_win_vals[i++] = ev->height; } if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) { config_win_mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH; config_win_vals[i++] = ev->border_width; } if(ev->value_mask & XCB_CONFIG_WINDOW_SIBLING) { config_win_mask |= XCB_CONFIG_WINDOW_SIBLING; config_win_vals[i++] = ev->sibling; } if(ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) { config_win_mask |= XCB_CONFIG_WINDOW_STACK_MODE; config_win_vals[i++] = ev->stack_mode; } xcb_configure_window(connection, ev->window, config_win_mask, config_win_vals); } return 0; } /** The configure notify event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_configurenotify(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_configure_notify_event_t *ev) { int screen_nbr; const xcb_screen_t *screen; for(screen_nbr = 0; screen_nbr < xcb_setup_roots_length(xcb_get_setup (connection)); screen_nbr++) if((screen = xcb_aux_get_screen(connection, screen_nbr)) != NULL && ev->window == screen->root && (ev->width != screen->width_in_pixels || ev->height != screen->height_in_pixels)) /* it's not that we panic, but restart */ a_exec(globalconf.argv); return 0; } /** The destroy notify event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_destroynotify(void *data __attribute__ ((unused)), xcb_connection_t *connection __attribute__ ((unused)), xcb_destroy_notify_event_t *ev) { client_t *c; if((c = client_getbywin(ev->window))) client_unmanage(c); return 0; } /** The enter notify event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_enternotify(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_enter_notify_event_t *ev) { client_t *c; xembed_window_t *emwin; if(ev->mode != XCB_NOTIFY_MODE_NORMAL || (ev->root_x == globalconf.pointer_x && ev->root_y == globalconf.pointer_y)) return 0; if((c = client_getbytitlebarwin(ev->event)) || (c = client_getbywin(ev->event))) { window_grabbuttons(c->win, c->phys_screen, c->buttons); /* The idea behind saving pointer_x and pointer_y is Bob Marley powered. * this will allow us top drop some EnterNotify events and thus not giving * focus to windows appering under the cursor without a cursor move */ globalconf.pointer_x = ev->root_x; globalconf.pointer_y = ev->root_y; luaA_client_userdata_new(c); luaA_dofunction(globalconf.L, globalconf.hooks.mouseover, 1); } else if((emwin = xembed_getbywin(globalconf.embedded, ev->event))) xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, xcb_aux_get_screen(connection, emwin->phys_screen)->root, ANY_MODIFIER); else window_root_grabbuttons(ev->root); return 0; } /** The expose event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_expose(void *data __attribute__ ((unused)), xcb_connection_t *connection __attribute__ ((unused)), xcb_expose_event_t *ev) { int screen; statusbar_t *statusbar; client_t *c; if(!ev->count) { for(screen = 0; screen < globalconf.screens_info->nscreen; screen++) for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next) if(statusbar->sw && statusbar->sw->window == ev->window) { simplewindow_refresh_pixmap(statusbar->sw); return 0; } if((c = client_getbytitlebarwin(ev->window))) simplewindow_refresh_pixmap(c->titlebar->sw); } return 0; } /** The key press event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_keypress(void *data __attribute__ ((unused)), xcb_connection_t *connection __attribute__ ((unused)), xcb_key_press_event_t *ev) { xcb_keysym_t keysym; keybinding_t *k; if(globalconf.keygrabber != LUA_REFNIL) { lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber); if(keygrabber_handlekpress(globalconf.L, ev)) { if(lua_pcall(globalconf.L, 2, 1, 0)) { warn("error running function: %s", lua_tostring(globalconf.L, -1)); keygrabber_ungrab(); } else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1)) keygrabber_ungrab(); } lua_pop(globalconf.L, 1); /* pop returned value or function if not called */ } else { keysym = xcb_key_symbols_get_keysym(globalconf.keysyms, ev->detail, 0); for(k = globalconf.keys; k; k = k->next) if(((k->keycode && ev->detail == k->keycode) || (k->keysym && keysym == k->keysym)) && k->fct && CLEANMASK(k->mod) == CLEANMASK(ev->state)) luaA_dofunction(globalconf.L, k->fct, 0); } return 0; } /** The map request event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_maprequest(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_map_request_event_t *ev) { int screen_nbr = 0; client_t *c; xcb_get_window_attributes_cookie_t wa_c; xcb_get_window_attributes_reply_t *wa_r; xcb_query_pointer_cookie_t qp_c = { 0 }; xcb_query_pointer_reply_t *qp_r = NULL; xcb_get_geometry_cookie_t geom_c; xcb_get_geometry_reply_t *geom_r; xcb_screen_iterator_t iter; wa_c = xcb_get_window_attributes(connection, ev->window); if(!(wa_r = xcb_get_window_attributes_reply(connection, wa_c, NULL))) return -1; if(wa_r->override_redirect) goto bailout; if(xembed_getbywin(globalconf.embedded, ev->window)) { xcb_map_window(connection, ev->window); xembed_window_activate(connection, ev->window); } else if(!(c = client_getbywin(ev->window))) { geom_c = xcb_get_geometry(connection, ev->window); if(globalconf.screens_info->xinerama_is_active) qp_c = xcb_query_pointer(connection, xcb_aux_get_screen(globalconf.connection, screen_nbr)->root); if(!(geom_r = xcb_get_geometry_reply(connection, geom_c, NULL))) { if(globalconf.screens_info->xinerama_is_active) { qp_r = xcb_query_pointer_reply(connection, qp_c, NULL); p_delete(&qp_r); } return -1; } if(globalconf.screens_info->xinerama_is_active && (qp_r = xcb_query_pointer_reply(connection, qp_c, NULL))) { screen_nbr = screen_get_bycoord(globalconf.screens_info, screen_nbr, qp_r->root_x, qp_r->root_y); p_delete(&qp_r); } else for(iter = xcb_setup_roots_iterator(xcb_get_setup(connection)), screen_nbr = 0; iter.rem && iter.data->root != geom_r->root; xcb_screen_next (&iter), ++screen_nbr); client_manage(ev->window, geom_r, screen_nbr); p_delete(&geom_r); } bailout: p_delete(&wa_r); return 0; } /** The property notify event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_propertynotify(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_property_notify_event_t *ev) { client_t *c; xcb_window_t trans; xembed_window_t *emwin; if(ev->state == XCB_PROPERTY_DELETE) return 0; /* ignore */ if((emwin = xembed_getbywin(globalconf.embedded, ev->window))) xembed_property_update(connection, emwin); else if((c = client_getbywin(ev->window))) { if(ev->atom == WM_TRANSIENT_FOR) { xutil_get_transient_for_hint(connection, c->win, &trans); if(!c->isfloating && (c->isfloating = (client_getbywin(trans) != NULL))) globalconf.screens[c->screen].need_arrange = true; } else if (ev->atom == WM_NORMAL_HINTS) client_updatesizehints(c); else if (ev->atom == WM_HINTS) client_updatewmhints(c); if(ev->atom == WM_NAME || ev->atom == xutil_intern_atom_reply(globalconf.connection, &globalconf.atoms, xutil_intern_atom(globalconf.connection, &globalconf.atoms, "_NET_WM_NAME"))) client_updatetitle(c); } return 0; } /** The unmap notify event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_unmapnotify(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_unmap_notify_event_t *ev) { client_t *c; xembed_window_t *em; int i; /* event->send_event (Xlib) is quivalent to (ev->response_type & * 0x80) in XCB because the SendEvent bit is available in the * response_type field */ bool send_event = ((ev->response_type & 0x80) >> 7); if((c = client_getbywin(ev->window)) && ev->event == xcb_aux_get_screen(connection, c->phys_screen)->root && send_event && window_getstate(c->win) == XCB_WM_NORMAL_STATE) client_unmanage(c); /** \todo invalidate for all screen might be too much */ if((em = xembed_getbywin(globalconf.embedded, ev->window))) { xembed_window_list_detach(&globalconf.embedded, em); for(i = 0; i < globalconf.screens_info->nscreen; i++) widget_invalidate_cache(i, WIDGET_CACHE_EMBEDDED); } return 0; } /** The randr screen change notify event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_randr_screen_change_notify(void *data __attribute__ ((unused)), xcb_connection_t *connection __attribute__ ((unused)), xcb_randr_screen_change_notify_event_t *ev) { if(!globalconf.have_randr) return -1; /* Code of XRRUpdateConfiguration Xlib function ported to XCB * (only the code relevant to RRScreenChangeNotify) as the latter * doesn't provide this kind of function */ if(ev->rotation & (XCB_RANDR_ROTATION_ROTATE_90 | XCB_RANDR_ROTATION_ROTATE_270)) xcb_randr_set_screen_size(connection, ev->root, ev->height, ev->width, ev->mheight, ev->mwidth); else xcb_randr_set_screen_size(connection, ev->root, ev->width, ev->height, ev->mwidth, ev->mheight); /* XRRUpdateConfiguration also executes the following instruction * but it's not useful because SubpixelOrder is not used at all at * the moment * * XRenderSetSubpixelOrder(dpy, snum, scevent->subpixel_order); */ a_exec(globalconf.argv); return 0; } /** The client message event handler. * \param data currently unused. * \param connection The connection to the X server. * \param ev The event. */ int event_handle_clientmessage(void *data __attribute__ ((unused)), xcb_connection_t *connection, xcb_client_message_event_t *ev) { xutil_intern_atom_request_t atom_xem_q, atom_systray_q; xcb_atom_t atom_xem, atom_systray; atom_xem_q = xutil_intern_atom(connection, &globalconf.atoms, "_XEMBED"); atom_systray_q = xutil_intern_atom(connection, &globalconf.atoms, "_NET_SYSTEM_TRAY_OPCODE"); atom_xem = xutil_intern_atom_reply(globalconf.connection, &globalconf.atoms, atom_xem_q); atom_systray = xutil_intern_atom_reply(globalconf.connection, &globalconf.atoms, atom_systray_q); if(ev->type == atom_xem) return xembed_process_client_message(ev); else if(ev->type == atom_systray) return systray_process_client_message(ev); return ewmh_process_client_message(ev); } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80