int sqShrinkMemoryBy(int oldLimit, int delta) { /* round delta DOWN to page size */ if(fShowAllocations) { warnPrintf("Shrinking by %d...",delta); } #ifdef DO_NOT_SHRINK { /* Experimental - do not unmap memory and avoid OGL crashes */ if(fShowAllocations) warnPrintf(" - ignored\n"); return oldLimit; } #endif delta &= pageMask; if(!VirtualFree(pageLimit-delta, delta, MEM_DECOMMIT)) { if(fShowAllocations) { warnPrintf("failed\n"); } /* failed to shrink */ return oldLimit; } /* otherwise, shrink pageLimit and return new top limit */ if(fShowAllocations) { warnPrintf("okay\n"); } pageLimit -= delta; usedMemory -= delta; return (int)pageLimit; }
/* win32JoystickDebugPrintRawValues: Print the raw values of a readout of the specified joystick. */ EXPORT(int) win32JoystickDebugPrintRawValues(void) { int index, err; JOYINFO info; if(methodArgumentCount() != 1) return primitiveFail(); index = stackIntegerValue(0); if(failed()) return 0; if(index < 1 || index > 2) return primitiveFail(); warnPrintf(TEXT("Raw joystick values (%d):\n"), index); err = joyGetPos(index-1, &info); if(err != JOYERR_NOERROR) { if(err == MMSYSERR_NODRIVER) warnPrintf(TEXT("\t<no driver present>\n\n")); else if(err == MMSYSERR_INVALPARAM) warnPrintf(TEXT("\t<invalid param in joyGetPos()>\n\n")); else if(err == MMSYSERR_BADDEVICEID) warnPrintf(TEXT("\t<bad device id>\n\n")); else if(err == JOYERR_UNPLUGGED) warnPrintf(TEXT("\t<joystick unplugged>\n\n")); else warnPrintf(TEXT("\t<unknown error: %d>\n\n"), err); } else { warnPrintf(TEXT("\tX: %d\n"), info.wXpos); warnPrintf(TEXT("\tY: %d\n"), info.wYpos); warnPrintf(TEXT("\tZ: %d\n"), info.wZpos); warnPrintf(TEXT("\tButtons: %x\n"), info.wButtons); } pop(1); /* Leave rcvr on stack */ return 1; }
int asyncFileClose(AsyncFile *f) { /* Close the given asynchronous file. */ AsyncFileState *state; if (!asyncFileValid(f)) return 0; /* already closed */ state = (AsyncFileState*) f->state; if(!CloseHandle(state->hFile)) { printLastError(TEXT("AsyncFileClose failed")); success(false); } state->hFile = INVALID_HANDLE_VALUE; SetEvent(state->hEvent); WaitForSingleObject(state->hThread, 5000); if(state->hThread) { warnPrintf(TEXT("Terminating async thread")); TerminateThread(state->hThread,0); } CloseHandle(state->hEvent); if (state->bufferPtr != NULL) asyncFree(state->bufferPtr); free((void *) f->state); f->state = NULL; f->sessionID = 0; return 1; }
void printLastError(TCHAR *prefix) { LPVOID lpMsgBuf; DWORD lastError; lastError = GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); warnPrintf(TEXT("%s (%ld) -- %s\n"), prefix, lastError, lpMsgBuf); LocalFree( lpMsgBuf ); }
int sqGrowMemoryBy(int oldLimit, int delta) { /* round delta UP to page size */ if(fShowAllocations) { warnPrintf("Growing memory by %d...", delta); } delta = (delta + pageSize) & pageMask; if(!VirtualAlloc(pageLimit, delta, MEM_COMMIT, PAGE_READWRITE)) { if(fShowAllocations) { warnPrintf("failed\n"); } /* failed to grow */ return oldLimit; } /* otherwise, expand pageLimit and return new top limit */ if(fShowAllocations) { warnPrintf("okay\n"); } pageLimit += delta; usedMemory += delta; return (int)pageLimit; }
int sqShrinkMemoryBy(int oldLimit, int delta) { /* round delta DOWN to page size */ if(fShowAllocations) { warnPrintf("Shrinking by %d...",delta); } delta &= pageBits; if(!VirtualFree(pageLimit-delta, delta, MEM_DECOMMIT)) { if(fShowAllocations) { warnPrintf("failed\n"); } /* failed to shrink */ return oldLimit; } /* otherwise, shrink pageLimit and return new top limit */ if(fShowAllocations) { warnPrintf("okay\n"); } pageLimit -= delta; usedMemory -= delta; return (int)pageLimit; }
void vprintLastError(TCHAR *fmt, ...) { LPVOID lpMsgBuf; DWORD lastError; TCHAR *buf; va_list args; buf = (TCHAR*) calloc(sizeof(TCHAR), 4096); va_start(args, fmt); wvsprintf(buf, fmt, args); va_end(args); lastError = GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); warnPrintf(TEXT("%s (%ld: %s)\n"), buf, lastError, lpMsgBuf); LocalFree( lpMsgBuf ); free(buf); }
/* ioShowDisplayOnWindow: similar to ioShowDisplay but adds the int windowIndex * Return true if ok, false if not, but not currently checked */ sqInt ioShowDisplayOnWindow(unsigned char* dispBits, sqInt width, sqInt height, sqInt depth, sqInt affectedL, sqInt affectedR, sqInt affectedT, sqInt affectedB, sqInt windowIndex) { HWND hwnd = (windowIndex == 1 ? stWindow : ((HWND)windowIndex)); HDC dc; BITMAPINFO *bmi; int lines; int lsbDisplay; if(!IsWindow(hwnd)) return 0; if(affectedR < affectedL || affectedT > affectedB) return 1; /* Careful here: After resizing the main window the affected area can be larger than the area covered by the display bits ... */ if (affectedR > width) affectedR= width-1; if (affectedB > height) affectedB= height-1; /* ... and don't forget left and top - else reverse_image_* will crash */ if (affectedL > width) affectedL= width-1; if (affectedT > height) affectedT= height-1; /* Don't draw empty areas */ if(affectedL == affectedR || affectedT == affectedB) return 1; /* reload the update rectangle */ updateRect.left = affectedL; updateRect.top = affectedT; updateRect.right = affectedR; updateRect.bottom = affectedB; /* ----- EXPERIMENTAL ----- */ lsbDisplay = depth < 0; if(lsbDisplay) depth = -depth; bmi = BmiForDepth(depth); if(!bmi) { return 0; } /* reverse the image bits if necessary */ if( !lsbDisplay && depth < 32 ) if(depth == 16) reverse_image_words((unsigned int*) dispBits, (unsigned int*) dispBits, depth, width, &updateRect); else reverse_image_bytes((unsigned int*) dispBits, (unsigned int*) dispBits, depth, width, &updateRect); bmi->bmiHeader.biWidth = width; bmi->bmiHeader.biHeight = -height; bmi->bmiHeader.biSizeImage = 0; dc = GetDC(hwnd); if(!dc) { printLastError(TEXT("ioShowDisplayBits: GetDC() failed")); return 0; } lines = SetDIBitsToDevice(dc, 0, /* dst_x */ 0, /* dst_y */ width, /* dst_w */ height, /* dst_h */ 0, /* src_x */ 0, /* src_y */ 0, /* start scan line in DIB */ height, /* num scan lines in DIB */ (void*) dispBits, /* bits */ bmi, DIB_RGB_COLORS); if(lines == 0) { /* Note: the above is at least five times faster than what follows. Unfortunately, it also requires quite a bit of resources to be available. These are almost always available except in a few extreme conditions - but to compensate for those the following is provided. */ int pitch, start, end, nPix, line, left; int bitsPtr; /* compute pitch of form */ pitch = ((width * depth) + 31 & ~31) / 8; /* compute first word of update region */ start = ((updateRect.left * depth) & ~31) / 8; /* compute last word of update region */ end = ((updateRect.right * depth) + 31 & ~31) / 8; /* compute #of bits covered in update region */ nPix = ((end - start) * 8) / depth; left = (start * 8) / depth; bmi->bmiHeader.biWidth = nPix; bmi->bmiHeader.biHeight = 1; bmi->bmiHeader.biSizeImage = 0; bitsPtr = dispBits + start + (updateRect.top * pitch); for(line = updateRect.top; line < updateRect.bottom; line++) { lines = SetDIBitsToDevice(dc, left, line, nPix, 1, 0, 0, 0, 1, (void*) bitsPtr, bmi, DIB_RGB_COLORS); bitsPtr += pitch; } } ReleaseDC(hwnd,dc); if(lines == 0) { printLastError(TEXT("SetDIBitsToDevice failed")); warnPrintf(TEXT("width=%d,height=%d,bits=%X,dc=%X\n"), width, height, dispBits,dc); } /* reverse the image bits if necessary */ if( !lsbDisplay && depth < 32 ) if(depth == 16) reverse_image_words((unsigned int*) dispBits, (unsigned int*) dispBits, depth, width, &updateRect); else reverse_image_bytes((unsigned int*) dispBits, (unsigned int*) dispBits, depth, width, &updateRect); return 1; }
/* win32JoystickDebugInfo: Print debugging information for all joysticks. */ EXPORT(int) win32JoystickDebugInfo(void) { int i; JOYCAPS *caps; int methodArgumentCount(void); int stackIntegerValue(int); int failed(void); if(methodArgumentCount() != 0) return primitiveFail(); warnPrintf(TEXT("<--- Joystick debug information --->\n")); for(i=0; i<numJoySticks; i++) { caps = joySticks + i; warnPrintf(TEXT("Joystick %d:\n"), i); warnPrintf(TEXT("\tName: %s\n"), caps->szPname); warnPrintf(TEXT("\tMin X: %d\n\tMax X: %d\n"), caps->wXmin, caps->wXmax); warnPrintf(TEXT("\tMin Y: %d\n\tMax Y: %d\n"), caps->wYmin, caps->wYmax); warnPrintf(TEXT("\tMin Z: %d\n\tMax Z: %d\n"), caps->wZmin, caps->wZmax); warnPrintf(TEXT("\tMin R: %d\n\tMax R: %d\n"), caps->wRmin, caps->wRmax); warnPrintf(TEXT("\tMin U: %d\n\tMax U: %d\n"), caps->wUmin, caps->wUmax); warnPrintf(TEXT("\tMin V: %d\n\tMax V: %d\n"), caps->wVmin, caps->wVmax); warnPrintf(TEXT("\tMaxButtons: %d\n"), caps->wMaxButtons); warnPrintf(TEXT("\tNumButtons: %d\n"), caps->wNumButtons); warnPrintf(TEXT("\tMaxAxes: %d\n"), caps->wMaxAxes); warnPrintf(TEXT("\tNumAxes: %d\n"), caps->wNumAxes); warnPrintf(TEXT("\tCaps: ")); if(caps->wCaps & JOYCAPS_HASZ) warnPrintf(TEXT("JOYCAPS_HASZ ")); if(caps->wCaps & JOYCAPS_HASR) warnPrintf(TEXT("JOYCAPS_HASR ")); if(caps->wCaps & JOYCAPS_HASU) warnPrintf(TEXT("JOYCAPS_HASU ")); if(caps->wCaps & JOYCAPS_HASV) warnPrintf(TEXT("JOYCAPS_HASV ")); if(caps->wCaps & JOYCAPS_HASPOV) warnPrintf(TEXT("JOYCAPS_HASPOV ")); warnPrintf(TEXT("\n")); } return 1; }
/* win32JoystickDebugPrintAlternativeValues: Print the raw values of an alternative readout of the specified joystick. */ EXPORT(int) win32JoystickDebugPrintAlternativeValues(void) { int index, err; JOYINFOEX info; if(methodArgumentCount() != 1) return primitiveFail(); index = stackIntegerValue(0); if(failed()) return 0; if(index < 1 || index > 2) return primitiveFail(); warnPrintf(TEXT("Alternative joystick values (%d):\n"), index); info.dwSize = sizeof(info); info.dwFlags = JOY_RETURNALL; err = joyGetPosEx(index-1, &info); if(err != JOYERR_NOERROR) { if(err == MMSYSERR_NODRIVER) warnPrintf(TEXT("\t<no driver present>\n\n")); else if(err == MMSYSERR_INVALPARAM) warnPrintf(TEXT("\t<invalid param in joyGetPos()>\n\n")); else if(err == MMSYSERR_BADDEVICEID) warnPrintf(TEXT("\t<bad device id>\n\n")); else if(err == JOYERR_UNPLUGGED) warnPrintf(TEXT("\t<joystick unplugged>\n\n")); else warnPrintf(TEXT("\t<unknown error: %d>\n\n"), err); } else { warnPrintf(TEXT("\tX: %lu\n"), info.dwXpos); warnPrintf(TEXT("\tY: %lu\n"), info.dwYpos); warnPrintf(TEXT("\tZ: %lu\n"), info.dwZpos); warnPrintf(TEXT("\tR: %lu\n"), info.dwRpos); warnPrintf(TEXT("\tU: %lu\n"), info.dwUpos); warnPrintf(TEXT("\tV: %lu\n"), info.dwVpos); warnPrintf(TEXT("\tButtons: %lx\n"), info.dwButtons); warnPrintf(TEXT("\tPOV: %lu\n"), info.dwPOV); } pop(1); /* Leave rcvr on stack */ return 1; }