]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions/moveresizeto.c
Add action_list_run.c/h with action_list_run() and struct ObActionListRun.
[dana/openbox.git] / openbox / actions / moveresizeto.c
1 #include "openbox/action.h"
2 #include "openbox/action_list_run.h"
3 #include "openbox/action_value.h"
4 #include "openbox/client.h"
5 #include "openbox/screen.h"
6 #include "openbox/frame.h"
7 #include "openbox/config.h"
8
9 enum {
10     CURRENT_MONITOR = -1,
11     ALL_MONITORS = -2,
12     NEXT_MONITOR = -3,
13     PREV_MONITOR = -4
14 };
15
16 typedef struct {
17     GravityCoord x;
18     GravityCoord y;
19     gint w;
20     gint w_denom;
21     gint h;
22     gint h_denom;
23     gint monitor;
24 } Options;
25
26 static gpointer setup_func(GHashTable *config);
27 static void free_func(gpointer o);
28 static gboolean run_func(const ObActionListRun *data, gpointer options);
29
30 void action_moveresizeto_startup(void)
31 {
32     action_register("MoveResizeTo", OB_ACTION_DEFAULT_FILTER_SINGLE,
33                     setup_func, free_func, run_func);
34 }
35
36 static gpointer setup_func(GHashTable *config)
37 {
38     ObActionValue *v;
39     Options *o;
40
41     o = g_slice_new0(Options);
42     o->x.pos = G_MININT;
43     o->y.pos = G_MININT;
44     o->w = G_MININT;
45     o->h = G_MININT;
46     o->monitor = CURRENT_MONITOR;
47
48     v = g_hash_table_lookup(config, "x");
49     if (v && action_value_is_string(v))
50         action_value_gravity_coord(v, &o->x);
51     v = g_hash_table_lookup(config, "y");
52     if (v && action_value_is_string(v))
53         action_value_gravity_coord(v, &o->y);
54
55     v = g_hash_table_lookup(config, "width");
56     if (v && action_value_is_string(v))
57         if (g_ascii_strcasecmp(action_value_string(v), "current") != 0)
58             action_value_fraction(v, &o->w, &o->w_denom);
59     v = g_hash_table_lookup(config, "height");
60     if (v && action_value_is_string(v))
61         if (g_ascii_strcasecmp(action_value_string(v), "current") != 0)
62             action_value_fraction(v, &o->h, &o->h_denom);
63
64     v = g_hash_table_lookup(config, "monitor");
65     if (v && action_value_is_string(v)) {
66         const gchar *s = action_value_string(v);
67         if (g_ascii_strcasecmp(s, "current") != 0) {
68             if (!g_ascii_strcasecmp(s, "all"))
69                 o->monitor = ALL_MONITORS;
70             else if(!g_ascii_strcasecmp(s, "next"))
71                 o->monitor = NEXT_MONITOR;
72             else if(!g_ascii_strcasecmp(s, "prev"))
73                 o->monitor = PREV_MONITOR;
74             else
75                 o->monitor = action_value_int(v) - 1;
76         }
77     }
78
79     return o;
80 }
81
82 static void free_func(gpointer o)
83 {
84     g_slice_free(Options, o);
85 }
86
87 /* Always return FALSE because its not interactive */
88 static gboolean run_func(const ObActionListRun *data, gpointer options)
89 {
90     Options *o = options;
91
92     if (data->client) {
93         Rect *area, *carea;
94         ObClient *c;
95         guint mon, cmon;
96         gint x, y, lw, lh, w, h;
97
98         c = data->client;
99         mon = o->monitor;
100         cmon = client_monitor(c);
101         switch (mon) {
102         case CURRENT_MONITOR:
103             mon = cmon; break;
104         case ALL_MONITORS:
105             mon = SCREEN_AREA_ALL_MONITORS; break;
106         case NEXT_MONITOR:
107             mon = (cmon + 1 > screen_num_monitors - 1) ? 0 : (cmon + 1); break;
108         case PREV_MONITOR:
109             mon = (cmon == 0) ? (screen_num_monitors - 1) : (cmon - 1); break;
110         default:
111             g_assert_not_reached();
112         }
113
114         area = screen_area(c->desktop, mon, NULL);
115         carea = screen_area(c->desktop, cmon, NULL);
116
117         w = o->w;
118         if (w == G_MININT) w = c->area.width;
119         else if (o->w_denom) w = (w * area->width) / o->w_denom;
120
121         h = o->h;
122         if (h == G_MININT) h = c->area.height;
123         else if (o->h_denom) h = (h * area->height) / o->h_denom;
124
125         /* it might not be able to resize how they requested, so find out what
126            it will actually be resized to */
127         x = c->area.x;
128         y = c->area.y;
129         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
130
131         /* get the frame's size */
132         w += c->frame->size.left + c->frame->size.right;
133         h += c->frame->size.top + c->frame->size.bottom;
134
135         x = o->x.pos;
136         if (o->x.denom)
137             x = (x * area->width) / o->x.denom;
138         if (o->x.center) x = (area->width - w) / 2;
139         else if (x == G_MININT) x = c->frame->area.x - carea->x;
140         else if (o->x.opposite) x = area->width - w - x;
141         x += area->x;
142
143         y = o->y.pos;
144         if (o->y.denom)
145             y = (y * area->height) / o->y.denom;
146         if (o->y.center) y = (area->height - h) / 2;
147         else if (y == G_MININT) y = c->frame->area.y - carea->y;
148         else if (o->y.opposite) y = area->height - h - y;
149         y += area->y;
150
151         /* get the client's size back */
152         w -= c->frame->size.left + c->frame->size.right;
153         h -= c->frame->size.top + c->frame->size.bottom;
154
155         frame_frame_gravity(c->frame, &x, &y); /* get the client coords */
156         client_try_configure(c, &x, &y, &w, &h, &lw, &lh, TRUE);
157         /* force it on screen if its moving to another monitor */
158         client_find_onscreen(c, &x, &y, w, h, mon != cmon);
159
160         action_client_move(data, TRUE);
161         client_configure(c, x, y, w, h, TRUE, TRUE, FALSE);
162         action_client_move(data, FALSE);
163
164         g_slice_free(Rect, area);
165         g_slice_free(Rect, carea);
166     }
167
168     return FALSE;
169 }