/
window.cpp
1672 lines (1412 loc) · 46.9 KB
/
window.cpp
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file window.cpp Window manager.
*
* Window manager that manages a QWidget-based window. @ingroup base
*
* The Doomsday window management is responsible for the positioning, sizing,
* and state of the game's native windows. In practice, the code operates on Qt
* top-level windows.
*
* At the moment, the quality of the code is adequate at best. See the todo
* notes below for ideas for future improvements.
*
* @todo Instead of 'rect' and 'normalRect', the window should have a
* 'fullscreenSize' and a 'normalRect'. It isn't ideal that when toggling
* between fullscreen and windowed mode, the fullscreen resolution is chosen
* based on the size of the normal-mode window.
*
* @todo It is not a good idea to duplicate window state locally (position,
* size, flags). Much of the complexity here is due to this duplication, trying
* to keep all the state consistent. Instead, the real QWidget should be used
* for these properties. Qt has a mechanism for storing the state of a window:
* QWidget::saveGeometry(), QMainWindow::saveState().
*
* @todo Refactor for multiple window support. One window should be the "main"
* window while others are secondary windows.
*
* @todo Deferred window changes should be done using a queue-type solution
* where it is possible to schedule multiple tasks into the future separately
* for each window. Each window could have its own queue.
*
* @todo Platform-specific behavior should be encapsulated in subclasses, e.g.,
* MacWindowBehavior. This would make the code easier to follow and more adaptable
* to the quirks of each platform.
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2008 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QWidget>
#include <QApplication>
#include <QPaintEvent>
#include <QDesktopWidget>
#include <QDebug>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <de/App>
#include <de/Config>
#include <de/Record>
#include <de/NumberValue>
#include <de/ArrayValue>
#ifdef MACOSX
# include "ui/displaymode_native.h"
#endif
#include "de_platform.h"
#include "ui/window.h"
#include "ui/consolewindow.h"
#include "ui/canvaswindow.h"
#include "ui/displaymode.h"
#include "../updater/downloaddialog.h"
#include "sys_system.h"
#include "busymode.h"
#include "dd_main.h"
#include "con_main.h"
#ifdef __CLIENT__
# include "gl/gl_main.h"
#endif
#include "ui/ui_main.h"
#include "filesys/fs_util.h"
#include <de/c_wrapper.h>
#include <de/Log>
#ifdef MACOSX
static const int WAIT_MILLISECS_AFTER_MODE_CHANGE = 100; // ms
#else
static const int WAIT_MILLISECS_AFTER_MODE_CHANGE = 10; // ms
#endif
/// Used to determine the valid region for windows on the desktop.
/// A window should never go fully (or nearly fully) outside the desktop.
static const int DESKTOP_EDGE_GRACE = 30; // pixels
static QRect desktopRect()
{
/// @todo Multimonitor? This checks the default screen.
return QApplication::desktop()->screenGeometry();
}
/*
static QRect desktopValidRect()
{
return desktopRect().adjusted(DESKTOP_EDGE_GRACE, DESKTOP_EDGE_GRACE,
-DESKTOP_EDGE_GRACE, -DESKTOP_EDGE_GRACE);
}
*/
static void updateMainWindowLayout(void);
static void updateWindowStateAfterUserChange(void);
static void useAppliedGeometryForWindows(void);
static void notifyAboutModeChange(void);
static void endWindowWait(void);
struct ddwindow_s
{
CanvasWindow* widget; ///< The widget this window represents.
void (*drawFunc)(void); ///< Draws the contents of the canvas.
QRect appliedGeometry; ///< Saved for detecting when changes have occurred.
bool needShowFullscreen;
bool needReshowFullscreen;
bool needShowNormal;
bool needRecreateCanvas;
bool needWait;
bool willUpdateWindowState;
ddwindowtype_t type;
boolean inited;
RectRaw geometry; ///< Current actual geometry.
RectRaw normalGeometry; ///< Normal-mode geometry (when not maximized or fullscreen).
int colorDepthBits;
int flags;
consolewindow_t console; ///< Only used for WT_CONSOLE windows.
void __inline assertWindow() const
{
assert(this);
assert(widget);
}
bool isBeingAdjusted() const {
return needShowFullscreen || needReshowFullscreen || needShowNormal || needRecreateCanvas || needWait;
}
int x() const { return geometry.origin.x; }
int y() const { return geometry.origin.y; }
int width() const { return geometry.size.width; }
int height() const { return geometry.size.height; }
QRect rect() const { return QRect(x(), y(), width(), height()); }
QRect normalRect() const { return QRect(normalGeometry.origin.x,
normalGeometry.origin.y,
normalGeometry.size.width,
normalGeometry.size.height); }
bool checkFlag(int flag) const { return (flags & flag) != 0; }
/**
* Checks all command line options that affect window geometry and applies
* them to this Window.
*/
void modifyAccordingToOptions()
{
if(CommandLine_Exists("-nofullscreen") || CommandLine_Exists("-window"))
{
setFlag(DDWF_FULLSCREEN, false);
}
if(CommandLine_Exists("-fullscreen") || CommandLine_Exists("-nowindow"))
{
setFlag(DDWF_FULLSCREEN);
}
if(CommandLine_CheckWith("-width", 1))
{
geometry.size.width = qMax(WINDOW_MIN_WIDTH, atoi(CommandLine_Next()));
if(!(flags & DDWF_FULLSCREEN))
{
normalGeometry.size.width = geometry.size.width;
}
}
if(CommandLine_CheckWith("-height", 1))
{
geometry.size.height = qMax(WINDOW_MIN_HEIGHT, atoi(CommandLine_Next()));
if(!(flags & DDWF_FULLSCREEN))
{
normalGeometry.size.height = geometry.size.height;
}
}
if(CommandLine_CheckWith("-winsize", 2))
{
geometry.size.width = qMax(WINDOW_MIN_WIDTH, atoi(CommandLine_Next()));
geometry.size.height = qMax(WINDOW_MIN_HEIGHT, atoi(CommandLine_Next()));
if(!(flags & DDWF_FULLSCREEN))
{
normalGeometry.size.width = geometry.size.width;
normalGeometry.size.height = geometry.size.height;
}
}
if(CommandLine_CheckWith("-colordepth", 1) || CommandLine_CheckWith("-bpp", 1))
{
colorDepthBits = qBound(8, atoi(CommandLine_Next()), 32);
}
if(CommandLine_Check("-nocenter"))
{
setFlag(DDWF_CENTER, false);
}
if(CommandLine_CheckWith("-xpos", 1))
{
normalGeometry.origin.x = atoi(CommandLine_Next());
setFlag(DDWF_CENTER | DDWF_MAXIMIZE, false);
}
if(CommandLine_CheckWith("-ypos", 1))
{
normalGeometry.origin.y = atoi(CommandLine_Next());
setFlag(DDWF_CENTER | DDWF_MAXIMIZE, false);
}
if(CommandLine_Check("-center"))
{
setFlag(DDWF_CENTER);
}
if(CommandLine_Check("-maximize"))
{
setFlag(DDWF_MAXIMIZE);
}
if(CommandLine_Check("-nomaximize"))
{
setFlag(DDWF_MAXIMIZE, false);
}
}
bool applyDisplayMode()
{
assertWindow();
if(!DisplayMode_Count()) return true; // No modes to change to.
if(flags & DDWF_FULLSCREEN)
{
const DisplayMode* mode = DisplayMode_FindClosest(width(), height(), colorDepthBits, 0);
if(mode && DisplayMode_Change(mode, true /* fullscreen: capture */))
{
geometry.size.width = DisplayMode_Current()->width;
geometry.size.height = DisplayMode_Current()->height;
#if defined MACOSX && defined __CLIENT__
// Pull the window again over the shield after the mode change.
DisplayMode_Native_Raise(Window_NativeHandle(this));
#endif
Window_TrapMouse(this, true);
return true;
}
}
else
{
return DisplayMode_Change(DisplayMode_OriginalMode(),
false /* windowed: don't capture */);
}
return false;
}
QRect centeredGeometry() const
{
QSize winSize = normalRect().size();
// Center the window.
QSize screenSize = desktopRect().size();
LOG_DEBUG("centeredGeometry: Current desktop rect %ix%i") << screenSize.width() << screenSize.height();
return QRect(desktopRect().topLeft() +
QPoint((screenSize.width() - winSize.width()) / 2,
(screenSize.height() - winSize.height()) / 2),
winSize);
}
/**
* Applies the information stored in the Window instance to the actual
* widget geometry. Centering is applied in this stage (it only affects the
* widget's geometry).
*/
void applyWindowGeometry()
{
LOG_AS("applyWindowGeometry");
assertWindow();
// While we're adjusting the window, the window move/resizing callbacks
// should've mess with the geometry values.
needWait = true;
LegacyCore_Timer(WAIT_MILLISECS_AFTER_MODE_CHANGE * 20, endWindowWait);
bool modeChanged = applyDisplayMode();
if(modeChanged)
{
// Others might be interested to hear about the mode change.
LegacyCore_Timer(WAIT_MILLISECS_AFTER_MODE_CHANGE, notifyAboutModeChange);
}
/*
* The following is a bit convoluted. The core idea is this, though: on
* some platforms, changes to the window's mode (normal, maximized,
* fullscreen/frameless) do not occur immediately. Instead, control
* needs to return to the event loop and the native window events need
* to play out. Thus some of the operations have to be performed in a
* deferred way, after a short wait. The ideal would be to listen to
* the native events and trigger the necessary updates after they
* occur; however, now we just use naive time-based delays.
*/
if(flags & DDWF_FULLSCREEN)
{
LOG_DEBUG("fullscreen mode (mode changed? %b)") << modeChanged;
if(!modeChanged) return; // We don't need to do anything.
if(widget->isVisible())
{
needShowFullscreen = !widget->isFullScreen();
#if defined(WIN32) || defined(Q_WS_X11)
if(widget->isFullScreen())
{
needShowFullscreen = false;
needReshowFullscreen = true;
}
#endif
LOG_DEBUG("widget is visible, need showFS:%b reshowFS:%b")
<< needShowFullscreen << needReshowFullscreen;
#ifdef MACOSX
// Kludge! See updateMainWindowLayout().
appliedGeometry = QRect(0, 0,
DisplayMode_Current()->width,
DisplayMode_Current()->height);
#endif
// The window is already visible, so let's allow a mode change to resolve itself
// before we go changing the window.
LegacyCore_Timer(WAIT_MILLISECS_AFTER_MODE_CHANGE, updateMainWindowLayout);
}
else
{
LOG_DEBUG("widget is not visible, setting geometry to ")
<< DisplayMode_Current()->width << "x"
<< DisplayMode_Current()->height;
widget->setGeometry(0, 0, DisplayMode_Current()->width, DisplayMode_Current()->height);
}
}
else
{
// The window is in windowed mode (frames and window decoration visible).
// We will restore it to its previous position and size.
QRect geom = normalRect(); // Previously stored normal geometry.
if(flags & DDWF_CENTER)
{
// Center the window.
geom = centeredGeometry();
}
if(flags & DDWF_MAXIMIZE)
{
// When a window is maximized, we'll let the native WM handle the sizing.
if(widget->isVisible())
{
LOG_DEBUG("Window maximized.");
widget->showMaximized();
}
// Non-visible windows will be shown later
// (as maximized, if the flag is set).
}
else
{
// The window is in normal mode: not maximized or fullscreen.
// If the window is already visible, changes to it need to be deferred
// so that the native counterpart can be updated, too
if(widget->isVisible() && (modeChanged || widget->isMaximized()))
{
if(modeChanged)
{
// We'll wait before the mode change takes full effect.
needShowNormal = true;
LegacyCore_Timer(WAIT_MILLISECS_AFTER_MODE_CHANGE, updateMainWindowLayout);
}
else
{
// Display mode was not changed, so we can immediately
// change the window state.
widget->showNormal();
}
}
appliedGeometry = geom;
if(widget->isVisible())
{
// The native window may not be ready to receive the updated geometry
// (e.g., window decoration not made visible yet). We'll apply the
// geometry after a delay.
LegacyCore_Timer(50 + WAIT_MILLISECS_AFTER_MODE_CHANGE, useAppliedGeometryForWindows);
}
else
{
// The native window is not visible yet, so we can apply any number
// of changes we like.
widget->setGeometry(geom);
}
}
}
}
/**
* Retrieves the actual widget geometry and updates the information stored
* in the Window instance.
*/
void fetchWindowGeometry()
{
assertWindow();
setFlag(DDWF_MAXIMIZE, widget->isMaximized());
QRect rect = widget->geometry();
geometry.origin.x = rect.x();
geometry.origin.y = rect.y();
geometry.size.width = rect.width();
geometry.size.height = rect.height();
// If the window is presently maximized or fullscreen, we will not
// store the actual coordinates.
if(!widget->isMaximized() && !(flags & DDWF_FULLSCREEN) && !isBeingAdjusted())
{
normalGeometry.origin.x = rect.x();
normalGeometry.origin.y = rect.y();
normalGeometry.size.width = rect.width();
DEBUG_Message(("ngw=%i [A]\n", normalGeometry.size.width));
normalGeometry.size.height = rect.height();
}
LOG_DEBUG("Current window geometry: %i,%i %ix%i (max:%b)")
<< geometry.origin.x << geometry.origin.y
<< geometry.size.width << geometry.size.height
<< ((flags & DDWF_MAXIMIZE) != 0);
LOG_DEBUG("Normal window geometry: %i,%i %ix%i")
<< normalGeometry.origin.x << normalGeometry.origin.y
<< normalGeometry.size.width << normalGeometry.size.height;
}
void setFlag(int flag, bool set = true)
{
if(set)
{
flags |= flag;
}
else
{
flags &= ~flag;
if(flag & DDWF_CENTER) LOG_DEBUG("Clearing DDWF_CENTER");
}
}
bool applyAttributes(int* attribs)
{
#ifdef __CLIENT__
LOG_AS("applyAttributes");
bool changed = false;
// Parse the attributes array and check the values.
assert(attribs);
for(int i = 0; attribs[i]; ++i)
{
switch(attribs[i++])
{
case DDWA_X:
if(x() != attribs[i])
{
normalGeometry.origin.x = attribs[i];
changed = true;
}
break;
case DDWA_Y:
if(y() != attribs[i])
{
normalGeometry.origin.y = attribs[i];
changed = true;
}
break;
case DDWA_WIDTH:
if(width() != attribs[i])
{
if(attribs[i] < WINDOW_MIN_WIDTH) return false;
normalGeometry.size.width = geometry.size.width = attribs[i];
DEBUG_Message(("ngw=%i [B]\n", normalGeometry.size.width));
changed = true;
}
break;
case DDWA_HEIGHT:
if(height() != attribs[i])
{
if(attribs[i] < WINDOW_MIN_HEIGHT) return false;
normalGeometry.size.height = geometry.size.height = attribs[i];
changed = true;
}
break;
#define IS_NONZERO(x) ((x) != 0)
case DDWA_CENTER:
if(IS_NONZERO(attribs[i]) != IS_NONZERO(checkFlag(DDWF_CENTER)))
{
setFlag(DDWF_CENTER, attribs[i]);
changed = true;
}
break;
case DDWA_MAXIMIZE:
if(IS_NONZERO(attribs[i]) != IS_NONZERO(checkFlag(DDWF_MAXIMIZE)))
{
setFlag(DDWF_MAXIMIZE, attribs[i]);
changed = true;
}
break;
case DDWA_FULLSCREEN:
if(IS_NONZERO(attribs[i]) != IS_NONZERO(checkFlag(DDWF_FULLSCREEN)))
{
if(attribs[i] && Updater_IsDownloadInProgress())
{
// Can't go to fullscreen when downloading.
return false;
}
setFlag(DDWF_FULLSCREEN, attribs[i]);
changed = true;
}
break;
case DDWA_VISIBLE:
if(IS_NONZERO(attribs[i]) != IS_NONZERO(checkFlag(DDWF_VISIBLE)))
{
setFlag(DDWF_VISIBLE, attribs[i]);
changed = true;
}
break;
#undef IS_NONZERO
case DDWA_COLOR_DEPTH_BITS:
qDebug() << attribs[i] << colorDepthBits;
if(attribs[i] != colorDepthBits)
{
colorDepthBits = attribs[i];
if(colorDepthBits < 8 || colorDepthBits > 32) return false; // Illegal value.
changed = true;
}
break;
default:
// Unknown attribute.
return false;
}
}
if(!changed)
{
VERBOSE(Con_Message("New window attributes same as before.\n"));
return true;
}
// Seems ok, apply them.
applyWindowGeometry();
#endif // __CLIENT__
return true;
}
void updateLayout()
{
//qDebug() << "Window::updateLayout:" << widget->geometry() << widget->canvas().geometry();
setFlag(DDWF_MAXIMIZE, widget->isMaximized());
geometry.size.width = widget->width();
geometry.size.height = widget->height();
if(!(flags & DDWF_FULLSCREEN))
{
DEBUG_Message(("Updating current view geometry for window, fetched %i x %i.\n", width(), height()));
if(!(flags & DDWF_MAXIMIZE) && !isBeingAdjusted())
{
// Update the normal-mode geometry (not fullscreen, not maximized).
normalGeometry.size.width = geometry.size.width;
DEBUG_Message(("ngw=%i [C]\n", normalGeometry.size.width));
normalGeometry.size.height = geometry.size.height;
DEBUG_Message(("Updating normal view geometry for window, fetched %i x %i.\n", width(), height()));
}
}
else
{
DEBUG_Message(("Updating view geometry for fullscreen (%i x %i).\n", width(), height()));
}
#ifdef __CLIENT__
// Update viewports.
R_SetViewGrid(0, 0);
if(BusyMode_Active() || UI_IsActive() || !DD_GameLoaded())
{
// Update for busy mode.
R_UseViewPort(0);
}
R_LoadSystemFonts();
if(UI_IsActive())
{
UI_UpdatePageLayout();
}
#endif
}
};
/// Current active window where all drawing operations occur.
const Window* theWindow;
static boolean winManagerInited = false;
static Window mainWindow;
static boolean mainWindowInited = false;
static void updateMainWindowLayout(void)
{
Window* win = Window_Main();
if(win->needReshowFullscreen)
{
LOG_DEBUG("Main window re-set to fullscreen mode.");
win->needReshowFullscreen = false;
win->widget->showNormal();
win->widget->showFullScreen();
}
if(win->needShowFullscreen)
{
LOG_DEBUG("Main window to fullscreen mode.");
win->needShowFullscreen = false;
win->widget->showFullScreen();
}
if(win->flags & DDWF_FULLSCREEN)
{
#if defined MACOSX && defined __CLIENT__
// For some interesting reason, we have to scale the window twice in fullscreen mode
// or the resulting layout won't be correct.
win->widget->setGeometry(QRect(0, 0, 320, 240));
win->widget->setGeometry(win->appliedGeometry);
DisplayMode_Native_Raise(Window_NativeHandle(win));
#endif
Window_TrapMouse(win, true);
}
if(win->needShowNormal)
{
LOG_DEBUG("Main window to normal mode (center:%b).") << ((win->flags & DDWF_CENTER) != 0);
win->needShowNormal = false;
win->widget->showNormal();
}
}
static void useAppliedGeometryForWindows(void)
{
Window* win = Window_Main();
if(!win || !win->widget) return;
if(win->flags & DDWF_CENTER)
{
win->appliedGeometry = win->centeredGeometry();
}
DEBUG_Message(("Using applied geometry: (%i,%i) %ix%i\n",
win->appliedGeometry.x(),
win->appliedGeometry.y(),
win->appliedGeometry.width(),
win->appliedGeometry.height()));
win->widget->setGeometry(win->appliedGeometry);
}
Window* Window_Main(void)
{
return &mainWindow;
}
static void notifyAboutModeChange(void)
{
LOG_MSG("Display mode has changed.");
DENG2_APP->notifyDisplayModeChanged();
}
static void endWindowWait(void)
{
Window* win = Window_Main();
if(win)
{
DEBUG_Message(("Window is no longer waiting for geometry changes.\n"));
// This flag is used for protecting against mode change resizings.
win->needWait = false;
}
}
static int getWindowIdx(const Window* wnd)
{
/// @todo Multiple windows.
if(wnd == &mainWindow) return mainWindowIdx;
return 0;
}
static __inline Window *getWindow(uint idx)
{
if(!winManagerInited)
return NULL; // Window manager is not initialized.
if(idx == 1)
return &mainWindow;
//assert(false); // We can only have window 1 (main window).
return NULL;
}
Window* Window_ByIndex(uint id)
{
return getWindow(id);
}
#if 0
static boolean setDDWindow(Window *window, int newWidth, int newHeight,
int newBPP, uint wFlags, uint uFlags)
{
int width, height, bpp, flags;
boolean newGLContext = false;
boolean changeWindowDimensions = false;
boolean inControlPanel = false;
if(novideo)
return true;
if(uFlags & DDSW_NOCHANGES)
return true; // Nothing to do.
// Grab the current values.
width = window->geometry.size.width;
height = window->geometry.size.height;
bpp = window->bpp;
flags = window->flags;
// Force update on init?
if(!window->inited && window->type == WT_NORMAL)
{
newGLContext = true;
}
if(window->type == WT_NORMAL)
inControlPanel = UI_IsActive();
// Change to fullscreen?
if(!(uFlags & DDSW_NOFULLSCREEN) &&
(flags & DDWF_FULLSCREEN) != (wFlags & DDWF_FULLSCREEN))
{
flags ^= DDWF_FULLSCREEN;
if(window->type == WT_NORMAL)
{
newGLContext = true;
//changeVideoMode = true;
}
}
// Change window size?
if(!(uFlags & DDSW_NOSIZE) && (width != newWidth || height != newHeight))
{
width = newWidth;
height = newHeight;
changeWindowDimensions = true;
if(window->type == WT_NORMAL)
newGLContext = true;
}
// Change BPP (bits per pixel)?
if(window->type == WT_NORMAL)
{
if(!(uFlags & DDSW_NOBPP) && bpp != newBPP)
{
if(!(newBPP == 32 || newBPP == 16))
Con_Error("Sys_SetWindow: Unsupported BPP %i.", newBPP);
bpp = newBPP;
newGLContext = true;
//changeVideoMode = true;
}
}
if(changeWindowDimensions && window->type == WT_NORMAL)
{
// Can't change the resolution while the UI is active.
// (controls need to be repositioned).
if(inControlPanel)
UI_End();
}
/*
if(changeVideoMode)
{
if(flags & DDWF_FULLSCREEN)
{
if(!Sys_ChangeVideoMode(width, height, bpp))
{
Sys_CriticalMessage("Sys_SetWindow: Resolution change failed.");
return false;
}
}
else
{
// Go back to normal display settings.
ChangeDisplaySettings(0, 0);
}
}
*/
// Update the current values.
window->geometry.size.width = width;
window->geometry.size.height = height;
window->bpp = bpp;
window->flags = flags;
if(!window->inited)
window->inited = true;
// Do NOT modify Window properties after this point.
// Do we need a new GL context due to changes to the window?
if(newGLContext)
{
#if 0
// Maybe requires a renderer restart.
extern boolean usingFog;
boolean glIsInited = GL_IsInited();
#if defined(WIN32)
void *data = window->hWnd;
#else
void *data = NULL;
#endif
boolean hadFog = false;
if(glIsInited)
{
// Shut everything down, but remember our settings.
hadFog = usingFog;
GL_TotalReset();
if(DD_GameLoaded() && gx.UpdateState)
gx.UpdateState(DD_RENDER_RESTART_PRE);
R_UnloadSvgs();
GL_ReleaseTextures();
}
if(createContext(window->geometry.size.width, window->geometry.size.height,
window->normal.bpp, (window->flags & DDWF_FULLSCREEN)? false : true, data))
{
// We can get on with initializing the OGL state.
Sys_GLConfigureDefaultState();
}
if(glIsInited)
{
// Re-initialize.
GL_TotalRestore();
GL_InitRefresh();
if(hadFog)
GL_UseFog(true);
if(DD_GameLoaded() && gx.UpdateState)
gx.UpdateState(DD_RENDER_RESTART_POST);
}
#endif
}
// If the window dimensions have changed, update any sub-systems
// which need to respond.
if(changeWindowDimensions && window->type == WT_NORMAL)
{
// Update viewport coordinates.
R_SetViewGrid(0, 0);
if(inControlPanel) // Reactivate the panel?
Con_Execute(CMDS_DDAY, "panel", true, false);
}
return true;
}
#endif
/**
* Initialize the window manager.
* Tasks include; checking the system environment for feature enumeration.
*
* @return @c true, if initialization was successful.
*/
boolean Sys_InitWindowManager(void)
{
LOG_AS("Sys_InitWindowManager");
if(winManagerInited)
return true; // Already been here.
LOG_MSG("Using Qt window management.");
CanvasWindow::setDefaultGLFormat();
memset(&mainWindow, 0, sizeof(mainWindow));
winManagerInited = true;
theWindow = &mainWindow;
return true;
}
/**
* Shutdown the window manager.
*
* @return @c true, if shutdown was successful.
*/
boolean Sys_ShutdownWindowManager(void)
{
if(!winManagerInited)
return false; // Window manager is not initialized.
/// @todo Delete all windows, not just the main one.
// Get rid of the windows.
Window_Delete(Window_Main());
// Now off-line, no more window management will be possible.
winManagerInited = false;
return true;
}
#if 0
/**
* Attempt to acquire a device context for OpenGL rendering and then init.
*
* @param width Width of the OGL window.
* @param height Height of the OGL window.
* @param bpp 0= the current display color depth is used.
* @param windowed @c true = windowed mode ELSE fullscreen.
* @param data Ptr to system-specific data, e.g a window handle
* or similar.
*
* @return @c true if successful.
*/
static boolean createContext(void)
{
Con_Message("createContext: OpenGL.\n");
#if 0
// Set GL attributes. We want at least 5 bits per color and a 16
// bit depth buffer. Plus double buffering, of course.
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#endif
/*
// Attempt to set the video mode.
if(!Sys_ChangeVideoMode(Window_Width(theWindow),
Window_Height(theWindow),
Window_ColorDepthBits(theWindow)))
{
Con_Error("createContext: Video mode change failed.\n");
return false;
}
*/
Sys_GLConfigureDefaultState();
#ifdef MACOSX
// Vertical sync is a GL context property.
GL_SetVSync(true);
#endif