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

Last change on this file was 9, checked in by dryeo, 5 years ago

Align to the 4.5 toolkit

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