source: branches/classes/c/c_common/helper.c

Last change on this file was 165, checked in by gyoung, 17 months ago

Changes to avoid opening winos2/mplayer.exe; Add avi, mov and jpeg to the file types opened by a modern video player. Use the generic video icon when the modern video player is used; quiet the screen when mplayer is used; get mplayer and ffplay to close when video is finished

File size: 13.3 KB
Line 
1/*
2 * This file is (C) Chris Wohlgemuth 2001-2005
3 */
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, or (at your option)
8 * 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; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19/*
20 * If you need another license for your prject/product contact me at
21 *
22 * http://www.os2world.com/cdwriting
23 * http://www.geocities.com/SiliconValley/Sector/5785/
24 */
25#define INCL_BASE
26#define INCL_WIN
27#define INCL_DOS
28#define INCL_DOSERRORS
29
30#include <os2.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33
34#include <stdio.h>
35#include <string.h>
36#include <stdarg.h>
37#include "except.h"
38#include "cwmmdataf.h"
39
40#include "cwaudioinc.h"
41#include "sys_funcs.h"
42
43#ifndef BS_NOTEBOOKBUTTON
44#define BS_NOTEBOOKBUTTON 8L /* Warp 4 notebook style */
45#endif
46
47#define RESDLLNAME "\\bin\\mmres_%c%c.dll"
48#define DEFRESDLLNAME "\\mmres_en.dll" /* bin directory already appended when used */
49
50extern char classDLLPath[CCHMAXPATH];
51extern char chrInstallDir[CCHMAXPATH];
52extern char resDLLPath[CCHMAXPATH];
53
54HMODULE queryModuleHandle(void);
55void writeLog(const char* chrFormat, ...);
56
57MRESULT EXPENTRY appTerminateHandlerProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
58
59ULONG launchPMProg(PSZ pszTitle, PSZ wrapperExe, PSZ parameters, WPObject *thisPtr, ULONG ulView)
60{
61 HWND hwndNotify;
62 PROGDETAILS pDetails;
63 HAPP happ;
64 HWND hwndAppTerminateHandler;
65 char startupDir[CCHMAXPATH] = {0};
66 char *chrPtr;
67
68
69 memset(&pDetails, 0, sizeof(pDetails));
70 pDetails.Length = sizeof(PROGDETAILS);
71 pDetails.progt.progc = PROG_DEFAULT;
72 pDetails.progt.fbVisible = SHE_VISIBLE;
73 pDetails.pszTitle = pszTitle;
74 pDetails.pszParameters = parameters;
75 /* Set the startup directory to the location of the executable */
76 strncpy(startupDir, wrapperExe, sizeof(startupDir) - 1);
77 // SysWriteToTrapLog("1: %s %s\n", startupDir, wrapperExe);
78 if((chrPtr=strrchr(startupDir,'\\'))!=NULLHANDLE) {
79 *chrPtr=0;
80 pDetails.pszStartupDir = startupDir;
81 //SysWriteToTrapLog(pDetails.pszStartupDir);
82 }
83 pDetails.pszExecutable = wrapperExe;
84
85 pDetails.pszEnvironment = NULLHANDLE;
86 pDetails.swpInitial.fl = SWP_ACTIVATE|SWP_ZORDER; /* window positioning */
87 // pDetails.swpInitial.cy = 0; /* width of window */
88 // pDetails.swpInitial.cx = 0; /* height of window */
89 // pDetails.swpInitial.y = 0; /* lower edge of window */
90 // pDetails.swpInitial.x = 0; /* left edge of window */
91 pDetails.swpInitial.hwndInsertBehind = HWND_TOP;
92 // pDetails.swpInitial.hwnd = 0;
93 // pDetails.swpInitial.ulReserved1 = 0;
94 // pDetails.swpInitial.ulReserved2 = 0;
95
96 /* Create an object window which will process the WM_APPTERMINATE message. While processing of this
97 message the view item (see below) will be removed from the inuse list thus removing the inuse emphasis. */
98 hwndAppTerminateHandler=WinCreateWindow(HWND_OBJECT,WC_STATIC,"CWMMAppTerminate",0,0,0,0,0,NULLHANDLE,HWND_BOTTOM,13344,NULL,NULL);
99 if(hwndAppTerminateHandler) {
100 WinSubclassWindow(hwndAppTerminateHandler,&appTerminateHandlerProc);
101 WinSetWindowULong(hwndAppTerminateHandler, QWL_USER,(ULONG)thisPtr);
102 }
103 /* Start the application */
104 if(strstr(strlwr(parameters), "mplayer") /*|| strstr(strlwr(parameters), "ffplay")*/)
105 happ = WinStartApp( hwndAppTerminateHandler, &pDetails, NULL, NULL, SAF_INSTALLEDCMDLINE | SAF_MINIMIZED);
106 else
107 happ = WinStartApp( hwndAppTerminateHandler, &pDetails, NULL, NULL, SAF_INSTALLEDCMDLINE);
108 //happ = WinStartApp( hwndAppTerminateHandler, &pDetails, parameters, NULL, 0);
109 if(happ) {
110 /* Application started. Add the object to inuse list. */
111 ULONG ulError;
112 MMDATAFILEVIEW *pParams=(MMDATAFILEVIEW*)_wpAllocMem(thisPtr,sizeof(MMDATAFILEVIEW), &ulError);
113
114 if(pParams) {
115 /* We have memory */
116 memset((void*)pParams ,0, sizeof(MMDATAFILEVIEW));
117 pParams->pid=0;
118 pParams->thisPtr=thisPtr; /* The object */
119 pParams->viewItem.view=ulView; /* The open view */
120 pParams->useItem.type=USAGE_OPENVIEW; /* We add a view item to the inus list */
121 pParams->viewItem.handle=happ; /* a HAPP seems to work here */
122 if(somIsObj(thisPtr))
123 _wpAddToObjUseList(thisPtr, &pParams->useItem); /* add to inuse list */
124 }
125 }
126 return happ;
127}
128
129
130void writeLog(const char* chrFormat, ...)
131{
132 char logNameLocal[CCHMAXPATH];
133 FILE *fHandle;
134
135 sprintf(logNameLocal,"d:\\cwmm.log");
136 fHandle=fopen(logNameLocal,"a");
137 if(fHandle) {
138 va_list arg_ptr;
139 void *tb;
140
141 va_start (arg_ptr, chrFormat);
142 vfprintf(fHandle, chrFormat, arg_ptr);
143 va_end (arg_ptr);
144 // fprintf(fHandle,logText);
145 fclose(fHandle);
146 }
147}
148
149/* Get the handle of our resource DLL */
150HMODULE queryResModuleHandle(void)
151{
152 static ULONG hModule=0;
153
154 if(!hModule)
155 {
156
157 if(queryModuleHandle())
158 {
159 char *chrPtr;
160 PSZ pszLang="";
161 char* found;
162 APIRET rc;
163
164 /* Got class dll */
165 /* Get install dir (base dir) */
166 strncpy(resDLLPath, chrInstallDir, sizeof(resDLLPath));
167 resDLLPath[sizeof(resDLLPath)-1]=0; /* Paranoia */
168
169 /* Now get language */
170 do
171 {
172 char buf[CCHMAXPATH];
173 /* Get Language var */
174 if(NO_ERROR!=DosScanEnv("LANG", &pszLang))
175 break;
176
177 /* Skip leading spaces */
178 chrPtr=pszLang;
179 while(*chrPtr==' ')
180 chrPtr++;
181
182 /* Check if value seems to be valid. The var must be something like xx_XX thus length is 5 */
183 if(strlen(chrPtr)<5)
184 break;
185
186 /* Extract the first two chars */
187 sprintf(buf, RESDLLNAME, chrPtr[0], chrPtr[1]);
188 strcat(resDLLPath,buf);
189 /* Insert message in Logfile */
190 // HlpWriteToTrapLog("Using the following DLL path: ");
191 // HlpWriteToTrapLog(resDLLPath);
192 // HlpWriteToTrapLog("\n");
193
194 rc=DosLoadModule(buf,sizeof(buf), resDLLPath, &hModule);
195 if(rc==NO_ERROR)
196 break;
197
198 /* Insert message in Logfile */
199 // writeLog("Ressource-DLL for the current countrycode not found. Trying to load default one (CDFLD001.DLL).\n");
200
201 /* NLS DLL not found. Try to load default */
202 found=strrchr(resDLLPath,'\\');
203 if(!found)
204 break;
205
206 *found=0;
207
208 strcat(resDLLPath, DEFRESDLLNAME);
209 // HlpWriteToTrapLog("Using the following DLL path: ");
210 // HlpWriteToTrapLog(resDLLPath);
211 // HlpWriteToTrapLog("\n");
212
213 rc=DosLoadModule(buf,sizeof(buf), resDLLPath, &hModule);
214 if(rc!=NO_ERROR) {
215 // writeLog("Can't load DLL!\n");
216 hModule=NULLHANDLE;
217 }
218 else {
219 //writeLog("Ressource DLL loaded.\n");
220 }
221
222 break;
223 }while(TRUE);
224 }
225 }
226 return hModule;
227 }
228
229 /* Get the HMODULE of the MM class DLL */
230HMODULE queryModuleHandle(void)
231{
232 PSZ moduleName;
233 static ULONG hModule=0;
234 somId mySomId;
235
236 if(!hModule)
237 {
238 char *chrPtr;
239 ULONG ulObj, ulBufLen, ulOffset;
240 char thePath[CCHMAXPATH];
241
242 if(DosQueryModFromEIP( &hModule,
243 &ulObj,
244 CCHMAXPATH,
245 thePath,
246 &ulOffset,
247 (ULONG)queryModuleHandle )!=NO_ERROR) {
248 SysWriteToTrapLog("Can't get module handle for CWMM class DLL!\n");
249 hModule=0;
250 return NULLHANDLE; /* Error */
251 }
252#if 0
253 /*
254 Sometimes the returned name isn't correct. To prevent loading a bunch of classes just
255 to get the module handle the following is replaced by the undocumented DosQueryModFromEIP()
256 call. Maybe this even fixes some deadlocks people had during WPS startup with the classes
257 because now the WPS isn't urged to load all classes at once.
258 */
259
260 mySomId=somIdFromString("M_MMImage");
261 moduleName=_somLocateClassFile(SOMClassMgrObject, mySomId, 1, 1);
262 /* The above function sometimes returns the class name instead of the dll name.
263 I suspect it's because with DTS there's no dllname modifier to tell SOM
264 the name. But why does it sometimes work??
265 To circumvent the problem not having a module handle we try this function for
266 the other classnames again hoping that one of the calls is succesful (until now
267 it is). */
268 SOMFree(mySomId);
269 if( DosQueryModuleHandle(moduleName, &hModule))
270 {
271 mySomId=somIdFromString("MMImage");
272 moduleName=_somLocateClassFile(SOMClassMgrObject, mySomId, 1, 1);
273 SOMFree(mySomId);
274 if( DosQueryModuleHandle(moduleName, &hModule))
275 {
276 mySomId=somIdFromString("M_MMAudio");
277 moduleName=_somLocateClassFile(SOMClassMgrObject, mySomId, 1, 1);
278 SOMFree(mySomId);
279 if( DosQueryModuleHandle(moduleName, &hModule))
280 {
281 mySomId=somIdFromString("M_MMAVC");
282 moduleName=_somLocateClassFile(SOMClassMgrObject, mySomId, 1, 1);
283 SOMFree(mySomId);
284 DosQueryModuleHandle(moduleName, &hModule);
285 }
286 }
287 }
288 strncpy(classDLLPath,moduleName, sizeof(classDLLPath));
289 classDLLPath[sizeof(classDLLPath)-1]=0;
290
291 /* Get install dir */
292 strncpy(chrInstallDir,moduleName, sizeof(chrInstallDir));
293 chrInstallDir[sizeof(chrInstallDir)-1]=0;
294 if((chrPtr=strrchr(chrInstallDir,'\\'))!=NULLHANDLE)
295 *chrPtr=0;
296#endif
297
298 /* Get module name. */
299 if(!DosQueryModuleName(hModule,sizeof(chrInstallDir),chrInstallDir)) {
300 strncpy(classDLLPath,chrInstallDir, sizeof(classDLLPath));
301 classDLLPath[sizeof(classDLLPath)-1]=0;
302
303 /* Get install dir */
304 if((chrPtr=strrchr(chrInstallDir,'\\'))!=NULLHANDLE)
305 *chrPtr=0;
306 }
307 }/* if(!hModule) */
308 return hModule;
309}
310
311
312PSZ queryModuleName(void)
313{
314 if(queryModuleHandle())
315 return classDLLPath;
316
317 return NULLHANDLE;
318}
319
320PSZ queryInstallDir(void)
321{
322 if(queryModuleHandle())
323 return chrInstallDir;
324
325 return NULLHANDLE;
326}
327
328/* MMCLS_INI_FILE_NAME = "\\cwmm.ini" See ...\common.h */
329PSZ _queryMMClassIniFile(void)
330{
331 static char iniName[CCHMAXPATH]= {0};
332
333 if(iniName[0]==0) {
334 /* Default is INI file in users home dir */
335 if(SysQueryHomeDir(iniName, sizeof(iniName))) {
336 strlcat(iniName, MMCLS_INI_FILE_NAME, sizeof(iniName)); /* MMCLS_INI_FILE_NAME = "\\CWMM.INI" */
337 return iniName;
338 }
339
340 sprintf(iniName, "%s\\bin%s", queryInstallDir(), MMCLS_INI_FILE_NAME);
341 }
342
343 return iniName;
344}
345
346BOOL cwMoveNotebookButtonsWarp4(HWND hwndDlg, USHORT usID, USHORT usDelta)
347{
348 if(SysQueryOSRelease()>=40) {
349 HENUM henum;
350 HWND hwnd;
351
352 /* Move the default notebook buttons */
353 if((henum=WinBeginEnumWindows(hwndDlg))!=NULLHANDLE) {
354 while((hwnd=WinGetNextWindow(henum))!=NULLHANDLE) {
355 if(WinQueryWindowUShort(hwnd,QWS_ID)<=usID)
356 WinSetWindowBits(hwnd, QWL_STYLE,
357 BS_NOTEBOOKBUTTON, BS_NOTEBOOKBUTTON);
358 else {
359 SWP swp;
360 POINTL ptl= {0};
361 ptl.y=usDelta;
362
363 WinMapDlgPoints(hwndDlg, &ptl, 1, TRUE);
364 /* Move all other controls */
365 if(WinQueryWindowPos(hwnd, &swp))
366 WinSetWindowPos(hwnd, NULLHANDLE, swp.x, swp.y-ptl.y,0,0, SWP_MOVE);
367 }
368 }
369 WinEndEnumWindows(henum);
370 }
371 }
372 return TRUE;
373}
374
375void HlpSendCommandToObject(char* chrObject, char* command)
376{
377 HOBJECT hObject;
378
379 hObject=WinQueryObject(chrObject);
380 if(hObject!=NULLHANDLE) {
381 WinSetObjectData(hObject, command);
382 }
383}
384
385#if 0
386void HlpOpenWebPage(char* chrUrl)
387{
388 HOBJECT hObject;
389 char tempDir[CCHMAXPATH];
390 char setup[CCHMAXPATH*2];
391
392 if(HlpQueryTempDir(chrInstallDir, tempDir, sizeof(tempDir)))
393 {
394 snprintf(setup, sizeof(setup), "LOCATOR=%s;%s", chrUrl, "OPEN=DEFAULT");
395 WinCreateObject("WPUrl","tempUrl", setup, tempDir,
396 CO_UPDATEIFEXISTS);
397 /* WinDestroyObject(hObject); Don't use it, blocks PM */
398 }
399}
400
401/* This function checks if the given file exists */
402ULONG checkFileSize(char* chrFileName)
403{
404 struct stat statBuf;
405
406 /* Check file path */
407 if(stat(chrFileName , &statBuf)==-1)
408 return 0;
409
410 return statBuf.st_size;
411}
412#endif
413
414
415
416
417
418
419
420
421
Note: See TracBrowser for help on using the repository browser.