| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/renderer/lnxscreenmode.cpp $ |
| 21 | * $Revision: 1.6 $ |
| 22 | * $Date: 2000/06/29 18:48:13 $ |
| 23 | * $Author: icculus $ |
| 24 | * |
| 25 | * |
| 26 | * |
| 27 | * $Log: lnxscreenmode.cpp,v $ |
| 28 | * Revision 1.6 2000/06/29 18:48:13 icculus |
| 29 | * Dumb typo fixed. |
| 30 | * |
| 31 | * Revision 1.5 2000/06/29 06:41:24 icculus |
| 32 | * mad commits. |
| 33 | * |
| 34 | * Revision 1.4 2000/06/24 01:15:15 icculus |
| 35 | * patched to compile. |
| 36 | * |
| 37 | * Revision 1.3 2000/05/29 05:25:57 icculus |
| 38 | * Command line fix: Changed "--nofullscreen" to "--windowed" (or "-w"). |
| 39 | * |
| 40 | * Revision 1.2 2000/04/28 20:25:51 icculus |
| 41 | * Updates, cleanups, and SDLification. |
| 42 | * |
| 43 | * Revision 1.1.1.1 2000/04/18 00:00:49 icculus |
| 44 | * initial checkin |
| 45 | * |
| 46 | * |
| 47 | * 4 9/09/99 4:38p Jeff |
| 48 | * fixed crashes if lib not loaded |
| 49 | * |
| 50 | * 3 9/06/99 9:21p Jeff |
| 51 | * lock video mode switching, don't set viewport when restoring (causing a |
| 52 | * bunch of screen garbage) |
| 53 | * |
| 54 | * 2 9/05/99 9:41p Jeff |
| 55 | * first checkin of Linux Screen manager |
| 56 | * |
| 57 | * $NoKeywords: $ |
| 58 | */ |
| 59 | |
| 60 | #include <stdio.h> |
| 61 | #include <stdlib.h> |
| 62 | #include <string.h> |
| 63 | #include "lnxscreenmode.h" |
| 64 | #include "pserror.h" |
| 65 | #include "args.h" |
| 66 | |
| 67 | CLnxVideoModes LinuxVideoMode; |
| 68 | |
| 69 | /* |
| 70 | typedef Bool (*VidModeQueryExtension_fp)(Display* dpy,int* event_base,int* error_base); |
| 71 | typedef Bool (*VidModeGetAllModeLines_fp)(Display* dpy,int screen,int* modecount,XF86VidModeModeInfo*** modelinesPtr); |
| 72 | typedef Bool (*VidModeSwitchToMode_fp)(Display* dpy,int screen,XF86VidModeModeInfo* modeline); |
| 73 | typedef Bool (*VidModeSetViewPort_fp)(Display* dpy,int screen,int x,int y); |
| 74 | typedef void (*VidModeFreeData_fp)(int num_modes,XF86VidModeModeInfo** modelinePtr); |
| 75 | typedef Bool (*VidModeGetModeLine_fp)(Display* dpy,int screen,int* dotclock,XF86VidModeModeLine* modeline); |
| 76 | typedef Bool (*VidModeLockModeSwitch_fp)(Display* dpy,int screen,int lock); |
| 77 | |
| 78 | VidModeQueryExtension_fp VidModeQueryExtension; |
| 79 | VidModeGetAllModeLines_fp VidModeGetAllModeLines; |
| 80 | VidModeSwitchToMode_fp VidModeSwitchToMode; |
| 81 | VidModeSetViewPort_fp VidModeSetViewPort; |
| 82 | VidModeFreeData_fp VidModeFreeData; |
| 83 | VidModeGetModeLine_fp VidModeGetModeLine; |
| 84 | VidModeLockModeSwitch_fp VidModeLockModeSwitch; |
| 85 | */ |
| 86 | |
| 87 | CLnxVideoModes::CLnxVideoModes() { |
| 88 | m_Inited = false; |
| 89 | m_NumVideoModes = 0; |
| 90 | m_ModeInfoList = NULL; |
| 91 | // m_Display = NULL; |
| 92 | m_VideoResolutionChanged = false; |
| 93 | /* |
| 94 | VidModeQueryExtension = NULL; |
| 95 | VidModeGetAllModeLines = NULL; |
| 96 | VidModeSwitchToMode = NULL; |
| 97 | VidModeSetViewPort = NULL; |
| 98 | VidModeFreeData = NULL; |
| 99 | VidModeGetModeLine = NULL; |
| 100 | VidModeLockModeSwitch = NULL; |
| 101 | */ |
| 102 | } |
| 103 | |
| 104 | CLnxVideoModes::~CLnxVideoModes() { |
| 105 | if (!m_Inited) |
| 106 | return; |
| 107 | |
| 108 | // shutdown and free the memory |
| 109 | RestoreVideoMode(); |
| 110 | |
| 111 | // VidModeFreeData(m_NumVideoModes,m_ModeInfoList); |
| 112 | } |
| 113 | |
| 114 | // enumerate all the possible video modes |
| 115 | bool CLnxVideoModes::Init(void) // Display *dpy,int screen) |
| 116 | { |
| 117 | #if 0 // with SDL2, we'll just use FULLSCREEN_DESKTOP and never change the physical vidmode |
| 118 | if (m_Inited) |
| 119 | return true; |
| 120 | |
| 121 | // m_Display = dpy; |
| 122 | // m_Screen = screen; |
| 123 | m_NumVideoModes = 0; |
| 124 | m_ModeInfoList = NULL; |
| 125 | m_Inited = true; |
| 126 | sdlflags = 0; |
| 127 | /* |
| 128 | if(!LoadLibraries()) |
| 129 | { |
| 130 | m_Inited = false; |
| 131 | fprintf(stdout,"LoadLib: Unable to load VideoMode libraries\n"); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | int dummy; |
| 136 | if(!VidModeQueryExtension(dpy,&dummy,&dummy)) |
| 137 | return false; |
| 138 | |
| 139 | if(!VidModeGetAllModeLines(m_Display,m_Screen,&m_NumVideoModes,&m_ModeInfoList)) |
| 140 | { |
| 141 | m_NumVideoModes = 0; |
| 142 | }else |
| 143 | { |
| 144 | */ |
| 145 | |
| 146 | const SDL_VideoInfo *vidInfo = NULL; |
| 147 | |
| 148 | sdlflags = SDL_HWSURFACE | SDL_FULLSCREEN; |
| 149 | sdlflags = SDL_OPENGL | SDL_FULLSCREEN; |
| 150 | if (FindArgChar("-windowed", 'w')) |
| 151 | sdlflags &= ~SDL_FULLSCREEN; |
| 152 | |
| 153 | vidInfo = SDL_GetVideoInfo(); |
| 154 | |
| 155 | m_ModeInfoList = SDL_ListModes(vidInfo->vfmt, sdlflags); |
| 156 | if (m_ModeInfoList == NULL) { |
| 157 | sdlflags &= ~SDL_FULLSCREEN; |
| 158 | m_ModeInfoList = SDL_ListModes(vidInfo->vfmt, sdlflags); // try without fullscreen. |
| 159 | if (m_ModeInfoList == NULL) { |
| 160 | sdlflags = 0; |
| 161 | m_ModeInfoList = SDL_ListModes(vidInfo->vfmt, sdlflags); // give me ANYTHING. |
| 162 | if (m_ModeInfoList == NULL) { |
| 163 | fprintf(stderr, "SDL: SDL_ListModes() failed.\n"); |
| 164 | return -3; |
| 165 | } // if |
| 166 | } // if |
| 167 | } // if |
| 168 | |
| 169 | if (m_ModeInfoList == (SDL_Rect **)-1) // anything is fine. |
| 170 | { |
| 171 | mprintf(0, "Windowed surface is probably going to be used."); |
| 172 | m_NumVideoModes = 0; |
| 173 | } // if |
| 174 | else { |
| 175 | mprintf(0, "-------------------------\n"); |
| 176 | mprintf(0, "Supported Mode Lines:\n"); |
| 177 | for (int i = 0; m_ModeInfoList[i] != NULL; i++) { |
| 178 | //,m_ModeInfoList[i]->hdisplay,m_ModeInfoList[i]->vdisplay); |
| 179 | mprintf(0, " Mode %d: %dX%d\n", i, m_ModeInfoList[i]->w, m_ModeInfoList[i]->h); |
| 180 | m_NumVideoModes++; |
| 181 | } // for |
| 182 | mprintf(0, "-------------------------\n"); |
| 183 | } // else |
| 184 | #endif |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | bool CLnxVideoModes::SwitchResolution(int width, int height) { |
| 190 | /* |
| 191 | if(!m_Inited) |
| 192 | return false; |
| 193 | |
| 194 | int i; |
| 195 | for(i=0;i<m_NumVideoModes;i++) |
| 196 | { |
| 197 | if(m_ModeInfoList[i]->hdisplay==width && m_ModeInfoList[i]->vdisplay==height) |
| 198 | { |
| 199 | // found a matching resolution, try to change to it |
| 200 | if(SwitchToMode(m_ModeInfoList[i])) |
| 201 | { |
| 202 | fprintf(stdout,"Mode switch to %dX%d OK!\n",width,height); |
| 203 | m_VideoResolutionChanged = (i==0)?false:true; |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return false; |
| 210 | */ |
| 211 | return (true); |
| 212 | } |
| 213 | |
| 214 | void CLnxVideoModes::RestoreVideoMode(void) { |
| 215 | /* |
| 216 | if(!m_Inited) |
| 217 | return; |
| 218 | |
| 219 | if(!m_VideoResolutionChanged) |
| 220 | return; |
| 221 | |
| 222 | // index 0 is the original mode |
| 223 | if(m_NumVideoModes<=1) |
| 224 | return; |
| 225 | |
| 226 | m_VideoResolutionChanged = false; |
| 227 | |
| 228 | VidModeSwitchToMode(m_Display,m_Screen,m_ModeInfoList[0]); |
| 229 | */ |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | bool CLnxVideoModes::SwitchToMode(XF86VidModeModeInfo *mode) |
| 235 | { |
| 236 | if(!m_Inited) |
| 237 | return false; |
| 238 | |
| 239 | if(!VidModeSwitchToMode(m_Display,m_Screen,mode)) |
| 240 | { |
| 241 | fprintf(stdout,"Video: Unable to switch mode to %dX%d\n",mode->hdisplay,mode->vdisplay); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | VidModeSetViewPort(m_Display,m_Screen,0,0); |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | bool CLnxVideoModes::QueryExtension(Display *dpy) |
| 250 | { |
| 251 | if(!m_Inited) |
| 252 | return false; |
| 253 | |
| 254 | int dummy; |
| 255 | if(!VidModeQueryExtension(dpy,&dummy,&dummy)) |
| 256 | return false; |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | void CLnxVideoModes::GetModeLine(Display *dpy,int screen,int *dotclock,XF86VidModeModeLine *modeline) |
| 261 | { |
| 262 | if(!m_Inited) |
| 263 | return; |
| 264 | |
| 265 | VidModeGetModeLine(dpy,screen,dotclock,modeline); |
| 266 | } |
| 267 | */ |
| 268 | |
| 269 | void CLnxVideoModes::Lock(bool lock) { |
| 270 | /* |
| 271 | if(!m_Inited) |
| 272 | return; |
| 273 | |
| 274 | VidModeLockModeSwitch(m_Display,m_Screen,(lock)?1:0); |
| 275 | */ |
| 276 | } |
| 277 | |
| 278 | bool CLnxVideoModes::LoadLibraries(void) { |
| 279 | /* |
| 280 | void *handle; |
| 281 | char *libname = "OutrageDynXLib.so"; |
| 282 | handle = dlopen(libname,RTLD_NOW|RTLD_GLOBAL); |
| 283 | if(!handle) |
| 284 | { |
| 285 | fprintf(stderr,"Library Load Failed: %s (%s)\n",libname,dlerror()); |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | VidModeQueryExtension = (VidModeQueryExtension_fp)dlsym(handle,"VidModeQueryExtension"); |
| 290 | VidModeGetAllModeLines = (VidModeGetAllModeLines_fp)dlsym(handle,"VidModeGetAllModeLines"); |
| 291 | VidModeSwitchToMode = (VidModeSwitchToMode_fp)dlsym(handle,"VidModeSwitchToMode"); |
| 292 | VidModeSetViewPort = (VidModeSetViewPort_fp)dlsym(handle,"VidModeSetViewPort"); |
| 293 | VidModeFreeData = (VidModeFreeData_fp)dlsym(handle,"VidModeFreeData"); |
| 294 | VidModeGetModeLine = (VidModeGetModeLine_fp)dlsym(handle,"VidModeGetModeLine"); |
| 295 | VidModeLockModeSwitch = (VidModeLockModeSwitch_fp)dlsym(handle,"VidModeLockModeSwitch"); |
| 296 | |
| 297 | if( !VidModeQueryExtension || |
| 298 | !VidModeGetAllModeLines || |
| 299 | !VidModeSwitchToMode || |
| 300 | !VidModeSetViewPort || |
| 301 | !VidModeFreeData || |
| 302 | !VidModeGetModeLine || |
| 303 | !VidModeLockModeSwitch) |
| 304 | { |
| 305 | fprintf(stderr,"Symbol Resolve Error: %s\n",libname); |
| 306 | return false; |
| 307 | } |
| 308 | */ |
| 309 | return true; |
| 310 | } |