Don't run actions in focus cycling actions backwards either (as in 664b0b4e943ba4d410...
[dana/openbox.git] / openbox / actions / cyclewindows.c
1 #include "openbox/actions.h"
2 #include "openbox/stacking.h"
3 #include "openbox/window.h"
4 #include "openbox/event.h"
5 #include "openbox/focus_cycle.h"
6 #include "openbox/openbox.h"
7 #include "gettext.h"
8
9 typedef struct {
10     gboolean linear;
11     gboolean dialog;
12     gboolean dock_windows;
13     gboolean desktop_windows;
14     gboolean all_desktops;
15     gboolean forward;
16     gboolean bar;
17     gboolean raise;
18     GSList *actions;
19 } Options;
20
21 static gboolean cycling = FALSE;
22
23 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node);
24 static gpointer setup_forward_func(ObParseInst *i, xmlDocPtr doc,
25                                    xmlNodePtr node);
26 static gpointer setup_backward_func(ObParseInst *i, xmlDocPtr doc,
27                                     xmlNodePtr node);
28 static void     free_func(gpointer options);
29 static gboolean run_func(ObActionsData *data, gpointer options);
30 static gboolean i_input_func(guint initial_state,
31                              XEvent *e,
32                              gpointer options,
33                              gboolean *used);
34 static void     i_cancel_func(gpointer options);
35
36 static void     end_cycle(gboolean cancel, guint state, Options *o);
37
38 void action_cyclewindows_startup(void)
39 {
40     actions_register("NextWindow", setup_forward_func, free_func,
41                      run_func, i_input_func, i_cancel_func);
42     actions_register("PreviousWindow", setup_backward_func, free_func,
43                      run_func, i_input_func, i_cancel_func);
44 }
45
46 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
47 {
48     xmlNodePtr n;
49     Options *o;
50
51     o = g_new0(Options, 1);
52     o->dialog = TRUE;
53     o->bar = TRUE;
54
55     if ((n = parse_find_node("linear", node)))
56         o->linear = parse_bool(doc, n);
57     if ((n = parse_find_node("dialog", node)))
58         o->dialog = parse_bool(doc, n);
59     if ((n = parse_find_node("bar", node)))
60         o->bar = parse_bool(doc, n);
61     if ((n = parse_find_node("raise", node)))
62         o->raise = parse_bool(doc, n);
63     if ((n = parse_find_node("panels", node)))
64         o->dock_windows = parse_bool(doc, n);
65     if ((n = parse_find_node("desktop", node)))
66         o->desktop_windows = parse_bool(doc, n);
67     if ((n = parse_find_node("allDesktops", node)))
68         o->all_desktops = parse_bool(doc, n);
69
70     if ((n = parse_find_node("finalactions", node))) {
71         xmlNodePtr m;
72
73         m = parse_find_node("action", n->xmlChildrenNode);
74         while (m) {
75             ObActionsAct *action = actions_parse(i, doc, m);
76             if (action) o->actions = g_slist_append(o->actions, action);
77             m = parse_find_node("action", m->next);
78         }
79     }
80     else {
81         o->actions = g_slist_prepend(o->actions,
82                                      actions_parse_string("Focus"));
83         o->actions = g_slist_prepend(o->actions,
84                                      actions_parse_string("Raise"));
85         o->actions = g_slist_prepend(o->actions,
86                                      actions_parse_string("Unshade"));
87     }
88
89     return o;
90 }
91
92 static gpointer setup_forward_func(ObParseInst *i, xmlDocPtr doc,
93                                    xmlNodePtr node)
94 {
95     Options *o = setup_func(i, doc, node);
96     o->forward = TRUE;
97     return o;
98 }
99
100 static gpointer setup_backward_func(ObParseInst *i, xmlDocPtr doc,
101                                     xmlNodePtr node)
102 {
103     Options *o = setup_func(i, doc, node);
104     o->forward = FALSE;
105     return o;
106 }
107
108 static void free_func(gpointer options)
109 {
110     Options *o = options;
111
112     while (o->actions) {
113         actions_act_unref(o->actions->data);
114         o->actions = g_slist_delete_link(o->actions, o->actions);
115     }
116
117     g_free(o);
118 }
119
120 static gboolean run_func(ObActionsData *data, gpointer options)
121 {
122     Options *o = options;
123     struct _ObClient *ft;
124
125     ft = focus_cycle(o->forward,
126                      o->all_desktops,
127                      o->dock_windows,
128                      o->desktop_windows,
129                      o->linear,
130                      TRUE,
131                      o->bar,
132                      o->dialog,
133                      FALSE, FALSE);
134     cycling = TRUE;
135
136     stacking_restore();
137     if (o->raise && ft) stacking_temp_raise(CLIENT_AS_WINDOW(ft));
138
139     return TRUE;
140 }
141
142 static gboolean i_input_func(guint initial_state,
143                              XEvent *e,
144                              gpointer options,
145                              gboolean *used)
146 {
147     if (e->type == KeyPress) {
148         /* Escape cancels no matter what */
149         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
150             end_cycle(TRUE, e->xkey.state, options);
151             return FALSE;
152         }
153
154         /* There were no modifiers and they pressed enter */
155         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) &&
156                  !initial_state)
157         {
158             end_cycle(FALSE, e->xkey.state, options);
159             return FALSE;
160         }
161     }
162     /* They released the modifiers */
163     else if (e->type == KeyRelease && initial_state &&
164              (e->xkey.state & initial_state) == 0)
165     {
166         end_cycle(FALSE, e->xkey.state, options);
167         return FALSE;
168     }
169
170     return TRUE;
171 }
172
173 static void i_cancel_func(gpointer options)
174 {
175     /* we get cancelled when we move focus, but we're not cycling anymore, so
176        just ignore that */
177     if (cycling)
178         end_cycle(TRUE, 0, options);
179 }
180
181 static void end_cycle(gboolean cancel, guint state, Options *o)
182 {
183     struct _ObClient *ft;
184
185     ft = focus_cycle(o->forward,
186                      o->all_desktops,
187                      o->dock_windows,
188                      o->desktop_windows,
189                      o->linear,
190                      TRUE,
191                      o->bar,
192                      o->dialog,
193                      TRUE, cancel);
194     cycling = FALSE;
195
196     if (ft)
197         actions_run_acts(o->actions, OB_USER_ACTION_KEYBOARD_KEY,
198                          state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, ft);
199
200     stacking_restore();
201 }