[5146ce]: xbmc / WinSystemX11.cpp Maximize Restore History

Repo status: analyzing...

Download this file

WinSystemX11.cpp    446 lines (380 with data), 12.5 kB

  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
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* 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, 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 XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#ifdef HAS_GLX
#include <SDL/SDL_syswm.h>
#include "WinSystemX11.h"
#include "SpecialProtocol.h"
#include "Settings.h"
#include "Texture.h"
#include "utils/log.h"
#include "linux/XRandR.h"
#include <vector>
using namespace std;
CWinSystemX11::CWinSystemX11() : CWinSystemBase(), m_screensaverReset(true)
{
m_eWindowSystem = WINDOW_SYSTEM_X11;
m_glContext = NULL;
m_SDLSurface = NULL;
m_dpy = NULL;
}
CWinSystemX11::~CWinSystemX11()
{
}
bool CWinSystemX11::InitWindowSystem()
{
if ((m_dpy = XOpenDisplay(NULL)))
{
SDL_EnableUNICODE(1);
// set repeat to 10ms to ensure repeat time < frame time
// so that hold times can be reliably detected
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 10);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
return CWinSystemBase::InitWindowSystem();
}
else
CLog::Log(LOGERROR, "GLX Error: No Display found");
return false;
}
bool CWinSystemX11::DestroyWindowSystem()
{
#if defined(HAS_XRANDR)
//restore videomode on exit
if (m_bFullScreen)
g_xrandr.RestoreState();
#endif
if (m_dpy)
{
if (m_glContext)
{
glXMakeCurrent(m_dpy, None, NULL);
glXDestroyContext(m_dpy, m_glContext);
}
m_glContext = 0;
//we don't call XCloseDisplay() here, since ati keeps a pointer to our m_dpy
//so instead we just let m_dpy die on exit
}
// m_SDLSurface is free()'d by SDL_Quit().
return true;
}
bool CWinSystemX11::CreateNewWindow(const CStdString& name, bool fullScreen, RESOLUTION_INFO& res, PHANDLE_EVENT_FUNC userFunction)
{
RESOLUTION_INFO& desktop = g_settings.m_ResInfo[RES_DESKTOP];
if (fullScreen &&
(res.iWidth != desktop.iWidth || res.iHeight != desktop.iHeight ||
res.fRefreshRate != desktop.fRefreshRate || res.iScreen != desktop.iScreen))
{
//on the first call to SDL_SetVideoMode, SDL stores the current displaymode
//SDL restores the displaymode on SDL_QUIT(), if we change the displaymode
//before the first call to SDL_SetVideoMode, SDL changes the displaymode back
//to the wrong mode on exit
CLog::Log(LOGINFO, "CWinSystemX11::CreateNewWindow initializing to desktop resolution first");
if (!SetFullScreen(true, desktop, false))
return false;
}
if(!SetFullScreen(fullScreen, res, false))
return false;
CTexture iconTexture;
iconTexture.LoadFromFile("special://xbmc/media/icon.png");
SDL_WM_SetIcon(SDL_CreateRGBSurfaceFrom(iconTexture.GetPixels(), iconTexture.GetWidth(), iconTexture.GetHeight(), 32, iconTexture.GetPitch(), 0xff0000, 0x00ff00, 0x0000ff, 0xff000000L), NULL);
SDL_WM_SetCaption("XBMC Media Center", NULL);
m_bWindowCreated = true;
return true;
}
bool CWinSystemX11::DestroyWindow()
{
return true;
}
bool CWinSystemX11::ResizeWindow(int newWidth, int newHeight, int newLeft, int newTop)
{
if(m_nWidth == newWidth
&& m_nHeight == newHeight)
return true;
m_nWidth = newWidth;
m_nHeight = newHeight;
int options = SDL_OPENGL;
if (m_bFullScreen)
options |= SDL_FULLSCREEN;
else
options |= SDL_RESIZABLE;
if ((m_SDLSurface = SDL_SetVideoMode(m_nWidth, m_nHeight, 0, options)))
{
RefreshGlxContext();
return true;
}
return false;
}
bool CWinSystemX11::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays)
{
m_nWidth = res.iWidth;
m_nHeight = res.iHeight;
m_bFullScreen = fullScreen;
#if defined(HAS_XRANDR)
XOutput out;
XMode mode;
out.name = res.strOutput;
mode.w = res.iWidth;
mode.h = res.iHeight;
mode.hz = res.fRefreshRate;
mode.id = res.strId;
if(m_bFullScreen)
g_xrandr.SetMode(out, mode);
else
g_xrandr.RestoreState();
#endif
int options = SDL_OPENGL;
if (m_bFullScreen)
options |= SDL_FULLSCREEN;
else
options |= SDL_RESIZABLE;
if ((m_SDLSurface = SDL_SetVideoMode(m_nWidth, m_nHeight, 0, options)))
{
if ((m_SDLSurface->flags & SDL_OPENGL) != SDL_OPENGL)
CLog::Log(LOGERROR, "CWinSystemX11::SetFullScreen SDL_OPENGL not set, SDL_GetError:%s", SDL_GetError());
RefreshGlxContext();
return true;
}
return false;
}
void CWinSystemX11::UpdateResolutions()
{
CWinSystemBase::UpdateResolutions();
#if defined(HAS_XRANDR)
if(g_xrandr.Query())
{
XOutput out = g_xrandr.GetCurrentOutput();
XMode mode = g_xrandr.GetCurrentMode(out.name);
UpdateDesktopResolution(g_settings.m_ResInfo[RES_DESKTOP], 0, mode.w, mode.h, mode.hz);
g_settings.m_ResInfo[RES_DESKTOP].strId = mode.id;
g_settings.m_ResInfo[RES_DESKTOP].strOutput = out.name;
}
else
#endif
{
int x11screen = DefaultScreen(m_dpy);
int w = DisplayWidth(m_dpy, x11screen);
int h = DisplayHeight(m_dpy, x11screen);
UpdateDesktopResolution(g_settings.m_ResInfo[RES_DESKTOP], 0, w, h, 0.0);
}
#if defined(HAS_XRANDR)
CLog::Log(LOGINFO, "Available videomodes (xrandr):");
vector<XOutput>::iterator outiter;
vector<XOutput> outs;
outs = g_xrandr.GetModes();
CLog::Log(LOGINFO, "Number of connected outputs: %"PRIdS"", outs.size());
string modename = "";
for (outiter = outs.begin() ; outiter != outs.end() ; outiter++)
{
XOutput out = *outiter;
vector<XMode>::iterator modeiter;
CLog::Log(LOGINFO, "Output '%s' has %"PRIdS" modes", out.name.c_str(), out.modes.size());
for (modeiter = out.modes.begin() ; modeiter!=out.modes.end() ; modeiter++)
{
XMode mode = *modeiter;
CLog::Log(LOGINFO, "ID:%s Name:%s Refresh:%f Width:%d Height:%d",
mode.id.c_str(), mode.name.c_str(), mode.hz, mode.w, mode.h);
RESOLUTION_INFO res;
res.iWidth = mode.w;
res.iHeight = mode.h;
if (mode.h>0 && mode.w>0 && out.hmm>0 && out.wmm>0)
res.fPixelRatio = ((float)out.wmm/(float)mode.w) / (((float)out.hmm/(float)mode.h));
else
res.fPixelRatio = 1.0f;
CLog::Log(LOGINFO, "Pixel Ratio: %f", res.fPixelRatio);
res.strMode.Format("%s: %s @ %.2fHz", out.name.c_str(), mode.name.c_str(), mode.hz);
res.strOutput = out.name;
res.strId = mode.id;
res.iSubtitles = (int)(0.95*mode.h);
res.fRefreshRate = mode.hz;
res.bFullScreen = true;
if ((float)mode.w / (float)mode.h >= 1.59)
res.dwFlags = D3DPRESENTFLAG_WIDESCREEN;
else
res.dwFlags = 0;
g_graphicsContext.ResetOverscan(res);
g_settings.m_ResInfo.push_back(res);
}
}
#endif
}
bool CWinSystemX11::IsSuitableVisual(XVisualInfo *vInfo)
{
int value;
if (glXGetConfig(m_dpy, vInfo, GLX_RGBA, &value) || !value)
return false;
if (glXGetConfig(m_dpy, vInfo, GLX_DOUBLEBUFFER, &value) || !value)
return false;
if (glXGetConfig(m_dpy, vInfo, GLX_RED_SIZE, &value) || value < 8)
return false;
if (glXGetConfig(m_dpy, vInfo, GLX_GREEN_SIZE, &value) || value < 8)
return false;
if (glXGetConfig(m_dpy, vInfo, GLX_BLUE_SIZE, &value) || value < 8)
return false;
if (glXGetConfig(m_dpy, vInfo, GLX_ALPHA_SIZE, &value) || value < 8)
return false;
if (glXGetConfig(m_dpy, vInfo, GLX_DEPTH_SIZE, &value) || value < 8)
return false;
return true;
}
bool CWinSystemX11::RefreshGlxContext()
{
bool retVal = false;
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if (SDL_GetWMInfo(&info) <= 0)
{
CLog::Log(LOGERROR, "Failed to get window manager info from SDL");
return false;
}
if(m_glWindow == info.info.x11.window && m_glContext)
{
CLog::Log(LOGERROR, "GLX: Same window as before, refreshing context");
glXMakeCurrent(m_dpy, None, NULL);
glXMakeCurrent(m_dpy, m_glWindow, m_glContext);
return true;
}
XVisualInfo vMask;
XVisualInfo *visuals;
XVisualInfo *vInfo = NULL;
int availableVisuals = 0;
vMask.screen = DefaultScreen(m_dpy);
XWindowAttributes winAttr;
m_glWindow = info.info.x11.window;
m_wmWindow = info.info.x11.wmwindow;
/* Assume a depth of 24 in case the below calls to XGetWindowAttributes()
or XGetVisualInfo() fail. That shouldn't happen unless something is
fatally wrong, but lets prepare for everything. */
vMask.depth = 24;
if (XGetWindowAttributes(m_dpy, m_glWindow, &winAttr))
{
vMask.visualid = XVisualIDFromVisual(winAttr.visual);
vInfo = XGetVisualInfo(m_dpy, VisualScreenMask | VisualIDMask, &vMask, &availableVisuals);
if (!vInfo)
CLog::Log(LOGWARNING, "Failed to get VisualInfo of SDL visual 0x%x", (unsigned) vInfo->visualid);
else if(!IsSuitableVisual(vInfo))
{
CLog::Log(LOGWARNING, "Visual 0x%x of the SDL window is not suitable, looking for another one...",
(unsigned) vInfo->visualid);
vMask.depth = vInfo->depth;
XFree(vInfo);
vInfo = NULL;
}
}
else
CLog::Log(LOGWARNING, "Failed to get SDL window attributes");
/* As per glXMakeCurrent documentation, we have to use the same visual as
m_glWindow. Since that was not suitable for use, we try to use another
one with the same depth and hope that the used implementation is less
strict than the documentation. */
if (!vInfo)
{
visuals = XGetVisualInfo(m_dpy, VisualScreenMask | VisualDepthMask, &vMask, &availableVisuals);
for (int i = 0; i < availableVisuals; i++)
{
if (IsSuitableVisual(&visuals[i]))
{
vMask.visualid = visuals[i].visualid;
vInfo = XGetVisualInfo(m_dpy, VisualScreenMask | VisualIDMask, &vMask, &availableVisuals);
break;
}
}
XFree(visuals);
}
if (vInfo)
{
CLog::Log(LOGNOTICE, "Using visual 0x%x", (unsigned) vInfo->visualid);
if (m_glContext)
{
glXMakeCurrent(m_dpy, None, NULL);
glXDestroyContext(m_dpy, m_glContext);
}
if ((m_glContext = glXCreateContext(m_dpy, vInfo, NULL, True)))
{
// make this context current
glXMakeCurrent(m_dpy, m_glWindow, m_glContext);
retVal = true;
}
else
CLog::Log(LOGERROR, "GLX Error: Could not create context");
XFree(vInfo);
}
else
CLog::Log(LOGERROR, "GLX Error: vInfo is NULL!");
return retVal;
}
void CWinSystemX11::ShowOSMouse(bool show)
{
SDL_ShowCursor(show ? 1 : 0);
}
void CWinSystemX11::ResetX11Screensaver()
{
if (m_bFullScreen)
{
//disallow the screensaver when we're fullscreen by periodically calling XResetScreenSaver(),
//normally SDL does this but we disable that in CApplication::Create()
//for some reason setting a 0 timeout with XSetScreenSaver doesn't work with gnome
if (!m_screensaverReset.IsRunning() || m_screensaverReset.GetElapsedSeconds() > 5.0f)
{
m_screensaverReset.StartZero();
XResetScreenSaver(m_dpy);
//need to flush the output buffer, since we don't check for events on m_dpy
XFlush(m_dpy);
}
}
else
{
m_screensaverReset.Stop();
}
}
void CWinSystemX11::NotifyAppActiveChange(bool bActivated)
{
if (bActivated && m_bWasFullScreenBeforeMinimize && !g_graphicsContext.IsFullScreenRoot())
g_graphicsContext.ToggleFullScreenRoot();
}
bool CWinSystemX11::Minimize()
{
m_bWasFullScreenBeforeMinimize = g_graphicsContext.IsFullScreenRoot();
if (m_bWasFullScreenBeforeMinimize)
g_graphicsContext.ToggleFullScreenRoot();
SDL_WM_IconifyWindow();
return true;
}
bool CWinSystemX11::Restore()
{
return false;
}
bool CWinSystemX11::Hide()
{
XUnmapWindow(m_dpy, m_wmWindow);
XSync(m_dpy, False);
return true;
}
bool CWinSystemX11::Show(bool raise)
{
XMapWindow(m_dpy, m_wmWindow);
XSync(m_dpy, False);
return true;
}
#endif