source: trunk/gotcha.cpp

Last change on this file was 115, checked in by Gregg Young, 4 years ago

Changes to remove obsolete help files; select language based on system language; remove language selection from Gotcha! Quiet; Combine program and help language selection in Gotcha. Changes to get Gotcha! Quiet working in ArcaOS installer.

  • Property svn:eol-style set to native
File size: 12.9 KB
RevLine 
[2]1/***
2 Main source of the Gotcha! screencapture program.
3 Copyright (C) 1998-2002 Thorsten Thielen <thth@c2226.de>
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"
[10]23#include "dll/gotchdll.h"
[2]24#include "settings.h"
25#include "string.h"
26
27#include "io.h"
[13]28#define INCL_EXCEPTQ_CLASS
29#define INCL_LOADEXCEPTQ
30#include "exceptq.h"
[2]31
32HAB hab;
33HWND hwndFrame, hwndSnapshot;
34PFNWP OldFrameWP, wpOldButton;
35PSETTINGS pset;
[38]36BOOL g_fIdle = FALSE, g_fSetPathName = FALSE;
[2]37HWND g_hwndMenuSSW;
38HMODULE g_hmod = NULLHANDLE;
39Helper *g_phelp = NULL;
[57]40BOOL g_usePMps = FALSE;
[115]41CHAR g_installerSavePath[_MAX_PATH] = {0};
42BOOL g_installer = FALSE;
43CHAR g_installerINI[_MAX_PATH] = {0};
[2]44
45#include "bitmap.cpp"
46#include "mainwin.cpp"
47#include "snapshot.cpp"
48#include "savebmp.cpp"
49
[27]50// ** CheckCmdlineArgs **************************************************** /*FOLD00*/
[2]51
52BOOL CheckCmdlineArgs (int argc, char *argv[])
53{
54 BOOL fAuto = FALSE;
55
56 for (USHORT i = 1; i < argc; i++)
57 {
[59]58#ifndef _QUIET_
[38]59 // batch mode
60 if ((stricmp (argv[i], "-a") == 0))
[2]61 {
62 if (pset->QuerySaveStyle () != FSS_FORCEFILE)
63 pset->SetFileSaveStyle (FSS_NUMFILES);
64 fAuto = TRUE;
65
66 if (i < argc-1)
67 if (argv[i+1][0] != '-')
68 {
69 i++;
70 pset->SetFileSaveStyle (FSS_NUMFILES);
71 pset->SetNumSaveDir (argv[i]);
72 }
73 }
74 // force saving to the given file name
75 else if ((stricmp (argv[i], "-f") == 0) && (i < argc-1))
76 {
77 if (argv[i+1][0] != '-')
78 {
79 i++;
80 pset->SetFileSaveStyle (FSS_FORCEFILE);
81 pset->SetForceSaveFile (argv[i]);
[27]82 g_fSetPathName = TRUE;
[2]83 }
84 }
85 // set to idle priority
86 else if (stricmp (argv[i], "-i") == 0)
87 {
[27]88 g_fIdle = TRUE;
[2]89 pset->SetFlag (SEI_IDLEPRIORITY, TRUE);
90 }
[59]91#else
[57]92 // use default PM print screen
[65]93 if (stricmp (argv[i], "-p") == 0)
[57]94 {
95 g_usePMps = TRUE;
96 }
[115]97 if (stricmp (argv[i], "-i") == 0 || stricmp (argv[i], "-i+") == 0)
[94]98 {
[115]99 CHAR * memdrv = getenv("MEMDRIVE");
100
[94]101 g_installer = TRUE;
[115]102
103 if (memdrv) {
104 strcpy(g_installerSavePath, memdrv);
105 strcat(g_installerSavePath, ":\\");
106 }
107 else
108 strcpy(g_installerSavePath, "Z:\\");
109 if (stricmp (argv[i], "-i+") == 0)
110 strcpy(g_installerINI, "gotchaq.ini");
111 else {
112 strcpy(g_installerINI, g_installerSavePath);
113 strcat(g_installerINI, "gotchaq.ini");
114 }
[94]115 }
[57]116#endif
[2]117 }
118
119 return fAuto;
120}
121
122// ** main **************************************************************** /*FOLD00*/
123
124int main (int argc, PSZ argv[])
125{
[13]126 ScopedExceptqLoader sel;
[2]127#ifdef _DOLOGDEBUG_
128 LogDebug( "Gotcha! start" );
129#endif
130#ifdef _DOLOGMEM_
131 LogMem("main", TRUE);
132#endif
133
134 // init system and msg queue
135 hab = WinInitialize (0);
136 HMQ hmq = WinCreateMsgQueue (hab, 0);
[94]137 int rc;
[115]138#ifdef _QUIET_ //Needed here for setting adjustments for installer version
139 CheckCmdlineArgs (argc, argv);
140#endif
[91]141 // Load settings moved up here so the string table is available for the already running error
[89]142 pset = new SETTINGS;
[94]143
144 pset->idleSetInIni = pset->QueryFlag(SEI_IDLEPRIORITY);
145 pset->saveStyle = pset->QuerySaveStyle ();
146 pset->pNumSaveDir = pset->QueryNumSaveDir ();
147 pset->pForceSaveFile = pset->QueryForceSaveFile();
148 pset->bSerialCapture = pset->SerialCapture ();
[115]149 //Reset Save style and directory for installer
150
[30]151#ifdef _QUIET_
[115]152
[50]153 if ((Version() < 2))
[2]154 {
[94]155 if (! g_installer)
156 DisplayError(RSTR (IDS_ERROR_DLLOUTDATED),
157 RSTR (IDS_ERROR_DLLOUTDATEDMSG));
[2]158 exit (0);
159 }
[50]160 HMTX hmtx = NULLHANDLE;
[89]161 // running multiple instances orphans all but the first hook unloaded
[50]162 rc = DosOpenMutexSem("\\SEM32\\GOTCHA", &hmtx);
163 if (!rc) {
[94]164 if (! g_installer)
165 DisplayError(RSTR (IDS_ERROR_ALREADYRUNNING),
166 RSTR (IDS_ERROR_ALREADYRUNNINGMSG));
[50]167 exit (0);
[27]168 }
[50]169 rc = DosCreateMutexSem("\\SEM32\\GOTCHA", &hmtx, 0, FALSE);
170 if (rc) {
[94]171 if (! g_installer)
172 DisplayError(RSTR (IDS_ERROR_SEMAPHOREFAILED),
173 RSTR (IDS_ERROR_SEMAPHOREFAILEDMSG));
[50]174 exit (0);
175 }
[30]176#endif
[27]177
[2]178 // register our window classes
179 WinRegisterClass (hab, "thth.wc.gotcha.main", WindowProcedure, 0L,
180 sizeof (ULONG)*2L);
[89]181#ifndef _QUIET_
[2]182 WinRegisterClass (hab, "thth.wc.gotcha.snapshot", wpSnapshot,
183 CS_SIZEREDRAW, sizeof (ULONG)*2L);
184
185 // check cmd line args and if "-a" found take screenshot and exit
186 if (CheckCmdlineArgs (argc, argv))
187 {
188 CaptureWindow (HWND_DESKTOP, HWND_DESKTOP, NULL, TRUE);
[27]189 pset->SetFileSaveStyle(pset->saveStyle);
[38]190 pset->SetNumSaveDir (pset->pNumSaveDir);
[2]191 delete pset;
192 WinDestroyMsgQueue (hmq);
193 WinTerminate (hab);
194 exit (0);
195 }
[92]196#endif
[2]197 SetIdlePriority(pset->QueryFlag(SEI_IDLEPRIORITY));
198
199 // create the windows
200 hwndFrame = CreateMainWindow ();
[89]201#ifndef _QUIET_
[2]202 hwndSnapshot = CreateSnapshotWindow ();
203 SWP swp;
204 USHORT us[7];
205
206 // position main window
207 pset->QueryWindowData (&swp, us, FALSE);
208 WinSetWindowPos (hwndFrame, HWND_DESKTOP, swp.x,swp.y, 0,0,
209 SWP_SHOW | SWP_MOVE);
[92]210#endif
[38]211
[30]212#ifdef _QUIET_
[57]213 bool fPrtScr = FALSE;
214 if (!g_usePMps) {
215 // always turn it off while running
216 // WinSet does not effect the ini setting
217 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
218 }
219 InitDLL (hab, hwndFrame, g_usePMps);
220 StartInputHook ();
[2]221#endif
[89]222#ifndef _QUIET_
[2]223 // position snapshot window
224 pset->QueryWindowData (&swp, us);
225
226 // size, activate & show window
227 WinSetWindowPos (hwndSnapshot, HWND_DESKTOP, swp.x,swp.y, swp.cx,swp.cy,
228 SWP_SHOW | SWP_SIZE | SWP_MOVE);
229 if (! pset->SnapshotWindow ())
230 WinShowWindow (hwndSnapshot, FALSE);
231 else
232 WinShowWindow (hwndSnapshot, TRUE);
[92]233
[2]234 WinSetWindowPos (hwndFrame, NULLHANDLE, 0,0, 0,0, SWP_SHOW);
235 WinSetWindowPos (WinWindowFromID (hwndFrame, FID_CLIENT), NULLHANDLE,
236 0,0, 0,0, SWP_SHOW);
[92]237#endif
[2]238 g_phelp = new Helper(hwndFrame);
239
240 // do the main msg loop
241 QMSG qmsg;
242 while (WinGetMsg (hab, &qmsg, 0L, 0, 0))
243 WinDispatchMsg (hab, &qmsg);
244
[27]245
246 // Set priorty to unless user set regular since running -q sets it to idle in the ini
[38]247 if (g_fIdle) {
[27]248 pset->SetFlag (SEI_IDLEPRIORITY, pset->idleSetInIni);
249 }
250 // Don't change the ini stored paths if the paths were set from the command line
251 if (g_fSetPathName) {
252 pset->SetFileSaveStyle(pset->saveStyle);
253 pset->SetForceSaveFile (pset->pForceSaveFile);
254 pset->SetNumSaveDir (pset->pNumSaveDir);
255 }
[89]256#ifndef _QUIET_
[2]257 // save size, etc. of snapshot window
258 WinQueryWindowPos (hwndSnapshot, &swp);
259 pset->SetWindowData (&swp);
[92]260
[2]261 // save size, etc. of main window
262 WinQueryWindowPos (hwndFrame, &swp);
263 pset->SetWindowData (&swp, FALSE);
[92]264#endif
[2]265 // goodbye windows!
[89]266#ifndef _QUIET_
[2]267 WinDestroyWindow (hwndSnapshot);
[89]268#endif
[2]269 WinDestroyWindow (hwndFrame);
270
[30]271#ifdef _QUIET_
[52]272 // Reset to user PM print screen choice
[57]273 if (!g_usePMps) {
274 ULONG ulDataSize = 0;
275 rc = PrfQueryProfileSize(HINI_USERPROFILE, "PM_ControlPanel",
276 "PrintScreen", &ulDataSize );
277 rc = PrfQueryProfileData(HINI_USERPROFILE, "PM_ControlPanel",
278 "PrintScreen", &fPrtScr, &ulDataSize);
279 if (!rc) // Print screen is on by default (no ini entry)
280 fPrtScr = TRUE;
281 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
282 }
[38]283 StopInputHook ();
[50]284 DosCloseMutexSem(hmtx);
[2]285#endif
[50]286
[2]287 delete g_phelp;
288 delete pset;
289
290 WinDestroyMsgQueue (hmq);
291 WinTerminate (hab);
292
293#ifdef _DOLOGMEM_
294 LogMem("main", FALSE);
295#endif
296#ifdef _DOLOGDEBUG_
297 LogDebug( "Gotcha! end" );
298#endif
299}
300
301// ** DisplayError ******************************************************** /*FOLD00*/
302
303VOID DisplayError (PSZ pszTitle, PSZ psz, ...)
304{
305 CHAR dstring[401];
306 va_list valst;
307
308 va_start (valst, psz);
309 vsnprintf (dstring, 401, psz, valst);
310 va_end (valst);
311
312 WinMessageBox (HWND_DESKTOP, WinQueryActiveWindow (HWND_DESKTOP), dstring,
313 pszTitle, 0, MB_OK | MB_ERROR | MB_APPLMODAL | MB_MOVEABLE);
314}
315
[115]316#if 0
[38]317// saymsg2 was adapted from code in FM/2
[27]318APIRET saymsg2(int DefaultButton, HWND hwnd, PCSZ pszTitle, PCSZ pszFmt, ...)
319{
320 ULONG i;
321 APIRET rc;
322 CHAR szMsg[4096];
323 va_list va;
324 MB2INFO *pmbInfo;
325 MB2D mb2dBut[3];
326 ULONG ulInfoSize = (sizeof(MB2INFO) + (sizeof(MB2D) * 2));
[2]327
[27]328 va_start(va, pszFmt);
329 szMsg[sizeof(szMsg) - 1] = 0;
330 vsprintf(szMsg, pszFmt, va);
331 va_end(va);
332
333 if (szMsg[sizeof(szMsg) - 1]) {
334 fprintf(stderr, "Buffer overflow in saymsg2 - need %u bytes\n", strlen(szMsg) + 1);
335 fflush(stderr);
336 }
337
[59]338 memset(mb2dBut, 0, sizeof(MB2D) * 2);
[55]339 strcpy(mb2dBut[0].achText,RSTR(IDS_OK));
340 strcpy(mb2dBut[1].achText,RSTR(IDS_CANCEL));
[59]341 //strcpy(mb2dBut[2].achText,RSTR(IDS_SETTINGS));
[27]342 mb2dBut[0].idButton = 1;
343 mb2dBut[1].idButton = 2;
[59]344 //mb2dBut[2].idButton = 3;
[27]345 if (DefaultButton)
346 mb2dBut[DefaultButton - 1].flStyle = BS_DEFAULT;
347 pmbInfo = (MB2INFO *) malloc(ulInfoSize);
348 memset(pmbInfo, 0, ulInfoSize);
349 if (pmbInfo) {
350 pmbInfo->cb = ulInfoSize;
351 pmbInfo->hIcon = 0;
[59]352 pmbInfo->cButtons = 2;
[27]353 pmbInfo->flStyle = MB_MOVEABLE | MB_ICONQUESTION ;
354 pmbInfo->hwndNotify = NULLHANDLE;
[59]355 for (i = 0; i < 2; i++) {
[27]356 memcpy( pmbInfo->mb2d+i , mb2dBut+i , sizeof(MB2D));
357 }
358 rc = WinMessageBox2(HWND_DESKTOP, hwnd,
359 szMsg, pszTitle, SM2_DIALOG,
360 pmbInfo);
361 WinSetFocus(HWND_DESKTOP, SM2_DIALOG);
362 free(pmbInfo);
363 return rc;
364 }
365 return MBID_ERROR;
366}
[115]367#endif
[27]368// ** AddSysMenuItem ****************************************************** /*FOLD00*/
369
[2]370VOID AddSysMenuItem (HWND hwndFrame, MENUITEM *Item, PSZ Text)
371{
372 HWND hwndSysMenu = WinWindowFromID (hwndFrame, FID_SYSMENU);
373 USHORT idSysMenu = SHORT1FROMMR (WinSendMsg (hwndSysMenu,
374 MM_ITEMIDFROMPOSITION, NULL,
375 NULL ));
376 MENUITEM miSysMenu;
377 WinSendMsg (hwndSysMenu, MM_QUERYITEM,
378 MPFROM2SHORT (idSysMenu,FALSE), MPFROMP(&miSysMenu));
379
380 HWND hwndSysSubMenu = miSysMenu.hwndSubMenu;
381
382 WinSendMsg (hwndSysSubMenu, MM_INSERTITEM, MPFROMP(Item), MPFROMP(Text));
383}
384
385// ** DoCountdown ********************************************************* /*fold00*/
386
387VOID DoCountdown (ULONG ul)
388{
389 if (ul > 10)
390 DosBeep (4000L-3000L, 20);
391 else
392 DosBeep (4000L-ul*300L, 20);
393}
394
395// ** SetIdlePriority **************************************************** /*FOLD00*/
396
397VOID SetIdlePriority (BOOL f)
398{
[52]399#ifndef _QUEIT_
[2]400 if (f)
401 DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0);
402 else
403 DosSetPriority(PRTYS_PROCESS, PRTYC_REGULAR, 0, 0);
[27]404#else
405 DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
406#endif
[2]407}
408
409// ***********************************************************************
410
411#ifdef _DOLOGMEM_
412VOID LogMem (PSZ psz, BOOL f)
413{
414 FILE *pf = fopen("gotcha.mem","ab");
415 static ULONG TotalPhysicalMemory, ul = 0;
416 ULONG i;
417 if (!f)
418 ul--;
419 DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &TotalPhysicalMemory,
420 sizeof(TotalPhysicalMemory));
421 for (i = 0; i < ul; i++)
422 fputs(" ", pf);
423 fprintf(pf,"%10ld (%s)\n", TotalPhysicalMemory, psz);
424 if (f)
425 ul++;
426 fclose(pf);
427}
428#endif
429
430#ifdef _DOLOGDEBUG_
431VOID LogDebug( PSZ psz, ... )
432{
433 CHAR dstring[401];
434 va_list valst;
435
436 va_start (valst, psz);
437 vsnprintf (dstring, 401, psz, valst);
438 va_end (valst);
439
440 FILE *pf = fopen( "gotcha.log", "ab" );
441 fprintf( pf, "%s\n", dstring );
442 fclose( pf );
443}
444#endif
445
446// ***********************************************************************
Note: See TracBrowser for help on using the repository browser.