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