hm..
[mikachu/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 "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
48     if ((n = parse_find_node("forward", node)))
49         o->forward = parse_bool(doc, n);
50     if ((n = parse_find_node("linear", node)))
51         o->linear = parse_bool(doc, n);
52     if ((n = parse_find_node("dialog", node)))
53         o->dialog = parse_bool(doc, n);
54     if ((n = parse_find_node("panels", node)))
55         o->dock_windows = parse_bool(doc, n);
56     if ((n = parse_find_node("desktop", node)))
57         o->desktop_windows = parse_bool(doc, n);
58     if ((n = parse_find_node("allDesktops", node)))
59         o->all_desktops = parse_bool(doc, n);
60
61     if ((n = parse_find_node("finalactions", node))) {
62         xmlNodePtr m;
63
64         m = parse_find_node("action", n->xmlChildrenNode);
65         while (m) {
66             ObActionsAct *action = actions_parse(i, doc, m);
67             if (action) o->actions = g_slist_prepend(o->actions, action);
68             m = parse_find_node("action", m->next);
69         }
70     }
71     return o;
72 }
73
74 static void free_func(gpointer options)
75 {
76     Options *o = options;
77
78     g_free(o);
79 }
80
81 static gboolean run_func(ObActionsData *data, gpointer options)
82 {
83     Options *o = options;
84
85     /* if using focus_delay, stop the timer now so that focus doesn't go moving
86        on us */
87     event_halt_focus_delay();
88     
89     focus_cycle(o->forward,
90                 o->all_desktops,
91                 o->dock_windows,
92                 o->desktop_windows,
93                 o->linear,
94                 TRUE,
95                 o->dialog,
96                 FALSE, FALSE);
97     cycling = TRUE;
98
99     return TRUE;
100 }
101
102 static gboolean i_input_func(guint initial_state,
103                              XEvent *e,
104                              gpointer options,
105                              gboolean *used)
106 {
107     if (e->type == KeyPress) {
108         /* Escape cancels no matter what */
109         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
110             end_cycle(TRUE, e->xkey.state, options);
111             return FALSE;
112         }
113
114         /* There were no modifiers and they pressed enter */
115         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) &&
116                  !initial_state)
117         {
118             end_cycle(FALSE, e->xkey.state, options);
119             return FALSE;
120         }
121     }
122     /* They released the modifiers */
123     else if (e->type == KeyRelease && initial_state &&
124              (e->xkey.state & initial_state) == 0)
125     {
126         end_cycle(FALSE, e->xkey.state, options);
127         return FALSE;
128     }
129
130     return TRUE;
131 }
132
133 static void i_cancel_func(gpointer options)
134 {
135     /* we get cancelled when we move focus, but we're not cycling anymore, so
136        just ignore that */
137     if (cycling)
138         end_cycle(TRUE, 0, options);
139 }
140
141 static void end_cycle(gboolean cancel, guint state, Options *o)
142 {
143     struct _ObClient *ft;
144
145     ft = focus_cycle(o->forward,
146                      o->all_desktops,
147                      o->dock_windows,
148                      o->desktop_windows,
149                      o->linear,
150                      TRUE,
151                      o->dialog,
152                      TRUE, cancel);
153
154     if (ft) {
155         actions_run_acts(o->actions, OB_USER_ACTION_KEYBOARD_KEY,
156                          state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, ft);
157     }
158     cycling = FALSE;
159 }