1 /*
2  * libgwater-xcb - XCB GSource
3  *
4  * Copyright © 2014-2017 Quentin "Sardem FF7" Glidic
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif /* HAVE_CONFIG_H */
29 
30 #ifdef G_LOG_DOMAIN
31 #undef G_LOG_DOMAIN
32 #endif /* G_LOG_DOMAIN */
33 #define G_LOG_DOMAIN "GWaterXcb"
34 
35 #include <stdlib.h>
36 
37 #include <glib.h>
38 
39 #include <xcb/xcb.h>
40 
41 #include "libgwater-xcb.h"
42 
43 struct _GWaterXcbSource {
44     GSource source;
45     gboolean connection_owned;
46     xcb_connection_t *connection;
47     gpointer fd;
48     GQueue *queue;
49 };
50 
51 static void
_g_water_xcb_source_event_free(gpointer data)52 _g_water_xcb_source_event_free(gpointer data)
53 {
54     free(data);
55 }
56 
57 static gboolean
_g_water_xcb_source_prepare(GSource * source,gint * timeout)58 _g_water_xcb_source_prepare(GSource *source, gint *timeout)
59 {
60     GWaterXcbSource *self = (GWaterXcbSource *)source;
61     xcb_flush(self->connection);
62     *timeout = -1;
63     return ! g_queue_is_empty(self->queue);
64 }
65 
66 static gboolean
_g_water_xcb_source_check(GSource * source)67 _g_water_xcb_source_check(GSource *source)
68 {
69     GWaterXcbSource *self = (GWaterXcbSource *)source;
70 
71     GIOCondition revents;
72     revents = g_source_query_unix_fd(source, self->fd);
73 
74     if ( revents & G_IO_IN )
75     {
76         xcb_generic_event_t *event;
77 
78         if ( xcb_connection_has_error(self->connection) )
79             return TRUE;
80 
81         while ( ( event = xcb_poll_for_event(self->connection) ) != NULL )
82             g_queue_push_tail(self->queue, event);
83     }
84 
85     return ! g_queue_is_empty(self->queue);
86 }
87 
88 static gboolean
_g_water_xcb_source_dispatch(GSource * source,GSourceFunc callback,gpointer user_data)89 _g_water_xcb_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
90 {
91     GWaterXcbSource *self = (GWaterXcbSource *)source;
92     xcb_generic_event_t *event;
93 
94     gboolean ret;
95 
96     event = g_queue_pop_head(self->queue);
97     ret = ((GWaterXcbEventCallback)callback)(event, user_data);
98     _g_water_xcb_source_event_free(event);
99 
100     return ret;
101 }
102 
103 static void
_g_water_xcb_source_finalize(GSource * source)104 _g_water_xcb_source_finalize(GSource *source)
105 {
106     GWaterXcbSource *self = (GWaterXcbSource *)source;
107 
108     g_queue_free_full(self->queue, _g_water_xcb_source_event_free);
109 
110     if ( self->connection_owned )
111         xcb_disconnect(self->connection);
112 }
113 
114 static GSourceFuncs _g_water_xcb_source_funcs = {
115     .prepare  = _g_water_xcb_source_prepare,
116     .check    = _g_water_xcb_source_check,
117     .dispatch = _g_water_xcb_source_dispatch,
118     .finalize = _g_water_xcb_source_finalize,
119 };
120 
121 GWaterXcbSource *
g_water_xcb_source_new(GMainContext * context,const gchar * display,gint * screen,GWaterXcbEventCallback callback,gpointer user_data,GDestroyNotify destroy_func)122 g_water_xcb_source_new(GMainContext *context, const gchar *display, gint *screen, GWaterXcbEventCallback callback, gpointer user_data, GDestroyNotify destroy_func)
123 {
124     g_return_val_if_fail(callback != NULL, NULL);
125 
126     xcb_connection_t *connection;
127     GWaterXcbSource *self;
128 
129     connection = xcb_connect(display, screen);
130     if ( xcb_connection_has_error(connection) )
131     {
132         xcb_disconnect(connection);
133         return NULL;
134     }
135 
136     self = g_water_xcb_source_new_for_connection(context, connection, callback, user_data, destroy_func);
137     self->connection_owned = TRUE;
138     return self;
139 }
140 
141 GWaterXcbSource *
g_water_xcb_source_new_for_connection(GMainContext * context,xcb_connection_t * connection,GWaterXcbEventCallback callback,gpointer user_data,GDestroyNotify destroy_func)142 g_water_xcb_source_new_for_connection(GMainContext *context, xcb_connection_t *connection, GWaterXcbEventCallback callback, gpointer user_data, GDestroyNotify destroy_func)
143 {
144     g_return_val_if_fail(connection != NULL, NULL);
145     g_return_val_if_fail(callback != NULL, NULL);
146 
147     GSource *source;
148     GWaterXcbSource *self;
149 
150     source = g_source_new(&_g_water_xcb_source_funcs, sizeof(GWaterXcbSource));
151     self = (GWaterXcbSource *)source;
152     self->connection = connection;
153 
154     self->queue = g_queue_new();
155 
156     self->fd = g_source_add_unix_fd(source, xcb_get_file_descriptor(self->connection), G_IO_IN);
157 
158     g_source_attach(source, context);
159 
160     g_source_set_callback(source, (GSourceFunc)callback, user_data, destroy_func);
161 
162     return self;
163 }
164 
165 void
g_water_xcb_source_free(GWaterXcbSource * self)166 g_water_xcb_source_free(GWaterXcbSource *self)
167 {
168     GSource * source = (GSource *)self;
169     g_return_if_fail(self != NULL);
170 
171     g_source_destroy(source);
172 
173     g_source_unref(source);
174 }
175 
176 xcb_connection_t *
g_water_xcb_source_get_connection(GWaterXcbSource * self)177 g_water_xcb_source_get_connection(GWaterXcbSource *self)
178 {
179     g_return_val_if_fail(self != NULL, NULL);
180 
181     return self->connection;
182 }
183