| 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/Lib/ddio.h $ |
| 21 | * $Revision: 30 $ |
| 22 | * $Date: 7/12/99 6:44p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Device Dependent IO system header |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/Lib/ddio.h $ |
| 28 | * |
| 29 | * 30 7/12/99 6:44p Jeff |
| 30 | * added directory lock file API functions |
| 31 | * |
| 32 | * 29 4/24/99 5:41p Samir |
| 33 | * moved key to ascii, ascii to key to the ddio_common library. |
| 34 | * |
| 35 | * 28 4/22/99 2:03a Jeff |
| 36 | * pass ddio_init_info through to keyboard handlers |
| 37 | * |
| 38 | * 27 4/01/99 11:26a Samir |
| 39 | * ddio_MouseGetCaps returns the button mask containing valid buttons. |
| 40 | * |
| 41 | * 26 3/23/99 9:05p Samir |
| 42 | * implemented mouse z axis (for Win32, mouse wheel.) and added functions |
| 43 | * to get binding text values. |
| 44 | * |
| 45 | * 25 2/21/99 6:38p Samir |
| 46 | * mouse and key input better. buffered mouse. |
| 47 | * |
| 48 | * 24 2/16/99 5:24p Kevin |
| 49 | * Converted timer to use 64bit int. |
| 50 | * |
| 51 | * 23 2/05/99 1:16p Samir |
| 52 | * reset low level keys when flushing keyboard in the high level. added a |
| 53 | * function to the low level to reset the status of a key, called from |
| 54 | * high level key flush. |
| 55 | * |
| 56 | * 22 2/04/99 11:19a Kevin |
| 57 | * Added function to find a CD drive letter based on it's volume name |
| 58 | * |
| 59 | * 21 1/25/99 6:47p Samir |
| 60 | * allow slow keyboard |
| 61 | * |
| 62 | * 20 1/25/99 11:02a Samir |
| 63 | * revamped mouse and key controls. |
| 64 | * |
| 65 | * 19 12/08/98 12:17p Jeff |
| 66 | * added 3 functions (similar to ddio_FindFiles) for finding directories |
| 67 | * in a path |
| 68 | * |
| 69 | * 18 11/19/98 5:40p Kevin |
| 70 | * Demo system |
| 71 | * |
| 72 | * 17 10/21/98 12:02p Samir |
| 73 | * properly update odd keys when they are released throug |
| 74 | * ddio_KeyGetDownTime. |
| 75 | * |
| 76 | * 16 10/20/98 12:58a Jeff |
| 77 | * added a way to force a lo-resolution timer |
| 78 | * |
| 79 | * 15 10/16/98 11:55a Kevin |
| 80 | * Made dlls loadable in a hog |
| 81 | * |
| 82 | * 14 8/19/98 2:18p Jeff |
| 83 | * changed ddio_CleanPath |
| 84 | * |
| 85 | * 13 8/15/98 3:42p Matt |
| 86 | * Added new function, ddio_GetFullPath() |
| 87 | * |
| 88 | * 12 7/29/98 5:38p Jeff |
| 89 | * added ddio functions to get parent path, and to clean a path name into |
| 90 | * the real path |
| 91 | * |
| 92 | * 11 7/28/98 6:05p Jeff |
| 93 | * added functions to get root directories (aka drives in windows/dos) |
| 94 | * |
| 95 | * 10 7/28/98 5:05p Samir |
| 96 | * added drive retrieval functions for Jeff. |
| 97 | * |
| 98 | * 9 7/01/98 4:55p Samir |
| 99 | * new simplified mouse system. |
| 100 | * |
| 101 | * 8 6/29/98 6:46p Samir |
| 102 | * MouseInit no longer takes in required number of mouse buttons. |
| 103 | * |
| 104 | * 7 3/19/98 3:18p Samir |
| 105 | * enforce constant char* arguments when needed. done in CFILE and bitmap |
| 106 | * libraries as well as ddio. |
| 107 | * |
| 108 | * 6 2/25/98 6:11p Samir |
| 109 | * Added functions to better deal with key flushing. |
| 110 | * |
| 111 | * 5 1/02/98 12:52p Samir |
| 112 | * Added function to translate keycode to ascii values. |
| 113 | * |
| 114 | * 4 10/16/97 2:29p Samir |
| 115 | * Changed DirectInput Keyboard to FOREGROUND |
| 116 | * |
| 117 | * 3 8/15/97 6:31p Samir |
| 118 | * Added findfile functions. |
| 119 | * |
| 120 | * 2 7/28/97 2:54p Samir |
| 121 | * Temp fix for absolute mouse position return values (Uses GetCursorPos.) |
| 122 | * Added Show and Hide cursor too. |
| 123 | * |
| 124 | * 1 6/23/97 9:25p Samir |
| 125 | * added because source safe sucks |
| 126 | * |
| 127 | * 26 6/11/97 1:07p Samir |
| 128 | * The removal of gameos and replaced with oeApplication, oeDatabase |
| 129 | * |
| 130 | * 25 5/12/97 1:21p Samir |
| 131 | * Commented out timer hook function calls. |
| 132 | * |
| 133 | * 24 5/08/97 1:55p Samir |
| 134 | * Moved a bunch of functions to ddio_common.h |
| 135 | * |
| 136 | * 23 5/06/97 4:23p Samir |
| 137 | * Ha ha. Jeremy's fault.... |
| 138 | * |
| 139 | * 22 5/6/97 4:24 PM Jeremy |
| 140 | * fixed a bug in ddio_MakePath |
| 141 | * |
| 142 | * 21 5/6/97 3:05 PM Jeremy |
| 143 | * added prototype for ddio_makepath |
| 144 | * |
| 145 | * 20 5/6/97 12:06 PM Jeremy |
| 146 | * changed comments to reflect convention for split path to be in local |
| 147 | * file system syntax |
| 148 | * |
| 149 | * 19 5/6/97 11:45 AM Jeremy |
| 150 | * changed split path to only look for path, filename, extension instead |
| 151 | * of including directory name as well, added prototypes for save/restore |
| 152 | * working dir and changed conventions for relative/absolute nature of |
| 153 | * directory calls |
| 154 | * |
| 155 | * 18 4/25/97 6:17p Jason |
| 156 | * added ddio_Deletefile function |
| 157 | * |
| 158 | * 17 4/16/97 11:32a Samir |
| 159 | * Added z axis support. |
| 160 | * |
| 161 | * 16 4/03/97 4:34p Jason |
| 162 | * added CopyFileTime to the cfile, ddio libs |
| 163 | * |
| 164 | * 15 3/20/97 11:08a Samir |
| 165 | * Added function to peek for keys in queue without removing them. |
| 166 | * |
| 167 | * 14 3/14/97 6:54 PM Jeremy |
| 168 | * added prototypes of ddio_FileDiff, ddio_GetFileLength, ddio_SplitPath |
| 169 | * |
| 170 | * 13 3/13/97 11:09a Samir |
| 171 | * Moved file stuff from gameos to ddio library |
| 172 | * |
| 173 | * 12 1/30/97 6:08p Samir |
| 174 | * Reflect new osObject type instead of gameos_object |
| 175 | * |
| 176 | * 11 1/23/97 2:23p Samir |
| 177 | * Added some more keyboard constants |
| 178 | * |
| 179 | * 10 1/20/97 3:48p Samir |
| 180 | * RCS check in |
| 181 | * |
| 182 | * $NoKeywords: $ |
| 183 | */ |
| 184 | |
| 185 | // ---------------------------------------------------------------------------- |
| 186 | // Device Dependent IO System Main Library Interface - functions here are located |
| 187 | // in the ddio_??? system where ??? is the os. |
| 188 | // ---------------------------------------------------------------------------- |
| 189 | |
| 190 | #ifndef DDIO_H |
| 191 | #define DDIO_H |
| 192 | |
| 193 | #include <cstdio> |
| 194 | #include <cstdint> |
| 195 | #include <filesystem> |
| 196 | |
| 197 | #include "chrono_timer.h" |
| 198 | #include "ddio_common.h" |
| 199 | #include "pstypes.h" |
| 200 | |
| 201 | // ---------------------------------------------------------------------------- |
| 202 | // Initialization and destruction functions |
| 203 | // ---------------------------------------------------------------------------- |
| 204 | |
| 205 | // preemptive = 1 for thread based IO. Initializes the IO system |
| 206 | bool ddio_InternalInit(ddio_init_info *init_info); |
| 207 | void ddio_InternalClose(); |
| 208 | |
| 209 | // ---------------------------------------------------------------------------- |
| 210 | // Keyboard Interface |
| 211 | // ---------------------------------------------------------------------------- |
| 212 | |
| 213 | // used by ddio system to initialize machine specific key code. |
| 214 | bool ddio_InternalKeyInit(ddio_init_info *init_info); |
| 215 | void ddio_InternalKeyClose(); |
| 216 | |
| 217 | // handled internally if keyboard system needs additional processing per frame |
| 218 | void ddio_InternalKeyFrame(); |
| 219 | void ddio_InternalKeySuspend(); |
| 220 | void ddio_InternalKeyResume(); |
| 221 | |
| 222 | // returns if key is up or down |
| 223 | bool ddio_InternalKeyState(uint8_t key); |
| 224 | |
| 225 | // returns internal key down time |
| 226 | float ddio_InternalKeyDownTime(uint8_t key); |
| 227 | |
| 228 | // flush a key internally |
| 229 | void ddio_InternalResetKey(uint8_t key); |
| 230 | |
| 231 | // ---------------------------------------------------------------------------- |
| 232 | // Device Dependent Timer Interface |
| 233 | // ---------------------------------------------------------------------------- |
| 234 | |
| 235 | #define timer_Init D3::ChronoTimer::Initialize |
| 236 | #define timer_GetTime D3::ChronoTimer::GetTime |
| 237 | #define timer_GetMSTime D3::ChronoTimer::GetTimeMS |
| 238 | |
| 239 | // ---------------------------------------------------------------------------- |
| 240 | // Device Dependent Mouse Interface |
| 241 | // ---------------------------------------------------------------------------- |
| 242 | |
| 243 | #define MOUSE_LB 1 // mouse button masks |
| 244 | #define MOUSE_RB 2 |
| 245 | #define MOUSE_CB 4 |
| 246 | #define MOUSE_B4 8 |
| 247 | #define MOUSE_B5 16 |
| 248 | #define MOUSE_B6 32 |
| 249 | #define MOUSE_B7 64 |
| 250 | #define MOUSE_B8 128 |
| 251 | |
| 252 | #define N_MSEBTNS 8 |
| 253 | |
| 254 | // initializes mouse. |
| 255 | bool ddio_MouseInit(); |
| 256 | void ddio_MouseClose(); |
| 257 | |
| 258 | // get device caps |
| 259 | int ddio_MouseGetCaps(int *btn, int *axis); |
| 260 | |
| 261 | // use these extensions to set exclusive or standard mouse modes |
| 262 | #define MOUSE_STANDARD_MODE 1 // uses absolute coordinates and simple buttons |
| 263 | #define MOUSE_EXCLUSIVE_MODE 2 // uses relative coordinates and advanced button information |
| 264 | void ddio_MouseMode(int mode); |
| 265 | |
| 266 | // resets position of mouse to center, resets virtual coord system to equal screen coordinate system |
| 267 | void ddio_MouseReset(); |
| 268 | |
| 269 | // resets mouse queue and button info only. |
| 270 | void ddio_MouseQueueFlush(); |
| 271 | |
| 272 | // handled internally if mouse system needs additional processing per frame |
| 273 | void ddio_InternalMouseFrame(); |
| 274 | |
| 275 | // used to prevent mouse input from being registered |
| 276 | void ddio_InternalMouseSuspend(); |
| 277 | void ddio_InternalMouseResume(); |
| 278 | |
| 279 | // returns absolute position of mouse, button state and mouse deltas. |
| 280 | /* x, y = absolute mouse position |
| 281 | dx, dy = mouse deltas since last call |
| 282 | return value is mouse button mask. values not needed should pass NULL pointers. |
| 283 | */ |
| 284 | int ddio_MouseGetState(int *x, int *y, int *dx, int *dy, int *z = NULL, int *dz = NULL); |
| 285 | |
| 286 | // gets a mouse button event, returns false if none. |
| 287 | bool ddio_MouseGetEvent(int *btn, bool *state); |
| 288 | |
| 289 | // returns string to binding. |
| 290 | const char *ddio_MouseGetBtnText(int btn); |
| 291 | const char *ddio_MouseGetAxisText(int axis); |
| 292 | |
| 293 | // return mouse button down time. |
| 294 | float ddio_MouseBtnDownTime(int btn); |
| 295 | |
| 296 | // return mouse button down time |
| 297 | int ddio_MouseBtnDownCount(int btn); |
| 298 | |
| 299 | // return mouse button up count |
| 300 | int ddio_MouseBtnUpCount(int btn); |
| 301 | |
| 302 | // set bounds for system polling of coordinates |
| 303 | void ddio_MouseSetLimits(int left, int top, int right, int bottom, int zmin = 0, int zmax = 0); |
| 304 | void ddio_MouseGetLimits(int *left, int *top, int *right, int *bottom, int *zmin = 0, int *zmax = 0); |
| 305 | |
| 306 | // virtual coordinate system for mouse (match to video resolution set for optimal mouse usage. |
| 307 | void ddio_MouseSetVCoords(int width, int height); |
| 308 | |
| 309 | // --------------------------------------------------------------------------- |
| 310 | // File Operations |
| 311 | // --------------------------------------------------------------------------- |
| 312 | |
| 313 | // Gets the full path of the executable file |
| 314 | bool ddio_GetBinaryPath(char *exec_path, size_t len); |
| 315 | |
| 316 | // deletes a file. Returns 1 if successful, 0 on failure |
| 317 | // This pathname is *RELATIVE* not fully qualified |
| 318 | int ddio_DeleteFile(const char *name); |
| 319 | |
| 320 | // retrieve the current working folder where file operation will occur. |
| 321 | // Note ---> The path in Get/Set working directory is in the *LOCAL* file system's syntax |
| 322 | // This pathname is relative *OR* fully qualified |
| 323 | void ddio_GetWorkingDir(char *path, int len); |
| 324 | bool ddio_SetWorkingDir(const char *path); |
| 325 | |
| 326 | // get a file length of a FILE |
| 327 | int ddio_GetFileLength(FILE *filePtr); |
| 328 | |
| 329 | // check if two files are different |
| 330 | // This pathname is *RELATIVE* not fully qualified |
| 331 | bool ddio_FileDiff(const std::filesystem::path &path1, const std::filesystem::path &path2); |
| 332 | |
| 333 | // copies one files timestamp to another |
| 334 | void ddio_CopyFileTime(const std::filesystem::path &dest, const std::filesystem::path &src); |
| 335 | |
| 336 | // Split a pathname into its component parts |
| 337 | // The path in splitpath is in the *LOCAL* file system's syntax |
| 338 | void ddio_SplitPath(const char *srcPath, char *path, char *filename, char *ext); |
| 339 | |
| 340 | // pass in a pathname (could be from ddio_SplitPath), root_path will have the drive name. |
| 341 | void ddio_GetRootFromPath(const char *srcPath, char *root_path); |
| 342 | |
| 343 | // retrieve root names, free up roots array (allocated with malloc) after use |
| 344 | int ddio_GetFileSysRoots(char **roots, int max_roots); |
| 345 | |
| 346 | // Constructs a path in the local file system's syntax |
| 347 | // builtPath: stores the constructed path |
| 348 | // absolutePathHeader: absolute path on which the sub directories will be appended |
| 349 | // (specified in local file system syntax) |
| 350 | // subdir: the first subdirectory |
| 351 | // takes a variable number of additional subdirectories which will be concatenated on to the path |
| 352 | // the last argument in the list of sub dirs *MUST* be NULL to terminate the list |
| 353 | void ddio_MakePath(char *newPath, const char *absolutePathHeader, const char *subDir, ...); |
| 354 | |
| 355 | // These functions allow one to find a file |
| 356 | // You use FindFileStart by giving it a wildcard (like *.*, *.txt, u??.*, whatever). It returns |
| 357 | // a filename in namebuf. |
| 358 | // Use FindNextFile to get the next file found with the wildcard given in FindFileStart. |
| 359 | // Use FindFileClose to end your search. |
| 360 | bool ddio_FindFileStart(const char *wildcard, char *namebuf); |
| 361 | bool ddio_FindNextFile(char *namebuf); |
| 362 | void ddio_FindFileClose(); |
| 363 | |
| 364 | // given a path (with no filename), it will return the parent path |
| 365 | // srcPath is the source given path |
| 366 | // dest is where the parent path will be placed |
| 367 | // returns true on success |
| 368 | // dest should be at least _MAX_PATH in length |
| 369 | bool ddio_GetParentPath(char *dest, const char *srcPath); |
| 370 | |
| 371 | // given a path, it cleans it up (if the path is c:\windows\..\dos it would make it c:\dos) |
| 372 | // srcPath is the original path |
| 373 | // dest is the finished cleaned path. |
| 374 | // dest should be at least _MAX_PATH in size |
| 375 | void ddio_CleanPath(char *dest, const char *srcPath); |
| 376 | |
| 377 | // Finds a full path from a relative path |
| 378 | // Parameters: full_path - filled in with the fully-specified path. Buffer must be at least _MAX_PATH bytes long |
| 379 | // rel_path - a path specification, either relative or absolute |
| 380 | // Returns TRUE if successful, FALSE if an error |
| 381 | bool ddio_GetFullPath(char *full_path, const char *rel_path); |
| 382 | |
| 383 | // Generates a temporary filename based on the prefix, and basedir |
| 384 | // Parameters: |
| 385 | // basedir - directory to put the files |
| 386 | // prefix - prefix for the temp filename |
| 387 | // filename - buffer to hold generated filename (must be at least _MAX_PATH in length) |
| 388 | // |
| 389 | // Returns TRUE if successful, FALSE if an error |
| 390 | bool ddio_GetTempFileName(const char *basedir, const char *prefix, char *filename); |
| 391 | |
| 392 | /** |
| 393 | * Check process existence by PID |
| 394 | * @param pid PID of requested process |
| 395 | * @return true if process exists, false otherwise |
| 396 | */ |
| 397 | bool ddio_CheckProcess(int pid); |
| 398 | |
| 399 | /** |
| 400 | * Get PID of current process |
| 401 | * @return |
| 402 | */ |
| 403 | int ddio_GetPID(); |
| 404 | |
| 405 | /** |
| 406 | * Creates a lock file in the specified directory. Before creation function |
| 407 | * checks if lockfile already created by another process. |
| 408 | * @param dir Directory for which the lock file should be created in |
| 409 | * @return true if lock file successfully created, false otherwise (unable to |
| 410 | * create, lock file already created by another process etc). |
| 411 | */ |
| 412 | bool ddio_CreateLockFile(const std::filesystem::path& dir); |
| 413 | |
| 414 | /** |
| 415 | * Deletes a lock file (for the current process) in the specified directory |
| 416 | * @param dir Directory for which the lock file should be deleted from |
| 417 | * @return true if lock file successfully deleted, false otherwise |
| 418 | */ |
| 419 | bool ddio_DeleteLockFile(const std::filesystem::path& dir); |
| 420 | |
| 421 | #endif |