]> git.basschouten.com Git - openhab-addons.git/blob
2a26425e37c413d2aa143b20e00e485e12a1c882
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.linuxinput.internal.evdev4j.jnr;
14
15 import static org.openhab.binding.linuxinput.internal.evdev4j.Utils.constantFromInt;
16 import static org.openhab.binding.linuxinput.internal.evdev4j.jnr.Utils.trimEnd;
17
18 import java.util.Optional;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.linuxinput.internal.evdev4j.Utils;
22
23 import jnr.constants.Constant;
24 import jnr.ffi.LibraryLoader;
25 import jnr.ffi.Runtime;
26 import jnr.ffi.Struct;
27 import jnr.ffi.annotations.IgnoreError;
28 import jnr.ffi.annotations.In;
29 import jnr.ffi.annotations.Out;
30 import jnr.ffi.byref.PointerByReference;
31 import jnr.ffi.mapper.FunctionMapper;
32
33 /**
34  * JNR library for libdevdev library (libevdev.h).
35  *
36  * @author Thomas Weißschuh - Initial contribution
37  */
38 @NonNullByDefault
39 @IgnoreError
40 public interface EvdevLibrary {
41     static EvdevLibrary load() {
42         FunctionMapper evdevFunctionMapper = (functionName, context) -> "libevdev_" + trimEnd("_", functionName);
43         return LibraryLoader.create(EvdevLibrary.class).library("evdev").mapper(evdevFunctionMapper).load();
44     }
45
46     static Handle makeHandle(EvdevLibrary lib) {
47         return new Handle(Runtime.getRuntime(lib));
48     }
49
50     class Handle extends Struct {
51         public Handle(Runtime runtime) {
52             super(runtime);
53         }
54     }
55
56     enum GrabMode {
57         GRAB(3),
58         UNGRAB(4);
59
60         private int value;
61
62         GrabMode(int value) {
63             this.value = value;
64         }
65
66         public int getValue() {
67             return value;
68         }
69     }
70
71     final class InputEvent extends Struct {
72         public UnsignedLong sec = new UnsignedLong();
73         public UnsignedLong usec = new UnsignedLong();
74         public Unsigned16 type = new Unsigned16();
75         public Unsigned16 code = new Unsigned16();
76         public Signed32 value = new Signed32();
77
78         public InputEvent(Runtime runtime) {
79             super(runtime);
80         }
81     }
82
83     int new_from_fd(int fd, @Out PointerByReference handle);
84
85     void free(@In Handle handle);
86
87     int grab(@In Handle handle, int grab);
88
89     int next_event(@In Handle handle, int flags, @Out InputEvent event);
90
91     String event_type_get_name(int type);
92
93     String event_code_get_name(int type, int code);
94
95     String event_value_get_name(int type, int code, int value);
96
97     boolean has_event_type(@In Handle handle, int type);
98
99     int enable_event_type(@In Handle handle, int type);
100
101     int disable_event_type(@In Handle handle, int type);
102
103     boolean has_event_code(@In Handle handle, int type, int code);
104
105     int enable_event_code(@In Handle handle, int type, int code);
106
107     int disable_event_code(@In Handle handle, int type, int code);
108
109     String get_name(@In Handle handle);
110
111     void set_name(@In Handle handle, String name);
112
113     String get_phys(@In Handle handle);
114
115     String get_uniq(@In Handle handle);
116
117     int get_id_product(@In Handle handle);
118
119     int get_id_vendor(@In Handle handle);
120
121     int get_id_bustype(@In Handle handle);
122
123     int get_id_version(@In Handle handle);
124
125     int get_driver_version(@In Handle handle);
126
127     @SuppressWarnings("unused")
128     class ReadFlag {
129         private ReadFlag() {
130         }
131
132         public static final int SYNC = 1;
133         public static final int NORMAL = 2;
134         public static final int FORCE_SYNC = 4;
135         public static final int BLOCKING = 8;
136     }
137
138     class KeyEventValue {
139         private KeyEventValue() {
140         }
141
142         public static final int UP = 0;
143         public static final int DOWN = 1;
144         public static final int REPEAT = 2;
145     }
146
147     enum Type implements Constant {
148         SYN(0x00),
149         KEY(0x01),
150         REL(0x02),
151         ABS(0x03),
152         MSC(0x04),
153         SW(0x05),
154         LED(0x11),
155         SND(0x12),
156         REP(0x14),
157         FF(0x15),
158         PWR(0x16),
159         FF_STATUS(0x17),
160         MAX(0x1f),
161         CNT(0x20);
162
163         private final int i;
164
165         Type(int i) {
166             this.i = i;
167         }
168
169         public static Optional<Type> fromInt(int i) {
170             return constantFromInt(Type.values(), i);
171         }
172
173         @Override
174         public int intValue() {
175             return i;
176         }
177
178         @Override
179         public long longValue() {
180             return i;
181         }
182
183         @Override
184         public boolean defined() {
185             return true;
186         }
187     }
188
189     enum BusType implements Constant {
190         PCI(0x01),
191         ISAPNP(0x02),
192         USB(0x03),
193         HIL(0x04),
194         BLUETOOTH(0x05),
195         VIRTUAL(0x06),
196
197         ISA(0x10),
198         I8042(0x11),
199         XTKBD(0x12),
200         RS232(0x13),
201         GAMEPORT(0x14),
202         PARPORT(0x15),
203         AMIGA(0x16),
204         ADB(0x17),
205         I2C(0x18),
206         HOST(0x19),
207         GSC(0x1A),
208         ATARI(0x1B),
209         SPI(0x1C),
210         RMI(0x1D),
211         CEC(0x1E),
212         INTEL_ISHTP(0x1F);
213
214         private final int i;
215
216         BusType(int i) {
217             this.i = i;
218         }
219
220         public static Optional<BusType> fromInt(int i) {
221             return Utils.constantFromInt(BusType.values(), i);
222         }
223
224         @Override
225         public int intValue() {
226             return i;
227         }
228
229         @Override
230         public long longValue() {
231             return i;
232         }
233
234         @Override
235         public boolean defined() {
236             return true;
237         }
238     }
239 }