19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _ULS_CALLCONV_
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define CALLCONV _System
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unidef.h>                    // Unicode API
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <uconv.h>                     // Unicode API (codepage conversion)
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <process.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <time.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_os2fslib.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic ULONG ulFCFToUse =
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_TITLEBAR |
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_SYSMENU |
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_MINBUTTON |
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_MAXBUTTON |
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_NOBYTEALIGN |
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_SIZEBORDER |
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FCF_TASKLIST;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int bMouseCaptured   = 0;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int bMouseCapturable = 0;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic HPOINTER hptrGlobalPointer = NULL;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic HPOINTER hptrCurrentIcon = NULL;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iWindowSizeX = 320;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int iWindowSizeY = 200;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int bWindowResized = 0;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma pack(1)
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct BMPINFO
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   BITMAPINFO;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   RGB  clr;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} BMPINFO, *PBMPINFO;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma pack()
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Backdoors:
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC void SDLCALL SDL_OS2FSLIB_SetFCFToUse(ULONG ulFCF)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ulFCFToUse = ulFCF;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Configuration defines:
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// We have to report empty alpha mask, otherwise SDL will select
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// alpha blitters, and this will have unwanted results, as we don't
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// support alpha channel in FSLib yet.
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REPORT_EMPTY_ALPHA_MASK
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Experimental: Move every FSLib_BitBlt() call into window message
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// processing function.
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This may fix dirt left on desktop. Or not.
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#define BITBLT_IN_WINMESSAGEPROC
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Experimental-2: Use WinLockWindowUpdate() in around bitblts!
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This is not enabled, because it seems to cause more problems
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// than good.
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#define USE_WINLOCKWINDOWUPDATE_AROUND_BITBLTS
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Use the following to show resized image instead of black stuff
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// even if the surface is resizable.
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#define RESIZE_EVEN_IF_RESIZABLE
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The translation table from a VK keysym to a SDL keysym */
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDLKey HWScanKeyMap[256];
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_keysym *TranslateKey(int vkey, int chcode, int scancode, SDL_keysym *keysym, int iPressed);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int  iShiftIsPressed;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BITBLT_IN_WINMESSAGEPROC
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WM_UPDATERECTSREQUEST   WM_USER+50
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_WINLOCKWINDOWUPDATE_AROUND_BITBLTS
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define FSLIB_BITBLT(hwnd, buffer, top, left, width, height) \
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { \
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinLockWindowUpdate(HWND_DESKTOP, HWND_DESKTOP); \
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      FSLib_BitBlt(hwnd, buffer, top, left, width, height); \
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinLockWindowUpdate(HWND_DESKTOP, NULL); \
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define FSLIB_BITBLT(hwnd, buffer, top, left, width, height) \
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSLib_BitBlt(hwnd, buffer, top, left, width, height);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// SetAccessableWindowPos
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Same as WinSetWindowPos(), but takes care for the window to be
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// always on the screen, the titlebar will be accessable everytime.
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL SetAccessableWindowPos(HWND hwnd, HWND hwndInsertBehind,
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   LONG x, LONG y,
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   LONG cx, LONG cy,
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ULONG fl)
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SWP swpDesktop, swp;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Get desktop area
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinQueryWindowPos(HWND_DESKTOP, &swpDesktop);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((fl & SWP_MOVE) && (fl & SWP_SIZE))
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // If both moving and sizing, then change size and pos now!!
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x+cx>swpDesktop.cx)
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x = swpDesktop.cx - cx;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x<0)
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x = 0;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y<0)
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y = 0;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y+cy>swpDesktop.cy)
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y = swpDesktop.cy - cy;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return WinSetWindowPos(hwnd, hwndInsertBehind, x, y, cx, cy, fl);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (fl & SWP_MOVE)
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Just moving
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinQueryWindowPos(hwnd, &swp);
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x+swp.cx>swpDesktop.cx)
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x = swpDesktop.cx - swp.cx;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x<0)
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x = 0;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y<0)
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y = 0;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y+swp.cy>swpDesktop.cy)
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y = swpDesktop.cy - swp.cy;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return WinSetWindowPos(hwnd, hwndInsertBehind, x, y, cx, cy, fl);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (fl & SWP_SIZE)
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Just sizing
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinQueryWindowPos(hwnd, &swp);
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    x = swp.x;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    y = swp.y;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x+cx>swpDesktop.cx)
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x = swpDesktop.cx - cx;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x<0)
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x = 0;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y<0)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y = 0;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y+cy>swpDesktop.cy)
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y = swpDesktop.cy - cy;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return WinSetWindowPos(hwnd, hwndInsertBehind, x, y, cx, cy, fl | SWP_MOVE);
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return WinSetWindowPos(hwnd, hwndInsertBehind, x, y, cx, cy, fl);
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic UniChar NativeCharToUniChar(int chcode)
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  UniChar ucResult = (UniChar) chcode;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int rc;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  UconvObject ucoTemp;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char     achFrom[2];
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char     *pchFrom;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  size_t   iFromCount;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  UniChar  aucTo[10];
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  UniChar  *pucTo;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  size_t   iToCount;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  size_t   iNonIdentical;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Create unicode convert object
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = UniCreateUconvObject(L"", &ucoTemp);
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rc!=ULS_SUCCESS)
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Could not create convert object!
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return ucResult;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Convert language code string to unicode string
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  achFrom[0] = (char) chcode;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  achFrom[1] = 0;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iFromCount = sizeof(char) * 2;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iToCount = sizeof(UniChar) * 2;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pucTo = &(aucTo[0]);
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pchFrom = &(achFrom[0]);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = UniUconvToUcs(ucoTemp,
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     &pchFrom,
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     &iFromCount,
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     &pucTo,
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     &iToCount,
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     &iNonIdentical);
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rc!=ULS_SUCCESS)
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Could not convert language code to UCS string!
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    UniFreeUconvObject(ucoTemp);
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return ucResult;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  UniFreeUconvObject(ucoTemp);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("%02x converted to %02x\n", (int) chcode, (int) (aucTo[0]));
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return aucTo[0];
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// TranslateKey
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This creates SDL Keycodes from VK_ and hardware scan codes
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_keysym *TranslateKey(int vkey, int chcode, int scancode, SDL_keysym *keysym, int iPressed)
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  keysym->scancode = (unsigned char) scancode;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  keysym->mod = KMOD_NONE;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  keysym->unicode = 0;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (iPressed && SDL_TranslateUNICODE)
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (chcode)
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      keysym->unicode = NativeCharToUniChar(chcode);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      keysym->unicode = vkey;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  keysym->sym = HWScanKeyMap[scancode];
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now stuffs based on state of shift key(s)!
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (vkey == VK_SHIFT)
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    iShiftIsPressed = iPressed;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((iShiftIsPressed) && (SDL_TranslateUNICODE))
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Change syms, if Unicode stuff is required
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // I think it's silly, but it's SDL...
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (keysym->sym)
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_BACKQUOTE:
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = '~';
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_1:
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_EXCLAIM;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_2:
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_AT;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_3:
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_HASH;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_4:
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_DOLLAR;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_5:
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = '%';
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_6:
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_CARET;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_7:
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_AMPERSAND;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_8:
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_ASTERISK;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_9:
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_LEFTPAREN;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_0:
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_RIGHTPAREN;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_MINUS:
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_UNDERSCORE;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_PLUS:
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_EQUALS;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_LEFTBRACKET:
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = '{';
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_RIGHTBRACKET:
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = '}';
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_SEMICOLON:
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_COLON;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_QUOTE:
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_QUOTEDBL;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_BACKSLASH:
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = '|';
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_COMMA:
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_LESS;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_PERIOD:
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_GREATER;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case SDLK_SLASH:
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        keysym->sym = SDLK_QUESTION;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      default:
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return keysym;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define CONVERTMOUSEPOSITION()  \
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* We have to inverse the mouse position, because every non-os/2 system */                                                \
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* has a coordinate system where the (0;0) is the top-left corner,      */                                                \
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* while on os/2 it's the bottom left corner!                           */                                                \
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (FSLib_QueryFSMode(hwnd))                                                                                              \
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {                                                                                                                         \
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* We're in FS mode!                                                        */                                          \
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* In FS mode our window is as big as fullscreen mode, but not necessary as */                                          \
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* big as the source buffer (can be bigger)                                 */                                          \
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* So, limit mouse pos to source buffer size!                               */                                          \
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (ppts->x<0) ppts->x = 0;                                                                                             \
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (ppts->y<0) ppts->y = 0;                                                                                             \
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (ppts->x>=pVideo->hidden->SrcBufferDesc.uiXResolution) ppts->x = pVideo->hidden->SrcBufferDesc.uiXResolution-1;      \
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (ppts->y>=pVideo->hidden->SrcBufferDesc.uiYResolution) ppts->y = pVideo->hidden->SrcBufferDesc.uiYResolution-1;      \
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pVideo->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */                                   \
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          ptl.x = ppts->x; ptl.y = ppts->y;                                                                                       \
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinMapWindowPoints(pVideo->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);                                                  \
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinSetPointerPos(HWND_DESKTOP, ptl.x, ptl.y);                                                                           \
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* Then convert OS/2 position to SDL position */                                                                        \
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          ppts->y = pVideo->hidden->SrcBufferDesc.uiYResolution - ppts->y - 1;                                                    \
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else                                                                                                                    \
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {                                                                                                                         \
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SWP swpClient;                                                                                                          \
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* We're in windowed mode! */                                                                                           \
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinQueryWindowPos(pVideo->hidden->hwndClient, &swpClient);                                                              \
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* Convert OS/2 mouse position to SDL position, and also scale it! */                                                   \
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (ppts->x) = (ppts->x) * pVideo->hidden->SrcBufferDesc.uiXResolution / swpClient.cx;                                       \
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (ppts->y) = (ppts->y) * pVideo->hidden->SrcBufferDesc.uiYResolution / swpClient.cy;                                       \
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (ppts->y) = pVideo->hidden->SrcBufferDesc.uiYResolution - (ppts->y)  - 1;                                                 \
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// WndProc
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This is the message processing window procedure for the
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// SDLWindowClass, which is the client window in our application.
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// It handles switching back and away from the app (taking care of
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// going out and back to and from fullscreen mode), sending keystrokes
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// and mouse events to where it has to be sent, etc...
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic MRESULT EXPENTRY WndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HPS ps;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  RECTL rcl;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *pVideo = NULL;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  switch (msg)
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_CHAR:  // Keypress notification
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//      printf("WM_CHAR\n"); fflush(stdout);
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = WinQueryWindowPtr(hwnd, 0);
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /*
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // We skip repeated keys:
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (CHARMSG(&msg)->cRepeat>1)
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//          printf("Repeated key (%d), skipping...\n", CHARMSG(&msg)->cRepeat); fflush(stdout);
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          return (MRESULT) TRUE;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        */
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // If it's not repeated, then let's see if its pressed or released!
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (SHORT1FROMMP(mp1) & KC_KEYUP)
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // A key has been released
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SDL_keysym keysym;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//          printf("WM_CHAR, keyup, code is [0x%0x]\n", CHAR4FROMMP(mp1)); // HW scan code
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // One problem is with F1, which gets only the keyup message because
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // it is a system key.
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // So, when we get keyup message, we simulate keydown too!
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // UPDATE:
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          //  This problem should be solved now, that the accelerator keys are
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          //  disabled for this window!
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /*
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (SHORT2FROMMP(mp2)==VK_F1)
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(SHORT2FROMMP(mp2), // VK_ code
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                           SHORT1FROMMP(mp2), // Character code
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                           CHAR4FROMMP(mp1),  // HW Scan code
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                           &keysym,0));
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }*/
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(SHORT2FROMMP(mp2), // VK_ code
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                         SHORT1FROMMP(mp2), // Character code
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                         CHAR4FROMMP(mp1),  // HW Scan code
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                         &keysym,0));
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // A key has been pressed
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SDL_keysym keysym;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//          printf("WM_CHAR, keydown, code is [0x%0x]\n", CHAR4FROMMP(mp1)); // HW scan code
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // Check for fastkeys: ALT+HOME to toggle FS mode
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          //                     ALT+END to close app
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if ((SHORT1FROMMP(mp1) & KC_ALT) &&
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              (SHORT2FROMMP(mp2) == VK_HOME))
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf(" Pressed ALT+HOME!\n"); fflush(stdout);
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Only switch between fullscreen and back if it's not
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // a resizable mode!
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                (!pVideo->hidden->pSDLSurface) ||
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ((pVideo->hidden->pSDLSurface)
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 && ((pVideo->hidden->pSDLSurface->flags & SDL_RESIZABLE)==0)
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                )
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               )
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              FSLib_ToggleFSMode(hwnd, !FSLib_QueryFSMode(hwnd));
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            else
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              printf(" Resizable mode, so discarding ALT+HOME!\n"); fflush(stdout);
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          } else
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if ((SHORT1FROMMP(mp1) & KC_ALT) &&
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              (SHORT2FROMMP(mp2) == VK_END))
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf(" Pressed ALT+END!\n"); fflush(stdout);
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Close window, and get out of loop!
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Also send event to SDL application, but we won't
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // wait for it to be processed!
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateQuit();
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinPostMsg(hwnd, WM_QUIT, 0, 0);
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          } else
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(SHORT2FROMMP(mp2), // VK_ code
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                          SHORT1FROMMP(mp2), // Character code
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                          CHAR4FROMMP(mp1),  // HW Scan code
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                                          &keysym,1));
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return (MRESULT) TRUE;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_TRANSLATEACCEL:
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        PQMSG pqmsg;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pqmsg = (PQMSG) mp1;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (mp1)
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (pqmsg->msg == WM_CHAR)
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // WM_CHAR message!
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Let's filter the ALT keypress and all other acceleration keys!
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return (MRESULT) FALSE;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break; // Default processing (pass to parent until frame control)
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_PAINT:  // Window redraw!
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_PAINT (0x%x)\n", hwnd); fflush(stdout);
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ps = WinBeginPaint(hwnd,0,&rcl);
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (!pVideo->hidden->pSDLSurface)
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          RECTL rclRect;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // So, don't blit now!
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          printf("WM_PAINT : Skipping blit while resizing (Pre!)!\n"); fflush(stdout);
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinQueryWindowRect(hwnd, &rclRect);
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // Fill with black
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinFillRect(ps, &rclRect, CLR_BLACK);
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (DosRequestMutexSem(pVideo->hidden->hmtxUseSrcBuffer, 1000)==NO_ERROR)
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int iTop, iLeft, iWidth, iHeight;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int iXScaleError, iYScaleError;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int iXScaleError2, iYScaleError2;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SWP swp;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Re-blit the modified area!
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // For this, we have to calculate the points, scaled!
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinQueryWindowPos(hwnd, &swp);
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("WM_PAINT : WinSize: %d %d, BufSize: %d %d\n",
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   swp.cx,
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   swp.cy,
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   pVideo->hidden->SrcBufferDesc.uiXResolution,
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   pVideo->hidden->SrcBufferDesc.uiYResolution
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  );
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fflush(stdout);
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef RESIZE_EVEN_IF_RESIZABLE
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // But only blit if the window is not resizable, or if
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // the window is resizable and the source buffer size is the
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // same as the destination buffer size!
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ((!pVideo->hidden->pSDLSurface) ||
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ((pVideo->hidden->pSDLSurface) &&
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (pVideo->hidden->pSDLSurface->flags & SDL_RESIZABLE) &&
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 ((swp.cx != pVideo->hidden->SrcBufferDesc.uiXResolution) ||
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  (swp.cy != pVideo->hidden->SrcBufferDesc.uiYResolution)
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 ) &&
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (!FSLib_QueryFSMode(hwnd))
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                )
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               )
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              RECTL rclRect;
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // Resizable surface and in resizing!
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // So, don't blit now!
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              printf("WM_PAINT : Skipping blit while resizing!\n"); fflush(stdout);
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinQueryWindowRect(hwnd, &rclRect);
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // Fill with black
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinFillRect(ps, &rclRect, CLR_BLACK);
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } else
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iXScaleError = (pVideo->hidden->SrcBufferDesc.uiXResolution-1) / swp.cx;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iYScaleError = (pVideo->hidden->SrcBufferDesc.uiYResolution-1) / swp.cy;
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iXScaleError<0) iXScaleError = 0;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iYScaleError<0) iYScaleError = 0;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iXScaleError2 = (swp.cx-1)/(pVideo->hidden->SrcBufferDesc.uiXResolution);
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iYScaleError2 = (swp.cy-1)/(pVideo->hidden->SrcBufferDesc.uiYResolution);
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iXScaleError2<0) iXScaleError2 = 0;
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iYScaleError2<0) iYScaleError2 = 0;
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iTop = (swp.cy - rcl.yTop) * pVideo->hidden->SrcBufferDesc.uiYResolution / swp.cy - iYScaleError;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iLeft = rcl.xLeft * pVideo->hidden->SrcBufferDesc.uiXResolution / swp.cx - iXScaleError;
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iWidth = ((rcl.xRight-rcl.xLeft) * pVideo->hidden->SrcBufferDesc.uiXResolution + swp.cx-1)
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                / swp.cx + 2*iXScaleError;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iHeight = ((rcl.yTop-rcl.yBottom) * pVideo->hidden->SrcBufferDesc.uiYResolution + swp.cy-1)
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                / swp.cy + 2*iYScaleError;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iWidth+=iXScaleError2;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iHeight+=iYScaleError2;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iTop<0) iTop = 0;
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iLeft<0) iLeft = 0;
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iTop+iHeight>pVideo->hidden->SrcBufferDesc.uiYResolution) iHeight = pVideo->hidden->SrcBufferDesc.uiYResolution-iTop;
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iLeft+iWidth>pVideo->hidden->SrcBufferDesc.uiXResolution) iWidth = pVideo->hidden->SrcBufferDesc.uiXResolution-iLeft;
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              printf("WM_PAINT : BitBlt: %d %d -> %d %d (Buf %d x %d)\n",
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     iTop, iLeft, iWidth, iHeight,
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     pVideo->hidden->SrcBufferDesc.uiXResolution,
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     pVideo->hidden->SrcBufferDesc.uiYResolution
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    );
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              fflush(stdout);
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              FSLIB_BITBLT(hwnd, pVideo->hidden->pchSrcBuffer, iTop, iLeft, iWidth, iHeight);
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            DosReleaseMutexSem(pVideo->hidden->hmtxUseSrcBuffer);
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      else
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("WM_PAINT : No pVideo!\n"); fflush(stdout);
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinEndPaint(ps);
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_PAINT : Done.\n");
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return 0;
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_SIZE:
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("WM_SIZE : (%d %d)\n",
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               SHORT1FROMMP(mp2), SHORT2FROMMP(mp2)); fflush(stdout);
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iWindowSizeX = SHORT1FROMMP(mp2);
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iWindowSizeY = SHORT2FROMMP(mp2);
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        bWindowResized = 1;
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Make sure the window will be redrawn
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinInvalidateRegion(hwnd, NULL, TRUE);
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_FSLIBNOTIFICATION:
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("WM_FSLIBNOTIFICATION\n"); fflush(stdout);
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((int)mp1 == FSLN_TOGGLEFSMODE)
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // FS mode changed, reblit image!
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pVideo = FSLib_GetUserParm(hwnd);
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (pVideo)
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (!pVideo->hidden->pSDLSurface)
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Resizable surface and in resizing!
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // So, don't blit now!
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("WM_FSLIBNOTIFICATION : Can not blit if there is no surface, doing nothing.\n"); fflush(stdout);
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          } else
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (DosRequestMutexSem(pVideo->hidden->hmtxUseSrcBuffer, 1000)==NO_ERROR)
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (pVideo->hidden->pSDLSurface)
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              {
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef RESIZE_EVEN_IF_RESIZABLE
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SWP swp;
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                // But only blit if the window is not resizable, or if
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                // the window is resizable and the source buffer size is the
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                // same as the destination buffer size!
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                WinQueryWindowPos(hwnd, &swp);
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ((!pVideo->hidden->pSDLSurface) ||
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    (
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (pVideo->hidden->pSDLSurface) &&
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (pVideo->hidden->pSDLSurface->flags & SDL_RESIZABLE) &&
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     ((swp.cx != pVideo->hidden->SrcBufferDesc.uiXResolution) ||
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      (swp.cy != pVideo->hidden->SrcBufferDesc.uiYResolution)
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     ) &&
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (!FSLib_QueryFSMode(hwnd))
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    )
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   )
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  // Resizable surface and in resizing!
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  // So, don't blit now!
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  printf("WM_FSLIBNOTIFICATION : Cannot blit while resizing, doing nothing.\n"); fflush(stdout);
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                } else
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  printf("WM_FSLIBNOTIFICATION : Blitting!\n"); fflush(stdout);
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  FSLIB_BITBLT(hwnd, pVideo->hidden->pchSrcBuffer,
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               0, 0,
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               pVideo->hidden->SrcBufferDesc.uiXResolution,
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               pVideo->hidden->SrcBufferDesc.uiYResolution);
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              }
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              else
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                printf("WM_FSLIBNOTIFICATION : No public surface!\n"); fflush(stdout);
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              DosReleaseMutexSem(pVideo->hidden->hmtxUseSrcBuffer);
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return (MPARAM) 1;
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_ACTIVATE:
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_ACTIVATE\n"); fflush(stdout);
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pVideo->hidden->fInFocus = (int) mp1;
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (pVideo->hidden->fInFocus)
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // Went into focus
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if ((pVideo->hidden->iMouseVisible) && (!bMouseCaptured))
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE));
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          else
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, NULL);
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (bMouseCapturable)
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Re-capture the mouse, if we captured it before!
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetCapture(HWND_DESKTOP, hwnd);
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            bMouseCaptured = 1;
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              SWP swpClient;
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              POINTL ptl;
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // Center the mouse to the middle of the window!
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinQueryWindowPos(pVideo->hidden->hwndClient, &swpClient);
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              ptl.x = 0; ptl.y = 0;
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinMapWindowPoints(pVideo->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              pVideo->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinSetPointerPos(HWND_DESKTOP,
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.x + swpClient.cx/2,
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.y + swpClient.cy/2);
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // Went out of focus
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinSetPointer(HWND_DESKTOP, WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE));
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (bMouseCaptured)
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Release the mouse
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetCapture(HWND_DESKTOP, hwnd);
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            bMouseCaptured = 0;
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_ACTIVATE done\n"); fflush(stdout);
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_BUTTON1DOWN:
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_BUTTON1DOWN\n"); fflush(stdout);
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_PrivateMouseButton(SDL_PRESSED,
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               SDL_BUTTON_LEFT,
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               0, 0); // Don't report mouse movement!
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (bMouseCapturable)
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // We should capture the mouse!
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (!bMouseCaptured)
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetCapture(HWND_DESKTOP, hwnd);
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, NULL);
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            bMouseCaptured = 1;
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              SWP swpClient;
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              POINTL ptl;
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // Center the mouse to the middle of the window!
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinQueryWindowPos(pVideo->hidden->hwndClient, &swpClient);
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              ptl.x = 0; ptl.y = 0;
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinMapWindowPoints(pVideo->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              pVideo->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinSetPointerPos(HWND_DESKTOP,
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.x + swpClient.cx/2,
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.y + swpClient.cy/2);
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_BUTTON1UP:
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_BUTTON1UP\n"); fflush(stdout);
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_PrivateMouseButton(SDL_RELEASED,
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             SDL_BUTTON_LEFT,
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             0, 0); // Don't report mouse movement!
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_BUTTON2DOWN:
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_BUTTON2DOWN\n"); fflush(stdout);
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_PrivateMouseButton(SDL_PRESSED,
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               SDL_BUTTON_RIGHT,
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               0, 0); // Don't report mouse movement!
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (bMouseCapturable)
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // We should capture the mouse!
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (!bMouseCaptured)
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetCapture(HWND_DESKTOP, hwnd);
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, NULL);
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            bMouseCaptured = 1;
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              SWP swpClient;
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              POINTL ptl;
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // Center the mouse to the middle of the window!
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinQueryWindowPos(pVideo->hidden->hwndClient, &swpClient);
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              ptl.x = 0; ptl.y = 0;
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinMapWindowPoints(pVideo->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              pVideo->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinSetPointerPos(HWND_DESKTOP,
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.x + swpClient.cx/2,
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.y + swpClient.cy/2);
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_BUTTON2UP:
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_BUTTON2UP\n"); fflush(stdout);
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_PrivateMouseButton(SDL_RELEASED,
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             SDL_BUTTON_RIGHT,
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             0, 0); // Don't report mouse movement!
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_BUTTON3DOWN:
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_BUTTON3DOWN\n"); fflush(stdout);
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_PrivateMouseButton(SDL_PRESSED,
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               SDL_BUTTON_MIDDLE,
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               0, 0); // Don't report mouse movement!
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (bMouseCapturable)
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // We should capture the mouse!
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (!bMouseCaptured)
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetCapture(HWND_DESKTOP, hwnd);
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, NULL);
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            bMouseCaptured = 1;
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              SWP swpClient;
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              POINTL ptl;
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              // Center the mouse to the middle of the window!
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinQueryWindowPos(pVideo->hidden->hwndClient, &swpClient);
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              ptl.x = 0; ptl.y = 0;
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinMapWindowPoints(pVideo->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              pVideo->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              WinSetPointerPos(HWND_DESKTOP,
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.x + swpClient.cx/2,
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               ptl.y + swpClient.cy/2);
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_BUTTON3UP:
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_BUTTON3UP\n"); fflush(stdout);
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_PrivateMouseButton(SDL_RELEASED,
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             SDL_BUTTON_MIDDLE,
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             0, 0); // Don't report mouse movement!
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_MOUSEMOVE:
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//      printf("WM_MOUSEMOVE\n"); fflush(stdout);
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (pVideo->hidden->iSkipWMMOUSEMOVE)
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pVideo->hidden->iSkipWMMOUSEMOVE--;
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          POINTS *ppts = (POINTS *) (&mp1);
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          POINTL ptl;
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (bMouseCaptured)
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SWP swpClient;
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinQueryWindowPos(pVideo->hidden->hwndClient, &swpClient);
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Send relative mouse position, and re-center the mouse
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Reposition the mouse to the center of the screen/window
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateMouseMotion(0, // Buttons not changed
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   1, // Relative position
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ppts->x - (swpClient.cx/2),
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   (swpClient.cy/2) - ppts->y);
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ptl.x = 0; ptl.y = 0;
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinMapWindowPoints(pVideo->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            pVideo->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Center the mouse to the middle of the window!
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointerPos(HWND_DESKTOP,
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             ptl.x + swpClient.cx/2,
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             ptl.y + swpClient.cy/2);
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          } else
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            CONVERTMOUSEPOSITION();
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Send absolute mouse position
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateMouseMotion(0, // Buttons not changed
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   0, // Absolute position
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ppts->x,
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ppts->y);
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((pVideo->hidden->iMouseVisible) && (!bMouseCaptured))
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//          printf("WM_MOUSEMOVE : ptr = %p\n", hptrGlobalPointer); fflush(stdout);
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (hptrGlobalPointer)
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, hptrGlobalPointer);
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          else
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            WinSetPointer(HWND_DESKTOP, WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE));
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        else
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinSetPointer(HWND_DESKTOP, NULL);
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//      printf("WM_MOUSEMOVE done\n"); fflush(stdout);
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return (MRESULT) FALSE;
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_CLOSE: // Window close
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("WM_CLOSE\n"); fflush(stdout);
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pVideo)
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Send Quit message to the SDL application!
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_PrivateQuit();
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BITBLT_IN_WINMESSAGEPROC
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case WM_UPDATERECTSREQUEST:
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo = FSLib_GetUserParm(hwnd);
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((pVideo) && (pVideo->hidden->pSDLSurface))
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (DosRequestMutexSem(pVideo->hidden->hmtxUseSrcBuffer, SEM_INDEFINITE_WAIT)==NO_ERROR)
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          int numrects;
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SDL_Rect *rects;
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          int i;
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SWP swp;
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          numrects = (int) mp1;
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          rects = (SDL_Rect *) mp2;
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          WinQueryWindowPos(hwnd, &swp);
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef RESIZE_EVEN_IF_RESIZABLE
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if ((!pVideo->hidden->pSDLSurface) ||
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              (
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               (pVideo->hidden->pSDLSurface) &&
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               (pVideo->hidden->pSDLSurface->flags & SDL_RESIZABLE) &&
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               ((swp.cx != pVideo->hidden->SrcBufferDesc.uiXResolution) ||
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                (swp.cy != pVideo->hidden->SrcBufferDesc.uiYResolution)
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               ) &&
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               (!FSLib_QueryFSMode(hwnd))
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              )
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             )
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Resizable surface and in resizing!
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // So, don't blit now!
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("[WM_UPDATERECTSREQUEST] : Skipping blit while resizing!\n"); fflush(stdout);
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          } else
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("[WM_UPDATERECTSREQUEST] : Blitting!\n"); fflush(stdout);
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // Blit the changed areas
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for (i=0; i<numrects; i++)
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              FSLIB_BITBLT(hwnd, pVideo->hidden->pchSrcBuffer,
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           rects[i].y, rects[i].x, rects[i].w, rects[i].h);
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          DosReleaseMutexSem(pVideo->hidden->hmtxUseSrcBuffer);
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return 0;
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    default:
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("Unhandled: %x\n", msg); fflush(stdout);
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Run the default window procedure for unhandled stuffs
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return WinDefWindowProc(hwnd, msg, mp1, mp2);
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// FrameWndProc
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This is the message processing window procedure for the
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// frame window of SDLWindowClass.
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic MRESULT EXPENTRY FrameWndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  PFNWP pOldFrameProc;
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  MRESULT result;
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  PTRACKINFO ti;
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int cx, cy, ncx, ncy;
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  RECTL rclTemp;
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  PSWP pswpTemp;
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *pVideo = NULL;
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pVideo = (SDL_VideoDevice *) WinQueryWindowULong(hwnd, QWL_USER);
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pOldFrameProc = pVideo->hidden->pfnOldFrameProc;
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((pVideo->hidden->bProportionalResize) &&
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (msg==WM_ADJUSTWINDOWPOS) &&
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (!FSLib_QueryFSMode(pVideo->hidden->hwndClient))
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     )
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pswpTemp = (PSWP) mp1;
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Resizing? */
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pswpTemp->fl & SWP_SIZE)
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Calculate client size */
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.xLeft = pswpTemp->x;
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.xRight = pswpTemp->x + pswpTemp->cx;
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.yBottom = pswpTemp->y;
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.yTop = pswpTemp->y + pswpTemp->cy;
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinCalcFrameRect(hwnd, &rclTemp, TRUE);
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ncx = cx = rclTemp.xRight - rclTemp.xLeft;
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ncy = cy = rclTemp.yTop - rclTemp.yBottom;
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Calculate new size to keep it proportional */
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((pVideo->hidden->ulResizingFlag & TF_LEFT) || (pVideo->hidden->ulResizingFlag & TF_RIGHT))
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* The window is resized horizontally */
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ncy = pVideo->hidden->SrcBufferDesc.uiYResolution * cx / pVideo->hidden->SrcBufferDesc.uiXResolution;
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((pVideo->hidden->ulResizingFlag & TF_TOP) || (pVideo->hidden->ulResizingFlag & TF_BOTTOM))
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* The window is resized vertically */
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ncx = pVideo->hidden->SrcBufferDesc.uiXResolution * cy / pVideo->hidden->SrcBufferDesc.uiYResolution;
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Calculate back frame coordinates */
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.xLeft = pswpTemp->x;
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.xRight = pswpTemp->x + ncx;
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.yBottom = pswpTemp->y;
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rclTemp.yTop = pswpTemp->y + ncy;
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinCalcFrameRect(hwnd, &rclTemp, FALSE);
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Store new size/position info */
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pswpTemp->cx = rclTemp.xRight - rclTemp.xLeft;
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!(pVideo->hidden->ulResizingFlag & TF_TOP))
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pswpTemp->y = pswpTemp->y + pswpTemp->cy - (rclTemp.yTop - rclTemp.yBottom);
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pswpTemp->cy = rclTemp.yTop - rclTemp.yBottom;
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pswpTemp->cy = rclTemp.yTop - rclTemp.yBottom;
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  result = (*pOldFrameProc)(hwnd, msg, mp1, mp2);
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((pVideo->hidden->bProportionalResize) && (msg==WM_QUERYTRACKINFO))
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ti = (PTRACKINFO) mp2;
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Store the direction of resizing */
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((ti->fs & TF_LEFT) || (ti->fs & TF_RIGHT) ||
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        (ti->fs & TF_TOP) || (ti->fs & TF_BOTTOM))
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo->hidden->ulResizingFlag = ti->fs;
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return result;
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// PMThreadFunc
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This function implements the PM-Thread, which initializes the
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// application window itself, the DIVE, and start message processing.
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/////////////////////////////////////////////////////////////////////
11449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint iNumOfPMThreadInstances = 0; // Global!
11459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PMThreadFunc(void *pParm)
11469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *pVideo = pParm;
11489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HAB hab;
11499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HMQ hmq;
11509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  QMSG msg;
11519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ULONG fcf;
11529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
11549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[PMThreadFunc] : Starting\n"); fflush(stdout);
11559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iNumOfPMThreadInstances++;
11589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Initialize PM, create a message queue.
11609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hab=WinInitialize(0);
11629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmq=WinCreateMsgQueue(hab,0);
11639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (hmq==0)
11649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
11659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
11669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[PMThreadFunc] : Could not create message queue!\n");
11679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("                 It might be that the application using SDL is not a PM app!\n");
11689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fflush(stdout);
11699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pVideo->hidden->iPMThreadStatus = 2;
11719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
11729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
11739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int rc;
11749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RECTL rectl;
11759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fcf = ulFCFToUse; // Get from global setting
11779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
11799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[PMThreadFunc] : FSLib_CreateWindow()!\n");
11809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fflush(stdout);
11819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rc = FSLib_CreateWindow(HWND_DESKTOP, 0, &fcf,
11849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            "SDL Application",
11859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            NULLHANDLE, 0,
11869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            &(pVideo->hidden->SrcBufferDesc),
11879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            WndProc,
11889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            &(pVideo->hidden->hwndClient),
11899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            &(pVideo->hidden->hwndFrame));
11909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
11929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[PMThreadFunc] : FSLib_CreateWindow() rc = %d\n", rc);
11939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fflush(stdout);
11949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!rc)
11979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
11999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : Could not create FSLib window!\n");
12009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
12019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo->hidden->iPMThreadStatus = 3;
12039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else
12049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
12059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
12069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : FSLib_AddUserParm()!\n");
12079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
12089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Store pVideo pointer in window data for client window, so
12119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // it will know the instance to which it belongs to.
12129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      FSLib_AddUserParm(pVideo->hidden->hwndClient, pVideo);
12139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Now set default image width height and fourcc!
12159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
12169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : SetWindowPos()!\n");
12179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
12189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Set the position and size of the main window,
12219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // and make it visible!
12229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Calculate frame window size from client window size
12239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.xLeft = 0;
12249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.yBottom = 0;
12259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.xRight = pVideo->hidden->SrcBufferDesc.uiXResolution; // Noninclusive
12269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.yTop = pVideo->hidden->SrcBufferDesc.uiYResolution; // Noninclusive
12279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinCalcFrameRect(pVideo->hidden->hwndFrame, &rectl, FALSE);
12289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetAccessableWindowPos(pVideo->hidden->hwndFrame,
12309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             HWND_TOP,
12319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN) - (rectl.xRight-rectl.xLeft)) / 2,
12329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (WinQuerySysValue (HWND_DESKTOP, SV_CYSCREEN) - (rectl.yTop-rectl.yBottom)) / 2,
12339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (rectl.xRight-rectl.xLeft),
12349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (rectl.yTop-rectl.yBottom),
12359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             SWP_SIZE | SWP_ACTIVATE | SWP_SHOW | SWP_MOVE);
12369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Subclass frame procedure and store old window proc address
12389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo->hidden->pfnOldFrameProc =
12399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinSubclassWindow(pVideo->hidden->hwndFrame, FrameWndProc);
12409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinSetWindowULong(pVideo->hidden->hwndFrame, QWL_USER, (ULONG) pVideo);
12419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
12439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : Entering message loop\n"); fflush(stdout);
12449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pVideo->hidden->iPMThreadStatus = 1;
12469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      while (WinGetMsg(hab, (PQMSG)&msg, 0, 0, 0))
12489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinDispatchMsg(hab, (PQMSG) &msg);
12499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
12519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : Leaving message loop\n"); fflush(stdout);
12529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // We should release the captured the mouse!
12549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (bMouseCaptured)
12559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
12569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinSetCapture(HWND_DESKTOP, NULLHANDLE);
12579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        bMouseCaptured = 0;
12589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
12599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Destroy our window
12609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinDestroyWindow(pVideo->hidden->hwndFrame); pVideo->hidden->hwndFrame=NULL;
12619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Show pointer to make sure it will not be left hidden.
12629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinSetPointer(HWND_DESKTOP, WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE));
12639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinShowPointer(HWND_DESKTOP, TRUE);
12649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
12659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Uninitialize PM
12669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyMsgQueue(hmq);
12679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // All done!
12689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pVideo->hidden->iPMThreadStatus = 0;
12699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
12709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinTerminate(hab);
12719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Commented out, should not be needed anymore, because we send it
12729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     from WM_CLOSE.
12739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Notify SDL that it should really die now...
12749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_PrivateQuit(); SDL_PrivateQuit(); SDL_PrivateQuit(); //... :))
12759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  */
12769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
12779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[PMThreadFunc] : End, status is %d!\n", pVideo->hidden->iPMThreadStatus); fflush(stdout);
12789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iNumOfPMThreadInstances--;
12819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // HACK to prevent zombie and hanging SDL applications, which does not take
12839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // care of closing the window for some reason:
12849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // There are some apps which do not process messages, so do a lot of things
12859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // without noticing that the application should close. To close these,
12869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // I've thought about the following:
12879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If the window is closed (the execution came here), I wait a bit to
12889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // give time to the app to finish its execution. If it does not, I kill it
12899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // using DosExit(). Brute force, but should work.
12909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (pVideo->hidden->iPMThreadStatus==0)
12919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
12929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DosSleep(5000); // Wait 5 secs
12939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // If a new PM thread has been spawned (reinitializing video mode), then all right.
12949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Otherwise, we have a problem, the app doesn't want to stop. Kill!
12959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (iNumOfPMThreadInstances==0)
12969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
12979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
12989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : It seems that the application haven't terminated itself\n"); fflush(stdout);
12999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : in the last 5 seconds, so we go berserk.\n"); fflush(stdout);
13009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[PMThreadFunc] : Brute force mode. :) Killing process! Dieeeee...\n"); fflush(stdout);
13019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
13029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DosExit(EXIT_PROCESS, -1);
13039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
13049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
13059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _endthread();
13069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct WMcursor
13099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HBITMAP hbm;
13119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HPOINTER hptr;
13129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *pchData;
13139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
13149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Free a window manager cursor */
13169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid os2fslib_FreeWMCursor(_THIS, WMcursor *cursor)
13179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (cursor)
13199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
13209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    GpiDeleteBitmap(cursor->hbm);
13219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyPointer(cursor->hptr);
13229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(cursor->pchData);
13239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(cursor);
13249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
13259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Local functions to convert the SDL cursor mask into OS/2 format */
13289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void memnot(Uint8 *dst, Uint8 *src, int len)
13299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while ( len-- > 0 )
13319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *dst++ = ~*src++;
13329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void memxor(Uint8 *dst, Uint8 *src1, Uint8 *src2, int len)
13349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while ( len-- > 0 )
13369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *dst++ = (*src1++)^(*src2++);
13379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a black/white window manager cursor */
13409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallWMcursor *os2fslib_CreateWMCursor_Win(_THIS, Uint8 *data, Uint8 *mask,
13419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                      int w, int h, int hot_x, int hot_y)
13429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HPOINTER hptr;
13449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HBITMAP hbm;
13459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  BITMAPINFOHEADER bmih;
13469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  BMPINFO          bmi;
13479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HPS              hps;
13489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *pchTemp;
13499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *xptr, *aptr;
13509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int maxx, maxy;
13519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int i, run, pad;
13529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WMcursor *pResult;
13539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  maxx = WinQuerySysValue(HWND_DESKTOP, SV_CXPOINTER);
13559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  maxy = WinQuerySysValue(HWND_DESKTOP, SV_CYPOINTER);
13569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Check for max size!
13589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((w>maxx) || (h>maxy))
13599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (WMcursor *) NULL;
13609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pResult = (WMcursor *) SDL_malloc(sizeof(WMcursor));
13629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!pResult) return (WMcursor *) NULL;
13639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pchTemp = (char *) SDL_malloc((maxx + 7)/8 * maxy*2);
13659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!pchTemp)
13669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
13679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(pResult);
13689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (WMcursor *) NULL;
13699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
13709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(pchTemp, 0, (maxx + 7)/8 * maxy*2);
13729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hps = WinGetPS(_this->hidden->hwndClient);
13749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cbFix = sizeof(BITMAPINFOHEADER);
13769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cx = maxx;
13779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cy = 2*maxy;
13789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cPlanes = 1;
13799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cBitCount = 1;
13809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.argbColor[0].bBlue = 0x00;
13819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.argbColor[0].bGreen = 0x00;
13829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.argbColor[0].bRed = 0x00;
13839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.argbColor[1].bBlue = 0x00;
13849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.argbColor[1].bGreen = 0x00;
13859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.argbColor[1].bRed = 0xff;
13869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
13889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cbFix = sizeof(BITMAPINFOHEADER);
13899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cx = maxx;
13909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cy = 2*maxy;
13919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cPlanes = 1;
13929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cBitCount = 1;
13939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  run = (w+7)/8;
13959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pad = (maxx+7)/8 - run;
13969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i=0; i<h; i++)
13989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
13999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    xptr = pchTemp + (maxx+7)/8 * (maxy-1-i);
14009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    aptr = pchTemp + (maxx+7)/8 * (maxy+maxy-1-i);
14019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memxor(xptr, data, mask, run);
14029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    xptr += run;
14039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    data += run;
14049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memnot(aptr, mask, run);
14059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mask += run;
14069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    aptr += run;
14079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(xptr,  0, pad);
14089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    xptr += pad;
14099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(aptr, ~0, pad);
14109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    aptr += pad;
14119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
14129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pad += run;
14139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i=h ; i<maxy; i++ )
14149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
14159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    xptr = pchTemp + (maxx+7)/8 * (maxy-1-i);
14169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    aptr = pchTemp + (maxx+7)/8 * (maxy+maxy-1-i);
14179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(xptr,  0, (maxx+7)/8);
14199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    xptr += (maxx+7)/8;
14209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(aptr, ~0, (maxx+7)/8);
14219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    aptr += (maxx+7)/8;
14229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
14239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hbm = GpiCreateBitmap(hps, (PBITMAPINFOHEADER2)&bmih, CBM_INIT, (PBYTE) pchTemp, (PBITMAPINFO2)&bmi);
14259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hptr = WinCreatePointer(HWND_DESKTOP, hbm, TRUE, hot_x, maxy - hot_y - 1);
14269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
14289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("HotSpot          : %d ; %d\n", hot_x, hot_y);
14299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("HPS returned     : %x\n", (ULONG)hps);
14309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("HBITMAP returned : %x\n", (ULONG)hbm);
14319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("HPOINTER returned: %x\n", (ULONG)hptr);
14329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
14339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinReleasePS(hps);
14359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
14379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[CreateWMCursor] : ptr = %p\n", hptr); fflush(stdout);
14389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
14399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pResult->hptr = hptr;
14419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pResult->hbm = hbm;
14429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pResult->pchData = pchTemp;
14439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
14459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[CreateWMCursor] : ptr = %p return.\n", hptr); fflush(stdout);
14469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
14479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return (WMcursor *) pResult;
14499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
14509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallWMcursor *os2fslib_CreateWMCursor_FS(_THIS, Uint8 *data, Uint8 *mask,
14529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                     int w, int h, int hot_x, int hot_y)
14539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
14549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
14559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[CreateWMCursor_FS] : returning pointer NULL\n"); fflush(stdout);
14569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
14579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // In FS mode we'll use software cursor
14599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return (WMcursor *) NULL;
14609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
14619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Show the specified cursor, or hide if cursor is NULL */
14639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint os2fslib_ShowWMCursor(_THIS, WMcursor *cursor)
14649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
14659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
14669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[ShowWMCursor] : ptr = %p\n", cursor); fflush(stdout);
14679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
14689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (cursor)
14709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
14719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinSetPointer(HWND_DESKTOP, cursor->hptr);
14729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hptrGlobalPointer = cursor->hptr;
14739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->iMouseVisible = 1;
14749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
14759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
14769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
14779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinSetPointer(HWND_DESKTOP, FALSE);
14789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hptrGlobalPointer = NULL;
14799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->iMouseVisible = 0;
14809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
14819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
14839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[ShowWMCursor] : ptr = %p, DONE\n", cursor); fflush(stdout);
14849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
14859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
14879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
14889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Warp the window manager cursor to (x,y)
14909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall If NULL, a mouse motion event is posted internally.
14919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
14929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid os2fslib_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
14939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
14949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  LONG lx, ly;
14959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SWP swpClient;
14969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  POINTL ptlPoints;
14979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinQueryWindowPos(_this->hidden->hwndClient, &swpClient);
14989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ptlPoints.x = swpClient.x;
14999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ptlPoints.y = swpClient.y;
15009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinMapWindowPoints(_this->hidden->hwndFrame, HWND_DESKTOP, &ptlPoints, 1);
15019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  lx = ptlPoints.x + (x*swpClient.cx) / _this->hidden->SrcBufferDesc.uiXResolution;
15029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ly = ptlPoints.y + swpClient.cy - ((y*swpClient.cy) / _this->hidden->SrcBufferDesc.uiYResolution) - 1;
15039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_PrivateMouseMotion(0, // Buttons not changed
15059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         0, // Absolute position
15069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         x,
15079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         y);
15089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinSetPointerPos(HWND_DESKTOP, lx, ly);
15109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* If not NULL, this is called when a mouse motion event occurs */
15149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid os2fslib_MoveWMCursor(_THIS, int x, int y)
15159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /*
15179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_Rect rect;
15189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
15209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[MoveWMCursor] : at %d ; %d\n", x, y); fflush(stdout);
15219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
15229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rect.x = x;
15249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rect.y = y;
15259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rect.w = 32;
15269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rect.h = 32;
15279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  os2fslib_UpdateRects(_this, 1, &rect);
15289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // TODO!
15299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  */
15309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Determine whether the mouse should be in relative mode or not.
15339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This function is called when the input grab state or cursor
15349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall visibility state changes.
15359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall If the cursor is not visible, and the input is grabbed, the
15369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall driver can place the mouse in relative mode, which may result
15379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall in higher accuracy sampling of the pointer motion.
15389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
15399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid os2fslib_CheckMouseMode(_THIS)
15409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_PumpEvents(_THIS)
15449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Notify SDL that if window has been resized!
15469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (
15479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (_this->hidden->pSDLSurface) &&
15489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (_this->hidden->pSDLSurface->flags & SDL_RESIZABLE) &&
15499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (
15509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       (_this->hidden->SrcBufferDesc.uiXResolution!=iWindowSizeX) ||
15519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       (_this->hidden->SrcBufferDesc.uiYResolution!=iWindowSizeY)
15529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ) &&
15539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (iWindowSizeX>0) &&
15549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      (iWindowSizeY>0)
15559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     )
15569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
15579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    static time_t prev_time;
15589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    time_t curr_time;
15599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    curr_time = time(NULL);
15619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((difftime(curr_time, prev_time)>=0.25) ||
15629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        (bWindowResized))
15639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
15649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Make sure we won't flood the event queue with resize events,
15659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // only send them at 250 msecs!
15669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // (or when the window is resized)
15679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
15689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[os2fslib_PumpEvents] : Calling PrivateResize (%d %d).\n",
15699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             iWindowSizeX, iWindowSizeY);
15709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
15719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
15729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Tell SDL the new size
15739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_PrivateResize(iWindowSizeX, iWindowSizeY);
15749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      prev_time = curr_time;
15759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      bWindowResized = 0;
15769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
15779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
15789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't actually allow hardware surfaces other than the main one */
15819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int os2fslib_AllocHWSurface(_THIS, SDL_Surface *surface)
15829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return(-1);
15849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_FreeHWSurface(_THIS, SDL_Surface *surface)
15869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return;
15889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We need to wait for vertical retrace on page flipped displays */
15919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int os2fslib_LockHWSurface(_THIS, SDL_Surface *surface)
15929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return(0);
15949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
15959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
15969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_UnlockHWSurface(_THIS, SDL_Surface *surface)
15979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
15989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return;
15999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
16009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int os2fslib_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
16029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
16039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_SetColors] : TODO!\n"); fflush(stdout);
16049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // TODO: Implement paletted modes
16059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return(1);
16069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
16079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_DestroyIcon(HWND hwndFrame)
16099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
16109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (hptrCurrentIcon)
16119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
16129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyPointer(hptrCurrentIcon);
16139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hptrCurrentIcon = NULL;
16149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinSendMsg(hwndFrame,
16169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               WM_SETICON,
16179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               NULL,
16189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               NULL);
16199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
16209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
16229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set the window icon image */
16249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid os2fslib_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
16259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
16269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWND hwndFrame;
16279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_Surface *icon_rgb;
16289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HPOINTER hptrIcon;
16299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HBITMAP hbm;
16309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  BITMAPINFOHEADER bmih;
16319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  BMPINFO          bmi;
16329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HPS              hps;
16339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *pchTemp;
16349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *pptr, *mptr, *dptr, *dmptr;
16359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int maxx, maxy, w, h, x, y;
16369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_Rect bounds;
16379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
16399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_SetIcon] : Creating and setting new icon\n"); fflush(stdout);
16409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
16419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hwndFrame = WinQueryWindow(_this->hidden->hwndClient, QW_PARENT);
16439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure the old icon resource will be free'd!
16459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  os2fslib_DestroyIcon(hwndFrame);
16469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((!icon) || (!mask))
16489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
16499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  w = icon->w;
16519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  h = icon->h;
16529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  maxx = WinQuerySysValue(HWND_DESKTOP, SV_CXICON);
16549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  maxy = WinQuerySysValue(HWND_DESKTOP, SV_CYICON);
16559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Check for max size!
16579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((w>maxx) || (h>maxy))
16589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
16599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pchTemp = (char *) SDL_malloc(w * h*2 * 4);
16619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!pchTemp)
16629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
16639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(pchTemp, 0, w * h*2 * 4);
16659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Convert surface to RGB, if it's not RGB yet!
16679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  icon_rgb = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
16689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                  32, 0, 0, 0, 0);
16699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( icon_rgb == NULL )
16709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
16719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(pchTemp);
16729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
16739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
16749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bounds.x = 0;
16759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bounds.y = 0;
16769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bounds.w = icon->w;
16779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bounds.h = icon->h;
16789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( SDL_LowerBlit(icon, &bounds, icon_rgb, &bounds) < 0 )
16799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
16809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_FreeSurface(icon_rgb);
16819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(pchTemp);
16829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
16839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
16849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Copy pixels upside-down from RGB surface into BMP, masked with the icon mask */
16869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Pixels
16889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pptr = (char *) (icon_rgb->pixels);
16899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Mask
16909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  mptr = mask;
16919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (y=0; y<h; y++)
16939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
16949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned char uchMaskByte;
16959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
16969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Destination
16979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dptr = pchTemp + w*4 * (h-y-1);
16989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Destination mask
16999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dmptr = pchTemp + w*h*4 + w*4 * (h-y-1);
17009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (x=0; x<w; x++)
17029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
17039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (x%8==0)
17049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
17059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        uchMaskByte = (unsigned char) (*mptr);
17069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        mptr++;
17079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else
17089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        uchMaskByte <<= 1;
17099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (uchMaskByte & 0x80)
17119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
17129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Copy RGB
17139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = *pptr++;
17149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = *pptr++;
17159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = *pptr++;
17169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = *pptr++;
17179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 0;
17199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 0;
17209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 0;
17219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 0;
17229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else
17239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
17249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Set pixels to fully transparent
17259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = 0; pptr++;
17269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = 0; pptr++;
17279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = 0; pptr++;
17289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dptr++ = 0; pptr++;
17299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 255;
17319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 255;
17329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 255;
17339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dmptr++ = 255;
17349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
17359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
17369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
17379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // There is no more need for the RGB surface
17399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_FreeSurface(icon_rgb);
17409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hps = WinGetPS(_this->hidden->hwndClient);
17429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cbFix = sizeof(BITMAPINFOHEADER);
17449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cx = w;
17459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cy = 2*h;
17469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cPlanes = 1;
17479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmi.cBitCount = 32;
17489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
17509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cbFix = sizeof(BITMAPINFOHEADER);
17519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cx = w;
17529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cy = 2*h;
17539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cPlanes = 1;
17549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  bmih.cBitCount = 32;
17559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hbm = GpiCreateBitmap(hps, (PBITMAPINFOHEADER2)&bmih, CBM_INIT, (PBYTE) pchTemp, (PBITMAPINFO2)&bmi);
17579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hptrIcon = WinCreatePointer(HWND_DESKTOP, hbm, FALSE, 0, 0);
17589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinReleasePS(hps);
17609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free pixel array
17629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(pchTemp);
17639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Change icon in frame window
17659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinSendMsg(hwndFrame,
17669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             WM_SETICON,
17679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             (MPARAM) hptrIcon,
17689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             NULL);
17699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /*
17719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Change icon in switchlist
17729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Seems like it's not needed, the WM_SETICON already does it.
17739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
17749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    PID pidFrame;
17759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HSWITCH hswitchFrame;
17769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SWCNTRL swctl;
17779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinQueryWindowProcess(hwndFrame, &pidFrame, NULL);
17799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hswitchFrame = WinQuerySwitchHandle(hwndFrame, pidFrame);
17809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinQuerySwitchEntry(hswitchFrame, &swctl);
17819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    swctl.hwndIcon = hptrIcon;
17839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinChangeSwitchEntry(hswitchFrame, &swctl);
17859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
17869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  */
17879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Store icon handle in global variable
17899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hptrCurrentIcon = hptrIcon;
17909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
17919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// ------------------------ REAL FUNCTIONS -----------------
17939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
17959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_SetCursorManagementFunctions(_THIS, int iForWindowedMode)
17969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
17979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (iForWindowedMode)
17989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
17999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->FreeWMCursor = os2fslib_FreeWMCursor;
18009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->CreateWMCursor = os2fslib_CreateWMCursor_Win;
18019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->ShowWMCursor = os2fslib_ShowWMCursor;
18029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->WarpWMCursor = os2fslib_WarpWMCursor;
18039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->MoveWMCursor = os2fslib_MoveWMCursor;
18049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->CheckMouseMode = NULL;//os2fslib_CheckMouseMode;
18059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
18069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
18079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // We'll have software mouse cursor in FS mode!
18089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->FreeWMCursor = os2fslib_FreeWMCursor;
18099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->CreateWMCursor = os2fslib_CreateWMCursor_FS;
18109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->ShowWMCursor = os2fslib_ShowWMCursor;
18119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->WarpWMCursor = os2fslib_WarpWMCursor;
18129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->MoveWMCursor = os2fslib_MoveWMCursor;
18139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->CheckMouseMode = NULL;//os2fslib_CheckMouseMode;
18149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
18159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
18169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_InitOSKeymap(_THIS)
18189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
18199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int i;
18209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iShiftIsPressed = 0;
18229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Map the VK and CH keysyms */
18249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for ( i=0; i<=255; ++i )
18259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HWScanKeyMap[i] = SDLK_UNKNOWN;
18269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // First line of keyboard:
18289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1] = SDLK_ESCAPE;
18299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3b] = SDLK_F1;
18309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3c] = SDLK_F2;
18319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3d] = SDLK_F3;
18329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3e] = SDLK_F4;
18339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3f] = SDLK_F5;
18349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x40] = SDLK_F6;
18359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x41] = SDLK_F7;
18369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x42] = SDLK_F8;
18379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x43] = SDLK_F9;
18389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x44] = SDLK_F10;
18399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x57] = SDLK_F11;
18409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x58] = SDLK_F12;
18419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5d] = SDLK_PRINT;
18429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x46] = SDLK_SCROLLOCK;
18439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5f] = SDLK_PAUSE;
18449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Second line of keyboard:
18469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x29] = SDLK_BACKQUOTE;
18479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2] = SDLK_1;
18489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3] = SDLK_2;
18499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4] = SDLK_3;
18509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5] = SDLK_4;
18519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x6] = SDLK_5;
18529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x7] = SDLK_6;
18539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x8] = SDLK_7;
18549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x9] = SDLK_8;
18559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0xa] = SDLK_9;
18569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0xb] = SDLK_0;
18579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0xc] = SDLK_MINUS;
18589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0xd] = SDLK_EQUALS;
18599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0xe] = SDLK_BACKSPACE;
18609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x68] = SDLK_INSERT;
18619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x60] = SDLK_HOME;
18629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x62] = SDLK_PAGEUP;
18639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x45] = SDLK_NUMLOCK;
18649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5c] = SDLK_KP_DIVIDE;
18659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x37] = SDLK_KP_MULTIPLY;
18669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4a] = SDLK_KP_MINUS;
18679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Third line of keyboard:
18699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0xf] = SDLK_TAB;
18709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x10] = SDLK_q;
18719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x11] = SDLK_w;
18729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x12] = SDLK_e;
18739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x13] = SDLK_r;
18749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x14] = SDLK_t;
18759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x15] = SDLK_y;
18769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x16] = SDLK_u;
18779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x17] = SDLK_i;
18789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x18] = SDLK_o;
18799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x19] = SDLK_p;
18809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1a] = SDLK_LEFTBRACKET;
18819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1b] = SDLK_RIGHTBRACKET;
18829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1c] = SDLK_RETURN;
18839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x69] = SDLK_DELETE;
18849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x65] = SDLK_END;
18859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x67] = SDLK_PAGEDOWN;
18869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x47] = SDLK_KP7;
18879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x48] = SDLK_KP8;
18889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x49] = SDLK_KP9;
18899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4e] = SDLK_KP_PLUS;
18909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
18919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Fourth line of keyboard:
18929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x3a] = SDLK_CAPSLOCK;
18939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1e] = SDLK_a;
18949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1f] = SDLK_s;
18959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x20] = SDLK_d;
18969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x21] = SDLK_f;
18979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x22] = SDLK_g;
18989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x23] = SDLK_h;
18999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x24] = SDLK_j;
19009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x25] = SDLK_k;
19019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x26] = SDLK_l;
19029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x27] = SDLK_SEMICOLON;
19039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x28] = SDLK_QUOTE;
19049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2b] = SDLK_BACKSLASH;
19059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4b] = SDLK_KP4;
19069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4c] = SDLK_KP5;
19079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4d] = SDLK_KP6;
19089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Fifth line of keyboard:
19109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2a] = SDLK_LSHIFT;
19119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x56] = SDLK_WORLD_1; // Code 161, letter i' on hungarian keyboard
19129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2c] = SDLK_z;
19139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2d] = SDLK_x;
19149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2e] = SDLK_c;
19159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x2f] = SDLK_v;
19169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x30] = SDLK_b;
19179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x31] = SDLK_n;
19189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x32] = SDLK_m;
19199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x33] = SDLK_COMMA;
19209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x34] = SDLK_PERIOD;
19219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x35] = SDLK_SLASH;
19229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x36] = SDLK_RSHIFT;
19239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x61] = SDLK_UP;
19249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x4f] = SDLK_KP1;
19259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x50] = SDLK_KP2;
19269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x51] = SDLK_KP3;
19279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5a] = SDLK_KP_ENTER;
19289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Sixth line of keyboard:
19309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x1d] = SDLK_LCTRL;
19319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x7e] = SDLK_LSUPER; // Windows key
19329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x38] = SDLK_LALT;
19339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x39] = SDLK_SPACE;
19349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5e] = SDLK_RALT;// Actually, altgr on my keyboard...
19359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x7f] = SDLK_RSUPER;
19369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x7c] = SDLK_MENU;
19379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x5b] = SDLK_RCTRL;
19389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x63] = SDLK_LEFT;
19399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x66] = SDLK_DOWN;
19409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x64] = SDLK_RIGHT;
19419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x52] = SDLK_KP0;
19429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HWScanKeyMap[0x53] = SDLK_KP_PERIOD;
19439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
19449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Iconify the window.
19479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This function returns 1 if there is a window manager and the
19489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall window was actually iconified, it returns 0 otherwise.
19499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
19509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint os2fslib_IconifyWindow(_THIS)
19519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
19529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HAB hab;
19539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HMQ hmq;
19549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ERRORID hmqerror;
19559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
19579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1) return 0;
19589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Cannot do anything in fullscreen mode!
19609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (FSLib_QueryFSMode(_this->hidden->hwndClient))
19619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
19629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure this thread is prepared for using the Presentation Manager!
19649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hab = WinInitialize(0);
19659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmq = WinCreateMsgQueue(hab,0);
19669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Remember if there was an error at WinCreateMsgQueue(), because we don't
19679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // want to destroy somebody else's queue later. :)
19689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmqerror = WinGetLastError(hab);
19699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinSetWindowPos(_this->hidden->hwndFrame, HWND_TOP,
19719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 0, 0, 0, 0, SWP_MINIMIZE);
19729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now destroy the message queue, if we've created it!
19749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ERRORIDERROR(hmqerror)==0)
19759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyMsgQueue(hmq);
19769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
19789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
19799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_GrabMode os2fslib_GrabInput(_THIS, SDL_GrabMode mode)
19819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
19829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HAB hab;
19839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HMQ hmq;
19849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ERRORID hmqerror;
19859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
19889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1)
19899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return SDL_GRAB_OFF;
19909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure this thread is prepared for using the Presentation Manager!
19929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hab = WinInitialize(0);
19939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmq = WinCreateMsgQueue(hab,0);
19949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Remember if there was an error at WinCreateMsgQueue(), because we don't
19959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // want to destroy somebody else's queue later. :)
19969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmqerror = WinGetLastError(hab);
19979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
19999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (mode == SDL_GRAB_OFF)
20009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
20019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
20029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_GrabInput] : Releasing mouse\n"); fflush(stdout);
20039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
20049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Release the mouse
20069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bMouseCapturable = 0;
20079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (bMouseCaptured)
20089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
20099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinSetCapture(HWND_DESKTOP, NULLHANDLE);
20109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      bMouseCaptured = 0;
20119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
20129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
20139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
20149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
20159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_GrabInput] : Capturing mouse\n"); fflush(stdout);
20169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
20179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Capture the mouse
20199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bMouseCapturable = 1;
20209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (WinQueryFocus(HWND_DESKTOP) == _this->hidden->hwndClient)
20219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
20229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinSetCapture(HWND_DESKTOP, _this->hidden->hwndClient);
20239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      bMouseCaptured = 1;
20249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
20259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SWP swpClient;
20269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        POINTL ptl;
20279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Center the mouse to the middle of the window!
20289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinQueryWindowPos(_this->hidden->hwndClient, &swpClient);
20299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ptl.x = 0; ptl.y = 0;
20309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinMapWindowPoints(_this->hidden->hwndClient, HWND_DESKTOP, &ptl, 1);
20319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        _this->hidden->iSkipWMMOUSEMOVE++; /* Don't take next WM_MOUSEMOVE into account!  */
20329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinSetPointerPos(HWND_DESKTOP,
20339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ptl.x + swpClient.cx/2,
20349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ptl.y + swpClient.cy/2);
20359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
20369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
20379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
20389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now destroy the message queue, if we've created it!
20409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ERRORIDERROR(hmqerror)==0)
20419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyMsgQueue(hmq);
20429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return mode;
20449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
20459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set the title and icon text */
20479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_SetCaption(_THIS, const char *title, const char *icon)
20489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
20499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HAB hab;
20509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HMQ hmq;
20519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ERRORID hmqerror;
20529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
20549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1) return;
20559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure this thread is prepared for using the Presentation Manager!
20579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hab = WinInitialize(0);
20589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmq = WinCreateMsgQueue(hab,0);
20599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Remember if there was an error at WinCreateMsgQueue(), because we don't
20609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // want to destroy somebody else's queue later. :)
20619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmqerror = WinGetLastError(hab);
20629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinSetWindowText(_this->hidden->hwndFrame, (char *) title);
20649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now destroy the message queue, if we've created it!
20669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ERRORIDERROR(hmqerror)==0)
20679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyMsgQueue(hmq);
20689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
20699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int os2fslib_ToggleFullScreen(_THIS, int on)
20719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
20729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
20739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_ToggleFullScreen] : %d\n", on); fflush(stdout);
20749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
20759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
20769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1) return 0;
20779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FSLib_ToggleFSMode(_this->hidden->hwndClient, on);
20799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Cursor manager functions to Windowed/FS mode*/
20809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  os2fslib_SetCursorManagementFunctions(_this, !on);
20819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
20829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
20839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is called after the video mode has been set, to get the
20859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall initial mouse state.  It should queue events as necessary to
20869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall properly represent the current mouse focus and position.
20879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
20889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_UpdateMouse(_THIS)
20899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
20909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  POINTL ptl;
20919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HAB hab;
20929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HMQ hmq;
20939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ERRORID hmqerror;
20949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SWP swpClient;
20959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
20979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1) return;
20989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
20999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure this thread is prepared for using the Presentation Manager!
21019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hab = WinInitialize(0);
21029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmq = WinCreateMsgQueue(hab,0);
21039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Remember if there was an error at WinCreateMsgQueue(), because we don't
21049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // want to destroy somebody else's queue later. :)
21059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmqerror = WinGetLastError(hab);
21069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->fInFocus)
21109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
21119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // If our app is in focus
21129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
21139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
21149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateAppActive(1, SDL_APPACTIVE);
21159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinQueryPointerPos(HWND_DESKTOP, &ptl);
21169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinMapWindowPoints(HWND_DESKTOP, _this->hidden->hwndClient, &ptl, 1);
21179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinQueryWindowPos(_this->hidden->hwndClient, &swpClient);
21189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Convert OS/2 mouse position to SDL position, and also scale it!
21199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ptl.x = ptl.x * _this->hidden->SrcBufferDesc.uiXResolution / swpClient.cx;
21209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ptl.y = ptl.y * _this->hidden->SrcBufferDesc.uiYResolution / swpClient.cy;
21219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ptl.y = _this->hidden->SrcBufferDesc.uiYResolution - ptl.y - 1;
21229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateMouseMotion(0, 0, (Sint16) (ptl.x), (Sint16) (ptl.y));
21239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
21249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
21259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // If we're not in focus
21269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
21279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
21289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateAppActive(0, SDL_APPACTIVE);
21299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_PrivateMouseMotion(0, 0, (Sint16) -1, (Sint16) -1);
21309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
21319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now destroy the message queue, if we've created it!
21339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ERRORIDERROR(hmqerror)==0)
21349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyMsgQueue(hmq);
21359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
21379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This pointer should exist in the native video subsystem and should
21399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall point to an appropriate update function for the current video mode
21409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
21419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
21429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
21439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
21449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1) return;
21459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BITBLT_IN_WINMESSAGEPROC
21479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinSendMsg(_this->hidden->hwndClient,
21489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 WM_UPDATERECTSREQUEST,
21499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (MPARAM) numrects,
21509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (MPARAM) rects);
21519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
21529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (DosRequestMutexSem(_this->hidden->hmtxUseSrcBuffer, SEM_INDEFINITE_WAIT)==NO_ERROR)
21539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
21549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
21559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (_this->hidden->pSDLSurface)
21579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
21589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef RESIZE_EVEN_IF_RESIZABLE
21599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SWP swp;
21609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // But only blit if the window is not resizable, or if
21619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // the window is resizable and the source buffer size is the
21629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // same as the destination buffer size!
21639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinQueryWindowPos(_this->hidden->hwndClient, &swp);
21649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((_this->hidden->pSDLSurface) &&
21659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (_this->hidden->pSDLSurface->flags & SDL_RESIZABLE) &&
21669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          ((swp.cx != _this->hidden->SrcBufferDesc.uiXResolution) ||
21679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           (swp.cy != _this->hidden->SrcBufferDesc.uiYResolution)
21689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          ) &&
21699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (!FSLib_QueryFSMode(_this->hidden->hwndClient))
21709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         )
21719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
21729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Resizable surface and in resizing!
21739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // So, don't blit now!
21749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
21759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("[UpdateRects] : Skipping blit while resizing!\n"); fflush(stdout);
21769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
21779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else
21789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
21799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
21809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /*
21819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Blit the whole window
21829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FSLIB_BITBLT(_this->hidden->hwndClient, _this->hidden->pchSrcBuffer,
21839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     0, 0,
21849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     _this->hidden->SrcBufferDesc.uiXResolution,
21859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     _this->hidden->SrcBufferDesc.uiYResolution);
21869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     */
21879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
21889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          printf("[os2fslib_UpdateRects] : Blitting!\n"); fflush(stdout);
21899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
21909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
21919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Blit the changed areas
21929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for (i=0; i<numrects; i++)
21939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          FSLIB_BITBLT(_this->hidden->hwndClient, _this->hidden->pchSrcBuffer,
21949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                       rects[i].y, rects[i].x, rects[i].w, rects[i].h);
21959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
21969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
21979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
21989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     else
21999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       printf("[os2fslib_UpdateRects] : No public surface!\n"); fflush(stdout);
22009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DosReleaseMutexSem(_this->hidden->hmtxUseSrcBuffer);
22029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
22039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
22049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
22059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_UpdateRects] : Error in mutex!\n"); fflush(stdout);
22069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
22099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Reverse the effects VideoInit() -- called if VideoInit() fails
22129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall or if the application is shutting down the video subsystem.
22139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
22149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_VideoQuit(_THIS)
22159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
22169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
22179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_VideoQuit]\n"); fflush(stdout);
22189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Close PM stuff if running!
22209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus == 1)
22219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
22229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int iTimeout;
22239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinPostMsg(_this->hidden->hwndFrame, WM_QUIT, (MPARAM) 0, (MPARAM) 0);
22249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // HACK: We had this line before:
22259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    //DosWaitThread((TID *) &(_this->hidden->tidPMThread), DCWW_WAIT);
22269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // We don't use it, because the PMThread will never stop, or if it stops,
22279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // it will kill the whole process as a emergency fallback.
22289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // So, we only check for the iPMThreadStatus stuff!
22299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
22309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_VideoQuit] : Waiting for PM thread to die\n"); fflush(stdout);
22319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    iTimeout=0;
22349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ((_this->hidden->iPMThreadStatus == 1) && (iTimeout<100))
22359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
22369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      iTimeout++;
22379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DosSleep(64);
22389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
22399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
22419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_VideoQuit] : End of wait.\n"); fflush(stdout);
22429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (_this->hidden->iPMThreadStatus == 1)
22459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
22469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
22479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[os2fslib_VideoQuit] : Killing PM thread!\n"); fflush(stdout);
22489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      _this->hidden->iPMThreadStatus = 0;
22519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DosKillThread(_this->hidden->tidPMThread);
22529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (_this->hidden->hwndFrame)
22549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
22559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
22569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("[os2fslib_VideoQuit] : Destroying PM window!\n"); fflush(stdout);
22579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
22589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        WinDestroyWindow(_this->hidden->hwndFrame); _this->hidden->hwndFrame=NULL;
22609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
22619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
22629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
22649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free result of an old ListModes() call, because there is
22669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // no FreeListModes() call in SDL!
22679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->pListModesResult)
22689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
22699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(_this->hidden->pListModesResult); _this->hidden->pListModesResult = NULL;
22709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
22719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free list of available fullscreen modes
22739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->pAvailableFSLibVideoModes)
22749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
22759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSLib_FreeVideoModeList(_this->hidden->pAvailableFSLibVideoModes);
22769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->pAvailableFSLibVideoModes = NULL;
22779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
22789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free application icon if we had one
22809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (hptrCurrentIcon)
22819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
22829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyPointer(hptrCurrentIcon);
22839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    hptrCurrentIcon = NULL;
22849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
22859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
22869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
22879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set the requested video mode, returning a surface which will be
22889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall set to the SDL_VideoSurface.  The width and height will already
22899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall be verified by ListModes(), and the video subsystem is free to
22909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall set the mode to a supported bit depth different from the one
22919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall specified -- the desired bpp will be emulated with a shadow
22929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall surface if necessary.  If a new mode is returned, this function
22939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall should take care of cleaning up the current mode.
22949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
22959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *os2fslib_SetVideoMode(_THIS, SDL_Surface *current,
22969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                          int width, int height, int bpp, Uint32 flags)
22979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
22989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  static int bFirstCall = 1;
22999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FSLib_VideoMode_p pModeInfo, pModeInfoFound;
23009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FSLib_VideoMode TempModeInfo;
23019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HAB hab;
23029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HMQ hmq;
23039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ERRORID hmqerror;
23049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  RECTL rectl;
23059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_Surface *pResult;
23069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no more window, nothing we can do!
23089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1) return NULL;
23099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
23119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_SetVideoMode] : Request for %dx%d @ %dBPP, flags=0x%x\n", width, height, bpp, flags); fflush(stdout);
23129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
23139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // We don't support palette modes!
23159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (bpp==8) bpp=32;
23169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Also, we don't support resizable modes in fullscreen mode.
23189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_RESIZABLE)
23199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    flags &= ~SDL_FULLSCREEN;
23209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // No double buffered mode
23229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_DOUBLEBUF)
23239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    flags &= ~SDL_DOUBLEBUF;
23249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // And, we don't support HWSURFACE yet.
23269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_HWSURFACE)
23279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
23289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    flags &= ~SDL_HWSURFACE;
23299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    flags |= SDL_SWSURFACE;
23309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
23319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
23339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_SetVideoMode] : Changed request to %dx%d @ %dBPP, flags=0x%x\n", width, height, bpp, flags); fflush(stdout);
23349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
23359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // First check if there is such a video mode they want!
23379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pModeInfoFound = NULL;
23389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // For fullscreen mode we don't support every resolution!
23409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // So, go through the video modes, and check for such a resolution!
23419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pModeInfoFound = NULL;
23429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pModeInfo = _this->hidden->pAvailableFSLibVideoModes;
23439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while (pModeInfo)
23459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
23469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Check all available fullscreen modes for this resolution
23479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((pModeInfo->uiXResolution == width) &&
23489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        (pModeInfo->uiYResolution == height) &&
23499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        (pModeInfo->uiBPP!=8)) // palettized modes not yet supported
23509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
23519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // If good resolution, try to find the exact BPP, or at least
23529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // something similar...
23539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!pModeInfoFound)
23549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pModeInfoFound = pModeInfo;
23559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      else
23569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((pModeInfoFound->uiBPP!=bpp) &&
23579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (pModeInfoFound->uiBPP<pModeInfo->uiBPP))
23589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pModeInfoFound = pModeInfo;
23599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
23609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pModeInfo = pModeInfo->pNext;
23619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
23629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If we did not find a good fullscreen mode, then try a similar
23649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!pModeInfoFound)
23659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
23669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
23679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Requested video mode not found, looking for a similar one!\n"); fflush(stdout);
23689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
23699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Go through the video modes again, and find a similar resolution!
23709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pModeInfo = _this->hidden->pAvailableFSLibVideoModes;
23719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (pModeInfo)
23729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
23739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Check all available fullscreen modes for this resolution
23749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((pModeInfo->uiXResolution >= width) &&
23759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (pModeInfo->uiYResolution >= height) &&
23769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          (pModeInfo->uiBPP == bpp))
23779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
23789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (!pModeInfoFound)
23799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pModeInfoFound = pModeInfo;
23809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        else
23819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (((pModeInfoFound->uiXResolution-width)*(pModeInfoFound->uiYResolution-height))>
23829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ((pModeInfo->uiXResolution-width)*(pModeInfo->uiYResolution-height)))
23839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
23849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // Found a mode which is closer than the current one
23859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pModeInfoFound = pModeInfo;
23869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
23879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
23889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pModeInfo = pModeInfo->pNext;
23899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
23909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
23919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
23929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If we did not find a good fullscreen mode, then return NULL
23939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!pModeInfoFound)
23949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
23959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
23969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Requested video mode not found!\n"); fflush(stdout);
23979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
23989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
23999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
24009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
24029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_SetVideoMode] : Found mode!\n"); fflush(stdout);
24039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
24049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // We'll possibly adjust the structure, so copy out the values
24069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // into TempModeInfo!
24079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memcpy(&TempModeInfo, pModeInfoFound, sizeof(TempModeInfo));
24089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pModeInfoFound = &TempModeInfo;
24099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_RESIZABLE)
24119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
24129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
24139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Requested mode is resizable, changing width/height\n"); fflush(stdout);
24149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
24159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Change width and height to requested one!
24169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TempModeInfo.uiXResolution = width;
24179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TempModeInfo.uiYResolution = height;
24189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TempModeInfo.uiScanLineSize = width * ((TempModeInfo.uiBPP+7)/8);
24199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
24209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // We can try create new surface!
24229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure this thread is prepared for using the Presentation Manager!
24249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hab = WinInitialize(0);
24259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmq = WinCreateMsgQueue(hab,0);
24269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Remember if there was an error at WinCreateMsgQueue(), because we don't
24279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // want to destroy somebody else's queue later. :)
24289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  hmqerror = WinGetLastError(hab);
24299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (DosRequestMutexSem(_this->hidden->hmtxUseSrcBuffer, SEM_INDEFINITE_WAIT)==NO_ERROR)
24339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
24349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
24359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Creating new SW surface\n"); fflush(stdout);
24369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
24379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Create new software surface!
24399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult = SDL_CreateRGBSurface(SDL_SWSURFACE,
24409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   pModeInfoFound->uiXResolution,
24419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   pModeInfoFound->uiYResolution,
24429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   pModeInfoFound->uiBPP,
24439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ((unsigned int) pModeInfoFound->PixelFormat.ucRedMask) << pModeInfoFound->PixelFormat.ucRedPosition,
24449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ((unsigned int) pModeInfoFound->PixelFormat.ucGreenMask) << pModeInfoFound->PixelFormat.ucGreenPosition,
24459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ((unsigned int) pModeInfoFound->PixelFormat.ucBlueMask) << pModeInfoFound->PixelFormat.ucBluePosition,
24469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   ((unsigned int) pModeInfoFound->PixelFormat.ucAlphaMask) << pModeInfoFound->PixelFormat.ucAlphaPosition);
24479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pResult == NULL)
24499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
24509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DosReleaseMutexSem(_this->hidden->hmtxUseSrcBuffer);
24519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
24529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return NULL;
24539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
24549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
24569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Adjusting pixel format\n"); fflush(stdout);
24579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
24589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Adjust pixel format mask!
24609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Rmask = ((unsigned int) pModeInfoFound->PixelFormat.ucRedMask) << pModeInfoFound->PixelFormat.ucRedPosition;
24619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Rshift = pModeInfoFound->PixelFormat.ucRedPosition;
24629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Rloss = pModeInfoFound->PixelFormat.ucRedAdjust;
24639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Gmask = ((unsigned int) pModeInfoFound->PixelFormat.ucGreenMask) << pModeInfoFound->PixelFormat.ucGreenPosition;
24649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Gshift = pModeInfoFound->PixelFormat.ucGreenPosition;
24659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Gloss = pModeInfoFound->PixelFormat.ucGreenAdjust;
24669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Bmask = ((unsigned int) pModeInfoFound->PixelFormat.ucBlueMask) << pModeInfoFound->PixelFormat.ucBluePosition;
24679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Bshift = pModeInfoFound->PixelFormat.ucBluePosition;
24689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Bloss = pModeInfoFound->PixelFormat.ucBlueAdjust;
24699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Amask = ((unsigned int) pModeInfoFound->PixelFormat.ucAlphaMask) << pModeInfoFound->PixelFormat.ucAlphaPosition;
24709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Ashift = pModeInfoFound->PixelFormat.ucAlphaPosition;
24719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Aloss = pModeInfoFound->PixelFormat.ucAlphaAdjust;
24729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef REPORT_EMPTY_ALPHA_MASK
24749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->format->Amask =
24759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pResult->format->Ashift =
24769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pResult->format->Aloss = 0;
24779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
24789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Adjust surface flags
24809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->flags |= (flags & SDL_FULLSCREEN);
24819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult->flags |= (flags & SDL_RESIZABLE);
24829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // It might be that the software surface pitch is not the same as
24849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // the pitch we have, so adjust that!
24859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pModeInfoFound->uiScanLineSize = pResult->pitch;
24869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Store new source buffer parameters!
24889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memcpy(&(_this->hidden->SrcBufferDesc), pModeInfoFound, sizeof(*pModeInfoFound));
24899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->pchSrcBuffer = pResult->pixels;
24909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
24929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Telling FSLib the stuffs\n"); fflush(stdout);
24939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
24949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Tell the FSLib window the new source image format
24969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSLib_SetSrcBufferDesc(_this->hidden->hwndClient, &(_this->hidden->SrcBufferDesc));
24979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
24989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (
24999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ((flags & SDL_RESIZABLE)==0) ||
25009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        (bFirstCall)
25019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       )
25029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
25039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      bFirstCall = 0;
25049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[os2fslib_SetVideoMode] : Modifying window size\n"); fflush(stdout);
25069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Calculate frame window size from client window size
25099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.xLeft = 0;
25109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.yBottom = 0;
25119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.xRight = pModeInfoFound->uiXResolution; // Noninclusive
25129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rectl.yTop = pModeInfoFound->uiYResolution; // Noninclusive
25139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WinCalcFrameRect(_this->hidden->hwndFrame, &rectl, FALSE);
25149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Set the new size of the main window
25169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetAccessableWindowPos(_this->hidden->hwndFrame,
25179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             HWND_TOP,
25189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             0, 0,
25199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (rectl.xRight-rectl.xLeft),
25209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (rectl.yTop-rectl.yBottom),
25219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             SWP_SIZE | SWP_ACTIVATE | SWP_SHOW);
25229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
25239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Set fullscreen mode flag, and switch to fullscreen if needed!
25259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (flags & SDL_FULLSCREEN)
25269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
25279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[os2fslib_SetVideoMode] : Also trying to switch to fullscreen\n");
25299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
25309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      FSLib_ToggleFSMode(_this->hidden->hwndClient, 1);
25329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Cursor manager functions to FS mode*/
25339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      os2fslib_SetCursorManagementFunctions(_this, 0);
25349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else
25359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
25369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[os2fslib_SetVideoMode] : Also trying to switch to desktop mode\n");
25389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fflush(stdout);
25399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      FSLib_ToggleFSMode(_this->hidden->hwndClient, 0);
25419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Cursor manager functions to Windowed mode*/
25429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      os2fslib_SetCursorManagementFunctions(_this, 1);
25439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
25449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->pSDLSurface = pResult;
25469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DosReleaseMutexSem(_this->hidden->hmtxUseSrcBuffer);
25489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
25499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
25509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Could not get hmtxUseSrcBuffer!\n"); fflush(stdout);
25529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pResult = NULL;
25559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
25569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // As we have the new surface, we don't need the current one anymore!
25589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((pResult) && (current))
25599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
25609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Freeing old surface\n"); fflush(stdout);
25629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_FreeSurface(current);
25649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
25659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Redraw window
25679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WinInvalidateRegion(_this->hidden->hwndClient, NULL, TRUE);
25689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now destroy the message queue, if we've created it!
25709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ERRORIDERROR(hmqerror)==0)
25719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
25729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_SetVideoMode] : Destroying message queue\n"); fflush(stdout);
25749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WinDestroyMsgQueue(hmq);
25769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
25779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_SetVideoMode] : Done\n"); fflush(stdout);
25809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* We're done */
25839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Return with the new surface!
25859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return pResult;
25869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
25879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
25889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* List the available video modes for the given pixel format, sorted
25899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall from largest to smallest.
25909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
25919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **os2fslib_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
25929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
25939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
25949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_ListModes] : ListModes of %d Bpp\n", format->BitsPerPixel);
25959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
25969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Destroy result of previous call, if there is any
25979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->pListModesResult)
25989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
25999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(_this->hidden->pListModesResult); _this->hidden->pListModesResult = NULL;
26009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
26019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // For resizable and windowed mode we support every resolution!
26039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((flags & SDL_RESIZABLE) && ((flags & SDL_FULLSCREEN) == 0))
26049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (SDL_Rect **)-1;
26059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Check if they need fullscreen or non-fullscreen video modes!
26079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((flags & SDL_FULLSCREEN) == 0)
26089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
26109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // For windowed mode we support every resolution!
26119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (SDL_Rect **)-1;
26129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
26139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
26149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSLib_VideoMode_p pFSMode;
26159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // For fullscreen mode we don't support every resolution!
26169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Now create a new list
26179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pFSMode = _this->hidden->pAvailableFSLibVideoModes;
26189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (pFSMode)
26199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
26209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pFSMode->uiBPP == format->BitsPerPixel)
26219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
26229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_Rect *pRect = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
26239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (pRect)
26249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
26259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // Fill description
26269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pRect->x = 0;
26279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pRect->y = 0;
26289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pRect->w = pFSMode->uiXResolution;
26299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pRect->h = pFSMode->uiYResolution;
26309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
26319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//          printf("!!! Seems to be good!\n");
26329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//        printf("F: %dx%d\n", pRect->w, pRect->h);
26339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
26349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          // And insert into list of pRects
26359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (!(_this->hidden->pListModesResult))
26369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
26379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
26389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//            printf("!!! Inserting to beginning\n");
26399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
26409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // We're the first one to be inserted!
26429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            _this->hidden->pListModesResult = (SDL_Rect**) SDL_malloc(2*sizeof(SDL_Rect*));
26439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (_this->hidden->pListModesResult)
26449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
26459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              _this->hidden->pListModesResult[0] = pRect;
26469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              _this->hidden->pListModesResult[1] = NULL;
26479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } else
26489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
26499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              SDL_free(pRect);
26509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
26519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          } else
26529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
26539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // We're not the first ones, so find the place where we
26549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            // have to insert ourselves
26559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_Rect **pNewList;
26569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int iPlace, iNumOfSlots, i;
26579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
26599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//            printf("!!! Searching where to insert\n");
26609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
26619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            iPlace = -1; iNumOfSlots = 1; // Count the last NULL too!
26639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for (i=0; _this->hidden->pListModesResult[i]; i++)
26649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
26659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              iNumOfSlots++;
26669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (iPlace==-1)
26679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              {
26689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ((_this->hidden->pListModesResult[i]->w*_this->hidden->pListModesResult[i]->h)<
26699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    (pRect->w*pRect->h))
26709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
26719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  iPlace = i;
26729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
26739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              }
26749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
26759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (iPlace==-1) iPlace = iNumOfSlots-1;
26769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
26789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//            printf("!!! From %d slots, it will be at %d\n", iNumOfSlots, iPlace);
26799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
26809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
26819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            pNewList = (SDL_Rect**) SDL_realloc(_this->hidden->pListModesResult, (iNumOfSlots+1)*sizeof(SDL_Rect*));
26829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (pNewList)
26839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
26849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              for (i=iNumOfSlots;i>iPlace;i--)
26859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pNewList[i] = pNewList[i-1];
26869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              pNewList[iPlace] = pRect;
26879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              _this->hidden->pListModesResult = pNewList;
26889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } else
26899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
26909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              SDL_free(pRect);
26919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
26929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
26939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
26949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
26959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pFSMode = pFSMode->pNext;
26969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
26979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
26989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
26999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//  printf("Returning list\n");
27009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
27019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return _this->hidden->pListModesResult;
27029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
27039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialize the native video subsystem, filling 'vformat' with the
27059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "best" display pixel format, returning 0 or -1 if there's an error.
27069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
27079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int os2fslib_VideoInit(_THIS, SDL_PixelFormat *vformat)
27089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
27099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FSLib_VideoMode_p pDesktopMode;
27109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
27129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_VideoInit] : Enter\n"); fflush(stdout);
27139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
27149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Report the best pixel format. For this,
27169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // we'll use the current desktop format.
27179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pDesktopMode = FSLib_GetDesktopVideoMode();
27189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!pDesktopMode)
27199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
27209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Could not query desktop video mode!");
27219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
27229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_VideoInit] : Could not query desktop video mode!\n");
27239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
27249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
27259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
27269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Determine the current screen size */
27289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->info.current_w = pDesktopMode->uiXResolution;
27299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->info.current_h = pDesktopMode->uiYResolution;
27309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Determine the screen depth */
27329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->BitsPerPixel = pDesktopMode->uiBPP;
27339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->BytesPerPixel = (vformat->BitsPerPixel+7)/8;
27349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Rmask = ((unsigned int) pDesktopMode->PixelFormat.ucRedMask) << pDesktopMode->PixelFormat.ucRedPosition;
27369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Rshift = pDesktopMode->PixelFormat.ucRedPosition;
27379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Rloss = pDesktopMode->PixelFormat.ucRedAdjust;
27389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Gmask = ((unsigned int) pDesktopMode->PixelFormat.ucGreenMask) << pDesktopMode->PixelFormat.ucGreenPosition;
27399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Gshift = pDesktopMode->PixelFormat.ucGreenPosition;
27409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Gloss = pDesktopMode->PixelFormat.ucGreenAdjust;
27419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Bmask = ((unsigned int) pDesktopMode->PixelFormat.ucBlueMask) << pDesktopMode->PixelFormat.ucBluePosition;
27429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Bshift = pDesktopMode->PixelFormat.ucBluePosition;
27439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Bloss = pDesktopMode->PixelFormat.ucBlueAdjust;
27449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Amask = ((unsigned int) pDesktopMode->PixelFormat.ucAlphaMask) << pDesktopMode->PixelFormat.ucAlphaPosition;
27459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Ashift = pDesktopMode->PixelFormat.ucAlphaPosition;
27469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Aloss = pDesktopMode->PixelFormat.ucAlphaAdjust;
27479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef REPORT_EMPTY_ALPHA_MASK
27499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->Amask =
27509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      vformat->Ashift =
27519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      vformat->Aloss = 0;
27529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
27539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Fill in some window manager capabilities
27559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->info.wm_available = 1;
27569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Initialize some internal variables
27589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->pListModesResult = NULL;
27599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->fInFocus = 0;
27609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iSkipWMMOUSEMOVE = 0;
27619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iMouseVisible = 1;
27629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (getenv("SDL_USE_PROPORTIONAL_WINDOW"))
27649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->bProportionalResize = 1;
27659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
27669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
27679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    PPIB pib;
27689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    PTIB tib;
27699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *pchFileName, *pchTemp;
27709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char achConfigFile[CCHMAXPATH];
27719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FILE *hFile;
27729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* No environment variable to have proportional window.
27749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * Ok, let's check if this executable is in config file!
27759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
27769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    _this->hidden->bProportionalResize = 0;
27779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
27789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DosGetInfoBlocks(&tib, &pib);
27799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pchTemp = pchFileName = pib->pib_pchcmd;
27809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (*pchTemp)
27819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
27829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (*pchTemp=='\\')
27839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pchFileName = pchTemp+1;
27849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pchTemp++;
27859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
27869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (getenv("HOME"))
27879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
27889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      sprintf(achConfigFile, "%s\\.sdl.proportionals", getenv("HOME"));
27899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      hFile = fopen(achConfigFile, "rt");
27909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!hFile)
27919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
27929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Seems like the file cannot be opened or does not exist.
27939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         * Let's try to create it with defaults!
27949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         */
27959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        hFile = fopen(achConfigFile, "wt");
27969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (hFile)
27979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
27989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; This file is a config file of SDL/2, containing\n");
27999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; the list of executables that must have proportional\n");
28009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; windows.\n");
28019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, ";\n");
28029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; You can add executable filenames into this file,\n");
28039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; one under the other. If SDL finds that a given\n");
28049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; program is in this list, then that application\n");
28059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; will have proportional windows, just like if\n");
28069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; the SET SDL_USE_PROPORTIONAL_WINDOW env. variable\n");
28079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "; would have been set for that process.\n");
28089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, ";\n");
28099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "\n");
28109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fprintf(hFile, "dosbox.exe\n");
28119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          fclose(hFile);
28129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
28139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        hFile = fopen(achConfigFile, "rt");
28159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
28169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (hFile)
28189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
28199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while (fgets(achConfigFile, sizeof(achConfigFile), hFile))
28209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
28219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* Cut \n from end of string */
28229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          while (achConfigFile[strlen(achConfigFile)-1] == '\n')
28249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            achConfigFile[strlen(achConfigFile)-1] = 0;
28259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* Compare... */
28279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (stricmp(achConfigFile, pchFileName)==0)
28289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
28299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Found it in config file! */
28309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            _this->hidden->bProportionalResize = 1;
28319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
28329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
28339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
28349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fclose(hFile);
28359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
28369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
28379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
28389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosCreateMutexSem(NULL, &(_this->hidden->hmtxUseSrcBuffer), 0, FALSE);
28409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now create our window with a default size
28429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // For this, we select the first available fullscreen mode as
28449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // current window size!
28459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memcpy(&(_this->hidden->SrcBufferDesc), _this->hidden->pAvailableFSLibVideoModes, sizeof(_this->hidden->SrcBufferDesc));
28469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Allocate new video buffer!
28479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->pchSrcBuffer = (char *) SDL_malloc(_this->hidden->pAvailableFSLibVideoModes->uiScanLineSize * _this->hidden->pAvailableFSLibVideoModes->uiYResolution);
28489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!_this->hidden->pchSrcBuffer)
28499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
28509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
28519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_VideoInit] : Yikes, not enough memory for new video buffer!\n"); fflush(stdout);
28529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
28539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Not enough memory for new video buffer!\n");
28549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
28559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
28569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // For this, we need a message processing thread.
28589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // We'll create a new thread for this, which will do everything
28599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // what is related to PM
28609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iPMThreadStatus = 0;
28619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->tidPMThread = _beginthread(PMThreadFunc, NULL, 65536, (void *) _this);
28629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->tidPMThread <= 0)
28639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
28649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
28659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_VideoInit] : Could not create PM thread!\n");
28669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
28679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Could not create PM thread");
28689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
28699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
28709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_DOSSETPRIORITY
28719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Burst the priority of PM Thread!
28729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, _this->hidden->tidPMThread);
28739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
28749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Wait for the PM thread to initialize!
28759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while (_this->hidden->iPMThreadStatus==0)
28769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DosSleep(32);
28779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If the PM thread could not set up everything, then
28789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // report an error!
28799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->iPMThreadStatus!=1)
28809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
28819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
28829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_VideoInit] : PMThread reported an error : %d\n", _this->hidden->iPMThreadStatus);
28839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
28849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Error initializing PM thread");
28859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
28869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
28879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
28899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
28909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
28929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_DeleteDevice(_THIS)
28939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
28949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
28959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_DeleteDevice]\n"); fflush(stdout);
28969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
28979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free used memory
28989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FSLib_FreeVideoModeList(_this->hidden->pAvailableFSLibVideoModes);
28999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->pListModesResult)
29009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(_this->hidden->pListModesResult);
29019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this->hidden->pchSrcBuffer)
29029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(_this->hidden->pchSrcBuffer);
29039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosCloseMutexSem(_this->hidden->hmtxUseSrcBuffer);
29049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(_this->hidden);
29059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(_this);
29069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  FSLib_Uninitialize();
29079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
29089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int os2fslib_Available(void)
29109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
29119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If we can run, it means that we could load FSLib,
29139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // so we assume that it's available then!
29149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
29159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
29169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void os2fslib_MorphToPM()
29189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
29199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  PPIB pib;
29209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  PTIB tib;
29219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosGetInfoBlocks(&tib, &pib);
29239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Change flag from VIO to PM:
29259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (pib->pib_ultype==2) pib->pib_ultype = 3;
29269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
29279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *os2fslib_CreateDevice(int devindex)
29299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
29309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *device;
29319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
29339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_CreateDevice] : Enter\n"); fflush(stdout);
29349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
29359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Initialize all variables that we clean on shutdown */
29379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
29389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( device )
29399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
29409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(device, 0, (sizeof *device));
29419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Also allocate memory for private data
29429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    device->hidden = (struct SDL_PrivateVideoData *) SDL_malloc((sizeof(struct SDL_PrivateVideoData)));
29439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
29449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( (device == NULL) || (device->hidden == NULL) )
29459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
29469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_OutOfMemory();
29479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( device )
29489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_free(device);
29499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
29509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
29519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(device->hidden, 0, (sizeof *device->hidden));
29529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Set the function pointers */
29549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
29559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("[os2fslib_CreateDevice] : VideoInit is %p\n", os2fslib_VideoInit); fflush(stdout);
29569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
29579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Initialization/Query functions */
29599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->VideoInit = os2fslib_VideoInit;
29609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->ListModes = os2fslib_ListModes;
29619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetVideoMode = os2fslib_SetVideoMode;
29629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->ToggleFullScreen = os2fslib_ToggleFullScreen;
29639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UpdateMouse = os2fslib_UpdateMouse;
29649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->CreateYUVOverlay = NULL;
29659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetColors = os2fslib_SetColors;
29669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UpdateRects = os2fslib_UpdateRects;
29679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->VideoQuit = os2fslib_VideoQuit;
29689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Hardware acceleration functions */
29699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->AllocHWSurface = os2fslib_AllocHWSurface;
29709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->CheckHWBlit = NULL;
29719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FillHWRect = NULL;
29729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetHWColorKey = NULL;
29739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetHWAlpha = NULL;
29749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->LockHWSurface = os2fslib_LockHWSurface;
29759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UnlockHWSurface = os2fslib_UnlockHWSurface;
29769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FlipHWSurface = NULL;
29779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FreeHWSurface = os2fslib_FreeHWSurface;
29789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Window manager functions */
29799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetCaption = os2fslib_SetCaption;
29809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetIcon = os2fslib_SetIcon;
29819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->IconifyWindow = os2fslib_IconifyWindow;
29829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->GrabInput = os2fslib_GrabInput;
29839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->GetWMInfo = NULL;
29849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Cursor manager functions to Windowed mode*/
29859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  os2fslib_SetCursorManagementFunctions(device, 1);
29869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Event manager functions */
29879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->InitOSKeymap = os2fslib_InitOSKeymap;
29889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->PumpEvents = os2fslib_PumpEvents;
29899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* The function used to dispose of this structure */
29909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->free = os2fslib_DeleteDevice;
29919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Make sure we'll be able to use Win* API even if the application
29939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // was linked to be a VIO application!
29949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  os2fslib_MorphToPM();
29959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now initialize FSLib, and query available video modes!
29979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!FSLib_Initialize())
29989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
29999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Could not initialize FSLib!
30009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_BUILD
30019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[os2fslib_CreateDevice] : Could not initialize FSLib!\n");
30029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
30039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Could not initialize FSLib!");
30049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device->hidden);
30059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device);
30069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
30079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
30089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->hidden->pAvailableFSLibVideoModes =
30099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSLib_GetVideoModeList();
30109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
30119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return device;
30129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
30139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
30149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap OS2FSLib_bootstrap = {
30159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        "os2fslib", "OS/2 Video Output using FSLib",
30169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        os2fslib_Available, os2fslib_CreateDevice
30179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
30189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019