2007-10-26 18:23:15 +02:00
|
|
|
/*
|
2009-09-17 17:51:06 +02:00
|
|
|
* xwindow.c - X window handling functions
|
2007-10-26 18:23:15 +02:00
|
|
|
*
|
2009-09-14 20:29:03 +02:00
|
|
|
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
|
2007-10-26 18:23:15 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_atom.h>
|
2007-10-26 18:23:15 +02:00
|
|
|
|
2009-09-17 17:51:06 +02:00
|
|
|
#include "xwindow.h"
|
2009-09-14 20:29:03 +02:00
|
|
|
#include "objects/button.h"
|
2008-06-30 18:55:14 +02:00
|
|
|
#include "common/atoms.h"
|
2007-10-26 18:23:15 +02:00
|
|
|
|
2009-08-14 16:52:22 +02:00
|
|
|
/** Mask shorthands */
|
|
|
|
#define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE)
|
|
|
|
|
2008-04-29 08:58:36 +02:00
|
|
|
/** Set client state (WM_STATE) property.
|
|
|
|
* \param win The window to set state.
|
|
|
|
* \param state The state to set.
|
2007-10-26 18:23:15 +02:00
|
|
|
*/
|
2008-03-21 16:50:17 +01:00
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_set_state(xcb_window_t win, long state)
|
2007-10-26 18:23:15 +02:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
long data[] = { state, XCB_NONE };
|
|
|
|
xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
|
2008-06-30 18:55:14 +02:00
|
|
|
WM_STATE, WM_STATE, 32, 2, data);
|
2007-10-26 18:23:15 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 18:59:34 +02:00
|
|
|
/** Send request to get a window state (WM_STATE).
|
2008-04-29 08:58:36 +02:00
|
|
|
* \param w A client window.
|
2008-08-13 18:59:34 +02:00
|
|
|
* \return The cookie associated with the request.
|
|
|
|
*/
|
|
|
|
xcb_get_property_cookie_t
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_get_state_unchecked(xcb_window_t w)
|
2008-08-13 18:59:34 +02:00
|
|
|
{
|
|
|
|
return xcb_get_property_unchecked(globalconf.connection, false, w, WM_STATE,
|
|
|
|
WM_STATE, 0L, 2L);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a window state (WM_STATE).
|
|
|
|
* \param cookie The cookie.
|
2009-04-26 13:01:37 +02:00
|
|
|
* \return The current state of the window, or 0 on error.
|
2007-10-26 18:23:15 +02:00
|
|
|
*/
|
2009-04-26 13:01:37 +02:00
|
|
|
uint32_t
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_get_state_reply(xcb_get_property_cookie_t cookie)
|
2007-10-26 18:23:15 +02:00
|
|
|
{
|
2010-03-29 11:21:20 +02:00
|
|
|
/* If no property is set, we just assume a sane default. */
|
|
|
|
uint32_t result = XCB_WM_STATE_NORMAL;
|
2008-03-21 16:50:17 +01:00
|
|
|
xcb_get_property_reply_t *prop_r;
|
|
|
|
|
2009-04-26 13:01:37 +02:00
|
|
|
if((prop_r = xcb_get_property_reply(globalconf.connection, cookie, NULL)))
|
|
|
|
{
|
|
|
|
if(xcb_get_property_value_length(prop_r))
|
|
|
|
result = *(uint32_t *) xcb_get_property_value(prop_r);
|
2008-03-21 16:50:17 +01:00
|
|
|
|
2009-04-26 13:01:37 +02:00
|
|
|
p_delete(&prop_r);
|
|
|
|
}
|
2008-03-21 16:50:17 +01:00
|
|
|
|
2007-10-26 18:23:15 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-04-29 08:58:36 +02:00
|
|
|
/** Configure a window with its new geometry and border size.
|
2008-06-13 19:17:35 +02:00
|
|
|
* \param win The X window id to configure.
|
2008-04-29 08:58:36 +02:00
|
|
|
* \param geometry The new window geometry.
|
|
|
|
* \param border The new border size.
|
2008-03-06 16:04:58 +01:00
|
|
|
*/
|
2008-03-21 16:50:17 +01:00
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_configure(xcb_window_t win, area_t geometry, int border)
|
2007-10-26 18:23:15 +02:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
xcb_configure_notify_event_t ce;
|
2007-10-26 18:23:15 +02:00
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
ce.response_type = XCB_CONFIGURE_NOTIFY;
|
2007-10-26 18:23:15 +02:00
|
|
|
ce.event = win;
|
|
|
|
ce.window = win;
|
2008-01-05 20:25:55 +01:00
|
|
|
ce.x = geometry.x;
|
|
|
|
ce.y = geometry.y;
|
|
|
|
ce.width = geometry.width;
|
|
|
|
ce.height = geometry.height;
|
2007-10-26 18:23:15 +02:00
|
|
|
ce.border_width = border;
|
2008-03-21 16:50:17 +01:00
|
|
|
ce.above_sibling = XCB_NONE;
|
|
|
|
ce.override_redirect = false;
|
|
|
|
xcb_send_event(globalconf.connection, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *) &ce);
|
2007-10-26 18:23:15 +02:00
|
|
|
}
|
|
|
|
|
2009-08-14 16:52:22 +02:00
|
|
|
/** Grab or ungrab buttons on a window.
|
|
|
|
* \param win The window.
|
|
|
|
* \param buttons The buttons to grab.
|
|
|
|
*/
|
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_buttons_grab(xcb_window_t win, button_array_t *buttons)
|
2009-08-14 16:52:22 +02:00
|
|
|
{
|
2009-10-02 16:10:50 +02:00
|
|
|
/* Ungrab everything first */
|
|
|
|
xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, win, XCB_BUTTON_MASK_ANY);
|
|
|
|
|
2009-08-14 16:52:22 +02:00
|
|
|
foreach(b, *buttons)
|
|
|
|
xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
|
|
|
|
XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
|
2009-08-17 14:10:03 +02:00
|
|
|
(*b)->button, (*b)->modifiers);
|
2009-08-14 16:52:22 +02:00
|
|
|
}
|
|
|
|
|
2009-08-31 13:54:36 +02:00
|
|
|
/** Grab key on a window.
|
|
|
|
* \param win The window.
|
|
|
|
* \param k The key.
|
|
|
|
*/
|
|
|
|
static void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_grabkey(xcb_window_t win, keyb_t *k)
|
2009-08-31 13:54:36 +02:00
|
|
|
{
|
|
|
|
if(k->keycode)
|
|
|
|
xcb_grab_key(globalconf.connection, true, win,
|
|
|
|
k->modifiers, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
|
|
|
|
else if(k->keysym)
|
|
|
|
{
|
|
|
|
xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym);
|
|
|
|
if(keycodes)
|
|
|
|
{
|
|
|
|
for(xcb_keycode_t *kc = keycodes; *kc; kc++)
|
|
|
|
xcb_grab_key(globalconf.connection, true, win,
|
|
|
|
k->modifiers, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
|
|
|
|
p_delete(&keycodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_grabkeys(xcb_window_t win, key_array_t *keys)
|
2009-08-31 13:54:36 +02:00
|
|
|
{
|
2009-10-02 16:10:50 +02:00
|
|
|
/* Ungrab everything first */
|
|
|
|
xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, win, XCB_BUTTON_MASK_ANY);
|
|
|
|
|
2009-08-31 13:54:36 +02:00
|
|
|
foreach(k, *keys)
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_grabkey(win, *k);
|
2009-08-31 13:54:36 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 02:00:58 +02:00
|
|
|
/** Get the opacity of a window.
|
2009-07-27 06:20:00 +02:00
|
|
|
* \param win The window.
|
2008-08-15 02:00:58 +02:00
|
|
|
* \return The opacity, between 0 and 1 or -1 or no opacity set.
|
|
|
|
*/
|
|
|
|
double
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_get_opacity(xcb_window_t win)
|
2008-08-15 02:00:58 +02:00
|
|
|
{
|
2009-05-25 13:39:58 +02:00
|
|
|
double ret;
|
|
|
|
|
2009-04-26 13:01:37 +02:00
|
|
|
xcb_get_property_cookie_t prop_c =
|
|
|
|
xcb_get_property_unchecked(globalconf.connection, false, win,
|
|
|
|
_NET_WM_WINDOW_OPACITY, CARDINAL, 0L, 1L);
|
2008-08-15 02:00:58 +02:00
|
|
|
|
2009-04-26 13:01:37 +02:00
|
|
|
xcb_get_property_reply_t *prop_r =
|
|
|
|
xcb_get_property_reply(globalconf.connection, prop_c, NULL);
|
2008-08-15 02:00:58 +02:00
|
|
|
|
2009-09-17 17:51:06 +02:00
|
|
|
ret = xwindow_get_opacity_from_reply(prop_r);
|
2009-05-25 13:39:58 +02:00
|
|
|
p_delete(&prop_r);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the opacity of a window.
|
2009-07-27 06:20:00 +02:00
|
|
|
* \param prop_r A reply to a get property request for _NET_WM_WINDOW_OPACITY.
|
2009-05-25 13:39:58 +02:00
|
|
|
* \return The opacity, between 0 and 1.
|
|
|
|
*/
|
2009-09-17 17:51:06 +02:00
|
|
|
double
|
|
|
|
xwindow_get_opacity_from_reply(xcb_get_property_reply_t *prop_r)
|
2009-05-25 13:39:58 +02:00
|
|
|
{
|
2009-04-13 14:17:03 +02:00
|
|
|
if(prop_r && prop_r->value_len && prop_r->format == 32)
|
2008-08-15 02:00:58 +02:00
|
|
|
{
|
2009-04-26 13:01:37 +02:00
|
|
|
uint32_t value = *(uint32_t *) xcb_get_property_value(prop_r);
|
2009-04-13 14:13:34 +02:00
|
|
|
return (double) value / (double) 0xffffffff;
|
2008-08-15 02:00:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set opacity of a window.
|
2008-04-29 08:58:36 +02:00
|
|
|
* \param win The window.
|
|
|
|
* \param opacity Opacity of the window, between 0 and 1.
|
|
|
|
*/
|
2008-03-21 16:50:17 +01:00
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_set_opacity(xcb_window_t win, double opacity)
|
2007-10-26 18:23:15 +02:00
|
|
|
{
|
2009-09-30 15:59:02 +02:00
|
|
|
if(win)
|
2007-10-26 18:23:15 +02:00
|
|
|
{
|
2009-09-30 15:59:02 +02:00
|
|
|
if(opacity >= 0 && opacity <= 1)
|
|
|
|
{
|
|
|
|
uint32_t real_opacity = opacity * 0xffffffff;
|
|
|
|
xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
|
|
|
|
_NET_WM_WINDOW_OPACITY, CARDINAL, 32, 1L, &real_opacity);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
|
2007-10-26 18:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 15:58:09 +02:00
|
|
|
/** Send WM_TAKE_FOCUS client message to window
|
|
|
|
* \param win destination window
|
|
|
|
*/
|
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_takefocus(xcb_window_t win)
|
2009-04-07 15:58:09 +02:00
|
|
|
{
|
|
|
|
xcb_client_message_event_t ev;
|
|
|
|
|
|
|
|
/* Initialize all of event's fields first */
|
|
|
|
p_clear(&ev, 1);
|
|
|
|
|
|
|
|
ev.response_type = XCB_CLIENT_MESSAGE;
|
|
|
|
ev.window = win;
|
|
|
|
ev.format = 32;
|
|
|
|
ev.data.data32[1] = XCB_CURRENT_TIME;
|
|
|
|
ev.type = WM_PROTOCOLS;
|
|
|
|
ev.data.data32[0] = WM_TAKE_FOCUS;
|
|
|
|
|
|
|
|
xcb_send_event(globalconf.connection, false, win,
|
|
|
|
XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
|
|
|
|
}
|
|
|
|
|
2009-10-02 16:33:39 +02:00
|
|
|
/** Set window cursor.
|
|
|
|
* \param w The window.
|
2009-08-05 15:56:00 +02:00
|
|
|
* \param c The cursor.
|
|
|
|
*/
|
|
|
|
void
|
2009-09-17 17:51:06 +02:00
|
|
|
xwindow_set_cursor(xcb_window_t w, xcb_cursor_t c)
|
2009-08-05 15:56:00 +02:00
|
|
|
{
|
|
|
|
xcb_change_window_attributes(globalconf.connection, w, XCB_CW_CURSOR,
|
|
|
|
(const uint32_t[]) { c });
|
|
|
|
}
|
|
|
|
|
2009-10-02 16:33:39 +02:00
|
|
|
/** Set a window border color.
|
|
|
|
* \param w The window.
|
|
|
|
* \param color The color.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
xwindow_set_border_color(xcb_window_t w, xcolor_t *color)
|
|
|
|
{
|
|
|
|
if(w)
|
|
|
|
xcb_change_window_attributes(globalconf.connection, w, XCB_CW_BORDER_PIXEL, &color->pixel);
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|