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"
28 #include "common/atoms.h"
30 extern awesome_t globalconf
;
32 static xcb_window_t selection_window
= XCB_NONE
;
34 /** Get the current X selection buffer.
35 * \param L The Lua VM state.
36 * \return The number of elements pushed on stack.
38 * \lreturn A string with the current X selection buffer.
41 luaA_selection_get(lua_State
*L
)
43 if(selection_window
== XCB_NONE
)
45 xcb_screen_t
*screen
= xutil_screen_get(globalconf
.connection
, globalconf
.default_screen
);
46 uint32_t mask
= XCB_CW_BACK_PIXEL
| XCB_CW_OVERRIDE_REDIRECT
| XCB_CW_EVENT_MASK
;
47 uint32_t values
[] = { screen
->black_pixel
, 1, XCB_EVENT_MASK_PROPERTY_CHANGE
};
49 selection_window
= xcb_generate_id(globalconf
.connection
);
51 xcb_create_window(globalconf
.connection
, screen
->root_depth
, selection_window
, screen
->root
,
52 0, 0, 1, 1, 0, XCB_COPY_FROM_PARENT
, screen
->root_visual
,
56 xcb_convert_selection(globalconf
.connection
, selection_window
,
57 PRIMARY
, UTF8_STRING
, XSEL_DATA
, XCB_CURRENT_TIME
);
58 xcb_flush(globalconf
.connection
);
60 xcb_generic_event_t
*event
;
64 event
= xcb_wait_for_event(globalconf
.connection
);
69 if(XCB_EVENT_RESPONSE_TYPE(event
) != XCB_SELECTION_NOTIFY
)
71 /* \todo Eventually, this may be rewritten with adding a static
72 * buffer, then a event handler for XCB_SELECTION_NOTIFY, then call
73 * xcb_event_poll_for_event_loop() and awesome_refresh(),
74 * then check if some static buffer has been filled with data.
75 * If yes, that'd be the xsel data, otherwise, re-loop.
76 * Anyway that's still brokes the socket or D-Bus, so maybe using
77 * ev_loop() would be even better.
79 xcb_event_handle(&globalconf
.evenths
, event
);
81 awesome_refresh(globalconf
.connection
);
85 xcb_selection_notify_event_t
*event_notify
=
86 (xcb_selection_notify_event_t
*) event
;
88 if(event_notify
->selection
== PRIMARY
89 && event_notify
->property
!= XCB_NONE
)
91 xcb_get_text_property_reply_t prop
;
92 xcb_get_property_cookie_t cookie
=
93 xcb_get_text_property(globalconf
.connection
,
94 event_notify
->requestor
,
95 event_notify
->property
);
97 if(xcb_get_text_property_reply(globalconf
.connection
,
100 lua_pushlstring(L
, prop
.name
, prop
.name_len
);
102 xcb_get_text_property_reply_wipe(&prop
);
104 xcb_delete_property(globalconf
.connection
,
105 event_notify
->requestor
,
106 event_notify
->property
);