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