1 /*
2  * Copyright (C) 2010  Intel Corp.
3  * Copyright (C) 2014  Jonas Ådahl
4  * Copyright (C) 2016  Red Hat Inc.
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  * Author: Damien Lespiau <damien.lespiau@intel.com>
20  * Author: Jonas Ådahl <jadahl@gmail.com>
21  */
22 
23 #ifndef META_SEAT_IMPL_H
24 #define META_SEAT_IMPL_H
25 
26 #ifndef META_INPUT_THREAD_H_INSIDE
27 #error "This header cannot be included directly. Use "backends/native/meta-input-thread.h""
28 #endif /* META_INPUT_THREAD_H_INSIDE */
29 
30 #include <gudev/gudev.h>
31 #include <libinput.h>
32 #include <linux/input-event-codes.h>
33 
34 #include "backends/meta-input-settings-private.h"
35 #include "backends/meta-viewport-info.h"
36 #include "backends/native/meta-backend-native-types.h"
37 #include "backends/native/meta-barrier-native.h"
38 #include "backends/native/meta-cursor-renderer-native.h"
39 #include "backends/native/meta-keymap-native.h"
40 #include "backends/native/meta-pointer-constraint-native.h"
41 #include "backends/native/meta-xkb-utils.h"
42 #include "clutter/clutter.h"
43 
44 typedef struct _MetaTouchState MetaTouchState;
45 typedef struct _MetaSeatImpl MetaSeatImpl;
46 typedef struct _MetaEventSource  MetaEventSource;
47 
48 struct _MetaTouchState
49 {
50   MetaSeatImpl *seat_impl;
51 
52   int device_slot;
53   int seat_slot;
54   graphene_point_t coords;
55 };
56 
57 struct _MetaSeatImpl
58 {
59   GObject parent_instance;
60 
61   GMainContext *main_context;
62   GMainContext *input_context;
63   GMainLoop *input_loop;
64   GThread *input_thread;
65   GMutex init_mutex;
66   GCond init_cond;
67 
68   MetaSeatNative *seat_native;
69   char *seat_id;
70   MetaSeatNativeFlag flags;
71   MetaEventSource *event_source;
72   struct libinput *libinput;
73   GRWLock state_lock;
74 
75   GSList *devices;
76   GHashTable *tools;
77 
78   ClutterInputDevice *core_pointer;
79   ClutterInputDevice *core_keyboard;
80 
81   GHashTable *touch_states;
82   GHashTable *cursor_renderers;
83 
84   struct xkb_state *xkb;
85   xkb_led_index_t caps_lock_led;
86   xkb_led_index_t num_lock_led;
87   xkb_led_index_t scroll_lock_led;
88   xkb_layout_index_t layout_idx;
89   uint32_t button_state;
90   int button_count[KEY_CNT];
91 
92   MetaBarrierManagerNative *barrier_manager;
93   MetaPointerConstraintImpl *pointer_constraint;
94 
95   MetaKeymapNative *keymap;
96   MetaInputSettings *input_settings;
97 
98   MetaViewportInfo *viewports;
99 
100   gboolean tablet_mode_switch_state;
101   gboolean has_touchscreen;
102   gboolean has_tablet_switch;
103   gboolean has_pointer;
104   gboolean touch_mode;
105   gboolean input_thread_initialized;
106 
107   /* keyboard repeat */
108   gboolean repeat;
109   uint32_t repeat_delay;
110   uint32_t repeat_interval;
111   uint32_t repeat_key;
112   uint32_t repeat_count;
113   ClutterInputDevice *repeat_device;
114   GSource *repeat_source;
115 
116   float pointer_x;
117   float pointer_y;
118 
119   /* Emulation of discrete scroll events out of smooth ones */
120   float accum_scroll_dx;
121   float accum_scroll_dy;
122 
123   gboolean released;
124 };
125 
126 #define META_TYPE_SEAT_IMPL meta_seat_impl_get_type ()
127 G_DECLARE_FINAL_TYPE (MetaSeatImpl, meta_seat_impl,
128                       META, SEAT_IMPL, GObject)
129 
130 MetaSeatImpl * meta_seat_impl_new (MetaSeatNative     *seat_native,
131                                    const char         *seat_id,
132                                    MetaSeatNativeFlag  flags);
133 
134 void meta_seat_impl_destroy (MetaSeatImpl *seat_impl);
135 
136 void meta_seat_impl_run_input_task (MetaSeatImpl *seat_impl,
137                                     GTask        *task,
138                                     GSourceFunc   dispatch_func);
139 
140 void meta_seat_impl_notify_key_in_impl (MetaSeatImpl       *seat_impl,
141                                         ClutterInputDevice *device,
142                                         uint64_t            time_us,
143                                         uint32_t            key,
144                                         uint32_t            state,
145                                         gboolean            update_keys);
146 
147 void meta_seat_impl_notify_relative_motion_in_impl (MetaSeatImpl       *seat_impl,
148                                                     ClutterInputDevice *input_device,
149                                                     uint64_t            time_us,
150                                                     float               dx,
151                                                     float               dy,
152                                                     float               dx_unaccel,
153                                                     float               dy_unaccel);
154 
155 void meta_seat_impl_notify_absolute_motion_in_impl (MetaSeatImpl       *seat_impl,
156                                                     ClutterInputDevice *input_device,
157                                                     uint64_t            time_us,
158                                                     float               x,
159                                                     float               y,
160                                                     double             *axes);
161 
162 void meta_seat_impl_notify_button_in_impl (MetaSeatImpl       *seat_impl,
163                                            ClutterInputDevice *input_device,
164                                            uint64_t            time_us,
165                                            uint32_t            button,
166                                            uint32_t            state);
167 
168 void meta_seat_impl_notify_scroll_continuous_in_impl (MetaSeatImpl             *seat_impl,
169                                                       ClutterInputDevice       *input_device,
170                                                       uint64_t                  time_us,
171                                                       double                    dx,
172                                                       double                    dy,
173                                                       ClutterScrollSource       source,
174                                                       ClutterScrollFinishFlags  flags);
175 
176 void meta_seat_impl_notify_discrete_scroll_in_impl (MetaSeatImpl        *seat_impl,
177                                                     ClutterInputDevice  *input_device,
178                                                     uint64_t             time_us,
179                                                     double               discrete_dx,
180                                                     double               discrete_dy,
181                                                     ClutterScrollSource  source);
182 
183 void meta_seat_impl_notify_touch_event_in_impl (MetaSeatImpl       *seat_impl,
184                                                 ClutterInputDevice *input_device,
185                                                 ClutterEventType    evtype,
186                                                 uint64_t            time_us,
187                                                 int                 slot,
188                                                 double              x,
189                                                 double              y);
190 
191 void meta_seat_impl_sync_leds_in_impl (MetaSeatImpl *seat_impl);
192 
193 MetaTouchState * meta_seat_impl_acquire_touch_state_in_impl (MetaSeatImpl *seat_impl,
194                                                              int           seat_slot);
195 MetaTouchState * meta_seat_impl_lookup_touch_state_in_impl (MetaSeatImpl *seat_impl,
196                                                             int           seat_slot);
197 void meta_seat_impl_release_touch_state_in_impl (MetaSeatImpl   *seat_impl,
198                                                  int             seat_slot);
199 
200 void meta_seat_impl_update_xkb_state_in_impl (MetaSeatImpl *seat_impl);
201 
202 void  meta_seat_impl_release_devices (MetaSeatImpl *seat_impl);
203 void  meta_seat_impl_reclaim_devices (MetaSeatImpl *seat_impl);
204 
205 struct xkb_state * meta_seat_impl_get_xkb_state_in_impl (MetaSeatImpl *seat_impl);
206 
207 void meta_seat_impl_set_keyboard_map (MetaSeatImpl      *seat_impl,
208                                       struct xkb_keymap *keymap);
209 
210 void meta_seat_impl_set_keyboard_layout_index (MetaSeatImpl       *seat_impl,
211                                                xkb_layout_index_t  idx);
212 
213 void meta_seat_impl_set_keyboard_repeat_in_impl (MetaSeatImpl *seat_impl,
214                                                  gboolean      repeat,
215                                                  uint32_t      delay,
216                                                  uint32_t      interval);
217 
218 MetaBarrierManagerNative * meta_seat_impl_get_barrier_manager (MetaSeatImpl *seat_impl);
219 
220 void meta_seat_impl_set_pointer_constraint (MetaSeatImpl              *seat_impl,
221                                             MetaPointerConstraintImpl *constraint_impl);
222 void meta_seat_impl_set_viewports (MetaSeatImpl     *seat_impl,
223                                    MetaViewportInfo *viewports);
224 
225 void meta_seat_impl_warp_pointer (MetaSeatImpl *seat_impl,
226                                   int           x,
227                                   int           y);
228 gboolean meta_seat_impl_query_state (MetaSeatImpl         *seat_impl,
229                                      ClutterInputDevice   *device,
230                                      ClutterEventSequence *sequence,
231                                      graphene_point_t     *coords,
232                                      ClutterModifierType  *modifiers);
233 ClutterInputDevice * meta_seat_impl_get_pointer (MetaSeatImpl *seat_impl);
234 ClutterInputDevice * meta_seat_impl_get_keyboard (MetaSeatImpl *seat_impl);
235 GSList * meta_seat_impl_get_devices_in_impl (MetaSeatImpl *seat_impl);
236 
237 MetaKeymapNative * meta_seat_impl_get_keymap (MetaSeatImpl *seat_impl);
238 
239 void meta_seat_impl_notify_kbd_a11y_flags_changed_in_impl (MetaSeatImpl          *seat_impl,
240                                                            MetaKeyboardA11yFlags  new_flags,
241                                                            MetaKeyboardA11yFlags  what_changed);
242 void meta_seat_impl_notify_kbd_a11y_mods_state_changed_in_impl (MetaSeatImpl   *seat_impl,
243                                                                 xkb_mod_mask_t  new_latched_mods,
244                                                                 xkb_mod_mask_t  new_locked_mods);
245 void meta_seat_impl_notify_bell_in_impl (MetaSeatImpl *seat_impl);
246 
247 MetaInputSettings * meta_seat_impl_get_input_settings (MetaSeatImpl *seat_impl);
248 
249 void meta_seat_impl_queue_main_thread_idle (MetaSeatImpl   *seat_impl,
250                                             GSourceFunc     func,
251                                             gpointer        user_data,
252                                             GDestroyNotify  destroy_notify);
253 
254 #endif /* META_SEAT_IMPL_H */
255