/
CIrrDeviceLinux.cpp
3189 lines (2791 loc) · 94.4 KB
/
CIrrDeviceLinux.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
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// Copyright (C) 2014-2015 Dawid Gan
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
extern bool GLContextDebugBit;
#include "CIrrDeviceLinux.h"
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <time.h>
#include <locale.h>
#include "IEventReceiver.h"
#include "ISceneManager.h"
#include "IGUIEnvironment.h"
#include "os.h"
#include "CTimer.h"
#include "irrString.h"
#include "Keycodes.h"
#include "COSOperator.h"
#include "CColorConverter.h"
#include "SIrrCreationParameters.h"
#include "IGUISpriteBank.h"
#include <X11/XKBlib.h>
#include <X11/Xatom.h>
#ifdef _IRR_COMPILE_WITH_OPENGL_
#include <GL/gl.h>
#ifdef _IRR_OPENGL_USE_EXTPOINTER_
#define GLX_GLXEXT_PROTOTYPES
#include "glxext.h"
#endif
#endif
#ifdef _IRR_LINUX_XCURSOR_
#include <X11/Xcursor/Xcursor.h>
#endif
#if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
#include <fcntl.h>
#include <unistd.h>
#ifdef __FreeBSD__
#include <sys/joystick.h>
#elif defined(__linux__)
// linux/joystick.h includes linux/input.h, which #defines values for various KEY_FOO keys.
// These override the irr::KEY_FOO equivalents, which stops key handling from working.
// As a workaround, defining _INPUT_H stops linux/input.h from being included; it
// doesn't actually seem to be necessary except to pull in sys/ioctl.h.
#define _INPUT_H
#include <sys/ioctl.h> // Would normally be included in linux/input.h
#include <linux/joystick.h>
#undef _INPUT_H
#endif
#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
#define XRANDR_ROTATION_LEFT (1 << 1)
#define XRANDR_ROTATION_RIGHT (1 << 3)
namespace irr
{
namespace video
{
extern bool useCoreContext;
IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
io::IFileSystem* io, CIrrDeviceLinux* device);
IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params,
video::SExposedVideoData& data, io::IFileSystem* io);
}
} // end namespace irr
#if defined(_IRR_COMPILE_WITH_X11_)
namespace
{
Atom X_ATOM_CLIPBOARD;
Atom X_ATOM_TARGETS;
Atom X_ATOM_UTF8_STRING;
};
#endif
namespace irr
{
const char* wmDeleteWindow = "WM_DELETE_WINDOW";
//! constructor
CIrrDeviceLinux::CIrrDeviceLinux(const SIrrlichtCreationParameters& param)
: CIrrDeviceStub(param),
#ifdef _IRR_COMPILE_WITH_X11_
display(0), visual(0), screennr(0), window(0), StdHints(0), SoftwareImage(0),
XInputMethod(0), XInputContext(0), m_font_set(0), numlock_mask(0),
m_ime_enabled(false),
#ifdef _IRR_COMPILE_WITH_OPENGL_
glxWin(0),
Context(0),
#endif
#endif
Width(param.WindowSize.Width), Height(param.WindowSize.Height),
WindowHasFocus(false), WindowMinimized(false),
UseXVidMode(false), UseXRandR(false), UseGLXWindow(false),
ExternalWindow(false), AutorepeatSupport(0)
{
#ifdef _DEBUG
setDebugName("CIrrDeviceLinux");
#endif
// print version, distribution etc.
// thx to LynxLuna for pointing me to the uname function
core::stringc linuxversion;
struct utsname LinuxInfo;
uname(&LinuxInfo);
linuxversion += LinuxInfo.sysname;
linuxversion += " ";
linuxversion += LinuxInfo.release;
linuxversion += " ";
linuxversion += LinuxInfo.version;
linuxversion += " ";
linuxversion += LinuxInfo.machine;
Operator = new COSOperator(linuxversion, this);
os::Printer::log(linuxversion.c_str(), ELL_INFORMATION);
// create keymap
createKeyMap();
// create window
if (CreationParams.DriverType != video::EDT_NULL)
{
// create the window, only if we do not use the null device
if (!createWindow())
return;
}
// create cursor control
CursorControl = new CCursorControl(this, CreationParams.DriverType == video::EDT_NULL);
// create driver
createDriver();
if (!VideoDriver)
return;
#ifdef _IRR_COMPILE_WITH_X11_
m_ime_position.x = 0;
m_ime_position.y = 0;
createInputContext();
numlock_mask = getNumlockMask(display);
// Get maximum 16 characters from input method
m_ime_char_holder.reallocate(16);
#endif
createGUIAndScene();
}
//! destructor
CIrrDeviceLinux::~CIrrDeviceLinux()
{
#ifdef _IRR_COMPILE_WITH_X11_
if (StdHints)
XFree(StdHints);
// Disable cursor (it is drop'ed in stub)
if (CursorControl)
{
CursorControl->setVisible(false);
static_cast<CCursorControl*>(CursorControl)->clearCursors();
}
// Must free OpenGL textures etc before destroying context, so can't wait for stub destructor
if ( GUIEnvironment )
{
GUIEnvironment->drop();
GUIEnvironment = NULL;
}
if ( SceneManager )
{
SceneManager->drop();
SceneManager = NULL;
}
if ( VideoDriver )
{
VideoDriver->drop();
VideoDriver = NULL;
}
destroyInputContext();
if (display)
{
#ifdef _IRR_COMPILE_WITH_OPENGL_
if (Context)
{
if (glxWin)
{
if (!glXMakeContextCurrent(display, None, None, NULL))
os::Printer::log("Could not release glx context.", ELL_WARNING);
}
else
{
if (!glXMakeCurrent(display, None, NULL))
os::Printer::log("Could not release glx context.", ELL_WARNING);
}
glXDestroyContext(display, Context);
if (glxWin)
glXDestroyWindow(display, glxWin);
}
#endif // #ifdef _IRR_COMPILE_WITH_OPENGL_
if (SoftwareImage)
XDestroyImage(SoftwareImage);
if (!ExternalWindow)
{
XDestroyWindow(display,window);
}
// Reset fullscreen resolution change
restoreResolution();
if (!ExternalWindow)
{
XCloseDisplay(display);
}
}
if (visual)
XFree(visual);
#endif // #ifdef _IRR_COMPILE_WITH_X11_
#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
{
if (ActiveJoysticks[joystick].fd >= 0)
{
close(ActiveJoysticks[joystick].fd);
}
}
#endif
}
#if defined(_IRR_COMPILE_WITH_X11_)
static bool XErrorSignaled = false;
int IrrPrintXError(Display *display, XErrorEvent *event)
{
char msg[256];
char msg2[256];
XErrorSignaled = true;
snprintf(msg, 256, "%d", event->request_code);
XGetErrorDatabaseText(display, "XRequest", msg, "unknown", msg2, 256);
XGetErrorText(display, event->error_code, msg, 256);
os::Printer::log("X Error", msg, ELL_WARNING);
os::Printer::log("From call ", msg2, ELL_WARNING);
return 0;
}
#endif
bool CIrrDeviceLinux::restoreResolution()
{
if (!CreationParams.Fullscreen)
return true;
#ifdef _IRR_LINUX_X11_VIDMODE_
if (UseXVidMode && CreationParams.Fullscreen)
{
XF86VidModeSwitchToMode(display, screennr, &oldVideoMode);
XF86VidModeSetViewPort(display, screennr, 0, 0);
}
#endif
#ifdef _IRR_LINUX_X11_RANDR_
if (UseXRandR && CreationParams.Fullscreen && old_mode != 0)
{
XRRScreenResources* res = XRRGetScreenResources(display, DefaultRootWindow(display));
if (!res)
return false;
XRROutputInfo* output = XRRGetOutputInfo(display, res, output_id);
if (!output || !output->crtc || output->connection == RR_Disconnected)
{
XRRFreeOutputInfo(output);
return false;
}
XRRCrtcInfo* crtc = XRRGetCrtcInfo(display, res, output->crtc);
if (!crtc)
{
XRRFreeOutputInfo(output);
return false;
}
Status s = XRRSetCrtcConfig(display, res, output->crtc, CurrentTime,
crtc->x, crtc->y, old_mode,
crtc->rotation, &output_id, 1);
XRRFreeOutputInfo(output);
XRRFreeCrtcInfo(crtc);
XRRFreeScreenResources(res);
if (s != Success)
return false;
}
#endif
return true;
}
bool CIrrDeviceLinux::changeResolution()
{
if (!CreationParams.Fullscreen)
return true;
getVideoModeList();
#if defined(_IRR_LINUX_X11_VIDMODE_) || defined(_IRR_LINUX_X11_RANDR_)
s32 eventbase, errorbase;
s32 bestMode = -1;
#endif
#ifdef _IRR_LINUX_X11_VIDMODE_
if (XF86VidModeQueryExtension(display, &eventbase, &errorbase))
{
// enumerate video modes
s32 modeCount;
XF86VidModeModeInfo** modes;
float refresh_rate, refresh_rate_old;
XF86VidModeGetAllModeLines(display, screennr, &modeCount, &modes);
// find fitting mode
for (s32 i = 0; i<modeCount; ++i)
{
if (bestMode==-1 && modes[i]->hdisplay >= Width && modes[i]->vdisplay >= Height)
{
bestMode = i;
}
else if (bestMode!=-1 &&
modes[i]->hdisplay == modes[bestMode]->hdisplay &&
modes[i]->vdisplay == modes[bestMode]->vdisplay)
{
refresh_rate_old = (modes[bestMode]->dotclock * 1000.0) / (modes[bestMode]->htotal * modes[bestMode]->vtotal);
refresh_rate = (modes[i]->dotclock * 1000.0) / (modes[i]->htotal * modes[i]->vtotal);
if (refresh_rate > refresh_rate_old)
{
bestMode = i;
}
}
else if (bestMode!=-1 &&
modes[i]->hdisplay >= Width &&
modes[i]->vdisplay >= Height &&
modes[i]->hdisplay <= modes[bestMode]->hdisplay &&
modes[i]->vdisplay <= modes[bestMode]->vdisplay)
{
bestMode = i;
}
}
if (bestMode != -1)
{
os::Printer::log("Starting vidmode fullscreen mode...", ELL_INFORMATION);
os::Printer::log("hdisplay: ", core::stringc(modes[bestMode]->hdisplay).c_str(), ELL_INFORMATION);
os::Printer::log("vdisplay: ", core::stringc(modes[bestMode]->vdisplay).c_str(), ELL_INFORMATION);
XF86VidModeSwitchToMode(display, screennr, modes[bestMode]);
XF86VidModeSetViewPort(display, screennr, 0, 0);
UseXVidMode=true;
}
else
{
os::Printer::log("Could not find specified video mode, running windowed.", ELL_WARNING);
CreationParams.Fullscreen = false;
}
XFree(modes);
}
#endif
#ifdef _IRR_LINUX_X11_RANDR_
while (XRRQueryExtension(display, &eventbase, &errorbase))
{
if (output_id == 0)
break;
XRRScreenResources* res = XRRGetScreenResources(display, DefaultRootWindow(display));
if (!res)
break;
XRROutputInfo* output = XRRGetOutputInfo(display, res, output_id);
if (!output || !output->crtc || output->connection == RR_Disconnected)
{
XRRFreeOutputInfo(output);
XRRFreeScreenResources(res);
break;
}
XRRCrtcInfo* crtc = XRRGetCrtcInfo(display, res, output->crtc);
if (!crtc)
{
XRRFreeOutputInfo(output);
XRRFreeScreenResources(res);
break;
}
float refresh_rate, refresh_rate_new;
core::dimension2d<u32> mode0_size = core::dimension2d<u32>(0, 0);
for (int i = 0; i < res->nmode; i++)
{
const XRRModeInfo* mode = &res->modes[i];
core::dimension2d<u32> size;
if (crtc->rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT))
{
size = core::dimension2d<u32>(mode->height, mode->width);
}
else
{
size = core::dimension2d<u32>(mode->width, mode->height);
}
if (bestMode == -1 && mode->id == output->modes[0])
{
mode0_size = size;
}
if (bestMode == -1 && size.Width == Width && size.Height == Height)
{
for (int j = 0; j < output->nmode; j++)
{
if (mode->id == output->modes[j])
{
bestMode = j;
refresh_rate = (mode->dotClock * 1000.0) / (mode->hTotal * mode->vTotal);
break;
}
}
}
else if (bestMode != -1 && size.Width == Width && size.Height == Height)
{
refresh_rate_new = (mode->dotClock * 1000.0) / (mode->hTotal * mode->vTotal);
if (refresh_rate_new <= refresh_rate)
break;
for (int j = 0; j < output->nmode; j++)
{
if (mode->id == output->modes[j])
{
bestMode = j;
refresh_rate = refresh_rate_new;
break;
}
}
}
}
// If video mode not found, try to use first available
if (bestMode == -1)
{
bestMode = 0;
Width = mode0_size.Width;
Height = mode0_size.Height;
}
Status s = XRRSetCrtcConfig(display, res, output->crtc, CurrentTime,
crtc->x, crtc->y, output->modes[bestMode],
crtc->rotation, &output_id, 1);
if (s == Success)
UseXRandR = true;
XRRFreeCrtcInfo(crtc);
XRRFreeOutputInfo(output);
XRRFreeScreenResources(res);
break;
}
if (UseXRandR == false)
{
os::Printer::log("Could not get video output. Try to run in windowed mode.", ELL_WARNING);
CreationParams.Fullscreen = false;
}
#endif
return CreationParams.Fullscreen;
}
#if defined(_IRR_COMPILE_WITH_X11_)
void IrrPrintXGrabError(int grabResult, const c8 * grabCommand )
{
if ( grabResult == GrabSuccess )
{
// os::Printer::log(grabCommand, ": GrabSuccess", ELL_INFORMATION);
return;
}
switch ( grabResult )
{
case AlreadyGrabbed:
os::Printer::log(grabCommand, ": AlreadyGrabbed", ELL_WARNING);
break;
case GrabNotViewable:
os::Printer::log(grabCommand, ": GrabNotViewable", ELL_WARNING);
break;
case GrabFrozen:
os::Printer::log(grabCommand, ": GrabFrozen", ELL_WARNING);
break;
case GrabInvalidTime:
os::Printer::log(grabCommand, ": GrabInvalidTime", ELL_WARNING);
break;
default:
os::Printer::log(grabCommand, ": grab failed with unknown problem", ELL_WARNING);
break;
}
}
#endif
#ifdef _IRR_COMPILE_WITH_OPENGL_
#ifdef _IRR_COMPILE_WITH_X11_
static GLXContext getMeAGLContext(Display *display, GLXFBConfig glxFBConfig, bool force_legacy_context)
{
GLXContext Context;
irr::video::useCoreContext = true;
int core43ctxdebug[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
None
};
int core43ctx[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None
};
int core33ctxdebug[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
None
};
int core33ctx[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None
};
int core31ctxdebug[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 1,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
None
};
int core31ctx[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 1,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None
};
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = 0;
glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
if(!force_legacy_context)
{
// create core 4.3 context
os::Printer::log("Creating OpenGL 4.3 context...", ELL_INFORMATION);
Context = glXCreateContextAttribsARB(display, glxFBConfig, 0, True, GLContextDebugBit ? core43ctxdebug : core43ctx);
if (!XErrorSignaled)
return Context;
XErrorSignaled = false;
// create core 3.3 context
os::Printer::log("Creating OpenGL 3.3 context...", ELL_INFORMATION);
Context = glXCreateContextAttribsARB(display, glxFBConfig, 0, True, GLContextDebugBit ? core33ctxdebug : core33ctx);
if (!XErrorSignaled)
return Context;
XErrorSignaled = false;
// create core 3.1 context (for older mesa)
os::Printer::log("Creating OpenGL 3.1 context...", ELL_INFORMATION);
Context = glXCreateContextAttribsARB(display, glxFBConfig, 0, True, GLContextDebugBit ? core31ctxdebug : core31ctx);
if (!XErrorSignaled)
return Context;
} // if(force_legacy_context)
XErrorSignaled = false;
irr::video::useCoreContext = false;
// fall back to legacy context
os::Printer::log("Creating legacy OpenGL 2.1 context...", ELL_INFORMATION);
Context = glXCreateNewContext(display, glxFBConfig, GLX_RGBA_TYPE, NULL, True);
return Context;
}
#endif
#endif
bool CIrrDeviceLinux::createWindow()
{
#ifdef _IRR_COMPILE_WITH_X11_
os::Printer::log("Creating X window...", ELL_INFORMATION);
XSetErrorHandler(IrrPrintXError);
display = XOpenDisplay(0);
if (!display)
{
os::Printer::log("Error: Need running XServer to start Irrlicht Engine.", ELL_ERROR);
if (XDisplayName(0)[0])
os::Printer::log("Could not open display", XDisplayName(0), ELL_ERROR);
else
os::Printer::log("Could not open display, set DISPLAY variable", ELL_ERROR);
return false;
}
screennr = DefaultScreen(display);
changeResolution();
#ifdef _IRR_COMPILE_WITH_OPENGL_
GLXFBConfig glxFBConfig = NULL;
int major, minor;
bool isAvailableGLX=false;
if (CreationParams.DriverType==video::EDT_OPENGL)
{
isAvailableGLX=glXQueryExtension(display,&major,&minor);
if (isAvailableGLX && glXQueryVersion(display, &major, &minor))
{
#ifdef GLX_VERSION_1_3
typedef GLXFBConfig * ( * PFNGLXCHOOSEFBCONFIGPROC) (Display *dpy, int screen, const int *attrib_list, int *nelements);
#ifdef _IRR_OPENGL_USE_EXTPOINTER_
PFNGLXCHOOSEFBCONFIGPROC glxChooseFBConfig = (PFNGLXCHOOSEFBCONFIGPROC)glXGetProcAddress(reinterpret_cast<const GLubyte*>("glXChooseFBConfig"));
#else
PFNGLXCHOOSEFBCONFIGPROC glxChooseFBConfig=glXChooseFBConfig;
#endif
if (major==1 && minor>2 && glxChooseFBConfig)
{
// attribute array for the draw buffer
int visualAttrBuffer[] =
{
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_RED_SIZE, 4,
GLX_GREEN_SIZE, 4,
GLX_BLUE_SIZE, 4,
GLX_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0,
GLX_DEPTH_SIZE, CreationParams.ZBufferBits, //10,11
GLX_DOUBLEBUFFER, CreationParams.Doublebuffer?True:False,
GLX_STENCIL_SIZE, CreationParams.Stencilbuffer?1:0,
#if defined(GLX_VERSION_1_4) && defined(GLX_SAMPLE_BUFFERS) // we need to check the extension string!
GLX_SAMPLE_BUFFERS, 1,
GLX_SAMPLES, CreationParams.AntiAlias, // 18,19
#elif defined(GLX_ARB_multisample)
GLX_SAMPLE_BUFFERS_ARB, 1,
GLX_SAMPLES_ARB, CreationParams.AntiAlias, // 18,19
#elif defined(GLX_SGIS_multisample)
GLX_SAMPLE_BUFFERS_SGIS, 1,
GLX_SAMPLES_SGIS, CreationParams.AntiAlias, // 18,19
#else
#error
#endif
#ifdef GLX_ARB_framebuffer_sRGB
GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, CreationParams.HandleSRGB,
#elif defined(GLX_EXT_framebuffer_sRGB)
GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, CreationParams.HandleSRGB,
#else
#error
#endif
GLX_STEREO, CreationParams.Stereobuffer?True:False,
None
};
GLXFBConfig *configList=0;
int nitems=0;
if (CreationParams.AntiAlias<2)
{
visualAttrBuffer[17] = 0;
visualAttrBuffer[19] = 0;
}
if (CreationParams.HandleSRGB == false)
{
visualAttrBuffer[21] = GLX_DONT_CARE;
}
// first round with unchanged values
{
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (!configList && CreationParams.AntiAlias)
{
while (!configList && (visualAttrBuffer[19]>1))
{
visualAttrBuffer[19] -= 1;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
}
if (!configList)
{
visualAttrBuffer[17] = 0;
visualAttrBuffer[19] = 0;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (configList)
{
os::Printer::log("No FSAA available.", ELL_WARNING);
CreationParams.AntiAlias=0;
}
else
{
//reenable multisampling
visualAttrBuffer[17] = 1;
visualAttrBuffer[19] = CreationParams.AntiAlias;
}
}
}
}
// Try to disable sRGB framebuffer
if (!configList && CreationParams.HandleSRGB)
{
os::Printer::log("No sRGB framebuffer available.", ELL_WARNING);
CreationParams.HandleSRGB=false;
visualAttrBuffer[21] = GLX_DONT_CARE;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (!configList && CreationParams.AntiAlias)
{
while (!configList && (visualAttrBuffer[19]>1))
{
visualAttrBuffer[19] -= 1;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
}
if (!configList)
{
visualAttrBuffer[17] = 0;
visualAttrBuffer[19] = 0;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (configList)
{
os::Printer::log("No FSAA available.", ELL_WARNING);
CreationParams.AntiAlias=0;
}
else
{
//reenable multisampling
visualAttrBuffer[17] = 1;
visualAttrBuffer[19] = CreationParams.AntiAlias;
}
}
}
}
// Next try with flipped stencil buffer value
// If the first round was with stencil flag it's now without
// Other way round also makes sense because some configs
// only have depth buffer combined with stencil buffer
if (!configList)
{
if (CreationParams.Stencilbuffer)
os::Printer::log("No stencilbuffer available, disabling stencil shadows.", ELL_WARNING);
CreationParams.Stencilbuffer = !CreationParams.Stencilbuffer;
visualAttrBuffer[15]=CreationParams.Stencilbuffer?1:0;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (!configList && CreationParams.AntiAlias)
{
while (!configList && (visualAttrBuffer[19]>1))
{
visualAttrBuffer[19] -= 1;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
}
if (!configList)
{
visualAttrBuffer[17] = 0;
visualAttrBuffer[19] = 0;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (configList)
{
os::Printer::log("No FSAA available.", ELL_WARNING);
CreationParams.AntiAlias=0;
}
else
{
//reenable multisampling
visualAttrBuffer[17] = 1;
visualAttrBuffer[19] = CreationParams.AntiAlias;
}
}
}
}
// Next try without double buffer
if (!configList && CreationParams.Doublebuffer)
{
os::Printer::log("No doublebuffering available.", ELL_WARNING);
CreationParams.Doublebuffer=false;
visualAttrBuffer[13] = GLX_DONT_CARE;
CreationParams.Stencilbuffer = false;
visualAttrBuffer[15]=0;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (!configList && CreationParams.AntiAlias)
{
while (!configList && (visualAttrBuffer[19]>1))
{
visualAttrBuffer[19] -= 1;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
}
if (!configList)
{
visualAttrBuffer[17] = 0;
visualAttrBuffer[19] = 0;
configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
if (configList)
{
os::Printer::log("No FSAA available.", ELL_WARNING);
CreationParams.AntiAlias=0;
}
else
{
//reenable multisampling
visualAttrBuffer[17] = 1;
visualAttrBuffer[19] = CreationParams.AntiAlias;
}
}
}
}
if (configList)
{
glxFBConfig=configList[0];
XFree(configList);
UseGLXWindow=true;
#ifdef _IRR_OPENGL_USE_EXTPOINTER_
typedef XVisualInfo * ( * PFNGLXGETVISUALFROMFBCONFIGPROC) (Display *dpy, GLXFBConfig config);
PFNGLXGETVISUALFROMFBCONFIGPROC glxGetVisualFromFBConfig= (PFNGLXGETVISUALFROMFBCONFIGPROC)glXGetProcAddress(reinterpret_cast<const GLubyte*>("glXGetVisualFromFBConfig"));
if (glxGetVisualFromFBConfig)
visual = glxGetVisualFromFBConfig(display,glxFBConfig);
#else
visual = glXGetVisualFromFBConfig(display,glxFBConfig);
#endif
}
}
else
#endif
{
// attribute array for the draw buffer
int visualAttrBuffer[] =
{
GLX_RGBA, GLX_USE_GL,
GLX_RED_SIZE, 4,
GLX_GREEN_SIZE, 4,
GLX_BLUE_SIZE, 4,
GLX_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0,
GLX_DEPTH_SIZE, CreationParams.ZBufferBits,
GLX_STENCIL_SIZE, CreationParams.Stencilbuffer?1:0, // 12,13
// The following attributes have no flags, but are
// either present or not. As a no-op we use
// GLX_USE_GL, which is silently ignored by glXChooseVisual
CreationParams.Doublebuffer?GLX_DOUBLEBUFFER:GLX_USE_GL, // 14
CreationParams.Stereobuffer?GLX_STEREO:GLX_USE_GL, // 15
#ifdef GLX_ARB_framebuffer_sRGB
CreationParams.HandleSRGB?GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB:GLX_USE_GL,
#elif defined(GLX_EXT_framebuffer_sRGB)
CreationParams.HandleSRGB?GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:GLX_USE_GL,
#endif
None
};
visual=glXChooseVisual(display, screennr, visualAttrBuffer);
if (!visual)
{
if (CreationParams.Stencilbuffer)
os::Printer::log("No stencilbuffer available, disabling.", ELL_WARNING);
CreationParams.Stencilbuffer = !CreationParams.Stencilbuffer;
visualAttrBuffer[13]=CreationParams.Stencilbuffer?1:0;
visual=glXChooseVisual(display, screennr, visualAttrBuffer);
if (!visual && CreationParams.Doublebuffer)
{
os::Printer::log("No doublebuffering available.", ELL_WARNING);
CreationParams.Doublebuffer=false;
visualAttrBuffer[14] = GLX_USE_GL;
visual=glXChooseVisual(display, screennr, visualAttrBuffer);
}
}
}
}
else
os::Printer::log("No GLX support available. OpenGL driver will not work.", ELL_WARNING);
}
// don't use the XVisual with OpenGL, because it ignores all requested
// properties of the CreationParams
else if (!visual)
#endif // _IRR_COMPILE_WITH_OPENGL_
// create visual with standard X methods
{
os::Printer::log("Using plain X visual");
XVisualInfo visTempl; //Template to hold requested values
int visNumber; // Return value of available visuals
visTempl.screen = screennr;
// ARGB visuals should be avoided for usual applications
visTempl.depth = CreationParams.WithAlphaChannel?32:24;
while ((!visual) && (visTempl.depth>=16))
{
visual = XGetVisualInfo(display, VisualScreenMask|VisualDepthMask,
&visTempl, &visNumber);
visTempl.depth -= 8;
}
}
if (!visual)
{
os::Printer::log("Fatal error, could not get visual.", ELL_ERROR);
XCloseDisplay(display);
display=0;
return false;
}
#ifdef _DEBUG
else
os::Printer::log("Visual chosen: ", core::stringc(static_cast<u32>(visual->visualid)).c_str(), ELL_DEBUG);
#endif
// create color map
Colormap colormap;
colormap = XCreateColormap(display,
RootWindow(display, visual->screen),
visual->visual, AllocNone);
attributes.colormap = colormap;
attributes.border_pixel = 0;
attributes.event_mask = StructureNotifyMask | FocusChangeMask | ExposureMask;
if (!CreationParams.IgnoreInput)
attributes.event_mask |= PointerMotionMask |
ButtonPressMask | KeyPressMask |
ButtonReleaseMask | KeyReleaseMask;
Atom *list;
Atom type;
int form;
unsigned long remain, len;
Atom WMCheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", false);
Status s = XGetWindowProperty(display, DefaultRootWindow(display),
WMCheck, 0L, 1L, False, XA_WINDOW,
&type, &form, &len, &remain,
(unsigned char **)&list);
XFree(list);
bool netWM = (s == Success) && len;
if (!CreationParams.WindowId)
{
attributes.override_redirect = !netWM && CreationParams.Fullscreen;
// create new Window
window = XCreateWindow(display,
RootWindow(display, visual->screen),
0, 0, Width, Height, 0, visual->depth,
InputOutput, visual->visual,
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
&attributes);
XMapRaised(display, window);
CreationParams.WindowId = (void*)window;
Atom wmDelete;
wmDelete = XInternAtom(display, wmDeleteWindow, True);
XSetWMProtocols(display, window, &wmDelete, 1);
if (CreationParams.Fullscreen)
{
if (netWM)
{
// Some window managers don't respect values from XCreateWindow and
// place window in random position. This may cause that fullscreen
// window is showed in wrong screen. It doesn't matter for vidmode
// which displays cloned image in all devices.
#ifdef _IRR_LINUX_X11_RANDR_
XMoveResizeWindow(display, window, crtc_x, crtc_y, Width, Height);
XRaiseWindow(display, window);
XFlush(display);
#endif
// Set the fullscreen mode via the window manager. This allows alt-tabing, volume hot keys & others.
// Get the needed atom from there freedesktop names
Atom WMStateAtom = XInternAtom(display, "_NET_WM_STATE", true);
Atom WMFullscreenAtom = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
XEvent xev = {0}; // The event should be filled with zeros before setting its attributes
xev.type = ClientMessage;
xev.xclient.window = window;