home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / JUMP.ZIP / JUMPLDR.C < prev    next >
Text File  |  1991-09-05  |  13KB  |  335 lines

  1. /*---------------------------------------------------------------------
  2.  
  3.      This loads the system hook dll (jumphook.dll) and starts the
  4.      program that actually does the 3270 Window controlling (jump.exe)
  5.  
  6. ----------------------------------------------------------------------*/
  7. #define INCL_DOS
  8. #define INCL_ERRORS
  9. #define INCL_PM
  10. #define NUL '\0'
  11.  
  12. #include <os2.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include "jumpldr.h"
  17.  
  18. HAB              hab;
  19. HMODULE          hmodule = (HMODULE)NULL;
  20. PFN              pfnInputHook;
  21.  
  22. MRESULT EXPENTRY JumpWndProc( HWND, USHORT, MPARAM, MPARAM );
  23.  
  24. VOID cdecl main()
  25.   {
  26.   HMQ              hmq;
  27.   HPOINTER         hptrIcon;
  28.   HWND             hwndDlg;
  29.  
  30.   hab = WinInitialize( NUL );
  31.   hmq = WinCreateMsgQueue( hab, 0 );
  32.  
  33.  
  34.  
  35.   if((DosCreateSem(1,&hsemDie,DIE_SEM_NAME)==ERROR_ALREADY_EXISTS));
  36.       DosOpenSem(&hsemDie,DIE_SEM_NAME);
  37.   if((DosCreateSem(1,&hsemJump,JUMP_SEM_NAME)==ERROR_ALREADY_EXISTS));
  38.       DosOpenSem(&hsemJump,JUMP_SEM_NAME);
  39.   DosSemSet(hsemJump);
  40.   DosSemRequest(hsemDie,0);
  41.  
  42.   hwndDlg = WinLoadDlg( HWND_DESKTOP,        /* Load the dialog window     */
  43.                         HWND_DESKTOP,        /* which allows turning the   */
  44.                         JumpDlgProc,         /* hook on and off.  This     */
  45.                         NUL,                /* window also provides a     */
  46.                         IDD_JUMPLDR,         /* visual notification that a */
  47.                         NUL );              /* hook may be active (if set)*/
  48.  
  49.   hptrIcon = WinLoadPointer( HWND_DESKTOP, NUL, IDD_JUMPLDR );
  50.  
  51.   WinSendMsg( hwndDlg,                       /* Attach the icon to the     */
  52.               WM_SETICON,                    /* frame window.              */
  53.               (MPARAM)hptrIcon,
  54.               (MPARAM)NULL );
  55.  
  56.   WinProcessDlg( hwndDlg );
  57.  
  58.   WinReleaseHook( hab,            /* Make sure hook is released */
  59.                   (HMQ)NULL,
  60.                   HK_INPUT,
  61.                   pfnInputHook,
  62.                   hmodule );
  63.  
  64.   /* clear the semaphores - first the DIE semaphore so when
  65.      the JUMP semaphore is cleared, JUMP.EXE knows to exit */
  66.   DosSemClear( hsemDie );
  67.   DosSemClear( hsemJump );
  68.   WinDestroyWindow( hwndDlg );
  69.  
  70.   WinDestroyMsgQueue( hmq );
  71.  
  72.   WinTerminate( hab );
  73.  
  74.   return;
  75.   }
  76.  
  77.  
  78. /***************************************************************************|
  79. | Change Pointer Window Procedure                                             |
  80. |***************************************************************************/
  81.  
  82. MRESULT EXPENTRY JumpDlgProc( hwnd, msg, mp1, mp2 )
  83. HWND         hwnd;
  84. USHORT       msg;
  85. MPARAM       mp1;
  86. MPARAM       mp2;
  87. {
  88.   USHORT           usCheckedState;
  89.   STARTDATA        StartData;     /* Start session data */
  90.   PSTARTDATA       pStartData;     /* Start session data */
  91.   USHORT           SessID;        /* Session ID (returned) */
  92.   USHORT           PID;           /* Process ID (returned) */
  93.   char start_sess_args[18]=" /c detach jump ";
  94.   HWND        hwndSysMenu;          /* system menu handle       */
  95.  
  96.   switch(msg)
  97.   {
  98.  
  99.     case WM_INITDLG:
  100.     {
  101.       USHORT      idSysMenu;            /* system menu id           */
  102.       MENUITEM    miSysMenu;            /* system menu item info    */
  103.       MENUITEM    miMenuItem;           /* About menu item info     */
  104.       USHORT           rc = 0;           /* return code */
  105.  
  106.         /* add About item to system menu */
  107.         if ( hwndSysMenu = WinWindowFromID( hwnd, FID_SYSMENU ) )
  108.         {
  109.           /* get handle of system submenu */
  110.           idSysMenu = SHORT1FROMMR( WinSendMsg( hwndSysMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT( 0 ), 0L ) );
  111.           WinSendMsg( hwndSysMenu, MM_QUERYITEM, MPFROM2SHORT( idSysMenu, FALSE ), MPFROMP( &miSysMenu ) );
  112.           hwndSysMenu = miSysMenu.hwndSubMenu;
  113.  
  114.           /* add menu separator */
  115.           miMenuItem.iPosition = MIT_END;
  116.           miMenuItem.afStyle = MIS_SEPARATOR;
  117.           miMenuItem.afAttribute = 0;
  118.           miMenuItem.id = 0;
  119.           miMenuItem.hwndSubMenu = NUL;
  120.           miMenuItem.hItem = NUL;
  121.           WinSendMsg( hwndSysMenu, MM_INSERTITEM, MPFROMP( &miMenuItem ), NUL );
  122.  
  123.           /* add About item */
  124.           miMenuItem.afStyle = MIS_TEXT;
  125.           miMenuItem.id = IDM_ABOUT;
  126.           WinSendMsg( hwndSysMenu, MM_INSERTITEM, MPFROMP( &miMenuItem ), MPFROMP( "A~bout..." ) );
  127.  
  128.        }
  129.  
  130.       WinSetDlgItemText( hwnd,            /* Set title bar text        */
  131.                           FID_TITLEBAR,
  132.                         "Session Jumper" );
  133.  
  134.       WinSendMsg( WinWindowFromID( hwnd,IDCB_JUMPSW ),
  135.                   BM_SETCHECK,                /* set check box */
  136.                   MPFROMSHORT(1),
  137.                   (MPARAM)NULL );
  138.  
  139.  
  140.       rc = DosLoadModule( NULL,                /* Load the DLL module        */
  141.                       0,                        /* containing the Hook        */
  142.                       "jumphook",               /* procedure (must use a DLL).*/
  143.                       &hmodule );               /* Get module handle          */
  144.       if(rc)
  145.       {
  146.           WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  147.                         "There was an error in loading JUMPHOOK.DLL",
  148.                         "DLL could not be loaded",
  149.                          0,MB_OK | MB_ERROR);
  150.           WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  151.       }
  152.       else
  153.       {
  154.           rc = DosGetProcAddr( hmodule,            /* Get address of Hook Proc.  */
  155.                           "JUMPHOOKPROC",          /* FOR THE WINSETHOOK AND     */
  156.                           &pfnInputHook );         /* WinReleaseHook calls.      */
  157.           if(rc)
  158.           {
  159.              WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  160.                            "There is an error in JUMPHOOK.DLL",
  161.                            "Error in DLL",
  162.                             0,MB_OK | MB_ERROR);
  163.              WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  164.           }
  165.           else
  166.           {
  167.              /* Start the jump program detached              */
  168.              /* so it doesn't show up in the task list or    */
  169.              /* as an icon                                   */
  170.              /* JUMP.EXE actually controls the 3270 sessions */
  171.  
  172.              StartData.Related = 1;         /* 0=independent session, 1=child session */
  173.              StartData.FgBg = 1;          /* 0=start in foreground, 1=start in background */
  174.              StartData.TraceOpt = 0;         /* 0=no trace, 1=trace */
  175.              StartData.PgmTitle = NULL;      /* address of program title */
  176.              StartData.PgmName = NULL;       /* address of program name */
  177.              StartData.TermQ = NULL;         /* address of program queue name */
  178.              StartData.Environment = NULL; /* address of environment string */
  179.              StartData.InheritOpt = 0;       /* inherit option (shell of program) */
  180.              StartData.SessionType = 2;      /* session type (standard, windowed, ) */
  181.              StartData.IconFile = NULL;         /* address of icon definition */
  182.              StartData.PgmHandle = 0;        /* program handle */
  183.              StartData.PgmControl=4;           /* initial state of windowed application */
  184.              StartData.InitXPos=0;             /* x coordinate of initial session window */
  185.              StartData.InitYPos=0;             /* y coordinate of initial session window */
  186.              StartData.InitXSize=0;            /* initial size of x */
  187.              StartData.InitYSize=0;            /* initial size of y */
  188.              StartData.Length = sizeof(StartData);               /* length of data structure in bytes */
  189.              pStartData = &StartData;
  190.              StartData.PgmInputs = (PSZ)&start_sess_args;
  191.              rc = DosStartSession(pStartData, &SessID, &PID);
  192.              /* Quit if JUMP.EXE could not be started */
  193.              if(rc)
  194.              {
  195.                 WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  196.                               "Place JUMP.EXE in a sub-directory on your PATH",
  197.                               "JUMP.EXE could not be found",
  198.                                0,MB_OK | MB_ERROR);
  199.                 WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  200.              }
  201.              WinSetHook( hab,
  202.                          (HMQ)NULL,   /* set hook to capture*/
  203.                          HK_INPUT,    /* input messages for filtering.*/
  204.                          pfnInputHook,
  205.                          hmodule );
  206.          }
  207.       }
  208.       break;
  209.     }
  210.  
  211.     case WM_COMMAND:
  212.       switch ( COMMANDMSG( &msg )->cmd )
  213.       {
  214.         case IDM_ABOUT:
  215.           WinDlgBox( HWND_DESKTOP, hwnd, AboutDlgProc, NUL, IDD_ABOUT, NUL );
  216.           return( (MRESULT)TRUE );
  217.           break;
  218.       break;
  219.       }
  220.  
  221.     case WM_CONTROL:
  222.       if(    (SHORT1FROMMP(mp1) != IDCB_JUMPSW)   /* Only care about CLICK */
  223.           || (SHORT2FROMMP(mp1) != BN_CLICKED) )  /* Check Box messages.   */
  224.         break;
  225.       usCheckedState                               /* Query if check box is*/
  226.         = SHORT1FROMMR( WinSendDlgItemMsg( hwnd,  /* set or not after     */
  227.                                     IDCB_JUMPSW,
  228.                                     BM_QUERYCHECK, /* activity in the check*/
  229.                                     (MPARAM)NULL,  /* box.                 */
  230.                                     (MPARAM)NULL ) );
  231.       if( usCheckedState == 1 )
  232.       {
  233.         WinSetHook( hab,                   /* Check box is checked so make */
  234.                     (HMQ)NULL,             /* sure hook is "set" to capture*/
  235.                     HK_INPUT,              /* input messages for filtering.*/
  236.                     pfnInputHook,
  237.                     hmodule );
  238.       }
  239.       else
  240.       {
  241.         WinReleaseHook( hab,               /* Check box is not checked so  */
  242.                         (HMQ)NULL,         /* make sure hook is not "set"  */
  243.                         HK_INPUT,          /* so input messages are not    */
  244.                         pfnInputHook,      /* intercepted by the hook      */
  245.                         hmodule );         /* procedure.                   */
  246.       }
  247.       break;
  248.  
  249.     case WM_ERASEBACKGROUND:
  250.       return( (MRESULT)TRUE );
  251.       break;
  252.  
  253.     case WM_MINMAXFRAME:
  254.       {
  255.         PSWP        pswp;                     /* pos change structure */
  256.  
  257.         /* hide check box when minimized so it doesn't overwrite icon */
  258.         pswp = PVOIDFROMMP( mp1 );
  259.         WinShowWindow(
  260.           WinWindowFromID( hwnd, IDCB_JUMPSW ),
  261.           !(pswp->fs & SWP_MINIMIZE)
  262.         );
  263.       }
  264.       break;
  265.  
  266.  
  267.   }
  268.  
  269.   return( WinDefDlgProc( hwnd, msg, mp1, mp2 ) );
  270. }
  271.  
  272. /*
  273.  * AboutDlgProc( hwndDlg, usMsg, mp1, mp2 ) : MRESULT;
  274.  *
  275.  *    hwndDlg        handle to dialog box
  276.  *    usMsg          message number
  277.  *    mp1            message parameter 1
  278.  *    mp2            message parameter 2
  279.  *
  280.  * This is the dialog procedure for the About dialog box.
  281.  *
  282.  */
  283.  
  284. MRESULT EXPENTRY AboutDlgProc( HWND hwndDlg, USHORT usMsg, MPARAM mp1, MPARAM mp2 )
  285. {
  286.   MRESULT       mresRtnVal;                 /* function return value  */
  287.   BOOL          fPassToDef;                 /* pass to def dlg proc?  */
  288.  
  289.   mresRtnVal = FALSE;
  290.   fPassToDef = FALSE;
  291.  
  292.   switch ( usMsg ) {
  293.  
  294.     case WM_INITDLG:
  295.       break;
  296.  
  297.     case WM_COMMAND:
  298.       switch ( COMMANDMSG( &usMsg )->cmd ) {
  299.         case DID_OK:
  300.           WinDismissDlg( hwndDlg, TRUE );
  301.           break;
  302.         default:
  303.           fPassToDef = TRUE;
  304.           break;
  305.       }
  306.       break;
  307.  
  308.     default:
  309.       fPassToDef = TRUE;
  310.       break;
  311.  
  312.   }
  313.  
  314.   if ( fPassToDef )
  315.     mresRtnVal = WinDefDlgProc( hwndDlg, usMsg, mp1, mp2 );
  316.  
  317.   return mresRtnVal;
  318. }
  319.  
  320. /* Exit List processing at program termination */
  321.  
  322. VOID APIENTRY ProgramTermination( )
  323.   {
  324.  
  325.   if( hmodule != (HMODULE)NULL )       /* Unfortunately does not free the  */
  326.     DosFreeModule( hmodule );          /* DLL because System Input Queue   */
  327.                                        /* hooks cannot be freed.           */
  328.  
  329.    DosCloseSem( hsemJump );            /* Close the Jump indicator sem  */
  330.                                        /* jump.exe closes the die sem   */
  331.  
  332.   DosExitList( EXLST_EXIT,             /* Required to end ExitList process.*/
  333.                (PFNEXITLIST)ProgramTermination );
  334.   }
  335.