My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "CtrlCore.h"

#ifdef GUI_X11

NAMESPACE_UPP

#define LLOG(x) // DLOG(x)

void TopWindow::SyncSizeHints()
{
GuiLock __;
Size min = GetMinSize();
Size max = GetMaxSize();
if(!sizeable)
min = max = GetRect().Size();
Window w = GetWindow();
if(w && (min != xminsize || max != xmaxsize)) {
xminsize = min;
xmaxsize = max;
size_hints->min_width = min.cx;
size_hints->min_height = min.cy;
size_hints->max_width = max.cx;
size_hints->max_height = max.cy;
size_hints->flags = PMinSize|PMaxSize;
XSetWMNormalHints(Xdisplay, w, size_hints);
}
}

void TopWindow::EndIgnoreTakeFocus()
{
GuiLock __;
ignoretakefocus = false;
}

void TopWindow::EventProc(XWindow& w, XEvent *event)
{
GuiLock __;
Ptr<Ctrl> this_ = this;
if(event->type == ClientMessage) {
if(event->xclient.format == 32 && event->xclient.message_type)
if(event->xclient.message_type == XAtom("WM_PROTOCOLS")) {
Atom a = event->xclient.data.l[0];
if(a == XAtom("WM_DELETE_WINDOW") && IsEnabled()) {
LLOG("DELETE_WINDOW " << Name());
WhenClose();
return;
}
if(a == XAtom("WM_TAKE_FOCUS")) {
LLOG("TAKE_FOCUS serial: " << event->xclient.serial);
Xeventtime = event->xclient.data.l[1];
TakeFocus();
return;
}
if(a == XAtom("_NET_WM_PING")) {
XEvent ev = *event;
ev.xclient.window = Xroot;
XSendEvent(Xdisplay, Xroot, 0, SubstructureRedirectMask|SubstructureNotifyMask, &ev);
return;
}
LLOG("Unknown WM_PROTOCOLS: " << XAtomName(a));
}
}
else
if(event->type == PropertyNotify && event->xproperty.atom == XAtom("_NET_WM_STATE")) {
LLOG("_NET_WM_STATE notify");
Vector<int> p = GetPropertyInts(GetWindow(), XAtom("_NET_WM_STATE"));
if(FindIndex(p, (int)XAtom("_NET_WM_STATE_HIDDEN")) >= 0) {
state = MINIMIZED;
LLOG("MINIMIZED");
}
else
if(FindIndex(p, (int)XAtom("_NET_WM_STATE_MAXIMIZED_HORZ")) >= 0 &&
FindIndex(p, (int)XAtom("_NET_WM_STATE_MAXIMIZED_VERT")) >= 0) {
state = MAXIMIZED;
LLOG("MAXIMIZED");
}
else {
state = OVERLAPPED;
LLOG("OVERLAPPED");
}
}
if(this_) Ctrl::EventProc(w, event);
if(this_) SyncSizeHints();
}

void TopWindow::DefSyncTitle()
{
GuiLock __;
if(title2 == title)
return;
title2 = title;
if(IsOpen() && GetWindow()) {
Window w = GetWindow();
XStoreName(Xdisplay, w, title.ToString());
XSetIconName(Xdisplay, w, title.ToString());
String utf8title = FromUnicode(title, CHARSET_UTF8);
XChangeProperty(Xdisplay, w, XAtom("_NET_WM_NAME"), XAtom("UTF8_STRING"),
8, PropModeReplace,
(const unsigned char *)~utf8title, utf8title.GetLength());
XChangeProperty(Xdisplay, w, XAtom("_NET_WM_ICON_NAME"), XAtom("UTF8_STRING"),
8, PropModeReplace,
(const unsigned char *)~utf8title, utf8title.GetLength());
}
}

void TopWindow::SyncTitle0()
{
GuiLock __;
LLOG("SyncTitle: " << title);
KillTimeCallback(TIMEID_DEFSYNCTITLE);
SetTimeCallback(0, THISBACK(DefSyncTitle), TIMEID_DEFSYNCTITLE);
LLOG("*SyncTitle: " << title);
}

void WmState(Window w, bool set, Atom a1, Atom a2 = 0)
{
XEvent e;
memset(&e, 0, sizeof(e));
e.xclient.type = ClientMessage;
e.xclient.message_type = XAtom("_NET_WM_STATE");
e.xclient.display = Xdisplay;
e.xclient.window = w;
e.xclient.format = 32;
e.xclient.data.l[0] = set;
e.xclient.data.l[1] = a1;
e.xclient.data.l[2] = a2;
XSendEvent(Xdisplay, Xroot, false, SubstructureNotifyMask | SubstructureRedirectMask, &e);
}

void TopWindow::SyncState0()
{
GuiLock __;
LLOG("SyncState");
SyncCaption();
if(IsOpen() && GetWindow()) {
Window w = GetWindow();
WmState(w, topmost, XAtom("_NET_WM_STATE_ABOVE"));
WmState(w, state == MAXIMIZED, XAtom("_NET_WM_STATE_MAXIMIZED_HORZ"), XAtom("_NET_WM_STATE_MAXIMIZED_VERT"));
if(state == MINIMIZED)
XIconifyWindow(Xdisplay, GetWindow(), Xscreenno);
else
XMapWindow(Xdisplay, GetWindow());

WmState(w, state == MINIMIZED, XAtom("_NET_WM_STATE_HIDDEN"));
WmState(w, fullscreen, XAtom("_NET_WM_STATE_FULLSCREEN"));
}
}

void TopWindow::SyncState()
{
ICall(THISBACK(SyncState0));
}

void TopWindow::SyncCaption0()
{
GuiLock __;
LLOG("SyncCaption");
SyncTitle();
if(IsOpen() && GetWindow()) {
unsigned long wina[6];
memset(wina, 0, sizeof(wina));
int n = 0;
Window w = GetWindow();
if(tool)
wina[n++] = XAtom("_NET_WM_WINDOW_TYPE_UTILITY");
if(GetOwner())
wina[n++] = XAtom("_NET_WM_WINDOW_TYPE_DIALOG");
wina[n++] = XAtom("_NET_WM_WINDOW_TYPE_NORMAL");
XChangeProperty(Xdisplay, GetWindow(), XAtom("_NET_WM_WINDOW_TYPE"), XAtom("ATOM"), 32,
PropModeReplace, (const unsigned char *)wina, n);

// n = 0;
// XChangeProperty(Xdisplay, GetWindow(), XAtom("_NET_WM_STATE"), XAtom("ATOM"), 32,
// PropModeReplace, (const unsigned char *)wina, n);
wm_hints->flags = InputHint|WindowGroupHint|StateHint;
if(urgent) {
if(IsForeground()) urgent = false;
wm_hints->flags |= XUrgencyHint;
WmState(w, urgent, XAtom("_NET_WM_STATE_DEMANDS_ATTENTION"));
}
wm_hints->initial_state = NormalState;
wm_hints->input = XTrue;
Ctrl *owner = GetOwner();
wm_hints->window_group = owner ? owner->GetWindow() : w;
if(!icon.IsEmpty()) {
Size isz = icon.GetSize();
int len = 2 + isz.cx * isz.cy;
Buffer<unsigned long> data(len);
unsigned long *t = data;
*t++ = isz.cx;
*t++ = isz.cy;
for(int y = 0; y < isz.cy; y++) {
const RGBA *q = icon[y];
for(int x = isz.cx; x--;) {
*t++ = ((dword)q->a << 24) |
(dword)q->b | ((dword)q->g << 8) | ((dword)q->r << 16);
q++;
}
}
XChangeProperty(Xdisplay, w, XAtom("_NET_WM_ICON"), XA_CARDINAL, 32, PropModeReplace,
(const unsigned char *)~data, len);
}
XSetWMHints(Xdisplay, w, wm_hints);
}
}

void TopWindow::CenterRect(Ctrl *owner)
{
GuiLock __;
SetupRect();
if(owner && center == 1 || center == 2) {
Size sz = GetRect().Size();
Rect r, wr;
wr = Ctrl::GetWorkArea();
GuiLock __;
Rect fm = windowFrameMargin;
if((fm.left|fm.right|fm.top|fm.bottom) == 0)
fm = Rect(8, 32, 8, 8);
if(center == 1)
r = owner->GetRect();
else
r = wr;
Point p = r.CenterPos(sz);
r = RectC(p.x, p.y, sz.cx, sz.cy);
wr.left += fm.left;
wr.right -= fm.right;
wr.top += fm.top;
wr.bottom -= fm.bottom;
if(r.top < wr.top) {
r.bottom += wr.top - r.top;
r.top = wr.top;
}
if(r.bottom > wr.bottom)
r.bottom = wr.bottom;
minsize.cx = min(minsize.cx, r.GetWidth());
minsize.cy = min(minsize.cy, r.GetHeight());
SetRect(r);
}
}

void TopWindow::Open(Ctrl *owner)
{
GuiLock __;
if(dokeys && (!GUI_AKD_Conservative() || GetAccessKeysDeep() <= 1))
DistributeAccessKeys();
UsrLogT(3, "OPEN " + Desc(this));
LLOG("OPEN " << Name() << " owner: " << UPP::Name(owner));
IgnoreMouseUp();
bool weplace = owner && center == 1 || center == 2 || !GetRect().IsEmpty();
if(fullscreen)
SetRect(0, 0, Xwidth, Xheight);
else
CenterRect(owner);
LLOG("Open NextRequest " << NextRequest(Xdisplay));
Create(owner, false, false);
XSetWMProperties (Xdisplay, GetWindow(), NULL, NULL, NULL, 0, NULL, NULL, NULL);
xminsize.cx = xmaxsize.cx = Null;
title2.Clear();
if(!weplace) {
LLOG("SyncCaption");
SyncCaption0();
}
LLOG("SyncSizeHints");
size_hints->flags = 0;
SyncSizeHints();
Rect r = GetRect();
size_hints->x = r.left;
size_hints->y = r.top;
size_hints->width = r.Width();
size_hints->height = r.Height();
size_hints->win_gravity = StaticGravity;
size_hints->flags |= PPosition|PSize|PWinGravity;
if(owner) {
ASSERT(owner->IsOpen());
LLOG("XSetTransientForHint");
XSetTransientForHint(Xdisplay, GetWindow(), owner->GetWindow());
}
LLOG("XSetWMNormalHints");
XSetWMNormalHints(Xdisplay, GetWindow(), size_hints);
Atom protocols[3];
protocols[0] = XAtom("WM_DELETE_WINDOW");
protocols[1] = XAtom("WM_TAKE_FOCUS");
protocols[2] = XAtom("_NET_WM_PING");
LLOG("XSetWMProtocols");
XSetWMProtocols(Xdisplay, GetWindow(), protocols, 3);
String x = GetExeTitle().ToString();
const char *progname = ~x;
class_hint->res_name = (char *)progname;
class_hint->res_class = (char *)progname;
XSetClassHint(Xdisplay, GetWindow(), class_hint);
LLOG("WndShow(" << visible << ")");
WndShow(visible);
if(visible) {
XEvent e;
LLOG("XWindowEvent");
XWindowEvent(Xdisplay, top->window, VisibilityChangeMask, &e);
ignoretakefocus = true;
SetTimeCallback(500, THISBACK(EndIgnoreTakeFocus));
LLOG("SetWndFocus");
SetWndFocus();
for(int i = 0; i < 50; i++) {
// X11 tries to move our window, so ignore the first set of ConfigureNotify
// and move the window into position after FocusIn - but not if we want WM to
// place the window
if(weplace)
while(XCheckTypedWindowEvent(Xdisplay, top->window, ConfigureNotify, &e)) {
if(e.xconfigure.window != top->window)
ProcessEvent(&e);
}
if(XCheckTypedWindowEvent(Xdisplay, top->window, FocusIn, &e)) {
ProcessEvent(&e);
if(e.xfocus.window == top->window)
break;
}
Sleep(10);
}
}
if(weplace) {
WndSetPos0(GetRect());
LLOG("SyncCaption");
SyncCaption0();
}
LLOG(">Open NextRequest " << NextRequest(Xdisplay));
LLOG(">OPENED " << Name());
PlaceFocus();
StateH(OPEN);
Vector<int> fe = GetPropertyInts(top->window, XAtom("_NET_FRAME_EXTENTS"));
if(fe.GetCount() >= 4 &&
fe[0] >= 0 && fe[0] <= 16 && fe[1] >= 0 && fe[1] <= 16 && //fluxbox returns wrong numbers - quick&dirty workaround
fe[2] >= 0 && fe[2] <= 64 && fe[3] >= 0 && fe[3] <= 48)
{
GuiLock __;
windowFrameMargin.left = max(windowFrameMargin.left, fe[0]);
windowFrameMargin.right = max(windowFrameMargin.right, fe[1]);
windowFrameMargin.top = max(windowFrameMargin.top, fe[2]);
windowFrameMargin.bottom = max(windowFrameMargin.bottom, fe[3]);
}
if(IsOpen() && top)
top->owner = owner;

long curr_pid = getpid();

static Window wm_client_leader;
ONCELOCK {
wm_client_leader = XCreateSimpleWindow(Xdisplay, Xroot, 0, 0, 1, 1, 0, 0, 0);
XChangeProperty(Xdisplay, wm_client_leader, XAtom("WM_CLIENT_LEADER"),
XA_WINDOW, 32, PropModeReplace, (byte *)&wm_client_leader, 1);
XChangeProperty(Xdisplay, wm_client_leader, XAtom("_NET_WM_PID"), XA_CARDINAL, 32,
PropModeReplace, (byte *) &curr_pid, 1);
}

Window win = GetWindow();
XChangeProperty(Xdisplay, win, XAtom("_NET_WM_PID"), XA_CARDINAL, 32,
PropModeReplace, (byte *) &curr_pid, 1);
XChangeProperty(Xdisplay, win, XAtom("WM_CLIENT_LEADER"),
XA_WINDOW, 32, PropModeReplace, (byte *)&wm_client_leader, 1);

int version = 5;
XChangeProperty(Xdisplay, win, XAtom("XdndAware"), XA_ATOM, 32,
0, (byte *)&version, 1);

SyncState0();
FixIcons();
}

void TopWindow::Open()
{
GuiLock __;
Open(GetActiveWindow());
}

void TopWindow::OpenMain()
{
GuiLock __;
Open(NULL);
}

void TopWindow::Minimize(bool)
{
GuiLock __;
state = MINIMIZED;
SyncState();
}

void TopWindow::Maximize(bool effect)
{
GuiLock __;
state = MAXIMIZED;
SyncState();
}

void TopWindow::Overlap(bool effect)
{
GuiLock __;
state = OVERLAPPED;
SyncState();
}

TopWindow& TopWindow::FullScreen(bool b)
{
GuiLock __;
fullscreen = b;
SyncState();
return *this;
}

TopWindow& TopWindow::TopMost(bool b, bool)
{
GuiLock __;
topmost = b;
SyncState();
return *this;
}

bool TopWindow::IsTopMost() const
{
GuiLock __;
return topmost;
}

void TopWindow::GuiPlatformConstruct()
{
size_hints = XAllocSizeHints();
wm_hints = XAllocWMHints();
class_hint = XAllocClassHint();
topmost = false;
}

void TopWindow::GuiPlatformDestruct()
{
XFree(size_hints);
XFree(wm_hints);
XFree(class_hint);
}

void TopWindow::SerializePlacement(Stream& s, bool reminimize)
{
GuiLock __;
int version = 0;
s / version;
Rect rect = GetRect();
s % overlapped % rect;
bool mn = state == MINIMIZED;
bool mx = state == MAXIMIZED;
s.Pack(mn, mx);
LLOG("TopWindow::SerializePlacement / " << (s.IsStoring() ? "write" : "read"));
LLOG("minimized = " << mn << ", maximized = " << mx);
LLOG("rect = " << rect << ", overlapped = " << overlapped);
if(s.IsLoading()) {
if(mn) rect = overlapped;
Rect limit = GetWorkArea();
Rect fm = windowFrameMargin;
if((fm.left|fm.right|fm.top|fm.bottom) == 0)
fm = Rect(8, 32, 8, 8);
limit.left += fm.left;
limit.right -= fm.right;
limit.top += fm.top;
limit.bottom -= fm.bottom;
Size sz = min(rect.Size(), limit.Size());
rect = RectC(
minmax(rect.left, limit.left, limit.right - sz.cx),
minmax(rect.top, limit.top, limit.bottom - sz.cy),
sz.cx, sz.cy);
state = OVERLAPPED;
if(mn && reminimize)
state = MINIMIZED;
if(mx)
state = MAXIMIZED;
if(state == OVERLAPPED)
SetRect(rect);
if(IsOpen()) {
if(state == MINIMIZED)
Minimize(false);
if(state == MAXIMIZED)
Maximize(false);
}
}
}

END_UPP_NAMESPACE

#endif

Change log

r4457 by cxl on Jan 21, 2012   Diff
*uppsrc: Fixed many GCC warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r3869 by cxl on Sep 17, 2011   Diff
developing WM_PID support
r3819 by cxl on Sep 4, 2011   Diff
CtrlCore: more WM hints...
r3818 by cxl on Sep 4, 2011   Diff
CtrlCore: X11 now supports _NET_WM_PID
and _NET_WM_PING protocols (means now
able to kill hung X11 process by close
button)
All revisions of this file

File info

Size: 12887 bytes, 482 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting