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
|
/*****************************************************************
* JAVAX11TEST
*****************************************************************/
package javax11test;
/*****************************************************************
* IMPORTS
*****************************************************************/
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
/*****************************************************************
* CLASS JavaX11Test
*****************************************************************/
public class JavaX11Test extends Application {
private static final int _NET_WM_STATE_ADD = 1;
private static X11.Atom ATOM_NET_WM_WINDOW_TYPE;
private static X11.Atom ATOM_NET_WM_WINDOW_TYPE_DOCK;
private static X11.Atom ATOM_NET_WM_STATE;
private static X11.Atom ATOM_NET_WM_STATE_BELOW;
private static X11.Atom ATOM_NET_CLIENT_LIST;
private final X11 x11;
private final X11.Display x11Display;
private final X11.Window x11RootWindow;
private Label label;
/*****************************************************************
* MAIN
* @param args
*****************************************************************/
public static void main(String[] args) {
launch(args);
}
/*****************************************************************
* CONSTRUCTEUR
*****************************************************************/
public JavaX11Test () {
x11 = X11.INSTANCE;
x11Display = x11.XOpenDisplay(null);
if (x11Display == null)
throw new Error("Can't open X Display");
x11RootWindow = x11.XDefaultRootWindow(x11Display);
if (x11RootWindow == null)
throw new Error("Can't open default root window");
ATOM_NET_WM_WINDOW_TYPE = x11.XInternAtom(x11Display, "_NET_WM_WINDOW_TYPE", false);
ATOM_NET_WM_WINDOW_TYPE_DOCK = x11.XInternAtom(x11Display, "_NET_WM_WINDOW_TYPE_DOCK", false);
ATOM_NET_WM_STATE = x11.XInternAtom(x11Display, "_NET_WM_STATE", false);
ATOM_NET_WM_STATE_BELOW = x11.XInternAtom(x11Display, "_NET_WM_STATE_BELOW", false);
ATOM_NET_CLIENT_LIST = x11.XInternAtom(x11Display, "_NET_CLIENT_LIST", false);
}
/*****************************************************************
* START
*****************************************************************/
@Override
public void start(Stage primaryStage) {
label = new Label();
label.setStyle( "-fx-font-smoothing-type: lcd;" +
"-fx-text-fill: black;" +
"-fx-font-weight: bold;" +
"-fx-font-size: 16;");
StackPane root = new StackPane();
root.getChildren().add(label);
Scene scene = new Scene(root, 100, 50);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setTitle("HelloWorld");
primaryStage.setX(10);
primaryStage.setY(200);
primaryStage.setScene(scene);
MakeClock();
primaryStage.show();
String className = getClass().getName();
int[] windowsIDList;
int windowID;
windowsIDList = getWindowsIDList();
windowID = findWindowIDClass(windowsIDList, className);
X11.Window wind = new X11.Window(windowID);
windowDock(wind);
windowBelow(wind);
}
/*****************************************************************
* MAKECLOCK
*****************************************************************/
public void MakeClock() {
Timeline digitalTime = new Timeline(
new KeyFrame(Duration.seconds(0), (ActionEvent actionEvent) -> {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
label.setText(dateFormat.format(cal.getTime()));
}),
new KeyFrame(Duration.seconds(1))
);
digitalTime.setCycleCount(Animation.INDEFINITE);
digitalTime.play();
}
/*****************************************************************
* WINDOWDOCK
* @param win
*****************************************************************/
public void windowDock(X11.Window win) {
X11.Atom atomType = ATOM_NET_WM_WINDOW_TYPE_DOCK;
NativeLongByReference longByRef = new NativeLongByReference();
longByRef.setValue(atomType);
x11.XChangeProperty( x11Display,
win,
ATOM_NET_WM_WINDOW_TYPE,
X11.XA_ATOM,
32,
X11.PropModeReplace,
longByRef.getPointer(),
1);
x11.XFlush(x11Display);
}
/*****************************************************************
* WINDOWBELOW
* @param win
* @return
*****************************************************************/
public int windowBelow(X11.Window win) {
X11.XClientMessageEvent event;
int maskVal = X11.SubstructureRedirectMask | X11.SubstructureNotifyMask;
NativeLong mask = new NativeLong(maskVal);
event = new X11.XClientMessageEvent();
event.type = X11.ClientMessage;
event.serial = new NativeLong(0);
event.send_event = 1;
event.message_type = ATOM_NET_WM_STATE;
event.window = win;
event.format = 32;
event.data.setType(NativeLong[].class);
event.data.l[0] = new NativeLong(_NET_WM_STATE_ADD);
event.data.l[1] = ATOM_NET_WM_STATE_BELOW;
event.data.l[2] = new NativeLong(0);
event.data.l[3] = new NativeLong(0);
event.data.l[4] = new NativeLong(0);
X11.XEvent e = new X11.XEvent();
e.setTypedValue(event);
if (x11.XSendEvent(x11Display, x11RootWindow, 0, mask, e) != 0) {
x11.XFlush(x11Display);
return X11.Success;
}
else
throw new Error("Cannot send " + "_NET_WM_STATE_BELOW" + " event.");
}
/*****************************************************************
* FINDWINDOWIDCLASS
* @param winsIDList
* @param className
* @return
*****************************************************************/
public int findWindowIDClass(int[] winsIDList, String className) {
X11.Window win;
String name;
for (int i = 0; i < winsIDList.length; i++) {
win = new X11.Window(winsIDList[i]);
name = getWindowClass(win);
if (name.contains(className) == true )
return winsIDList[i];
}
return 0;
}
/*****************************************************************
* FINDWINDOWIDCLASS
* @param window
* @return
*****************************************************************/
public String getWindowClass(X11.Window window) {
try {
String className;
byte[] bytes = getProperty(window, X11.XA_STRING, X11.XA_WM_CLASS);
if (bytes == null)
throw new Error("getProperty : bytes[] = null");
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == '\0') bytes[i] = '.';
}
className = new String(bytes, "UTF8");
return className;
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
/*****************************************************************
* GETIDAllWINDOWS
* @return
*****************************************************************/
public int[] getWindowsIDList() {
byte[] bytes = getProperty(x11RootWindow, X11.XA_WINDOW, ATOM_NET_CLIENT_LIST);
int[] winsIDList = new int[bytes.length / X11.Window.SIZE];
if (winsIDList.length == 0)
throw new Error("getProperty : winsIDList[].length = 0");
for (int i = 0; i < winsIDList.length; i++) {
winsIDList[i] = ((bytes[3 + (X11.XID.SIZE * i)] & 0xff) << 24)
| ((bytes[2 + (X11.XID.SIZE * i)] & 0xff) << 16)
| ((bytes[1 + (X11.XID.SIZE * i)] & 0xff) << 8)
| ((bytes[(X11.XID.SIZE * i)] & 0xff));
}
return winsIDList;
}
/*****************************************************************
* GETPROPERTY
* @param window
* @param xa_prop_type
* @param xa_prop_name
* @return
*****************************************************************/
public byte[] getProperty(X11.Window window, X11.Atom xa_prop_type, X11.Atom xa_prop_name) {
IntByReference ret_format_ref = new IntByReference();
NativeLongByReference ret_nitems_ref = new NativeLongByReference();
PointerByReference ret_prop_ref = new PointerByReference();
x11.XGetWindowProperty( x11Display, window,
xa_prop_name,
new NativeLong(0), new NativeLong(1024),
false,
xa_prop_type,
new X11.AtomByReference(),
ret_format_ref, ret_nitems_ref,
new NativeLongByReference(),
ret_prop_ref);
Pointer ret_prop = ret_prop_ref.getValue();
int ret_format = ret_format_ref.getValue();
long ret_nitems = ret_nitems_ref.getValue().longValue();
Map<Integer, Integer> nbytes = new HashMap<>();
nbytes.put(32, 8);
nbytes.put(16, 4);
nbytes.put(8, 1);
nbytes.put(0, 0);
int length = Math.min((int) ret_nitems * nbytes.get(ret_format), 4096);
byte[] ret = ret_prop.getByteArray(0, length);
x11.XFree(ret_prop);
if (ret.length == 0)
throw new Error("getProperty : byte[].length = 0");
return ret;
}
} // END OF CLASS JavaX11Test |
Partager