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