source: trunk/gotcha.cpp@ 89

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

Remove snapshot window stuff from Gotcha Quiet. Add strings and adjust code so the error caused by trying to open GQ twice can be translated

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