File: aepanel.c

package info (click to toggle)
aewm 1.3.12-3
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 356 kB
  • sloc: ansic: 2,557; makefile: 77; sh: 20
file content (345 lines) | stat: -rw-r--r-- 9,384 bytes parent folder | download | duplicates (4)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* aewm - Copyright 1998-2007 Decklin Foster <decklin@red-bean.com>.
 * This program is free software; please see LICENSE for details. */

#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include "common.h"
#include "atom.h"
#include "menu.h"

typedef struct client client_t;
struct client {
    client_t *next;
    Window   win;
    void     *widget;
    int      save;
};

void *make_launchitem_cb(void *, char *, char *);
void update_clientitem(GtkWidget *, Window);
void cleanup_clientitem(client_t *);
GdkFilterReturn check_event(GdkXEvent *, GdkEvent *, gpointer);
void update_client_list(GtkWidget *);
void show_menu_cb(GtkWidget *, gpointer);
void raise_win_cb(GtkWidget *, Window);
void fork_exec_cb(GtkWidget *, char *);
void setup_panel_atoms();
void sig_handler(int);
void set_strut(Window, strut_t *);

Atom net_wm_strut;
Atom net_wm_strut_partial;
Atom net_wm_wintype;
Atom net_wm_wintype_dock;

#define NAME_SIZE 48

client_t *head = NULL;

GtkWidget *menu_button = NULL;
int opt_bottom;

int main(int argc, char **argv)
{
    GtkWidget *toplevel, *hbox, *launch_menu;
    GtkWidget *clients_box;
    struct sigaction act;
    char *opt_config = NULL;
    strut_t s = { 0, 0, 0, 0 };
    client_t *c;
    int i, rw, rh;
    unsigned long read, left;
    Window w;

    setlocale(LC_ALL, "");
    gtk_init(&argc, &argv);
    gdk_error_trap_push();

    for (i = 1; i < argc; i++) {
        if ARG("config", "rc", 1)  {
            opt_config = argv[++i];
        } else if ARG("bottom", "b", 0)  {
            opt_bottom = 1;
        } else {
            fprintf(stderr,
                "usage: aepanel [--bottom|-b] [--config|-rc <file>]\n");
            exit(2);
        }
    }

    act.sa_handler = sig_handler;
    act.sa_flags = 0;
    sigaction(SIGCHLD, &act, NULL);

    dpy = GDK_DISPLAY();
    root = GDK_ROOT_WINDOW();

    setup_switch_atoms();
    setup_panel_atoms();

    toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(toplevel), "delete_event",
        G_CALLBACK(gtk_main_quit), NULL);

    launch_menu = gtk_menu_new();

    hbox = gtk_hbox_new(FALSE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(hbox), 0);
    gtk_container_add(GTK_CONTAINER(toplevel), hbox);

    menu_button = gtk_button_new_with_label("Launch");
    gtk_signal_connect(GTK_OBJECT(menu_button), "clicked",
        GTK_SIGNAL_FUNC(show_menu_cb), launch_menu);
    gtk_box_pack_start(GTK_BOX(hbox), menu_button, FALSE, FALSE, 0);

    clients_box = gtk_hbox_new(TRUE, 0);
    gtk_container_add(GTK_CONTAINER(hbox), clients_box);
    gtk_container_set_resize_mode(GTK_CONTAINER(clients_box),
        GTK_RESIZE_QUEUE);

    XSelectInput(dpy, GDK_ROOT_WINDOW(), PropertyChangeMask);
    gdk_window_add_filter(gdk_get_default_root_window(),
        check_event, clients_box);

    make_launch_menu(opt_config, launch_menu, make_launchitem_cb);

    for (i = 0, left = 1; left; i += read) {
        read = get_atoms(root, net_client_list, XA_WINDOW, i, &w, 1, &left);
        if (read)
            update_clientitem(clients_box, w);
        else
            break;
    }

    for (c = head; c; c = c->next)
        cleanup_clientitem(c);

    gtk_widget_show_all(hbox);
    gtk_widget_realize(toplevel);

    gtk_window_set_skip_taskbar_hint(GTK_WINDOW(toplevel), TRUE);
    gtk_window_set_skip_pager_hint(GTK_WINDOW(toplevel), TRUE);
    gtk_window_set_decorated(GTK_WINDOW(toplevel), FALSE);
    gtk_window_stick(GTK_WINDOW(toplevel));

    /* This last call is not working for some reason, so we'll just have to
     * kludge it for now. */

#ifdef _HEY_LOOK_AT_THAT_SOMEBODY_FIXED_THE_GTKWINDOW_TYPE_HINT_CRAP
    gtk_window_set_type_hint(GTK_WINDOW(toplevel),
        GDK_WINDOW_TYPE_HINT_DOCK);
#else
    set_atoms(GDK_WINDOW_XID(toplevel->window), net_wm_wintype, XA_ATOM,
        &net_wm_type_dock, 1);
#endif

    gdk_window_get_size(gdk_get_default_root_window(), &rw, &rh);
    gtk_widget_set_size_request(toplevel, rw, -1);
    gtk_window_move(GTK_WINDOW(toplevel), 0,
        opt_bottom ? rh - toplevel->allocation.height : 0);

    gtk_widget_show_all(toplevel);

    if (opt_bottom) s.bottom = toplevel->allocation.height;
    else s.top = toplevel->allocation.height;
    set_strut(GDK_WINDOW_XID(toplevel->window), &s);

    gtk_main();
    return 0;
}

void *make_launchitem_cb(void *menu, char *label, char *cmd)
{
    GtkWidget *item, *sub_menu = NULL;

    item = gtk_menu_item_new_with_label(label);
    gtk_menu_append(GTK_MENU(menu), item);

    if (cmd) {
        gtk_signal_connect(GTK_OBJECT(item), "activate",
            GTK_SIGNAL_FUNC(fork_exec_cb), cmd);
    } else {
        sub_menu = gtk_menu_new();
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub_menu);
    }

    gtk_widget_show(item);
    return sub_menu;
}

void update_clientitem(GtkWidget *container, Window w)
{
    char buf[NAME_SIZE];
    client_t *c;

    for (c = head; c; c = c->next) {
        if (c->win == w) {
            if (is_on_cur_desk(w))
                gtk_widget_show(GTK_WIDGET(c->widget));
            else
                gtk_widget_hide(GTK_WIDGET(c->widget));
            c->save = 1;
            return;
        }
    }

    if (!is_skip(w) && is_on_cur_desk(w)) {
        c = malloc(sizeof *c);
        c->next = head;
        head = c;
        c->win = w;
        c->save = 1;

        snprint_wm_name(buf, sizeof buf, w);
        c->widget = gtk_button_new_with_label(buf);
        gtk_signal_connect(GTK_OBJECT(c->widget), "clicked",
            GTK_SIGNAL_FUNC(raise_win_cb), (gpointer)w);
        gtk_box_pack_start(GTK_BOX(container), c->widget, TRUE, TRUE, 0);
        gtk_misc_set_alignment(GTK_MISC(GTK_BIN(c->widget)->child), 0, 0.5);
        gtk_widget_show(c->widget);

        XSelectInput(dpy, c->win, PropertyChangeMask);
        gdk_window_add_filter(gdk_window_lookup(c->win),
            check_event, container);
    }
}

void cleanup_clientitem(client_t *c)
{
    client_t *p;

    if (c->save) {
        c->save = 0;
    } else {
        gtk_widget_destroy(GTK_WIDGET(c->widget));
        if (head == c) {
            head = c->next;
        } else {
            for (p = head; p && p->next; p = p->next)
                if (p->next == c) p->next = c->next;
        }
        free(c);
    }
}

GdkFilterReturn check_event(GdkXEvent *gdk_xevent, GdkEvent *event,
    gpointer container)
{
    XEvent *e = gdk_xevent;
    client_t *c;
    char buf[NAME_SIZE];

    if (e->type == PropertyNotify) {
        if (e->xproperty.window == root) {
            if (e->xproperty.atom == net_cur_desk ||
                    e->xproperty.atom == net_client_list)
                update_client_list(GTK_WIDGET(container));
        } else {
            if (e->xproperty.atom == net_wm_desk) {
                update_client_list(GTK_WIDGET(container));
            } else {
                /* don't really care which atom changed; just redo it */
                for (c = head; c; c = c->next) {
                    if (c->win == e->xproperty.window) {
                        snprint_wm_name(buf, sizeof buf, c->win);
                        gtk_label_set_text(
                            GTK_LABEL(GTK_BIN(c->widget)->child), buf);
                    }
                }
            }
        }
    }

    return GDK_FILTER_CONTINUE;
}

void update_client_list(GtkWidget *container)
{
    client_t *c, *save_next;
    unsigned long read, left;
    int i;
    Window w;

    for (i = 0, left = 1; left; i += read) {
        read = get_atoms(root, net_client_list, XA_WINDOW, i, &w, 1, &left);
        if (read)
            update_clientitem(container, w);
        else
            break;
    }

    for (c = head; c; c = save_next) {
        save_next = c->next;
        cleanup_clientitem(c);
    }
}

void menu_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
    gpointer data)
{
    GtkWidget *button = GTK_WIDGET(data);
    GtkRequisition req;
    gint wx, wy;

    gdk_window_get_root_origin(button->window, &wx, &wy);
    *x = wx + button->allocation.x;
    *y = wy + button->allocation.y;

    if (opt_bottom) {
        gtk_widget_size_request(GTK_WIDGET(menu), &req);
        *y -= req.height;
    } else {
        *y += button->allocation.height;
    }
}

void show_menu_cb(GtkWidget *widget, gpointer menu)
{
    gtk_menu_popup(menu, NULL, NULL, menu_position, menu_button, 0, 0);
}

void raise_win_cb(GtkWidget *widget, Window w)
{
    raise_win(w);
}

void fork_exec_cb(GtkWidget *widget, char *data)
{
    fork_exec(data);
}

void setup_panel_atoms()
{
    net_wm_strut = XInternAtom(dpy, "_NET_WM_STRUT", False);
    net_wm_strut_partial = XInternAtom(dpy, "_NET_WM_STRUT_PARTIAL", False);
    net_wm_wintype = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
    net_wm_wintype_dock = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
}

void sig_handler(int signal)
{
    if (signal == SIGCHLD)
        wait(NULL);
}

void set_strut(Window w, strut_t *s)
{
    unsigned long data[4];

    data[0] = s->left;
    data[1] = s->right;
    data[2] = s->top;
    data[3] = s->bottom;

    XChangeProperty(dpy, w, net_wm_strut, XA_CARDINAL,
        32, PropModeReplace, (unsigned char *)data, 4);
}