Merge branch 'backport' into work
[mikachu/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 dock_windows;
12     gboolean desktop_windows;
13     gboolean all_desktops;
14     gboolean forward;
15     gboolean bar;
16     gboolean raise;
17     ObFocusCyclePopupMode dialog_mode;
18     GSList *actions;
19 } Options;
20
21 static gboolean cycling = FALSE;
22
23 static gpointer setup_func(xmlNodePtr node);
24 static gpointer setup_forward_func(xmlNodePtr node);
25 static gpointer setup_backward_func(xmlNodePtr node);
26 static void     free_func(gpointer options);
27 static gboolean run_func(ObActionsData *data, gpointer options);
28 static gboolean i_input_func(guint initial_state,
29                              XEvent *e,
30                              gpointer options,
31                              gboolean *used);
32 static void     i_cancel_func(gpointer options);
33
34 static void     end_cycle(gboolean cancel, guint state, Options *o);
35
36 void action_cyclewindows_startup(void)
37 {
38     actions_register("NextWindow", setup_forward_func, free_func,
39                      run_func, i_input_func, i_cancel_func);
40     actions_register("PreviousWindow", setup_backward_func, free_func,
41                      run_func, i_input_func, i_cancel_func);
42 }
43
44 static gpointer setup_func(xmlNodePtr node)
45 {
46     xmlNodePtr n;
47     Options *o;
48
49     o = g_new0(Options, 1);
50     o->bar = TRUE;
51     o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_LIST;
52
53     if ((n = obt_parse_find_node(node, "linear")))
54         o->linear = obt_parse_node_bool(n);
55     if ((n = obt_parse_find_node(node, "dialog"))) {
56         if (obt_parse_node_contains(n, "none"))
57             o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_NONE;
58         else if (obt_parse_node_contains(n, "icons"))
59             o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_ICONS;
60     }
61     if ((n = obt_parse_find_node(node, "bar")))
62         o->bar = obt_parse_node_bool(n);
63     if ((n = obt_parse_find_node(node, "raise")))
64         o->raise = obt_parse_node_bool(n);
65     if ((n = obt_parse_find_node(node, "panels")))
66         o->dock_windows = obt_parse_node_bool(n);
67     if ((n = obt_parse_find_node(node, "desktop")))
68         o->desktop_windows = obt_parse_node_bool(n);
69     if ((n = obt_parse_find_node(node, "allDesktops")))
70         o->all_desktops = obt_parse_node_bool(n);
71
72     if ((n = obt_parse_find_node(node, "finalactions"))) {
73         xmlNodePtr m;
74
75         m = obt_parse_find_node(n->children, "action");
76         while (m) {
77             ObActionsAct *action = actions_parse(m);
78             if (action) o->actions = g_slist_append(o->actions, action);
79             m = obt_parse_find_node(m->next, "action");
80         }
81     }
82     else {
83         o->actions = g_slist_prepend(o->actions,
84                                      actions_parse_string("Focus"));
85         o->actions = g_slist_prepend(o->actions,
86                                      actions_parse_string("Raise"));
87         o->actions = g_slist_prepend(o->actions,
88                                      actions_parse_string("Unshade"));
89     }
90
91     return o;
92 }
93
94 static gpointer setup_forward_func(xmlNodePtr node)
95 {
96     Options *o = setup_func(node);
97     o->forward = TRUE;
98     return o;
99 }
100
101 static gpointer setup_backward_func(xmlNodePtr node)
102 {
103     Options *o = setup_func(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_mode,
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_mode,
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 }