add the client_list_menu plugin
[mikachu/openbox.git] / plugins / menu / client_list_menu.c
1 #include "kernel/openbox.h"
2 #include "kernel/menu.h"
3 #include "kernel/action.h"
4 #include "kernel/screen.h"
5 #include "kernel/client.h"
6 #include "kernel/focus.h"
7
8 #include "render/theme.h"
9
10 #include <glib.h>
11
12 static char *PLUGIN_NAME = "client_list_menu";
13
14 typedef struct {
15     GSList *submenus;
16 } Client_List_Menu_Data;
17
18 typedef struct {
19     guint desktop;
20 } Client_List_Desktop_Menu_Data;
21
22 #define CLIENT_LIST_MENU(m) ((ObMenu *)m)
23 #define CLIENT_LIST_MENU_DATA(m) ((Client_List_Menu_Data *)((ObMenu *)m)->plugin_data)
24
25 #define CLIENT_LIST_DESKTOP_MENU(m) ((ObMenu *)m)
26 #define CLIENT_LIST_DESKTOP_MENU_DATA(m) ((Client_List_Desktop_Menu_Data *)((ObMenu *)m)->plugin_data)
27
28 static void self_update(ObMenu *self);
29 static void self_destroy(ObMenu *self);
30
31 void plugin_setup_config() { }
32 void plugin_shutdown() { }
33 void plugin_destroy (ObMenu *m) { }
34
35 void *plugin_create()
36 {
37     ObMenu *menu = menu_new_full("Desktops", "client-list-menu", NULL,
38                                  NULL, self_update, NULL,
39                                  NULL, NULL, self_destroy);
40
41     menu->plugin = PLUGIN_NAME;
42     menu->plugin_data = g_new(Client_List_Menu_Data, 1);
43     CLIENT_LIST_MENU_DATA(menu)->submenus = NULL;
44
45     return (void *)menu;
46 }
47
48 void plugin_startup()
49 {
50     plugin_create("client_list_menu");
51 }
52
53
54 static void desk_update(ObMenu *self)
55 {
56     GList *it;
57     guint desk;
58
59     menu_clear(self);
60
61     desk = CLIENT_LIST_DESKTOP_MENU_DATA(self)->desktop;
62
63     for (it = focus_order[desk]; it; it = g_list_next(it)) {
64         ObClient *c = (ObClient *)it->data;
65         if (client_normal(c)) {
66             ObAction* a = action_from_string("activate");
67             a->data.activate.c = c;
68             menu_add_entry(self, menu_entry_new((c->iconic ?
69                                                  c->icon_title :
70                                                  c->title), a));
71         }
72     }
73
74     menu_render(self);
75 }
76
77 static void desk_selected(ObMenuEntry *entry,
78                           unsigned int button, unsigned int x, unsigned int y)
79 {
80     entry->action->data.activate.here = (button == 2);
81     entry->parent->client = entry->action->data.activate.c;
82     menu_entry_fire(entry, button, x, y);
83 }
84
85 static void desk_destroy(ObMenu *self)
86 {
87     g_free(self->plugin_data);
88 }
89
90 static void self_update(ObMenu *self)
91 {
92     guint i, n;
93     ObMenu *deskmenu;
94     gchar *s;
95     GList *eit, *enext;
96     GSList *sit, *snext;
97
98     n = g_slist_length(CLIENT_LIST_MENU_DATA(self)->submenus);
99
100     for (i = 0; i < screen_num_desktops; ++i) {
101         if (i >= n) {
102             s = g_strdup_printf("client-list-menu-desktop-%d", i);
103             deskmenu = menu_new_full(screen_desktop_names[i], s, self,
104                                      NULL,
105                                      desk_update, desk_selected, NULL, NULL,
106                                      desk_destroy);
107             g_free(s);
108
109             deskmenu->plugin = PLUGIN_NAME;
110             deskmenu->plugin_data = g_new(Client_List_Desktop_Menu_Data, 1);
111             CLIENT_LIST_DESKTOP_MENU_DATA(deskmenu)->desktop = i;
112
113             CLIENT_LIST_MENU_DATA(self)->submenus =
114                 g_slist_append(CLIENT_LIST_MENU_DATA(self)->submenus,
115                                deskmenu);
116         }
117
118         menu_add_entry(self, menu_entry_new_submenu(screen_desktop_names[i],
119                                                     deskmenu));
120     }
121
122     for (eit = g_list_nth(self->entries, i); eit; eit = enext) {
123         enext = g_list_next(eit);
124         menu_entry_free(eit->data);
125         self->entries = g_list_delete_link(self->entries, eit);
126     }
127
128     for (sit = g_slist_nth(CLIENT_LIST_MENU_DATA(self)->submenus, i);
129          sit; sit = snext) {
130         snext = g_slist_next(sit);
131         menu_free(sit->data);
132         CLIENT_LIST_MENU_DATA(self)->submenus = 
133             g_slist_delete_link(CLIENT_LIST_MENU_DATA(self)->submenus, sit);
134     }
135
136     menu_render(self);
137 }
138
139 static void self_destroy(ObMenu *self)
140 {
141     g_free(self->plugin_data);
142 }