-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_desktop.c
More file actions
79 lines (67 loc) · 2.14 KB
/
toggle_desktop.c
File metadata and controls
79 lines (67 loc) · 2.14 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
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
// Utility that shows the desktop if windows are in focus, or unshows the desktop if
// the desktop is already showing. Essentially, what you'd normally bind to Win+D.
//
// Source:
// https://www.linuxquestions.org/questions/linux-software-2/how-to-show-desktop-in-xfce4-601161/#post2967109
// To build:
// apt instal llibx11-dev
// gcc -o toggle_desktop toggle_desktop.c -lX11
int main(int argc, char *argv[])
{
Display *d;
Window root;
Atom _NET_SHOWING_DESKTOP, actual_type;
int actual_format, error, current;
unsigned long nitems, after;
unsigned char *data = NULL;
/* Open the default display */
if(!(d = XOpenDisplay(NULL))) {
fprintf(stderr, "Cannot open display \"%s\".\n", XDisplayName(NULL));
exit(EXIT_FAILURE);
}
/* This is the default root window */
root = DefaultRootWindow(d);
/* find the Atom for _NET_SHOWING_DESKTOP */
_NET_SHOWING_DESKTOP = XInternAtom(d, "_NET_SHOWING_DESKTOP", False);
/* Obtain the current state of _NET_SHOWING_DESKTOP on the default root window */
error = XGetWindowProperty(d, root, _NET_SHOWING_DESKTOP, 0, 1, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems, &after, &data);
if(error != Success) {
fprintf(stderr, "Received error %d!\n", error);
XCloseDisplay(d);
exit(EXIT_FAILURE);
}
/* The current state should be in data[0] */
if(data) {
current = data[0];
XFree(data);
data = NULL;
}
/* If nitems is 0, forget about data[0] and assume that current should be False */
if(!nitems) {
fprintf(stderr, "Unexpected result.\n");
fprintf(stderr, "Assuming unshown desktop!\n");
current = False;
}
/* Initialize Xevent struct */
XEvent xev = {
.xclient = {
.type = ClientMessage,
.send_event = True,
.display = d,
.window = root,
.message_type = _NET_SHOWING_DESKTOP,
.format = 32,
.data.l[0] = !current /* That’s what we want the new state to be */
}
};
/* Send the event to the window manager */
XSendEvent(d, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
XCloseDisplay(d);
exit(EXIT_SUCCESS);
return 0;
}