| 1 | static char RCSid[] = |
| 2 | "$Header: d:/cvsroot/tads/TADS2/msdos/LTKWIN.C,v 1.2 1999/05/17 02:52:18 MJRoberts Exp $"; |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1993 by Steve McAdams. All Rights Reserved. |
| 6 | * |
| 7 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 8 | * on using and copying this software. |
| 9 | */ |
| 10 | /* |
| 11 | Name |
| 12 | ltkwin.c - Implementation of the porting Tool Kit for Windows |
| 13 | |
| 14 | Function |
| 15 | ltkini - initialize ltk routines |
| 16 | ltkfre - free ltk context |
| 17 | |
| 18 | ltk_suballoc - suballocate from heap |
| 19 | ltk_sigsuballoc - suballocate from heap (signal failure) |
| 20 | ltk_subfree - free suballocted memory |
| 21 | ltk_alloc - allocate permanent global memory |
| 22 | ltk_sigalloc - allocate permanent global memory (signal failure) |
| 23 | ltk_free - free allocated global memory |
| 24 | |
| 25 | ltk_dlg - print message in dialog box |
| 26 | ltk_errlog - error logging function for LER routines |
| 27 | |
| 28 | ltk_beep - sound a beep |
| 29 | |
| 30 | ltk_beg_wait - begin waiting (use hourglass mouse ptr) |
| 31 | ltk_end_wait - end waiting (revert to normal mouse ptr) |
| 32 | |
| 33 | Notes |
| 34 | |
| 35 | Modified |
| 36 | 02/11/93 SMcAdams - Creation |
| 37 | */ |
| 38 | |
| 39 | #include <stdarg.h> |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | #include <os.h> |
| 44 | #include <ltk.h> |
| 45 | #include <ler.h> |
| 46 | #include <windows.h> |
| 47 | |
| 48 | #ifdef TURBO |
| 49 | # define INLINE asm |
| 50 | #else |
| 51 | # define INLINE _asm |
| 52 | #endif |
| 53 | |
| 54 | |
| 55 | /*--------------------------- PRIVATE DATA TYPES ---------------------------*/ |
| 56 | struct ltk_mblk |
| 57 | { |
| 58 | HANDLE mblkhdl; |
| 59 | ub1 mblkbuf[1]; |
| 60 | }; |
| 61 | typedef struct ltk_mblk ltk_mblk; |
| 62 | |
| 63 | |
| 64 | |
| 65 | /*------------------------------- STATIC DATA ------------------------------*/ |
| 66 | static ub2 ltkseg = 0; /* heap segment */ |
| 67 | static HANDLE ltkhdl = 0; /* heap segment handle */ |
| 68 | |
| 69 | |
| 70 | |
| 71 | /*--------------------------------- ltkini ---------------------------------*/ |
| 72 | /* |
| 73 | * ltkini - allocate and INItialize toolkit context. |
| 74 | */ |
| 75 | void ltkini(unsigned short heapsiz) |
| 76 | { |
| 77 | HANDLE memhdl; /* memory handle */ |
| 78 | HANDLE syshdl; /* sys context handle */ |
| 79 | ub2 segd; /* the segment descriptor */ |
| 80 | void *heap; /* ptr to heap memory */ |
| 81 | |
| 82 | /* allocate and lock heap segment */ |
| 83 | if (!(ltkhdl = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, heapsiz)) || |
| 84 | !(heap = GlobalLock(ltkhdl))) |
| 85 | { |
| 86 | /* no luck allocating memory */ |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * get the actual size allocated. subtract 16 for Windows overhead |
| 92 | * (see doc for LocalInit) |
| 93 | */ |
| 94 | heapsiz = GlobalSize(ltkhdl) - 16; |
| 95 | |
| 96 | /* |
| 97 | * set DS to new segment |
| 98 | */ |
| 99 | ltkseg = HIWORD(heap); /* get the segment */ |
| 100 | INLINE |
| 101 | { |
| 102 | push ds |
| 103 | mov ax, ltkseg |
| 104 | mov ds, ax |
| 105 | } |
| 106 | |
| 107 | /* initialize the heap segment */ |
| 108 | if (!LocalInit(ltkseg, 0, heapsiz)) |
| 109 | { |
| 110 | /* restore DS */ |
| 111 | INLINE |
| 112 | { |
| 113 | pop ds |
| 114 | } |
| 115 | |
| 116 | /* unlock and free the global memory */ |
| 117 | GlobalUnlock(ltkhdl); |
| 118 | GlobalFree(ltkhdl); |
| 119 | |
| 120 | /* can't use heap */ |
| 121 | ltkhdl = 0; |
| 122 | ltkseg = 0; |
| 123 | |
| 124 | /* no more we can do */ |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * restore the data segment |
| 130 | */ |
| 131 | INLINE |
| 132 | { |
| 133 | pop ds |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /*--------------------------------- ltkfre ---------------------------------*/ |
| 139 | /* |
| 140 | * ltkfre - FREe toolkit context. |
| 141 | */ |
| 142 | void ltkfre() |
| 143 | { |
| 144 | /* unlock and free the heap segment */ |
| 145 | GlobalUnlock(ltkhdl); |
| 146 | GlobalFree(ltkhdl); |
| 147 | |
| 148 | /* reset statics */ |
| 149 | ltkhdl = 0; |
| 150 | ltkseg = 0; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /*------------------------------ ltk_suballoc ------------------------------*/ |
| 155 | /* |
| 156 | * ltk_suballoc - SUB ALLOCate memory from heap segment. |
| 157 | */ |
| 158 | void *ltk_suballoc(siz) |
| 159 | ub2 siz; /* size to allocate */ |
| 160 | { |
| 161 | HANDLE memhdl; /* memory handle */ |
| 162 | ltk_mblk *mblk; /* memory block */ |
| 163 | |
| 164 | /* assert that we have a heap segment */ |
| 165 | if (!ltkseg || !ltkhdl) return((void *)0); |
| 166 | |
| 167 | /* verify that we can allocate the requested size */ |
| 168 | if ((ub4)siz + (ub4)sizeof(ltk_mblk) > 0x10000L) return((void *)0); |
| 169 | |
| 170 | /* use the heap's data segment */ |
| 171 | INLINE |
| 172 | { |
| 173 | push ds |
| 174 | mov ax, ltkseg |
| 175 | mov ds, ax |
| 176 | } |
| 177 | |
| 178 | /* allocate and lock a block of the requested size */ |
| 179 | if (!(memhdl = LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT, |
| 180 | sizeof(ltk_mblk)+siz)) || |
| 181 | !(mblk = (ltk_mblk *)LocalLock(memhdl))) |
| 182 | { |
| 183 | /* fix up data segment */ |
| 184 | INLINE |
| 185 | { |
| 186 | pop ds |
| 187 | } |
| 188 | return((void *)0); |
| 189 | } |
| 190 | |
| 191 | /* return to our own data segment */ |
| 192 | INLINE |
| 193 | { |
| 194 | pop ds |
| 195 | } |
| 196 | |
| 197 | /* stash the handle and we're on our way */ |
| 198 | mblk->mblkhdl = memhdl; |
| 199 | return(mblk->mblkbuf); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /*---------------------------- ltk_sigsuballoc -----------------------------*/ |
| 204 | /* |
| 205 | * ltk_sigsuballoc - allocate from heap, signal failure. |
| 206 | */ |
| 207 | void *ltk_sigsuballoc(errcx, siz) |
| 208 | errcxdef *errcx; /* error context to signal with */ |
| 209 | ub2 siz; /* size to allocate */ |
| 210 | { |
| 211 | void *ptr; /* ptr to allocated memory */ |
| 212 | |
| 213 | /* allocate the memory */ |
| 214 | if (!(ptr = ltk_suballoc(siz))) |
| 215 | { |
| 216 | /* signal an error */ |
| 217 | errsigf(errcx, "LTK", 0); |
| 218 | } |
| 219 | |
| 220 | /* return the memory */ |
| 221 | return(ptr); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /*------------------------------ ltk_subfree -------------------------------*/ |
| 226 | /* |
| 227 | * ltk_subfree - FREe memory allocated by ltk_suballoc |
| 228 | */ |
| 229 | void ltk_subfree(ptr) |
| 230 | void *ptr; /* block to free */ |
| 231 | { |
| 232 | ltk_mblk *mblk; /* the memory block */ |
| 233 | |
| 234 | /* assert that we have a heap segment */ |
| 235 | if (!ltkseg || !ltkhdl) return; |
| 236 | |
| 237 | /* get our handle */ |
| 238 | mblk = (ltk_mblk *)((ub1 *)ptr - offsetof(ltk_mblk, mblkbuf)); |
| 239 | |
| 240 | /* use the heap's seg as DS */ |
| 241 | INLINE |
| 242 | { |
| 243 | push ds |
| 244 | mov ax, ltkseg |
| 245 | mov ds, ax |
| 246 | } |
| 247 | |
| 248 | /* unlock and free the memory */ |
| 249 | LocalUnlock(mblk->mblkhdl); |
| 250 | LocalFree(mblk->mblkhdl); |
| 251 | |
| 252 | /* finally, return to our regular DS */ |
| 253 | INLINE |
| 254 | { |
| 255 | pop ds |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /*------------------------------- ltk_alloc --------------------------------*/ |
| 261 | /* |
| 262 | * ltk_alloc - Allocate a block of memory. In windows, the block of |
| 263 | * memory is allocated, along with a place to stash the memory handle, |
| 264 | * so we can free this block up again when the time comes. |
| 265 | */ |
| 266 | void *ltk_alloc(siz) |
| 267 | size_t siz; /* size of block to allocate */ |
| 268 | { |
| 269 | HANDLE memhdl; /* memory handle */ |
| 270 | ltk_mblk *mblk; /* memory block */ |
| 271 | |
| 272 | /* verify that the we can allocate the requested size */ |
| 273 | if ((ub4)siz+(ub4)sizeof(ltk_mblk) >= 0x10000) return((void *)0); |
| 274 | |
| 275 | /* allocate a block of the requested size */ |
| 276 | memhdl = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, sizeof(ltk_mblk)+siz); |
| 277 | if (!memhdl) return((void *)0); |
| 278 | |
| 279 | /* lock the memory, stash the handle, and we're outta here */ |
| 280 | mblk = (ltk_mblk *)GlobalLock(memhdl); |
| 281 | if (!mblk) return((void *)0); |
| 282 | |
| 283 | mblk->mblkhdl = memhdl; |
| 284 | return(mblk->mblkbuf); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /*------------------------------ ltk_sigalloc ------------------------------*/ |
| 289 | /* |
| 290 | * ltk_sigalloc - allocate permanent global memory, and signal on failure. |
| 291 | */ |
| 292 | void *ltk_sigalloc(errcx, siz) |
| 293 | errcxdef *errcx; /* error context */ |
| 294 | size_t siz; /* size to allocate */ |
| 295 | { |
| 296 | void *ptr; /* pointer to allocated memory */ |
| 297 | |
| 298 | if (!(ptr = ltk_alloc(siz))) |
| 299 | { |
| 300 | /* signal error */ |
| 301 | errsigf(errcx, "LTK", 0); |
| 302 | } |
| 303 | |
| 304 | /* return a ptr to the allocated memory */ |
| 305 | return(ptr); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /*-------------------------------- ltk_free --------------------------------*/ |
| 310 | /* |
| 311 | * ltk_free - free a block of memory allocated by ltk_alloc. This |
| 312 | * takes the memory handle stashed just behind the allocation, and frees |
| 313 | * the block of memory. |
| 314 | */ |
| 315 | void ltk_free(mem) |
| 316 | void *mem; /* memory to free */ |
| 317 | { |
| 318 | ltk_mblk *mblk; /* the memory block */ |
| 319 | |
| 320 | /* get our handle */ |
| 321 | mblk = (ltk_mblk *)((ub1 *)mem - offsetof(ltk_mblk, mblkbuf)); |
| 322 | |
| 323 | /* unlock and free the memory, and we're outta here */ |
| 324 | GlobalUnlock(mblk->mblkhdl); |
| 325 | GlobalFree(mblk->mblkhdl); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | #ifndef __DPMI16__ |
| 331 | /*------------------------------- ltk_errlog -------------------------------*/ |
| 332 | /* |
| 333 | * ltk_errlog - ERRor LOGging function. Logs an error from the LER |
| 334 | * system. |
| 335 | */ |
| 336 | void ltk_errlog(ctx, fac, errno, argc, argv) |
| 337 | void *ctx; /* error logger context */ |
| 338 | char *fac; /* facility code */ |
| 339 | int errno; /* error number */ |
| 340 | int argc; /* argument count */ |
| 341 | erradef *argv; /* arguments */ |
| 342 | { |
| 343 | char buf[128]; /* formatted error buffer */ |
| 344 | char msg[128]; /* message buffer */ |
| 345 | |
| 346 | /* $$$ filter out error #504 $$$ */ |
| 347 | if (errno == 504) return; |
| 348 | |
| 349 | /* get the error message into msg */ |
| 350 | errmsg(ctx, msg, sizeof(msg), errno); |
| 351 | |
| 352 | /* format the error message */ |
| 353 | errfmt(buf, (int)sizeof(buf), msg, argc, argv); |
| 354 | |
| 355 | /* display a dialog box containing the error message */ |
| 356 | ltk_dlg("Error", buf); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /*-------------------------------- ltk_dlg ---------------------------------*/ |
| 361 | /* |
| 362 | * ltk_dlg - DiaLog. Puts the given message in a dialog box. |
| 363 | */ |
| 364 | void ltk_dlg(char *title, char *msg, ...) |
| 365 | #ifdef NEVER |
| 366 | char *title; /* message box title */ |
| 367 | char *msg; /* error message text */ |
| 368 | #endif |
| 369 | { |
| 370 | va_list argp; /* printf args */ |
| 371 | char inbuf[80]; /* input buffer */ |
| 372 | char outbuf[160]; /* allow inbuf to double in size */ |
| 373 | |
| 374 | /* clip the input message, if necessary */ |
| 375 | strncpy(inbuf, msg, sizeof(inbuf)); |
| 376 | inbuf[sizeof(inbuf)-1] = '\0'; |
| 377 | |
| 378 | /* get the printf args, build the message, and display it */ |
| 379 | va_start(argp, msg); |
| 380 | vsprintf(outbuf, inbuf, argp); |
| 381 | |
| 382 | /* display the message */ |
| 383 | MessageBox((HWND)NULL, outbuf, title, |
| 384 | MB_TASKMODAL | MB_ICONEXCLAMATION | MB_OK); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /*-------------------------------- ltk_beep --------------------------------*/ |
| 389 | /* |
| 390 | * ltk_beep - BEEP the PC's speaker. |
| 391 | */ |
| 392 | void ltk_beep() |
| 393 | { |
| 394 | MessageBeep(MB_ICONEXCLAMATION); |
| 395 | } |
| 396 | |
| 397 | /*------------------------------ ltk_beg_wait ------------------------------*/ |
| 398 | /* |
| 399 | * ltk_beg_wait - Put up hourglass prompt. |
| 400 | */ |
| 401 | void ltk_beg_wait() |
| 402 | { |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /*------------------------------ ltk_end_wait ------------------------------*/ |
| 407 | /* |
| 408 | * ltk_end_wait - Put up normal prompt. |
| 409 | */ |
| 410 | void ltk_end_wait() |
| 411 | { |
| 412 | } |
| 413 | |
| 414 | #endif /* __DPMI16__ */ |