|
| 1 | +/*==LICENSE==* |
| 2 | +
|
| 3 | +CyanWorlds.com Engine - MMOG client, server and tools |
| 4 | +Copyright (C) 2011 Cyan Worlds, Inc. |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +
|
| 19 | +Additional permissions under GNU GPL version 3 section 7 |
| 20 | +
|
| 21 | +If you modify this Program, or any covered work, by linking or |
| 22 | +combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, |
| 23 | +NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent |
| 24 | +JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK |
| 25 | +(or a modified version of those libraries), |
| 26 | +containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, |
| 27 | +PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG |
| 28 | +JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the |
| 29 | +licensors of this Program grant you additional |
| 30 | +permission to convey the resulting work. Corresponding Source for a |
| 31 | +non-source form of such a combination shall include the source code for |
| 32 | +the parts of OpenSSL and IJG JPEG Library used as well as that of the covered |
| 33 | +work. |
| 34 | +
|
| 35 | +You can contact Cyan Worlds, Inc. by email legal@cyan.com |
| 36 | + or by snail mail at: |
| 37 | + Cyan Worlds, Inc. |
| 38 | + 14617 N Newport Hwy |
| 39 | + Mead, WA 99021 |
| 40 | +
|
| 41 | +*==LICENSE==*/ |
| 42 | + |
| 43 | +#include "hsOptionalCall.h" |
| 44 | +#include "plX11DisplayHelper.h" |
| 45 | + |
| 46 | +// X11 includes MUST be after everything else to avoid contamination! |
| 47 | +#include <X11/Xlib.h> |
| 48 | +hsOptionalCallDecl("libX11", XCloseDisplay); |
| 49 | +hsOptionalCallDecl("libX11", XOpenDisplay); |
| 50 | + |
| 51 | +#if __has_include(<X11/extensions/Xrandr.h>) |
| 52 | +# define HAS_XRANDR |
| 53 | +# include <X11/extensions/Xrandr.h> |
| 54 | + hsOptionalCallDecl("libXrandr", XRRSizes); |
| 55 | +#endif |
| 56 | + |
| 57 | +plX11DisplayHelper::plX11DisplayHelper() : fCurrentDisplay() |
| 58 | +{ |
| 59 | +} |
| 60 | + |
| 61 | +plX11DisplayHelper::~plX11DisplayHelper() |
| 62 | +{ |
| 63 | + if (fCurrentDisplay) |
| 64 | + __XCloseDisplay(fCurrentDisplay); |
| 65 | +} |
| 66 | + |
| 67 | +void plX11DisplayHelper::SetCurrentScreen(hsDisplayHndl display) const |
| 68 | +{ |
| 69 | + if (fCurrentDisplay == display) |
| 70 | + return; |
| 71 | + else if (fCurrentDisplay) |
| 72 | + __XCloseDisplay(fCurrentDisplay); |
| 73 | + |
| 74 | + fCurrentDisplay = reinterpret_cast<Display*>(display); |
| 75 | + int screen = DefaultScreen(fCurrentDisplay); |
| 76 | + |
| 77 | + fDisplayModes.clear(); |
| 78 | + |
| 79 | +#ifdef HAS_XRANDR |
| 80 | + if ((bool)__XRRSizes) { |
| 81 | + // We can use the X RandR extension to get resolutions |
| 82 | + int num_sizes; |
| 83 | + XRRScreenSize* xrrs; |
| 84 | + |
| 85 | + xrrs = *__XRRSizes(fCurrentDisplay, 0, &num_sizes); |
| 86 | + for (int i = 0; i < num_sizes; i++) { |
| 87 | + // Don't include unreasonably small sizes |
| 88 | + if (xrrs[i].width < 800 || xrrs[i].height < 600) |
| 89 | + continue; |
| 90 | + |
| 91 | + fDisplayModes.emplace_back(plDisplayMode { |
| 92 | + xrrs[i].width, |
| 93 | + xrrs[i].height, |
| 94 | + DefaultDepth(fCurrentDisplay, screen) |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + else |
| 99 | +#endif |
| 100 | + { |
| 101 | + fDisplayModes.emplace_back(plDisplayMode { |
| 102 | + DisplayWidth(fCurrentDisplay, screen), |
| 103 | + DisplayHeight(fCurrentDisplay, screen), |
| 104 | + DefaultDepth(fCurrentDisplay, screen) |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + std::sort(fDisplayModes.begin(), fDisplayModes.end(), |
| 109 | + [](plDisplayMode a, plDisplayMode b) { |
| 110 | + int resolutionA = a.Width * a.Height; |
| 111 | + int resolutionB = b.Width * b.Height; |
| 112 | + return resolutionA > resolutionB; |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +std::vector<plDisplayMode> plX11DisplayHelper::GetSupportedDisplayModes(hsDisplayHndl display, int ColorDepth) const |
| 117 | +{ |
| 118 | + // Cache the current display so we can answer repeat requests quickly. |
| 119 | + // SetCurrentScreen will catch redundant sets. |
| 120 | + SetCurrentScreen(display); |
| 121 | + return fDisplayModes; |
| 122 | +} |
| 123 | + |
| 124 | +hsDisplayHndl plX11DisplayHelper::DefaultDisplay() const |
| 125 | +{ |
| 126 | + if (!fCurrentDisplay) { |
| 127 | + Display* display = *__XOpenDisplay(nullptr); |
| 128 | + SetCurrentScreen(reinterpret_cast<hsDisplayHndl>(display)); |
| 129 | + } |
| 130 | + |
| 131 | + return reinterpret_cast<hsDisplayHndl>(fCurrentDisplay); |
| 132 | +} |
0 commit comments