home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cons-010.zip / Console / src / CWRP.c < prev    next >
C/C++ Source or Header  |  1997-09-10  |  9KB  |  332 lines

  1. /******************************************************************************\
  2. |*                                                                            *|
  3. |* Console Window Request Processor                                           *|
  4. |* Copyright (C) 1997 by FRIENDS software                                     *|
  5. |* All Rights Reserved                                                        *|
  6. |* Portability: OS/2                                                          *|
  7. |*                                                                            *|
  8. |* This program is free software; you can redistribute it and/or modify       *|
  9. |* it under the terms of the GNU General Public License as published by       *|
  10. |* the Free Software Foundation; either version 2 of the License, or          *|
  11. |* (at your option) any later version.                                        *|
  12. |*                                                                            *|
  13. |* This program is distributed in the hope that it will be useful,            *|
  14. |* but WITHOUT ANY WARRANTY; without even the implied warranty of             *|
  15. |* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *|
  16. |* GNU General Public License for more details.                               *|
  17. |*                                                                            *|
  18. |* You should have received a copy of the GNU General Public License          *|
  19. |* along with this program; if not, write to the Free Software                *|
  20. |* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  *|
  21. |*                                                                            *|
  22. \******************************************************************************/
  23.  
  24. #define INCL_DOS
  25. #define INCL_DOSERRORS
  26. #define INCL_WIN
  27. #include <os2.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include "CWRP.h"
  32.  
  33. static tConsoleManagerSharedMem *ShMem;
  34. static PFNWP OldWP;
  35. static void *share = NULL;
  36. static PID gavepid = 0;
  37.  
  38. void error(int errno)
  39. {
  40.  int i;
  41.  char *msg;
  42.  
  43.  switch (errno)
  44.  {
  45.   case 1:
  46.    msg = "Cannot create shared memory -- is CWRP already running?";
  47.    break;
  48.   case 2:
  49.    msg = "Cannot create console agent window";
  50.    break;
  51.   case 3:
  52.    msg = "Invalid Console Window request";
  53.    break;
  54.   case 4:
  55.    msg = "Cannot create some semaphores";
  56.    break;
  57.   case 5:
  58.    msg = "This program is not intended to be launched manually! "
  59.          "It is run at appropiate time automatically.";
  60.    break;
  61.   case 6:
  62.    msg = "Cannot obtain font dialog window handle";
  63.    break;
  64.   default:
  65.    return;
  66.  }
  67.  
  68.  for (i = 0; i < 10; i++)
  69.   DosBeep(i * 100 + errno * 500 + 1000, 30);
  70.  
  71.  WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, msg, "CWRP error",
  72.                0, MB_CANCEL | MB_ERROR | MB_MOVEABLE);
  73. }
  74.  
  75. static int lockCount = 0;
  76.  
  77. BOOL LockUpdate()
  78. {
  79.  lockCount++;
  80.  WinLockWindowUpdate(HWND_DESKTOP, HWND_DESKTOP);
  81.  return (lockCount == 1);
  82. }
  83.  
  84. BOOL UnlockUpdate()
  85. {
  86.  if (--lockCount == 0)
  87.   WinLockWindowUpdate(HWND_DESKTOP, NULLHANDLE);
  88.  return (lockCount == 0);
  89. }
  90.  
  91. BOOL ShareMemoryWith(PID pid)
  92. {
  93.  if (gavepid != pid)
  94.  {
  95.   gavepid = pid;
  96.   if (DosAllocSharedMem((PPVOID)&share, NULL, 4096, OBJ_GIVEABLE | OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR)
  97.    return FALSE;
  98.   if (DosGiveSharedMem((PVOID)share, pid, PAG_READ | PAG_WRITE) != NO_ERROR)
  99.   {
  100.    DosFreeMem((PVOID)share);
  101.    return FALSE;
  102.   }
  103.  }
  104.  return TRUE;
  105. }
  106.  
  107. BOOL InitSharedMem()
  108. {
  109.  return (DosAllocSharedMem(
  110.   (PVOID)&ShMem,                /* Pointer to object pointer */
  111.   cwrpSharedMem,                   /* Shared memory name */
  112.   sizeof(*ShMem),                   /* Desired size of object */
  113.   PAG_COMMIT |                        /* Commit memory */
  114.   PAG_WRITE)                /* Allocate memory as read/write */
  115.   == NO_ERROR);
  116. }
  117.  
  118. BOOL InitSem()
  119. {
  120.  if (DosCreateEventSem(
  121.   NULL,                            /* Unnamed semaphore */
  122.   &ShMem->ready,                     /* Semaphore handle */
  123.   DC_SEM_SHARED,                     /* Shared semaphore */
  124.   FALSE)                           /* Not posted */
  125.   != NO_ERROR)
  126.   return FALSE;
  127.  
  128.  if (DosCreateMutexSem(
  129.   NULL,                            /* Unnamed semaphore */
  130.   &ShMem->inuse,                     /* Semaphore handle */
  131.   DC_SEM_SHARED,                     /* Shared semaphore */
  132.   FALSE)                            /* Not owned */
  133.   != NO_ERROR)
  134.   return FALSE;
  135.  
  136.  return TRUE;
  137. }
  138.  
  139. BOOL SetConsoleFont(HWND vioHWND)
  140. {
  141.  int i;
  142.  HWND dlgHWND, listHWND;
  143.  
  144.  LockUpdate();
  145.  WinPostMsg(vioHWND, WM_SYSCOMMAND, (MPARAM)153, MPFROM2SHORT(CMDSRC_MENU, FALSE));
  146.  for (i = 0; i < 100; i++)
  147.  {
  148.   dlgHWND = WinQueryWindow(vioHWND, QW_NEXTTOP);
  149.   listHWND = WinWindowFromID(dlgHWND, 506);
  150.   if (dlgHWND && listHWND) break;
  151.   DosSleep(100);
  152.  }
  153.  if (!dlgHWND || !listHWND) { error(6); goto failed; }
  154.  WinShowWindow(dlgHWND, FALSE);
  155.  UnlockUpdate();
  156.  WinSendMsg(listHWND, LM_SELECTITEM, (MPARAM)ShMem->fNo, (MPARAM)TRUE);
  157.  WinSendMsg(dlgHWND, WM_COMMAND, (MPARAM)501, MPFROM2SHORT(CMDSRC_PUSHBUTTON, FALSE));
  158.  return TRUE;
  159.  
  160. failed:
  161.  UnlockUpdate();
  162.  return FALSE;
  163. }
  164.  
  165. ULONG QueryConsoleFont(HWND vioHWND)
  166. {
  167.  int i;
  168.  HWND dlgHWND, listHWND;
  169.  
  170.  LockUpdate();
  171.  WinPostMsg(vioHWND, WM_SYSCOMMAND, (MPARAM)153, MPFROM2SHORT(CMDSRC_MENU, FALSE));
  172.  for (i = 0; i < 100; i++)
  173.  {
  174.   dlgHWND = WinQueryWindow(vioHWND, QW_NEXTTOP);
  175.   listHWND = WinWindowFromID(dlgHWND, 506);
  176.   if (dlgHWND && listHWND) break;
  177.   DosSleep(100);
  178.  }
  179.  if (!dlgHWND || !listHWND) { error(6); goto failed; }
  180.  WinShowWindow(dlgHWND, FALSE);
  181.  UnlockUpdate();
  182.  
  183.  i = (int)WinSendMsg(listHWND, LM_QUERYSELECTION, (MPARAM)LIT_FIRST, (MPARAM)0);
  184.  WinSendMsg(dlgHWND, WM_COMMAND, (MPARAM)DID_CANCEL, MPFROM2SHORT(CMDSRC_PUSHBUTTON, FALSE));
  185.  return i;
  186.  
  187. failed:
  188.  UnlockUpdate();
  189.  return -1;
  190. }
  191.  
  192. BOOL ResizeConsole(HWND vioHWND)
  193. {
  194.  SWP swp;
  195.  
  196.  WinQueryWindowPos(vioHWND, &swp);
  197.  if (swp.fl & SWP_MINIMIZE)
  198.  {
  199.   if (ShMem->swp.fl & SWP_MOVE)
  200.   {
  201.    WinSetWindowUShort(vioHWND, QWS_XRESTORE, ShMem->swp.x);
  202.    WinSetWindowUShort(vioHWND, QWS_YRESTORE, ShMem->swp.y);
  203.   }
  204.   if (ShMem->swp.fl & SWP_SIZE)
  205.   {
  206.    WinSetWindowUShort(vioHWND, QWS_CXRESTORE, ShMem->swp.cx);
  207.    WinSetWindowUShort(vioHWND, QWS_CYRESTORE, ShMem->swp.cy);
  208.   }
  209.   return TRUE;
  210.  }
  211.  else
  212.   return (WinSetWindowPos(vioHWND, ShMem->swp.hwndInsertBehind,
  213.            ShMem->swp.x, ShMem->swp.y, ShMem->swp.cx, ShMem->swp.cy, ShMem->swp.fl));
  214. }
  215.  
  216. BOOL ShowConsole(HWND vioHWND)
  217. {
  218.  WinSetWindowPos(vioHWND, NULLHANDLE, 0,0,0,0, ShMem->Show ? SWP_SHOW : SWP_HIDE);
  219.  return TRUE;
  220. }
  221.  
  222. ULONG QueryConsoleBorder(HWND vioHWND)
  223. {
  224.  PID pid;
  225.  TID tid;
  226.  PPOINTL bs;
  227.  
  228.  WinQueryWindowProcess(vioHWND, &pid, &tid);
  229.  if (!ShareMemoryWith(pid))
  230.   goto fallback;
  231.  
  232.  bs = share;
  233.  if (!WinSendMsg(vioHWND, WM_QUERYBORDERSIZE, MPFROMP(bs), (MPARAM)0))
  234.   goto fallback;
  235.  return (bs->x | (bs->y << 16));
  236.  
  237. fallback:
  238.  return (WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER) |
  239.         (WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER) << 16));
  240. }
  241.  
  242. MRESULT ConManProc(HWND wnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  243. {
  244.  switch (msg)
  245.  {
  246.   case WM_CONREQUEST:
  247.   {
  248. #ifdef DEBUG
  249.    char *txt = alloca(256);
  250.    sprintf(txt, "<%08x> <%d> <%08x>", (unsigned int)wnd, (int)mp1, (int)mp2);
  251.    WinSetWindowText(wnd, (PSZ)txt);
  252. #endif
  253.    switch ((ULONG)mp1)
  254.    {
  255.     case cwrqLockOutput:
  256.      LockUpdate();
  257.      break;
  258.     case cwrqUnlockOutput:
  259.      UnlockUpdate();
  260.      break;
  261.     case cwrqSetFont:
  262.      SetConsoleFont((HWND)mp2);
  263.      break;
  264.     case cwrqSetPos:
  265.      ResizeConsole((HWND)mp2);
  266.      break;
  267.     case cwrqShow:
  268.      ShowConsole((HWND)mp2);
  269.      break;
  270.     case cwrqQueryFont:
  271.      ShMem->ret = QueryConsoleFont((HWND)mp2);
  272.      break;
  273.     case cwrqQueryBorder:
  274.      ShMem->ret = QueryConsoleBorder((HWND)mp2);
  275.      break;
  276.     default:
  277.      error(3);
  278.    }
  279.    DosPostEventSem(ShMem->ready);
  280.    return (MRESULT)0;
  281.   }
  282.   default:
  283.    return OldWP(wnd, msg, mp1, mp2);
  284.  }
  285. }
  286.  
  287. int main(int argc, char *argv[])
  288. {
  289.  HAB ab = WinInitialize(0);
  290.  HMQ mq = WinCreateMsgQueue(ab, 0);
  291.  QMSG msg;
  292.  
  293. #ifndef DEBUG
  294.  if (strcmp(argv[0], arg0_magic) != 0)
  295.  {
  296.   error(5);
  297.   goto failed;
  298.  }
  299. #endif
  300.  
  301.  if (!InitSharedMem()) { error(1); goto failed; }
  302.  if (!InitSem())       { error(4); goto failed; }
  303.  
  304.  ShMem->manager = WinCreateWindow(
  305.   HWND_DESKTOP,                        /* Parent window */
  306.   WC_STATIC,                           /* Class name */
  307.   "Console Window Request Processor",              /* Window text */
  308. #ifdef DEBUG
  309.   SS_TEXT | DT_CENTER | WS_VISIBLE,             /* Window style */
  310. #else
  311.   SS_TEXT,                         /* Window style */
  312. #endif
  313.   0, 0,                               /* Position (x,y) */
  314.   200, 40,                      /* Size (width,height) */
  315.   NULLHANDLE,                         /* Owner window */
  316.   HWND_TOP,                           /* Sibling window */
  317.   0,                                /* Window id */
  318.   NULL,                             /* Control data */
  319.   NULL);                          /* Pres parameters */
  320.  
  321.  if (!ShMem->manager) { error(2); goto failed; }
  322.  
  323.  OldWP = WinSubclassWindow(ShMem->manager, &ConManProc);
  324.  while (WinGetMsg(ab, &msg, 0, 0, 0))
  325.   WinDispatchMsg(ab, &msg);
  326.  
  327. failed:
  328.  WinDestroyMsgQueue(mq);
  329.  WinTerminate(ab);
  330.  return 0;
  331. }
  332.