]> icculus.org git repositories - dana/openbox.git/blob - plugins/resistance.c
export config values
[dana/openbox.git] / plugins / resistance.c
1 #include "../kernel/dispatch.h"
2 #include "../kernel/client.h"
3 #include "../kernel/frame.h"
4 #include "../kernel/stacking.h"
5 #include "../kernel/screen.h"
6 #include "../kernel/config.h"
7 #include <glib.h>
8
9 #define DEFAULT_RESISTANCE 10
10
11 void plugin_setup_config()
12 {
13     ConfigValue val;
14
15     config_def_set(config_def_new("resistance", Config_Integer,
16                                   "Edge Resistance",
17                                   "The amount of resistance to provide when "
18                                   "moving windows past edges."
19                                   "positioned."));
20     config_def_set(config_def_new("resistance.windows", Config_Bool,
21                                   "Edge Resistance On Windows",
22                                   "Whether to provide edge resistance when "
23                                   "moving windows past the edge of another "
24                                   "window."));
25     val.bool = TRUE;
26     config_set("resistance.windows", Config_Bool, val);
27 }
28
29 static void resist(Client *c, int *x, int *y)
30 {
31     GList *it;
32     Rect *area;
33     int l, t, r, b; /* requested edges */
34     int al, at, ar, ab; /* screen area edges */
35     int cl, ct, cr, cb; /* current edges */
36     int w, h; /* current size */
37     gboolean snapx = FALSE, snapy = FALSE;
38     ConfigValue resist, window_resist;
39
40     if (!config_get("resistance", Config_Integer, &resist) ||
41         resist.integer < 0) {
42         resist.integer = DEFAULT_RESISTANCE;
43         config_set("resistance", Config_Integer, resist);
44     }
45     if (!config_get("resistance.windows", Config_Bool, &window_resist))
46         g_assert_not_reached();
47
48     w = c->frame->area.width;
49     h = c->frame->area.height;
50
51     l = *x;
52     t = *y;
53     r = l + w - 1;
54     b = t + h - 1;
55
56     cl = c->frame->area.x;
57     ct = c->frame->area.y;
58     cr = cl + c->frame->area.width - 1;
59     cb = ct + c->frame->area.height - 1;
60     
61     /* snap to other clients */
62     if (window_resist.bool)
63         for (it = stacking_list; it != NULL; it = it->next) {
64             Client *target;
65             int tl, tt, tr, tb; /* 1 past the target's edges on each side */
66
67             target = it->data;
68             /* don't snap to self or non-visibles */
69             if (!target->frame->visible || target == c) continue; 
70
71             tl = target->frame->area.x - 1;
72             tt = target->frame->area.y - 1;
73             tr = tl + target->frame->area.width + 1;
74             tb = tt + target->frame->area.height + 1;
75
76             /* snapx and snapy ensure that the window snaps to the top-most
77                window edge available, without going all the way from
78                bottom-to-top in the stacking list
79             */
80             if (!snapx && cl >= tr && l < tr && l >= tr - resist.integer)
81                 *x = tr, snapx = TRUE;
82             else if (!snapx && cr <= tl && r > tl && r <= tl + resist.integer)
83                 *x = tl - w + 1, snapx = TRUE;
84             else if (!snapy && ct >= tb && t < tb && t >= tb - resist.integer)
85                 *y = tb, snapy = TRUE;
86             else if (!snapy && cb <= tt && b > tt && b <= tt + resist.integer)
87                 *y = tt - h + 1, snapy = TRUE;
88
89             if (snapx && snapy) break;
90         }
91
92     /* get the screen boundaries */
93     area = screen_area(c->desktop);
94     al = area->x;
95     at = area->y;
96     ar = al + area->width - 1;
97     ab = at + area->height - 1;
98
99     /* snap to screen edges */
100     if (cl >= al && l < al && l >= al - resist.integer)
101         *x = al;
102     else if (cr <= ar && r > ar && r <= ar + resist.integer)
103             *x = ar - w + 1;
104     if (ct >= at && t < at && t >= at - resist.integer)
105         *y = at;
106     else if (cb <= ab && b > ab && b < ab + resist.integer)
107         *y = ab - h + 1;
108 }
109
110 static void event(ObEvent *e, void *foo)
111 {
112     g_assert(e->type == Event_Client_Moving);
113
114     resist(e->data.c.client, &e->data.c.num[0], &e->data.c.num[1]);
115 }
116
117 void plugin_startup()
118 {
119     dispatch_register(Event_Client_Moving, (EventHandler)event, NULL);
120 }
121
122 void plugin_shutdown()
123 {
124     dispatch_register(0, (EventHandler)event, NULL);
125 }