Debian Codegraph @

clutter/x11/clutter-stage-x11.c

raw
1 /* Clutter.
2 * An OpenGL based 'interactive canvas' library.
3 * Authored By Matthew Allum <[email protected]>
4 * Copyright (C) 2006-2007 OpenedHand
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 */
21
22 #include "config.h"
23
24 #include <math.h>
25 #include <stdlib.h>
26
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include <cogl/cogl.h>
32
33 #include "clutter-backend-x11.h"
34 #include "clutter-stage-x11.h"
35 #include "clutter-x11.h"
36
37 #include "clutter-actor-private.h"
38 #include "clutter-debug.h"
39 #include "clutter-device-manager-private.h"
40 #include "clutter-enum-types.h"
41 #include "clutter-event-translator.h"
42 #include "clutter-event-private.h"
43 #include "clutter-feature.h"
44 #include "clutter-main.h"
45 #include "clutter-paint-volume-private.h"
46 #include "clutter-private.h"
47 #include "clutter-stage-private.h"
48
49 #define STAGE_X11_IS_MAPPED(s) ((((ClutterStageX11 *) (s))->wm_state & STAGE_X11_WITHDRAWN) == 0)
50
51 static ClutterStageWindowIface *clutter_stage_window_parent_iface = NULL;
52
53 static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface);
54 static void clutter_event_translator_iface_init (ClutterEventTranslatorIface *iface);
55
56 static ClutterStageCogl *clutter_x11_get_stage_window_from_window (Window win);
57
58 static GHashTable *clutter_stages_by_xid = NULL;
59
60 #define clutter_stage_x11_get_type _clutter_stage_x11_get_type
61
62 G_DEFINE_TYPE_WITH_CODE (ClutterStageX11,
63 clutter_stage_x11,
64 CLUTTER_TYPE_STAGE_COGL,
65 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW,
66 clutter_stage_window_iface_init)
67 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_EVENT_TRANSLATOR,
68 clutter_event_translator_iface_init));
69
70 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
71 #define _NET_WM_STATE_ADD 1 /* add/set property */
72 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
73
74 static void
75 send_wmspec_change_state (ClutterBackendX11 *backend_x11,
76 Window window,
77 Atom state,
78 gboolean add)
79 {
80 XClientMessageEvent xclient;
81
82 CLUTTER_NOTE (BACKEND, "%s NET_WM state", add ? "adding" : "removing");
83
84 memset (&xclient, 0, sizeof (xclient));
85
86 xclient.type = ClientMessage;
87 xclient.window = window;
88 xclient.message_type = backend_x11->atom_NET_WM_STATE;
89 xclient.format = 32;
90
91 xclient.data.l[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
92 xclient.data.l[1] = state;
93 xclient.data.l[2] = 0;
94 xclient.data.l[3] = 0;
95 xclient.data.l[4] = 0;
96
97 XSendEvent (backend_x11->xdpy,
98 DefaultRootWindow (backend_x11->xdpy),
99 False,
100 SubstructureRedirectMask | SubstructureNotifyMask,
101 (XEvent *)&xclient);
102 }
103
104 static void
105 update_state (ClutterStageX11 *stage_x11,
106 ClutterBackendX11 *backend_x11,
107 Atom *state,
108 gboolean add)
109 {
110 if (add)
111 {
112 /* FIXME: This wont work if we support more states */
113 XChangeProperty (backend_x11->xdpy,
114 stage_x11->xwin,
115 backend_x11->atom_NET_WM_STATE, XA_ATOM, 32,
116 PropModeReplace,
117 (unsigned char *) state, 1);
118 }
119 else
120 {
121 /* FIXME: This wont work if we support more states */
122 XDeleteProperty (backend_x11->xdpy,
123 stage_x11->xwin,
124 backend_x11->atom_NET_WM_STATE);
125 }
126 }
127
128 static void
129 clutter_stage_x11_fix_window_size (ClutterStageX11 *stage_x11,
130 gint new_width,
131 gint new_height)
132 {
133 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
134 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
135
136 if (stage_x11->xwin != None && !stage_x11->is_foreign_xwin)
137 {
138 guint min_width, min_height;
139 XSizeHints *size_hints;
140 gboolean resize;
141
142 resize = clutter_stage_get_user_resizable (stage_cogl->wrapper);
143
144 size_hints = XAllocSizeHints();
145
146 clutter_stage_get_minimum_size (stage_cogl->wrapper,
147 &min_width,
148 &min_height);
149
150 if (new_width <= 0)
151 new_width = min_width * stage_x11->scale_factor;
152
153 if (new_height <= 0)
154 new_height = min_height * stage_x11->scale_factor;
155
156 size_hints->flags = 0;
157
158 /* If we are going fullscreen then we don't want any
159 restrictions on the window size */
160 if (!stage_x11->fullscreening)
161 {
162 if (resize)
163 {
164 size_hints->min_width = min_width * stage_x11->scale_factor;
165 size_hints->min_height = min_height * stage_x11->scale_factor;
166 size_hints->flags = PMinSize;
167 }
168 else
169 {
170 size_hints->min_width = new_width;
171 size_hints->min_height = new_height;
172 size_hints->max_width = new_width;
173 size_hints->max_height = new_height;
174 size_hints->flags = PMinSize | PMaxSize;
175 }
176 }
177
178 XSetWMNormalHints (backend_x11->xdpy, stage_x11->xwin, size_hints);
179
180 XFree(size_hints);
181 }
182 }
183
184 static void
185 clutter_stage_x11_set_wm_protocols (ClutterStageX11 *stage_x11)
186 {
187 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
188 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
189 Atom protocols[2];
190 int n = 0;
191
192 protocols[n++] = backend_x11->atom_WM_DELETE_WINDOW;
193 protocols[n++] = backend_x11->atom_NET_WM_PING;
194
195 XSetWMProtocols (backend_x11->xdpy, stage_x11->xwin, protocols, n);
196 }
197
198 static void
199 clutter_stage_x11_get_geometry (ClutterStageWindow *stage_window,
200 cairo_rectangle_int_t *geometry)
201 {
202 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
203 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
204 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
205
206 geometry->x = geometry->y = 0;
207
208 /* If we're fullscreen, return the size of the display.
209 *
210 * FIXME - this is utterly broken for anything that is not a single
211 * head set up; the window manager will give us the right size in a
212 * ConfigureNotify, but between the fullscreen signal emission on the
213 * stage and the following frame, the size returned by the stage will
214 * be wrong.
215 */
216 if (_clutter_stage_is_fullscreen (stage_cogl->wrapper) &&
217 stage_x11->fullscreening)
218 {
219 geometry->width = DisplayWidth (backend_x11->xdpy, backend_x11->xscreen_num);
220 geometry->height = DisplayHeight (backend_x11->xdpy, backend_x11->xscreen_num);
221
222 return;
223 }
224
225 geometry->width = stage_x11->xwin_width / stage_x11->scale_factor;
226 geometry->height = stage_x11->xwin_height / stage_x11->scale_factor;
227 }
228
229 static void
230 clutter_stage_x11_resize (ClutterStageWindow *stage_window,
231 gint width,
232 gint height)
233 {
234 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
235 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
236 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
237
238 if (stage_x11->is_foreign_xwin)
239 {
240 /* If this is a foreign window we won't get a ConfigureNotify,
241 * so we need to manually set the size and queue a relayout on the
242 * stage here (as is normally done in response to ConfigureNotify).
243 */
244 stage_x11->xwin_width = width * stage_x11->scale_factor;
245 stage_x11->xwin_height = height * stage_x11->scale_factor;
246 clutter_actor_queue_relayout (CLUTTER_ACTOR (stage_cogl->wrapper));
247 return;
248 }
249
250 /* If we're going fullscreen, don't mess with the size */
251 if (stage_x11->fullscreening)
252 return;
253
254 if (width == 0 || height == 0)
255 {
256 /* Should not happen, if this turns up we need to debug it and
257 * determine the cleanest way to fix.
258 */
259 g_warning ("X11 stage not allowed to have 0 width or height");
260 width = 1;
261 height = 1;
262 }
263
264 CLUTTER_NOTE (BACKEND, "New size received: (%d, %d)", width, height);
265
266 width *= stage_x11->scale_factor;
267 height *= stage_x11->scale_factor;
268
269 if (stage_x11->xwin != None)
270 {
271 clutter_stage_x11_fix_window_size (stage_x11, width, height);
272
273 if (width != stage_x11->xwin_width ||
274 height != stage_x11->xwin_height)
275 {
276 CLUTTER_NOTE (BACKEND, "%s: XResizeWindow[%x] (%d, %d)",
277 G_STRLOC,
278 (unsigned int) stage_x11->xwin,
279 width,
280 height);
281
282 /* XXX: in this case we can rely on a subsequent
283 * ConfigureNotify that will result in the stage
284 * being reallocated so we don't actively do anything
285 * to affect the stage allocation here. */
286 XResizeWindow (backend_x11->xdpy,
287 stage_x11->xwin,
288 width,
289 height);
290 }
291 }
292 else
293 {
294 /* if the backing window hasn't been created yet, we just
295 * need to store the new window size
296 */
297 stage_x11->xwin_width = width;
298 stage_x11->xwin_height = height;
299 }
300 }
301
302 static inline void
303 set_wm_pid (ClutterStageX11 *stage_x11)
304 {
305 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
306 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
307 long pid;
308
309 if (stage_x11->xwin == None || stage_x11->is_foreign_xwin)
310 return;
311
312 /* this will take care of WM_CLIENT_MACHINE and WM_LOCALE_NAME */
313 XSetWMProperties (backend_x11->xdpy, stage_x11->xwin,
314 NULL,
315 NULL,
316 NULL, 0,
317 NULL, NULL, NULL);
318
319 pid = getpid ();
320 XChangeProperty (backend_x11->xdpy,
321 stage_x11->xwin,
322 backend_x11->atom_NET_WM_PID, XA_CARDINAL, 32,
323 PropModeReplace,
324 (guchar *) &pid, 1);
325 }
326
327 static inline void
328 set_wm_title (ClutterStageX11 *stage_x11)
329 {
330 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
331 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
332
333 if (stage_x11->xwin == None || stage_x11->is_foreign_xwin)
334 return;
335
336 if (stage_x11->title == NULL)
337 {
338 XDeleteProperty (backend_x11->xdpy,
339 stage_x11->xwin,
340 backend_x11->atom_NET_WM_NAME);
341 }
342 else
343 {
344 XChangeProperty (backend_x11->xdpy,
345 stage_x11->xwin,
346 backend_x11->atom_NET_WM_NAME,
347 backend_x11->atom_UTF8_STRING,
348 8,
349 PropModeReplace,
350 (unsigned char *) stage_x11->title,
351 (int) strlen (stage_x11->title));
352 }
353 }
354
355 static inline void
356 set_cursor_visible (ClutterStageX11 *stage_x11)
357 {
358 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
359 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
360
361 if (stage_x11->xwin == None)
362 return;
363
364 CLUTTER_NOTE (BACKEND, "setting cursor state ('%s') over stage window (%u)",
365 stage_x11->is_cursor_visible ? "visible" : "invisible",
366 (unsigned int) stage_x11->xwin);
367
368 if (stage_x11->is_cursor_visible)
369 {
370 XUndefineCursor (backend_x11->xdpy, stage_x11->xwin);
371 }
372 else
373 {
374 XColor col;
375 Pixmap pix;
376 Cursor curs;
377
378 pix = XCreatePixmap (backend_x11->xdpy, stage_x11->xwin, 1, 1, 1);
379 memset (&col, 0, sizeof (col));
380 curs = XCreatePixmapCursor (backend_x11->xdpy,
381 pix, pix,
382 &col, &col,
383 1, 1);
384 XFreePixmap (backend_x11->xdpy, pix);
385 XDefineCursor (backend_x11->xdpy, stage_x11->xwin, curs);
386 }
387 }
388
389 static void
390 on_window_scaling_factor_notify (GObject *settings,
391 GParamSpec *pspec,
392 ClutterStageX11 *stage_x11)
393 {
394 g_object_get (settings,
395 "window-scaling-factor", &stage_x11->scale_factor,
396 NULL);
397
398 clutter_stage_x11_resize (CLUTTER_STAGE_WINDOW (stage_x11),
399 stage_x11->xwin_width,
400 stage_x11->xwin_height);
401 }
402
403 static void
404 clutter_stage_x11_unrealize (ClutterStageWindow *stage_window)
405 {
406 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);
407 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
408
409 if (clutter_stages_by_xid != NULL)
410 {
411 CLUTTER_NOTE (BACKEND, "Removing X11 stage 0x%x [%p]",
412 (unsigned int) stage_x11->xwin,
413 stage_x11);
414
415 g_hash_table_remove (clutter_stages_by_xid,
416 GINT_TO_POINTER (stage_x11->xwin));
417 }
418
419 /* Clutter still uses part of the deprecated stateful API of Cogl
420 * (in particulart cogl_set_framebuffer). It means Cogl can keep an
421 * internal reference to the onscreen object we rendered to. In the
422 * case of foreign window, we want to avoid this, as we don't know
423 * what's going to happen to that window.
424 *
425 * The following call sets the current Cogl framebuffer to a dummy
426 * 1x1 one if we're unrealizing the current one, so Cogl doesn't
427 * keep any reference to the foreign window.
428 */
429 if (cogl_get_draw_framebuffer () == COGL_FRAMEBUFFER (stage_cogl->onscreen))
430 _clutter_backend_reset_cogl_framebuffer (stage_cogl->backend);
431
432 clutter_stage_window_parent_iface->unrealize (stage_window);
433 }
434
435 void
436 _clutter_stage_x11_update_foreign_event_mask (CoglOnscreen *onscreen,
437 guint32 event_mask,
438 void *user_data)
439 {
440 ClutterStageX11 *stage_x11 = user_data;
441 ClutterStageCogl *stage_cogl = user_data;
442 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
443 XSetWindowAttributes attrs;
444
445 attrs.event_mask = event_mask | CLUTTER_STAGE_X11_EVENT_MASK;
446
447 XChangeWindowAttributes (backend_x11->xdpy,
448 stage_x11->xwin,
449 CWEventMask,
450 &attrs);
451 }
452
453 static void
454 clutter_stage_x11_set_fullscreen (ClutterStageWindow *stage_window,
455 gboolean is_fullscreen)
456 {
457 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
458 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
459 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
460 ClutterStage *stage = stage_cogl->wrapper;
461 gboolean was_fullscreen;
462
463 if (stage == NULL || CLUTTER_ACTOR_IN_DESTRUCTION (stage))
464 return;
465
466 was_fullscreen = _clutter_stage_is_fullscreen (stage);
467 is_fullscreen = !!is_fullscreen;
468
469 if (was_fullscreen == is_fullscreen)
470 return;
471
472 CLUTTER_NOTE (BACKEND, "%ssetting fullscreen", is_fullscreen ? "" : "un");
473
474 if (is_fullscreen)
475 {
476 #if 0
477 int width, height;
478
479 /* FIXME: this will do the wrong thing for dual-headed
480 displays. This will return the size of the combined display
481 but Metacity (at least) will fullscreen to only one of the
482 displays. This will cause the actor to report the wrong size
483 until the ConfigureNotify for the correct size is received */
484 width = DisplayWidth (backend_x11->xdpy, backend_x11->xscreen_num);
485 height = DisplayHeight (backend_x11->xdpy, backend_x11->xscreen_num);
486 #endif
487
488 /* Set the fullscreen hint so we can retain the old size of the window. */
489 stage_x11->fullscreening = TRUE;
490
491 if (stage_x11->xwin != None)
492 {
493 /* if the actor is not mapped we resize the stage window to match
494 * the size of the screen; this is useful for e.g. EGLX to avoid
495 * a resize when calling clutter_stage_fullscreen() before showing
496 * the stage
497 */
498 if (!STAGE_X11_IS_MAPPED (stage_x11))
499 {
500 CLUTTER_NOTE (BACKEND, "Fullscreening unmapped stage");
501
502 update_state (stage_x11, backend_x11,
503 &backend_x11->atom_NET_WM_STATE_FULLSCREEN,
504 TRUE);
505 }
506 else
507 {
508 CLUTTER_NOTE (BACKEND, "Fullscreening mapped stage");
509
510 /* We need to fix the window size so that it will remove
511 the maximum and minimum window hints. Otherwise
512 metacity will honour the restrictions and not
513 fullscreen correctly. */
514 clutter_stage_x11_fix_window_size (stage_x11, -1, -1);
515
516 send_wmspec_change_state (backend_x11, stage_x11->xwin,
517 backend_x11->atom_NET_WM_STATE_FULLSCREEN,
518 TRUE);
519 }
520 }
521 else
522 stage_x11->fullscreen_on_realize = TRUE;
523 }
524 else
525 {
526 stage_x11->fullscreening = FALSE;
527
528 if (stage_x11->xwin != None)
529 {
530 if (!STAGE_X11_IS_MAPPED (stage_x11))
531 {
532 CLUTTER_NOTE (BACKEND, "Un-fullscreening unmapped stage");
533
534 update_state (stage_x11, backend_x11,
535 &backend_x11->atom_NET_WM_STATE_FULLSCREEN,
536 FALSE);
537 }
538 else
539 {
540 CLUTTER_NOTE (BACKEND, "Un-fullscreening mapped stage");
541
542 send_wmspec_change_state (backend_x11,
543 stage_x11->xwin,
544 backend_x11->atom_NET_WM_STATE_FULLSCREEN,
545 FALSE);
546
547 /* Fix the window size to restore the minimum/maximum
548 restriction */
549 clutter_stage_x11_fix_window_size (stage_x11,
550 stage_x11->xwin_width,
551 stage_x11->xwin_height);
552 }
553 }
554 else
555 stage_x11->fullscreen_on_realize = FALSE;
556 }
557
558 /* XXX: Note we rely on the ConfigureNotify mechanism as the common
559 * mechanism to handle notifications of new X window sizes from the
560 * X server so we don't actively change the stage viewport here or
561 * queue a relayout etc. */
562 }
563
564 void
565 _clutter_stage_x11_events_device_changed (ClutterStageX11 *stage_x11,
566 ClutterInputDevice *device,
567 ClutterDeviceManager *device_manager)
568 {
569 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
570
571 if (clutter_input_device_get_device_mode (device) == CLUTTER_INPUT_MODE_FLOATING)
572 _clutter_device_manager_select_stage_events (device_manager,
573 stage_cogl->wrapper);
574 }
575
576 static void
577 stage_events_device_added (ClutterDeviceManager *device_manager,
578 ClutterInputDevice *device,
579 gpointer user_data)
580 {
581 ClutterStageWindow *stage_window = user_data;
582 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);
583
584 if (clutter_input_device_get_device_mode (device) == CLUTTER_INPUT_MODE_FLOATING)
585 _clutter_device_manager_select_stage_events (device_manager,
586 stage_cogl->wrapper);
587 }
588
589 static gboolean
590 clutter_stage_x11_realize (ClutterStageWindow *stage_window)
591 {
592 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
593 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);
594 ClutterBackend *backend = CLUTTER_BACKEND (stage_cogl->backend);
595 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (backend);
596 ClutterDeviceManager *device_manager;
597 gfloat width, height;
598
599 clutter_actor_get_size (CLUTTER_ACTOR (stage_cogl->wrapper), &width, &height);
600
601 CLUTTER_NOTE (BACKEND, "Wrapper size: %.2f x %.2f", width, height);
602
603 width = width * (float) stage_x11->scale_factor;
604 height = height * (float) stage_x11->scale_factor;
605
606 CLUTTER_NOTE (BACKEND, "Creating a new Cogl onscreen surface: %.2f x %.2f (factor: %d)",
607 width, height,
608 stage_x11->scale_factor);
609
610 stage_cogl->onscreen = cogl_onscreen_new (backend->cogl_context, width, height);
611
612 /* We just created a window of the size of the actor. No need to fix
613 the size of the stage, just update it. */
614 stage_x11->xwin_width = width;
615 stage_x11->xwin_height = height;
616
617 if (stage_x11->xwin != None)
618 {
619 cogl_x11_onscreen_set_foreign_window_xid (stage_cogl->onscreen,
620 stage_x11->xwin,
621 _clutter_stage_x11_update_foreign_event_mask,
622 stage_x11);
623
624 }
625
626 /* Chain to the parent class now. ClutterStageCogl will call cogl_framebuffer_allocate,
627 which will create the X Window we need */
628
629 if (!(clutter_stage_window_parent_iface->realize (stage_window)))
630 return FALSE;
631
632 if (stage_x11->xwin == None)
633 stage_x11->xwin = cogl_x11_onscreen_get_window_xid (stage_cogl->onscreen);
634
635 if (clutter_stages_by_xid == NULL)
636 clutter_stages_by_xid = g_hash_table_new (NULL, NULL);
637
638 g_hash_table_insert (clutter_stages_by_xid,
639 GINT_TO_POINTER (stage_x11->xwin),
640 stage_x11);
641
642 set_wm_pid (stage_x11);
643 set_wm_title (stage_x11);
644 set_cursor_visible (stage_x11);
645
646 /* we unconditionally select input events even with event retrieval
647 * disabled because we need to guarantee that the Clutter internal
648 * state is maintained when calling clutter_x11_handle_event() without
649 * requiring applications or embedding toolkits to select events
650 * themselves. if we did that, we'd have to document the events to be
651 * selected, and also update applications and embedding toolkits each
652 * time we added a new mask, or a new class of events.
653 *
654 * see: http://bugzilla.clutter-project.org/show_bug.cgi?id=998
655 * for the rationale of why we did conditional selection. it is now
656 * clear that a compositor should clear out the input region, since
657 * it cannot assume a perfectly clean slate coming from us.
658 *
659 * see: http://bugzilla.clutter-project.org/show_bug.cgi?id=2228
660 * for an example of things that break if we do conditional event
661 * selection.
662 */
663 XSelectInput (backend_x11->xdpy, stage_x11->xwin, CLUTTER_STAGE_X11_EVENT_MASK);
664
665 /* input events also depent on the actual device, so we need to
666 * use the device manager to let every device select them, using
667 * the event mask we passed to XSelectInput as the template
668 */
669 device_manager = clutter_device_manager_get_default ();
670 if (G_UNLIKELY (device_manager != NULL))
671 {
672 _clutter_device_manager_select_stage_events (device_manager,
673 stage_cogl->wrapper);
674
675 g_signal_connect (device_manager, "device-added",
676 G_CALLBACK (stage_events_device_added),
677 stage_window);
678 }
679
680 clutter_stage_x11_fix_window_size (stage_x11,
681 stage_x11->xwin_width,
682 stage_x11->xwin_height);
683 clutter_stage_x11_set_wm_protocols (stage_x11);
684
685 if (stage_x11->fullscreen_on_realize)
686 {
687 stage_x11->fullscreen_on_realize = FALSE;
688
689 clutter_stage_x11_set_fullscreen (stage_window, TRUE);
690 }
691
692 CLUTTER_NOTE (BACKEND, "Successfully realized stage");
693
694 return TRUE;
695 }
696
697 static void
698 clutter_stage_x11_set_cursor_visible (ClutterStageWindow *stage_window,
699 gboolean cursor_visible)
700 {
701 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
702
703 stage_x11->is_cursor_visible = !!cursor_visible;
704 set_cursor_visible (stage_x11);
705 }
706
707 static void
708 clutter_stage_x11_set_title (ClutterStageWindow *stage_window,
709 const gchar *title)
710 {
711 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
712
713 g_free (stage_x11->title);
714 stage_x11->title = g_strdup (title);
715 set_wm_title (stage_x11);
716 }
717
718 static void
719 clutter_stage_x11_set_user_resizable (ClutterStageWindow *stage_window,
720 gboolean is_resizable)
721 {
722 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
723
724 clutter_stage_x11_fix_window_size (stage_x11,
725 stage_x11->xwin_width,
726 stage_x11->xwin_height);
727 }
728
729 static inline void
730 update_wm_hints (ClutterStageX11 *stage_x11)
731 {
732 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
733 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
734 XWMHints wm_hints;
735
736 if (stage_x11->wm_state & STAGE_X11_WITHDRAWN)
737 return;
738
739 if (stage_x11->is_foreign_xwin)
740 return;
741
742 wm_hints.flags = StateHint | InputHint;
743 wm_hints.initial_state = NormalState;
744 wm_hints.input = stage_x11->accept_focus ? True : False;
745
746 XSetWMHints (backend_x11->xdpy, stage_x11->xwin, &wm_hints);
747 }
748
749 static void
750 clutter_stage_x11_set_accept_focus (ClutterStageWindow *stage_window,
751 gboolean accept_focus)
752 {
753 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
754
755 stage_x11->accept_focus = !!accept_focus;
756 update_wm_hints (stage_x11);
757 }
758
759 static void
760 set_stage_x11_state (ClutterStageX11 *stage_x11,
761 ClutterStageX11State unset_flags,
762 ClutterStageX11State set_flags)
763 {
764 ClutterStageX11State new_stage_state, old_stage_state;
765
766 old_stage_state = stage_x11->wm_state;
767
768 new_stage_state = old_stage_state;
769 new_stage_state |= set_flags;
770 new_stage_state &= ~unset_flags;
771
772 if (new_stage_state == old_stage_state)
773 return;
774
775 stage_x11->wm_state = new_stage_state;
776 }
777
778 static void
779 clutter_stage_x11_show (ClutterStageWindow *stage_window,
780 gboolean do_raise)
781 {
782 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
783 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
784 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
785
786 if (stage_x11->xwin != None)
787 {
788 if (do_raise && !stage_x11->is_foreign_xwin)
789 {
790 CLUTTER_NOTE (BACKEND, "Raising stage[%lu]",
791 (unsigned long) stage_x11->xwin);
792 XRaiseWindow (backend_x11->xdpy, stage_x11->xwin);
793 }
794
795 if (!STAGE_X11_IS_MAPPED (stage_x11))
796 {
797 CLUTTER_NOTE (BACKEND, "Mapping stage[%lu]",
798 (unsigned long) stage_x11->xwin);
799
800 set_stage_x11_state (stage_x11, STAGE_X11_WITHDRAWN, 0);
801
802 update_wm_hints (stage_x11);
803
804 if (stage_x11->fullscreening)
805 clutter_stage_x11_set_fullscreen (stage_window, TRUE);
806 else
807 clutter_stage_x11_set_fullscreen (stage_window, FALSE);
808 }
809
810 g_assert (STAGE_X11_IS_MAPPED (stage_x11));
811
812 clutter_actor_map (CLUTTER_ACTOR (stage_cogl->wrapper));
813
814 if (!stage_x11->is_foreign_xwin)
815 XMapWindow (backend_x11->xdpy, stage_x11->xwin);
816 }
817 }
818
819 static void
820 clutter_stage_x11_hide (ClutterStageWindow *stage_window)
821 {
822 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
823 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
824 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
825
826 if (stage_x11->xwin != None)
827 {
828 if (STAGE_X11_IS_MAPPED (stage_x11))
829 set_stage_x11_state (stage_x11, 0, STAGE_X11_WITHDRAWN);
830
831 g_assert (!STAGE_X11_IS_MAPPED (stage_x11));
832
833 clutter_actor_unmap (CLUTTER_ACTOR (stage_cogl->wrapper));
834
835 if (!stage_x11->is_foreign_xwin)
836 XWithdrawWindow (backend_x11->xdpy, stage_x11->xwin, 0);
837 }
838 }
839
840 static gboolean
841 clutter_stage_x11_can_clip_redraws (ClutterStageWindow *stage_window)
842 {
843 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
844
845 /* while resizing a window, clipped redraws are disabled in order to
846 * avoid artefacts.
847 */
848 return stage_x11->clipped_redraws_cool_off == 0;
849 }
850
851 static void
852 clutter_stage_x11_set_scale_factor (ClutterStageWindow *stage_window,
853 int factor)
854 {
855 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
856
857 if (stage_x11->scale_factor == factor)
858 return;
859
860 stage_x11->scale_factor = factor;
861
862 clutter_stage_x11_resize (stage_window, stage_x11->xwin_width, stage_x11->xwin_height);
863 }
864
865 static int
866 clutter_stage_x11_get_scale_factor (ClutterStageWindow *stage_window)
867 {
868 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window);
869
870 return stage_x11->scale_factor;
871 }
872
873 static void
874 clutter_stage_x11_finalize (GObject *gobject)
875 {
876 ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (gobject);
877
878 g_free (stage_x11->title);
879
880 G_OBJECT_CLASS (clutter_stage_x11_parent_class)->finalize (gobject);
881 }
882
883 static void
884 clutter_stage_x11_dispose (GObject *gobject)
885 {
886 ClutterEventTranslator *translator = CLUTTER_EVENT_TRANSLATOR (gobject);
887 ClutterBackend *backend = CLUTTER_STAGE_COGL (gobject)->backend;
888
889 _clutter_backend_remove_event_translator (backend, translator);
890
891 G_OBJECT_CLASS (clutter_stage_x11_parent_class)->dispose (gobject);
892 }
893
894 static void
895 clutter_stage_x11_class_init (ClutterStageX11Class *klass)
896 {
897 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
898
899 gobject_class->finalize = clutter_stage_x11_finalize;
900 gobject_class->dispose = clutter_stage_x11_dispose;
901 }
902
903 static void
904 clutter_stage_x11_init (ClutterStageX11 *stage)
905 {
906 ClutterSettings *settings;
907
908 stage->xwin = None;
909 stage->xwin_width = 640;
910 stage->xwin_height = 480;
911
912 stage->wm_state = STAGE_X11_WITHDRAWN;
913
914 stage->is_foreign_xwin = FALSE;
915 stage->fullscreening = FALSE;
916 stage->is_cursor_visible = TRUE;
917 stage->accept_focus = TRUE;
918
919 stage->title = NULL;
920
921 settings = clutter_settings_get_default ();
922 g_signal_connect (settings, "notify::window-scaling-factor",
923 G_CALLBACK (on_window_scaling_factor_notify),
924 stage);
925 on_window_scaling_factor_notify (G_OBJECT (settings), NULL, stage);
926 }
927
928 static void
929 clutter_stage_window_iface_init (ClutterStageWindowIface *iface)
930 {
931 clutter_stage_window_parent_iface = g_type_interface_peek_parent (iface);
932
933 iface->set_title = clutter_stage_x11_set_title;
934 iface->set_fullscreen = clutter_stage_x11_set_fullscreen;
935 iface->set_cursor_visible = clutter_stage_x11_set_cursor_visible;
936 iface->set_user_resizable = clutter_stage_x11_set_user_resizable;
937 iface->set_accept_focus = clutter_stage_x11_set_accept_focus;
938 iface->show = clutter_stage_x11_show;
939 iface->hide = clutter_stage_x11_hide;
940 iface->resize = clutter_stage_x11_resize;
941 iface->get_geometry = clutter_stage_x11_get_geometry;
942 iface->realize = clutter_stage_x11_realize;
943 iface->unrealize = clutter_stage_x11_unrealize;
944 iface->can_clip_redraws = clutter_stage_x11_can_clip_redraws;
945 iface->set_scale_factor = clutter_stage_x11_set_scale_factor;
946 iface->get_scale_factor = clutter_stage_x11_get_scale_factor;
947 }
948
949 static inline void
950 set_user_time (ClutterBackendX11 *backend_x11,
951 ClutterStageX11 *stage_x11,
952 long timestamp)
953 {
954 if (timestamp != CLUTTER_CURRENT_TIME)
955 {
956 XChangeProperty (backend_x11->xdpy,
957 stage_x11->xwin,
958 backend_x11->atom_NET_WM_USER_TIME,
959 XA_CARDINAL, 32,
960 PropModeReplace,
961 (unsigned char *) &timestamp, 1);
962 }
963 }
964
965 static gboolean
966 handle_wm_protocols_event (ClutterBackendX11 *backend_x11,
967 ClutterStageX11 *stage_x11,
968 XEvent *xevent)
969 {
970 Atom atom = (Atom) xevent->xclient.data.l[0];
971
972 if (atom == backend_x11->atom_WM_DELETE_WINDOW &&
973 xevent->xany.window == stage_x11->xwin)
974 {
975 #ifdef CLUTTER_ENABLE_DEBUG
976 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
977
978 /* the WM_DELETE_WINDOW is a request: we do not destroy
979 * the window right away, as it might contain vital data;
980 * we relay the event to the application and we let it
981 * handle the request
982 */
983 CLUTTER_NOTE (EVENT, "Delete stage %s[%p], win:0x%x",
984 _clutter_actor_get_debug_name (CLUTTER_ACTOR (stage_cogl->wrapper)),
985 stage_cogl->wrapper,
986 (unsigned int) stage_x11->xwin);
987 #endif /* CLUTTER_ENABLE_DEBUG */
988
989 set_user_time (backend_x11, stage_x11, xevent->xclient.data.l[1]);
990
991 return TRUE;
992 }
993 else if (atom == backend_x11->atom_NET_WM_PING &&
994 xevent->xany.window == stage_x11->xwin)
995 {
996 XClientMessageEvent xclient = xevent->xclient;
997
998 xclient.window = backend_x11->xwin_root;
999 XSendEvent (backend_x11->xdpy, xclient.window,
1000 False,
1001 SubstructureRedirectMask | SubstructureNotifyMask,
1002 (XEvent *) &xclient);
1003 return FALSE;
1004 }
1005
1006 /* do not send any of the WM_PROTOCOLS events to the queue */
1007 return FALSE;
1008 }
1009
1010 static gboolean
1011 clipped_redraws_cool_off_cb (void *data)
1012 {
1013 ClutterStageX11 *stage_x11 = data;
1014
1015 stage_x11->clipped_redraws_cool_off = 0;
1016
1017 return G_SOURCE_REMOVE;
1018 }
1019
1020 static ClutterTranslateReturn
1021 clutter_stage_x11_translate_event (ClutterEventTranslator *translator,
1022 gpointer native,
1023 ClutterEvent *event)
1024 {
1025 ClutterStageX11 *stage_x11;
1026 ClutterStageCogl *stage_cogl;
1027 ClutterTranslateReturn res = CLUTTER_TRANSLATE_CONTINUE;
1028 ClutterBackendX11 *backend_x11;
1029 Window stage_xwindow;
1030 XEvent *xevent = native;
1031 ClutterStage *stage;
1032
1033 stage_cogl = clutter_x11_get_stage_window_from_window (xevent->xany.window);
1034 if (stage_cogl == NULL)
1035 return CLUTTER_TRANSLATE_CONTINUE;
1036
1037 stage = stage_cogl->wrapper;
1038 stage_x11 = CLUTTER_STAGE_X11 (stage_cogl);
1039 backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
1040 stage_xwindow = stage_x11->xwin;
1041
1042 switch (xevent->type)
1043 {
1044 case ConfigureNotify:
1045 if (!stage_x11->is_foreign_xwin)
1046 {
1047 gboolean size_changed = FALSE;
1048
1049 CLUTTER_NOTE (BACKEND, "ConfigureNotify[%x] (%d, %d)",
1050 (unsigned int) stage_x11->xwin,
1051 xevent->xconfigure.width,
1052 xevent->xconfigure.height);
1053
1054 /* When fullscreen, we'll keep the xwin_width/height
1055 variables to track the old size of the window and we'll
1056 assume all ConfigureNotifies constitute a size change */
1057 if (_clutter_stage_is_fullscreen (stage))
1058 size_changed = TRUE;
1059 else if ((stage_x11->xwin_width != xevent->xconfigure.width) ||
1060 (stage_x11->xwin_height != xevent->xconfigure.height))
1061 {
1062 size_changed = TRUE;
1063 stage_x11->xwin_width = xevent->xconfigure.width;
1064 stage_x11->xwin_height = xevent->xconfigure.height;
1065 }
1066
1067 clutter_actor_set_size (CLUTTER_ACTOR (stage),
1068 xevent->xconfigure.width / stage_x11->scale_factor,
1069 xevent->xconfigure.height / stage_x11->scale_factor);
1070
1071 if (size_changed)
1072 {
1073 /* XXX: This is a workaround for a race condition when
1074 * resizing windows while there are in-flight
1075 * glXCopySubBuffer blits happening.
1076 *
1077 * The problem stems from the fact that rectangles for the
1078 * blits are described relative to the bottom left of the
1079 * window and because we can't guarantee control over the X
1080 * window gravity used when resizing so the gravity is
1081 * typically NorthWest not SouthWest.
1082 *
1083 * This means if you grow a window vertically the server
1084 * will make sure to place the old contents of the window
1085 * at the top-left/north-west of your new larger window, but
1086 * that may happen asynchronous to GLX preparing to do a
1087 * blit specified relative to the bottom-left/south-west of
1088 * the window (based on the old smaller window geometry).
1089 *
1090 * When the GLX issued blit finally happens relative to the
1091 * new bottom of your window, the destination will have
1092 * shifted relative to the top-left where all the pixels you
1093 * care about are so it will result in a nasty artefact
1094 * making resizing look very ugly!
1095 *
1096 * We can't currently fix this completely, in-part because
1097 * the window manager tends to trample any gravity we might
1098 * set. This workaround instead simply disables blits for a
1099 * while if we are notified of any resizes happening so if
1100 * the user is resizing a window via the window manager then
1101 * they may see an artefact for one frame but then we will
1102 * fallback to redrawing the full stage until the cooling
1103 * off period is over.
1104 */
1105 if (stage_x11->clipped_redraws_cool_off)
1106 g_source_remove (stage_x11->clipped_redraws_cool_off);
1107
1108 stage_x11->clipped_redraws_cool_off =
1109 clutter_threads_add_timeout (1000,
1110 clipped_redraws_cool_off_cb,
1111 stage_x11);
1112
1113 /* Queue a relayout - we want glViewport to be called
1114 * with the correct values, and this is done in ClutterStage
1115 * via cogl_onscreen_clutter_backend_set_size ().
1116 *
1117 * We queue a relayout, because if this ConfigureNotify is
1118 * in response to a size we set in the application, the
1119 * set_size() call above is essentially a null-op.
1120 *
1121 * Make sure we do this only when the size has changed,
1122 * otherwise we end up relayouting on window moves.
1123 */
1124 clutter_actor_queue_relayout (CLUTTER_ACTOR (stage));
1125
1126 /* the resize process is complete, so we can ask the stage
1127 * to set up the GL viewport with the new size
1128 */
1129 clutter_stage_ensure_viewport (stage);
1130 }
1131 }
1132 break;
1133
1134 case PropertyNotify:
1135 if (xevent->xproperty.atom == backend_x11->atom_NET_WM_STATE &&
1136 xevent->xproperty.window == stage_xwindow &&
1137 !stage_x11->is_foreign_xwin)
1138 {
1139 Atom type;
1140 gint format;
1141 gulong n_items, bytes_after;
1142 guchar *data = NULL;
1143 gboolean fullscreen_set = FALSE;
1144
1145 clutter_x11_trap_x_errors ();
1146 XGetWindowProperty (backend_x11->xdpy, stage_xwindow,
1147 backend_x11->atom_NET_WM_STATE,
1148 0, G_MAXLONG,
1149 False, XA_ATOM,
1150 &type, &format, &n_items,
1151 &bytes_after, &data);
1152 clutter_x11_untrap_x_errors ();
1153
1154 if (type != None && data != NULL)
1155 {
1156 gboolean is_fullscreen = FALSE;
1157 Atom *atoms = (Atom *) data;
1158 gulong i;
1159
1160 for (i = 0; i < n_items; i++)
1161 {
1162 if (atoms[i] == backend_x11->atom_NET_WM_STATE_FULLSCREEN)
1163 fullscreen_set = TRUE;
1164 }
1165
1166 is_fullscreen = _clutter_stage_is_fullscreen (stage_cogl->wrapper);
1167
1168 if (fullscreen_set != is_fullscreen)
1169 {
1170 if (fullscreen_set)
1171 _clutter_stage_update_state (stage_cogl->wrapper,
1172 0,
1173 CLUTTER_STAGE_STATE_FULLSCREEN);
1174 else
1175 _clutter_stage_update_state (stage_cogl->wrapper,
1176 CLUTTER_STAGE_STATE_FULLSCREEN,
1177 0);
1178 }
1179
1180 XFree (data);
1181 }
1182 }
1183 break;
1184
1185 case FocusIn:
1186 if (!_clutter_stage_is_activated (stage_cogl->wrapper))
1187 {
1188 _clutter_stage_update_state (stage_cogl->wrapper,
1189 0,
1190 CLUTTER_STAGE_STATE_ACTIVATED);
1191 }
1192 break;
1193
1194 case FocusOut:
1195 if (_clutter_stage_is_activated (stage_cogl->wrapper))
1196 {
1197 _clutter_stage_update_state (stage_cogl->wrapper,
1198 CLUTTER_STAGE_STATE_ACTIVATED,
1199 0);
1200 }
1201 break;
1202
1203 case Expose:
1204 {
1205 XExposeEvent *expose = (XExposeEvent *) xevent;
1206 cairo_rectangle_int_t clip;
1207
1208 CLUTTER_NOTE (EVENT,
1209 "expose for stage: %s[%p], win:0x%x - "
1210 "redrawing area (x: %d, y: %d, width: %d, height: %d)",
1211 _clutter_actor_get_debug_name (CLUTTER_ACTOR (stage)),
1212 stage,
1213 (unsigned int) stage_xwindow,
1214 expose->x,
1215 expose->y,
1216 expose->width,
1217 expose->height);
1218
1219 clip.x = expose->x / stage_x11->scale_factor;
1220 clip.y = expose->y / stage_x11->scale_factor;
1221 clip.width = expose->width / stage_x11->scale_factor;
1222 clip.height = expose->height / stage_x11->scale_factor;
1223 clutter_actor_queue_redraw_with_clip (CLUTTER_ACTOR (stage), &clip);
1224 }
1225 break;
1226
1227 case DestroyNotify:
1228 CLUTTER_NOTE (EVENT,
1229 "Destroy notification received for stage %s[%p], win:0x%x",
1230 _clutter_actor_get_debug_name (CLUTTER_ACTOR (stage)),
1231 stage,
1232 (unsigned int) stage_xwindow);
1233 event->any.type = CLUTTER_DESTROY_NOTIFY;
1234 event->any.stage = stage;
1235 res = CLUTTER_TRANSLATE_QUEUE;
1236 break;
1237
1238 case ClientMessage:
1239 CLUTTER_NOTE (EVENT, "Client message for stage %s[%p], win:0x%x",
1240 _clutter_actor_get_debug_name (CLUTTER_ACTOR (stage)),
1241 stage,
1242 (unsigned int) stage_xwindow);
1243 if (handle_wm_protocols_event (backend_x11, stage_x11, xevent))
1244 {
1245 event->any.type = CLUTTER_DELETE;
1246 event->any.stage = stage;
1247 res = CLUTTER_TRANSLATE_QUEUE;
1248 }
1249 break;
1250
1251 case MappingNotify:
1252 CLUTTER_NOTE (EVENT, "Refresh keyboard mapping");
1253 XRefreshKeyboardMapping (&xevent->xmapping);
1254 backend_x11->keymap_serial += 1;
1255 res = CLUTTER_TRANSLATE_REMOVE;
1256 break;
1257
1258 default:
1259 res = CLUTTER_TRANSLATE_CONTINUE;
1260 break;
1261 }
1262
1263 return res;
1264 }
1265
1266 static void
1267 clutter_event_translator_iface_init (ClutterEventTranslatorIface *iface)
1268 {
1269 iface->translate_event = clutter_stage_x11_translate_event;
1270 }
1271
1272 /**
1273 * clutter_x11_get_stage_window: (skip)
1274 * @stage: a #ClutterStage
1275 *
1276 * Gets the stages X Window.
1277 *
1278 * Return value: An XID for the stage window.
1279 *
1280 * Since: 0.4
1281 */
1282 Window
1283 clutter_x11_get_stage_window (ClutterStage *stage)
1284 {
1285 ClutterStageWindow *impl;
1286
1287 g_return_val_if_fail (CLUTTER_IS_STAGE (stage), None);
1288
1289 impl = _clutter_stage_get_window (stage);
1290 g_assert (CLUTTER_IS_STAGE_X11 (impl));
1291
1292 return CLUTTER_STAGE_X11 (impl)->xwin;
1293 }
1294
1295 static ClutterStageCogl *
1296 clutter_x11_get_stage_window_from_window (Window win)
1297 {
1298 if (clutter_stages_by_xid == NULL)
1299 return NULL;
1300
1301 return g_hash_table_lookup (clutter_stages_by_xid,
1302 GINT_TO_POINTER (win));
1303 }
1304
1305 /**
1306 * clutter_x11_get_stage_from_window:
1307 * @win: an X Window ID
1308 *
1309 * Gets the stage for a particular X window.
1310 *
1311 * Return value: (transfer none): A #ClutterStage, or% NULL if a stage
1312 * does not exist for the window
1313 *
1314 * Since: 0.8
1315 */
1316 ClutterStage *
1317 clutter_x11_get_stage_from_window (Window win)
1318 {
1319 ClutterStageCogl *stage_cogl;
1320
1321 stage_cogl = clutter_x11_get_stage_window_from_window (win);
1322
1323 if (stage_cogl != NULL)
1324 return stage_cogl->wrapper;
1325
1326 return NULL;
1327 }
1328
1329 /**
1330 * clutter_x11_get_stage_visual: (skip)
1331 * @stage: a #ClutterStage
1332 *
1333 * Returns an XVisualInfo suitable for creating a foreign window for the given
1334 * stage. NOTE: It doesn't do as the name may suggest, which is return the
1335 * XVisualInfo that was used to create an existing window for the given stage.
1336 *
1337 * XXX: It might be best to deprecate this function and replace with something
1338 * along the lines of clutter_backend_x11_get_foreign_visual () or perhaps
1339 * clutter_stage_x11_get_foreign_visual ()
1340 *
1341 * Return value: (transfer full): An XVisualInfo suitable for creating a
1342 * foreign stage. Use XFree() to free the returned value instead
1343 *
1344 * Deprecated: 1.2: Use clutter_x11_get_visual_info() instead
1345 *
1346 * Since: 0.4
1347 */
1348 XVisualInfo *
1349 clutter_x11_get_stage_visual (ClutterStage *stage)
1350 {
1351 ClutterBackend *backend = clutter_get_default_backend ();
1352 ClutterBackendX11 *backend_x11;
1353
1354 g_return_val_if_fail (CLUTTER_IS_BACKEND_X11 (backend), NULL);
1355 backend_x11 = CLUTTER_BACKEND_X11 (backend);
1356
1357 return _clutter_backend_x11_get_visual_info (backend_x11);
1358 }
1359
1360 typedef struct {
1361 ClutterStageX11 *stage_x11;
1362 cairo_rectangle_int_t geom;
1363 Window xwindow;
1364 guint destroy_old_xwindow : 1;
1365 } ForeignWindowData;
1366
1367 static void
1368 set_foreign_window_callback (ClutterActor *actor,
1369 void *data)
1370 {
1371 ForeignWindowData *fwd = data;
1372 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (fwd->stage_x11);
1373 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
1374
1375 CLUTTER_NOTE (BACKEND, "Setting foreign window (0x%x)",
1376 (unsigned int) fwd->xwindow);
1377
1378 if (fwd->destroy_old_xwindow && fwd->stage_x11->xwin != None)
1379 {
1380 CLUTTER_NOTE (BACKEND, "Destroying previous window (0x%x)",
1381 (unsigned int) fwd->xwindow);
1382 XDestroyWindow (backend_x11->xdpy, fwd->stage_x11->xwin);
1383 }
1384
1385 fwd->stage_x11->xwin = fwd->xwindow;
1386 fwd->stage_x11->is_foreign_xwin = TRUE;
1387
1388 fwd->stage_x11->xwin_width = fwd->geom.width * fwd->stage_x11->scale_factor;
1389 fwd->stage_x11->xwin_height = fwd->geom.height * fwd->stage_x11->scale_factor;
1390
1391 clutter_actor_set_size (actor, fwd->geom.width, fwd->geom.height);
1392
1393 if (clutter_stages_by_xid == NULL)
1394 clutter_stages_by_xid = g_hash_table_new (NULL, NULL);
1395
1396 g_hash_table_insert (clutter_stages_by_xid,
1397 GINT_TO_POINTER (fwd->stage_x11->xwin),
1398 fwd->stage_x11);
1399
1400 /* calling this with the stage unrealized will unset the stage
1401 * from the GL context; once the stage is realized the GL context
1402 * will be set again
1403 */
1404 clutter_stage_ensure_current (CLUTTER_STAGE (actor));
1405 }
1406
1407 /**
1408 * clutter_x11_set_stage_foreign:
1409 * @stage: a #ClutterStage
1410 * @xwindow: an existing X Window id
1411 *
1412 * Target the #ClutterStage to use an existing external X Window
1413 *
1414 * Return value: %TRUE if foreign window is valid
1415 *
1416 * Since: 0.4
1417 */
1418 gboolean
1419 clutter_x11_set_stage_foreign (ClutterStage *stage,
1420 Window xwindow)
1421 {
1422 ClutterBackendX11 *backend_x11;
1423 ClutterStageX11 *stage_x11;
1424 ClutterStageCogl *stage_cogl;
1425 ClutterStageWindow *impl;
1426 ClutterActor *actor;
1427 gint x, y;
1428 guint width, height, border, depth;
1429 Window root_return;
1430 Status status;
1431 ForeignWindowData fwd;
1432 XVisualInfo *xvisinfo;
1433
1434 g_return_val_if_fail (CLUTTER_IS_STAGE (stage), FALSE);
1435 g_return_val_if_fail (!CLUTTER_ACTOR_IN_DESTRUCTION (stage), FALSE);
1436 g_return_val_if_fail (xwindow != None, FALSE);
1437
1438 impl = _clutter_stage_get_window (stage);
1439 stage_x11 = CLUTTER_STAGE_X11 (impl);
1440 stage_cogl = CLUTTER_STAGE_COGL (impl);
1441 backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
1442
1443 xvisinfo = _clutter_backend_x11_get_visual_info (backend_x11);
1444 g_return_val_if_fail (xvisinfo != NULL, FALSE);
1445
1446 clutter_x11_trap_x_errors ();
1447
1448 status = XGetGeometry (backend_x11->xdpy, xwindow,
1449 &root_return,
1450 &x, &y,
1451 &width, &height,
1452 &border,
1453 &depth);
1454
1455 if (clutter_x11_untrap_x_errors () || !status)
1456 {
1457 g_critical ("Unable to retrieve the geometry of the foreign window: "
1458 "XGetGeometry() failed (status code: %d)", status);
1459 return FALSE;
1460 }
1461
1462 if (width == 0 || height == 0)
1463 {
1464 g_warning ("The size of the foreign window is 0x0");
1465 return FALSE;
1466 }
1467
1468 if (depth != xvisinfo->depth)
1469 {
1470 g_warning ("The depth of the visual of the foreign window is %d, but "
1471 "Clutter has been initialized to require a visual depth "
1472 "of %d",
1473 depth,
1474 xvisinfo->depth);
1475 return FALSE;
1476 }
1477
1478 fwd.stage_x11 = stage_x11;
1479 fwd.xwindow = xwindow;
1480
1481 /* destroy the old Window, if we have one and it's ours */
1482 if (stage_x11->xwin != None && !stage_x11->is_foreign_xwin)
1483 fwd.destroy_old_xwindow = TRUE;
1484 else
1485 fwd.destroy_old_xwindow = FALSE;
1486
1487 fwd.geom.x = x;
1488 fwd.geom.y = y;
1489 fwd.geom.width = width / stage_x11->scale_factor;
1490 fwd.geom.height = height / stage_x11->scale_factor;
1491
1492 actor = CLUTTER_ACTOR (stage);
1493
1494 _clutter_actor_rerealize (actor,
1495 set_foreign_window_callback,
1496 &fwd);
1497
1498 /* Queue a relayout - so the stage will be allocated the new
1499 * window size.
1500 *
1501 * Note also that when the stage gets allocated the new
1502 * window size that will result in the stage's
1503 * priv->viewport being changed, which will in turn result
1504 * in the Cogl viewport changing when _clutter_do_redraw
1505 * calls _clutter_stage_maybe_setup_viewport().
1506 */
1507 clutter_actor_queue_relayout (actor);
1508
1509 return TRUE;
1510 }
1511
1512 void
1513 _clutter_stage_x11_set_user_time (ClutterStageX11 *stage_x11,
1514 guint32 user_time)
1515 {
1516 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
1517 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
1518
1519 set_user_time (backend_x11, stage_x11, user_time);
1520 }
1521
1522 gboolean
1523 _clutter_stage_x11_get_root_coords (ClutterStageX11 *stage_x11,
1524 gint *root_x,
1525 gint *root_y)
1526 {
1527 ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_x11);
1528 ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (stage_cogl->backend);
1529 gint return_val;
1530 Window child;
1531 gint tx, ty;
1532
1533 return_val = XTranslateCoordinates (backend_x11->xdpy,
1534 stage_x11->xwin,
1535 backend_x11->xwin_root,
1536 0, 0, &tx, &ty,
1537 &child);
1538
1539 if (root_x)
1540 *root_x = tx;
1541
1542 if (root_y)
1543 *root_y = ty;
1544
1545 return (return_val == 0);
1546 }