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