WARN() about missing /dev/sequencer, it's too confusing for newbies.
[wine] / dlls / user / resource.c
1 /*
2  * USER resource functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  */
7
8 #include "windef.h"
9 #include "winbase.h"
10 #include "winerror.h"
11 #include "winnls.h"
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
14
15 #include "heap.h"
16 #include "ldt.h"
17 #include "debugtools.h"
18
19 DEFAULT_DEBUG_CHANNEL(resource);
20 DECLARE_DEBUG_CHANNEL(accel);
21
22 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
23 typedef struct
24 {
25     BYTE   fVirt;
26     BYTE   pad0;
27     WORD   key;
28     WORD   cmd;
29     WORD   pad1;
30 } PE_ACCEL, *LPPE_ACCEL;
31
32 /**********************************************************************
33  *                      LoadAccelerators16      [USER.177]
34  */
35 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
36 {
37     HRSRC16     hRsrc;
38
39     TRACE_(accel)("%04x %s\n", instance, debugres_a(lpTableName) );
40
41     if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATORA ))) {
42       WARN_(accel)("couldn't find accelerator table resource\n");
43       return 0;
44     }
45
46     TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
47     return LoadResource16(instance,hRsrc);
48 }
49
50 /**********************************************************************
51  *                      LoadAcceleratorsW       (USER32.356)
52  * The image layout seems to look like this (not 100% sure):
53  * 00:  BYTE    type            type of accelerator
54  * 01:  BYTE    pad             (to WORD boundary)
55  * 02:  WORD    event
56  * 04:  WORD    IDval           
57  * 06:  WORD    pad             (to DWORD boundary)
58  */
59 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
60 {
61     HRSRC hRsrc;
62     HACCEL hMem,hRetval=0;
63     DWORD size;
64
65     if (HIWORD(lpTableName))
66         TRACE_(accel)("%p '%s'\n",
67                       (LPVOID)instance, (char *)( lpTableName ) );
68     else
69         TRACE_(accel)("%p 0x%04x\n",
70                        (LPVOID)instance, LOWORD(lpTableName) );
71
72     if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
73     {
74       WARN_(accel)("couldn't find accelerator table resource\n");
75     } else {
76       hMem = LoadResource( instance, hRsrc );
77       size = SizeofResource( instance, hRsrc );
78       if(size>=sizeof(PE_ACCEL))
79       {
80         LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
81         LPACCEL16 accel16;
82         int i,nrofaccells = size/sizeof(PE_ACCEL);
83
84         hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
85         accel16 = (LPACCEL16)GlobalLock16(hRetval);
86         for (i=0;i<nrofaccells;i++) {
87                 accel16[i].fVirt = accel_table[i].fVirt;
88                 accel16[i].key = accel_table[i].key;
89                 accel16[i].cmd = accel_table[i].cmd;
90         }
91         accel16[i-1].fVirt |= 0x80;
92       }
93     }
94     TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
95     return hRetval;
96 }
97
98 /***********************************************************************
99  *              LoadAcceleratorsA   (USER32.355)
100  */
101 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
102 {
103         LPWSTR   uni;
104         HACCEL result;
105         if (HIWORD(lpTableName))
106                 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
107         else
108                 uni = (LPWSTR)lpTableName;
109         result = LoadAcceleratorsW(instance,uni);
110         if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
111         return result;
112 }
113
114 /**********************************************************************
115  *             CopyAcceleratorTableA   (USER32.58)
116  */
117 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
118 {
119   return CopyAcceleratorTableW(src, dst, entries);
120 }
121
122 /**********************************************************************
123  *             CopyAcceleratorTableW   (USER32.59)
124  *
125  * By mortene@pvv.org 980321
126  */
127 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
128                                      INT entries)
129 {
130   int i,xsize;
131   LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
132   BOOL done = FALSE;
133
134   /* Do parameter checking to avoid the explosions and the screaming
135      as far as possible. */
136   if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
137     WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
138          (LPVOID)src, (LPVOID)dst, entries);
139     return 0;
140   }
141   xsize = GlobalSize16(src)/sizeof(ACCEL16);
142   if (xsize>entries) entries=xsize;
143
144   i=0;
145   while(!done) {
146     /* Spit out some debugging information. */
147     TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
148           i, accel[i].fVirt, accel[i].key, accel[i].cmd);
149
150     /* Copy data to the destination structure array (if dst == NULL,
151        we're just supposed to count the number of entries). */
152     if(dst) {
153       dst[i].fVirt = accel[i].fVirt;
154       dst[i].key = accel[i].key;
155       dst[i].cmd = accel[i].cmd;
156
157       /* Check if we've reached the end of the application supplied
158          accelerator table. */
159       if(i+1 == entries) {
160         /* Turn off the high order bit, just in case. */
161         dst[i].fVirt &= 0x7f;
162         done = TRUE;
163       }
164     }
165
166     /* The highest order bit seems to mark the end of the accelerator
167        resource table, but not always. Use GlobalSize() check too. */
168     if((accel[i].fVirt & 0x80) != 0) done = TRUE;
169
170     i++;
171   }
172
173   return i;
174 }
175
176 /*********************************************************************
177  *                    CreateAcceleratorTableA   (USER32.64)
178  *
179  * By mortene@pvv.org 980321
180  */
181 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
182 {
183   HACCEL        hAccel;
184   LPACCEL16     accel;
185   int           i;
186
187   /* Do parameter checking just in case someone's trying to be
188      funny. */
189   if(cEntries < 1) {
190     WARN_(accel)("Application sent invalid parameters (%p %d).\n",
191          lpaccel, cEntries);
192     SetLastError(ERROR_INVALID_PARAMETER);
193     return (HACCEL)NULL;
194   }
195   FIXME_(accel)("should check that the accelerator descriptions are valid,"
196         " return NULL and SetLastError() if not.\n");
197
198
199   /* Allocate memory and copy the table. */
200   hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
201
202   TRACE_(accel)("handle %x\n", hAccel);
203   if(!hAccel) {
204     ERR_(accel)("Out of memory.\n");
205     SetLastError(ERROR_NOT_ENOUGH_MEMORY);
206     return (HACCEL)NULL;
207   }
208   accel = GlobalLock16(hAccel);
209   for (i=0;i<cEntries;i++) {
210         accel[i].fVirt = lpaccel[i].fVirt;
211         accel[i].key = lpaccel[i].key;
212         accel[i].cmd = lpaccel[i].cmd;
213   }
214   /* Set the end-of-table terminator. */
215   accel[cEntries-1].fVirt |= 0x80;
216
217   TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
218   return hAccel;
219 }
220
221 /*********************************************************************
222  *                    CreateAcceleratorTableW   (USER32.64)
223  *
224  * 
225  */
226 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
227 {
228   HACCEL        hAccel;
229   LPACCEL16     accel;
230   int           i;
231   char          ckey;  
232
233   /* Do parameter checking just in case someone's trying to be
234      funny. */
235   if(cEntries < 1) {
236     WARN_(accel)("Application sent invalid parameters (%p %d).\n",
237          lpaccel, cEntries);
238     SetLastError(ERROR_INVALID_PARAMETER);
239     return (HACCEL)NULL;
240   }
241   FIXME_(accel)("should check that the accelerator descriptions are valid,"
242         " return NULL and SetLastError() if not.\n");
243
244
245   /* Allocate memory and copy the table. */
246   hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
247
248   TRACE_(accel)("handle %x\n", hAccel);
249   if(!hAccel) {
250     ERR_(accel)("Out of memory.\n");
251     SetLastError(ERROR_NOT_ENOUGH_MEMORY);
252     return (HACCEL)NULL;
253   }
254   accel = GlobalLock16(hAccel);
255
256
257   for (i=0;i<cEntries;i++) {
258        accel[i].fVirt = lpaccel[i].fVirt;
259        if( !(accel[i].fVirt & FVIRTKEY) ) {
260           ckey = (char) lpaccel[i].key;
261          if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
262             WARN_(accel)("Error converting ASCII accelerator table to Unicode");
263        }
264        else 
265          accel[i].key = lpaccel[i].key; 
266        accel[i].cmd = lpaccel[i].cmd;
267   }
268
269   /* Set the end-of-table terminator. */
270   accel[cEntries-1].fVirt |= 0x80;
271
272   TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
273   return hAccel;
274 }
275
276 /******************************************************************************
277  * DestroyAcceleratorTable [USER32.130]
278  * Destroys an accelerator table
279  *
280  * NOTES
281  *    By mortene@pvv.org 980321
282  *
283  * PARAMS
284  *    handle [I] Handle to accelerator table
285  *
286  * RETURNS STD
287  */
288 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
289 {
290     return !GlobalFree16(handle); 
291 }
292   
293 /**********************************************************************
294  *     LoadString16   (USER.176)
295  */
296 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
297                            LPSTR buffer, INT16 buflen )
298 {
299     HGLOBAL16 hmem;
300     HRSRC16 hrsrc;
301     unsigned char *p;
302     int string_num;
303     int i;
304
305     TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
306           instance, resource_id, (int) buffer, buflen);
307
308     hrsrc = FindResource16( instance, (LPCSTR)((resource_id>>4)+1), RT_STRINGA );
309     if (!hrsrc) return 0;
310     hmem = LoadResource16( instance, hrsrc );
311     if (!hmem) return 0;
312     
313     p = LockResource16(hmem);
314     string_num = resource_id & 0x000f;
315     for (i = 0; i < string_num; i++)
316         p += *p + 1;
317     
318     TRACE("strlen = %d\n", (int)*p );
319     
320     if (buffer == NULL) return *p;
321     i = min(buflen - 1, *p);
322     if (i > 0) {
323         memcpy(buffer, p + 1, i);
324         buffer[i] = '\0';
325     } else {
326         if (buflen > 1) {
327             buffer[0] = '\0';
328             return 0;
329         }
330         WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
331     }
332     FreeResource16( hmem );
333
334     TRACE("'%s' loaded !\n", buffer);
335     return i;
336 }
337
338 /**********************************************************************
339  *      LoadStringW             (USER32.376)
340  */
341 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
342                             LPWSTR buffer, INT buflen )
343 {
344     HGLOBAL hmem;
345     HRSRC hrsrc;
346     WCHAR *p;
347     int string_num;
348     int i;
349
350     if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
351         resource_id = (UINT)(-((INT)resource_id));
352     TRACE("instance = %04x, id = %04x, buffer = %08x, "
353           "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
354
355     /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out 
356      * 20 - 31. */
357     hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
358                              RT_STRINGW );
359     if (!hrsrc) return 0;
360     hmem = LoadResource( instance, hrsrc );
361     if (!hmem) return 0;
362     
363     p = LockResource(hmem);
364     string_num = resource_id & 0x000f;
365     for (i = 0; i < string_num; i++)
366         p += *p + 1;
367     
368     TRACE("strlen = %d\n", (int)*p );
369     
370     if (buffer == NULL) return *p;
371     i = min(buflen - 1, *p);
372     if (i > 0) {
373         memcpy(buffer, p + 1, i * sizeof (WCHAR));
374         buffer[i] = (WCHAR) 0;
375     } else {
376         if (buflen > 1) {
377             buffer[0] = (WCHAR) 0;
378             return 0;
379         }
380 #if 0
381         WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
382 #endif
383     }
384
385     TRACE("%s loaded !\n", debugstr_w(buffer));
386     return i;
387 }
388
389 /**********************************************************************
390  *      LoadStringA     (USER32.375)
391  */
392 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
393                             LPSTR buffer, INT buflen )
394 {
395     INT    retval;
396     LPWSTR wbuf;
397
398     TRACE("instance = %04x, id = %04x, buffer = %08x, "
399           "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
400
401     if(buffer == NULL) /* asked size of string */
402         return LoadStringW(instance, resource_id, NULL, 0);
403     
404     wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
405     if(!wbuf)
406         return 0;
407
408     retval = LoadStringW(instance, resource_id, wbuf, buflen);
409     if(retval != 0)
410     {
411         retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
412         buffer[retval] = 0;
413         TRACE("%s loaded !\n", debugstr_a(buffer));
414     }
415     else buffer[0] = 0;    /* no check of buflen here */
416     HeapFree( GetProcessHeap(), 0, wbuf );
417
418     return retval;
419 }