tag: Improve tag property::index support (FS#1229)
[awesome.git] / selection.c
blob721001e45e22ae071bb3f7048c48ec9915d141ef
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 "selection.h"
24 #include "globalconf.h"
25 #include "common/atoms.h"
26 #include "event.h"
28 #include <xcb/xcb_atom.h>
29 #include <xcb/xcb_event.h>
31 static xcb_window_t selection_window = XCB_NONE;
33 /** Get the current X selection buffer.
34 * \param L The Lua VM state.
35 * \return The number of elements pushed on stack.
36 * \luastack
37 * \lreturn A string with the current X selection buffer.
39 int
40 luaA_selection_get(lua_State *L)
42 if(selection_window == XCB_NONE)
44 xcb_screen_t *screen = globalconf.screen;
45 uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
46 uint32_t values[] = { screen->black_pixel, 1, XCB_EVENT_MASK_PROPERTY_CHANGE };
48 selection_window = xcb_generate_id(globalconf.connection);
50 xcb_create_window(globalconf.connection, screen->root_depth, selection_window, screen->root,
51 0, 0, 1, 1, 0, XCB_COPY_FROM_PARENT, screen->root_visual,
52 mask, values);
55 xcb_convert_selection(globalconf.connection, selection_window,
56 XCB_ATOM_PRIMARY, UTF8_STRING, XSEL_DATA, globalconf.timestamp);
57 xcb_flush(globalconf.connection);
59 xcb_generic_event_t *event;
61 while(true)
63 event = xcb_wait_for_event(globalconf.connection);
65 if(!event)
66 return 0;
68 if(XCB_EVENT_RESPONSE_TYPE(event) != XCB_SELECTION_NOTIFY)
70 /* \todo Eventually, this may be rewritten with adding a static
71 * buffer, then a event handler for XCB_SELECTION_NOTIFY, then call
72 * xcb_event_poll_for_event_loop() and awesome_refresh(),
73 * then check if some static buffer has been filled with data.
74 * If yes, that'd be the xsel data, otherwise, re-loop.
75 * Anyway that's still brakes the socket or D-Bus, so maybe using
76 * ev_loop() would be even better.
78 event_handle(event);
79 p_delete(&event);
80 awesome_refresh();
81 continue;
84 xcb_selection_notify_event_t *event_notify =
85 (xcb_selection_notify_event_t *) event;
87 if(event_notify->selection == XCB_ATOM_PRIMARY
88 && event_notify->property != XCB_NONE)
90 xcb_icccm_get_text_property_reply_t prop;
91 xcb_get_property_cookie_t cookie =
92 xcb_icccm_get_text_property(globalconf.connection,
93 event_notify->requestor,
94 event_notify->property);
96 if(xcb_icccm_get_text_property_reply(globalconf.connection,
97 cookie, &prop, NULL))
99 lua_pushlstring(L, prop.name, prop.name_len);
101 xcb_icccm_get_text_property_reply_wipe(&prop);
103 xcb_delete_property(globalconf.connection,
104 event_notify->requestor,
105 event_notify->property);
107 p_delete(&event);
109 return 1;
111 else
112 break;
116 p_delete(&event);
117 return 0;
120 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80