add the adddesktop and removedesktop actions
[dana/openbox-history.git] / openbox / actions / addremovedesktop.c
1 #include "openbox/actions.h"
2 #include "openbox/screen.h"
3 #include "openbox/client.h"
4 #include "openbox/debug.h"
5 #include <glib.h>
6
7 typedef struct {
8     gboolean current;
9     gboolean add;
10 } Options;
11
12 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
13 static gpointer setup_add_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
14 static gpointer setup_remove_func(ObParseInst *i,
15                                   xmlDocPtr doc, xmlNodePtr node);
16 static void     free_func(gpointer options);
17 static gboolean run_func(ObActionsData *data, gpointer options);
18
19 void action_addremovedesktop_startup()
20 {
21     actions_register("AddDesktop",
22                      setup_add_func,
23                      free_func,
24                      run_func,
25                      NULL, NULL);
26     actions_register("RemoveDesktop",
27                      setup_remove_func,
28                      free_func,
29                      run_func,
30                      NULL, NULL);
31 }
32
33 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
34 {
35     xmlNodePtr n;
36     Options *o;
37
38     o = g_new0(Options, 1);
39
40     if ((n = parse_find_node("where", node))) {
41         gchar *s = parse_string(doc, n);
42         if (!g_ascii_strcasecmp(s, "last"))
43             o->current = FALSE;
44         else if (!g_ascii_strcasecmp(s, "current"))
45             o->current = TRUE;
46         g_free(s);
47     }
48
49     return o;
50 }
51
52 static gpointer setup_add_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
53 {
54     Options *o = setup_func(i, doc, node);
55     o->add = TRUE;
56     return o;
57 }
58
59 static gpointer setup_remove_func(ObParseInst *i,
60                                   xmlDocPtr doc, xmlNodePtr node)
61 {
62     Options *o = setup_func(i, doc, node);
63     o->add = FALSE;
64     return o;
65 }
66
67 static void free_func(gpointer options)
68 {
69     Options *o = options;
70
71     g_free(o);
72 }
73
74 /* Always return FALSE because its not interactive */
75 static gboolean run_func(ObActionsData *data, gpointer options)
76 {
77     Options *o = options;
78
79     actions_client_move(data, FALSE);
80
81     if (o->add) {
82         screen_set_num_desktops(screen_num_desktops+1);
83
84         /* move all the clients over */
85         if (o->current) {
86             GList *it;
87
88             for (it = client_list; it; it = g_list_next(it)) {
89                 ObClient *c = it->data;
90                 if (c->desktop != DESKTOP_ALL && c->desktop >= screen_desktop)
91                     client_set_desktop(c, c->desktop+1, FALSE, TRUE);
92             }
93         }
94     }
95     else if (screen_num_desktops > 1) {
96         guint rmdesktop, movedesktop;
97         GList *it, *stacking_copy;
98
99         /* what desktop are we removing and moving to? */
100         if (o->current)
101             rmdesktop = screen_desktop;
102         else
103             rmdesktop = screen_num_desktops - 1;
104         if (rmdesktop < screen_num_desktops - 1)
105             movedesktop = rmdesktop + 1;
106         else
107             movedesktop = rmdesktop;
108
109         /* make a copy of the list cuz we're changing it */
110         stacking_copy = g_list_copy(stacking_list);
111         for (it = g_list_last(stacking_copy); it; it = g_list_previous(it)) {
112             if (WINDOW_IS_CLIENT(it->data)) {
113                 ObClient *c = it->data;
114                 guint d = c->desktop;
115                 if (d != DESKTOP_ALL && d >= movedesktop) {
116                     client_set_desktop(c, c->desktop - 1, TRUE, TRUE);
117                     ob_debug("moving window %s\n", c->title);
118                 }
119                 /* raise all the windows that are on the current desktop which
120                    is being merged */
121                 if ((screen_desktop == rmdesktop - 1 ||
122                      screen_desktop == rmdesktop) &&
123                     (d == DESKTOP_ALL || d == screen_desktop))
124                 {
125                     stacking_raise(CLIENT_AS_WINDOW(c));
126                     ob_debug("raising window %s\n", c->title);
127                 }
128             }
129         }
130
131         /* act like we're changing desktops */
132         if (screen_desktop < screen_num_desktops - 1) {
133             gint d = screen_desktop;
134             screen_desktop = screen_last_desktop;
135             screen_set_desktop(d, TRUE);
136             ob_debug("fake desktop change\n");
137         }
138
139         screen_set_num_desktops(screen_num_desktops-1);
140     }
141
142     actions_client_move(data, TRUE);
143
144     return FALSE;
145 }