source: trunk/gotcha.cpp@ 38

Last change on this file since 38 was 38, checked in by Gregg Young, 8 years ago

Remove -q command line switch and associated code. Clean up code to prevent the other command line switches from changing the saved settings.

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