2014-04-21 19:13:04 -04:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Red Hat
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Written by:
|
|
|
|
* Jasper St. Pierre <jstpierre@mecheye.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "meta-backend-x11.h"
|
|
|
|
|
2014-04-21 19:47:04 -04:00
|
|
|
#include <clutter/x11/clutter-x11.h>
|
|
|
|
|
2014-04-22 10:14:59 -04:00
|
|
|
#include <X11/extensions/sync.h>
|
|
|
|
|
2014-04-21 19:13:04 -04:00
|
|
|
#include "meta-idle-monitor-xsync.h"
|
2014-04-21 19:41:11 -04:00
|
|
|
#include "meta-monitor-manager-xrandr.h"
|
|
|
|
#include "backends/meta-monitor-manager-dummy.h"
|
2014-04-27 10:49:29 -04:00
|
|
|
#include "meta-cursor-renderer-x11.h"
|
2014-04-21 19:13:04 -04:00
|
|
|
|
2014-04-23 10:50:07 -04:00
|
|
|
#include <meta/util.h>
|
2014-04-23 14:50:27 -04:00
|
|
|
#include "display-private.h"
|
|
|
|
#include "compositor/compositor-private.h"
|
2014-04-23 10:50:07 -04:00
|
|
|
|
2014-04-22 10:14:59 -04:00
|
|
|
struct _MetaBackendX11Private
|
|
|
|
{
|
|
|
|
/* The host X11 display */
|
|
|
|
Display *xdisplay;
|
|
|
|
GSource *source;
|
|
|
|
|
|
|
|
int xsync_event_base;
|
|
|
|
int xsync_error_base;
|
2014-04-23 13:59:58 -04:00
|
|
|
|
|
|
|
int xinput_opcode;
|
|
|
|
int xinput_event_base;
|
|
|
|
int xinput_error_base;
|
2014-04-22 10:14:59 -04:00
|
|
|
};
|
|
|
|
typedef struct _MetaBackendX11Private MetaBackendX11Private;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (MetaBackendX11, meta_backend_x11, META_TYPE_BACKEND);
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_alarm_notify (MetaBackend *backend,
|
2014-04-23 15:19:08 -04:00
|
|
|
XEvent *event)
|
2014-04-22 10:14:59 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= backend->device_id_max; i++)
|
|
|
|
if (backend->device_monitors[i])
|
2014-04-23 15:19:08 -04:00
|
|
|
meta_idle_monitor_xsync_handle_xevent (backend->device_monitors[i], (XSyncAlarmNotifyEvent*) event);
|
2014-04-22 10:14:59 -04:00
|
|
|
}
|
|
|
|
|
2014-04-28 13:22:17 -04:00
|
|
|
static void
|
|
|
|
translate_device_event (MetaBackendX11 *x11,
|
|
|
|
XIDeviceEvent *device_event)
|
|
|
|
{
|
2014-04-27 10:49:29 -04:00
|
|
|
Window stage_window = meta_backend_x11_get_xwindow (x11);
|
2014-04-28 13:27:02 -04:00
|
|
|
|
|
|
|
if (device_event->event != stage_window)
|
|
|
|
{
|
|
|
|
/* This codepath should only ever trigger as an X11 compositor,
|
|
|
|
* and never under nested, as under nested all backend events
|
|
|
|
* should be reported with respect to the stage window. */
|
|
|
|
g_assert (!meta_is_wayland_compositor ());
|
|
|
|
|
|
|
|
device_event->event = stage_window;
|
|
|
|
|
|
|
|
/* As an X11 compositor, the stage window is always at 0,0, so
|
|
|
|
* using root coordinates will give us correct stage coordinates
|
|
|
|
* as well... */
|
|
|
|
device_event->event_x = device_event->root_x;
|
|
|
|
device_event->event_y = device_event->root_y;
|
|
|
|
}
|
2014-04-24 12:06:32 -04:00
|
|
|
}
|
|
|
|
|
2014-04-23 13:59:58 -04:00
|
|
|
/* Clutter makes the assumption that there is only one X window
|
|
|
|
* per stage, which is a valid assumption to make for a generic
|
|
|
|
* application toolkit. As such, it will ignore any events sent
|
|
|
|
* to the a stage that isn't its X window.
|
|
|
|
*
|
|
|
|
* When running as an X window manager, we need to respond to
|
|
|
|
* events from lots of windows. Trick Clutter into translating
|
|
|
|
* these events by pretending we got an event on the stage window.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
maybe_spoof_event_as_stage_event (MetaBackendX11 *x11,
|
|
|
|
XEvent *event)
|
|
|
|
{
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
|
|
|
|
if (event->type == GenericEvent &&
|
|
|
|
event->xcookie.extension == priv->xinput_opcode)
|
|
|
|
{
|
|
|
|
XIEvent *input_event = (XIEvent *) event->xcookie.data;
|
|
|
|
|
|
|
|
switch (input_event->evtype)
|
|
|
|
{
|
|
|
|
case XI_Motion:
|
|
|
|
case XI_ButtonPress:
|
|
|
|
case XI_ButtonRelease:
|
|
|
|
case XI_KeyPress:
|
|
|
|
case XI_KeyRelease:
|
2014-04-24 12:06:32 -04:00
|
|
|
translate_device_event (x11, (XIDeviceEvent *) input_event);
|
|
|
|
break;
|
2014-04-23 13:59:58 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 10:14:59 -04:00
|
|
|
static void
|
|
|
|
handle_host_xevent (MetaBackend *backend,
|
2014-04-23 15:19:08 -04:00
|
|
|
XEvent *event)
|
2014-04-22 10:14:59 -04:00
|
|
|
{
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
2014-04-23 12:05:14 -04:00
|
|
|
gboolean bypass_clutter = FALSE;
|
2014-04-22 10:14:59 -04:00
|
|
|
|
2014-04-23 15:17:21 -04:00
|
|
|
XGetEventData (priv->xdisplay, &event->xcookie);
|
|
|
|
|
2014-04-23 15:19:08 -04:00
|
|
|
if (event->type == (priv->xsync_event_base + XSyncAlarmNotify))
|
|
|
|
handle_alarm_notify (backend, event);
|
2014-04-22 10:14:59 -04:00
|
|
|
|
2014-04-23 12:05:14 -04:00
|
|
|
{
|
|
|
|
MetaMonitorManager *manager = meta_backend_get_monitor_manager (backend);
|
2014-04-23 12:07:38 -04:00
|
|
|
if (META_IS_MONITOR_MANAGER_XRANDR (manager) &&
|
2014-04-23 15:19:08 -04:00
|
|
|
meta_monitor_manager_xrandr_handle_xevent (META_MONITOR_MANAGER_XRANDR (manager), event))
|
2014-04-23 12:05:14 -04:00
|
|
|
{
|
|
|
|
bypass_clutter = TRUE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!bypass_clutter)
|
2014-07-07 19:13:31 -04:00
|
|
|
{
|
|
|
|
maybe_spoof_event_as_stage_event (x11, event);
|
|
|
|
clutter_x11_handle_event (event);
|
|
|
|
}
|
2014-04-23 15:17:21 -04:00
|
|
|
|
|
|
|
XFreeEventData (priv->xdisplay, &event->xcookie);
|
2014-04-22 10:14:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GSource base;
|
|
|
|
GPollFD event_poll_fd;
|
|
|
|
MetaBackend *backend;
|
|
|
|
} XEventSource;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
x_event_source_prepare (GSource *source,
|
|
|
|
int *timeout)
|
|
|
|
{
|
|
|
|
XEventSource *x_source = (XEventSource *) source;
|
|
|
|
MetaBackend *backend = x_source->backend;
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
|
|
|
|
*timeout = -1;
|
|
|
|
|
|
|
|
return XPending (priv->xdisplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
x_event_source_check (GSource *source)
|
|
|
|
{
|
|
|
|
XEventSource *x_source = (XEventSource *) source;
|
|
|
|
MetaBackend *backend = x_source->backend;
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
|
|
|
|
return XPending (priv->xdisplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
x_event_source_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
XEventSource *x_source = (XEventSource *) source;
|
|
|
|
MetaBackend *backend = x_source->backend;
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
|
|
|
|
while (XPending (priv->xdisplay))
|
|
|
|
{
|
2014-04-23 15:19:08 -04:00
|
|
|
XEvent event;
|
2014-04-22 10:14:59 -04:00
|
|
|
|
2014-04-23 15:19:08 -04:00
|
|
|
XNextEvent (priv->xdisplay, &event);
|
2014-04-22 10:14:59 -04:00
|
|
|
|
2014-04-23 15:19:08 -04:00
|
|
|
handle_host_xevent (backend, &event);
|
2014-04-22 10:14:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSourceFuncs x_event_funcs = {
|
|
|
|
x_event_source_prepare,
|
|
|
|
x_event_source_check,
|
|
|
|
x_event_source_dispatch,
|
|
|
|
};
|
|
|
|
|
|
|
|
static GSource *
|
|
|
|
x_event_source_new (MetaBackend *backend)
|
|
|
|
{
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
GSource *source;
|
|
|
|
XEventSource *x_source;
|
|
|
|
|
|
|
|
source = g_source_new (&x_event_funcs, sizeof (XEventSource));
|
|
|
|
x_source = (XEventSource *) source;
|
|
|
|
x_source->backend = backend;
|
|
|
|
x_source->event_poll_fd.fd = ConnectionNumber (priv->xdisplay);
|
|
|
|
x_source->event_poll_fd.events = G_IO_IN;
|
|
|
|
g_source_add_poll (source, &x_source->event_poll_fd);
|
|
|
|
|
|
|
|
g_source_attach (source, NULL);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_backend_x11_post_init (MetaBackend *backend)
|
|
|
|
{
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
int major, minor;
|
|
|
|
|
|
|
|
priv->xdisplay = clutter_x11_get_default_display ();
|
|
|
|
|
|
|
|
priv->source = x_event_source_new (backend);
|
|
|
|
|
2014-04-22 12:45:13 -04:00
|
|
|
if (!XSyncQueryExtension (priv->xdisplay, &priv->xsync_event_base, &priv->xsync_error_base) ||
|
|
|
|
!XSyncInitialize (priv->xdisplay, &major, &minor))
|
2014-04-22 10:14:59 -04:00
|
|
|
meta_fatal ("Could not initialize XSync");
|
|
|
|
|
2014-04-23 13:59:58 -04:00
|
|
|
{
|
|
|
|
int major = 2, minor = 3;
|
|
|
|
gboolean has_xi = FALSE;
|
|
|
|
|
|
|
|
if (XQueryExtension (priv->xdisplay,
|
|
|
|
"XInputExtension",
|
|
|
|
&priv->xinput_opcode,
|
|
|
|
&priv->xinput_error_base,
|
|
|
|
&priv->xinput_event_base))
|
|
|
|
{
|
|
|
|
if (XIQueryVersion (priv->xdisplay, &major, &minor) == Success)
|
|
|
|
{
|
|
|
|
int version = (major * 10) + minor;
|
|
|
|
if (version >= 22)
|
|
|
|
has_xi = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_xi)
|
|
|
|
meta_fatal ("X server doesn't have the XInput extension, version 2.2 or newer\n");
|
|
|
|
}
|
|
|
|
|
2014-04-22 10:14:59 -04:00
|
|
|
META_BACKEND_CLASS (meta_backend_x11_parent_class)->post_init (backend);
|
|
|
|
}
|
2014-04-21 19:13:04 -04:00
|
|
|
|
|
|
|
static MetaIdleMonitor *
|
|
|
|
meta_backend_x11_create_idle_monitor (MetaBackend *backend,
|
|
|
|
int device_id)
|
|
|
|
{
|
|
|
|
return g_object_new (META_TYPE_IDLE_MONITOR_XSYNC,
|
|
|
|
"device-id", device_id,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2014-04-21 19:41:11 -04:00
|
|
|
static MetaMonitorManager *
|
|
|
|
meta_backend_x11_create_monitor_manager (MetaBackend *backend)
|
|
|
|
{
|
|
|
|
/* If we're a Wayland compositor using the X11 backend,
|
|
|
|
* we're a nested configuration, so return the dummy
|
|
|
|
* monitor setup. */
|
|
|
|
if (meta_is_wayland_compositor ())
|
|
|
|
return g_object_new (META_TYPE_MONITOR_MANAGER_DUMMY, NULL);
|
|
|
|
|
|
|
|
return g_object_new (META_TYPE_MONITOR_MANAGER_XRANDR, NULL);
|
|
|
|
}
|
|
|
|
|
2014-04-27 10:49:29 -04:00
|
|
|
static MetaCursorRenderer *
|
|
|
|
meta_backend_x11_create_cursor_renderer (MetaBackend *backend)
|
|
|
|
{
|
|
|
|
return g_object_new (META_TYPE_CURSOR_RENDERER_X11, NULL);
|
|
|
|
}
|
|
|
|
|
2014-04-23 10:50:07 -04:00
|
|
|
static gboolean
|
|
|
|
meta_backend_x11_grab_device (MetaBackend *backend,
|
|
|
|
int device_id,
|
|
|
|
uint32_t timestamp)
|
|
|
|
{
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
unsigned char mask_bits[XIMaskLen (XI_LASTEVENT)] = { 0 };
|
|
|
|
XIEventMask mask = { XIAllMasterDevices, sizeof (mask_bits), mask_bits };
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
XISetMask (mask.mask, XI_ButtonPress);
|
|
|
|
XISetMask (mask.mask, XI_ButtonRelease);
|
|
|
|
XISetMask (mask.mask, XI_Enter);
|
|
|
|
XISetMask (mask.mask, XI_Leave);
|
|
|
|
XISetMask (mask.mask, XI_Motion);
|
2014-04-23 13:36:11 -04:00
|
|
|
XISetMask (mask.mask, XI_KeyPress);
|
|
|
|
XISetMask (mask.mask, XI_KeyRelease);
|
2014-04-23 10:50:07 -04:00
|
|
|
|
|
|
|
ret = XIGrabDevice (priv->xdisplay, device_id,
|
2014-04-27 10:49:29 -04:00
|
|
|
meta_backend_x11_get_xwindow (x11),
|
2014-04-23 10:50:07 -04:00
|
|
|
timestamp,
|
2014-05-13 15:52:26 -04:00
|
|
|
None,
|
2014-04-23 10:50:07 -04:00
|
|
|
XIGrabModeAsync, XIGrabModeAsync,
|
|
|
|
False, /* owner_events */
|
|
|
|
&mask);
|
|
|
|
|
|
|
|
return (ret == Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
meta_backend_x11_ungrab_device (MetaBackend *backend,
|
|
|
|
int device_id,
|
|
|
|
uint32_t timestamp)
|
|
|
|
{
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = XIUngrabDevice (priv->xdisplay, device_id, timestamp);
|
|
|
|
|
|
|
|
return (ret == Success);
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:11:23 -04:00
|
|
|
static void
|
|
|
|
meta_backend_x11_warp_pointer (MetaBackend *backend,
|
|
|
|
int x,
|
|
|
|
int y)
|
|
|
|
{
|
|
|
|
MetaBackendX11 *x11 = META_BACKEND_X11 (backend);
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
|
|
|
|
XIWarpPointer (priv->xdisplay,
|
|
|
|
META_VIRTUAL_CORE_POINTER_ID,
|
|
|
|
None,
|
|
|
|
meta_backend_x11_get_xwindow (x11),
|
|
|
|
0, 0, 0, 0,
|
|
|
|
x, y);
|
|
|
|
}
|
|
|
|
|
2014-04-21 19:13:04 -04:00
|
|
|
static void
|
|
|
|
meta_backend_x11_class_init (MetaBackendX11Class *klass)
|
|
|
|
{
|
|
|
|
MetaBackendClass *backend_class = META_BACKEND_CLASS (klass);
|
|
|
|
|
2014-04-22 10:14:59 -04:00
|
|
|
backend_class->post_init = meta_backend_x11_post_init;
|
2014-04-21 19:13:04 -04:00
|
|
|
backend_class->create_idle_monitor = meta_backend_x11_create_idle_monitor;
|
2014-04-21 19:41:11 -04:00
|
|
|
backend_class->create_monitor_manager = meta_backend_x11_create_monitor_manager;
|
2014-04-27 10:49:29 -04:00
|
|
|
backend_class->create_cursor_renderer = meta_backend_x11_create_cursor_renderer;
|
2014-04-23 10:50:07 -04:00
|
|
|
|
|
|
|
backend_class->grab_device = meta_backend_x11_grab_device;
|
|
|
|
backend_class->ungrab_device = meta_backend_x11_ungrab_device;
|
2014-05-27 14:11:23 -04:00
|
|
|
backend_class->warp_pointer = meta_backend_x11_warp_pointer;
|
2014-04-21 19:13:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_backend_x11_init (MetaBackendX11 *x11)
|
|
|
|
{
|
2014-04-22 10:14:59 -04:00
|
|
|
/* We do X11 event retrieval ourselves */
|
|
|
|
clutter_x11_disable_event_retrieval ();
|
2014-04-21 19:13:04 -04:00
|
|
|
}
|
2014-04-23 09:57:16 -04:00
|
|
|
|
|
|
|
Display *
|
|
|
|
meta_backend_x11_get_xdisplay (MetaBackendX11 *x11)
|
|
|
|
{
|
|
|
|
MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11);
|
|
|
|
|
|
|
|
return priv->xdisplay;
|
|
|
|
}
|
|
|
|
|
2014-04-27 10:49:29 -04:00
|
|
|
Window
|
|
|
|
meta_backend_x11_get_xwindow (MetaBackendX11 *x11)
|
|
|
|
{
|
|
|
|
MetaDisplay *display = meta_get_display ();
|
|
|
|
MetaCompositor *compositor = display->compositor;
|
|
|
|
|
|
|
|
if (compositor == NULL)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
ClutterStage *stage = CLUTTER_STAGE (compositor->stage);
|
|
|
|
return clutter_x11_get_stage_window (stage);
|
|
|
|
}
|