source: trunk/gotcha.cpp@ 52

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

Fix code for disabling PM's print screen.

  • Property svn:eol-style set to native
File size: 11.9 KB
RevLine 
[2]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"
[10]23#include "dll/gotchdll.h"
[2]24#include "settings.h"
25#include "string.h"
26
27#include "io.h"
[13]28#define INCL_EXCEPTQ_CLASS
29#define INCL_LOADEXCEPTQ
30#include "exceptq.h"
[2]31
32HAB hab;
33HWND hwndFrame, hwndSnapshot;
34PFNWP OldFrameWP, wpOldButton;
35PSETTINGS pset;
[38]36BOOL g_fIdle = FALSE, g_fSetPathName = FALSE;
[2]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
[27]46// ** CheckCmdlineArgs **************************************************** /*FOLD00*/
[2]47
48BOOL CheckCmdlineArgs (int argc, char *argv[])
49{
50 BOOL fAuto = FALSE;
51
52 for (USHORT i = 1; i < argc; i++)
53 {
[38]54 // batch mode
55 if ((stricmp (argv[i], "-a") == 0))
[2]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]);
[27]77 g_fSetPathName = TRUE;
[2]78 }
79 }
80 // set to idle priority
81 else if (stricmp (argv[i], "-i") == 0)
82 {
[27]83 g_fIdle = TRUE;
[2]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{
[13]95 ScopedExceptqLoader sel;
[2]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());
[30]108#ifdef _QUIET_
[50]109 if ((Version() < 2))
[2]110 {
[38]111 DisplayError("GOTCHDLL.DLL TOO OLD",
[36]112 "The file gotchdll.dll is to old for this version of "
113 "Gotcha! Quiet As you should have received a sufficiently "
[2]114 "new version with the program, check if an older "
[36]115 "version of gotchdll.dll is still residing somewhere "
[2]116 "in your LIBPATH. And did you actually copy the new "
[36]117 "gotchdll.dll to some directory in your LIBPATH?");
[2]118 exit (0);
119 }
[27]120 int rc;
[50]121 HMTX hmtx = NULLHANDLE;
122 // running multiple instances orphans all but the first closed hook unloaded
123 rc = DosOpenMutexSem("\\SEM32\\GOTCHA", &hmtx);
124 if (!rc) {
125 DisplayError("GOTCHA QUIET ALREADY RUNNING",
126 "Another instance of Gotcha Quiet is running."
127 " You can open Gotcha Quiet's settings using"
128 " the Alt+Print Screen key combination");
129 exit (0);
[27]130 }
[50]131 rc = DosCreateMutexSem("\\SEM32\\GOTCHA", &hmtx, 0, FALSE);
132 if (rc) {
133 DisplayError("Semaphore creation failed",
134 "Try restarting Gotcha Quiet");
135 exit (0);
136 }
[30]137#endif
[27]138
[2]139 // register our window classes
140 WinRegisterClass (hab, "thth.wc.gotcha.main", WindowProcedure, 0L,
141 sizeof (ULONG)*2L);
142 WinRegisterClass (hab, "thth.wc.gotcha.snapshot", wpSnapshot,
143 CS_SIZEREDRAW, sizeof (ULONG)*2L);
144
145 // load the settings
146 pset = new SETTINGS;
147
[27]148 pset->idleSetInIni = pset->QueryFlag(SEI_IDLEPRIORITY);
149 pset->saveStyle = pset->QuerySaveStyle ();
150 pset->pNumSaveDir = pset->QueryNumSaveDir ();
151 pset->pForceSaveFile = pset->QueryForceSaveFile();
152 pset->bSerialCapture = pset->SerialCapture ();
[50]153
154#ifdef _QUIET_
155 bool fPrtScr = FALSE;
[52]156 // always turn it off while running
157 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
158 // WinSet does not effect the ini setting
[50]159 ULONG ulDataSize = 0;
[52]160 rc = PrfQueryProfileSize(HINI_USERPROFILE, "PM_ControlPanel", "PrintScreen",
[50]161 &ulDataSize );
162 rc = PrfQueryProfileData(HINI_USERPROFILE, "PM_ControlPanel", "PrintScreen",
163 &fPrtScr, &ulDataSize);
[52]164 if (!rc) // Print screen is on by default (no ini entry)
165 fPrtScr = TRUE;
[50]166#endif
[2]167 // check cmd line args and if "-a" found take screenshot and exit
168 if (CheckCmdlineArgs (argc, argv))
169 {
170 CaptureWindow (HWND_DESKTOP, HWND_DESKTOP, NULL, TRUE);
[27]171 pset->SetFileSaveStyle(pset->saveStyle);
[38]172 pset->SetNumSaveDir (pset->pNumSaveDir);
[2]173 delete pset;
174 WinDestroyMsgQueue (hmq);
175 WinTerminate (hab);
176 exit (0);
177 }
178
179 SetIdlePriority(pset->QueryFlag(SEI_IDLEPRIORITY));
180
181 // create the windows
182 hwndFrame = CreateMainWindow ();
183 hwndSnapshot = CreateSnapshotWindow ();
184
185 SWP swp;
186 USHORT us[7];
187
188 // position main window
189 pset->QueryWindowData (&swp, us, FALSE);
190 WinSetWindowPos (hwndFrame, HWND_DESKTOP, swp.x,swp.y, 0,0,
191 SWP_SHOW | SWP_MOVE);
192
[38]193
[30]194#ifdef _QUIET_
[2]195 InitDLL (hab, hwndFrame);
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
[27]222
223 // Set priorty to unless user set regular since running -q sets it to idle in the ini
[38]224 if (g_fIdle) {
[27]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 }
[2]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
[30]245#ifdef _QUIET_
[52]246 // Reset to user PM print screen choice
247 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
[38]248 StopInputHook ();
[50]249 DosCloseMutexSem(hmtx);
[2]250#endif
[50]251
[2]252 delete g_phelp;
253 delete pset;
254
255 WinDestroyMsgQueue (hmq);
256 WinTerminate (hab);
257
258#ifdef _DOLOGMEM_
259 LogMem("main", FALSE);
260#endif
261#ifdef _DOLOGDEBUG_
262 LogDebug( "Gotcha! end" );
263#endif
264}
265
266// ** DisplayError ******************************************************** /*FOLD00*/
267
268VOID DisplayError (PSZ pszTitle, PSZ psz, ...)
269{
270 CHAR dstring[401];
271 va_list valst;
272
273 va_start (valst, psz);
274 vsnprintf (dstring, 401, psz, valst);
275 va_end (valst);
276
277 WinMessageBox (HWND_DESKTOP, WinQueryActiveWindow (HWND_DESKTOP), dstring,
278 pszTitle, 0, MB_OK | MB_ERROR | MB_APPLMODAL | MB_MOVEABLE);
279}
280
[38]281// saymsg2 was adapted from code in FM/2
[27]282APIRET saymsg2(int DefaultButton, HWND hwnd, PCSZ pszTitle, PCSZ pszFmt, ...)
283{
284 ULONG i;
285 APIRET rc;
286 CHAR szMsg[4096];
287 va_list va;
288 MB2INFO *pmbInfo;
289 MB2D mb2dBut[3];
290 ULONG ulInfoSize = (sizeof(MB2INFO) + (sizeof(MB2D) * 2));
[2]291
[27]292 va_start(va, pszFmt);
293 szMsg[sizeof(szMsg) - 1] = 0;
294 vsprintf(szMsg, pszFmt, va);
295 va_end(va);
296
297 if (szMsg[sizeof(szMsg) - 1]) {
298 fprintf(stderr, "Buffer overflow in saymsg2 - need %u bytes\n", strlen(szMsg) + 1);
299 fflush(stderr);
300 }
301
302 memset(mb2dBut, 0, sizeof(MB2D) * 3);
303 strcpy(mb2dBut[0].achText,"Ok");
304 strcpy(mb2dBut[1].achText,"Cancel");
305 strcpy(mb2dBut[2].achText,"Settings");
306 mb2dBut[0].idButton = 1;
307 mb2dBut[1].idButton = 2;
308 mb2dBut[2].idButton = 3;
309 if (DefaultButton)
310 mb2dBut[DefaultButton - 1].flStyle = BS_DEFAULT;
311 pmbInfo = (MB2INFO *) malloc(ulInfoSize);
312 memset(pmbInfo, 0, ulInfoSize);
313 if (pmbInfo) {
314 pmbInfo->cb = ulInfoSize;
315 pmbInfo->hIcon = 0;
316 pmbInfo->cButtons = 3;
317 pmbInfo->flStyle = MB_MOVEABLE | MB_ICONQUESTION ;
318 pmbInfo->hwndNotify = NULLHANDLE;
319 for (i = 0; i < 3; i++) {
320 memcpy( pmbInfo->mb2d+i , mb2dBut+i , sizeof(MB2D));
321 }
322 rc = WinMessageBox2(HWND_DESKTOP, hwnd,
323 szMsg, pszTitle, SM2_DIALOG,
324 pmbInfo);
325 WinSetFocus(HWND_DESKTOP, SM2_DIALOG);
326 free(pmbInfo);
327 return rc;
328 }
329 return MBID_ERROR;
330}
331// ** AddSysMenuItem ****************************************************** /*FOLD00*/
332
[2]333VOID AddSysMenuItem (HWND hwndFrame, MENUITEM *Item, PSZ Text)
334{
335 HWND hwndSysMenu = WinWindowFromID (hwndFrame, FID_SYSMENU);
336 USHORT idSysMenu = SHORT1FROMMR (WinSendMsg (hwndSysMenu,
337 MM_ITEMIDFROMPOSITION, NULL,
338 NULL ));
339 MENUITEM miSysMenu;
340 WinSendMsg (hwndSysMenu, MM_QUERYITEM,
341 MPFROM2SHORT (idSysMenu,FALSE), MPFROMP(&miSysMenu));
342
343 HWND hwndSysSubMenu = miSysMenu.hwndSubMenu;
344
345 WinSendMsg (hwndSysSubMenu, MM_INSERTITEM, MPFROMP(Item), MPFROMP(Text));
346}
347
348// ** DoCountdown ********************************************************* /*fold00*/
349
350VOID DoCountdown (ULONG ul)
351{
352 if (ul > 10)
353 DosBeep (4000L-3000L, 20);
354 else
355 DosBeep (4000L-ul*300L, 20);
356}
357
358// ** SetIdlePriority **************************************************** /*FOLD00*/
359
360VOID SetIdlePriority (BOOL f)
361{
[52]362#ifndef _QUEIT_
[2]363 if (f)
364 DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0);
365 else
366 DosSetPriority(PRTYS_PROCESS, PRTYC_REGULAR, 0, 0);
[27]367#else
368 DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
369#endif
[2]370}
371
372// ***********************************************************************
373
374#ifdef _DOLOGMEM_
375VOID LogMem (PSZ psz, BOOL f)
376{
377 FILE *pf = fopen("gotcha.mem","ab");
378 static ULONG TotalPhysicalMemory, ul = 0;
379 ULONG i;
380 if (!f)
381 ul--;
382 DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &TotalPhysicalMemory,
383 sizeof(TotalPhysicalMemory));
384 for (i = 0; i < ul; i++)
385 fputs(" ", pf);
386 fprintf(pf,"%10ld (%s)\n", TotalPhysicalMemory, psz);
387 if (f)
388 ul++;
389 fclose(pf);
390}
391#endif
392
393#ifdef _DOLOGDEBUG_
394VOID LogDebug( PSZ psz, ... )
395{
396 CHAR dstring[401];
397 va_list valst;
398
399 va_start (valst, psz);
400 vsnprintf (dstring, 401, psz, valst);
401 va_end (valst);
402
403 FILE *pf = fopen( "gotcha.log", "ab" );
404 fprintf( pf, "%s\n", dstring );
405 fclose( pf );
406}
407#endif
408
409// ***********************************************************************
Note: See TracBrowser for help on using the repository browser.