aboutsummaryrefslogtreecommitdiffstatsci
path: root/testDrm/src/Linux/Bindings/LibInput.cs
blob: aa72c8e13b2bdd61402cf4225d4b575e733d0348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#region License
//
// LibInput.cs
//
// Author:
//       Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2014 Stefanos Apostolopoulos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#endregion

#pragma warning disable 0169, 0219

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Linux
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    delegate int OpenRestrictedCallback(IntPtr path, int flags, IntPtr data);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    delegate void CloseRestrictedCallback(int fd, IntPtr data);

    class LibInput
    {
        internal const string lib = "libinput";

        [DllImport(lib, EntryPoint = "libinput_udev_create_context", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr CreateContext(InputInterface @interface,
            IntPtr user_data, IntPtr udev);

        [DllImport(lib, EntryPoint = "libinput_udev_assign_seat", CallingConvention = CallingConvention.Cdecl)]
        public static extern int AssignSeat(IntPtr libinput, string seat_id);

        [DllImport(lib, EntryPoint = "libinput_destroy", CallingConvention = CallingConvention.Cdecl)]
        public static extern void DestroyContext(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_event_destroy", CallingConvention = CallingConvention.Cdecl)]
        public static extern void DestroyEvent(IntPtr @event);

        [DllImport(lib, EntryPoint = "libinput_device_get_sysname", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr DeviceGetNameInternal(IntPtr device);
        public static string DeviceGetName(IntPtr device)
        {
            unsafe
            {
                return new string((sbyte*)DeviceGetNameInternal(device));
            }
        }

        [DllImport(lib, EntryPoint = "libinput_device_get_user_data", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr DeviceGetData(IntPtr device);

        [DllImport(lib, EntryPoint = "libinput_device_set_user_data", CallingConvention = CallingConvention.Cdecl)]
        public static extern void DeviceSetData(IntPtr device, IntPtr user_data);

        [DllImport(lib, EntryPoint = "libinput_device_get_output_name", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr DeviceGetOutputNameInternal(IntPtr device);
        public static string DeviceGetOutputName(IntPtr device)
        {
            unsafe
            {
                sbyte* pname = (sbyte*)DeviceGetOutputNameInternal(device);
                return pname == null ? String.Empty : new string(pname);
            }
        }

        [DllImport(lib, EntryPoint = "libinput_device_get_seat", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr DeviceGetSeat(IntPtr device);

        [DllImport(lib, EntryPoint = "libinput_device_has_capability", CallingConvention = CallingConvention.Cdecl)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeviceHasCapability(IntPtr device, DeviceCapability capability);

        [DllImport(lib, EntryPoint = "libinput_dispatch", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Dispatch(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_event_get_device", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr GetDevice(IntPtr @event);

        [DllImport(lib, EntryPoint = "libinput_get_event", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr GetEvent(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_event_get_keyboard_event", CallingConvention = CallingConvention.Cdecl)]
        public static extern KeyboardEvent GetKeyboardEvent(IntPtr @event);

        [DllImport(lib, EntryPoint = "libinput_event_get_pointer_event", CallingConvention = CallingConvention.Cdecl)]
        public static extern PointerEvent GetPointerEvent(IntPtr @event);

        [DllImport(lib, EntryPoint = "libinput_event_get_type", CallingConvention = CallingConvention.Cdecl)]
        public static extern InputEventType GetEventType(IntPtr @event);

        [DllImport(lib, EntryPoint = "libinput_get_fd", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetFD(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_next_event_type", CallingConvention = CallingConvention.Cdecl)]
        public static extern InputEventType NextEventType(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_resume", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Resume(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_suspend", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Suspend(IntPtr libinput);

        [DllImport(lib, EntryPoint = "libinput_seat_get_logical_name", CallingConvention = CallingConvention.Cdecl)]
        static extern public IntPtr SeatGetLogicalNameInternal(IntPtr seat);
        public static string SeatGetLogicalName(IntPtr seat)
        {
            unsafe
            {
                return new string((sbyte*)SeatGetLogicalNameInternal(seat));
            }
        }

        [DllImport(lib, EntryPoint = "libinput_seat_get_physical_name", CallingConvention = CallingConvention.Cdecl)]
        static extern public IntPtr SeatGetPhysicalNameInternal(IntPtr seat);
        public static string SeatGetPhysicalName(IntPtr seat)
        {
            unsafe
            {
                return new string((sbyte*)SeatGetPhysicalNameInternal(seat));
            }
        }
    }

    enum DeviceCapability
    {
        Keyboard = 0,
        Mouse,
        Touch
    }

    enum InputEventType
    {
        None = 0,

        DeviceAdded,
        DeviceRemoved,

        KeyboardKey = 300,

        PointerMotion = 400,
        PointerMotionAbsolute,
        PointerButton,
        PointerAxis,

        TouchDown = 500,
        TouchUP,
        TouchMotion,
        TouchCancel,

        /// \internal
        /// <summary>
        /// Signals the end of a set of touchpoints at one device sample
        /// time. This event has no coordinate information attached.
        /// </summary>
        TouchFrame
    }

    enum ButtonState
    {
        Released = 0,
        Pressed = 1
    }

    enum KeyState
    {
        Released = 0,
        Pressed = 1
    }

    enum PointerAxis
    {
        VerticalScroll = 0,
        HorizontalScroll = 1
    }

    struct Fixed24
    {
        internal readonly int Value;

        public static implicit operator double(Fixed24 n)
        {
            long l = ((1023L + 44L) << 52) + (1L << 51) + n.Value;
            unsafe
            {
                double d = *(double*)&l;
                return d - (3L << 43);
            }
        }

        public static implicit operator float(Fixed24 n)
        {
            return (float)(double)n;
        }

        public static explicit operator int(Fixed24 n)
        {
            return n.Value >> 8;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    class InputInterface
    {
        internal readonly IntPtr open;
        internal readonly IntPtr close;

        public InputInterface(
            OpenRestrictedCallback open_restricted,
            CloseRestrictedCallback close_restricted)
        {
            if (open_restricted == null || close_restricted == null)
                throw new ArgumentNullException();

            open = Marshal.GetFunctionPointerForDelegate(open_restricted);
            close = Marshal.GetFunctionPointerForDelegate(close_restricted);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    struct KeyboardEvent
    {
        IntPtr @event;

        public IntPtr BaseEvent { get { return GetBaseEvent(@event); } }
        public IntPtr Event { get { return @event; } }
        public uint Time { get { return GetTime(@event); } }
        public uint Key { get { return GetKey(@event); } }
        public uint KeyCount { get { return GetSeatKeyCount(@event); } }
        public KeyState KeyState { get { return GetKeyState(@event); } }

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_keyboard_get_time", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetTime(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_keyboard_get_base_event", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr GetBaseEvent(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_keyboard_get_seat_key_count", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetSeatKeyCount(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_keyboard_get_key", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetKey(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_keyboard_get_key_state", CallingConvention = CallingConvention.Cdecl)]
        static extern KeyState GetKeyState(IntPtr @event);
    }


    [StructLayout(LayoutKind.Sequential)]
    struct PointerEvent
    {
        IntPtr @event;

        public IntPtr BaseEvent { get { return GetBaseEvent(@event); } }
        public IntPtr Event { get { return @event; } }
        public uint Time { get { return GetTime(@event); } }
        public Linux.oldEvDev.EvdevButton Button { get { return (Linux.oldEvDev.EvdevButton)GetButton(@event); } }
        public uint ButtonCount { get { return GetButtonCount(@event); } }
        public ButtonState ButtonState { get { return GetButtonState(@event); } }
        public bool HasAxis(PointerAxis axis) { return HasAxis(@event, axis) != 0; }
        public double AxisValue(PointerAxis axis) { return GetAxisValue(@event, axis); }
        public double DeltaX { get { return GetDX(@event); } }
        public double DeltaY { get { return GetDY(@event); } }
        public double X { get { return GetAbsX(@event); } }
        public double Y { get { return GetAbsY(@event); } }
        public double TransformedX(int width) { return GetAbsXTransformed(@event, width); }
        public double TransformedY(int height) { return GetAbsYTransformed(@event, height); }

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_time", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetTime(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_base_event", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr GetBaseEvent(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_seat_key_count", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetSeatKeyCount(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_button", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetButton(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_seat_button_count", CallingConvention = CallingConvention.Cdecl)]
        static extern uint GetButtonCount(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_button_state", CallingConvention = CallingConvention.Cdecl)]
        static extern ButtonState GetButtonState(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_has_axis", CallingConvention = CallingConvention.Cdecl)]
        static extern int HasAxis(IntPtr @event, PointerAxis axis);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_axis_value", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetAxisValue(IntPtr @event, PointerAxis axis);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_dx", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetDX(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_dy", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetDY(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_absolute_x", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetAbsX(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_absolute_y", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetAbsY(IntPtr @event);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_absolute_x_transformed", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetAbsXTransformed(IntPtr @event, int width);

        [DllImport(LibInput.lib, EntryPoint = "libinput_event_pointer_get_absolute_y_transformed", CallingConvention = CallingConvention.Cdecl)]
        static extern double GetAbsYTransformed(IntPtr @event, int height);

		public override string ToString ()
		{
			return string.Format ("[PointerEvent: BaseEvent={0}, Event={1}, Time={2}, Button={3}, ButtonCount={4}, ButtonState={5}, DeltaX={6}, DeltaY={7}, X={8}, Y={9}]", BaseEvent, Event, Time, Button, ButtonCount, ButtonState, DeltaX, DeltaY, X, Y);
		}
    }
}