| 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/linux/lnxapp.cpp $ |
| 21 | * $Revision: 1.3 $ |
| 22 | * $Date: 2004/02/25 00:04:06 $ |
| 23 | * $Author: ryan $ |
| 24 | * |
| 25 | * Linux application routines |
| 26 | * |
| 27 | * $Log: lnxapp.cpp,v $ |
| 28 | * Revision 1.3 2004/02/25 00:04:06 ryan |
| 29 | * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete). |
| 30 | * |
| 31 | * Revision 1.2 2000/04/28 20:18:27 icculus |
| 32 | * Replaced X stuff with SDL stuff. |
| 33 | * |
| 34 | * Revision 1.1.1.1 2000/04/18 00:00:39 icculus |
| 35 | * initial checkin |
| 36 | * |
| 37 | * |
| 38 | * 25 10/19/99 8:35p Jeff |
| 39 | * added a couple functions for Window OpenGL |
| 40 | * |
| 41 | * 24 10/17/99 3:55p Jeff |
| 42 | * |
| 43 | * 23 10/15/99 12:14p Jeff |
| 44 | * dyna load pthread library |
| 45 | * |
| 46 | * 22 9/24/99 8:26p Jeff |
| 47 | * better cleanup (including restoring the mouse) in case of a crash or |
| 48 | * error exit |
| 49 | * |
| 50 | * 21 9/09/99 4:35p Jeff |
| 51 | * dynamically load the Outrage Xlib extension library (for DGA). Init |
| 52 | * DGA if needed for Mouse control |
| 53 | * |
| 54 | * 20 9/07/99 4:37p Jeff |
| 55 | * |
| 56 | * 19 9/06/99 9:22p Jeff |
| 57 | * set initial window position to 0,0 |
| 58 | * |
| 59 | * 18 9/05/99 9:44p Jeff |
| 60 | * handle window option flag |
| 61 | * |
| 62 | * 17 7/15/99 11:37a Jeff |
| 63 | * turn on/off autorepeat |
| 64 | * |
| 65 | * 16 7/14/99 9:09p Jeff |
| 66 | * added comment header |
| 67 | * |
| 68 | * $NoKeywords: $ |
| 69 | */ |
| 70 | |
| 71 | #include <cstdlib> |
| 72 | #include <cctype> |
| 73 | #if defined(POSIX) |
| 74 | #include <sys/time.h> |
| 75 | #include <termios.h> |
| 76 | #else |
| 77 | #include "winsock.h" |
| 78 | #endif |
| 79 | |
| 80 | #include "application.h" |
| 81 | #include "lnxapp.h" |
| 82 | |
| 83 | #ifdef buttons // termios.h defines buttons, but SDL's headers use that symbol. |
| 84 | #undef buttons |
| 85 | #endif |
| 86 | |
| 87 | #if defined(POSIX) |
| 88 | static struct termios Linux_initial_terminal_settings; |
| 89 | #endif |
| 90 | |
| 91 | bool oeLnxApplication::os_initialized = false; |
| 92 | bool oeLnxApplication::first_time = true; |
| 93 | |
| 94 | bool con_Create(int flags); |
| 95 | void con_Destroy(); |
| 96 | void con_Defer(); |
| 97 | |
| 98 | void Sleep(int millis) { |
| 99 | struct timeval tv{}; |
| 100 | tv.tv_sec = 0; |
| 101 | tv.tv_usec = millis * 1000; |
| 102 | select(0, nullptr, nullptr, nullptr, &tv); |
| 103 | } |
| 104 | |
| 105 | char *strupr(char *string) { |
| 106 | while (string && *string) { |
| 107 | *string = toupper(*string); |
| 108 | string++; |
| 109 | } |
| 110 | return string; |
| 111 | } |
| 112 | |
| 113 | static uint32_t LinuxAppFlags = 0; |
| 114 | // static Display *LinuxAppDisplay=NULL; |
| 115 | static bool LinuxAppSetAtExit = false; |
| 116 | static bool LinuxAppDontCallShutdown = false; |
| 117 | |
| 118 | void LnxAppShutdown() { |
| 119 | if (LinuxAppDontCallShutdown) |
| 120 | return; |
| 121 | LinuxAppDontCallShutdown = true; |
| 122 | if (LinuxAppFlags & OEAPP_CONSOLE) { |
| 123 | con_Destroy(); |
| 124 | #if defined(POSIX) |
| 125 | tcsetattr(0, TCSANOW, &Linux_initial_terminal_settings); |
| 126 | #endif |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Creates the application object |
| 131 | oeLnxApplication::oeLnxApplication(unsigned flags) { |
| 132 | m_Flags = flags; |
| 133 | m_AppActive = true; |
| 134 | |
| 135 | if (flags & OEAPP_CONSOLE) { |
| 136 | #if defined(POSIX) |
| 137 | tcgetattr(0, &Linux_initial_terminal_settings); |
| 138 | #endif |
| 139 | con_Create(m_Flags); |
| 140 | } |
| 141 | |
| 142 | LinuxAppFlags = m_Flags; |
| 143 | |
| 144 | if (!LinuxAppSetAtExit) { |
| 145 | LinuxAppSetAtExit = true; |
| 146 | atexit(LnxAppShutdown); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Create object with a premade info |
| 151 | oeLnxApplication::oeLnxApplication(tLnxAppInfo *appinfo) { |
| 152 | #if defined(POSIX) |
| 153 | tcgetattr(0, &Linux_initial_terminal_settings); |
| 154 | #endif |
| 155 | m_Flags = appinfo->flags; |
| 156 | m_X = appinfo->wnd_x; |
| 157 | m_Y = appinfo->wnd_y; |
| 158 | m_W = appinfo->wnd_w; |
| 159 | m_H = appinfo->wnd_h; |
| 160 | m_AppActive = true; |
| 161 | |
| 162 | if (m_Flags & OEAPP_CONSOLE) { |
| 163 | con_Create(m_Flags); |
| 164 | } |
| 165 | |
| 166 | LinuxAppFlags = m_Flags; |
| 167 | |
| 168 | if (!LinuxAppSetAtExit) { |
| 169 | LinuxAppSetAtExit = true; |
| 170 | atexit(LnxAppShutdown); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Destructor |
| 175 | oeLnxApplication::~oeLnxApplication() { LnxAppShutdown(); } |
| 176 | |
| 177 | // initializes the object |
| 178 | void oeLnxApplication::init() { |
| 179 | if (m_WasCreated) { |
| 180 | // Create graphics window and prepare for graphics! |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Function to retrieve information from object through a platform defined structure. |
| 185 | void oeLnxApplication::get_info(void *info) { |
| 186 | tLnxAppInfo *appinfo = (tLnxAppInfo *)info; |
| 187 | appinfo->flags = m_Flags; |
| 188 | appinfo->wnd_x = m_X; |
| 189 | appinfo->wnd_y = m_Y; |
| 190 | appinfo->wnd_w = m_W; |
| 191 | appinfo->wnd_h = m_H; |
| 192 | } |
| 193 | |
| 194 | // defer returns some flags. essentially this function defers program control to OS. |
| 195 | unsigned oeLnxApplication::defer() { |
| 196 | bool res = 0; |
| 197 | |
| 198 | if (m_DeferFunc) { |
| 199 | res = 1; |
| 200 | (*m_DeferFunc)(true); |
| 201 | } |
| 202 | |
| 203 | con_Defer(); |
| 204 | |
| 205 | return res; |
| 206 | } |
| 207 | |
| 208 | // set a function to run when deferring to OS. |
| 209 | void oeLnxApplication::set_defer_handler(void (*func)(bool)) { m_DeferFunc = func; } |
| 210 | |
| 211 | // delays app for a certain amount of time |
| 212 | void oeLnxApplication::delay(float secs) { |
| 213 | int msecs = (int)(secs * 1000.0f); |
| 214 | Sleep(msecs); |
| 215 | } |
| 216 | |
| 217 | // Function to get the flags |
| 218 | int oeLnxApplication::flags() const { return m_Flags; } |
| 219 | |
| 220 | // Sizes the displayable region of the app (the window) |
| 221 | void oeLnxApplication::set_sizepos(int x, int y, int w, int h) { |
| 222 | m_X = x; |
| 223 | m_Y = y; |
| 224 | m_W = w; |
| 225 | m_H = h; |
| 226 | } |
| 227 | |
| 228 | const char *oeLnxApplication::get_window_name(void) { return "Descent 3"; } |
| 229 | |
| 230 | void oeLnxApplication::clear_window(void) { |
| 231 | } |
| 232 | |
| 233 | // initializes OS components. |
| 234 | void oeLnxApplication::os_init() { |
| 235 | /* We only need to do this once */ |
| 236 | if (!os_initialized) { |
| 237 | os_initialized = true; |
| 238 | } |
| 239 | } |