awful.rules: add does_match and matching_rules functions (FS#1224)
[awesome.git] / selection.c
blob979d3d5a611febce5bbed63f7818c230edf32510
1 /*
2 * selection.c - Selection handling
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
5 * Copyright © 2009 Gregor Best <farhaven@googlemail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <xcb/xcb_atom.h>
25 #include "selection.h"
26 #include "event.h"
27 #include "common/atoms.h"
28 #include "common/xutil.h"
30 static xcb_window_t selection_window = XCB_NONE;
32 /** Get the current X selection buffer.
33 * \param L The Lua VM state.
34 * \return The number of elements pushed on stack.
35 * \luastack
36 * \lreturn A string with the current X selection buffer.
38 int
39 luaA_selection_get(lua_State *L)
41 if(selection_window == XCB_NONE)
43 xcb_screen_t *screen = globalconf.screen;
44 uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
45 uint32_t values[] = { screen->black_pixel, 1, XCB_EVENT_MASK_PROPERTY_CHANGE };
47 selection_window = xcb_generate_id(globalconf.connection);
49 xcb_create_window(globalconf.connection, screen->root_depth, selection_window, screen->root,
50 0, 0, 1, 1, 0, XCB_COPY_FROM_PARENT, screen->root_visual,
51 mask, values);
54 xcb_convert_selection(globalconf.connection, selection_window,
55 XCB_ATOM_PRIMARY, UTF8_STRING, XSEL_DATA, globalconf.timestamp);
56 xcb_flush(globalconf.connection);
58 xcb_generic_event_t *event;
60 while(true)
62 event = xcb_wait_for_event(globalconf.connection);
64 if(!event)
65 return 0;
67 if(XCB_EVENT_RESPONSE_TYPE(event) != XCB_SELECTION_NOTIFY)
69 /* \todo Eventually, this may be rewritten with adding a static
70 * buffer, then a event handler for XCB_SELECTION_NOTIFY, then call
71 * xcb_event_poll_for_event_loop() and awesome_refresh(),
72 * then check if some static buffer has been filled with data.
73 * If yes, that'd be the xsel data, otherwise, re-loop.
74 * Anyway that's still brakes the socket or D-Bus, so maybe using
75 * ev_loop() would be even better.
77 event_handle(event);
78 p_delete(&event);
79 awesome_refresh();
80 continue;
83 xcb_selection_notify_event_t *event_notify =
84 (xcb_selection_notify_event_t *) event;
86 if(event_notify->selection == XCB_ATOM_PRIMARY
87 && event_notify->property != XCB_NONE)
89 xcb_icccm_get_text_property_reply_t prop;
90 xcb_get_property_cookie_t cookie =
91 xcb_icccm_get_text_property(globalconf.connection,
92 event_notify->requestor,
93 event_notify->property);
95 if(xcb_icccm_get_text_property_reply(globalconf.connection,
96 cookie, &prop, NULL))
98 lua_pushlstring(L, prop.name, prop.name_len);
100 xcb_icccm_get_text_property_reply_wipe(&prop);
102 xcb_delete_property(globalconf.connection,
103 event_notify->requestor,
104 event_notify->property);
106 p_delete(&event);
108 return 1;
110 else
111 break;
115 p_delete(&event);
116 return 0;
119 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80