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
Line 
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"
23#include "dll/gotchdll.h"
24#include "settings.h"
25#include "string.h"
26
27#include "io.h"
28#define INCL_EXCEPTQ_CLASS
29#define INCL_LOADEXCEPTQ
30#include "exceptq.h"
31
32HAB hab;
33HWND hwndFrame, hwndSnapshot;
34PFNWP OldFrameWP, wpOldButton;
35PSETTINGS pset;
36BOOL g_fIdle = FALSE, g_fSetPathName = FALSE;
37HWND g_hwndMenuSSW;
38HMODULE g_hmod = NULLHANDLE;
39Helper *g_phelp = NULL;
40BOOL g_usePMps = FALSE;
41CHAR g_installerSavePath[_MAX_PATH] = {0};
42BOOL g_installer = FALSE;
43CHAR g_installerINI[_MAX_PATH] = {0};
44
45#include "bitmap.cpp"
46#include "mainwin.cpp"
47#include "snapshot.cpp"
48#include "savebmp.cpp"
49
50// ** CheckCmdlineArgs **************************************************** /*FOLD00*/
51
52BOOL CheckCmdlineArgs (int argc, char *argv[])
53{
54 BOOL fAuto = FALSE;
55
56 for (USHORT i = 1; i < argc; i++)
57 {
58#ifndef _QUIET_
59 // batch mode
60 if ((stricmp (argv[i], "-a") == 0))
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]);
82 g_fSetPathName = TRUE;
83 }
84 }
85 // set to idle priority
86 else if (stricmp (argv[i], "-i") == 0)
87 {
88 g_fIdle = TRUE;
89 pset->SetFlag (SEI_IDLEPRIORITY, TRUE);
90 }
91#else
92 // use default PM print screen
93 if (stricmp (argv[i], "-p") == 0)
94 {
95 g_usePMps = TRUE;
96 }
97 if (stricmp (argv[i], "-i") == 0 || stricmp (argv[i], "-i+") == 0)
98 {
99 CHAR * memdrv = getenv("MEMDRIVE");
100
101 g_installer = TRUE;
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 }
115 }
116#endif
117 }
118
119 return fAuto;
120}
121
122// ** main **************************************************************** /*FOLD00*/
123
124int main (int argc, PSZ argv[])
125{
126 ScopedExceptqLoader sel;
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);
137 int rc;
138#ifdef _QUIET_ //Needed here for setting adjustments for installer version
139 CheckCmdlineArgs (argc, argv);
140#endif
141 // Load settings moved up here so the string table is available for the already running error
142 pset = new SETTINGS;
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 ();
149 //Reset Save style and directory for installer
150
151#ifdef _QUIET_
152
153 if ((Version() < 2))
154 {
155 if (! g_installer)
156 DisplayError(RSTR (IDS_ERROR_DLLOUTDATED),
157 RSTR (IDS_ERROR_DLLOUTDATEDMSG));
158 exit (0);
159 }
160 HMTX hmtx = NULLHANDLE;
161 // running multiple instances orphans all but the first hook unloaded
162 rc = DosOpenMutexSem("\\SEM32\\GOTCHA", &hmtx);
163 if (!rc) {
164 if (! g_installer)
165 DisplayError(RSTR (IDS_ERROR_ALREADYRUNNING),
166 RSTR (IDS_ERROR_ALREADYRUNNINGMSG));
167 exit (0);
168 }
169 rc = DosCreateMutexSem("\\SEM32\\GOTCHA", &hmtx, 0, FALSE);
170 if (rc) {
171 if (! g_installer)
172 DisplayError(RSTR (IDS_ERROR_SEMAPHOREFAILED),
173 RSTR (IDS_ERROR_SEMAPHOREFAILEDMSG));
174 exit (0);
175 }
176#endif
177
178 // register our window classes
179 WinRegisterClass (hab, "thth.wc.gotcha.main", WindowProcedure, 0L,
180 sizeof (ULONG)*2L);
181#ifndef _QUIET_
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);
189 pset->SetFileSaveStyle(pset->saveStyle);
190 pset->SetNumSaveDir (pset->pNumSaveDir);
191 delete pset;
192 WinDestroyMsgQueue (hmq);
193 WinTerminate (hab);
194 exit (0);
195 }
196#endif
197 SetIdlePriority(pset->QueryFlag(SEI_IDLEPRIORITY));
198
199 // create the windows
200 hwndFrame = CreateMainWindow ();
201#ifndef _QUIET_
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);
210#endif
211
212#ifdef _QUIET_
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 ();
221#endif
222#ifndef _QUIET_
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);
233
234 WinSetWindowPos (hwndFrame, NULLHANDLE, 0,0, 0,0, SWP_SHOW);
235 WinSetWindowPos (WinWindowFromID (hwndFrame, FID_CLIENT), NULLHANDLE,
236 0,0, 0,0, SWP_SHOW);
237#endif
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
245
246 // Set priorty to unless user set regular since running -q sets it to idle in the ini
247 if (g_fIdle) {
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 }
256#ifndef _QUIET_
257 // save size, etc. of snapshot window
258 WinQueryWindowPos (hwndSnapshot, &swp);
259 pset->SetWindowData (&swp);
260
261 // save size, etc. of main window
262 WinQueryWindowPos (hwndFrame, &swp);
263 pset->SetWindowData (&swp, FALSE);
264#endif
265 // goodbye windows!
266#ifndef _QUIET_
267 WinDestroyWindow (hwndSnapshot);
268#endif
269 WinDestroyWindow (hwndFrame);
270
271#ifdef _QUIET_
272 // Reset to user PM print screen choice
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 }
283 StopInputHook ();
284 DosCloseMutexSem(hmtx);
285#endif
286
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
316#if 0
317// saymsg2 was adapted from code in FM/2
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));
327
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
338 memset(mb2dBut, 0, sizeof(MB2D) * 2);
339 strcpy(mb2dBut[0].achText,RSTR(IDS_OK));
340 strcpy(mb2dBut[1].achText,RSTR(IDS_CANCEL));
341 //strcpy(mb2dBut[2].achText,RSTR(IDS_SETTINGS));
342 mb2dBut[0].idButton = 1;
343 mb2dBut[1].idButton = 2;
344 //mb2dBut[2].idButton = 3;
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;
352 pmbInfo->cButtons = 2;
353 pmbInfo->flStyle = MB_MOVEABLE | MB_ICONQUESTION ;
354 pmbInfo->hwndNotify = NULLHANDLE;
355 for (i = 0; i < 2; i++) {
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}
367#endif
368// ** AddSysMenuItem ****************************************************** /*FOLD00*/
369
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{
399#ifndef _QUEIT_
400 if (f)
401 DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0);
402 else
403 DosSetPriority(PRTYS_PROCESS, PRTYC_REGULAR, 0, 0);
404#else
405 DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
406#endif
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.