add a SessionLogout action
[mikachu/openbox.git] / openbox / actions / directionalwindows.c
1 #include "openbox/actions.h"
2 #include "openbox/event.h"
3 #include "openbox/stacking.h"
4 #include "openbox/window.h"
5 #include "openbox/focus_cycle.h"
6 #include "openbox/openbox.h"
7 #include "openbox/misc.h"
8 #include "gettext.h"
9
10 typedef struct {
11     gboolean interactive;
12     gboolean dialog;
13     gboolean dock_windows;
14     gboolean desktop_windows;
15     ObDirection direction;
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_cycle_func(ObParseInst *i, xmlDocPtr doc,
25                                  xmlNodePtr node);
26 static gpointer setup_target_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_directionalwindows_startup(void)
39 {
40     actions_register("DirectionalCycleWindows", setup_cycle_func, free_func,
41                      run_func, i_input_func, i_cancel_func);
42     actions_register("DirectionalTargetWindow", setup_target_func, free_func,
43                      run_func, NULL, NULL);
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("dialog", node)))
56         o->dialog = parse_bool(doc, n);
57     if ((n = parse_find_node("bar", node)))
58         o->bar = parse_bool(doc, n);
59     if ((n = parse_find_node("raise", node)))
60         o->raise = parse_bool(doc, n);
61     if ((n = parse_find_node("panels", node)))
62         o->dock_windows = parse_bool(doc, n);
63     if ((n = parse_find_node("desktop", node)))
64         o->desktop_windows = parse_bool(doc, n);
65     if ((n = parse_find_node("direction", node))) {
66         gchar *s = parse_string(doc, n);
67         if (!g_ascii_strcasecmp(s, "north") ||
68             !g_ascii_strcasecmp(s, "up"))
69             o->direction = OB_DIRECTION_NORTH;
70         else if (!g_ascii_strcasecmp(s, "northwest"))
71             o->direction = OB_DIRECTION_NORTHWEST;
72         else if (!g_ascii_strcasecmp(s, "northeast"))
73             o->direction = OB_DIRECTION_NORTHEAST;
74         else if (!g_ascii_strcasecmp(s, "west") ||
75                  !g_ascii_strcasecmp(s, "left"))
76             o->direction = OB_DIRECTION_WEST;
77         else if (!g_ascii_strcasecmp(s, "east") ||
78                  !g_ascii_strcasecmp(s, "right"))
79             o->direction = OB_DIRECTION_EAST;
80         else if (!g_ascii_strcasecmp(s, "south") ||
81                  !g_ascii_strcasecmp(s, "down"))
82             o->direction = OB_DIRECTION_SOUTH;
83         else if (!g_ascii_strcasecmp(s, "southwest"))
84             o->direction = OB_DIRECTION_SOUTHWEST;
85         else if (!g_ascii_strcasecmp(s, "southeast"))
86             o->direction = OB_DIRECTION_SOUTHEAST;
87         g_free(s);
88     }
89
90     if ((n = parse_find_node("finalactions", node))) {
91         xmlNodePtr m;
92
93         m = parse_find_node("action", n->xmlChildrenNode);
94         while (m) {
95             ObActionsAct *action = actions_parse(i, doc, m);
96             if (action) o->actions = g_slist_prepend(o->actions, action);
97             m = parse_find_node("action", m->next);
98         }
99     }
100     else {
101         o->actions = g_slist_prepend(o->actions,
102                                      actions_parse_string("Focus"));
103         o->actions = g_slist_prepend(o->actions,
104                                      actions_parse_string("Raise"));
105         o->actions = g_slist_prepend(o->actions,
106                                      actions_parse_string("Unshade"));
107     }
108
109     return o;
110 }
111
112 static gpointer setup_cycle_func(ObParseInst *i, xmlDocPtr doc,
113                                  xmlNodePtr node)
114 {
115     Options *o = setup_func(i, doc, node);
116     o->interactive = TRUE;
117     return o;
118 }
119
120 static gpointer setup_target_func(ObParseInst *i, xmlDocPtr doc,
121                                   xmlNodePtr node)
122 {
123     Options *o = setup_func(i, doc, node);
124     o->interactive = FALSE;
125     return o;
126 }
127
128 static void free_func(gpointer options)
129 {
130     Options *o = options;
131
132     while (o->actions) {
133         actions_act_unref(o->actions->data);
134         o->actions = g_slist_delete_link(o->actions, o->actions);
135     }
136
137     g_free(o);
138 }
139
140 static gboolean run_func(ObActionsData *data, gpointer options)
141 {
142     Options *o = options;
143
144     if (!o->interactive)
145         end_cycle(FALSE, data->state, o);
146     else {
147         struct _ObClient *ft;
148
149         ft = focus_directional_cycle(o->direction,
150                                      o->dock_windows,
151                                      o->desktop_windows,
152                                      TRUE,
153                                      o->bar,
154                                      o->dialog,
155                                      FALSE, FALSE);
156         cycling = TRUE;
157
158         stacking_restore();
159         if (o->raise && ft) stacking_temp_raise(CLIENT_AS_WINDOW(ft));
160     }
161
162     return o->interactive;
163 }
164
165 static gboolean i_input_func(guint initial_state,
166                              XEvent *e,
167                              gpointer options,
168                              gboolean *used)
169 {
170     if (e->type == KeyPress) {
171         /* Escape cancels no matter what */
172         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
173             end_cycle(TRUE, e->xkey.state, options);
174             return FALSE;
175         }
176
177         /* There were no modifiers and they pressed enter */
178         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) &&
179                  !initial_state)
180         {
181             end_cycle(FALSE, e->xkey.state, options);
182             return FALSE;
183         }
184     }
185     /* They released the modifiers */
186     else if (e->type == KeyRelease && initial_state &&
187              (e->xkey.state & initial_state) == 0)
188     {
189         end_cycle(FALSE, e->xkey.state, options);
190         return FALSE;
191     }
192
193     return TRUE;
194 }
195
196 static void i_cancel_func(gpointer options)
197 {
198     /* we get cancelled when we move focus, but we're not cycling anymore, so
199        just ignore that */
200     if (cycling)
201         end_cycle(TRUE, 0, options);
202 }
203
204 static void end_cycle(gboolean cancel, guint state, Options *o)
205 {
206     struct _ObClient *ft;
207
208     ft = focus_directional_cycle(o->direction,
209                                  o->dock_windows,
210                                  o->desktop_windows,
211                                  o->interactive,
212                                  o->bar,
213                                  o->dialog,
214                                  TRUE, cancel);
215     cycling = FALSE;
216
217     if (ft)
218         actions_run_acts(o->actions, OB_USER_ACTION_KEYBOARD_KEY,
219                          state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, ft);
220
221     stacking_restore();
222 }