source: trunk/gotcha.cpp@ 57

Last change on this file since 57 was 57, checked in by Gregg Young, 7 years ago

Add a -p command line switch to allow use of the default PM Print screen behavior (capture to printer)

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