| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // Operating system specific functions |
|---|
| 34 | // |
|---|
| 35 | // Author: J. van Wijk |
|---|
| 36 | // |
|---|
| 37 | // JvW 24-07-2005 Initial version, split off from TXUTIL |
|---|
| 38 | |
|---|
| 39 | #include <txlib.h> |
|---|
| 40 | |
|---|
| 41 | #if defined (DOS32) |
|---|
| 42 | |
|---|
| 43 | /*****************************************************************************/ |
|---|
| 44 | // Wait for specified nr of msec (approximation, +/- 32 msec) |
|---|
| 45 | /*****************************************************************************/ |
|---|
| 46 | void TxBusyWait |
|---|
| 47 | ( |
|---|
| 48 | ULONG msec // IN nr of msec to wait |
|---|
| 49 | ) |
|---|
| 50 | { |
|---|
| 51 | ULONG t1 = (ULONG) clock(); // start time |
|---|
| 52 | ULONG t2; |
|---|
| 53 | |
|---|
| 54 | ENTER(); |
|---|
| 55 | |
|---|
| 56 | do |
|---|
| 57 | { |
|---|
| 58 | t2 = (ULONG) clock(); |
|---|
| 59 | } while (t2 <= (t1 + msec)); |
|---|
| 60 | |
|---|
| 61 | VRETURN(); |
|---|
| 62 | } // end 'TxBusyWait' |
|---|
| 63 | /*---------------------------------------------------------------------------*/ |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | /*****************************************************************************/ |
|---|
| 67 | // Set keyboard mapping using FreeDOS keyb and .kl files, optional codepage |
|---|
| 68 | /*****************************************************************************/ |
|---|
| 69 | ULONG TxSetNlsKeyboard // RET result |
|---|
| 70 | ( |
|---|
| 71 | char *spec, // IN keyb file basename |
|---|
| 72 | char *cp // IN codepage string or "" |
|---|
| 73 | ) |
|---|
| 74 | { |
|---|
| 75 | ULONG rc = NO_ERROR; // function return |
|---|
| 76 | TXLN line; // full-path to kl file |
|---|
| 77 | TXTM keyb; // kl filename / keyb cmd |
|---|
| 78 | FILE *kl; // .kl file |
|---|
| 79 | |
|---|
| 80 | ENTER(); |
|---|
| 81 | |
|---|
| 82 | sprintf( keyb, "key\\%s.kl", spec); |
|---|
| 83 | if ((kl = TxFindAndOpenFile( keyb, "PATH", line)) == NULL) |
|---|
| 84 | { |
|---|
| 85 | sprintf( keyb, "%s.kl", spec); |
|---|
| 86 | kl = TxFindAndOpenFile( keyb, "PATH", line); |
|---|
| 87 | } |
|---|
| 88 | if (kl != NULL) |
|---|
| 89 | { |
|---|
| 90 | fclose( kl); |
|---|
| 91 | TxExternalCommand( "keyb /u"); // unload |
|---|
| 92 | sprintf( keyb, "keyb %2.2s,%s,%s", spec, cp, line); |
|---|
| 93 | TxExternalCommand( keyb); // set new |
|---|
| 94 | TxExternalCommand( "keyb"); // show |
|---|
| 95 | } |
|---|
| 96 | else |
|---|
| 97 | { |
|---|
| 98 | rc = ERROR_FILE_NOT_FOUND; |
|---|
| 99 | } |
|---|
| 100 | RETURN (rc); |
|---|
| 101 | } // end 'TxSetNlsKeyboard' |
|---|
| 102 | /*---------------------------------------------------------------------------*/ |
|---|
| 103 | |
|---|
| 104 | /*****************************************************************************/ |
|---|
| 105 | // Allocate DPMI compatible memory |
|---|
| 106 | /*****************************************************************************/ |
|---|
| 107 | void *txDpmiAlloc // RET PM linear address |
|---|
| 108 | ( |
|---|
| 109 | size_t nr, // IN number of items |
|---|
| 110 | size_t size, // IN size per item |
|---|
| 111 | short *selector // OUT PM selector |
|---|
| 112 | ) |
|---|
| 113 | { |
|---|
| 114 | void *rc = NULL; |
|---|
| 115 | union REGS regs; |
|---|
| 116 | struct SREGS sreg; |
|---|
| 117 | |
|---|
| 118 | ENTER(); |
|---|
| 119 | TRARGS(("nr of items: %d size: %d\n", nr, size)); |
|---|
| 120 | |
|---|
| 121 | if ((nr != 0) && (size != 0)) |
|---|
| 122 | { |
|---|
| 123 | memset( ®s, 0, sizeof(regs)); |
|---|
| 124 | memset( &sreg, 0, sizeof(sreg)); |
|---|
| 125 | |
|---|
| 126 | regs.w.ax = TXDX_DPMI_ALLOC; |
|---|
| 127 | regs.w.bx = (USHORT) (((ULONG) (nr * size) -1) / 16) +1; // 16 byte paragraphs |
|---|
| 128 | |
|---|
| 129 | TRACES(("Alloc DPMI memory, paragraphs: %4.4hx\n", regs.w.bx)); |
|---|
| 130 | |
|---|
| 131 | txDpmiCall( ®s, &sreg) |
|---|
| 132 | |
|---|
| 133 | if (regs.x.cflag == 0) |
|---|
| 134 | { |
|---|
| 135 | *selector = regs.w.dx; |
|---|
| 136 | rc = (char *) (regs.w.ax << 4); |
|---|
| 137 | } |
|---|
| 138 | TRACES(("seg: %4.4hx sel: %4.4hx cflag: %hu\n", |
|---|
| 139 | regs.w.ax, regs.w.dx, regs.x.cflag)); |
|---|
| 140 | } |
|---|
| 141 | RETURN (rc); |
|---|
| 142 | } // end 'txDpmiAlloc' |
|---|
| 143 | /*---------------------------------------------------------------------------*/ |
|---|
| 144 | |
|---|
| 145 | /*****************************************************************************/ |
|---|
| 146 | // Free DPMI compatible memory |
|---|
| 147 | /*****************************************************************************/ |
|---|
| 148 | void txDpmiFree |
|---|
| 149 | ( |
|---|
| 150 | short selector // IN PM selector |
|---|
| 151 | ) |
|---|
| 152 | { |
|---|
| 153 | union REGS regs; |
|---|
| 154 | struct SREGS sreg; |
|---|
| 155 | |
|---|
| 156 | ENTER(); |
|---|
| 157 | |
|---|
| 158 | if (selector != 0) |
|---|
| 159 | { |
|---|
| 160 | memset( ®s, 0, sizeof(regs)); |
|---|
| 161 | memset( &sreg, 0, sizeof(sreg)); |
|---|
| 162 | |
|---|
| 163 | TRACES(("free DPMI memory, selector: %4.4hx\n", selector)); |
|---|
| 164 | regs.w.ax = TXDX_DPMI_FREEM; |
|---|
| 165 | regs.w.dx = selector; |
|---|
| 166 | txDpmiCall( ®s, &sreg) |
|---|
| 167 | } |
|---|
| 168 | VRETURN(); |
|---|
| 169 | } // end 'txDpmiFree' |
|---|
| 170 | /*---------------------------------------------------------------------------*/ |
|---|
| 171 | |
|---|
| 172 | #elif defined (WIN32) |
|---|
| 173 | // no WIN specific functions yet |
|---|
| 174 | |
|---|
| 175 | #elif defined (LINUX) |
|---|
| 176 | |
|---|
| 177 | /*****************************************************************************/ |
|---|
| 178 | // Sleep for specified nr of msec |
|---|
| 179 | /*****************************************************************************/ |
|---|
| 180 | void TxSleepMsec |
|---|
| 181 | ( |
|---|
| 182 | ULONG msec // IN nr of msec to wait |
|---|
| 183 | ) |
|---|
| 184 | { |
|---|
| 185 | struct timespec requested; |
|---|
| 186 | struct timespec remaining; // after EINTR from a signal |
|---|
| 187 | |
|---|
| 188 | ENTER(); |
|---|
| 189 | |
|---|
| 190 | requested.tv_sec = msec / 1000; |
|---|
| 191 | requested.tv_nsec = (msec % 1000) * 1000000; |
|---|
| 192 | |
|---|
| 193 | nanosleep( &requested, &remaining); |
|---|
| 194 | } // end 'TxSleepMsec' |
|---|
| 195 | /*--------------------------------------------------------------------------- */ |
|---|
| 196 | |
|---|
| 197 | /*************************************************************************************************/ |
|---|
| 198 | // Translate (Linux) errno value to corresponding DFSee + DOS/OS2/WIN like RC |
|---|
| 199 | /*************************************************************************************************/ |
|---|
| 200 | ULONG TxRcFromErrno // RET TXW return-code |
|---|
| 201 | ( |
|---|
| 202 | int err // IN error number (errno) |
|---|
| 203 | ) |
|---|
| 204 | { |
|---|
| 205 | ULONG rc; // function return |
|---|
| 206 | |
|---|
| 207 | ENTER(); |
|---|
| 208 | |
|---|
| 209 | switch (errno) |
|---|
| 210 | { |
|---|
| 211 | case 0: rc = NO_ERROR; break; |
|---|
| 212 | case ENOENT: rc = ERROR_FILE_NOT_FOUND; break; |
|---|
| 213 | case EACCES: rc = ERROR_ACCESS_DENIED; break; |
|---|
| 214 | case EEXIST: rc = ERROR_FILE_EXISTS; break; |
|---|
| 215 | case EMFILE: case ENFILE: rc = ERROR_TOO_MANY_OPEN_FILES; break; |
|---|
| 216 | case ENXIO: case ENODEV: |
|---|
| 217 | case ENOTDIR: case EISDIR: rc = ERROR_INVALID_DRIVE; break; |
|---|
| 218 | case EFBIG: rc = ERROR_WRITE_FAULT; break; |
|---|
| 219 | case ENOSPC: rc = ERROR_DISK_FULL; break; |
|---|
| 220 | case EINVAL: rc = ERROR_INVALID_PARAMETER; break; |
|---|
| 221 | case ENAMETOOLONG: rc = ERROR_FILENAME_EXCED_RANGE; break; |
|---|
| 222 | case EBADF: rc = ERROR_INVALID_HANDLE; break; |
|---|
| 223 | case ENOSYS: rc = ERROR_INVALID_PARAMETER; break; |
|---|
| 224 | case ENOMEM: rc = TX_ALLOC_ERROR; break; |
|---|
| 225 | default: rc = ERROR_GEN_FAILURE; break; |
|---|
| 226 | } |
|---|
| 227 | TRACES(( "Translated errno value: %d to RC: %lu\n", errno, rc)); |
|---|
| 228 | RETURN (rc); |
|---|
| 229 | } // end 'TxRcFromErrno' |
|---|
| 230 | /*-----------------------------------------------------------------------------------------------*/ |
|---|
| 231 | |
|---|
| 232 | #else |
|---|
| 233 | static TXF_OS2LFAPI txfLargeAPIentryp = {NULL, NULL}; |
|---|
| 234 | static BOOL txfLargeAPItested = FALSE; |
|---|
| 235 | |
|---|
| 236 | #define TXF_DOSCALLS ((PSZ) "doscalls") // name of dll with DOS calls |
|---|
| 237 | #define TXF_DOSOPENL_ORDINAL ((ULONG) 981) |
|---|
| 238 | #define TXF_DOSSEEKL_ORDINAL ((ULONG) 988) |
|---|
| 239 | |
|---|
| 240 | /*****************************************************************************/ |
|---|
| 241 | // Test if OS2 large-file support (> 2GiB) is available; Fill entrypoints |
|---|
| 242 | /*****************************************************************************/ |
|---|
| 243 | BOOL TxLargeFileApiOS2 // RET large file API's OK |
|---|
| 244 | ( |
|---|
| 245 | TXF_OS2LFAPI *entrypoints // OUT LF-API entrypoints |
|---|
| 246 | ) // or NULL |
|---|
| 247 | { |
|---|
| 248 | BOOL rc = FALSE; // function return |
|---|
| 249 | TXLN dlmerror; // one line of data |
|---|
| 250 | HMODULE doscalls = 0; // handle DOSCALLS dll |
|---|
| 251 | ULONG dr; // API return code |
|---|
| 252 | |
|---|
| 253 | if (txfLargeAPItested == FALSE) |
|---|
| 254 | { |
|---|
| 255 | if ((DosLoadModule( dlmerror, TXMAXLN, TXF_DOSCALLS, &doscalls)) == NO_ERROR) |
|---|
| 256 | { |
|---|
| 257 | TRACES(("Txf DOSCALLS module handle: %lu\n", doscalls)); |
|---|
| 258 | |
|---|
| 259 | dr = DosQueryProcAddr( doscalls, TXF_DOSOPENL_ORDINAL, NULL, |
|---|
| 260 | (PFN *) &(txfLargeAPIentryp.DosOpenLarge)); |
|---|
| 261 | |
|---|
| 262 | TRACES(( "Txf DosOpenL, ordinal %lu, rc: %lu fn:%8.8lx\n", |
|---|
| 263 | TXF_DOSOPENL_ORDINAL, dr, txfLargeAPIentryp.DosOpenLarge)); |
|---|
| 264 | |
|---|
| 265 | dr = DosQueryProcAddr( doscalls, TXF_DOSSEEKL_ORDINAL, NULL, |
|---|
| 266 | (PFN *) &(txfLargeAPIentryp.DosSeekLarge)); |
|---|
| 267 | |
|---|
| 268 | TRACES(( "DosSetFilePtrL, ordinal %lu, rc: %lu fn:%8.8lx\n", |
|---|
| 269 | TXF_DOSSEEKL_ORDINAL, dr, txfLargeAPIentryp.DosSeekLarge)); |
|---|
| 270 | } |
|---|
| 271 | else |
|---|
| 272 | { |
|---|
| 273 | TRACES(("Txf Error %lu loading DOSCALLS DLL: '%s'\n", rc, TXF_DOSCALLS)); |
|---|
| 274 | } |
|---|
| 275 | txfLargeAPItested = TRUE; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | if (entrypoints != NULL) |
|---|
| 279 | { |
|---|
| 280 | *entrypoints = txfLargeAPIentryp; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if ((txfLargeAPIentryp.DosOpenLarge != NULL) && |
|---|
| 284 | (txfLargeAPIentryp.DosSeekLarge != NULL) ) |
|---|
| 285 | { |
|---|
| 286 | rc = TRUE; |
|---|
| 287 | } |
|---|
| 288 | return( rc); |
|---|
| 289 | } // end 'TxLargeFileApiOS2' |
|---|
| 290 | /*---------------------------------------------------------------------------*/ |
|---|
| 291 | #endif |
|---|