-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.java
214 lines (177 loc) · 7.09 KB
/
Utils.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
package de.jcm.dynamicwallpaper;
import com.sun.jna.Memory;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import org.apache.commons.io.IOUtils;
import org.lwjgl.glfw.GLFWNativeWin32;
import org.lwjgl.glfw.GLFWNativeX11;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import static org.lwjgl.system.windows.User32.*;
public class Utils
{
public static InputStream openURLStream(URL url) throws IOException
{
URLConnection connection = url.openConnection();
// Some servers block Java 8's HTTP client.
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0");
return connection.getInputStream();
}
private static WinDef.HWND getWorkerW()
{
WinDef.HWND progman = User32.INSTANCE.FindWindow("Progman", null);
User32.INSTANCE.SendMessage(progman, 0x052C, new WinDef.WPARAM(0xD), new WinDef.LPARAM(0));
User32.INSTANCE.SendMessage(progman, 0x052C, new WinDef.WPARAM(0xD), new WinDef.LPARAM(1));
AtomicReference<WinDef.HWND> workerRef = new AtomicReference<>();
User32.INSTANCE.EnumWindows(new WinUser.WNDENUMPROC()
{
@Override
public boolean callback(WinDef.HWND hWnd, Pointer data)
{
if(User32.INSTANCE.FindWindowEx(hWnd, null, "SHELLDLL_DefView", null)==null)
return true;
WinDef.HWND worker = User32.INSTANCE.FindWindowEx(null, hWnd, "WorkerW", null);
if(worker != null)
{
workerRef.set(worker);
}
return true;
}
}, null);
return workerRef.get();
}
public static void makeWallpaper(long window)
{
if(Platform.isWindows())
windowsMakeWallpaper(window);
else if(Platform.isLinux() && Platform.isX11())
linuxMakeWallpaper(window);
else
throw new UnsupportedOperationException("not supported on this platform");
}
public static void destroyWallpaper(long window)
{
if(Platform.isWindows())
windowsDestroyWallpaper(window);
else if(Platform.isLinux() && Platform.isX11())
{
// We don't need to do anything here I think
}
else
throw new UnsupportedOperationException("not supported on this platform");
}
private static void windowsMakeWallpaper(long window)
{
long nativeWindow = GLFWNativeWin32.glfwGetWin32Window(window);
// procedure from https://github.com/Francesco149/weebp
WinDef.HWND thisWindow = new WinDef.HWND(new Pointer(nativeWindow));
WinDef.HWND workerW = getWorkerW();
WinDef.RECT rect = new WinDef.RECT();
User32.INSTANCE.GetWindowRect(thisWindow, rect);
long style = User32.INSTANCE.GetWindowLong(thisWindow, User32.GWL_STYLE);
style &= ~(
WS_CAPTION |
WS_THICKFRAME |
WS_SYSMENU |
WS_MAXIMIZEBOX |
WS_MINIMIZEBOX
);
style |= User32.WS_CHILD;
User32.INSTANCE.SetWindowLong(thisWindow, User32.GWL_STYLE, (int) style);
// not sure if we need those, but better keep them in
long exStyle = User32.INSTANCE.GetWindowLong(thisWindow, User32.GWL_EXSTYLE);
exStyle &= ~(
WS_EX_DLGMODALFRAME |
WS_EX_COMPOSITED |
WS_EX_WINDOWEDGE |
WS_EX_CLIENTEDGE |
WS_EX_LAYERED |
WS_EX_STATICEDGE |
WS_EX_TOOLWINDOW |
WS_EX_APPWINDOW
);
User32.INSTANCE.SetWindowLong(thisWindow, User32.GWL_EXSTYLE, (int) exStyle);
User32.INSTANCE.SetParent(thisWindow, workerW);
User32.INSTANCE.ShowWindow(thisWindow, User32.SW_SHOW);
// not sure wtf we do here, but it seems to work (not really well, but idk)
User32.INSTANCE.MoveWindow(thisWindow, 0, rect.top, rect.right,
rect.bottom+10, false);
rect.clear();
}
private static void windowsDestroyWallpaper(long window)
{
long nativeWindow = GLFWNativeWin32.glfwGetWin32Window(window);
// procedure from https://github.com/Francesco149/weebp
WinDef.HWND thisWindow = new WinDef.HWND(new Pointer(nativeWindow));
User32.INSTANCE.SetParent(thisWindow, User32.INSTANCE.GetDesktopWindow());
long style = User32.INSTANCE.GetWindowLong(thisWindow, User32.GWL_STYLE);
style |= User32.WS_OVERLAPPEDWINDOW;
User32.INSTANCE.SetWindowLong(thisWindow, User32.GWL_STYLE, (int) style);
// not sure if we need those, but better keep them in
long exStyle = User32.INSTANCE.GetWindowLong(thisWindow, User32.GWL_EXSTYLE);
exStyle |= WS_EX_APPWINDOW;
User32.INSTANCE.SetWindowLong(thisWindow, User32.GWL_EXSTYLE, (int) exStyle);
getWorkerW();
}
private static void linuxMakeWallpaper(long window)
{
long nativeWindow = GLFWNativeX11.glfwGetX11Window(window);
X11.Window thisWindow = new X11.Window(nativeWindow);
X11.Display display = X11.INSTANCE.XOpenDisplay(null);
Memory memory = new Memory(8);
int screen = X11.INSTANCE.XDefaultScreen(display);
Xlib.INSTANCE.XReparentWindow(display, thisWindow, X11.INSTANCE.XRootWindow(display, screen),0, 0);
Xlib.INSTANCE.XLowerWindow(display, thisWindow);
memory.setByte(0L, (byte) 0);
X11.INSTANCE.XChangeProperty(display, thisWindow, xAtom(display, "_WIN_LAYER"), X11.XA_CARDINAL,
32, X11.PropModeAppend, memory, 1);
memory.setNativeLong(0, xAtom(display, "_NET_WM_STATE_BELOW"));
X11.INSTANCE.XChangeProperty(display, thisWindow, xAtom(display, "_NET_WM_STATE"), X11.XA_ATOM,
32, X11.PropModeAppend, memory, 1);
memory.setNativeLong(0, xAtom(display, "_NET_WM_STATE_SKIP_TASKBAR"));
X11.INSTANCE.XChangeProperty(display, thisWindow, xAtom(display, "_NET_WM_STATE"), X11.XA_ATOM,
32, X11.PropModeAppend, memory, 1);
memory.setNativeLong(0, xAtom(display, "_NET_WM_STATE_SKIP_PAGER"));
X11.INSTANCE.XChangeProperty(display, thisWindow, xAtom(display, "_NET_WM_STATE"), X11.XA_ATOM,
32, X11.PropModeAppend, memory, 1);
X11.INSTANCE.XFlush(display);
X11.INSTANCE.XCloseDisplay(display);
}
public static void linuxWalkWindowTree(X11.Display display, X11.Window window, Consumer<X11.Window> windowConsumer)
{
windowConsumer.accept(window);
X11.WindowByReference root = new X11.WindowByReference();
X11.WindowByReference parent = new X11.WindowByReference();
PointerByReference children = new PointerByReference();
IntByReference childrenCount = new IntByReference();
X11.INSTANCE.XQueryTree(display, window, root, parent, children, childrenCount);
Pointer pointer = children.getValue();
for(int i=0; i<childrenCount.getValue(); i++)
{
X11.Window child = new X11.Window(pointer.getLong(8L * i));
linuxWalkWindowTree(display, child, windowConsumer);
}
}
private static X11.Atom xAtom(X11.Display display, String name)
{
return X11.INSTANCE.XInternAtom(display, name, false);
}
public static String read(String path) throws IOException
{
try(InputStream in = DynamicWallpaper.class.getResourceAsStream(path))
{
return IOUtils.toString(in, StandardCharsets.UTF_8);
}
}
}