cycleinwdows defaults to forward
[dana/openbox-history.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 "gettext.h"
6
7 typedef struct {
8     gboolean linear;
9     gboolean dialog;
10     gboolean dock_windows;
11     gboolean desktop_windows;
12     gboolean all_desktops;
13     gboolean forward;
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 void     free_func(gpointer options);
21 static gboolean run_func(ObActionsData *data, gpointer options);
22 static gboolean i_input_func(guint initial_state,
23                              XEvent *e,
24                              gpointer options,
25                              gboolean *used);
26 static void     i_cancel_func(gpointer options);
27
28 static void     end_cycle(gboolean cancel, guint state, Options *o);
29
30 void action_cyclewindows_startup()
31 {
32     actions_register("CycleWindows",
33                      setup_func,
34                      free_func,
35                      run_func,
36                      i_input_func,
37                      i_cancel_func);
38 }
39
40 static gpointer setup_func(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
41 {
42     xmlNodePtr n;
43     Options *o;
44
45     o = g_new0(Options, 1);
46     o->dialog = TRUE;
47     o->forward = TRUE;
48
49     if ((n = parse_find_node("forward", node)))
50         o->forward = parse_bool(doc, n);
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     return o;
73 }
74
75 static void free_func(gpointer options)
76 {
77     Options *o = options;
78
79     g_free(o);
80 }
81
82 static gboolean run_func(ObActionsData *data, gpointer options)
83 {
84     Options *o = options;
85
86     /* if using focus_delay, stop the timer now so that focus doesn't go moving
87        on us */
88     event_halt_focus_delay();
89     
90     focus_cycle(o->forward,
91                 o->all_desktops,
92                 o->dock_windows,
93                 o->desktop_windows,
94                 o->linear,
95                 TRUE,
96                 o->dialog,
97                 FALSE, FALSE);
98     cycling = TRUE;
99
100     return TRUE;
101 }
102
103 static gboolean i_input_func(guint initial_state,
104                              XEvent *e,
105                              gpointer options,
106                              gboolean *used)
107 {
108     if (e->type == KeyPress) {
109         /* Escape cancels no matter what */
110         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
111             end_cycle(TRUE, e->xkey.state, options);
112             return FALSE;
113         }
114
115         /* There were no modifiers and they pressed enter */
116         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) &&
117                  !initial_state)
118         {
119             end_cycle(FALSE, e->xkey.state, options);
120             return FALSE;
121         }
122     }
123     /* They released the modifiers */
124     else if (e->type == KeyRelease && initial_state &&
125              (e->xkey.state & initial_state) == 0)
126     {
127         end_cycle(FALSE, e->xkey.state, options);
128         return FALSE;
129     }
130
131     return TRUE;
132 }
133
134 static void i_cancel_func(gpointer options)
135 {
136     /* we get cancelled when we move focus, but we're not cycling anymore, so
137        just ignore that */
138     if (cycling)
139         end_cycle(TRUE, 0, options);
140 }
141
142 static void end_cycle(gboolean cancel, guint state, Options *o)
143 {
144     struct _ObClient *ft;
145
146     ft = focus_cycle(o->forward,
147                      o->all_desktops,
148                      o->dock_windows,
149                      o->desktop_windows,
150                      o->linear,
151                      TRUE,
152                      o->dialog,
153                      TRUE, cancel);
154
155     if (ft) {
156         actions_run_acts(o->actions, OB_USER_ACTION_KEYBOARD_KEY,
157                          state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, ft);
158     }
159     cycling = FALSE;
160 }