]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/placement/placement.c
needs the libsn flags to use openbox.h now
[mikachu/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/parse.h"
7 #include "history.h"
8 #include <glib.h>
9
10 static gboolean history;
11
12 static void parse_assign(char *name, ParseToken *value)
13 {
14     if  (!g_ascii_strcasecmp(name, "remember")) {
15         if (value->type != TOKEN_BOOL)
16             yyerror("invalid value");
17         else
18             history = value->data.bool;
19     } else
20         yyerror("invalid option");
21     parse_free_token(value);
22 }
23
24 void plugin_setup_config()
25 {
26     history = TRUE;
27
28     parse_reg_section("placement", NULL, parse_assign);
29 }
30
31 static void place_random(Client *c)
32 {
33     int l, r, t, b;
34     int x, y;
35     Rect *area;
36
37     if (ob_state == State_Starting) return;
38
39     area = screen_area(c->desktop);
40
41     l = area->x;
42     t = area->y;
43     r = area->x + area->width - c->frame->area.width;
44     b = area->y + area->height - c->frame->area.height;
45
46     if (r > l) x = g_random_int_range(l, r + 1);
47     else       x = 0;
48     if (b > t) y = g_random_int_range(t, b + 1);
49     else       y = 0;
50
51     frame_frame_gravity(c->frame, &x, &y); /* get where the client should be */
52     client_configure(c, Corner_TopLeft, x, y, c->area.width, c->area.height,
53                      TRUE, TRUE);
54 }
55
56 static void event(ObEvent *e, void *foo)
57 {
58     g_assert(e->type == Event_Client_New);
59
60     /* requested a position */
61     if (e->data.c.client->positioned) return;
62
63     if (!history || !place_history(e->data.c.client))
64         place_random(e->data.c.client);
65 }
66
67 void plugin_startup()
68 {
69     dispatch_register(Event_Client_New, (EventHandler)event, NULL);
70
71     history_startup();
72 }
73
74 void plugin_shutdown()
75 {
76     dispatch_register(0, (EventHandler)event, NULL);
77
78     history_shutdown();
79 }