make history placement optional
[dana/openbox.git] / plugins / placement / placement.c
1 #include "../../kernel/dispatch.h"
2 #include "../../kernel/client.h"
3 #include "../../kernel/frame.h"
4 #include "../../kernel/screen.h"
5 #include "../../kernel/openbox.h"
6 #include "../../kernel/config.h"
7 #include "history.h"
8 #include <glib.h>
9
10 gboolean history = TRUE;
11
12 void plugin_setup_config()
13 {
14     ConfigValue val;
15
16     config_def_set(config_def_new("placement.remember", Config_Bool,
17                                   "Remember Window Positions",
18                                   "Place windows where they last were "
19                                   "positioned."));
20     val.bool = TRUE;
21     config_set("placement.remember", Config_Bool, val);
22 }
23
24 static void place_random(Client *c)
25 {
26     int l, r, t, b;
27     int x, y;
28     Rect *area;
29
30     if (ob_state == State_Starting) return;
31
32     area = screen_area(c->desktop);
33
34     l = area->x;
35     t = area->y;
36     r = area->x + area->width - c->frame->area.width;
37     b = area->y + area->height - c->frame->area.height;
38
39     if (r > l) x = g_random_int_range(l, r + 1);
40     else       x = 0;
41     if (b > t) y = g_random_int_range(t, b + 1);
42     else       y = 0;
43
44     frame_frame_gravity(c->frame, &x, &y); /* get where the client should be */
45     client_configure(c, Corner_TopLeft, x, y, c->area.width, c->area.height,
46                      TRUE, TRUE);
47 }
48
49 static void event(ObEvent *e, void *foo)
50 {
51     ConfigValue remember;
52     gboolean r;
53
54     g_assert(e->type == Event_Client_New);
55
56     /* requested a position */
57     if (e->data.c.client->positioned) return;
58
59     r = config_get("placement.remember", Config_Bool, &remember);
60     g_assert(r);
61
62     if (!remember.bool || !place_history(e->data.c.client))
63         place_random(e->data.c.client);
64 }
65
66 void plugin_startup()
67 {
68     dispatch_register(Event_Client_New, (EventHandler)event, NULL);
69
70     history_startup();
71 }
72
73 void plugin_shutdown()
74 {
75     dispatch_register(0, (EventHandler)event, NULL);
76
77     history_shutdown();
78 }