1 /*
2  * libgwater-wayland - Wayland 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 "GWaterWaylandServer"
34 
35 #include <errno.h>
36 
37 #include <glib.h>
38 
39 #include <wayland-server-core.h>
40 
41 #include "libgwater-wayland-server.h"
42 
43 struct _GWaterWaylandServerSource {
44     GSource source;
45     gboolean display_owned;
46     struct wl_display *display;
47     struct wl_event_loop *loop;
48     gpointer fd;
49 };
50 
51 static gboolean
_g_water_wayland_server_source_prepare(GSource * source,gint * timeout)52 _g_water_wayland_server_source_prepare(GSource *source, gint *timeout)
53 {
54     GWaterWaylandServerSource *self = (GWaterWaylandServerSource *)source;
55 
56     wl_display_flush_clients(self->display);
57 
58     *timeout = -1;
59     return FALSE;
60 }
61 
62 static gboolean
_g_water_wayland_server_source_check(GSource * source)63 _g_water_wayland_server_source_check(GSource *source)
64 {
65     GWaterWaylandServerSource *self = (GWaterWaylandServerSource *)source;
66 
67     GIOCondition revents;
68     revents = g_source_query_unix_fd(source, self->fd);
69 
70     return ( revents > 0 );
71 }
72 
73 static gboolean
_g_water_wayland_server_source_dispatch(GSource * source,GSourceFunc callback,gpointer user_data)74 _g_water_wayland_server_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
75 {
76     GWaterWaylandServerSource *self = (GWaterWaylandServerSource *)source;
77     GIOCondition revents;
78 
79     revents = g_source_query_unix_fd(source, self->fd);
80     if ( revents & G_IO_IN )
81     {
82         if ( wl_event_loop_dispatch(self->loop, 0) < 0 )
83         {
84             if ( callback != NULL )
85                 return callback(user_data);
86             return FALSE;
87         }
88     }
89 
90     errno = 0;
91     if ( revents & (G_IO_ERR | G_IO_HUP) )
92     {
93         if ( callback != NULL )
94             return callback(user_data);
95         return FALSE;
96     }
97 
98     return TRUE;
99 }
100 
101 static void
_g_water_wayland_server_source_finalize(GSource * source)102 _g_water_wayland_server_source_finalize(GSource *source)
103 {
104     GWaterWaylandServerSource *self = (GWaterWaylandServerSource *)source;
105 
106     if ( self->display_owned )
107         wl_display_destroy(self->display);
108 }
109 
110 static GSourceFuncs _g_water_wayland_server_source_funcs = {
111     .prepare  = _g_water_wayland_server_source_prepare,
112     .check    = _g_water_wayland_server_source_check,
113     .dispatch = _g_water_wayland_server_source_dispatch,
114     .finalize = _g_water_wayland_server_source_finalize,
115 };
116 
117 GWaterWaylandServerSource *
g_water_wayland_server_source_new(GMainContext * context)118 g_water_wayland_server_source_new(GMainContext *context)
119 {
120     struct wl_display *display;
121     GWaterWaylandServerSource *self;
122 
123     display = wl_display_create();
124     if ( display == NULL )
125         return NULL;
126 
127     self = g_water_wayland_server_source_new_for_display(context, display);
128     self->display_owned = TRUE;
129     return self;
130 }
131 
132 GWaterWaylandServerSource *
g_water_wayland_server_source_new_for_display(GMainContext * context,struct wl_display * display)133 g_water_wayland_server_source_new_for_display(GMainContext *context, struct wl_display *display)
134 {
135     g_return_val_if_fail(display != NULL, NULL);
136 
137     GSource *source;
138     GWaterWaylandServerSource *self;
139 
140     source = g_source_new(&_g_water_wayland_server_source_funcs, sizeof(GWaterWaylandServerSource));
141     self = (GWaterWaylandServerSource *)source;
142     self->display = display;
143     self->loop = wl_display_get_event_loop(self->display);
144 
145     self->fd = g_source_add_unix_fd(source, wl_event_loop_get_fd(self->loop), G_IO_IN | G_IO_ERR | G_IO_HUP);
146 
147     g_source_attach(source, context);
148 
149     return self;
150 }
151 
152 void
g_water_wayland_server_source_free(GWaterWaylandServerSource * self)153 g_water_wayland_server_source_free(GWaterWaylandServerSource *self)
154 {
155     GSource * source = (GSource *)self;
156     g_return_if_fail(self != NULL);
157 
158     g_source_destroy(source);
159 
160     g_source_unref(source);
161 }
162 
163 void
g_water_wayland_server_source_set_error_callback(GWaterWaylandServerSource * self,GSourceFunc callback,gpointer user_data,GDestroyNotify destroy_notify)164 g_water_wayland_server_source_set_error_callback(GWaterWaylandServerSource *self, GSourceFunc callback, gpointer user_data, GDestroyNotify destroy_notify)
165 {
166     g_return_if_fail(self != NULL);
167 
168     g_source_set_callback((GSource *)self, callback, user_data, destroy_notify);
169 }
170 
171 struct wl_display *
g_water_wayland_server_source_get_display(GWaterWaylandServerSource * self)172 g_water_wayland_server_source_get_display(GWaterWaylandServerSource *self)
173 {
174     g_return_val_if_fail(self != NULL, NULL);
175 
176     return self->display;
177 }
178