all repos — openbox @ 0090ec430ae66abb068ba9e48f3ea1690b05e52d

openbox fork - make it a bit more like ryudo

tools/kdetrayproxy/kdetrayproxy.c (raw)

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
215
216
217
218
219
220
221
222
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <sys/time.h>

typedef struct IList {
    Window win;
    int ignore_unmaps;

    struct IList *next;
} IList;

Display *display;
Window root;
Atom winhint;
Atom roothint;
int xfd;
IList *list;

void init();
void eventloop();
void handleevent(XEvent *e);
void addicon(Window win);
void removeicon(Window win, int unmap);
int issystray(Atom *a, int n);
void updatehint();
Window findclient(Window win);
int ignore_errors(Display *d, XErrorEvent *e);
void wait_time(unsigned int t);

int main()
{
    init();
    updatehint();
    eventloop();
    return 0;
}

void init()
{
    display = XOpenDisplay(NULL);
    if (!display) {
        fprintf(stderr, "Could not open display\n");
        exit(EXIT_FAILURE);
    }

    xfd = ConnectionNumber(display);

    root = RootWindowOfScreen(DefaultScreenOfDisplay(display));

    winhint = XInternAtom(display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", 0);
    roothint = XInternAtom(display, "_KDE_NET_SYSTEM_TRAY_WINDOWS", 0);

    XSelectInput(display, root, SubstructureNotifyMask);
}

void eventloop()
{
    XEvent e;
    fd_set set;

    while (1) {
        int event = False;
        while (XPending(display)) {
            event = True;
            XNextEvent(display, &e);
            handleevent(&e);
        }
        if (!event) {
            FD_ZERO(&set);
            FD_SET(xfd, &set);
            select(xfd + 1, &set, NULL, NULL, NULL);
        }
    }
}

void handleevent(XEvent *e)
{
    switch (e->type) {
    case MapNotify:
    {
        Atom *a;
        int n;
        Window w;

        w = findclient(e->xmap.window);
        if (w) {
            a = XListProperties(display, w, &n);
            if (issystray(a, n))
                addicon(w);
            XFree(a);
        }
        break;
    }
    case UnmapNotify:
        removeicon(e->xunmap.window, True);
        break;
    case DestroyNotify:
        removeicon(e->xdestroywindow.window, False);
        break;
    }
}

int ignore_errors(Display *d, XErrorEvent *e)
{
    (void)d; (void)e;
    return 1;
}

void addicon(Window win)
{
    IList *it;

    for (it = list; it; it = it->next)
        if (it->win == win) return; /* duplicate */

    it = list;
    list = malloc(sizeof(IList));
    list->win = win;
    list->ignore_unmaps = 2;
    list->next = it;

    XSelectInput(display, win, StructureNotifyMask);
    /* if i set the root hint too fast the dock app can fuck itself up */
    wait_time(1000000 / 8);
    updatehint();
}

void removeicon(Window win, int unmap)
{
    IList *it, *last = NULL;
    void *old;

    for (it = list; it; last = it, it = it->next)
        if (it->win == win) {
            if (it->ignore_unmaps && unmap) {
                it->ignore_unmaps--;
                return;
            }

            if (!last)
                list = it->next;
            else
                last->next = it->next;

            XSync(display, False);
            old = XSetErrorHandler(ignore_errors);
            XSelectInput(display, win, NoEventMask);
            XSync(display, False);
            XSetErrorHandler(old);
            free(it);

            updatehint();
        }
}

int issystray(Atom *a, int n)
{
    int i, r = False;

    for (i = 0; i < n; ++i) { 
        if (a[i] == winhint) {
            r = True;
            break;
        }
    }
    return r;
}

void updatehint()
{
    IList *it;
    int *wins, n, i;

    for (it = list, n = 0; it; it = it->next, ++n) ;
    if (n) {
        wins = malloc(sizeof(int) * n);
        for (it = list, i = 0; it; it = it->next, ++i)
            wins[i] = it->win;
    } else
        wins = NULL;
    XChangeProperty(display, root, roothint, XA_WINDOW, 32, PropModeReplace,
                    (unsigned char*) wins, n);
}

Window findclient(Window win)
{
  Window r, *children;
  unsigned int n, i;
  Atom state = XInternAtom(display, "WM_STATE", True);
  Atom ret_type;
  int ret_format;
  unsigned long ret_items, ret_bytesleft;
  unsigned long *prop_return;

  XQueryTree(display, win, &r, &r, &children, &n);
  for (i = 0; i < n; ++i) {
    Window w = findclient(children[i]);
    if (w) return w;
  }

  /* try me */
  XGetWindowProperty(display, win, state, 0, 1,
		     False, state, &ret_type, &ret_format,
		     &ret_items, &ret_bytesleft,
		     (unsigned char**) &prop_return); 
  if (ret_type == None || ret_items < 1)
    return None;
  return win; /* found it! */
}

void wait_time(unsigned int t)
{
    struct timeval time;
    time.tv_sec = 0;
    time.tv_usec = t;
    select(1, NULL, NULL, NULL, &time);
}