home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / vwin.zip / vwin / src / vwin.c < prev    next >
C/C++ Source or Header  |  1998-05-19  |  8KB  |  233 lines

  1. /***********************************************/
  2. /*                                             */
  3. /* VWIN: VIO Window manipulation               */
  4. /*                                             */
  5. /* Peter Flass <Flass@LBDC.Senate.State.NY.US> */
  6. /*                                             */
  7. /* Function: Minimize/restore OS/2 command     */
  8. /*           window via keyboard command.      */
  9. /*                                             */
  10. /* Operation: * Get the parent process id of   */
  11. /*              the current process (the OS/2  */
  12. /*              command window).               */
  13. /*            * Start a PM process and pass    */
  14. /*              PID. Then in the PM process:   */
  15. /*            * Retrieve the PM SwitchList     */
  16. /*            * Find the VIO process in the    */
  17. /*              switchlist; get its HWND.      */
  18. /*            * Send the 'MINIMIZE' or 'RESTORE*/
  19. /*              command to the VIO window.     */
  20. /*                                             */
  21. /* Requirements: Compiled witn EMX 0.9c and    */
  22. /*               OS/2 Warp 4.                  */
  23. /*                                             */
  24. /* Note: A simpler version that doesn't need   */
  25. /*       the auxiliary process will work in an */
  26. /*       OS/2 window but not a fullscreen sess.*/
  27. /*                                             */
  28. /***********************************************/
  29.  
  30. #define INCL_DOSPROCESS
  31. #define INCL_DOSSESMGR
  32. #define INCL_WINSWITCHLIST
  33. #define INCL_WINFRAMEMGR
  34. #include <os2.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38.  
  39. const UCHAR *pszCmds[] = { "MIN",       "RES",      "" };
  40. const USHORT usMsgs[] = { SC_MINIMIZE, SC_RESTORE };
  41.  
  42. VOID viowin( INT argc, PSZ argv[], ULONG ulPpid, USHORT usMsg );
  43. VOID fullscr( INT argc, PSZ argv[], ULONG ulPpid, USHORT usMsg );
  44.  
  45. main(INT argc, PSZ argv[]) {
  46.    PTIB   ptib;
  47.    PPIB   ppib;
  48.    APIRET ulRc;
  49.    UCHAR  szCmd[16];
  50.    USHORT usMsg;
  51.    INT    i; 
  52.  
  53.    /*--------------------------------*/
  54.    /* Get process id and type        */
  55.    /*--------------------------------*/
  56.    ulRc = DosGetInfoBlocks(&ptib,&ppib);
  57.  
  58.    /*--------------------------------*/
  59.    /* If PM process(3), retrieve PID */
  60.    /* from argv and execute command  */
  61.    /*--------------------------------*/
  62.    if(ppib->pib_ultype==3) pm(argc,argv);
  63.  
  64.    /*--------------------------------*/
  65.    /* Get the command                */
  66.    /*--------------------------------*/
  67.    memset(szCmd,'\0',sizeof(szCmd));
  68.    if(argc<2) usage(argv[0]);
  69.    strncpy(szCmd,argv[1],15);
  70.    strupr(szCmd);
  71.    for(i=0;*pszCmds[i]!='\0';i++) {
  72.       if(strncmp(szCmd,pszCmds[i],strlen(pszCmds[i]))==0) break;  
  73.       }
  74.    if(*pszCmds[i]=='\0') usage(argv[0]);
  75.    usMsg = usMsgs[i];
  76.  
  77.    /*--------------------------------*/
  78.    /* If Fullscreen(0)               */
  79.    /* Start PM session and pass      */
  80.    /* ID of parent process           */
  81.    /*--------------------------------*/
  82.    if(ppib->pib_ultype==0) 
  83.      fullscr(argc,argv,ppib->pib_ulppid,usMsg);
  84.  
  85.    /*--------------------------------*/
  86.    /* If VIO(2), pass pid of         */
  87.    /* parent process directly        */
  88.    /*--------------------------------*/
  89.    if(ppib->pib_ultype==2) 
  90.      viowin(argc,argv,ppib->pib_ulppid,usMsg);
  91.  
  92.    /*--------------------------------*/
  93.    /* Otherwise error                */
  94.    /*--------------------------------*/
  95.    printf("%s: This program must be run in an OS/2 session\n",argv[0]);
  96.    exit(1);                  /* Error detatched process or DOS real-mode */
  97.    }
  98.  
  99. /***********************************************/
  100. /* VIO Window, start PM process                */
  101. /*   Call window procedure directly            */
  102. /***********************************************/
  103. VOID viowin( INT argc, PSZ argv[], ULONG ulPpid, USHORT usMsg ) {
  104.    UCHAR     szPpid[16];
  105.    UCHAR     szMsg[16];
  106.    VOID     *pParm[3];
  107.  
  108.    memset((PSZ)&szPpid,'\x0',sizeof szPpid);
  109.    sprintf(szPpid,"%08X",ulPpid);
  110.    memset((PSZ)&szMsg,'\x0',sizeof szMsg);
  111.    sprintf(szMsg,"%04X",usMsg);
  112.    pParm[0] = "";
  113.    pParm[1] = (PVOID)&szPpid;
  114.    pParm[2] = (PVOID)&szMsg;
  115.    pm(3,&pParm);
  116.    exit(0);
  117.    }
  118.  
  119. /***********************************************/
  120. /* Fullscreen session, start PM process        */
  121. /*   and pass ppid                             */
  122. /***********************************************/
  123. VOID fullscr( INT argc, PSZ argv[], ULONG ulPpid, USHORT usMsg ) {
  124.    UCHAR     ucPath[CCHMAXPATH+1];
  125.    STARTDATA sd;
  126.    APIRET    ulRc;
  127.    PID       pidChildProc;
  128.    ULONG     ulChildSess;
  129.    UCHAR     szParm[24];
  130.  
  131.    memset((PSZ)&szParm,'\x0',sizeof szParm);
  132.    sprintf(szParm,"%08X %04X",ulPpid,usMsg);
  133.  
  134.    /*--------------------------------*/
  135.    /* Get my full path name          */
  136.    /*--------------------------------*/
  137.    memset((PSZ)&ucPath,'\x0',sizeof ucPath);
  138.    _execname(ucPath,sizeof ucPath);
  139.  
  140.    /*--------------------------------*/
  141.    /* Build 'STARTDATA' structrure   */
  142.    /*--------------------------------*/
  143.    memset((PSZ)&sd,'\x0',sizeof sd);
  144.    sd.Length      = sizeof sd;
  145.    sd.PgmName     = ucPath;
  146.    sd.PgmInputs   = szParm;
  147.    sd.SessionType = SSF_TYPE_PM;
  148.    sd.PgmControl  = SSF_CONTROL_MINIMIZE|SSF_CONTROL_INVISIBLE;  
  149.  
  150.    /*--------------------------------*/
  151.    /* Start PM session to send msg   */
  152.    /*--------------------------------*/
  153.    ulRc = DosStartSession( &sd, &ulChildSess, &pidChildProc );
  154.    if(ulRc!=0)
  155.       printf("%s: DosStartSession RC=%d\n",argv[0],ulRc);
  156.    exit(0);
  157.    }
  158.  
  159. /***********************************************/
  160. /* PM process: retrive caller's ppid & command */
  161. /*   and issue command                         */
  162. /* ( *NO Message Queue* )                      */
  163. /***********************************************/
  164. pm( INT argc, PSZ argv[] ) {
  165.    ULONG    ulPpid;
  166.    USHORT   usCmd;
  167.    HAB      habA;
  168.    HWND     hwndVio;
  169.    PSWBLOCK pswb;
  170.    PSWCNTRL pswc;
  171.    APIRET   ulRc;
  172.    ULONG    ulItems;
  173.    ULONG    ulBufSz;
  174.    INT      i;
  175.  
  176.    /*--------------------------------*/
  177.    /* ARGV(0)=Program path           */
  178.    /* ARGV(1)=PPID                   */
  179.    /*--------------------------------*/
  180.    if(argc<3) return;
  181.  
  182.    /*--------------------------------*/
  183.    /* Get PID of VIO process         */
  184.    /*--------------------------------*/
  185.    sscanf(argv[1],"%8x",&ulPpid);
  186.  
  187.    /*--------------------------------*/
  188.    /* Get Command code               */
  189.    /*--------------------------------*/
  190.    sscanf(argv[2],"%4hx",&usCmd);
  191.  
  192.    /*--------------------------------*/
  193.    /* Get copy of switch list        */
  194.    /*--------------------------------*/
  195.    habA = WinInitialize(0);
  196.    ulItems = WinQuerySwitchList(habA,NULL,0); /* Get # items */
  197.    ulBufSz = ulItems * sizeof(SWENTRY) + sizeof(HSWITCH);
  198.    pswb = malloc(ulBufSz);
  199.    ulItems = WinQuerySwitchList(habA,pswb,ulBufSz);
  200.    if (ulItems==0) {
  201.       exit(1);
  202.       }
  203.  
  204.    /*--------------------------------*/
  205.    /* Find VIO pid in switch list    */
  206.    /*--------------------------------*/
  207.    for(i=0;i<ulItems;i++) {
  208.       if(pswb->aswentry[i].swctl.idProcess == ulPpid) break;
  209.       } /* end for i */
  210.    if(i>=ulItems) {    /* Not found */
  211.       exit(1);
  212.       }
  213.  
  214.    /*--------------------------------*/
  215.    /* Send the command to the window */
  216.    /*--------------------------------*/
  217.    hwndVio = pswb->aswentry[i].swctl.hwnd; /* VIO Window handle */
  218.    ulRc = WinPostMsg(
  219.               hwndVio,
  220.               WM_SYSCOMMAND,
  221.               MPFROMSHORT(usCmd),
  222.               MPFROM2SHORT(CMDSRC_OTHER,FALSE)
  223.              );
  224.    ulRc = WinTerminate(habA);
  225.    free(pswb);
  226.    }
  227.  
  228. usage( UCHAR *pgm ) {
  229.    printf("Usage: %s MIN|RES\n",pgm);
  230.    exit(1);
  231.    }
  232.  
  233.