-
Notifications
You must be signed in to change notification settings - Fork 0
/
nightcap.c
190 lines (147 loc) · 4.01 KB
/
nightcap.c
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
// James Wah (abrasive) 2023
// This file is in the public domain.
// handle a delightful Windows/X11 type name conflict
#define Status XStatus
#include <X11/Xlib.h>
#undef Status
#undef ControlMask
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <stdint.h>
#include <signal.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static const wchar_t whole_window_prop[] = L"__wine_x11_whole_window";
HANDLE hProc = NULL;
void cleanup(int signal) {
if (hProc) {
TerminateProcess(hProc, 0);
hProc = NULL;
}
exit(0);
}
BOOL found_child = FALSE;
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {
found_child = TRUE;
}
Display *dpy = NULL;
int parent_xwindow;
void die(const char *format, ...) {
va_list ap;
va_start(ap, format);
char msg[512];
vsprintf(msg, format, ap);
if (!msg)
exit(1);
printf("ERROR: %s\n", msg);
XGCValues gc_values;
gc_values.foreground = XWhitePixel(dpy, 0);
GC gc = XCreateGC(dpy, parent_xwindow, GCForeground, &gc_values);
XTextItem xti = {
.chars = msg,
.nchars = strlen(msg),
};
XDrawText(dpy, parent_xwindow, gc, 100, 100, &xti, 1);
XFlush(dpy);
Sleep(3000);
exit(1);
}
int main(int argc, char **argv) {
signal(SIGTERM, cleanup);
signal(SIGINT, cleanup);
dpy = XOpenDisplay(NULL);
HINSTANCE hInstance = NULL;
const char *parent_xwindow_str = getenv("XSCREENSAVER_WINDOW");
if (!parent_xwindow_str) {
printf("error: XSCREENSAVER_WINDOW is not set\n");
return 1;
}
parent_xwindow = strtol(parent_xwindow_str, NULL, 0);
Window root;
int x, y;
unsigned int width, height, border_width, depth;
XGetGeometry(dpy, parent_xwindow, &root, &x, &y, &width, &height, &border_width, &depth);
if (argc < 2) {
printf("usage: %s screensaver.scr\n", argv[0]);
return 1;
}
const wchar_t CLASS_NAME[] = L"nightcap";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"nightcap.exe",
WS_POPUP,
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
char cmdline[MAX_PATH];
wchar_t wcmdline[MAX_PATH];
sprintf(cmdline, "\"%s\" /p %lu", argv[1], (uintptr_t)hwnd);
MultiByteToWideChar(CP_UTF8, 0, cmdline, -1, wcmdline, MAX_PATH);
if (!CreateProcess(NULL,
wcmdline,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi)) {
die("CreateProcess failed (%d). Probably the .scr was not found?", GetLastError());
}
while (!found_child) {
EnumChildWindows(hwnd, EnumChildProc, 0);
Sleep(5);
}
RECT winsize;
GetWindowRect(hwnd, &winsize);
hProc = pi.hProcess;
int our_xwindow = (uintptr_t)GetPropW(hwnd, whole_window_prop);
int result = XReparentWindow(dpy, our_xwindow, parent_xwindow, 0, 0);
XFlush(dpy);
ShowWindow(hwnd, SW_SHOW);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
TerminateProcess(hProc, 0);
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
if (hProc) {
TerminateProcess(hProc, 0);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}