/
X.java
1196 lines (1053 loc) · 42.6 KB
/
X.java
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008 Stefan Endrullis, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package jnacontrib.x11.api;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Memory;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.ArrayList;
import java.awt.Rectangle;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.platform.unix.X11.Atom;
import com.sun.jna.platform.unix.X11.WindowByReference;
/**
* Object oriented X window system.
* <p/>
* Some code segments in this class are based on the code of the program
* wmctrl (licensed under GPLv2) written by Tomas Styblo <tripie@cpan.org>.
* Thanks a lot, Tomas!
*
* @author Stefan Endrullis
*/
public class X {
/** Remove/unset property. */
public static final int _NET_WM_STATE_REMOVE = 0;
/** Add/set property. */
public static final int _NET_WM_STATE_ADD = 1;
/** Toggle property. */
public static final int _NET_WM_STATE_TOGGLE = 2;
/** Maximal property value length. */
public static final int MAX_PROPERTY_VALUE_LEN = 4096;
private static final X11 x11 = X11.INSTANCE;
private static int bytesToInt(byte[] prop) {
return ((prop[3] & 0xff) << 24)
| ((prop[2] & 0xff) << 16)
| ((prop[1] & 0xff) << 8)
| ((prop[0] & 0xff));
}
private static int bytesToInt(byte[] prop, int offset) {
return ((prop[3 + offset] & 0xff) << 24)
| ((prop[2 + offset] & 0xff) << 16)
| ((prop[1 + offset] & 0xff) << 8)
| ((prop[offset] & 0xff));
}
/**
* X Display.
*/
public static class Display {
/**
* Open display.
*/
private X11.Display x11Display;
/**
* HashMap<String,X11.Atom>.
*/
private HashMap<String, Atom> atomsHash = new HashMap<String, Atom>();
/**
* Creates the OOWindowUtils using the default display.
*/
public Display() {
x11Display = x11.XOpenDisplay(null);
if (x11Display == null) {
throw new Error("Can't open X Display");
}
}
/**
* Creates the OOWindowUtils using a given display.
*
* @param x11Display open display
*/
public Display(X11.Display x11Display) {
this.x11Display = x11Display;
if (x11Display == null) {
throw new Error("X Display is null");
}
}
/**
* Closes the display.
*/
public void close() {
x11.XCloseDisplay(x11Display);
}
/**
* Flushes the output buffer / event queue.
*/
public void flush() {
x11.XFlush(x11Display);
}
/**
* Returns the X11 display.
*
* @return X11 display
*/
public X11.Display getX11Display() {
return x11Display;
}
/**
* Get internal atoms by name.
*
* @param name name of the atom
* @return atom
*/
public X11.Atom getAtom(String name) {
X11.Atom atom = atomsHash.get(name);
if (atom == null) {
atom = x11.XInternAtom(x11Display, name, false);
atomsHash.put(name, atom);
}
return atom;
}
/**
* Returns the window manager information as an window.
*
* @return window manager information as an window
* @throws X11Exception thrown if X11 window errors occurred
*/
public Window getWindowManagerInfo() throws X11Exception {
Window rootWindow = getRootWindow();
try {
return rootWindow.getWindowProperty(X11.XA_WINDOW, "_NET_SUPPORTING_WM_CHECK");
} catch (X11Exception e) {
try {
return rootWindow.getWindowProperty(X11.XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK");
} catch (X11Exception e1) {
throw new X11Exception("Cannot get window manager info properties. (_NET_SUPPORTING_WM_CHECK or _WIN_SUPPORTING_WM_CHECK)");
}
}
}
/**
* Returns the root window.
*
* @return root window
*/
public Window getRootWindow() {
return new Window(this, x11.XDefaultRootWindow(x11Display));
}
/**
* Returns the current active window.
*
* @return current active window
* @throws X11Exception thrown if X11 window errors occurred
*/
public Window getActiveWindow() throws X11Exception {
return getRootWindow().getWindowProperty(X11.XA_WINDOW, "_NET_ACTIVE_WINDOW");
}
/**
* Returns all windows managed by the window manager.
*
* @return all windows managed by the window manager
* @throws X11Exception thrown if X11 window errors occurred
*/
public Window[] getWindows() throws X11Exception {
byte[] bytes;
Window rootWindow = getRootWindow();
try {
bytes = rootWindow.getProperty(X11.XA_WINDOW, "_NET_CLIENT_LIST");
} catch (X11Exception e) {
try {
bytes = rootWindow.getProperty(X11.XA_CARDINAL, "_WIN_CLIENT_LIST");
} catch (X11Exception e1) {
throw new X11Exception("Cannot get client list properties (_NET_CLIENT_LIST or _WIN_CLIENT_LIST)");
}
}
Window[] windowList = new Window[bytes.length / X11.Window.SIZE];
for (int i = 0; i < windowList.length; i++) {
windowList[i] = new Window(this, new X11.Window(bytesToInt(bytes, X11.XID.SIZE * i)));
}
return windowList;
}
/**
* Returns the number of desktops.
*
* @return number of desktops
* @throws X11Exception thrown if X11 window errors occurred
*/
public int getDesktopCount() throws X11Exception {
Window root = getRootWindow();
try {
return root.getIntProperty(X11.XA_CARDINAL, "_NET_NUMBER_OF_DESKTOPS");
} catch (X11Exception e) {
try {
return root.getIntProperty(X11.XA_CARDINAL, "_WIN_WORKSPACE_COUNT");
} catch (X11Exception e1) {
throw new X11Exception("Cannot get number of desktops properties (_NET_NUMBER_OF_DESKTOPS or _WIN_WORKSPACE_COUNT)");
}
}
}
/**
* Returns the number of the active desktop.
*
* @return number of the active desktop
* @throws X11Exception thrown if X11 window errors occurred
*/
public int getActiveDesktopNumber() throws X11Exception {
Window root = getRootWindow();
int cur_desktop;
try {
cur_desktop = root.getIntProperty(X11.XA_CARDINAL, "_NET_CURRENT_DESKTOP");
} catch (X11Exception e) {
try {
cur_desktop = root.getIntProperty(X11.XA_CARDINAL, "_WIN_WORKSPACE");
} catch (X11Exception e1) {
throw new X11Exception("Cannot get current desktop properties (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)");
}
}
return cur_desktop;
}
/**
* Returns the available desktops.
*
* @return available desktops
* @throws X11Exception thrown if X11 window errors occurred
*/
public Desktop[] getDesktops() throws X11Exception {
Window root = getRootWindow();
String[] desktopNames;
try {
desktopNames = root.getUtf8ListProperty(getAtom("UTF8_STRING"), "_NET_DESKTOP_NAMES");
} catch (X11Exception e) {
try {
desktopNames = root.getStringListProperty(X11.XA_STRING, "_WIN_WORKSPACE_NAMES");
} catch (X11Exception e1) {
throw new X11Exception("Cannot get desktop names properties (_NET_DESKTOP_NAMES or _WIN_WORKSPACE_NAMES)");
}
}
Desktop[] desktops = new Desktop[getDesktopCount()];
for (int i = 0; i < desktops.length; i++) {
desktops[i] = new Desktop(this, i, desktopNames[i]);
}
return desktops;
}
/**
* Switches to the given desktop.
*
* @param nr desktop number
* @throws X11Exception thrown if X11 window errors occurred
*/
public void switchDesktop(int nr) throws X11Exception {
getRootWindow().clientMsg("_NET_CURRENT_DESKTOP", nr, 0, 0, 0, 0);
}
/**
* Sets the "showing the desktop" state.
*
* @param state true if the desktop should be shown
* @throws X11Exception thrown if X11 window errors occurred
*/
public void showingDesktop(boolean state) throws X11Exception {
getRootWindow().clientMsg("_NET_SHOWING_DESKTOP", state ? 1 : 0, 0, 0, 0, 0);
}
/**
* Enables / disables the auto-repeat of pressed keys.
*
* @param on true if auto-repeat shall be enabled
*/
public void setKeyAutoRepeat(boolean on) {
if (on) {
x11.XAutoRepeatOn(x11Display);
} else {
x11.XAutoRepeatOff(x11Display);
}
}
/**
* Returns the key symbol corresponding to the the key name.
*
* @param keyName name of the key
* @return key symbol
*/
public X11.KeySym getKeySym(String keyName) {
return x11.XStringToKeysym(keyName);
}
/**
* Returns the key symbol corresponding to the keycode.
*
* @param keyCode keycode
* @param index element of the keycode vector
* @return key symbol
*/
public X11.KeySym getKeySym(byte keyCode, int index) {
return x11.XKeycodeToKeysym(x11Display, keyCode, index);
}
/**
* Returns the keycode corresponding to the key symbol.
*
* @param keySym key symbol
* @return keycode
*/
public byte getKeyCode(X11.KeySym keySym) {
return x11.XKeysymToKeycode(x11Display, keySym);
}
/**
* Returns the keycode corresponding to the key name.
*
* @param keyName name of the key
* @return keycode
*/
public byte getKeyCode(String keyName) {
return x11.XKeysymToKeycode(x11Display, getKeySym(keyName));
}
/**
* Returns the key name corresponding to the key symbol.
*
* @param keySym key symbol
* @return name of the key
*/
public String getKeyName(X11.KeySym keySym) {
return x11.XKeysymToString(keySym);
}
/**
* Returns the key name corresponding to the keycode and the index in the keycode vector.
*
* @param keyCode keycode
* @param index index in the keycode vector
* @return name of the key
*/
public String getKeyName(byte keyCode, int index) {
return getKeyName(getKeySym(keyCode, index));
}
/**
* Returns the modifier keymap.
*
* @return modifier keymap
*/
public ModifierKeymap getModifierKeymap() {
X11.XModifierKeymapRef xModifierKeymapRef = x11.XGetModifierMapping(x11Display);
ModifierKeymap modifierKeymap = new ModifierKeymap(xModifierKeymapRef);
x11.XFreeModifiermap(xModifierKeymapRef);
return modifierKeymap;
}
/**
* Sets the modifier keymap.
*
* @param modifierKeymap modifier keymap
*/
public void setModifierKeymap(ModifierKeymap modifierKeymap) {
X11.XModifierKeymapRef xModifierKeymapRef = modifierKeymap.toXModifierKeyamp();
x11.XSetModifierMapping(x11Display,xModifierKeymapRef);
}
}
/**
* Modifier keymap. The lists shift, lock, control, mod1, mod1, mod1, mod1, mod1
* contain the keycodes as Byte objects. You can directly access these lists to
* read, replace, remove or insert new keycodes to these modifiers.
* To apply a new modifier keymap call
* {@link X.Display#setModifierKeymap(ModifierKeymap)}.
*/
public static class ModifierKeymap {
/** Shift modifier as an ArrayList<Byte>. */
public ArrayList<Byte> shift = new ArrayList<Byte>(4);
/** Lock modifier as an ArrayList<Byte>. */
public ArrayList<Byte> lock = new ArrayList<Byte>(4);
/** Control modifier as an ArrayList<Byte>. */
public ArrayList<Byte> control = new ArrayList<Byte>(4);
/** Mod1 modifier as an ArrayList<Byte>. */
public ArrayList<Byte> mod1 = new ArrayList<Byte>(4);
/** Mod2 modifier as an ArrayList<Byte>. */
public ArrayList<Byte> mod2 = new ArrayList<Byte>(4);
/** Mod3 modifier as an ArrayList<Byte>. */
public ArrayList<Byte> mod3 = new ArrayList<Byte>(4);
/** Mod4 modifier as an ArrayList<Byte>. */
public ArrayList<Byte> mod4 = new ArrayList<Byte>(4);
/** Mod5 modifier as an ArrayList<Byte>. */
public ArrayList<Byte> mod5 = new ArrayList<Byte>(4);
/**
* Creates an empty modifier keymap.
*/
public ModifierKeymap() {
}
/**
* Creates a modifier keymap and reads the modifiers from the XModifierKeymap.
*
* @param xModifierKeymapRef XModifierKeymap
*/
public ModifierKeymap(X11.XModifierKeymapRef xModifierKeymapRef) {
fromXModifierKeymap(xModifierKeymapRef);
}
/**
* Reads all modifiers from the XModifierKeymap.
*
* @param xModifierKeymapRef XModifierKeymap
*/
public void fromXModifierKeymap(X11.XModifierKeymapRef xModifierKeymapRef) {
int count = xModifierKeymapRef.max_keypermod;
byte[] keys = xModifierKeymapRef.modifiermap.getByteArray(0, 8*count);
ArrayList<Byte>[] allModifiers = getAllModifiers();
for (int modNr = 0; modNr < 8; modNr++) {
ArrayList<Byte> modifier = allModifiers[modNr];
modifier.clear();
for (int keyNr = 0; keyNr < count; keyNr++) {
byte key = keys[modNr*count + keyNr];
if (key != 0) {
modifier.add(Byte.valueOf(key));
}
}
}
}
/**
* Returns an XModifierKeymap corresponding to this object.
*
* @return XModifierKeymap
*/
public X11.XModifierKeymapRef toXModifierKeyamp() {
ArrayList<Byte>[] allModifiers = getAllModifiers();
// determine max list size
int count = 0;
for (int i = 0; i < allModifiers.length; i++) {
count = Math.max(count, allModifiers[i].size());
}
byte[] keys = new byte[8*count];
for (int modNr = 0; modNr < 8; modNr++) {
ArrayList<Byte> modifier = allModifiers[modNr];
for (int keyNr = 0; keyNr < modifier.size(); keyNr++) {
keys[modNr*count + keyNr] = modifier.get(keyNr).byteValue();
}
}
X11.XModifierKeymapRef xModifierKeymapRef = new X11.XModifierKeymapRef();
xModifierKeymapRef.max_keypermod = count;
xModifierKeymapRef.modifiermap = new Memory(keys.length);
xModifierKeymapRef.modifiermap.write(0, keys, 0, keys.length);
return xModifierKeymapRef;
}
/**
* Returns all modifiers as an array.
*
* @return array of modifier lists
*/
public ArrayList<Byte>[] getAllModifiers() {
return new ArrayList[]{
shift, lock, control, mod1, mod2, mod3, mod4, mod5
};
}
}
/**
* X Desktop.
*/
public static class Desktop {
public X.Display display;
public int number;
public String name;
public Desktop(Display display, int number, String name) {
this.display = display;
this.number = number;
this.name = name;
}
}
/**
* X Window.
*/
public static class Window {
private X.Display display;
private X11.Window x11Window;
/**
* Returns the X11 window object.
*
* @return X11 window
*/
public X11.Window getX11Window() {
return x11Window;
}
/**
* Returns the ID of the window.
*
* @return window ID
*/
public int getID() {
return x11Window.intValue();
}
/**
* Creates the window.
*
* @param display display where the window is allocated
* @param x11Window X11 window
*/
public Window(X.Display display, X11.Window x11Window) {
this.display = display;
this.x11Window = x11Window;
}
/**
* Returns the title of the window.
*
* @return title of the window
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getTitle() throws X11Exception {
try {
return getUtf8Property(display.getAtom("UTF8_STRING"), "_NET_WM_NAME");
} catch (X11Exception e) {
return getUtf8Property(X11.XA_STRING, X11.XA_WM_NAME);
}
}
/**
* Returns the window class.
*
* @return window class
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getWindowClass() throws X11Exception {
return getUtf8Property(X11.XA_STRING, X11.XA_WM_CLASS);
}
/**
* Returns the PID of the window.
*
* @return PID of the window
* @throws X11Exception thrown if X11 window errors occurred
*/
public Integer getPID() throws X11Exception {
return getIntProperty(X11.XA_CARDINAL, "_NET_WM_PID");
}
/**
* Returns the desktop ID of the window.
*
* @return desktop ID of the window
* @throws X11Exception thrown if X11 window errors occurred
*/
public int getDesktop() throws X11Exception {
try {
return getIntProperty(X11.XA_CARDINAL, "_NET_WM_DESKTOP");
} catch (X11Exception e) {
return getIntProperty(X11.XA_CARDINAL, "_WIN_WORKSPACE");
}
}
/**
* Returns the client machine name of the window.
*
* @return client machine name of the window
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getMachine() throws X11Exception {
return getStringProperty(X11.XA_STRING, "WM_CLIENT_MACHINE");
}
/**
* Returns the XWindowAttributes of the window.
*
* @return XWindowAttributes of the window
*/
public X11.XWindowAttributes getXWindowAttributes() {
X11.XWindowAttributes xwa = new X11.XWindowAttributes();
x11.XGetWindowAttributes(display.x11Display, x11Window, xwa);
return xwa;
}
/**
* Returns the geometry of the window.
*
* @return geometry of the window
*/
public Geometry getGeometry() {
X11.WindowByReference junkRoot = new X11.WindowByReference();
IntByReference junkX = new IntByReference();
IntByReference junkY = new IntByReference();
IntByReference x = new IntByReference();
IntByReference y = new IntByReference();
IntByReference width = new IntByReference();
IntByReference height = new IntByReference();
IntByReference borderWidth = new IntByReference();
IntByReference depth = new IntByReference();
x11.XGetGeometry(display.x11Display, x11Window, junkRoot, junkX, junkY, width, height, borderWidth, depth);
x11.XTranslateCoordinates(display.x11Display, x11Window, junkRoot.getValue(), junkX.getValue(),
junkY.getValue(), x, y, junkRoot);
return new Geometry(x.getValue(), y.getValue(), width.getValue(), height.getValue(),
borderWidth.getValue(), depth.getValue());
}
/**
* Returns the bounding box of the window.
*
* @return bounding box of the window
*/
public Rectangle getBounds() {
X11.WindowByReference junkRoot = new X11.WindowByReference();
IntByReference junkX = new IntByReference();
IntByReference junkY = new IntByReference();
IntByReference x = new IntByReference();
IntByReference y = new IntByReference();
IntByReference width = new IntByReference();
IntByReference height = new IntByReference();
IntByReference border_width = new IntByReference();
IntByReference depth = new IntByReference();
x11.XGetGeometry(display.x11Display, x11Window, junkRoot, junkX, junkY, width, height, border_width, depth);
x11.XTranslateCoordinates(display.x11Display, x11Window, junkRoot.getValue(), junkX.getValue(),
junkY.getValue(), x, y, junkRoot);
int xVal = x.getValue();
int yVal = y.getValue();
return new Rectangle(xVal, yVal, xVal + width.getValue(), yVal + height.getValue());
}
/**
* Activates the window.
*
* @throws X11Exception thrown if X11 window errors occurred
*/
public void activate() throws X11Exception {
clientMsg("_NET_ACTIVE_WINDOW", 0, 0, 0, 0, 0);
x11.XMapRaised(display.x11Display, x11Window);
}
/**
* Moves the window to the specified desktop.
*
* @param desktopNr desktop
* @return X11.SUCCESS if closing was successful
* @throws X11Exception thrown if X11 window errors occurred
*/
public int moveToDesktop(int desktopNr) throws X11Exception {
return clientMsg("_NET_WM_DESKTOP", desktopNr, 0, 0, 0, 0);
}
/**
* Selects the input events to listen for.
*
* @param eventMask event mask representing the events to listen for
*/
public void selectInput(int eventMask) {
x11.XSelectInput(display.x11Display, x11Window, new NativeLong(eventMask));
}
public int nextEvent(X11.XEvent event) {
return x11.XNextEvent(display.x11Display, event);
}
public void sendEvent(int eventMask, X11.XEvent event) {
x11.XSendEvent(display.x11Display, x11Window, 1, new NativeLong(eventMask), event);
}
/**
* Closes the window gracefully.
*
* @return X11.SUCCESS if closing was successful
* @throws X11Exception thrown if X11 window errors occurred
*/
public int close() throws X11Exception {
return clientMsg("_NET_CLOSE_WINDOW", 0, 0, 0, 0, 0);
}
/**
* Returns the property value as integer.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as integer or null if not found
* @throws X11Exception thrown if X11 window errors occurred
*/
public Integer getIntProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
byte[] property = getProperty(xa_prop_type, xa_prop_name);
if( property == null ){
return null;
}
return bytesToInt(property);
}
/**
* Returns the property value as integer.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as integer
* @throws X11Exception thrown if X11 window errors occurred
*/
public Integer getIntProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return getIntProperty(xa_prop_type, display.getAtom(xa_prop_name));
}
/**
* Returns the property value as window.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as window, or null if not found
* @throws X11Exception thrown if X11 window errors occurred
*/
public Window getWindowProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
Integer windowId = getIntProperty(xa_prop_type, xa_prop_name);
if( windowId == null ){
return null;
}
X11.Window x11Window = new X11.Window(windowId);
return new Window(display, x11Window);
}
/**
* Returns the property value as window.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as window
* @throws X11Exception thrown if X11 window errors occurred
*/
public Window getWindowProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return getWindowProperty(xa_prop_type, display.getAtom(xa_prop_name));
}
/**
* Returns the property value as a null terminated byte array.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as a null terminated byte array, or null if not found
* @throws X11Exception thrown if X11 window errors occurred
*/
public byte[] getNullTerminatedProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
byte[] bytesOrig = getProperty(xa_prop_type, xa_prop_name);
byte[] bytesDest;
if( bytesOrig == null ){
return null;
}
// search for '\0'
int i;
for (i = 0; i < bytesOrig.length; i++) {
if (bytesOrig[i] == '\0') break;
}
if (i < bytesOrig.length - 1) {
bytesDest = new byte[i + 1];
System.arraycopy(bytesOrig, 0, bytesDest, 0, i + 1);
} else {
bytesDest = bytesOrig;
}
return bytesDest;
}
/**
* Returns the property value as a null terminated byte array.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as a null terminated byte array
* @throws X11Exception thrown if X11 window errors occurred
*/
public byte[] getNullTerminatedProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return getNullTerminatedProperty(xa_prop_type, display.getAtom(xa_prop_name));
}
/**
* Returns the property value as byte array where every '\0' character is replaced by '.'.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as byte array where every '\0' character is replaced by '.'. null if the property was not found
* @throws X11Exception thrown if X11 window errors occurred
*/
public byte[] getNullReplacedStringProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
byte[] bytes = getProperty(xa_prop_type, xa_prop_name);
if( bytes == null ){
return null;
}
// search for '\0'
int i;
for (i = 0; i < bytes.length; i++) {
if (bytes[i] == '\0') {
bytes[i] = '.';
}
}
return bytes;
}
/**
* Returns the property value as byte array where every '\0' character is replaced by '.'.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as byte array where every '\0' character is replaced by '.'
* @throws X11Exception thrown if X11 window errors occurred
*/
public byte[] getNullReplacedStringProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return getNullReplacedStringProperty(xa_prop_type, display.getAtom(xa_prop_name));
}
/**
* Returns the property value as string where every '\0' character is replaced by '.'.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as string where every '\0' character is replaced by '.'
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getStringProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
return new String(getNullReplacedStringProperty(xa_prop_type, xa_prop_name));
}
/**
* Returns the property value as string where every '\0' character is replaced by '.'.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as string where every '\0' character is replaced by '.'
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getStringProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return new String(getNullReplacedStringProperty(xa_prop_type, xa_prop_name));
}
/**
* Returns the property value as string list.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as string list
* @throws X11Exception thrown if X11 window errors occurred
*/
public String[] getStringListProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
return new String(getProperty(xa_prop_type, xa_prop_name)).split("\0");
}
/**
* Returns the property value as string list.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as string list, or null if the property value does not exist
* @throws X11Exception thrown if X11 window errors occurred
*/
public String[] getStringListProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
byte[] property = getProperty(xa_prop_type, xa_prop_name);
if( property == null ){
return null;
}
return new String(property).split("\0");
}
/**
* Returns the property value as UTF8 string where every '\0' character is replaced by '.'.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as UTF8 string where every '\0' character is replaced by '.'
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getUtf8Property(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
try {
byte[] property = getNullReplacedStringProperty(xa_prop_type, xa_prop_name);
if( property == null ){
return null;
}
return new String(property, "UTF8");
} catch (UnsupportedEncodingException e) {
throw new X11Exception(e);
}
}
/**
* Returns the property value as UTF8 string where every '\0' character is replaced by '.'.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as UTF8 string where every '\0' character is replaced by '.'
* @throws X11Exception thrown if X11 window errors occurred
*/
public String getUtf8Property(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return getUtf8Property(xa_prop_type, display.getAtom(xa_prop_name));
}
/**
* Returns the property value as UTF8 string list
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as UTF8 string list
* @throws X11Exception thrown if X11 window errors occurred
*/
public String[] getUtf8ListProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
try {
return new String(getProperty(xa_prop_type, xa_prop_name), "UTF8").split("\0");
} catch (UnsupportedEncodingException e) {
throw new X11Exception(e);
}
}
/**
* Returns the property value as UTF8 string list
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as UTF8 string list
* @throws X11Exception thrown if X11 window errors occurred
*/
public String[] getUtf8ListProperty(X11.Atom xa_prop_type, String xa_prop_name) throws X11Exception {
return getUtf8ListProperty(xa_prop_type, display.getAtom(xa_prop_name));
}
/**
* Returns the property value as a byte array.
*
* @param xa_prop_type property type
* @param xa_prop_name property name
* @return property value as a byte array
* @throws X11Exception thrown if X11 window errors occurred
*/
public byte[] getProperty(X11.Atom xa_prop_type, X11.Atom xa_prop_name) throws X11Exception {
X11.AtomByReference xa_ret_type_ref = new X11.AtomByReference();