]> git.basschouten.com Git - openhab-addons.git/blob
1ef11ec85742dc9b828daabdcd439e53745143ca
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 event_type_get_max(int type);
102
103     int disable_event_type(@In Handle handle, int type);
104
105     boolean has_event_code(@In Handle handle, int type, int code);
106
107     int enable_event_code(@In Handle handle, int type, int code);
108
109     int disable_event_code(@In Handle handle, int type, int code);
110
111     String get_name(@In Handle handle);
112
113     void set_name(@In Handle handle, String name);
114
115     String get_phys(@In Handle handle);
116
117     String get_uniq(@In Handle handle);
118
119     int get_id_product(@In Handle handle);
120
121     int get_id_vendor(@In Handle handle);
122
123     int get_id_bustype(@In Handle handle);
124
125     int get_id_version(@In Handle handle);
126
127     int get_driver_version(@In Handle handle);
128
129     @SuppressWarnings("unused")
130     class ReadFlag {
131         private ReadFlag() {
132         }
133
134         public static final int SYNC = 1;
135         public static final int NORMAL = 2;
136         public static final int FORCE_SYNC = 4;
137         public static final int BLOCKING = 8;
138     }
139
140     class KeyEventValue {
141         private KeyEventValue() {
142         }
143
144         public static final int UP = 0;
145         public static final int DOWN = 1;
146         public static final int REPEAT = 2;
147     }
148
149     enum Type implements Constant {
150         SYN(0x00),
151         KEY(0x01),
152         REL(0x02),
153         ABS(0x03),
154         MSC(0x04),
155         SW(0x05),
156         LED(0x11),
157         SND(0x12),
158         REP(0x14),
159         FF(0x15),
160         PWR(0x16),
161         FF_STATUS(0x17),
162         MAX(0x1f),
163         CNT(0x20);
164
165         private final int i;
166
167         Type(int i) {
168             this.i = i;
169         }
170
171         public static Optional<Type> fromInt(int i) {
172             return constantFromInt(Type.values(), i);
173         }
174
175         @Override
176         public int intValue() {
177             return i;
178         }
179
180         @Override
181         public long longValue() {
182             return i;
183         }
184
185         @Override
186         public boolean defined() {
187             return true;
188         }
189     }
190
191     enum BusType implements Constant {
192         PCI(0x01),
193         ISAPNP(0x02),
194         USB(0x03),
195         HIL(0x04),
196         BLUETOOTH(0x05),
197         VIRTUAL(0x06),
198
199         ISA(0x10),
200         I8042(0x11),
201         XTKBD(0x12),
202         RS232(0x13),
203         GAMEPORT(0x14),
204         PARPORT(0x15),
205         AMIGA(0x16),
206         ADB(0x17),
207         I2C(0x18),
208         HOST(0x19),
209         GSC(0x1A),
210         ATARI(0x1B),
211         SPI(0x1C),
212         RMI(0x1D),
213         CEC(0x1E),
214         INTEL_ISHTP(0x1F);
215
216         private final int i;
217
218         BusType(int i) {
219             this.i = i;
220         }
221
222         public static Optional<BusType> fromInt(int i) {
223             return Utils.constantFromInt(BusType.values(), i);
224         }
225
226         @Override
227         public int intValue() {
228             return i;
229         }
230
231         @Override
232         public long longValue() {
233             return i;
234         }
235
236         @Override
237         public boolean defined() {
238             return true;
239         }
240     }
241 }