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
|
#include "common.h"
#if HAVE_STDIO_H
# include <stdio.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_ERRNO_H
# include <errno.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <X11/Xlib.h>
#include <X11/Xatom.h>
static char* find_desktop(const char* display, int use_prefix);
int main(int argc, char** argv)
{
int a;
const char* display = NULL;
char* desktop;
int usage = 0, error = 0, prefix = 0;
for (a = 1; a < argc; ++a) {
if (argv[a][0] == '-') {
if (strcmp(argv[a] + 1, "display") == 0) {
++a;
if (a == argc) {
error = 1;
} else {
display = argv[a];
}
} else if (strcmp(argv[a] + 1, "prefix") == 0) {
prefix = 1;
} else if (strcmp(argv[a] + 1, "help") == 0 ||
strcmp(argv[a] + 1, "-help") == 0) {
usage = 1;
} else {
error = 1;
}
} else {
error = 1;
}
}
if (usage) {
fputs("Usage `find_desktop [OPTIONS...]`\n", stdout);
fputs("\n", stdout);
fputs("Options:\n", stdout);
fputs(" -display <DISPLAY> X11 display to connect to instead of default\n", stdout);
fputs(" -prefix prefix display number with source\n", stdout);
fputs(" -help display this text and exit\n", stdout);
fputs("\n", stdout);
return error ? EXIT_FAILURE : EXIT_SUCCESS;
}
if (error) {
fputs("Try `find_desktop -help` for usage.\n", stdout);
return EXIT_FAILURE;
}
desktop = find_desktop(display, prefix);
if (!desktop)
{
return EXIT_FAILURE;
}
fprintf(stdout, "%s\n", desktop);
free(desktop);
return EXIT_SUCCESS;
}
static int _get_from_e17_using_window(Display* dsp, Window wnd);
static char* create_desktop(const char* prefix, int desktop, int use_prefix);
char* find_desktop(const char* display, int use_prefix)
{
Display* dsp = XOpenDisplay(display);
Window root;
int screen;
Atom _net_current_desktop, actual;
int format = 32;
unsigned long count, remaining;
unsigned char* data = NULL;
const char* windowid_env;
const char* desktop_env;
if (dsp == NULL) {
fputs("Unable to open X11 display.\n", stderr);
return NULL;
}
screen = DefaultScreen(dsp);
root = RootWindow(dsp, screen);
desktop_env = getenv("DESKTOP");
if (desktop_env == NULL || strncmp(desktop_env, "Enlightenment-0.17",
18) != 0) {
/* Skip _NET_CURRENT_DESKTOP if we're in e17 */
/* Try to read the _NET_CURRENT_DESKTOP property */
_net_current_desktop = XInternAtom(dsp, "_NET_CURRENT_DESKTOP", 0);
if (!XGetWindowProperty(dsp, root, _net_current_desktop,
0, sizeof(long), False, XA_CARDINAL, &actual,
&format, &count, &remaining, &data) &&
data != NULL && count > 0 && format == 32) {
int ret = ((long*)data)[0];
XFree(data);
XCloseDisplay(dsp);
return create_desktop("ewmh", ret, use_prefix);
}
}
/* Failing that, let's see if our owner knows */
windowid_env = getenv("WINDOWID");
if (windowid_env != NULL) {
unsigned long windowid;
char* end = NULL;
errno = 0;
windowid = strtoul(windowid_env, &end, 10);
if (windowid != 0 && errno == 0 && end != NULL && *end == '\0') {
int ret = _get_from_e17_using_window(dsp, (Window)windowid);
if (ret != -1) {
XCloseDisplay(dsp);
return create_desktop("enlightenment", ret, use_prefix);
}
}
}
/* Try creating a bogus window then */
{
XSetWindowAttributes attrs;
Window wnd;
int ret;
wnd = XCreateWindow(dsp, root, 0, 0, 1, 1,
0, CopyFromParent, CopyFromParent,
CopyFromParent, 0, &attrs);
XMapWindow(dsp, wnd);
ret = _get_from_e17_using_window(dsp, wnd);
XUnmapWindow(dsp, wnd);
XDestroyWindow(dsp, wnd);
if (ret != -1) {
XCloseDisplay(dsp);
return create_desktop("enlightenment", ret, use_prefix);
}
}
/* Don't know what else to do */
XCloseDisplay(dsp);
return NULL;
}
char* create_desktop(const char* prefix, int desktop, int use_prefix)
{
if (use_prefix) {
size_t len = strlen(prefix) + 11;
char* ret = malloc(len);
snprintf(ret, len, "%s_%d", prefix, desktop);
return ret;
} else {
char* ret = malloc(10);
snprintf(ret, 10, "%d", desktop);
return ret;
}
}
static Atom __e_window_desk = 0;
int _get_from_e17_using_window(Display* dsp, Window wnd)
{
Atom actual = 0;
int format = 32;
unsigned long count = 0, remaining = 0;
unsigned char* data = NULL;
int try;
if (__e_window_desk == 0) {
__e_window_desk = XInternAtom(dsp, "__E_WINDOW_DESK", 0);
if (__e_window_desk == 0)
return -1;
}
for (try = 0; try < 2; ++try)
{
if (!XGetWindowProperty(dsp, wnd, __e_window_desk,
0, 2 * sizeof(long), False, XA_CARDINAL,
&actual, &format, &count, &remaining, &data))
{
if (data != NULL)
{
break;
}
}
sleep(1);
}
if (data != NULL && count > 0 && format == 32) {
int x = ((long*)data)[0];
int y = ((long*)data)[1];
XFree(data);
return (x * 256) + y;
}
return -1;
}
|