1 /* Copyright (c) 2012 Tobias Wolf, All Rights Reserved
2  *
3  * The contents of this file is dual-licensed under 2
4  * alternative Open Source/Free licenses: LGPL 2.1 or later and
5  * Apache License 2.0. (starting with JNA version 4.0.0).
6  *
7  * You can freely decide which license you want to apply to
8  * the project.
9  *
10  * You may obtain a copy of the LGPL License at:
11  *
12  * http://www.gnu.org/licenses/licenses.html
13  *
14  * A copy is also included in the downloadable source code package
15  * containing JNA, in file "LGPL2.1".
16  *
17  * You may obtain a copy of the Apache License at:
18  *
19  * http://www.apache.org/licenses/
20  *
21  * A copy is also included in the downloadable source code package
22  * containing JNA, in file "AL2.0".
23  */
24 package com.sun.jna.platform.win32;
25 
26 import com.sun.jna.Native;
27 import com.sun.jna.Pointer;
28 import com.sun.jna.Structure;
29 import com.sun.jna.Structure.FieldOrder;
30 import com.sun.jna.platform.win32.Guid.GUID;
31 import com.sun.jna.platform.win32.WinDef.LONG;
32 import com.sun.jna.platform.win32.WinNT.HANDLE;
33 import com.sun.jna.platform.win32.WinUser.HDEVNOTIFY;
34 import com.sun.jna.win32.W32APITypeMapper;
35 import java.nio.charset.StandardCharsets;
36 import java.util.logging.Logger;
37 
38 /**
39  * Based on dbt.h (various types)
40  *
41  * @author Tobias Wolf, wolf.tobias@gmx.net
42  */
43 public interface DBT {
44 
45     /** The dbt no disk space. */
46     int DBT_NO_DISK_SPACE = 0x0047;
47 
48     /** The dbt low disk space. */
49     int DBT_LOW_DISK_SPACE = 0x0048;
50 
51     /** The dbt configmgprivate. */
52     int DBT_CONFIGMGPRIVATE = 0x7FFF;
53 
54     /** The dbt devicearrival. */
55     int DBT_DEVICEARRIVAL = 0x8000;
56 
57     /** The dbt devicequeryremove. */
58     int DBT_DEVICEQUERYREMOVE = 0x8001;
59 
60     /** The dbt devicequeryremovefailed. */
61     int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
62 
63     /** The dbt deviceremovepending. */
64     int DBT_DEVICEREMOVEPENDING = 0x8003;
65 
66     /** The dbt deviceremovecomplete. */
67     int DBT_DEVICEREMOVECOMPLETE = 0x8004;
68 
69     /** A device has been added to or removed from the system. */
70     int DBT_DEVNODES_CHANGED = 0x0007;
71 
72     /** The dbt devicetypespecific. */
73     int DBT_DEVICETYPESPECIFIC = 0x8005;
74 
75     /** The dbt customevent. */
76     int DBT_CUSTOMEVENT = 0x8006;
77 
78     /** The guid devinterface usb device. */
79     GUID GUID_DEVINTERFACE_USB_DEVICE = new GUID(
80             "{A5DCBF10-6530-11D2-901F-00C04FB951ED}");
81 
82     /** The guid devinterface hid. */
83     GUID GUID_DEVINTERFACE_HID = new GUID("{4D1E55B2-F16F-11CF-88CB-001111000030}");
84 
85     /** The guid devinterface volume. */
86     GUID GUID_DEVINTERFACE_VOLUME = new GUID("{53F5630D-B6BF-11D0-94F2-00A0C91EFB8B}");
87 
88     /** The guid devinterface keyboard. */
89     GUID GUID_DEVINTERFACE_KEYBOARD = new GUID("{884b96c3-56ef-11d1-bc8c-00a0c91405dd}");
90 
91     /** The guid devinterface mouse. */
92     GUID GUID_DEVINTERFACE_MOUSE = new GUID("{378DE44C-56EF-11D1-BC8C-00A0C91405DD}");
93 
94     /**
95      * The Class DEV_BROADCAST_HDR.
96      */
97     @FieldOrder({"dbch_size", "dbch_devicetype", "dbch_reserved"})
98     public class DEV_BROADCAST_HDR extends Structure {
99         /** The dbch_size. */
100         public int dbch_size;
101 
102         /** The dbch_devicetype. */
103         public int dbch_devicetype;
104 
105         /** The dbch_reserved. */
106         public int dbch_reserved;
107 
108         /**
109          * Instantiates a new dev broadcast hdr.
110          */
DEV_BROADCAST_HDR()111         public DEV_BROADCAST_HDR() {
112             super();
113         }
114 
115         /**
116          * Instantiates a new dev broadcast hdr.
117          *
118          * @param pointer
119          *            the pointer
120          */
DEV_BROADCAST_HDR(long pointer)121         public DEV_BROADCAST_HDR(long pointer) {
122             this(new Pointer(pointer));
123         }
124 
125         /**
126          * Instantiates a new dev broadcast hdr.
127          *
128          * @param memory
129          *            the memory
130          */
DEV_BROADCAST_HDR(Pointer memory)131         public DEV_BROADCAST_HDR(Pointer memory) {
132             super(memory);
133             read();
134         }
135     }
136 
137     /** The dbt devtyp oem. */
138     int DBT_DEVTYP_OEM = 0x00000000;
139 
140     /** The dbt devtyp devnode. */
141     int DBT_DEVTYP_DEVNODE = 0x00000001;
142 
143     /** The dbt devtyp volume. */
144     int DBT_DEVTYP_VOLUME = 0x00000002;
145 
146     /** The dbt devtyp port. */
147     int DBT_DEVTYP_PORT = 0x00000003;
148 
149     /** The dbt devtyp net. */
150     int DBT_DEVTYP_NET = 0x00000004;
151 
152     /** The dbt devtyp deviceinterface. */
153     int DBT_DEVTYP_DEVICEINTERFACE = 0x00000005;
154 
155     /** The dbt devtyp handle. */
156     int DBT_DEVTYP_HANDLE = 0x00000006;
157 
158     /**
159      * The Class DEV_BROADCAST_OEM.
160      */
161     @FieldOrder({"dbco_size", "dbco_devicetype", "dbco_reserved", "dbco_identifier", "dbco_suppfunc"})
162     public class DEV_BROADCAST_OEM extends Structure {
163         /** The dbco_size. */
164         public int dbco_size;
165 
166         /** The dbco_devicetype. */
167         public int dbco_devicetype;
168 
169         /** The dbco_reserved. */
170         public int dbco_reserved;
171 
172         /** The dbco_identifier. */
173         public int dbco_identifier;
174 
175         /** The dbco_suppfunc. */
176         public int dbco_suppfunc;
177 
178         /**
179          * Instantiates a new dev broadcast oem.
180          */
DEV_BROADCAST_OEM()181         public DEV_BROADCAST_OEM() {
182             super();
183         }
184 
185         /**
186          * Instantiates a new dev broadcast oem.
187          *
188          * @param memory
189          *            the memory
190          */
DEV_BROADCAST_OEM(Pointer memory)191         public DEV_BROADCAST_OEM(Pointer memory) {
192             super(memory);
193             read();
194         }
195     }
196 
197     /**
198      * The Class DEV_BROADCAST_DEVNODE.
199      */
200     @FieldOrder({"dbcd_size", "dbcd_devicetype", "dbcd_reserved", "dbcd_devnode"})
201     public class DEV_BROADCAST_DEVNODE extends Structure {
202         /** The dbcd_size. */
203         public int dbcd_size;
204 
205         /** The dbcd_devicetype. */
206         public int dbcd_devicetype;
207 
208         /** The dbcd_reserved. */
209         public int dbcd_reserved;
210 
211         /** The dbcd_devnode. */
212         public int dbcd_devnode;
213 
214         /**
215          * Instantiates a new dev broadcast devnode.
216          */
DEV_BROADCAST_DEVNODE()217         public DEV_BROADCAST_DEVNODE() {
218             super();
219         }
220 
221         /**
222          * Instantiates a new dev broadcast devnode.
223          *
224          * @param memory
225          *            the memory
226          */
DEV_BROADCAST_DEVNODE(Pointer memory)227         public DEV_BROADCAST_DEVNODE(Pointer memory) {
228             super(memory);
229             read();
230         }
231     }
232 
233     /**
234      * The Class DEV_BROADCAST_VOLUME.
235      */
236     @FieldOrder({"dbcv_size", "dbcv_devicetype", "dbcv_reserved", "dbcv_unitmask", "dbcv_flags"})
237     public class DEV_BROADCAST_VOLUME extends Structure {
238         /** The dbcv_size. */
239         public int dbcv_size;
240 
241         /** The dbcv_devicetype. */
242         public int dbcv_devicetype;
243 
244         /** The dbcv_reserved. */
245         public int dbcv_reserved;
246 
247         /** The dbcv_unitmask. */
248         public int dbcv_unitmask;
249 
250         /** The dbcv_flags. */
251         public short dbcv_flags;
252 
253         /**
254          * Instantiates a new dev broadcast volume.
255          */
DEV_BROADCAST_VOLUME()256         public DEV_BROADCAST_VOLUME() {
257             super();
258         }
259 
260         /**
261          * Instantiates a new dev broadcast volume.
262          *
263          * @param memory
264          *            the memory
265          */
DEV_BROADCAST_VOLUME(Pointer memory)266         public DEV_BROADCAST_VOLUME(Pointer memory) {
267             super(memory);
268             read();
269         }
270     }
271 
272     /** The dbt change affects media in drive, not physical device or drive. */
273     int DBTF_MEDIA = 0x0001;
274 
275     /** The dbt indicated logical volume is a network volume. */
276     int DBTF_NET = 0x0002;
277 
278     /**
279      * The Class DEV_BROADCAST_PORT.
280      */
281     @FieldOrder({"dbcp_size", "dbcp_devicetype", "dbcp_reserved", "dbcp_name"})
282     public class DEV_BROADCAST_PORT extends Structure {
283         /** The dbcp_size. */
284         public int dbcp_size;
285 
286         /** The dbcp_devicetype. */
287         public int dbcp_devicetype;
288 
289         /** The dbcp_reserved. */
290         public int dbcp_reserved;
291 
292         /** The dbcp_name. */
293         public byte[] dbcp_name = new byte[1];
294 
295         /**
296          * Instantiates a new dev broadcast port.
297          */
DEV_BROADCAST_PORT()298         public DEV_BROADCAST_PORT() {
299             super();
300         }
301 
302         /**
303          * Instantiates a new dev broadcast port.
304          *
305          * @param memory
306          *            the memory
307          */
DEV_BROADCAST_PORT(Pointer memory)308         public DEV_BROADCAST_PORT(Pointer memory) {
309             super(memory);
310             read();
311         }
312 
313         @Override
read()314         public void read() {
315             int size = getPointer().getInt(0); // Read dbcp_size (first field in structure)
316             this.dbcp_name = new byte[size - this.fieldOffset("dbcp_name")];
317             super.read();
318         }
319 
320         /**
321          * Gets the dbcc_name.
322          *
323          * @return the dbcp_name
324          */
getDbcpName()325         public String getDbcpName() {
326             if(W32APITypeMapper.DEFAULT == W32APITypeMapper.ASCII) {
327                 return Native.toString(this.dbcp_name);
328             } else {
329                 return new String(this.dbcp_name, StandardCharsets.UTF_16LE);
330             }
331         }
332     }
333 
334     /**
335      * The Class DEV_BROADCAST_NET.
336      */
337     @FieldOrder({"dbcn_size", "dbcn_devicetype",
338                 "dbcn_reserved", "dbcn_resource", "dbcn_flags"})
339     public class DEV_BROADCAST_NET extends Structure {
340         /** The dbcn_size. */
341         public int dbcn_size;
342 
343         /** The dbcn_devicetype. */
344         public int dbcn_devicetype;
345 
346         /** The dbcn_reserved. */
347         public int dbcn_reserved;
348 
349         /** The dbcn_resource. */
350         public int dbcn_resource;
351 
352         /** The dbcn_flags. */
353         public int dbcn_flags;
354 
355         /**
356          * Instantiates a new dev broadcast net.
357          */
DEV_BROADCAST_NET()358         public DEV_BROADCAST_NET() {
359             super();
360         }
361 
362         /**
363          * Instantiates a new dev broadcast net.
364          *
365          * @param memory
366          *            the memory
367          */
DEV_BROADCAST_NET(Pointer memory)368         public DEV_BROADCAST_NET(Pointer memory) {
369             super(memory);
370             read();
371         }
372     }
373 
374     /**
375      * The Class DEV_BROADCAST_DEVICEINTERFACE.
376      */
377     @FieldOrder({"dbcc_size", "dbcc_devicetype",
378         "dbcc_reserved", "dbcc_classguid", "dbcc_name"})
379     public class DEV_BROADCAST_DEVICEINTERFACE extends Structure {
380         /** The dbcc_size. */
381         public int dbcc_size;
382 
383         /** The dbcc_devicetype. */
384         public int dbcc_devicetype;
385 
386         /** The dbcc_reserved. */
387         public int dbcc_reserved;
388 
389         /** The dbcc_classguid. */
390         public GUID dbcc_classguid;
391 
392         /** The dbcc_name. */
393         public char[] dbcc_name = new char[1];
394 
395         /**
396          * Instantiates a new dev broadcast deviceinterface.
397          */
DEV_BROADCAST_DEVICEINTERFACE()398         public DEV_BROADCAST_DEVICEINTERFACE() {
399             super();
400         }
401 
402         /**
403          * Dev broadcast hdr.
404          *
405          * @param pointer
406          *            the pointer
407          */
DEV_BROADCAST_DEVICEINTERFACE(long pointer)408         public DEV_BROADCAST_DEVICEINTERFACE(long pointer) {
409             this(new Pointer(pointer));
410         }
411 
412         /**
413          * Instantiates a new dev broadcast deviceinterface.
414          *
415          * @param memory
416          *            the memory
417          */
DEV_BROADCAST_DEVICEINTERFACE(Pointer memory)418         public DEV_BROADCAST_DEVICEINTERFACE(Pointer memory) {
419             super(memory);
420             read();
421         }
422 
423         @Override
read()424         public void read() {
425             if(W32APITypeMapper.DEFAULT == W32APITypeMapper.ASCII) {
426                 Logger.getLogger(DBT.class.getName()).warning("DEV_BROADCAST_DEVICEINTERFACE must not be used with w32.ascii = true!");
427             }
428             int size = getPointer().getInt(0); // Read dbcc_size (first field in structure)
429             // figure out how long dbcc_name should be based on the size
430             int len = (size - this.fieldOffset("dbcc_name")) / Native.WCHAR_SIZE;
431             this.dbcc_name = new char[len];
432             super.read();
433         }
434 
435         /**
436          * Gets the dbcc_name.
437          *
438          * @return the dbcc_name
439          */
getDbcc_name()440         public String getDbcc_name() {
441             return Native.toString(this.dbcc_name);
442         }
443     }
444 
445     /**
446      * The Class DEV_BROADCAST_HANDLE.
447      */
448     @FieldOrder({"dbch_size", "dbch_devicetype", "dbch_reserved", "dbch_handle",
449         "dbch_hdevnotify", "dbch_eventguid", "dbch_nameoffset", "dbch_data"})
450     public class DEV_BROADCAST_HANDLE extends Structure {
451         /** The dbch_size. */
452         public int dbch_size;
453 
454         /** The dbch_devicetype. */
455         public int dbch_devicetype;
456 
457         /** The dbch_reserved. */
458         public int dbch_reserved;
459 
460         /** The dbch_handle. */
461         public HANDLE dbch_handle;
462 
463         /** The dbch_hdevnotify. */
464         public HDEVNOTIFY dbch_hdevnotify;
465 
466         /** The dbch_eventguid. */
467         public GUID dbch_eventguid;
468 
469         /** The dbch_nameoffset. */
470         public LONG dbch_nameoffset;
471 
472         /** The dbch_data. */
473         public byte[] dbch_data = new byte[1];
474 
475         /**
476          * Instantiates a new dev broadcast handle.
477          */
DEV_BROADCAST_HANDLE()478         public DEV_BROADCAST_HANDLE() {
479             super();
480         }
481 
482         /**
483          * Instantiates a new dev broadcast handle.
484          *
485          * @param memory
486          *            the memory
487          */
DEV_BROADCAST_HANDLE(Pointer memory)488         public DEV_BROADCAST_HANDLE(Pointer memory) {
489             super(memory);
490             read();
491         }
492     }
493 }
494