source: trunk/gotcha.cpp@ 92

Last change on this file since 92 was 92, checked in by Gregg Young, 5 years ago

Clean up Gotcha! Quiet's ini file by removing the snapshot entries. #ifdef out some more code from Gotcha! Quiet which is unneeded (setting the size and position of an object window is a useless waste of code)

  • Property svn:eol-style set to native
File size: 12.0 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 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 // Load settings moved up here so the string table is available for the already running error
117 pset = new SETTINGS;
118 //DisplayError("DEBUG", "%d", Version());
119#ifdef _QUIET_
120 if ((Version() < 2))
121 {
122 DisplayError(RSTR (IDS_ERROR_DLLOUTDATED),
123 RSTR (IDS_ERROR_DLLOUTDATEDMSG));
124 exit (0);
125 }
126 int rc;
127 HMTX hmtx = NULLHANDLE;
128 // running multiple instances orphans all but the first hook unloaded
129 rc = DosOpenMutexSem("\\SEM32\\GOTCHA", &hmtx);
130 if (!rc) {
131 DisplayError(RSTR (IDS_ERROR_ALREADYRUNNING),
132 RSTR (IDS_ERROR_ALREADYRUNNINGMSG));
133 exit (0);
134 }
135 rc = DosCreateMutexSem("\\SEM32\\GOTCHA", &hmtx, 0, FALSE);
136 if (rc) {
137 DisplayError(RSTR (IDS_ERROR_SEMAPHOREFAILED),
138 RSTR (IDS_ERROR_SEMAPHOREFAILEDMSG));
139 exit (0);
140 }
141#endif
142
143 // register our window classes
144 WinRegisterClass (hab, "thth.wc.gotcha.main", WindowProcedure, 0L,
145 sizeof (ULONG)*2L);
146#ifndef _QUIET_
147 WinRegisterClass (hab, "thth.wc.gotcha.snapshot", wpSnapshot,
148 CS_SIZEREDRAW, sizeof (ULONG)*2L);
149#endif
150 // load the settings
151 //pset = new SETTINGS;
152
153 pset->idleSetInIni = pset->QueryFlag(SEI_IDLEPRIORITY);
154 pset->saveStyle = pset->QuerySaveStyle ();
155 pset->pNumSaveDir = pset->QueryNumSaveDir ();
156 pset->pForceSaveFile = pset->QueryForceSaveFile();
157 pset->bSerialCapture = pset->SerialCapture ();
158#ifndef _QUIET_
159 // check cmd line args and if "-a" found take screenshot and exit
160 if (CheckCmdlineArgs (argc, argv))
161 {
162 CaptureWindow (HWND_DESKTOP, HWND_DESKTOP, NULL, TRUE);
163 pset->SetFileSaveStyle(pset->saveStyle);
164 pset->SetNumSaveDir (pset->pNumSaveDir);
165 delete pset;
166 WinDestroyMsgQueue (hmq);
167 WinTerminate (hab);
168 exit (0);
169 }
170#endif
171 SetIdlePriority(pset->QueryFlag(SEI_IDLEPRIORITY));
172
173 // create the windows
174 hwndFrame = CreateMainWindow ();
175#ifndef _QUIET_
176 hwndSnapshot = CreateSnapshotWindow ();
177 SWP swp;
178 USHORT us[7];
179
180 // position main window
181 pset->QueryWindowData (&swp, us, FALSE);
182 WinSetWindowPos (hwndFrame, HWND_DESKTOP, swp.x,swp.y, 0,0,
183 SWP_SHOW | SWP_MOVE);
184#endif
185
186#ifdef _QUIET_
187 bool fPrtScr = FALSE;
188 if (!g_usePMps) {
189 // always turn it off while running
190 // WinSet does not effect the ini setting
191 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
192 }
193 InitDLL (hab, hwndFrame, g_usePMps);
194 StartInputHook ();
195#endif
196#ifndef _QUIET_
197 // position snapshot window
198 pset->QueryWindowData (&swp, us);
199
200 // size, activate & show window
201 WinSetWindowPos (hwndSnapshot, HWND_DESKTOP, swp.x,swp.y, swp.cx,swp.cy,
202 SWP_SHOW | SWP_SIZE | SWP_MOVE);
203 if (! pset->SnapshotWindow ())
204 WinShowWindow (hwndSnapshot, FALSE);
205 else
206 WinShowWindow (hwndSnapshot, TRUE);
207
208 WinSetWindowPos (hwndFrame, NULLHANDLE, 0,0, 0,0, SWP_SHOW);
209 WinSetWindowPos (WinWindowFromID (hwndFrame, FID_CLIENT), NULLHANDLE,
210 0,0, 0,0, SWP_SHOW);
211#endif
212
213 g_phelp = new Helper(hwndFrame);
214
215
216 // do the main msg loop
217 QMSG qmsg;
218 while (WinGetMsg (hab, &qmsg, 0L, 0, 0))
219 WinDispatchMsg (hab, &qmsg);
220
221
222 // Set priorty to unless user set regular since running -q sets it to idle in the ini
223 if (g_fIdle) {
224 pset->SetFlag (SEI_IDLEPRIORITY, pset->idleSetInIni);
225 }
226 // Don't change the ini stored paths if the paths were set from the command line
227 if (g_fSetPathName) {
228 pset->SetFileSaveStyle(pset->saveStyle);
229 pset->SetForceSaveFile (pset->pForceSaveFile);
230 pset->SetNumSaveDir (pset->pNumSaveDir);
231 }
232#ifndef _QUIET_
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#endif
241 // goodbye windows!
242#ifndef _QUIET_
243 WinDestroyWindow (hwndSnapshot);
244#endif
245 WinDestroyWindow (hwndFrame);
246
247#ifdef _QUIET_
248 // Reset to user PM print screen choice
249 if (!g_usePMps) {
250 ULONG ulDataSize = 0;
251 rc = PrfQueryProfileSize(HINI_USERPROFILE, "PM_ControlPanel",
252 "PrintScreen", &ulDataSize );
253 rc = PrfQueryProfileData(HINI_USERPROFILE, "PM_ControlPanel",
254 "PrintScreen", &fPrtScr, &ulDataSize);
255 if (!rc) // Print screen is on by default (no ini entry)
256 fPrtScr = TRUE;
257 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
258 }
259 StopInputHook ();
260 DosCloseMutexSem(hmtx);
261#endif
262
263 delete g_phelp;
264 delete pset;
265
266 WinDestroyMsgQueue (hmq);
267 WinTerminate (hab);
268
269#ifdef _DOLOGMEM_
270 LogMem("main", FALSE);
271#endif
272#ifdef _DOLOGDEBUG_
273 LogDebug( "Gotcha! end" );
274#endif
275}
276
277// ** DisplayError ******************************************************** /*FOLD00*/
278
279VOID DisplayError (PSZ pszTitle, PSZ psz, ...)
280{
281 CHAR dstring[401];
282 va_list valst;
283
284 va_start (valst, psz);
285 vsnprintf (dstring, 401, psz, valst);
286 va_end (valst);
287
288 WinMessageBox (HWND_DESKTOP, WinQueryActiveWindow (HWND_DESKTOP), dstring,
289 pszTitle, 0, MB_OK | MB_ERROR | MB_APPLMODAL | MB_MOVEABLE);
290}
291
292// saymsg2 was adapted from code in FM/2
293APIRET saymsg2(int DefaultButton, HWND hwnd, PCSZ pszTitle, PCSZ pszFmt, ...)
294{
295 ULONG i;
296 APIRET rc;
297 CHAR szMsg[4096];
298 va_list va;
299 MB2INFO *pmbInfo;
300 MB2D mb2dBut[3];
301 ULONG ulInfoSize = (sizeof(MB2INFO) + (sizeof(MB2D) * 2));
302
303 va_start(va, pszFmt);
304 szMsg[sizeof(szMsg) - 1] = 0;
305 vsprintf(szMsg, pszFmt, va);
306 va_end(va);
307
308 if (szMsg[sizeof(szMsg) - 1]) {
309 fprintf(stderr, "Buffer overflow in saymsg2 - need %u bytes\n", strlen(szMsg) + 1);
310 fflush(stderr);
311 }
312
313 memset(mb2dBut, 0, sizeof(MB2D) * 2);
314 strcpy(mb2dBut[0].achText,RSTR(IDS_OK));
315 strcpy(mb2dBut[1].achText,RSTR(IDS_CANCEL));
316 //strcpy(mb2dBut[2].achText,RSTR(IDS_SETTINGS));
317 mb2dBut[0].idButton = 1;
318 mb2dBut[1].idButton = 2;
319 //mb2dBut[2].idButton = 3;
320 if (DefaultButton)
321 mb2dBut[DefaultButton - 1].flStyle = BS_DEFAULT;
322 pmbInfo = (MB2INFO *) malloc(ulInfoSize);
323 memset(pmbInfo, 0, ulInfoSize);
324 if (pmbInfo) {
325 pmbInfo->cb = ulInfoSize;
326 pmbInfo->hIcon = 0;
327 pmbInfo->cButtons = 2;
328 pmbInfo->flStyle = MB_MOVEABLE | MB_ICONQUESTION ;
329 pmbInfo->hwndNotify = NULLHANDLE;
330 for (i = 0; i < 2; i++) {
331 memcpy( pmbInfo->mb2d+i , mb2dBut+i , sizeof(MB2D));
332 }
333 rc = WinMessageBox2(HWND_DESKTOP, hwnd,
334 szMsg, pszTitle, SM2_DIALOG,
335 pmbInfo);
336 WinSetFocus(HWND_DESKTOP, SM2_DIALOG);
337 free(pmbInfo);
338 return rc;
339 }
340 return MBID_ERROR;
341}
342// ** AddSysMenuItem ****************************************************** /*FOLD00*/
343
344VOID AddSysMenuItem (HWND hwndFrame, MENUITEM *Item, PSZ Text)
345{
346 HWND hwndSysMenu = WinWindowFromID (hwndFrame, FID_SYSMENU);
347 USHORT idSysMenu = SHORT1FROMMR (WinSendMsg (hwndSysMenu,
348 MM_ITEMIDFROMPOSITION, NULL,
349 NULL ));
350 MENUITEM miSysMenu;
351 WinSendMsg (hwndSysMenu, MM_QUERYITEM,
352 MPFROM2SHORT (idSysMenu,FALSE), MPFROMP(&miSysMenu));
353
354 HWND hwndSysSubMenu = miSysMenu.hwndSubMenu;
355
356 WinSendMsg (hwndSysSubMenu, MM_INSERTITEM, MPFROMP(Item), MPFROMP(Text));
357}
358
359// ** DoCountdown ********************************************************* /*fold00*/
360
361VOID DoCountdown (ULONG ul)
362{
363 if (ul > 10)
364 DosBeep (4000L-3000L, 20);
365 else
366 DosBeep (4000L-ul*300L, 20);
367}
368
369// ** SetIdlePriority **************************************************** /*FOLD00*/
370
371VOID SetIdlePriority (BOOL f)
372{
373#ifndef _QUEIT_
374 if (f)
375 DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0);
376 else
377 DosSetPriority(PRTYS_PROCESS, PRTYC_REGULAR, 0, 0);
378#else
379 DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
380#endif
381}
382
383// ***********************************************************************
384
385#ifdef _DOLOGMEM_
386VOID LogMem (PSZ psz, BOOL f)
387{
388 FILE *pf = fopen("gotcha.mem","ab");
389 static ULONG TotalPhysicalMemory, ul = 0;
390 ULONG i;
391 if (!f)
392 ul--;
393 DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &TotalPhysicalMemory,
394 sizeof(TotalPhysicalMemory));
395 for (i = 0; i < ul; i++)
396 fputs(" ", pf);
397 fprintf(pf,"%10ld (%s)\n", TotalPhysicalMemory, psz);
398 if (f)
399 ul++;
400 fclose(pf);
401}
402#endif
403
404#ifdef _DOLOGDEBUG_
405VOID LogDebug( PSZ psz, ... )
406{
407 CHAR dstring[401];
408 va_list valst;
409
410 va_start (valst, psz);
411 vsnprintf (dstring, 401, psz, valst);
412 va_end (valst);
413
414 FILE *pf = fopen( "gotcha.log", "ab" );
415 fprintf( pf, "%s\n", dstring );
416 fclose( pf );
417}
418#endif
419
420// ***********************************************************************
Note: See TracBrowser for help on using the repository browser.