Release 960331
[wine.git] / loader / resource.c
blob0de3993e58879c4e46570832a014b281cebc3d31
1 /*
2 * Resources
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include "arch.h"
16 #include "windows.h"
17 #include "gdi.h"
18 #include "global.h"
19 #include "neexe.h"
20 #include "accel.h"
21 #include "dlls.h"
22 #include "module.h"
23 #include "resource.h"
24 #include "stddebug.h"
25 #include "debug.h"
26 #include "libres.h"
28 #define PrintId(name) \
29 if (HIWORD((DWORD)name)) \
30 dprintf_resource( stddeb, "'%s'", (char *)PTR_SEG_TO_LIN(name)); \
31 else \
32 dprintf_resource( stddeb, "#%04x", LOWORD(name));
35 /**********************************************************************
36 * FindResource (KERNEL.60)
38 HRSRC FindResource( HMODULE hModule, SEGPTR name, SEGPTR type )
40 NE_MODULE *pModule;
42 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
43 dprintf_resource(stddeb, "FindResource: module=%04x type=", hModule );
44 PrintId( type );
45 if (HIWORD(name)) /* Check for '#xxx' name */
47 char *ptr = PTR_SEG_TO_LIN( name );
48 if (ptr[0] == '#') {
49 if (!(name = (SEGPTR)atoi( ptr + 1 ))) return 0;
52 dprintf_resource( stddeb, " name=" );
53 PrintId( name );
54 dprintf_resource( stddeb, "\n" );
55 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
56 #ifndef WINELIB
57 if (pModule->flags & NE_FFLAGS_WIN32)
59 fprintf(stderr,"Don't know how to FindResource() for Win32 module\n");
60 return 0;
62 return NE_FindResource( hModule, type, name );
63 #else
64 return LIBRES_FindResource( hModule, name, type );
65 #endif
69 /**********************************************************************
70 * LoadResource (KERNEL.61)
72 HGLOBAL LoadResource( HMODULE hModule, HRSRC hRsrc )
74 NE_MODULE *pModule;
76 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
77 dprintf_resource(stddeb, "LoadResource: module=%04x res=%04x\n",
78 hModule, hRsrc );
79 if (!hRsrc) return 0;
80 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
81 #ifndef WINELIB
82 if (pModule->flags & NE_FFLAGS_WIN32)
84 fprintf(stderr,"Don't know how to LoadResource() for Win32 module\n");
85 return 0;
87 return NE_LoadResource( hModule, hRsrc );
88 #else
89 return LIBRES_LoadResource( hModule, hRsrc );
90 #endif
94 /**********************************************************************
95 * LockResource (KERNEL.62)
97 /* 16-bit version */
98 SEGPTR WIN16_LockResource( HGLOBAL handle )
100 #ifndef WINELIB
101 HMODULE hModule;
102 NE_MODULE *pModule;
104 dprintf_resource(stddeb, "LockResource: handle=%04x\n", handle );
105 if (!handle) return (SEGPTR)0;
106 hModule = GetExePtr( handle );
107 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
108 if (pModule->flags & NE_FFLAGS_WIN32)
110 fprintf(stderr,"Don't know how to LockResource() for Win32 module\n");
111 return 0;
113 return NE_LockResource( hModule, handle );
114 #else
115 return LIBRES_LockResource( handle );
116 #endif
119 /* 32-bit version */
120 LPVOID LockResource( HGLOBAL handle )
122 #ifndef WINELIB
123 HMODULE hModule;
124 NE_MODULE *pModule;
126 dprintf_resource(stddeb, "LockResource: handle=%04x\n", handle );
127 if (!handle) return NULL;
128 hModule = GetExePtr( handle );
129 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
130 if (pModule->flags & NE_FFLAGS_WIN32)
132 fprintf(stderr,"Don't know how to LockResource() for Win32 module\n");
133 return 0;
135 return (LPSTR)PTR_SEG_TO_LIN( NE_LockResource( hModule, handle ) );
136 #else
137 return LIBRES_LockResource( handle );
138 #endif
142 /**********************************************************************
143 * FreeResource (KERNEL.63)
145 BOOL FreeResource( HGLOBAL handle )
147 #ifndef WINELIB
148 HMODULE hModule;
149 NE_MODULE *pModule;
151 dprintf_resource(stddeb, "FreeResource: handle=%04x\n", handle );
152 if (!handle) return FALSE;
153 hModule = GetExePtr( handle );
154 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
155 if (pModule->flags & NE_FFLAGS_WIN32)
157 fprintf(stderr,"Don't know how to FreeResource() for Win32 module\n");
158 return 0;
160 return NE_FreeResource( hModule, handle );
161 #else
162 return LIBRES_FreeResource( handle );
163 #endif
167 /**********************************************************************
168 * AccessResource (KERNEL.64)
170 INT AccessResource( HINSTANCE hModule, HRSRC hRsrc )
172 NE_MODULE *pModule;
174 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
175 dprintf_resource(stddeb, "AccessResource: module=%04x res=%04x\n",
176 hModule, hRsrc );
177 if (!hRsrc) return 0;
178 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
179 #ifndef WINELIB
180 if (pModule->flags & NE_FFLAGS_WIN32)
182 fprintf(stderr,"Don't know how to AccessResource() for Win32 module\n");
183 return 0;
185 return NE_AccessResource( hModule, hRsrc );
186 #else
187 return LIBRES_AccessResource( hModule, hRsrc );
188 #endif
192 /**********************************************************************
193 * SizeofResource (KERNEL.65)
195 DWORD SizeofResource( HMODULE hModule, HRSRC hRsrc )
197 NE_MODULE *pModule;
199 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
200 dprintf_resource(stddeb, "SizeofResource: module=%04x res=%04x\n",
201 hModule, hRsrc );
202 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
203 #ifndef WINELIB
204 if (pModule->flags & NE_FFLAGS_WIN32)
206 fprintf(stderr,"Don't know how to SizeOfResource() for Win32 module\n");
207 return 0;
209 return NE_SizeofResource( hModule, hRsrc );
210 #else
211 return LIBRES_SizeofResource( hModule, hRsrc );
212 #endif
216 /**********************************************************************
217 * AllocResource (KERNEL.66)
219 HGLOBAL AllocResource( HMODULE hModule, HRSRC hRsrc, DWORD size )
221 NE_MODULE *pModule;
223 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
224 dprintf_resource(stddeb, "AllocResource: module=%04x res=%04x size=%ld\n",
225 hModule, hRsrc, size );
226 if (!hRsrc) return 0;
227 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
228 #ifndef WINELIB
229 if (pModule->flags & NE_FFLAGS_WIN32)
231 fprintf(stderr,"Don't know how to AllocResource() for Win32 module\n");
232 return 0;
234 return NE_AllocResource( hModule, hRsrc, size );
235 #else
236 return LIBRES_AllocResource( hModule, hRsrc, size );
237 #endif
240 /**********************************************************************
241 * DirectResAlloc (KERNEL.168)
243 * Check Schulman, p. 232 for details
245 HANDLE DirectResAlloc(HANDLE hInstance, WORD wType, WORD wSize)
247 dprintf_resource(stddeb,"DirectResAlloc(%04x,%04x,%04x)\n",
248 hInstance, wType, wSize );
249 hInstance = GetExePtr(hInstance);
250 if(!hInstance)return 0;
251 if(wType != 0x10) /* 0x10 is the only observed value, passed from
252 CreateCursorIndirect. */
253 fprintf(stderr, "DirectResAlloc: wType = %x\n", wType);
254 return GLOBAL_Alloc(GMEM_MOVEABLE, wSize, hInstance, FALSE, FALSE, FALSE);
258 /**********************************************************************
259 * LoadAccelerators [USER.177]
261 HANDLE LoadAccelerators(HANDLE instance, SEGPTR lpTableName)
263 HANDLE hAccel;
264 HANDLE rsc_mem;
265 HRSRC hRsrc;
266 BYTE *lp;
267 ACCELHEADER *lpAccelTbl;
268 int i, n;
270 if (HIWORD(lpTableName))
271 dprintf_accel( stddeb, "LoadAccelerators: %04x '%s'\n",
272 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
273 else
274 dprintf_accel( stddeb, "LoadAccelerators: %04x %04x\n",
275 instance, LOWORD(lpTableName) );
277 if (!(hRsrc = FindResource( instance, lpTableName, RT_ACCELERATOR )))
278 return 0;
279 if (!(rsc_mem = LoadResource( instance, hRsrc ))) return 0;
281 lp = (BYTE *)LockResource(rsc_mem);
282 n = SizeofResource( instance, hRsrc ) / sizeof(ACCELENTRY);
283 hAccel = GlobalAlloc(GMEM_MOVEABLE,
284 sizeof(ACCELHEADER) + (n + 1)*sizeof(ACCELENTRY));
285 lpAccelTbl = (LPACCELHEADER)GlobalLock(hAccel);
286 lpAccelTbl->wCount = 0;
287 for (i = 0; i < n; i++) {
288 lpAccelTbl->tbl[i].type = *(lp++);
289 lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
290 lp += 2;
291 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
292 lp += 2;
293 if (lpAccelTbl->tbl[i].wEvent == 0) break;
294 dprintf_accel(stddeb,
295 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
296 i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval,
297 lpAccelTbl->tbl[i].type);
298 lpAccelTbl->wCount++;
300 GlobalUnlock(hAccel);
301 FreeResource( rsc_mem );
302 return hAccel;
305 /**********************************************************************
306 * TranslateAccelerator [USER.178]
308 int TranslateAccelerator(HWND hWnd, HANDLE hAccel, LPMSG msg)
310 ACCELHEADER *lpAccelTbl;
311 int i;
313 if (hAccel == 0 || msg == NULL) return 0;
314 if (msg->message != WM_KEYDOWN &&
315 msg->message != WM_KEYUP &&
316 msg->message != WM_SYSKEYDOWN &&
317 msg->message != WM_SYSKEYUP &&
318 msg->message != WM_CHAR) return 0;
320 dprintf_accel(stddeb, "TranslateAccelerators hAccel=%04x !\n", hAccel);
322 lpAccelTbl = (LPACCELHEADER)GlobalLock(hAccel);
323 for (i = 0; i < lpAccelTbl->wCount; i++) {
324 if(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) {
325 if(msg->wParam == lpAccelTbl->tbl[i].wEvent &&
326 (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)) {
327 INT mask = 0;
329 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= SHIFT_ACCEL;
330 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= CONTROL_ACCEL;
331 if(GetKeyState(VK_MENU) & 0x8000) mask |= ALT_ACCEL;
332 if(mask == (lpAccelTbl->tbl[i].type &
333 (SHIFT_ACCEL | CONTROL_ACCEL | ALT_ACCEL))) {
334 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval,
335 0x00010000L);
336 GlobalUnlock(hAccel);
337 return 1;
339 if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
340 return 1;
343 else {
344 if (msg->wParam == lpAccelTbl->tbl[i].wEvent &&
345 msg->message == WM_CHAR) {
346 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
347 GlobalUnlock(hAccel);
348 return 1;
352 GlobalUnlock(hAccel);
353 return 0;
356 /**********************************************************************
357 * LoadString
360 LoadString(HANDLE instance, WORD resource_id, LPSTR buffer, int buflen)
362 HANDLE hmem, hrsrc;
363 unsigned char *p;
364 int string_num;
365 int i;
367 dprintf_resource(stddeb,"LoadString: inst=%04x id=%04x buff=%08x len=%d\n",
368 instance, resource_id, (int) buffer, buflen);
370 hrsrc = FindResource( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING );
371 if (!hrsrc) return 0;
372 hmem = LoadResource( instance, hrsrc );
373 if (!hmem) return 0;
375 p = LockResource(hmem);
376 string_num = resource_id & 0x000f;
377 for (i = 0; i < string_num; i++)
378 p += *p + 1;
380 dprintf_resource( stddeb, "strlen = %d\n", (int)*p );
382 i = MIN(buflen - 1, *p);
383 if (buffer == NULL)
384 return i;
385 if (i > 0) {
386 memcpy(buffer, p + 1, i);
387 buffer[i] = '\0';
388 } else {
389 if (buflen > 1) {
390 buffer[0] = '\0';
391 return 0;
393 fprintf(stderr,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen, *p);
394 fprintf(stderr,"LoadString // and try to obtain string '%s'\n", p + 1);
396 FreeResource( hmem );
398 dprintf_resource(stddeb,"LoadString // '%s' copied !\n", buffer);
399 return i;