1 static char RCSId[] = "$Id: resource.c,v 1.4 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
4 #include <X11/Intrinsic.h>
11 #include "prototypes.h"
19 #define MIN(a,b) ((a) < (b) ? (a) : (b))
21 typedef struct resource_s
23 struct resource_s *next;
26 struct resource_nameinfo_s nameinfo;
30 static int ResourceFd = -1;
31 static HANDLE ResourceInst = 0;
32 static struct w_files *ResourceFileInfo = NULL;
33 static RESOURCE *Top = NULL;
34 extern HINSTANCE hSysRes;
36 HANDLE RSC_LoadResource(int instance, char *rsc_name, int type, int *image_size_ret);
39 /**********************************************************************
43 OpenResourceFile(HANDLE instance)
47 if (ResourceInst == instance)
50 w = GetFileInfo(instance);
57 ResourceInst = instance;
59 ResourceFd = open(w->filename, O_RDONLY);
64 /**********************************************************************
68 ConvertCoreBitmap( HDC hdc, BITMAPCOREHEADER * image )
73 int i, size, n_colors;
75 n_colors = 1 << image->bcBitCount;
77 if (image->bcBitCount < 24)
79 size = sizeof(BITMAPINFOHEADER) + n_colors * sizeof(RGBQUAD);
80 bits = (char *) (image + 1) + (n_colors * sizeof(RGBTRIPLE));
84 size = sizeof(BITMAPINFOHEADER);
85 bits = (char *) (image + 1);
87 bmpInfo = (BITMAPINFO *) malloc( size );
89 bmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
90 bmpInfo->bmiHeader.biWidth = image->bcWidth;
91 bmpInfo->bmiHeader.biHeight = image->bcHeight;
92 bmpInfo->bmiHeader.biPlanes = image->bcPlanes;
93 bmpInfo->bmiHeader.biBitCount = image->bcBitCount;
94 bmpInfo->bmiHeader.biCompression = 0;
95 bmpInfo->bmiHeader.biSizeImage = 0;
96 bmpInfo->bmiHeader.biXPelsPerMeter = 0;
97 bmpInfo->bmiHeader.biYPelsPerMeter = 0;
98 bmpInfo->bmiHeader.biClrUsed = 0;
99 bmpInfo->bmiHeader.biClrImportant = 0;
101 if (image->bcBitCount < 24)
103 RGBTRIPLE * oldMap = (RGBTRIPLE *)(image + 1);
104 RGBQUAD * newMap = bmpInfo->bmiColors;
105 for (i = 0; i < n_colors; i++, oldMap++, newMap++)
107 newMap->rgbRed = oldMap->rgbtRed;
108 newMap->rgbGreen = oldMap->rgbtGreen;
109 newMap->rgbBlue = oldMap->rgbtBlue;
110 newMap->rgbReserved = 0;
114 hbitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
115 bits, bmpInfo, DIB_RGB_COLORS );
120 /**********************************************************************
124 ConvertInfoBitmap( HDC hdc, BITMAPINFO * image )
126 char * bits = ((char *)image) + DIB_BitmapInfoSize(image, DIB_RGB_COLORS);
127 return CreateDIBitmap( hdc, &image->bmiHeader, CBM_INIT,
128 bits, image, DIB_RGB_COLORS );
131 /**********************************************************************
132 * FindResourceByNumber
135 FindResourceByNumber(struct resource_nameinfo_s *result_p,
136 int type_id, int resource_id)
138 struct resource_typeinfo_s typeinfo;
139 struct resource_nameinfo_s nameinfo;
140 unsigned short size_shift;
145 * Move to beginning of resource table.
147 rtoff = (ResourceFileInfo->mz_header->ne_offset +
148 ResourceFileInfo->ne_header->resource_tab_offset);
149 lseek(ResourceFd, rtoff, SEEK_SET);
154 if (read(ResourceFd, &size_shift, sizeof(size_shift)) !=
157 printf("FindResourceByNumber (%s) bad block size !\n", resource_id);
164 typeinfo.type_id = 0xffff;
165 while (typeinfo.type_id != 0) {
166 if (read(ResourceFd, &typeinfo, sizeof(typeinfo)) !=
168 printf("FindResourceByNumber (%X) bad typeinfo size !\n", resource_id);
171 #ifdef DEBUG_RESOURCE
172 printf("FindResourceByNumber type=%X count=%d\n",
173 typeinfo.type_id, typeinfo.count);
175 if (typeinfo.type_id == 0) break;
176 if (typeinfo.type_id == type_id || type_id == -1) {
177 for (i = 0; i < typeinfo.count; i++) {
178 if (read(ResourceFd, &nameinfo, sizeof(nameinfo)) !=
180 printf("FindResourceByNumber (%X) bad nameinfo size !\n", resource_id);
183 #ifdef DEBUG_RESOURCE
184 printf("FindResource: search type=%X id=%X // type=%X id=%X\n",
185 type_id, resource_id, typeinfo.type_id, nameinfo.id);
187 if (nameinfo.id == resource_id) {
188 memcpy(result_p, &nameinfo, sizeof(nameinfo));
194 lseek(ResourceFd, (typeinfo.count * sizeof(nameinfo)), SEEK_CUR);
200 /**********************************************************************
204 FindResourceByName(struct resource_nameinfo_s *result_p,
205 int type_id, char *resource_name)
207 struct resource_typeinfo_s typeinfo;
208 struct resource_nameinfo_s nameinfo;
209 unsigned short size_shift;
210 off_t old_pos, new_pos;
211 unsigned char nbytes;
217 * Move to beginning of resource table.
219 rtoff = (ResourceFileInfo->mz_header->ne_offset +
220 ResourceFileInfo->ne_header->resource_tab_offset);
221 lseek(ResourceFd, rtoff, SEEK_SET);
226 if (read(ResourceFd, &size_shift, sizeof(size_shift)) !=
229 printf("FindResourceByName (%s) bad block size !\n", resource_name);
236 typeinfo.type_id = 0xffff;
237 while (typeinfo.type_id != 0)
239 if (read(ResourceFd, &typeinfo, sizeof(typeinfo)) !=
242 printf("FindResourceByName (%s) bad typeinfo size !\n", resource_name);
245 #ifdef DEBUG_RESOURCE
246 printf("FindResourceByName typeinfo.type_id=%X type_id=%X\n",
247 typeinfo.type_id, type_id);
249 if (typeinfo.type_id == 0) break;
250 if (typeinfo.type_id == type_id || type_id == -1)
252 for (i = 0; i < typeinfo.count; i++)
254 if (read(ResourceFd, &nameinfo, sizeof(nameinfo)) !=
257 printf("FindResourceByName (%s) bad nameinfo size !\n", resource_name);
261 if (nameinfo.id & 0x8000)
264 old_pos = lseek(ResourceFd, 0, SEEK_CUR);
265 new_pos = rtoff + nameinfo.id;
266 lseek(ResourceFd, new_pos, SEEK_SET);
267 read(ResourceFd, &nbytes, 1);
268 read(ResourceFd, name, nbytes);
269 lseek(ResourceFd, old_pos, SEEK_SET);
271 #ifdef DEBUG_RESOURCE
272 printf("FindResourceByName type_id=%X name='%s' resource_name='%s'\n",
273 typeinfo.type_id, name, resource_name);
275 if (strcasecmp(name, resource_name) == 0)
277 memcpy(result_p, &nameinfo, sizeof(nameinfo));
283 lseek(ResourceFd, (typeinfo.count * sizeof(nameinfo)), SEEK_CUR);
290 /**********************************************************************
291 * LoadIcon [USER.174]
293 HICON LoadIcon(HANDLE instance, LPSTR icon_name)
298 ICONDESCRIP *lpicodesc;
302 BITMAPINFOHEADER *bih;
308 int i, j, image_size;
309 #ifdef DEBUG_RESOURCE
310 printf("LoadIcon: instance = %04x, name = %08x\n",
311 instance, icon_name);
314 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
315 if (instance == (HANDLE)NULL) instance = hSysRes;
316 rsc_mem = RSC_LoadResource(instance, icon_name, NE_RSCTYPE_GROUP_ICON,
318 if (rsc_mem == (HANDLE)NULL) {
319 printf("LoadIcon / Icon %04X not Found !\n", icon_name);
320 ReleaseDC(GetDesktopWindow(), hdc);
323 lp = (WORD *)GlobalLock(rsc_mem);
326 ReleaseDC(GetDesktopWindow(), hdc);
329 lpicodesc = (ICONDESCRIP *)(lp + 3);
330 hIcon = GlobalAlloc(GMEM_MOVEABLE, sizeof(ICONALLOC) + 1024);
331 if (hIcon == (HICON)NULL) {
333 ReleaseDC(GetDesktopWindow(), hdc);
336 printf("LoadIcon Alloc hIcon=%X\n", hIcon);
337 lpico = (ICONALLOC *)GlobalLock(hIcon);
338 lpico->descriptor = *lpicodesc;
339 width = lpicodesc->Width;
340 height = lpicodesc->Height;
341 GlobalUnlock(rsc_mem);
343 rsc_mem = RSC_LoadResource(instance,
344 MAKEINTRESOURCE(lpicodesc->icoDIBOffset),
345 NE_RSCTYPE_ICON, &image_size);
346 if (rsc_mem == (HANDLE)NULL) {
347 printf("LoadIcon / Icon %04X Bitmaps not Found !\n", icon_name);
348 ReleaseDC(GetDesktopWindow(), hdc);
351 lp = (WORD *)GlobalLock(rsc_mem);
354 ReleaseDC(GetDesktopWindow(), hdc);
357 bmi = (BITMAPINFO *)lp;
358 bih = (BITMAPINFOHEADER *)lp;
359 rgbq = &bmi->bmiColors[0];
360 bih->biHeight = bih->biHeight / 2;
362 printf("LoadIcon / image_size=%d width=%d height=%d bih->biBitCount=%d bih->biSizeImage=%ld\n",
363 image_size, width, height, bih->biBitCount, bih->biSizeImage);
365 if (bih->biSize == sizeof(BITMAPINFOHEADER))
366 lpico->hBitmap = ConvertInfoBitmap(hdc, (BITMAPINFO *)bih);
370 bih->biClrUsed = bih->biClrImportant = 2;
371 rgbq[0].rgbBlue = 0xFF;
372 rgbq[0].rgbGreen = 0xFF;
373 rgbq[0].rgbRed = 0xFF;
374 rgbq[0].rgbReserved = 0x00;
375 rgbq[1].rgbBlue = 0x00;
376 rgbq[1].rgbGreen = 0x00;
377 rgbq[1].rgbRed = 0x00;
378 rgbq[1].rgbReserved = 0x00;
379 lpico->hBitMask = CreateDIBitmap(hdc, bih, CBM_INIT,
380 (LPSTR)lp + bih->biSizeImage - sizeof(BITMAPINFOHEADER) / 2 - 4,
381 (BITMAPINFO *)bih, DIB_RGB_COLORS );
382 GlobalUnlock(rsc_mem);
384 hMemDC = CreateCompatibleDC(hdc);
385 hMemDC2 = CreateCompatibleDC(hdc);
386 SelectObject(hMemDC, lpico->hBitmap);
387 SelectObject(hMemDC2, lpico->hBitMask);
388 BitBlt(hMemDC, 0, 0, bih->biWidth, bih->biHeight, hMemDC2, 0, 0, SRCINVERT);
391 ReleaseDC(GetDesktopWindow(), hdc);
397 /**********************************************************************
398 * DestroyIcon [USER.457]
400 BOOL DestroyIcon(HICON hIcon)
403 if (hIcon == (HICON)NULL) return FALSE;
404 lpico = (ICONALLOC *)GlobalLock(hIcon);
405 if (lpico->hBitmap != (HBITMAP)NULL) DeleteObject(lpico->hBitmap);
411 /**********************************************************************
412 * LoadAccelerators [USER.177]
414 HANDLE LoadAccelerators(HANDLE instance, LPSTR lpTableName)
419 ACCELHEADER *lpAccelTbl;
422 if (((LONG)lpTableName & 0xFFFF0000L) == 0L)
423 printf("LoadAccelerators: instance = %04X, name = %08X\n",
424 instance, lpTableName);
426 printf("LoadAccelerators: instance = %04X, name = '%s'\n",
427 instance, lpTableName);
429 if (instance == (HANDLE)NULL) instance = hSysRes;
430 rsc_mem = RSC_LoadResource(instance, lpTableName, NE_RSCTYPE_ACCELERATOR,
432 if (rsc_mem == (HANDLE)NULL) {
433 printf("LoadAccelerators / AccelTable %04X not Found !\n", lpTableName);
436 lp = (BYTE *)GlobalLock(rsc_mem);
442 printf("LoadAccelerators / image_size=%d\n", image_size);
444 hAccel = GlobalAlloc(GMEM_MOVEABLE,
445 sizeof(ACCELHEADER) + sizeof(ACCELENTRY) + image_size);
446 lpAccelTbl = (LPACCELHEADER)GlobalLock(hAccel);
447 lpAccelTbl->wCount = 0;
449 lpAccelTbl->tbl[i].type = *(lp++);
450 lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
452 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
454 if (lpAccelTbl->tbl[i].wEvent == 0) break;
456 printf("Accelerator #%u / event=%04X id=%04X type=%02X \n",
457 i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval,
458 lpAccelTbl->tbl[i].type);
460 lpAccelTbl->wCount++;
462 GlobalUnlock(hAccel);
463 GlobalUnlock(rsc_mem);
468 /**********************************************************************
469 * TranslateAccelerator [USER.178]
471 int TranslateAccelerator(HWND hWnd, HANDLE hAccel, LPMSG msg)
473 ACCELHEADER *lpAccelTbl;
475 if (hAccel == 0 || msg == NULL) return 0;
476 if (msg->message != WM_KEYDOWN &&
477 msg->message != WM_KEYUP &&
478 msg->message != WM_CHAR) return 0;
480 printf("TranslateAccelerators hAccel=%04X !\n", hAccel);
482 lpAccelTbl = (LPACCELHEADER)GlobalLock(hAccel);
483 for (i = 0; i < lpAccelTbl->wCount; i++) {
484 /* if (lpAccelTbl->tbl[i].type & SHIFT_ACCEL) { */
485 /* if (lpAccelTbl->tbl[i].type & CONTROL_ACCEL) { */
486 if (lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) {
487 if (msg->wParam == lpAccelTbl->tbl[i].wEvent &&
488 msg->message == WM_KEYDOWN) {
489 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
492 if (msg->message == WM_KEYUP) return 1;
495 if (msg->wParam == lpAccelTbl->tbl[i].wEvent &&
496 msg->message == WM_CHAR) {
497 SendMessage(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
502 GlobalUnlock(hAccel);
506 /**********************************************************************
507 * FindResource [KERNEL.60]
509 HANDLE FindResource(HANDLE instance, LPSTR resource_name, LPSTR type_name)
517 #ifdef DEBUG_RESOURCE
518 printf("FindResource hInst=%04X typename=%08X resname=%08X\n",
519 instance, type_name, resource_name);
521 if (OpenResourceFile(instance) < 0)
524 rh = GlobalAlloc(GMEM_MOVEABLE, sizeof(*r));
527 r = (RESOURCE *)GlobalLock(rh);
534 if (((int) resource_name & 0xffff0000) == 0)
536 r->size_shift = FindResourceByNumber(&r->nameinfo, (int)type_name,
537 (int) resource_name | 0x8000);
541 r->size_shift = FindResourceByName(&r->nameinfo, (int)type_name,
545 if (r->size_shift == -1)
554 /**********************************************************************
555 * LoadResource [KERNEL.61]
557 HANDLE LoadResource(HANDLE instance, HANDLE hResInfo)
567 if (OpenResourceFile(instance) < 0)
570 r = (RESOURCE *)GlobalLock(hResInfo);
574 image_size = r->nameinfo.length << r->size_shift;
576 h = r->rsc_mem = GlobalAlloc(GMEM_MOVEABLE, image_size);
577 image = GlobalLock(h);
579 lseek(ResourceFd, ((int) r->nameinfo.offset << r->size_shift), SEEK_SET);
581 if (image == NULL || read(ResourceFd, image, image_size) != image_size)
584 GlobalUnlock(hResInfo);
589 GlobalUnlock(hResInfo);
593 /**********************************************************************
594 * LockResource [KERNEL.62]
596 LPSTR LockResource(HANDLE hResData)
598 return GlobalLock(hResData);
601 /**********************************************************************
602 * FreeResource [KERNEL.63]
604 HANDLE FreeResource(HANDLE hResData)
608 for (r = rp = Top; r != NULL; r = r->next)
610 if (r->info_mem == hResData)
617 GlobalFree(r->rsc_mem);
618 GlobalFree(r->info_mem);
626 /**********************************************************************
627 * AccessResource [KERNEL.64]
629 int AccessResource(HANDLE instance, HANDLE hResInfo)
632 #ifdef DEBUG_RESOURCE
633 printf("AccessResource(%04X, %04X);\n", instance, hResInfo);
636 resfile = OpenResourceFile(instance);
644 /**********************************************************************
648 RSC_LoadResource(int instance, char *rsc_name, int type, int *image_size_ret)
650 struct resource_nameinfo_s nameinfo;
663 else if (OpenResourceFile(instance) < 0)
667 * Get resource by ordinal
669 if (((int) rsc_name & 0xffff0000) == 0)
671 size_shift = FindResourceByNumber(&nameinfo, type,
672 (int) rsc_name | 0x8000);
675 * Get resource by name
679 size_shift = FindResourceByName(&nameinfo, type, rsc_name);
681 if (size_shift == -1) {
682 printf("RSC_LoadResource / Resource '%X' not Found !\n", rsc_name);
688 lseek(ResourceFd, ((int) nameinfo.offset << size_shift), SEEK_SET);
690 image_size = nameinfo.length << size_shift;
691 if (image_size_ret != NULL)
692 *image_size_ret = image_size;
693 hmem = GlobalAlloc(GMEM_MOVEABLE, image_size);
694 image = GlobalLock(hmem);
695 if (image == NULL || read(ResourceFd, image, image_size) != image_size)
705 /**********************************************************************
709 LoadString(HANDLE instance, WORD resource_id, LPSTR buffer, int buflen)
717 #ifdef DEBUG_RESOURCE
718 printf("LoadString: instance = %04x, id = %d, "
719 "buffer = %08x, length = %d\n",
720 instance, resource_id, buffer, buflen);
723 hmem = RSC_LoadResource(instance, (char *) ((resource_id >> 4) + 1),
724 NE_RSCTYPE_STRING, &rsc_size);
728 p = GlobalLock(hmem);
729 string_num = resource_id & 0x000f;
730 for (i = 0; i < string_num; i++)
733 i = MIN(buflen - 1, *p);
734 memcpy(buffer, p + 1, i);
739 #ifdef DEBUG_RESOURCE
740 printf(" '%s'\n", buffer);
745 /**********************************************************************
749 RSC_LoadMenu(HANDLE instance, LPSTR menu_name)
751 return RSC_LoadResource(instance, menu_name, NE_RSCTYPE_MENU, NULL);
754 /**********************************************************************
758 LoadBitmap(HANDLE instance, LPSTR bmp_name)
766 #ifdef DEBUG_RESOURCE
767 printf("LoadBitmap: instance = %04x, name = %08x\n",
770 if (instance == (HANDLE)NULL) instance = hSysRes;
771 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
773 rsc_mem = RSC_LoadResource(instance, bmp_name, NE_RSCTYPE_BITMAP,
775 if (rsc_mem == (HANDLE)NULL) {
776 printf("LoadBitmap / BitMap %04X not Found !\n", bmp_name);
779 lp = (long *) GlobalLock(rsc_mem);
785 if (*lp == sizeof(BITMAPCOREHEADER))
786 hbitmap = ConvertCoreBitmap( hdc, (BITMAPCOREHEADER *) lp );
787 else if (*lp == sizeof(BITMAPINFOHEADER))
788 hbitmap = ConvertInfoBitmap( hdc, (BITMAPINFO *) lp );