home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / gotch175.zip / src / gotcha.cpp < prev    next >
C/C++ Source or Header  |  2002-12-28  |  9KB  |  314 lines

  1. /***
  2.  Main source of the Gotcha! screencapture program.
  3.  Copyright (C) 1998-2002 Thorsten Thielen <thth@gmx.net>
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  ***/
  19.  
  20. #define INCL_WINHOOKS
  21.  
  22. #include "gotcha.h"
  23. #include "settings.h"
  24. #include "string.h"
  25.  
  26. #include "io.h"
  27.  
  28. HAB         hab;
  29. HWND        hwndFrame, hwndSnapshot;
  30. PFNWP       OldFrameWP, wpOldButton;
  31. PSETTINGS   pset;
  32. BOOL        g_fQuiet = FALSE;
  33. HWND        g_hwndMenuSSW;
  34. HMODULE     g_hmod = NULLHANDLE;
  35. Helper      *g_phelp = NULL;
  36.  
  37. #include "bitmap.cpp"
  38. #include "mainwin.cpp"
  39. #include "snapshot.cpp"
  40. #include "savebmp.cpp"
  41.  
  42. // ** CheckCmdlineArgs **************************************************** /*fold00*/
  43.  
  44. BOOL CheckCmdlineArgs (int argc, char *argv[])
  45. {
  46.     BOOL   fAuto = FALSE;
  47.  
  48.     for (USHORT i = 1; i < argc; i++)
  49.     {
  50.         // quiet or batch mode
  51.         if ((stricmp (argv[i], "-q") == 0) || (stricmp (argv[i], "-a") == 0))
  52.         {
  53.             if (pset->QuerySaveStyle () != FSS_FORCEFILE)
  54.                 pset->SetFileSaveStyle (FSS_NUMFILES);
  55.  
  56.             if (stricmp (argv[i], "-q") == 0)
  57.             {
  58.                 pset->SerialCapture (FALSE);
  59.                 g_fQuiet = TRUE;
  60.             }
  61.             else
  62.                 fAuto = TRUE;
  63.  
  64.             if (i < argc-1)
  65.                 if (argv[i+1][0] != '-')
  66.                 {
  67.                     i++;
  68.                     pset->SetFileSaveStyle (FSS_NUMFILES);
  69.                     pset->SetNumSaveDir (argv[i]);
  70.                 }
  71.         }
  72.         // force saving to the given file name
  73.         else if ((stricmp (argv[i], "-f") == 0) && (i < argc-1))
  74.         {
  75.             if (argv[i+1][0] != '-')
  76.             {
  77.                 i++;
  78.                 pset->SetFileSaveStyle (FSS_FORCEFILE);
  79.                 pset->SetForceSaveFile (argv[i]);
  80.             }
  81.         }
  82.         // set to idle priority
  83.         else if (stricmp (argv[i], "-i") == 0)
  84.         {
  85.             pset->SetFlag (SEI_IDLEPRIORITY, TRUE);
  86.         }
  87.     }
  88.  
  89.     return fAuto;
  90. }
  91.  
  92. // ** main **************************************************************** /*FOLD00*/
  93.  
  94. int main (int argc, PSZ argv[])
  95. {
  96. #ifdef _DOLOGDEBUG_
  97.     LogDebug( "Gotcha! start" );
  98. #endif
  99. #ifdef _DOLOGMEM_
  100.     LogMem("main", TRUE);
  101. #endif
  102.  
  103.     // init system and msg queue
  104.     hab = WinInitialize (0);
  105.     HMQ hmq = WinCreateMsgQueue (hab, 0);
  106.  
  107.     //DisplayError("DEBUG", "%d", Version());
  108. #ifdef _USEDLL_
  109.     if (g_fQuiet && (Version() < 1))
  110.     {
  111.         DisplayError("GOTCHA.DLL TO OLD",
  112.                      "The file gotcha.dll is to old for this version of "
  113.                      "Gotcha! As you should have received a sufficiently "
  114.                      "new version with the program, check if an older "
  115.                      "version of gotcha.dll is still residing somewhere "
  116.                      "in your LIBPATH. And did you copy the new gotcha.dll to "
  117.                      "some directory in your LIBPATH?");
  118.         exit (0);
  119.     }
  120. #endif
  121.  
  122.     // register our window classes
  123.     WinRegisterClass (hab, "thth.wc.gotcha.main", WindowProcedure, 0L,
  124.                       sizeof (ULONG)*2L);
  125.     WinRegisterClass (hab, "thth.wc.gotcha.snapshot", wpSnapshot,
  126.                       CS_SIZEREDRAW, sizeof (ULONG)*2L);
  127.  
  128.     // load the settings
  129.     pset = new SETTINGS;
  130.  
  131.     // check cmd line args and if "-a" found take screenshot and exit
  132.     if (CheckCmdlineArgs (argc, argv))
  133.     {
  134.         CaptureWindow (HWND_DESKTOP, HWND_DESKTOP, NULL, TRUE);
  135.         delete pset;
  136.         WinDestroyMsgQueue (hmq);
  137.         WinTerminate (hab);
  138.         exit (0);
  139.     }
  140.  
  141.     SetIdlePriority(pset->QueryFlag(SEI_IDLEPRIORITY));
  142.  
  143.     // create the windows
  144.     hwndFrame = CreateMainWindow ();
  145.     hwndSnapshot = CreateSnapshotWindow ();
  146.  
  147.     SWP swp;
  148.     USHORT us[7];
  149.  
  150.     // position main window
  151.     pset->QueryWindowData (&swp, us, FALSE);
  152.     WinSetWindowPos (hwndFrame, HWND_DESKTOP, swp.x,swp.y, 0,0,
  153.                      SWP_SHOW | SWP_MOVE);
  154.  
  155.     // init the quiet mode
  156.     if (g_fQuiet)
  157.     {
  158. #ifdef _USEDLL_
  159.         InitDLL (hab, hwndFrame);
  160.         StartInputHook ();
  161. #endif
  162.  
  163.         CHAR ach[61];
  164.         WinQueryWindowText (hwndFrame, sizeof (ach), ach);
  165.         strcat (ach, RSTR (IDS_QUIETMODE));
  166.         WinSetWindowText (hwndFrame, ach);
  167.         WinSetWindowPos (hwndFrame, NULLHANDLE, 0,0, 0,0, SWP_MINIMIZE);
  168.     }
  169.  
  170.     // position snapshot window
  171.     pset->QueryWindowData (&swp, us);
  172.  
  173.     // size, activate & show window
  174.     WinSetWindowPos (hwndSnapshot, HWND_DESKTOP, swp.x,swp.y, swp.cx,swp.cy,
  175.                      SWP_SHOW | SWP_SIZE | SWP_MOVE);
  176.     if (! pset->SnapshotWindow ())
  177.         WinShowWindow (hwndSnapshot, FALSE);
  178.     else
  179.         WinShowWindow (hwndSnapshot, TRUE);
  180.  
  181.     WinSetWindowPos (hwndFrame, NULLHANDLE, 0,0, 0,0, SWP_SHOW);
  182.     WinSetWindowPos (WinWindowFromID (hwndFrame, FID_CLIENT), NULLHANDLE,
  183.                      0,0, 0,0, SWP_SHOW);
  184.  
  185.     g_phelp = new Helper(hwndFrame);
  186.  
  187.  
  188.     // do the main msg loop
  189.     QMSG   qmsg;
  190.     while (WinGetMsg (hab, &qmsg, 0L, 0, 0))
  191.         WinDispatchMsg (hab, &qmsg);
  192.  
  193.     // save size, etc. of snapshot window
  194.     WinQueryWindowPos (hwndSnapshot, &swp);
  195.     pset->SetWindowData (&swp);
  196.  
  197.     // save size, etc. of main window
  198.     WinQueryWindowPos (hwndFrame, &swp);
  199.     pset->SetWindowData (&swp, FALSE);
  200.  
  201.     // goodbye windows!
  202.     WinDestroyWindow (hwndSnapshot);
  203.     WinDestroyWindow (hwndFrame);
  204.  
  205. #ifdef _USEDLL_
  206.     if (g_fQuiet)
  207.         StopInputHook ();
  208. #endif
  209.  
  210.     delete g_phelp;
  211.     delete pset;
  212.  
  213.     WinDestroyMsgQueue (hmq);
  214.     WinTerminate (hab);
  215.  
  216. #ifdef _DOLOGMEM_
  217.     LogMem("main", FALSE);
  218. #endif
  219. #ifdef _DOLOGDEBUG_
  220.     LogDebug( "Gotcha! end" );
  221. #endif
  222. }
  223.  
  224. // ** DisplayError ******************************************************** /*FOLD00*/
  225.  
  226. VOID DisplayError (PSZ pszTitle, PSZ psz, ...)
  227. {
  228.     CHAR      dstring[401];
  229.     va_list   valst;
  230.  
  231.     va_start (valst, psz);
  232.     vsnprintf (dstring, 401, psz, valst);
  233.     va_end (valst);
  234.  
  235.     WinMessageBox (HWND_DESKTOP, WinQueryActiveWindow (HWND_DESKTOP), dstring,
  236.                    pszTitle, 0, MB_OK | MB_ERROR | MB_APPLMODAL | MB_MOVEABLE);
  237. }
  238.  
  239. // ** AddSysMenuItem ****************************************************** /*fold00*/
  240.  
  241. VOID AddSysMenuItem (HWND hwndFrame, MENUITEM *Item, PSZ Text)
  242. {
  243.     HWND   hwndSysMenu = WinWindowFromID (hwndFrame, FID_SYSMENU);
  244.     USHORT idSysMenu   = SHORT1FROMMR (WinSendMsg (hwndSysMenu,
  245.                                                    MM_ITEMIDFROMPOSITION, NULL,
  246.                                                    NULL ));
  247.     MENUITEM   miSysMenu;
  248.     WinSendMsg (hwndSysMenu, MM_QUERYITEM,
  249.                 MPFROM2SHORT (idSysMenu,FALSE), MPFROMP(&miSysMenu));
  250.  
  251.     HWND   hwndSysSubMenu = miSysMenu.hwndSubMenu;
  252.  
  253.     WinSendMsg (hwndSysSubMenu, MM_INSERTITEM, MPFROMP(Item), MPFROMP(Text));
  254. }
  255.  
  256. // ** DoCountdown ********************************************************* /*fold00*/
  257.  
  258. VOID DoCountdown (ULONG ul)
  259. {
  260.     if (ul > 10)
  261.         DosBeep (4000L-3000L, 20);
  262.     else
  263.         DosBeep (4000L-ul*300L, 20);
  264. }
  265.  
  266. // ** SetIdlePriority **************************************************** /*FOLD00*/
  267.  
  268. VOID SetIdlePriority (BOOL f)
  269. {
  270.     if (f)
  271.         DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0);
  272.     else
  273.         DosSetPriority(PRTYS_PROCESS, PRTYC_REGULAR, 0, 0);
  274. }
  275.  
  276. // ***********************************************************************
  277.  
  278. #ifdef _DOLOGMEM_
  279. VOID LogMem (PSZ psz, BOOL f)
  280. {
  281.     FILE *pf = fopen("gotcha.mem","ab");
  282.     static ULONG TotalPhysicalMemory, ul = 0;
  283.     ULONG i;
  284.     if (!f)
  285.         ul--;
  286.     DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &TotalPhysicalMemory,
  287.                     sizeof(TotalPhysicalMemory));
  288.     for (i = 0; i < ul; i++)
  289.         fputs("  ", pf);
  290.     fprintf(pf,"%10ld (%s)\n", TotalPhysicalMemory, psz);
  291.     if (f)
  292.         ul++;
  293.     fclose(pf);
  294. }
  295. #endif
  296.  
  297. #ifdef _DOLOGDEBUG_
  298. VOID LogDebug( PSZ psz, ... )
  299. {
  300.     CHAR      dstring[401];
  301.     va_list   valst;
  302.  
  303.     va_start (valst, psz);
  304.     vsnprintf (dstring, 401, psz, valst);
  305.     va_end (valst);
  306.  
  307.     FILE *pf = fopen( "gotcha.log", "ab" );
  308.     fprintf( pf, "%s\n", dstring );
  309.     fclose( pf );
  310. }
  311. #endif
  312.  
  313. // ***********************************************************************
  314.