source: trunk/gotcha.cpp@ 59

Last change on this file since 59 was 59, checked in by Gregg Young, 6 years ago

Fix #ifdef for the command line switches

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