1 /*
2 * GStreamer
3 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdint.h>
26 #include <stdlib.h>
27
28 #include "xcb_event_source.h"
29 #include "vkdisplay_xcb.h"
30 #include "vkwindow_xcb.h"
31
32 static gint
_compare_xcb_window(GstVulkanWindowXCB * window_xcb,xcb_window_t * window_id)33 _compare_xcb_window (GstVulkanWindowXCB * window_xcb, xcb_window_t * window_id)
34 {
35 return window_xcb->win_id - *window_id;
36 }
37
38 static GstVulkanWindowXCB *
_find_window_from_xcb_window(GstVulkanDisplayXCB * display_xcb,xcb_window_t window_id)39 _find_window_from_xcb_window (GstVulkanDisplayXCB * display_xcb,
40 xcb_window_t window_id)
41 {
42 GstVulkanDisplay *display = GST_VULKAN_DISPLAY (display_xcb);
43 GstVulkanWindowXCB *ret = NULL;
44 GList *l;
45
46 if (!window_id)
47 return NULL;
48
49 GST_OBJECT_LOCK (display);
50 l = g_list_find_custom (display->windows, &window_id,
51 (GCompareFunc) _compare_xcb_window);
52 if (l)
53 ret = gst_object_ref (l->data);
54 GST_OBJECT_UNLOCK (display);
55
56 return ret;
57 }
58
59 static gboolean
_xcb_handle_event(GstVulkanDisplayXCB * display_xcb)60 _xcb_handle_event (GstVulkanDisplayXCB * display_xcb)
61 {
62 xcb_connection_t *connection =
63 GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
64 xcb_generic_event_t *event;
65 gboolean ret = TRUE;
66
67 while ((event = xcb_poll_for_event (connection))) {
68 uint8_t event_code = event->response_type & 0x7f;
69
70 switch (event_code) {
71 case XCB_CLIENT_MESSAGE:{
72 xcb_client_message_event_t *client_event;
73 xcb_intern_atom_cookie_t cookie;
74 xcb_intern_atom_reply_t *reply;
75
76 client_event = (xcb_client_message_event_t *) event;
77 cookie = xcb_intern_atom (connection, 0, 16, "WM_DELETE_WINDOW");
78 reply = xcb_intern_atom_reply (connection, cookie, 0);
79
80 if (client_event->data.data32[0] == reply->atom) {
81 GstVulkanWindowXCB *window_xcb;
82
83 window_xcb =
84 _find_window_from_xcb_window (display_xcb, client_event->window);
85
86 if (window_xcb) {
87 GST_INFO_OBJECT (window_xcb, "Close requested");
88
89 gst_vulkan_window_close (GST_VULKAN_WINDOW (window_xcb));
90 gst_vulkan_display_remove_window (GST_VULKAN_DISPLAY (display_xcb),
91 GST_VULKAN_WINDOW (window_xcb));
92 gst_object_unref (window_xcb);
93 }
94 }
95
96 g_free (reply);
97 break;
98 }
99 case XCB_CONFIGURE_NOTIFY:{
100 xcb_configure_notify_event_t *configure_event;
101 GstVulkanWindowXCB *window_xcb;
102
103 configure_event = (xcb_configure_notify_event_t *) event;
104 window_xcb =
105 _find_window_from_xcb_window (display_xcb, configure_event->window);
106
107 if (window_xcb) {
108 gst_vulkan_window_resize (GST_VULKAN_WINDOW (window_xcb),
109 configure_event->width, configure_event->height);
110
111 gst_object_unref (window_xcb);
112 }
113 break;
114 }
115 case XCB_EXPOSE:{
116 xcb_expose_event_t *expose_event = (xcb_expose_event_t *) event;
117 GstVulkanWindowXCB *window_xcb;
118
119 /* non-zero means that other Expose follows
120 * so just wait for the last one
121 * in theory we should not receive non-zero because
122 * we have no sub areas here but just in case */
123 if (expose_event->count != 0)
124 break;
125
126 window_xcb =
127 _find_window_from_xcb_window (display_xcb, expose_event->window);
128
129 if (window_xcb) {
130 gst_vulkan_window_redraw (GST_VULKAN_WINDOW (window_xcb));
131 gst_object_unref (window_xcb);
132 }
133 break;
134 }
135 #if 0
136 case KeyPress:
137 case KeyRelease:
138 keysym = XkbKeycodeToKeysym (window_xcb->device,
139 event.xkey.keycode, 0, 0);
140 key_str = XKeysymToString (keysym);
141 key_data = g_slice_new (struct key_event);
142 key_data->window = window;
143 key_data->key_str = XKeysymToString (keysym);
144 key_data->event_type =
145 event.type == KeyPress ? "key-press" : "key-release";
146 GST_DEBUG ("input event key %d pressed over window at %d,%d (%s)",
147 event.xkey.keycode, event.xkey.x, event.xkey.y, key_str);
148 g_main_context_invoke (window->navigation_context,
149 (GSourceFunc) gst_vulkan_window_key_event_cb, key_data);
150 break;
151 case ButtonPress:
152 case ButtonRelease:
153 GST_DEBUG ("input event mouse button %d pressed over window at %d,%d",
154 event.xbutton.button, event.xbutton.x, event.xbutton.y);
155 mouse_data = g_slice_new (struct mouse_event);
156 mouse_data->window = window;
157 mouse_data->event_type =
158 event.type ==
159 ButtonPress ? "mouse-button-press" : "mouse-button-release";
160 mouse_data->button = event.xbutton.button;
161 mouse_data->posx = (double) event.xbutton.x;
162 mouse_data->posy = (double) event.xbutton.y;
163
164 g_main_context_invoke (window->navigation_context,
165 (GSourceFunc) gst_vulkan_window_mouse_event_cb, mouse_data);
166 break;
167 case MotionNotify:
168 GST_DEBUG ("input event pointer moved over window at %d,%d",
169 event.xmotion.x, event.xmotion.y);
170 mouse_data = g_slice_new (struct mouse_event);
171 mouse_data->window = window;
172 mouse_data->event_type = "mouse-move";
173 mouse_data->button = 0;
174 mouse_data->posx = (double) event.xbutton.x;
175 mouse_data->posy = (double) event.xbutton.y;
176
177 g_main_context_invoke (window->navigation_context, (GSourceFunc)
178 gst_vulkan_window_mouse_event_cb, mouse_data);
179 break;
180 #endif
181 default:
182 GST_DEBUG ("unhandled XCB type: %u", event_code);
183 break;
184 }
185
186 g_free (event);
187 }
188
189 return ret;
190 }
191
192 typedef struct _XCBEventSource
193 {
194 GSource source;
195 GPollFD pfd;
196 uint32_t mask;
197 GstVulkanDisplayXCB *display_xcb;
198 } XCBEventSource;
199
200 static gboolean
xcb_event_source_prepare(GSource * base,gint * timeout)201 xcb_event_source_prepare (GSource * base, gint * timeout)
202 {
203 *timeout = -1;
204 return FALSE;
205 }
206
207 static gboolean
xcb_event_source_check(GSource * base)208 xcb_event_source_check (GSource * base)
209 {
210 XCBEventSource *source = (XCBEventSource *) base;
211 gboolean retval;
212
213 retval = source->pfd.revents;
214
215 return retval;
216 }
217
218 static gboolean
xcb_event_source_dispatch(GSource * base,GSourceFunc callback,gpointer data)219 xcb_event_source_dispatch (GSource * base, GSourceFunc callback, gpointer data)
220 {
221 XCBEventSource *source = (XCBEventSource *) base;
222
223 gboolean ret = _xcb_handle_event (source->display_xcb);
224
225 if (callback)
226 callback (data);
227
228 return ret;
229 }
230
231 static GSourceFuncs xcb_event_source_funcs = {
232 xcb_event_source_prepare,
233 xcb_event_source_check,
234 xcb_event_source_dispatch,
235 NULL
236 };
237
238 GSource *
xcb_event_source_new(GstVulkanDisplayXCB * display_xcb)239 xcb_event_source_new (GstVulkanDisplayXCB * display_xcb)
240 {
241 xcb_connection_t *connection;
242 XCBEventSource *source;
243
244 connection = GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
245 g_return_val_if_fail (connection != NULL, NULL);
246
247 source = (XCBEventSource *)
248 g_source_new (&xcb_event_source_funcs, sizeof (XCBEventSource));
249 source->display_xcb = display_xcb;
250 source->pfd.fd = xcb_get_file_descriptor (connection);
251 source->pfd.events = G_IO_IN | G_IO_ERR;
252 g_source_add_poll (&source->source, &source->pfd);
253
254 return &source->source;
255 }
256