1 /*
2  * Clutter.
3  *
4  * An OpenGL based 'interactive canvas' library.
5  *
6  * Copyright (C) 2011  Intel Corp.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * Authors:
22  *  Emmanuele Bassi <ebassi@linux.intel.com>
23  *  Robert Bragg <robert@linux.intel.com>
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include "clutter-input-device-wayland.h"
31 #include "clutter-device-manager-wayland.h"
32 
33 #include "clutter-backend.h"
34 #include "wayland/clutter-backend-wayland.h"
35 #include "wayland/clutter-backend-wayland-priv.h"
36 #include "clutter-debug.h"
37 #include "clutter-device-manager-private.h"
38 #include "clutter-private.h"
39 
40 #include <wayland-client.h>
41 #include <wayland-client-protocol.h>
42 
43 enum
44 {
45   PROP_0
46 };
47 
48 G_DEFINE_TYPE (ClutterDeviceManagerWayland,
49                _clutter_device_manager_wayland,
50                CLUTTER_TYPE_DEVICE_MANAGER);
51 
52 static void
clutter_device_manager_wayland_add_device(ClutterDeviceManager * manager,ClutterInputDevice * device)53 clutter_device_manager_wayland_add_device (ClutterDeviceManager *manager,
54                                            ClutterInputDevice   *device)
55 {
56   ClutterDeviceManagerWayland *manager_wayland =
57     CLUTTER_DEVICE_MANAGER_WAYLAND (manager);
58   ClutterInputDeviceType device_type;
59   gboolean is_pointer, is_keyboard;
60 
61   device_type = clutter_input_device_get_device_type (device);
62   is_pointer  = (device_type == CLUTTER_POINTER_DEVICE)  ? TRUE : FALSE;
63   is_keyboard = (device_type == CLUTTER_KEYBOARD_DEVICE) ? TRUE : FALSE;
64 
65   manager_wayland->devices = g_slist_prepend (manager_wayland->devices, device);
66 
67   if (is_pointer && manager_wayland->core_pointer == NULL)
68     manager_wayland->core_pointer = device;
69 
70   if (is_keyboard && manager_wayland->core_keyboard == NULL)
71     manager_wayland->core_keyboard = device;
72 }
73 
74 static void
clutter_device_manager_wayland_remove_device(ClutterDeviceManager * manager,ClutterInputDevice * device)75 clutter_device_manager_wayland_remove_device (ClutterDeviceManager *manager,
76                                               ClutterInputDevice   *device)
77 {
78   ClutterDeviceManagerWayland *manager_wayland = CLUTTER_DEVICE_MANAGER_WAYLAND (manager);
79 
80   manager_wayland->devices = g_slist_remove (manager_wayland->devices, device);
81 }
82 
83 static const GSList *
clutter_device_manager_wayland_get_devices(ClutterDeviceManager * manager)84 clutter_device_manager_wayland_get_devices (ClutterDeviceManager *manager)
85 {
86   return CLUTTER_DEVICE_MANAGER_WAYLAND (manager)->devices;
87 }
88 
89 static ClutterInputDevice *
clutter_device_manager_wayland_get_core_device(ClutterDeviceManager * manager,ClutterInputDeviceType type)90 clutter_device_manager_wayland_get_core_device (ClutterDeviceManager *manager,
91                                                 ClutterInputDeviceType type)
92 {
93   ClutterDeviceManagerWayland *manager_wayland;
94 
95   manager_wayland = CLUTTER_DEVICE_MANAGER_WAYLAND (manager);
96 
97   switch (type)
98     {
99     case CLUTTER_POINTER_DEVICE:
100       return manager_wayland->core_pointer;
101 
102     case CLUTTER_KEYBOARD_DEVICE:
103       return manager_wayland->core_keyboard;
104 
105     case CLUTTER_EXTENSION_DEVICE:
106     default:
107       return NULL;
108     }
109 
110   return NULL;
111 }
112 
113 static ClutterInputDevice *
clutter_device_manager_wayland_get_device(ClutterDeviceManager * manager,gint id)114 clutter_device_manager_wayland_get_device (ClutterDeviceManager *manager,
115                                            gint                  id)
116 {
117   ClutterDeviceManagerWayland *manager_wayland =
118     CLUTTER_DEVICE_MANAGER_WAYLAND (manager);
119   GSList *l;
120 
121   for (l = manager_wayland->devices; l != NULL; l = l->next)
122     {
123       ClutterInputDevice *device = l->data;
124 
125       if (clutter_input_device_get_device_id (device) == id)
126         return device;
127     }
128 
129   return NULL;
130 }
131 
132 static void
_clutter_device_manager_wayland_class_init(ClutterDeviceManagerWaylandClass * klass)133 _clutter_device_manager_wayland_class_init (ClutterDeviceManagerWaylandClass *klass)
134 {
135   ClutterDeviceManagerClass *manager_class;
136 
137   manager_class = CLUTTER_DEVICE_MANAGER_CLASS (klass);
138   manager_class->add_device = clutter_device_manager_wayland_add_device;
139   manager_class->remove_device = clutter_device_manager_wayland_remove_device;
140   manager_class->get_devices = clutter_device_manager_wayland_get_devices;
141   manager_class->get_core_device = clutter_device_manager_wayland_get_core_device;
142   manager_class->get_device = clutter_device_manager_wayland_get_device;
143 }
144 
145 static void
_clutter_device_manager_wayland_init(ClutterDeviceManagerWayland * self)146 _clutter_device_manager_wayland_init (ClutterDeviceManagerWayland *self)
147 {
148 }
149 
150 void
_clutter_device_manager_wayland_add_input_group(ClutterDeviceManager * manager,uint32_t id)151 _clutter_device_manager_wayland_add_input_group (ClutterDeviceManager *manager,
152                                                  uint32_t id)
153 {
154   ClutterBackend *backend = _clutter_device_manager_get_backend (manager);
155   ClutterBackendWayland *backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
156   ClutterInputDeviceWayland *device;
157 
158   device = g_object_new (CLUTTER_TYPE_INPUT_DEVICE_WAYLAND,
159                          "id", id,
160                          "device-type", CLUTTER_POINTER_DEVICE,
161                          "name", "wayland device",
162                          "enabled", TRUE,
163                          NULL);
164 
165   device->input_device =
166     wl_registry_bind (backend_wayland->wayland_registry, id,
167                       &wl_seat_interface, 1);
168   wl_seat_add_listener (device->input_device,
169                         &_clutter_seat_wayland_listener,
170                         device);
171   wl_seat_set_user_data (device->input_device, device);
172 
173   _clutter_device_manager_add_device (manager, CLUTTER_INPUT_DEVICE (device));
174 }
175 
176 ClutterDeviceManager *
_clutter_device_manager_wayland_new(ClutterBackend * backend)177 _clutter_device_manager_wayland_new (ClutterBackend *backend)
178 {
179   return g_object_new (CLUTTER_TYPE_DEVICE_MANAGER_WAYLAND,
180                        "backend", backend,
181                        NULL);
182 }
183 
184 void
_clutter_events_wayland_init(ClutterBackend * backend)185 _clutter_events_wayland_init (ClutterBackend *backend)
186 {
187   ClutterBackendWayland *backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
188 
189   /* XXX: We actually create the wayland device manager in the backend
190    * post_parse vfunc because that's the point where we connect to a compositor
191    * and that's also the point where we will be notified of input devices so we
192    * need the device-manager to exist early on.
193    *
194    * To be consistent with other clutter backends though we only associate the
195    * device manager with the backend when _clutter_events_wayland_init() is
196    * called in _clutter_backend_init_events(). This should still allow the
197    * runtime selection of an alternative input backend if desired and in that
198    * case the wayland device manager will be benign.
199    *
200    * FIXME: At some point we could perhaps collapse the
201    * _clutter_backend_post_parse(), and _clutter_backend_init_events()
202    * functions into one called something like _clutter_backend_init() which
203    * would allow the real backend to manage the precise order of
204    * initialization.
205    */
206 
207   backend->device_manager = g_object_ref (backend_wayland->device_manager);
208 }
209 
210 void
_clutter_events_wayland_uninit(ClutterBackend * backend)211 _clutter_events_wayland_uninit (ClutterBackend *backend)
212 {
213   g_object_unref (backend->device_manager);
214   backend->device_manager = NULL;
215 }
216