| 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/ddio_lnx/lnxio.cpp $ |
| 21 | * $Revision: 1.6 $ |
| 22 | * $Date: 2004/02/25 00:04:06 $ |
| 23 | * $Author: ryan $ |
| 24 | * |
| 25 | * Linux IO routines |
| 26 | * |
| 27 | * $Log: lnxio.cpp,v $ |
| 28 | * Revision 1.6 2004/02/25 00:04:06 ryan |
| 29 | * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete). |
| 30 | * |
| 31 | * Revision 1.5 2000/05/29 05:20:13 icculus |
| 32 | * Removed a real repetitive mprintf(). |
| 33 | * |
| 34 | * Revision 1.4 2000/05/29 05:19:17 icculus |
| 35 | * Serial behaviour is now correct (and the Rock'n'Ride works!). A |
| 36 | * little more debugging code was added. |
| 37 | * |
| 38 | * Revision 1.3 2000/04/25 06:10:27 icculus |
| 39 | * Added buttloads of debug information, and fixed some more glitches. |
| 40 | * |
| 41 | * Revision 1.2 2000/04/24 03:18:53 icculus |
| 42 | * Serial port i/o code written, so I can ROCK'n'RIDE it! |
| 43 | * |
| 44 | * Revision 1.1.1.1 2000/04/18 00:00:33 icculus |
| 45 | * initial checkin |
| 46 | * |
| 47 | * |
| 48 | * 5 7/14/99 9:06p Jeff |
| 49 | * added comment header |
| 50 | * |
| 51 | * $NoKeywords: $ |
| 52 | */ |
| 53 | |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | // Linux IO System Main Library Interface |
| 56 | // ---------------------------------------------------------------------------- |
| 57 | |
| 58 | #include <cstdarg> |
| 59 | #include <cstdio> |
| 60 | |
| 61 | #ifdef MACOSX |
| 62 | #include <mach-o/dyld.h> |
| 63 | #endif |
| 64 | |
| 65 | #ifdef WIN32 |
| 66 | #include <windows.h> |
| 67 | #endif |
| 68 | |
| 69 | #include "application.h" |
| 70 | #include "ddio.h" |
| 71 | #include "pserror.h" |
| 72 | |
| 73 | bool DDIO_init = false; |
| 74 | oeLnxApplication *Lnx_app_obj = NULL; |
| 75 | |
| 76 | // ---------------------------------------------------------------------------- |
| 77 | // Initialization and destruction functions |
| 78 | // ---------------------------------------------------------------------------- |
| 79 | |
| 80 | bool ddio_InternalInit(ddio_init_info *init_info) { |
| 81 | mprintf(0, "DDIO: ddio_InternalInit() called."); |
| 82 | Lnx_app_obj = (oeLnxApplication *)init_info->obj; |
| 83 | DDIO_init = true; |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | void ddio_InternalClose() { |
| 88 | mprintf(0, "DDIO: ddio_InternalClose() called."); |
| 89 | |
| 90 | if (DDIO_init) { |
| 91 | DDIO_init = false; |
| 92 | Lnx_app_obj = NULL; |
| 93 | } // if |
| 94 | |
| 95 | mprintf(0, "DDIO: ddio_InternalClose() returning."); |
| 96 | } |
| 97 | |
| 98 | void ddio_DebugMessage(unsigned err, char *fmt, ...) { |
| 99 | char buf[128]; |
| 100 | va_list arglist; |
| 101 | |
| 102 | va_start(arglist, fmt); |
| 103 | std::vsnprintf(buf, sizeof(buf), fmt, arglist); |
| 104 | va_end(arglist); |
| 105 | |
| 106 | mprintf(0, "%s\n", buf); |
| 107 | } |
| 108 | |
| 109 | bool ddio_GetBinaryPath(char *exec_path, size_t len) { |
| 110 | #ifdef MACOSX |
| 111 | if (exec_path == NULL || len == 0) { |
| 112 | fprintf(stderr, "Invalid arguments\n"); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | uint32_t size = (uint32_t)len; |
| 117 | if (_NSGetExecutablePath(exec_path, &size) != 0) { |
| 118 | fprintf(stderr, "Buffer too small; need size %u\n", size); |
| 119 | return false; |
| 120 | } |
| 121 | #elif defined(__LINUX__) |
| 122 | if (realpath("/proc/self/exe", exec_path) == NULL) { |
| 123 | perror("realpath"); |
| 124 | return false; |
| 125 | } |
| 126 | #else |
| 127 | if (GetModuleFileName(NULL, exec_path, len) == 0) { |
| 128 | DWORD error = GetLastError(); |
| 129 | Error("GetModuleFileName failed!"); |
| 130 | return false; |
| 131 | } |
| 132 | exec_path[len - 1] = '\0'; |
| 133 | return true; |
| 134 | |
| 135 | #endif |
| 136 | exec_path[len - 1] = '\0'; |
| 137 | return true; |
| 138 | } |