]> Dogcows Code - chaz/openbox/blob - plugins/placement/placement.c
remove the event dispatcher
[chaz/openbox] / plugins / placement / placement.c
1 #include "kernel/dispatch.h"
2 #include "kernel/client.h"
3 #include "kernel/group.h"
4 #include "kernel/frame.h"
5 #include "kernel/screen.h"
6 #include "kernel/openbox.h"
7 #include "parser/parse.h"
8 #include "history.h"
9 #include <glib.h>
10
11 static gboolean history;
12
13 static void parse_xml(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, void *d)
14 {
15 xmlNodePtr n;
16
17 node = node->xmlChildrenNode;
18 if ((n = parse_find_node("remember", node)))
19 history = parse_bool(doc, n);
20 }
21
22 void plugin_setup_config(ObParseInst *i)
23 {
24 history = TRUE;
25
26 parse_register(i, "placement", parse_xml, NULL);
27 }
28
29 static Rect* pick_head(ObClient *c)
30 {
31 /* try direct parent first */
32 if (c->transient_for && c->transient_for != OB_TRAN_GROUP) {
33 return screen_area_monitor(c->desktop,
34 client_monitor(c->transient_for));
35 }
36
37 /* more than one guy in his group (more than just him) */
38 if (c->group && c->group->members->next) {
39 GSList *it;
40
41 /* try on the client's desktop */
42 for (it = c->group->members; it; it = g_slist_next(it)) {
43 ObClient *itc = it->data;
44 if (itc != c &&
45 (itc->desktop == c->desktop ||
46 itc->desktop == DESKTOP_ALL || c->desktop == DESKTOP_ALL))
47 return screen_area_monitor(c->desktop,
48 client_monitor(it->data));
49 }
50
51 /* try on all desktops */
52 for (it = c->group->members; it; it = g_slist_next(it)) {
53 ObClient *itc = it->data;
54 if (itc != c)
55 return screen_area_monitor(c->desktop,
56 client_monitor(it->data));
57 }
58 }
59
60 return NULL;
61 }
62
63 static void place_random(ObClient *c)
64 {
65 int l, r, t, b;
66 int x, y;
67 Rect *area;
68
69 if (ob_state() == OB_STATE_STARTING) return;
70
71 area = pick_head(c);
72 if (!area)
73 area = screen_area_monitor(c->desktop,
74 g_random_int_range(0, screen_num_monitors));
75
76 l = area->x;
77 t = area->y;
78 r = area->x + area->width - c->frame->area.width;
79 b = area->y + area->height - c->frame->area.height;
80
81 if (r > l) x = g_random_int_range(l, r + 1);
82 else x = 0;
83 if (b > t) y = g_random_int_range(t, b + 1);
84 else y = 0;
85
86 frame_frame_gravity(c->frame, &x, &y); /* get where the client should be */
87 client_configure(c, OB_CORNER_TOPLEFT, x, y, c->area.width, c->area.height,
88 TRUE, TRUE);
89 }
90
91 static void event(ObEvent *e, void *foo)
92 {
93 g_assert(e->type == Event_Client_New);
94
95 /* requested a position */
96 if (e->data.c.client->positioned) return;
97
98 if (e->data.c.client->transient_for) {
99 if (e->data.c.client->transient_for != OB_TRAN_GROUP) {
100 ObClient *c = e->data.c.client;
101 ObClient *p = e->data.c.client->transient_for;
102 int x = (p->frame->area.width - c->frame->area.width) / 2 +
103 p->frame->area.x;
104 int y = (p->frame->area.height - c->frame->area.height) / 2 +
105 p->frame->area.y;
106 client_configure(c, OB_CORNER_TOPLEFT, x, y,
107 c->area.width, c->area.height,
108 TRUE, TRUE);
109 return;
110 } else {
111 GSList *it;
112 ObClient *c = e->data.c.client;
113 gboolean first = TRUE;
114 int l, r, t, b;
115 for (it = c->group->members; it; it = it->next) {
116 ObClient *m = it->data;
117 if (!(m == c || m->transient_for)) {
118 if (first) {
119 l = m->frame->area.x;
120 t = m->frame->area.y;
121 r = m->frame->area.x + m->frame->area.width - 1;
122 b = m->frame->area.y + m->frame->area.height - 1;
123 first = FALSE;
124 } else {
125 l = MIN(l, m->frame->area.x);
126 t = MIN(t, m->frame->area.y);
127 r = MAX(r, m->frame->area.x +m->frame->area.width - 1);
128 b = MAX(b, m->frame->area.y +m->frame->area.height -1);
129 }
130 }
131 }
132 if (!first) {
133 int x = ((r + 1 - l) - c->frame->area.width) / 2 + l;
134 int y = ((b + 1 - t) - c->frame->area.height) / 2 + t;
135 client_configure(c, OB_CORNER_TOPLEFT, x, y,
136 c->area.width, c->area.height,
137 TRUE, TRUE);
138 return;
139 }
140 }
141 }
142
143 /* center parentless dialogs on the screen */
144 if (e->data.c.client->type == OB_CLIENT_TYPE_DIALOG) {
145 Rect *area;
146 ObClient *c = e->data.c.client;
147 int x, y;
148
149 area = pick_head(c);
150 if (!area)
151 area = screen_area_monitor(c->desktop, 0);
152
153 x = (area->width - c->frame->area.width) / 2 + area->x;
154 y = (area->height - c->frame->area.height) / 2 + area->y;
155 client_configure(c, OB_CORNER_TOPLEFT, x, y,
156 c->area.width, c->area.height,
157 TRUE, TRUE);
158 return;
159 }
160
161 if (!history || !place_history(e->data.c.client))
162 place_random(e->data.c.client);
163 }
164
165 void plugin_startup()
166 {
167 dispatch_register(Event_Client_New, (EventHandler)event, NULL);
168
169 history_startup();
170 }
171
172 void plugin_shutdown()
173 {
174 dispatch_register(0, (EventHandler)event, NULL);
175
176 history_shutdown();
177 }
This page took 0.03925 seconds and 4 git commands to generate.