home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / float.zip / float.c next >
Text File  |  1993-05-01  |  4KB  |  168 lines

  1. /*** window floater, using tasklist name ***/
  2.  
  3. #define INCL_PM
  4. #define INCL_DOSPROCESS
  5.  
  6. #include <os2.h>
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. /*** private version of toupper ***/
  13.  
  14. char toupper(char c)
  15. {
  16.     if ((c >= 'a') && (c <= 'z'))
  17.     {
  18.         return c + 'A' - 'a';
  19.     }
  20.     else
  21.     {
  22.         return c;
  23.     }
  24. }
  25.  
  26. /*** check if the given string matches task name (for subsytem exe) ***/
  27.  
  28. BOOL FloatMatchTaskName(PSZ pszTitle, PSZ pszTaskName)
  29. {
  30.     for (; *pszTaskName; pszTitle ++, pszTaskName ++)
  31.     {
  32.         if ((!(*pszTitle)) || (toupper(*pszTitle) != toupper(*pszTaskName)))
  33.         {
  34.             return FALSE;
  35.         }
  36.     }
  37.  
  38.     return TRUE;
  39. }
  40.  
  41. /*** find window handle of the task ***/
  42.  
  43. HWND FloatFindWindow(PSZ pszTitle)
  44. {
  45.     PSWBLOCK pswblock;
  46.     ULONG cbSize;
  47.     ULONG ulCount;
  48.     HAB hab;
  49.     PSWENTRY pswentry;
  50.     PSWCNTRL pswcntrl;
  51.  
  52.     hab = WinQueryAnchorBlock(HWND_DESKTOP);
  53.  
  54.     ulCount = WinQuerySwitchList(hab, NULL, 0L);
  55.  
  56.     cbSize = sizeof(SWENTRY) * ulCount + sizeof(ULONG);
  57.     pswblock = (PSWBLOCK)malloc(cbSize);
  58.  
  59.     ulCount = WinQuerySwitchList(hab, pswblock, cbSize);
  60.  
  61.     for (ulCount = 0; ulCount < pswblock->cswentry; ulCount ++)
  62.     {
  63.         pswentry = pswblock->aswentry + ulCount;
  64.         pswcntrl = &pswentry->swctl;
  65.  
  66.         if (FloatMatchTaskName(pswcntrl->szSwtitle, pszTitle))
  67.         {
  68.             return pswcntrl->hwnd;
  69.         }
  70.     }
  71.  
  72.     return (HWND)NULLHANDLE;
  73. }
  74.  
  75. /*** main routine ***/
  76.  
  77. #define MAIN_OK 0
  78. #define MAIN_ERROR -1
  79.  
  80. int main(int lArgc, PSZ pszArgv[])
  81. {
  82.     LONG lInterval;
  83.     HWND hwnd;
  84.     PSZ pszTaskName;
  85.     PSZ pszInterval;
  86.     SWP swpSaved;
  87.  
  88.     if (lArgc < 3)
  89.     {
  90.         printf("usage: float interval-in-ms partial-taskname\n");
  91.         exit(MAIN_ERROR);
  92.     }
  93.  
  94.     pszInterval = pszArgv[1];
  95.     pszTaskName = pszArgv[2];
  96.  
  97.     lInterval = atol(pszInterval);
  98.  
  99.     if (*pszTaskName == '0')
  100.     {
  101.         pszTaskName ++;
  102.         if (toupper(*pszTaskName) == 'X')
  103.         {
  104.             sscanf(++pszTaskName, "%lX", &hwnd);
  105.         }
  106.         else
  107.         {
  108.             sscanf(pszTaskName, "%ld", &hwnd);
  109.         }
  110.     }
  111.     else
  112.     {
  113.         hwnd = FloatFindWindow(pszTaskName);
  114.     }
  115.  
  116.     if (!hwnd)
  117.     {
  118.         printf("error: no window found for [%s]\n", pszTaskName);
  119.         exit(MAIN_ERROR);
  120.     }
  121.  
  122.     if (WinIsWindowShowing(hwnd))
  123.     {
  124.         if (!WinQueryWindowPos(hwnd, &swpSaved))
  125.         {
  126.             printf("error: cannot save position\n");
  127.             exit(MAIN_ERROR);
  128.         }
  129.     }
  130.     else
  131.     {
  132.         printf("error: the window must be showing\n");
  133.         exit(MAIN_ERROR);
  134.     }
  135.  
  136.     for (;;)
  137.     {
  138.         if (!WinIsWindowShowing(hwnd))
  139.         {
  140.             if (!WinSetWindowPos(hwnd, HWND_TOP, swpSaved.x, swpSaved.y, 0L, 0L, SWP_MOVE))
  141.             {
  142.                 printf("error: cannot move to saved position\n");
  143.                 exit(MAIN_ERROR);
  144.             }
  145.         }
  146.         else
  147.         {
  148.             if (!WinQueryWindowPos(hwnd, &swpSaved))
  149.             {
  150.                 printf("error: cannot update saved position\n");
  151.                 exit(MAIN_ERROR);
  152.             }
  153.         }
  154.  
  155.         if (WinSetWindowPos(hwnd, HWND_TOP, 0L, 0L, 0L, 0L, SWP_ZORDER))
  156.         {
  157.             DosSleep(lInterval);
  158.         }
  159.         else
  160.         {
  161.             printf("error: cannot change z-order\n");
  162.             exit(MAIN_ERROR);
  163.         }
  164.     }
  165.  
  166.     exit(MAIN_OK);
  167. }
  168.