-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmint_fullscreen_fix.c
More file actions
167 lines (147 loc) · 5.54 KB
/
Copy pathmint_fullscreen_fix.c
File metadata and controls
167 lines (147 loc) · 5.54 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
/* Helper function to get an Atom from its name */
Atom get_atom(Display * display,
const char * name) {
return XInternAtom(display, name, False);
}
/* Get the active window using the _NET_ACTIVE_WINDOW property */
Window get_active_window(Display * display) {
Atom net_active_window = get_atom(display, "_NET_ACTIVE_WINDOW");
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char * prop = NULL;
Window active_window = 0;
if (XGetWindowProperty(display, DefaultRootWindow(display), net_active_window,
0, sizeof(Window) / 4, False, XA_WINDOW, &
actual_type, & actual_format, & nitems, & bytes_after, &
prop) == Success && prop) {
active_window = * (Window * ) prop;
XFree(prop);
}
return active_window;
}
/* Check if a window is fullscreen by looking for the _NET_WM_STATE_FULLSCREEN atom */
int is_fullscreen(Display * display, Window window) {
Atom net_wm_state = get_atom(display, "_NET_WM_STATE");
Atom net_wm_state_fullscreen = get_atom(display, "_NET_WM_STATE_FULLSCREEN");
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char * prop = NULL;
int fullscreen = 0;
if (XGetWindowProperty(display, window, net_wm_state, 0, 1024, False,
XA_ATOM, & actual_type, & actual_format, &
nitems, & bytes_after, & prop) == Success && prop) {
Atom * atoms = (Atom * ) prop;
for (unsigned long i = 0; i < nitems; i++) {
if (atoms[i] == net_wm_state_fullscreen) {
fullscreen = 1;
break;
}
}
XFree(prop);
}
return fullscreen;
}
typedef struct {
int x, y;
int width, height;
}
Geometry;
/* Get the monitor geometry under the pointer using XRandr */
Geometry get_monitor_geometry(Display * display, Window root, int pointer_x, int pointer_y) {
Geometry geo = {
0,
0,
0,
0
};
XRRScreenResources * screen = XRRGetScreenResources(display, root);
if (!screen)
return geo;
for (int i = 0; i < screen -> ncrtc; i++) {
XRRCrtcInfo * crtc = XRRGetCrtcInfo(display, screen, screen -> crtcs[i]);
if (crtc && crtc -> width && crtc -> height) {
/* Check if pointer is within this monitor (CRTC) */
if (pointer_x >= crtc -> x && pointer_x < crtc -> x + crtc -> width &&
pointer_y >= crtc -> y && pointer_y < crtc -> y + crtc -> height) {
geo.x = crtc -> x;
geo.y = crtc -> y;
geo.width = crtc -> width;
geo.height = crtc -> height;
XRRFreeCrtcInfo(crtc);
break;
}
XRRFreeCrtcInfo(crtc);
}
}
XRRFreeScreenResources(screen);
return geo;
}
/* Force the given window to cover the monitor where the pointer is located */
void force_fullscreen_on_cursor(Display * display, Window win) {
Window root = DefaultRootWindow(display);
Window ret_root, ret_child;
int root_x, root_y, win_x, win_y;
unsigned int mask;
if (XQueryPointer(display, root, & ret_root, & ret_child, &
root_x, & root_y, & win_x, & win_y, & mask)) {
Geometry geo = get_monitor_geometry(display, root, root_x, root_y);
if (geo.width && geo.height) {
printf("Forcing window %lu to fullscreen on monitor at (%d, %d) size %dx%d\n",
win, geo.x, geo.y, geo.width, geo.height);
XMoveResizeWindow(display, win, geo.x, geo.y, geo.width, geo.height);
XFlush(display);
} else {
printf("No monitor geometry found for pointer at (%d, %d)\n", root_x, root_y);
}
} else {
printf("Unable to query pointer position.\n");
}
}
int main() {
Display * display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "Failed to open X display\n");
return 1;
}
Window root = DefaultRootWindow(display);
Atom net_active_window = get_atom(display, "_NET_ACTIVE_WINDOW");
/* Listen for property change events on the root window */
XSelectInput(display, root, PropertyChangeMask);
printf("Listening for fullscreen events...\n");
Window last_window = 0;
int last_fullscreen = 0;
while (1) {
XEvent event;
XNextEvent(display, & event);
/* Detect when the active window changes */
if (event.type == PropertyNotify && event.xproperty.atom == net_active_window) {
Window active_window = get_active_window(display);
if (active_window != last_window) {
last_window = active_window;
last_fullscreen = is_fullscreen(display, active_window); // Initialize with current fullscreen state
}
}
/* Check for changes in the fullscreen state */
if (last_window && is_fullscreen(display, last_window) != last_fullscreen) {
last_fullscreen = !last_fullscreen;
if (last_fullscreen) {
printf("Window %lu is now FULLSCREEN\n", last_window);
/* Intercept the fullscreen event and force it to the monitor under the cursor */
force_fullscreen_on_cursor(display, last_window);
} else {
printf("Window %lu is now NORMAL\n", last_window);
}
}
}
XCloseDisplay(display);
return 0;
}