1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gdk.DeviceManager;
26 
27 private import gdk.Device;
28 private import gdk.Display;
29 private import gdk.c.functions;
30 public  import gdk.c.types;
31 private import glib.ListG;
32 private import gobject.ObjectG;
33 private import gobject.Signals;
34 public  import gtkc.gdktypes;
35 private import std.algorithm;
36 
37 
38 /**
39  * In addition to a single pointer and keyboard for user interface input,
40  * GDK contains support for a variety of input devices, including graphics
41  * tablets, touchscreens and multiple pointers/keyboards interacting
42  * simultaneously with the user interface. Such input devices often have
43  * additional features, such as sub-pixel positioning information and
44  * additional device-dependent information.
45  * 
46  * In order to query the device hierarchy and be aware of changes in the
47  * device hierarchy (such as virtual devices being created or removed, or
48  * physical devices being plugged or unplugged), GDK provides
49  * #GdkDeviceManager.
50  * 
51  * By default, and if the platform supports it, GDK is aware of multiple
52  * keyboard/pointer pairs and multitouch devices. This behavior can be
53  * changed by calling gdk_disable_multidevice() before gdk_display_open().
54  * There should rarely be a need to do that though, since GDK defaults
55  * to a compatibility mode in which it will emit just one enter/leave
56  * event pair for all devices on a window. To enable per-device
57  * enter/leave events and other multi-pointer interaction features,
58  * gdk_window_set_support_multidevice() must be called on
59  * #GdkWindows (or gtk_widget_set_support_multidevice() on widgets).
60  * window. See the gdk_window_set_support_multidevice() documentation
61  * for more information.
62  * 
63  * On X11, multi-device support is implemented through XInput 2.
64  * Unless gdk_disable_multidevice() is called, the XInput 2
65  * #GdkDeviceManager implementation will be used as the input source.
66  * Otherwise either the core or XInput 1 implementations will be used.
67  * 
68  * For simple applications that don’t have any special interest in
69  * input devices, the so-called “client pointer”
70  * provides a reasonable approximation to a simple setup with a single
71  * pointer and keyboard. The device that has been set as the client
72  * pointer can be accessed via gdk_device_manager_get_client_pointer().
73  * 
74  * Conceptually, in multidevice mode there are 2 device types. Virtual
75  * devices (or master devices) are represented by the pointer cursors
76  * and keyboard foci that are seen on the screen. Physical devices (or
77  * slave devices) represent the hardware that is controlling the virtual
78  * devices, and thus have no visible cursor on the screen.
79  * 
80  * Virtual devices are always paired, so there is a keyboard device for every
81  * pointer device. Associations between devices may be inspected through
82  * gdk_device_get_associated_device().
83  * 
84  * There may be several virtual devices, and several physical devices could
85  * be controlling each of these virtual devices. Physical devices may also
86  * be “floating”, which means they are not attached to any virtual device.
87  * 
88  * # Master and slave devices
89  * 
90  * |[
91  * carlos@sacarino:~$ xinput list
92  * ⎡ Virtual core pointer                          id=2    [master pointer  (3)]
93  * ⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
94  * ⎜   ↳ Wacom ISDv4 E6 Pen stylus                 id=10   [slave  pointer  (2)]
95  * ⎜   ↳ Wacom ISDv4 E6 Finger touch               id=11   [slave  pointer  (2)]
96  * ⎜   ↳ SynPS/2 Synaptics TouchPad                id=13   [slave  pointer  (2)]
97  * ⎜   ↳ TPPS/2 IBM TrackPoint                     id=14   [slave  pointer  (2)]
98  * ⎜   ↳ Wacom ISDv4 E6 Pen eraser                 id=16   [slave  pointer  (2)]
99  * ⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
100  * ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
101  * ↳ Power Button                              id=6    [slave  keyboard (3)]
102  * ↳ Video Bus                                 id=7    [slave  keyboard (3)]
103  * ↳ Sleep Button                              id=8    [slave  keyboard (3)]
104  * ↳ Integrated Camera                         id=9    [slave  keyboard (3)]
105  * ↳ AT Translated Set 2 keyboard              id=12   [slave  keyboard (3)]
106  * ↳ ThinkPad Extra Buttons                    id=15   [slave  keyboard (3)]
107  * ]|
108  * 
109  * By default, GDK will automatically listen for events coming from all
110  * master devices, setting the #GdkDevice for all events coming from input
111  * devices. Events containing device information are #GDK_MOTION_NOTIFY,
112  * #GDK_BUTTON_PRESS, #GDK_2BUTTON_PRESS, #GDK_3BUTTON_PRESS,
113  * #GDK_BUTTON_RELEASE, #GDK_SCROLL, #GDK_KEY_PRESS, #GDK_KEY_RELEASE,
114  * #GDK_ENTER_NOTIFY, #GDK_LEAVE_NOTIFY, #GDK_FOCUS_CHANGE,
115  * #GDK_PROXIMITY_IN, #GDK_PROXIMITY_OUT, #GDK_DRAG_ENTER, #GDK_DRAG_LEAVE,
116  * #GDK_DRAG_MOTION, #GDK_DRAG_STATUS, #GDK_DROP_START, #GDK_DROP_FINISHED
117  * and #GDK_GRAB_BROKEN. When dealing with an event on a master device,
118  * it is possible to get the source (slave) device that the event originated
119  * from via gdk_event_get_source_device().
120  * 
121  * On a standard session, all physical devices are connected by default to
122  * the "Virtual Core Pointer/Keyboard" master devices, hence routing all events
123  * through these. This behavior is only modified by device grabs, where the
124  * slave device is temporarily detached for as long as the grab is held, and
125  * more permanently by user modifications to the device hierarchy.
126  * 
127  * On certain application specific setups, it may make sense
128  * to detach a physical device from its master pointer, and mapping it to
129  * an specific window. This can be achieved by the combination of
130  * gdk_device_grab() and gdk_device_set_mode().
131  * 
132  * In order to listen for events coming from devices
133  * other than a virtual device, gdk_window_set_device_events() must be
134  * called. Generally, this function can be used to modify the event mask
135  * for any given device.
136  * 
137  * Input devices may also provide additional information besides X/Y.
138  * For example, graphics tablets may also provide pressure and X/Y tilt
139  * information. This information is device-dependent, and may be
140  * queried through gdk_device_get_axis(). In multidevice mode, virtual
141  * devices will change axes in order to always represent the physical
142  * device that is routing events through it. Whenever the physical device
143  * changes, the #GdkDevice:n-axes property will be notified, and
144  * gdk_device_list_axes() will return the new device axes.
145  * 
146  * Devices may also have associated “keys” or
147  * macro buttons. Such keys can be globally set to map into normal X
148  * keyboard events. The mapping is set using gdk_device_set_key().
149  * 
150  * In GTK+ 3.20, a new #GdkSeat object has been introduced that
151  * supersedes #GdkDeviceManager and should be preferred in newly
152  * written code.
153  */
154 public class DeviceManager : ObjectG
155 {
156 	/** the main Gtk struct */
157 	protected GdkDeviceManager* gdkDeviceManager;
158 
159 	/** Get the main Gtk struct */
160 	public GdkDeviceManager* getDeviceManagerStruct(bool transferOwnership = false)
161 	{
162 		if (transferOwnership)
163 			ownedRef = false;
164 		return gdkDeviceManager;
165 	}
166 
167 	/** the main Gtk struct as a void* */
168 	protected override void* getStruct()
169 	{
170 		return cast(void*)gdkDeviceManager;
171 	}
172 
173 	/**
174 	 * Sets our main struct and passes it to the parent class.
175 	 */
176 	public this (GdkDeviceManager* gdkDeviceManager, bool ownedRef = false)
177 	{
178 		this.gdkDeviceManager = gdkDeviceManager;
179 		super(cast(GObject*)gdkDeviceManager, ownedRef);
180 	}
181 
182 
183 	/** */
184 	public static GType getType()
185 	{
186 		return gdk_device_manager_get_type();
187 	}
188 
189 	/**
190 	 * Returns the client pointer, that is, the master pointer that acts as the core pointer
191 	 * for this application. In X11, window managers may change this depending on the interaction
192 	 * pattern under the presence of several pointers.
193 	 *
194 	 * You should use this function seldomly, only in code that isn’t triggered by a #GdkEvent
195 	 * and there aren’t other means to get a meaningful #GdkDevice to operate on.
196 	 *
197 	 * Deprecated: Use gdk_seat_get_pointer() instead.
198 	 *
199 	 * Returns: The client pointer. This memory is
200 	 *     owned by GDK and must not be freed or unreferenced.
201 	 *
202 	 * Since: 3.0
203 	 */
204 	public Device getClientPointer()
205 	{
206 		auto p = gdk_device_manager_get_client_pointer(gdkDeviceManager);
207 
208 		if(p is null)
209 		{
210 			return null;
211 		}
212 
213 		return ObjectG.getDObject!(Device)(cast(GdkDevice*) p);
214 	}
215 
216 	/**
217 	 * Gets the #GdkDisplay associated to @device_manager.
218 	 *
219 	 * Returns: the #GdkDisplay to which
220 	 *     @device_manager is associated to, or #NULL. This memory is
221 	 *     owned by GDK and must not be freed or unreferenced.
222 	 *
223 	 * Since: 3.0
224 	 */
225 	public Display getDisplay()
226 	{
227 		auto p = gdk_device_manager_get_display(gdkDeviceManager);
228 
229 		if(p is null)
230 		{
231 			return null;
232 		}
233 
234 		return ObjectG.getDObject!(Display)(cast(GdkDisplay*) p);
235 	}
236 
237 	/**
238 	 * Returns the list of devices of type @type currently attached to
239 	 * @device_manager.
240 	 *
241 	 * Deprecated: , use gdk_seat_get_pointer(), gdk_seat_get_keyboard()
242 	 * and gdk_seat_get_slaves() instead.
243 	 *
244 	 * Params:
245 	 *     type = device type to get.
246 	 *
247 	 * Returns: a list of
248 	 *     #GdkDevices. The returned list must be
249 	 *     freed with g_list_free (). The list elements are owned by
250 	 *     GTK+ and must not be freed or unreffed.
251 	 *
252 	 * Since: 3.0
253 	 */
254 	public ListG listDevices(GdkDeviceType type)
255 	{
256 		auto p = gdk_device_manager_list_devices(gdkDeviceManager, type);
257 
258 		if(p is null)
259 		{
260 			return null;
261 		}
262 
263 		return new ListG(cast(GList*) p);
264 	}
265 
266 	protected class OnDeviceAddedDelegateWrapper
267 	{
268 		void delegate(Device, DeviceManager) dlg;
269 		gulong handlerId;
270 
271 		this(void delegate(Device, DeviceManager) dlg)
272 		{
273 			this.dlg = dlg;
274 			onDeviceAddedListeners ~= this;
275 		}
276 
277 		void remove(OnDeviceAddedDelegateWrapper source)
278 		{
279 			foreach(index, wrapper; onDeviceAddedListeners)
280 			{
281 				if (wrapper.handlerId == source.handlerId)
282 				{
283 					onDeviceAddedListeners[index] = null;
284 					onDeviceAddedListeners = std.algorithm.remove(onDeviceAddedListeners, index);
285 					break;
286 				}
287 			}
288 		}
289 	}
290 	OnDeviceAddedDelegateWrapper[] onDeviceAddedListeners;
291 
292 	/**
293 	 * The ::device-added signal is emitted either when a new master
294 	 * pointer is created, or when a slave (Hardware) input device
295 	 * is plugged in.
296 	 *
297 	 * Params:
298 	 *     device = the newly added #GdkDevice.
299 	 */
300 	gulong addOnDeviceAdded(void delegate(Device, DeviceManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
301 	{
302 		auto wrapper = new OnDeviceAddedDelegateWrapper(dlg);
303 		wrapper.handlerId = Signals.connectData(
304 			this,
305 			"device-added",
306 			cast(GCallback)&callBackDeviceAdded,
307 			cast(void*)wrapper,
308 			cast(GClosureNotify)&callBackDeviceAddedDestroy,
309 			connectFlags);
310 		return wrapper.handlerId;
311 	}
312 
313 	extern(C) static void callBackDeviceAdded(GdkDeviceManager* devicemanagerStruct, GdkDevice* device, OnDeviceAddedDelegateWrapper wrapper)
314 	{
315 		wrapper.dlg(ObjectG.getDObject!(Device)(device), wrapper.outer);
316 	}
317 
318 	extern(C) static void callBackDeviceAddedDestroy(OnDeviceAddedDelegateWrapper wrapper, GClosure* closure)
319 	{
320 		wrapper.remove(wrapper);
321 	}
322 
323 	protected class OnDeviceChangedDelegateWrapper
324 	{
325 		void delegate(Device, DeviceManager) dlg;
326 		gulong handlerId;
327 
328 		this(void delegate(Device, DeviceManager) dlg)
329 		{
330 			this.dlg = dlg;
331 			onDeviceChangedListeners ~= this;
332 		}
333 
334 		void remove(OnDeviceChangedDelegateWrapper source)
335 		{
336 			foreach(index, wrapper; onDeviceChangedListeners)
337 			{
338 				if (wrapper.handlerId == source.handlerId)
339 				{
340 					onDeviceChangedListeners[index] = null;
341 					onDeviceChangedListeners = std.algorithm.remove(onDeviceChangedListeners, index);
342 					break;
343 				}
344 			}
345 		}
346 	}
347 	OnDeviceChangedDelegateWrapper[] onDeviceChangedListeners;
348 
349 	/**
350 	 * The ::device-changed signal is emitted whenever a device
351 	 * has changed in the hierarchy, either slave devices being
352 	 * disconnected from their master device or connected to
353 	 * another one, or master devices being added or removed
354 	 * a slave device.
355 	 *
356 	 * If a slave device is detached from all master devices
357 	 * (gdk_device_get_associated_device() returns %NULL), its
358 	 * #GdkDeviceType will change to %GDK_DEVICE_TYPE_FLOATING,
359 	 * if it's attached, it will change to %GDK_DEVICE_TYPE_SLAVE.
360 	 *
361 	 * Params:
362 	 *     device = the #GdkDevice that changed.
363 	 */
364 	gulong addOnDeviceChanged(void delegate(Device, DeviceManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
365 	{
366 		auto wrapper = new OnDeviceChangedDelegateWrapper(dlg);
367 		wrapper.handlerId = Signals.connectData(
368 			this,
369 			"device-changed",
370 			cast(GCallback)&callBackDeviceChanged,
371 			cast(void*)wrapper,
372 			cast(GClosureNotify)&callBackDeviceChangedDestroy,
373 			connectFlags);
374 		return wrapper.handlerId;
375 	}
376 
377 	extern(C) static void callBackDeviceChanged(GdkDeviceManager* devicemanagerStruct, GdkDevice* device, OnDeviceChangedDelegateWrapper wrapper)
378 	{
379 		wrapper.dlg(ObjectG.getDObject!(Device)(device), wrapper.outer);
380 	}
381 
382 	extern(C) static void callBackDeviceChangedDestroy(OnDeviceChangedDelegateWrapper wrapper, GClosure* closure)
383 	{
384 		wrapper.remove(wrapper);
385 	}
386 
387 	protected class OnDeviceRemovedDelegateWrapper
388 	{
389 		void delegate(Device, DeviceManager) dlg;
390 		gulong handlerId;
391 
392 		this(void delegate(Device, DeviceManager) dlg)
393 		{
394 			this.dlg = dlg;
395 			onDeviceRemovedListeners ~= this;
396 		}
397 
398 		void remove(OnDeviceRemovedDelegateWrapper source)
399 		{
400 			foreach(index, wrapper; onDeviceRemovedListeners)
401 			{
402 				if (wrapper.handlerId == source.handlerId)
403 				{
404 					onDeviceRemovedListeners[index] = null;
405 					onDeviceRemovedListeners = std.algorithm.remove(onDeviceRemovedListeners, index);
406 					break;
407 				}
408 			}
409 		}
410 	}
411 	OnDeviceRemovedDelegateWrapper[] onDeviceRemovedListeners;
412 
413 	/**
414 	 * The ::device-removed signal is emitted either when a master
415 	 * pointer is removed, or when a slave (Hardware) input device
416 	 * is unplugged.
417 	 *
418 	 * Params:
419 	 *     device = the just removed #GdkDevice.
420 	 */
421 	gulong addOnDeviceRemoved(void delegate(Device, DeviceManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
422 	{
423 		auto wrapper = new OnDeviceRemovedDelegateWrapper(dlg);
424 		wrapper.handlerId = Signals.connectData(
425 			this,
426 			"device-removed",
427 			cast(GCallback)&callBackDeviceRemoved,
428 			cast(void*)wrapper,
429 			cast(GClosureNotify)&callBackDeviceRemovedDestroy,
430 			connectFlags);
431 		return wrapper.handlerId;
432 	}
433 
434 	extern(C) static void callBackDeviceRemoved(GdkDeviceManager* devicemanagerStruct, GdkDevice* device, OnDeviceRemovedDelegateWrapper wrapper)
435 	{
436 		wrapper.dlg(ObjectG.getDObject!(Device)(device), wrapper.outer);
437 	}
438 
439 	extern(C) static void callBackDeviceRemovedDestroy(OnDeviceRemovedDelegateWrapper wrapper, GClosure* closure)
440 	{
441 		wrapper.remove(wrapper);
442 	}
443 
444 	/**
445 	 * Disables multidevice support in GDK. This call must happen prior
446 	 * to gdk_display_open(), gtk_init(), gtk_init_with_args() or
447 	 * gtk_init_check() in order to take effect.
448 	 *
449 	 * Most common GTK+ applications won’t ever need to call this. Only
450 	 * applications that do mixed GDK/Xlib calls could want to disable
451 	 * multidevice support if such Xlib code deals with input devices in
452 	 * any way and doesn’t observe the presence of XInput 2.
453 	 *
454 	 * Since: 3.0
455 	 */
456 	public static void disableMultidevice()
457 	{
458 		gdk_disable_multidevice();
459 	}
460 }