winmm/tests: Fix failure on Win9x and WinMe.
[wine/multimedia.git] / programs / winemenubuilder / winemenubuilder.c
blob90c613af33d74597424c244a7594c311602293b6
1 /*
2 * Helper program to build unix menu entries
4 * Copyright 1997 Marcus Meissner
5 * Copyright 1998 Juergen Schmied
6 * Copyright 2003 Mike McCormack for CodeWeavers
7 * Copyright 2004 Dmitry Timoshkov
8 * Copyright 2005 Bill Medland
9 * Copyright 2008 Damjan Jovanovic
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * This program is used to replicate the Windows desktop and start menu
27 * into the native desktop's copies. Desktop entries are merged directly
28 * into the native desktop. The Windows Start Menu corresponds to a Wine
29 * entry within the native "start" menu and replicates the whole tree
30 * structure of the Windows Start Menu. Currently it does not differentiate
31 * between the user's desktop/start menu and the "All Users" copies.
33 * This program will read a Windows shortcut file using the IShellLink
34 * interface, then invoke wineshelllink with the appropriate arguments
35 * to create a KDE/Gnome menu entry for the shortcut.
37 * winemenubuilder [ -w ] <shortcut.lnk>
39 * If the -w parameter is passed, and the shortcut cannot be created,
40 * this program will wait for the parent process to finish and then try
41 * again. This covers the case when a ShortCut is created before the
42 * executable containing its icon.
44 * TODO
45 * Handle data lnk files. There is no icon in the file; the icon is in
46 * the handler for the file type (or pointed to by the lnk file). Also it
47 * might be better to use a native handler (e.g. a native acroread for pdf
48 * files).
49 * Differentiate between the user's entries and the "All Users" entries.
50 * If it is possible to add the desktop files to the native system's
51 * shared location for an "All Users" entry then do so. As a suggestion the
52 * shared menu Wine base could be writable to the wine group, or a wineadm
53 * group.
57 #include "config.h"
58 #include "wine/port.h"
60 #include <ctype.h>
61 #include <stdio.h>
62 #include <string.h>
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66 #include <errno.h>
67 #include <stdarg.h>
69 #define COBJMACROS
71 #include <windows.h>
72 #include <shlobj.h>
73 #include <objidl.h>
74 #include <shlguid.h>
75 #include <appmgmt.h>
76 #include <tlhelp32.h>
78 #include "wine/unicode.h"
79 #include "wine/debug.h"
80 #include "wine/library.h"
81 #include "wine.xpm"
83 #ifdef HAVE_PNG_H
84 #undef FAR
85 #include <png.h>
86 #endif
88 WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
90 #define in_desktop_dir(csidl) ((csidl)==CSIDL_DESKTOPDIRECTORY || \
91 (csidl)==CSIDL_COMMON_DESKTOPDIRECTORY)
92 #define in_startmenu(csidl) ((csidl)==CSIDL_STARTMENU || \
93 (csidl)==CSIDL_COMMON_STARTMENU)
95 /* link file formats */
97 #include "pshpack1.h"
99 typedef struct
101 BYTE bWidth;
102 BYTE bHeight;
103 BYTE bColorCount;
104 BYTE bReserved;
105 WORD wPlanes;
106 WORD wBitCount;
107 DWORD dwBytesInRes;
108 WORD nID;
109 } GRPICONDIRENTRY;
111 typedef struct
113 WORD idReserved;
114 WORD idType;
115 WORD idCount;
116 GRPICONDIRENTRY idEntries[1];
117 } GRPICONDIR;
119 typedef struct
121 BYTE bWidth;
122 BYTE bHeight;
123 BYTE bColorCount;
124 BYTE bReserved;
125 WORD wPlanes;
126 WORD wBitCount;
127 DWORD dwBytesInRes;
128 DWORD dwImageOffset;
129 } ICONDIRENTRY;
131 typedef struct
133 WORD idReserved;
134 WORD idType;
135 WORD idCount;
136 } ICONDIR;
139 #include "poppack.h"
141 typedef struct
143 HRSRC *pResInfo;
144 int nIndex;
145 } ENUMRESSTRUCT;
147 static char *xdg_config_dir;
148 static char *xdg_data_dir;
150 /* Icon extraction routines
152 * FIXME: should use PrivateExtractIcons and friends
153 * FIXME: should not use stdio
156 #define MASK(x,y) (pAND[(x) / 8 + (nHeight - (y) - 1) * nANDWidthBytes] & (1 << (7 - (x) % 8)))
158 /* PNG-specific code */
159 #ifdef SONAME_LIBPNG
161 static void *libpng_handle;
162 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
163 MAKE_FUNCPTR(png_create_info_struct);
164 MAKE_FUNCPTR(png_create_write_struct);
165 MAKE_FUNCPTR(png_destroy_write_struct);
166 MAKE_FUNCPTR(png_init_io);
167 MAKE_FUNCPTR(png_set_bgr);
168 MAKE_FUNCPTR(png_set_text);
169 MAKE_FUNCPTR(png_set_IHDR);
170 MAKE_FUNCPTR(png_write_end);
171 MAKE_FUNCPTR(png_write_info);
172 MAKE_FUNCPTR(png_write_row);
173 #undef MAKE_FUNCPTR
175 static void *load_libpng(void)
177 if ((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL)
179 #define LOAD_FUNCPTR(f) \
180 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
181 libpng_handle = NULL; \
182 return NULL; \
184 LOAD_FUNCPTR(png_create_info_struct);
185 LOAD_FUNCPTR(png_create_write_struct);
186 LOAD_FUNCPTR(png_destroy_write_struct);
187 LOAD_FUNCPTR(png_init_io);
188 LOAD_FUNCPTR(png_set_bgr);
189 LOAD_FUNCPTR(png_set_IHDR);
190 LOAD_FUNCPTR(png_set_text);
191 LOAD_FUNCPTR(png_write_end);
192 LOAD_FUNCPTR(png_write_info);
193 LOAD_FUNCPTR(png_write_row);
194 #undef LOAD_FUNCPTR
196 return libpng_handle;
199 static BOOL SaveIconResAsPNG(const BITMAPINFO *pIcon, const char *png_filename, LPCWSTR commentW)
201 static const char comment_key[] = "Created from";
202 FILE *fp;
203 png_structp png_ptr;
204 png_infop info_ptr;
205 png_text comment;
206 int nXORWidthBytes, nANDWidthBytes, color_type = 0, i, j;
207 BYTE *row, *copy = NULL;
208 const BYTE *pXOR, *pAND = NULL;
209 int nWidth = pIcon->bmiHeader.biWidth;
210 int nHeight = pIcon->bmiHeader.biHeight;
211 int nBpp = pIcon->bmiHeader.biBitCount;
213 switch (nBpp)
215 case 32:
216 color_type |= PNG_COLOR_MASK_ALPHA;
217 /* fall through */
218 case 24:
219 color_type |= PNG_COLOR_MASK_COLOR;
220 break;
221 default:
222 return FALSE;
225 if (!libpng_handle && !load_libpng())
227 WINE_WARN("Unable to load libpng\n");
228 return FALSE;
231 if (!(fp = fopen(png_filename, "w")))
233 WINE_ERR("unable to open '%s' for writing: %s\n", png_filename, strerror(errno));
234 return FALSE;
237 nXORWidthBytes = 4 * ((nWidth * nBpp + 31) / 32);
238 nANDWidthBytes = 4 * ((nWidth + 31 ) / 32);
239 pXOR = (const BYTE*) pIcon + sizeof(BITMAPINFOHEADER) + pIcon->bmiHeader.biClrUsed * sizeof(RGBQUAD);
240 if (nHeight > nWidth)
242 nHeight /= 2;
243 pAND = pXOR + nHeight * nXORWidthBytes;
246 /* Apply mask if present */
247 if (pAND)
249 RGBQUAD bgColor;
251 /* copy bytes before modifying them */
252 copy = HeapAlloc( GetProcessHeap(), 0, nHeight * nXORWidthBytes );
253 memcpy( copy, pXOR, nHeight * nXORWidthBytes );
254 pXOR = copy;
256 /* image and mask are upside down reversed */
257 row = copy + (nHeight - 1) * nXORWidthBytes;
259 /* top left corner */
260 bgColor.rgbRed = row[0];
261 bgColor.rgbGreen = row[1];
262 bgColor.rgbBlue = row[2];
263 bgColor.rgbReserved = 0;
265 for (i = 0; i < nHeight; i++, row -= nXORWidthBytes)
266 for (j = 0; j < nWidth; j++, row += nBpp >> 3)
267 if (MASK(j, i))
269 RGBQUAD *pixel = (RGBQUAD *)row;
270 pixel->rgbBlue = bgColor.rgbBlue;
271 pixel->rgbGreen = bgColor.rgbGreen;
272 pixel->rgbRed = bgColor.rgbRed;
273 if (nBpp == 32)
274 pixel->rgbReserved = bgColor.rgbReserved;
278 comment.text = NULL;
280 if (!(png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) ||
281 !(info_ptr = ppng_create_info_struct(png_ptr)))
282 goto error;
284 if (setjmp(png_jmpbuf(png_ptr)))
286 /* All future errors jump here */
287 WINE_ERR("png error\n");
288 goto error;
291 ppng_init_io(png_ptr, fp);
292 ppng_set_IHDR(png_ptr, info_ptr, nWidth, nHeight, 8,
293 color_type,
294 PNG_INTERLACE_NONE,
295 PNG_COMPRESSION_TYPE_DEFAULT,
296 PNG_FILTER_TYPE_DEFAULT);
298 /* Set comment */
299 comment.compression = PNG_TEXT_COMPRESSION_NONE;
300 comment.key = (png_charp)comment_key;
301 i = WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, NULL, 0, NULL, NULL);
302 comment.text = HeapAlloc(GetProcessHeap(), 0, i);
303 WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, comment.text, i, NULL, NULL);
304 comment.text_length = i - 1;
305 ppng_set_text(png_ptr, info_ptr, &comment, 1);
308 ppng_write_info(png_ptr, info_ptr);
309 ppng_set_bgr(png_ptr);
310 for (i = nHeight - 1; i >= 0 ; i--)
311 ppng_write_row(png_ptr, (png_bytep)pXOR + nXORWidthBytes * i);
312 ppng_write_end(png_ptr, info_ptr);
314 ppng_destroy_write_struct(&png_ptr, &info_ptr);
315 if (png_ptr) ppng_destroy_write_struct(&png_ptr, NULL);
316 fclose(fp);
317 HeapFree(GetProcessHeap(), 0, copy);
318 HeapFree(GetProcessHeap(), 0, comment.text);
319 return TRUE;
321 error:
322 if (png_ptr) ppng_destroy_write_struct(&png_ptr, NULL);
323 fclose(fp);
324 unlink(png_filename);
325 HeapFree(GetProcessHeap(), 0, copy);
326 HeapFree(GetProcessHeap(), 0, comment.text);
327 return FALSE;
329 #endif /* SONAME_LIBPNG */
331 static BOOL SaveIconResAsXPM(const BITMAPINFO *pIcon, const char *szXPMFileName, LPCWSTR commentW)
333 FILE *fXPMFile;
334 int nHeight;
335 int nXORWidthBytes;
336 int nANDWidthBytes;
337 BOOL b8BitColors;
338 int nColors;
339 const BYTE *pXOR;
340 const BYTE *pAND;
341 BOOL aColorUsed[256] = {0};
342 int nColorsUsed = 0;
343 int i,j;
344 char *comment;
346 if (!((pIcon->bmiHeader.biBitCount == 4) || (pIcon->bmiHeader.biBitCount == 8)))
348 WINE_FIXME("Unsupported color depth %d-bit\n", pIcon->bmiHeader.biBitCount);
349 return FALSE;
352 if (!(fXPMFile = fopen(szXPMFileName, "w")))
354 WINE_TRACE("unable to open '%s' for writing: %s\n", szXPMFileName, strerror(errno));
355 return FALSE;
358 i = WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, NULL, 0, NULL, NULL);
359 comment = HeapAlloc(GetProcessHeap(), 0, i);
360 WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, comment, i, NULL, NULL);
362 nHeight = pIcon->bmiHeader.biHeight / 2;
363 nXORWidthBytes = 4 * ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount / 32)
364 + ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount % 32) > 0));
365 nANDWidthBytes = 4 * ((pIcon->bmiHeader.biWidth / 32)
366 + ((pIcon->bmiHeader.biWidth % 32) > 0));
367 b8BitColors = pIcon->bmiHeader.biBitCount == 8;
368 nColors = pIcon->bmiHeader.biClrUsed ? pIcon->bmiHeader.biClrUsed
369 : 1 << pIcon->bmiHeader.biBitCount;
370 pXOR = (const BYTE*) pIcon + sizeof (BITMAPINFOHEADER) + (nColors * sizeof (RGBQUAD));
371 pAND = pXOR + nHeight * nXORWidthBytes;
373 #define COLOR(x,y) (b8BitColors ? pXOR[(x) + (nHeight - (y) - 1) * nXORWidthBytes] : (x) % 2 ? pXOR[(x) / 2 + (nHeight - (y) - 1) * nXORWidthBytes] & 0xF : (pXOR[(x) / 2 + (nHeight - (y) - 1) * nXORWidthBytes] & 0xF0) >> 4)
375 for (i = 0; i < nHeight; i++) {
376 for (j = 0; j < pIcon->bmiHeader.biWidth; j++) {
377 if (!aColorUsed[COLOR(j,i)] && !MASK(j,i))
379 aColorUsed[COLOR(j,i)] = TRUE;
380 nColorsUsed++;
385 if (fprintf(fXPMFile, "/* XPM */\n/* %s */\nstatic char *icon[] = {\n", comment) <= 0)
386 goto error;
387 if (fprintf(fXPMFile, "\"%d %d %d %d\",\n",
388 (int) pIcon->bmiHeader.biWidth, nHeight, nColorsUsed + 1, 2) <=0)
389 goto error;
391 for (i = 0; i < nColors; i++) {
392 if (aColorUsed[i])
393 if (fprintf(fXPMFile, "\"%.2X c #%.2X%.2X%.2X\",\n", i, pIcon->bmiColors[i].rgbRed,
394 pIcon->bmiColors[i].rgbGreen, pIcon->bmiColors[i].rgbBlue) <= 0)
395 goto error;
397 if (fprintf(fXPMFile, "\" c None\"") <= 0)
398 goto error;
400 for (i = 0; i < nHeight; i++)
402 if (fprintf(fXPMFile, ",\n\"") <= 0)
403 goto error;
404 for (j = 0; j < pIcon->bmiHeader.biWidth; j++)
406 if MASK(j,i)
408 if (fprintf(fXPMFile, " ") <= 0)
409 goto error;
411 else
412 if (fprintf(fXPMFile, "%.2X", COLOR(j,i)) <= 0)
413 goto error;
415 if (fprintf(fXPMFile, "\"") <= 0)
416 goto error;
418 if (fprintf(fXPMFile, "};\n") <= 0)
419 goto error;
421 #undef MASK
422 #undef COLOR
424 HeapFree(GetProcessHeap(), 0, comment);
425 fclose(fXPMFile);
426 return TRUE;
428 error:
429 HeapFree(GetProcessHeap(), 0, comment);
430 fclose(fXPMFile);
431 unlink( szXPMFileName );
432 return FALSE;
435 static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
437 ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
439 if (!sEnumRes->nIndex--)
441 *sEnumRes->pResInfo = FindResourceW(hModule, lpszName, (LPCWSTR)RT_GROUP_ICON);
442 return FALSE;
444 else
445 return TRUE;
448 static BOOL extract_icon32(LPCWSTR szFileName, int nIndex, char *szXPMFileName)
450 HMODULE hModule;
451 HRSRC hResInfo;
452 LPCWSTR lpName = NULL;
453 HGLOBAL hResData;
454 GRPICONDIR *pIconDir;
455 BITMAPINFO *pIcon;
456 ENUMRESSTRUCT sEnumRes;
457 int nMax = 0;
458 int nMaxBits = 0;
459 int i;
460 BOOL ret = FALSE;
462 hModule = LoadLibraryExW(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
463 if (!hModule)
465 WINE_WARN("LoadLibraryExW (%s) failed, error %d\n",
466 wine_dbgstr_w(szFileName), GetLastError());
467 return FALSE;
470 if (nIndex < 0)
472 hResInfo = FindResourceW(hModule, MAKEINTRESOURCEW(-nIndex), (LPCWSTR)RT_GROUP_ICON);
473 WINE_TRACE("FindResourceW (%s) called, return %p, error %d\n",
474 wine_dbgstr_w(szFileName), hResInfo, GetLastError());
476 else
478 hResInfo=NULL;
479 sEnumRes.pResInfo = &hResInfo;
480 sEnumRes.nIndex = nIndex;
481 if (!EnumResourceNamesW(hModule, (LPCWSTR)RT_GROUP_ICON,
482 EnumResNameProc, (LONG_PTR)&sEnumRes) &&
483 sEnumRes.nIndex != 0)
485 WINE_TRACE("EnumResourceNamesW failed, error %d\n", GetLastError());
489 if (hResInfo)
491 if ((hResData = LoadResource(hModule, hResInfo)))
493 if ((pIconDir = LockResource(hResData)))
495 for (i = 0; i < pIconDir->idCount; i++)
497 if ((pIconDir->idEntries[i].wBitCount >= nMaxBits) && (pIconDir->idEntries[i].wBitCount <= 8))
499 nMaxBits = pIconDir->idEntries[i].wBitCount;
501 if ((pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth) >= nMax)
503 lpName = MAKEINTRESOURCEW(pIconDir->idEntries[i].nID);
504 nMax = pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth;
510 FreeResource(hResData);
513 else
515 WINE_WARN("found no icon\n");
516 FreeLibrary(hModule);
517 return FALSE;
520 if ((hResInfo = FindResourceW(hModule, lpName, (LPCWSTR)RT_ICON)))
522 if ((hResData = LoadResource(hModule, hResInfo)))
524 if ((pIcon = LockResource(hResData)))
526 #ifdef SONAME_LIBPNG
527 if (SaveIconResAsPNG(pIcon, szXPMFileName, szFileName))
528 ret = TRUE;
529 else
530 #endif
532 memcpy(szXPMFileName + strlen(szXPMFileName) - 3, "xpm", 3);
533 if (SaveIconResAsXPM(pIcon, szXPMFileName, szFileName))
534 ret = TRUE;
538 FreeResource(hResData);
542 FreeLibrary(hModule);
543 return ret;
546 static BOOL ExtractFromEXEDLL(LPCWSTR szFileName, int nIndex, char *szXPMFileName)
548 if (!extract_icon32(szFileName, nIndex, szXPMFileName) /*&&
549 !extract_icon16(szFileName, szXPMFileName)*/)
550 return FALSE;
551 return TRUE;
554 static int ExtractFromICO(LPCWSTR szFileName, char *szXPMFileName)
556 FILE *fICOFile = NULL;
557 ICONDIR iconDir;
558 ICONDIRENTRY *pIconDirEntry = NULL;
559 int nMax = 0, nMaxBits = 0;
560 int nIndex = 0;
561 void *pIcon = NULL;
562 int i;
563 char *filename = NULL;
565 filename = wine_get_unix_file_name(szFileName);
566 if (!(fICOFile = fopen(filename, "r")))
568 WINE_TRACE("unable to open '%s' for reading: %s\n", filename, strerror(errno));
569 goto error;
572 if (fread(&iconDir, sizeof (ICONDIR), 1, fICOFile) != 1 ||
573 (iconDir.idReserved != 0) || (iconDir.idType != 1))
575 WINE_WARN("Invalid ico file format\n");
576 goto error;
579 if ((pIconDirEntry = HeapAlloc(GetProcessHeap(), 0, iconDir.idCount * sizeof (ICONDIRENTRY))) == NULL)
580 goto error;
581 if (fread(pIconDirEntry, sizeof (ICONDIRENTRY), iconDir.idCount, fICOFile) != iconDir.idCount)
582 goto error;
584 for (i = 0; i < iconDir.idCount; i++)
586 WINE_TRACE("[%d]: %d x %d @ %d\n", i, pIconDirEntry[i].bWidth, pIconDirEntry[i].bHeight, pIconDirEntry[i].wBitCount);
587 if (pIconDirEntry[i].wBitCount >= nMaxBits &&
588 (pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth) >= nMax)
590 nIndex = i;
591 nMax = pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth;
592 nMaxBits = pIconDirEntry[i].wBitCount;
595 WINE_TRACE("Selected: %d\n", nIndex);
597 if ((pIcon = HeapAlloc(GetProcessHeap(), 0, pIconDirEntry[nIndex].dwBytesInRes)) == NULL)
598 goto error;
599 if (fseek(fICOFile, pIconDirEntry[nIndex].dwImageOffset, SEEK_SET))
600 goto error;
601 if (fread(pIcon, pIconDirEntry[nIndex].dwBytesInRes, 1, fICOFile) != 1)
602 goto error;
605 /* Prefer PNG over XPM */
606 #ifdef SONAME_LIBPNG
607 if (!SaveIconResAsPNG(pIcon, szXPMFileName, szFileName))
608 #endif
610 memcpy(szXPMFileName + strlen(szXPMFileName) - 3, "xpm", 3);
611 if (!SaveIconResAsXPM(pIcon, szXPMFileName, szFileName))
612 goto error;
615 HeapFree(GetProcessHeap(), 0, pIcon);
616 HeapFree(GetProcessHeap(), 0, pIconDirEntry);
617 fclose(fICOFile);
618 HeapFree(GetProcessHeap(), 0, filename);
619 return 1;
621 error:
622 HeapFree(GetProcessHeap(), 0, pIcon);
623 HeapFree(GetProcessHeap(), 0, pIconDirEntry);
624 if (fICOFile) fclose(fICOFile);
625 HeapFree(GetProcessHeap(), 0, filename);
626 return 0;
629 static BOOL create_default_icon( const char *filename, const char* comment )
631 FILE *fXPM;
632 unsigned int i;
634 if (!(fXPM = fopen(filename, "w"))) return FALSE;
635 if (fprintf(fXPM, "/* XPM */\n/* %s */\nstatic char * icon[] = {", comment) <= 0)
636 goto error;
637 for (i = 0; i < sizeof(wine_xpm)/sizeof(wine_xpm[0]); i++) {
638 if (fprintf( fXPM, "\n\"%s\",", wine_xpm[i]) <= 0)
639 goto error;
641 if (fprintf( fXPM, "};\n" ) <=0)
642 goto error;
643 fclose( fXPM );
644 return TRUE;
645 error:
646 fclose( fXPM );
647 unlink( filename );
648 return FALSE;
652 static unsigned short crc16(const char* string)
654 unsigned short crc = 0;
655 int i, j, xor_poly;
657 for (i = 0; string[i] != 0; i++)
659 char c = string[i];
660 for (j = 0; j < 8; c >>= 1, j++)
662 xor_poly = (c ^ crc) & 1;
663 crc >>= 1;
664 if (xor_poly)
665 crc ^= 0xa001;
668 return crc;
671 static char* heap_printf(const char *format, ...)
673 va_list args;
674 int size = 4096;
675 char *buffer;
676 int n;
678 va_start(args, format);
679 while (1)
681 buffer = HeapAlloc(GetProcessHeap(), 0, size);
682 if (buffer == NULL)
683 break;
684 n = vsnprintf(buffer, size, format, args);
685 if (n == -1)
686 size *= 2;
687 else if (n >= size)
688 size = n + 1;
689 else
690 break;
691 HeapFree(GetProcessHeap(), 0, buffer);
693 va_end(args);
694 return buffer;
697 static BOOL create_directories(char *directory)
699 BOOL ret = TRUE;
700 int i;
702 for (i = 0; directory[i]; i++)
704 if (i > 0 && directory[i] == '/')
706 directory[i] = 0;
707 mkdir(directory, 0777);
708 directory[i] = '/';
711 if (mkdir(directory, 0777) && errno != EEXIST)
712 ret = FALSE;
714 return ret;
717 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
718 static char *extract_icon( LPCWSTR path, int index, BOOL bWait )
720 unsigned short crc;
721 char *iconsdir = NULL, *ico_path = NULL, *ico_name, *xpm_path = NULL;
722 char* s;
723 int n;
725 /* Where should we save the icon? */
726 WINE_TRACE("path=[%s] index=%d\n", wine_dbgstr_w(path), index);
727 iconsdir = heap_printf("%s/icons", xdg_data_dir);
728 if (iconsdir)
730 if (mkdir(iconsdir, 0777) && errno != EEXIST)
732 WINE_WARN("couldn't make icons directory %s\n", wine_dbgstr_a(iconsdir));
733 goto end;
736 else
738 WINE_TRACE("no icon created\n");
739 return NULL;
742 /* Determine the icon base name */
743 n = WideCharToMultiByte(CP_UNIXCP, 0, path, -1, NULL, 0, NULL, NULL);
744 ico_path = HeapAlloc(GetProcessHeap(), 0, n);
745 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, ico_path, n, NULL, NULL);
746 s=ico_name=ico_path;
747 while (*s!='\0') {
748 if (*s=='/' || *s=='\\') {
749 *s='\\';
750 ico_name=s;
751 } else {
752 *s=tolower(*s);
754 s++;
756 if (*ico_name=='\\') *ico_name++='\0';
757 s=strrchr(ico_name,'.');
758 if (s) *s='\0';
760 /* Compute the source-path hash */
761 crc=crc16(ico_path);
763 /* Try to treat the source file as an exe */
764 xpm_path=HeapAlloc(GetProcessHeap(), 0, strlen(iconsdir)+1+4+1+strlen(ico_name)+1+12+1+3);
765 sprintf(xpm_path,"%s/%04x_%s.%d.png",iconsdir,crc,ico_name,index);
766 if (ExtractFromEXEDLL( path, index, xpm_path ))
767 goto end;
769 /* Must be something else, ignore the index in that case */
770 sprintf(xpm_path,"%s/%04x_%s.png",iconsdir,crc,ico_name);
771 if (ExtractFromICO( path, xpm_path))
772 goto end;
773 if (!bWait)
775 sprintf(xpm_path,"%s/%04x_%s.xpm",iconsdir,crc,ico_name);
776 if (create_default_icon( xpm_path, ico_path ))
777 goto end;
780 HeapFree( GetProcessHeap(), 0, xpm_path );
781 xpm_path=NULL;
783 end:
784 HeapFree(GetProcessHeap(), 0, iconsdir);
785 HeapFree(GetProcessHeap(), 0, ico_path);
786 return xpm_path;
789 /* This escapes \ in filenames */
790 static LPSTR escape(LPCWSTR arg)
792 LPSTR narg, x;
793 LPCWSTR esc;
794 int len = 0, n;
796 esc = arg;
797 while((esc = strchrW(esc, '\\')))
799 esc++;
800 len++;
803 len += WideCharToMultiByte(CP_UNIXCP, 0, arg, -1, NULL, 0, NULL, NULL);
804 narg = HeapAlloc(GetProcessHeap(), 0, len);
806 x = narg;
807 while (*arg)
809 n = WideCharToMultiByte(CP_UNIXCP, 0, arg, 1, x, len, NULL, NULL);
810 x += n;
811 len -= n;
812 if (*arg == '\\')
813 *x++='\\'; /* escape \ */
814 arg++;
816 *x = 0;
817 return narg;
820 static int fork_and_wait( const char *linker, const char *link_name, const char *path,
821 int desktop, const char *args, const char *icon_name,
822 const char *workdir, const char *description )
824 int pos = 0;
825 const char *argv[20];
826 int retcode;
828 WINE_TRACE( "linker app='%s' link='%s' mode=%s "
829 "path='%s' args='%s' icon='%s' workdir='%s' descr='%s'\n",
830 linker, link_name, desktop ? "desktop" : "menu",
831 path, args, icon_name, workdir, description );
833 argv[pos++] = linker ;
834 argv[pos++] = "--link";
835 argv[pos++] = link_name;
836 argv[pos++] = "--path";
837 argv[pos++] = path;
838 argv[pos++] = desktop ? "--desktop" : "--menu";
839 if (args && strlen(args))
841 argv[pos++] = "--args";
842 argv[pos++] = args;
844 if (icon_name)
846 argv[pos++] = "--icon";
847 argv[pos++] = icon_name;
849 if (workdir && strlen(workdir))
851 argv[pos++] = "--workdir";
852 argv[pos++] = workdir;
854 if (description && strlen(description))
856 argv[pos++] = "--descr";
857 argv[pos++] = description;
859 argv[pos] = NULL;
861 retcode=spawnvp( _P_WAIT, linker, argv );
862 if (retcode!=0)
863 WINE_ERR("%s returned %d\n",linker,retcode);
864 return retcode;
867 /* Return a heap-allocated copy of the unix format difference between the two
868 * Windows-format paths.
869 * locn is the owning location
870 * link is within locn
872 static char *relative_path( LPCWSTR link, LPCWSTR locn )
874 char *unix_locn, *unix_link;
875 char *relative = NULL;
877 unix_locn = wine_get_unix_file_name(locn);
878 unix_link = wine_get_unix_file_name(link);
879 if (unix_locn && unix_link)
881 size_t len_unix_locn, len_unix_link;
882 len_unix_locn = strlen (unix_locn);
883 len_unix_link = strlen (unix_link);
884 if (len_unix_locn < len_unix_link && memcmp (unix_locn, unix_link, len_unix_locn) == 0 && unix_link[len_unix_locn] == '/')
886 size_t len_rel;
887 char *p = strrchr (unix_link + len_unix_locn, '/');
888 p = strrchr (p, '.');
889 if (p)
891 *p = '\0';
892 len_unix_link = p - unix_link;
894 len_rel = len_unix_link - len_unix_locn;
895 relative = HeapAlloc(GetProcessHeap(), 0, len_rel);
896 if (relative)
898 memcpy (relative, unix_link + len_unix_locn + 1, len_rel);
902 if (!relative)
903 WINE_WARN("Could not separate the relative link path of %s in %s\n", wine_dbgstr_w(link), wine_dbgstr_w(locn));
904 HeapFree(GetProcessHeap(), 0, unix_locn);
905 HeapFree(GetProcessHeap(), 0, unix_link);
906 return relative;
909 /***********************************************************************
911 * GetLinkLocation
913 * returns TRUE if successful
914 * *loc will contain CS_DESKTOPDIRECTORY, CS_STARTMENU, CS_STARTUP etc.
915 * *relative will contain the address of a heap-allocated copy of the portion
916 * of the filename that is within the specified location, in unix form
918 static BOOL GetLinkLocation( LPCWSTR linkfile, DWORD *loc, char **relative )
920 WCHAR filename[MAX_PATH], shortfilename[MAX_PATH], buffer[MAX_PATH];
921 DWORD len, i, r, filelen;
922 const DWORD locations[] = {
923 CSIDL_STARTUP, CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU,
924 CSIDL_COMMON_STARTUP, CSIDL_COMMON_DESKTOPDIRECTORY,
925 CSIDL_COMMON_STARTMENU };
927 WINE_TRACE("%s\n", wine_dbgstr_w(linkfile));
928 filelen=GetFullPathNameW( linkfile, MAX_PATH, shortfilename, NULL );
929 if (filelen==0 || filelen>MAX_PATH)
930 return FALSE;
932 WINE_TRACE("%s\n", wine_dbgstr_w(shortfilename));
934 /* the CSLU Toolkit uses a short path name when creating .lnk files;
935 * expand or our hardcoded list won't match.
937 filelen=GetLongPathNameW(shortfilename, filename, MAX_PATH);
938 if (filelen==0 || filelen>MAX_PATH)
939 return FALSE;
941 WINE_TRACE("%s\n", wine_dbgstr_w(filename));
943 for( i=0; i<sizeof(locations)/sizeof(locations[0]); i++ )
945 if (!SHGetSpecialFolderPathW( 0, buffer, locations[i], FALSE ))
946 continue;
948 len = lstrlenW(buffer);
949 if (len >= MAX_PATH)
950 continue; /* We've just trashed memory! Hopefully we are OK */
952 if (len > filelen || filename[len]!='\\')
953 continue;
954 /* do a lstrcmpinW */
955 filename[len] = 0;
956 r = lstrcmpiW( filename, buffer );
957 filename[len] = '\\';
958 if ( r )
959 continue;
961 /* return the remainder of the string and link type */
962 *loc = locations[i];
963 *relative = relative_path (filename, buffer);
964 return (*relative != NULL);
967 return FALSE;
970 /* gets the target path directly or through MSI */
971 static HRESULT get_cmdline( IShellLinkW *sl, LPWSTR szPath, DWORD pathSize,
972 LPWSTR szArgs, DWORD argsSize)
974 IShellLinkDataList *dl = NULL;
975 EXP_DARWIN_LINK *dar = NULL;
976 HRESULT hr;
978 szPath[0] = 0;
979 szArgs[0] = 0;
981 hr = IShellLinkW_GetPath( sl, szPath, pathSize, NULL, SLGP_RAWPATH );
982 if (hr == S_OK && szPath[0])
984 IShellLinkW_GetArguments( sl, szArgs, argsSize );
985 return hr;
988 hr = IShellLinkW_QueryInterface( sl, &IID_IShellLinkDataList, (LPVOID*) &dl );
989 if (FAILED(hr))
990 return hr;
992 hr = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
993 if (SUCCEEDED(hr))
995 WCHAR* szCmdline;
996 DWORD cmdSize;
998 cmdSize=0;
999 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, NULL, &cmdSize );
1000 if (hr == ERROR_SUCCESS)
1002 cmdSize++;
1003 szCmdline = HeapAlloc( GetProcessHeap(), 0, cmdSize*sizeof(WCHAR) );
1004 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, szCmdline, &cmdSize );
1005 WINE_TRACE(" command : %s\n", wine_dbgstr_w(szCmdline));
1006 if (hr == ERROR_SUCCESS)
1008 WCHAR *s, *d;
1009 int bcount, in_quotes;
1011 /* Extract the application path */
1012 bcount=0;
1013 in_quotes=0;
1014 s=szCmdline;
1015 d=szPath;
1016 while (*s)
1018 if ((*s==0x0009 || *s==0x0020) && !in_quotes)
1020 /* skip the remaining spaces */
1021 do {
1022 s++;
1023 } while (*s==0x0009 || *s==0x0020);
1024 break;
1026 else if (*s==0x005c)
1028 /* '\\' */
1029 *d++=*s++;
1030 bcount++;
1032 else if (*s==0x0022)
1034 /* '"' */
1035 if ((bcount & 1)==0)
1037 /* Preceded by an even number of '\', this is
1038 * half that number of '\', plus a quote which
1039 * we erase.
1041 d-=bcount/2;
1042 in_quotes=!in_quotes;
1043 s++;
1045 else
1047 /* Preceded by an odd number of '\', this is
1048 * half that number of '\' followed by a '"'
1050 d=d-bcount/2-1;
1051 *d++='"';
1052 s++;
1054 bcount=0;
1056 else
1058 /* a regular character */
1059 *d++=*s++;
1060 bcount=0;
1062 if ((d-szPath) == pathSize)
1064 /* Keep processing the path till we get to the
1065 * arguments, but 'stand still'
1067 d--;
1070 /* Close the application path */
1071 *d=0;
1073 lstrcpynW(szArgs, s, argsSize);
1075 HeapFree( GetProcessHeap(), 0, szCmdline );
1077 LocalFree( dar );
1080 IShellLinkDataList_Release( dl );
1081 return hr;
1084 static BOOL InvokeShellLinker( IShellLinkW *sl, LPCWSTR link, BOOL bWait )
1086 static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
1087 '\\','s','t','a','r','t','.','e','x','e',0};
1088 char *link_name = NULL, *icon_name = NULL, *work_dir = NULL;
1089 char *escaped_path = NULL, *escaped_args = NULL, *escaped_description = NULL;
1090 WCHAR szTmp[INFOTIPSIZE];
1091 WCHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
1092 WCHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH];
1093 int iIconId = 0, r = -1;
1094 DWORD csidl = -1;
1095 HANDLE hsem = NULL;
1097 if ( !link )
1099 WINE_ERR("Link name is null\n");
1100 return FALSE;
1103 if( !GetLinkLocation( link, &csidl, &link_name ) )
1105 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
1106 return TRUE;
1108 if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
1110 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
1111 return TRUE;
1113 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
1115 szTmp[0] = 0;
1116 IShellLinkW_GetWorkingDirectory( sl, szTmp, MAX_PATH );
1117 ExpandEnvironmentStringsW(szTmp, szWorkDir, MAX_PATH);
1118 WINE_TRACE("workdir : %s\n", wine_dbgstr_w(szWorkDir));
1120 szTmp[0] = 0;
1121 IShellLinkW_GetDescription( sl, szTmp, INFOTIPSIZE );
1122 ExpandEnvironmentStringsW(szTmp, szDescription, INFOTIPSIZE);
1123 WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription));
1125 get_cmdline( sl, szPath, MAX_PATH, szArgs, INFOTIPSIZE);
1126 WINE_TRACE("path : %s\n", wine_dbgstr_w(szPath));
1127 WINE_TRACE("args : %s\n", wine_dbgstr_w(szArgs));
1129 szTmp[0] = 0;
1130 IShellLinkW_GetIconLocation( sl, szTmp, MAX_PATH, &iIconId );
1131 ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
1132 WINE_TRACE("icon file : %s\n", wine_dbgstr_w(szIconPath) );
1134 if( !szPath[0] )
1136 LPITEMIDLIST pidl = NULL;
1137 IShellLinkW_GetIDList( sl, &pidl );
1138 if( pidl && SHGetPathFromIDListW( pidl, szPath ) )
1139 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
1142 /* extract the icon */
1143 if( szIconPath[0] )
1144 icon_name = extract_icon( szIconPath , iIconId, bWait );
1145 else
1146 icon_name = extract_icon( szPath, iIconId, bWait );
1148 /* fail - try once again after parent process exit */
1149 if( !icon_name )
1151 if (bWait)
1153 WINE_WARN("Unable to extract icon, deferring.\n");
1154 goto cleanup;
1156 WINE_ERR("failed to extract icon from %s\n",
1157 wine_dbgstr_w( szIconPath[0] ? szIconPath : szPath ));
1160 /* check the path */
1161 if( szPath[0] )
1163 static const WCHAR exeW[] = {'.','e','x','e',0};
1164 WCHAR *p;
1166 /* check for .exe extension */
1167 if (!(p = strrchrW( szPath, '.' )) ||
1168 strchrW( p, '\\' ) || strchrW( p, '/' ) ||
1169 lstrcmpiW( p, exeW ))
1171 /* Not .exe - use 'start.exe' to launch this file */
1172 p = szArgs + lstrlenW(szPath) + 2;
1173 if (szArgs[0])
1175 p[0] = ' ';
1176 memmove( p+1, szArgs, min( (lstrlenW(szArgs) + 1) * sizeof(szArgs[0]),
1177 sizeof(szArgs) - (p + 1 - szArgs) * sizeof(szArgs[0]) ) );
1179 else
1180 p[0] = 0;
1182 szArgs[0] = '"';
1183 lstrcpyW(szArgs + 1, szPath);
1184 p[-1] = '"';
1186 GetWindowsDirectoryW(szPath, MAX_PATH);
1187 lstrcatW(szPath, startW);
1190 /* convert app working dir */
1191 if (szWorkDir[0])
1192 work_dir = wine_get_unix_file_name( szWorkDir );
1194 else
1196 /* if there's no path... try run the link itself */
1197 lstrcpynW(szArgs, link, MAX_PATH);
1198 GetWindowsDirectoryW(szPath, MAX_PATH);
1199 lstrcatW(szPath, startW);
1202 /* escape the path and parameters */
1203 escaped_path = escape(szPath);
1204 escaped_args = escape(szArgs);
1205 escaped_description = escape(szDescription);
1207 /* running multiple instances of wineshelllink
1208 at the same time may be dangerous */
1209 hsem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
1210 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hsem, FALSE, INFINITE, QS_ALLINPUT ) )
1212 WINE_ERR("failed wait for semaphore\n");
1213 goto cleanup;
1216 r = fork_and_wait("wineshelllink", link_name, escaped_path,
1217 in_desktop_dir(csidl), escaped_args, icon_name,
1218 work_dir ? work_dir : "", escaped_description);
1220 ReleaseSemaphore( hsem, 1, NULL );
1222 cleanup:
1223 if (hsem) CloseHandle( hsem );
1224 HeapFree( GetProcessHeap(), 0, icon_name );
1225 HeapFree( GetProcessHeap(), 0, work_dir );
1226 HeapFree( GetProcessHeap(), 0, link_name );
1227 HeapFree( GetProcessHeap(), 0, escaped_args );
1228 HeapFree( GetProcessHeap(), 0, escaped_path );
1229 HeapFree( GetProcessHeap(), 0, escaped_description );
1231 if (r && !bWait)
1232 WINE_ERR("failed to fork and exec wineshelllink\n" );
1234 return ( r == 0 );
1237 static BOOL WaitForParentProcess( void )
1239 PROCESSENTRY32 procentry;
1240 HANDLE hsnapshot = NULL, hprocess = NULL;
1241 DWORD ourpid = GetCurrentProcessId();
1242 BOOL ret = FALSE, rc;
1244 WINE_TRACE("Waiting for parent process\n");
1245 if ((hsnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )) ==
1246 INVALID_HANDLE_VALUE)
1248 WINE_ERR("CreateToolhelp32Snapshot failed, error %d\n", GetLastError());
1249 goto done;
1252 procentry.dwSize = sizeof(PROCESSENTRY32);
1253 rc = Process32First( hsnapshot, &procentry );
1254 while (rc)
1256 if (procentry.th32ProcessID == ourpid) break;
1257 rc = Process32Next( hsnapshot, &procentry );
1259 if (!rc)
1261 WINE_WARN("Unable to find current process id %d when listing processes\n", ourpid);
1262 goto done;
1265 if ((hprocess = OpenProcess( SYNCHRONIZE, FALSE, procentry.th32ParentProcessID )) ==
1266 NULL)
1268 WINE_WARN("OpenProcess failed pid=%d, error %d\n", procentry.th32ParentProcessID,
1269 GetLastError());
1270 goto done;
1273 if (MsgWaitForMultipleObjects( 1, &hprocess, FALSE, INFINITE, QS_ALLINPUT ) == WAIT_OBJECT_0)
1274 ret = TRUE;
1275 else
1276 WINE_ERR("Unable to wait for parent process, error %d\n", GetLastError());
1278 done:
1279 if (hprocess) CloseHandle( hprocess );
1280 if (hsnapshot) CloseHandle( hsnapshot );
1281 return ret;
1284 static BOOL Process_Link( LPCWSTR linkname, BOOL bWait )
1286 IShellLinkW *sl;
1287 IPersistFile *pf;
1288 HRESULT r;
1289 WCHAR fullname[MAX_PATH];
1290 DWORD len;
1292 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(linkname), bWait);
1294 if( !linkname[0] )
1296 WINE_ERR("link name missing\n");
1297 return 1;
1300 len=GetFullPathNameW( linkname, MAX_PATH, fullname, NULL );
1301 if (len==0 || len>MAX_PATH)
1303 WINE_ERR("couldn't get full path of link file\n");
1304 return 1;
1307 r = CoInitialize( NULL );
1308 if( FAILED( r ) )
1310 WINE_ERR("CoInitialize failed\n");
1311 return 1;
1314 r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1315 &IID_IShellLinkW, (LPVOID *) &sl );
1316 if( FAILED( r ) )
1318 WINE_ERR("No IID_IShellLink\n");
1319 return 1;
1322 r = IShellLinkW_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
1323 if( FAILED( r ) )
1325 WINE_ERR("No IID_IPersistFile\n");
1326 return 1;
1329 r = IPersistFile_Load( pf, fullname, STGM_READ );
1330 if( SUCCEEDED( r ) )
1332 /* If something fails (eg. Couldn't extract icon)
1333 * wait for parent process and try again
1335 if( ! InvokeShellLinker( sl, fullname, bWait ) && bWait )
1337 WaitForParentProcess();
1338 InvokeShellLinker( sl, fullname, FALSE );
1342 IPersistFile_Release( pf );
1343 IShellLinkW_Release( sl );
1345 CoUninitialize();
1347 return !r;
1351 static CHAR *next_token( LPSTR *p )
1353 LPSTR token = NULL, t = *p;
1355 if( !t )
1356 return NULL;
1358 while( t && !token )
1360 switch( *t )
1362 case ' ':
1363 t++;
1364 continue;
1365 case '"':
1366 /* unquote the token */
1367 token = ++t;
1368 t = strchr( token, '"' );
1369 if( t )
1370 *t++ = 0;
1371 break;
1372 case 0:
1373 t = NULL;
1374 break;
1375 default:
1376 token = t;
1377 t = strchr( token, ' ' );
1378 if( t )
1379 *t++ = 0;
1380 break;
1383 *p = t;
1384 return token;
1387 static BOOL init_xdg(void)
1389 if (getenv("XDG_CONFIG_HOME"))
1390 xdg_config_dir = heap_printf("%s/menus/applications-merged", getenv("XDG_CONFIG_HOME"));
1391 else
1392 xdg_config_dir = heap_printf("%s/.config/menus/applications-merged", getenv("HOME"));
1393 if (xdg_config_dir)
1395 if (getenv("XDG_DATA_HOME"))
1396 xdg_data_dir = heap_printf("%s", getenv("XDG_DATA_HOME"));
1397 else
1398 xdg_data_dir = heap_printf("%s/.local/share", getenv("HOME"));
1399 if (xdg_data_dir)
1401 char *buffer;
1402 create_directories(xdg_data_dir);
1403 buffer = heap_printf("%s/desktop-directories", xdg_data_dir);
1404 if (buffer)
1406 mkdir(buffer, 0777);
1407 HeapFree(GetProcessHeap(), 0, buffer);
1409 return TRUE;
1411 HeapFree(GetProcessHeap(), 0, xdg_config_dir);
1413 return FALSE;
1416 /***********************************************************************
1418 * WinMain
1420 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
1422 LPSTR token = NULL, p;
1423 BOOL bWait = FALSE;
1424 int ret = 0;
1426 init_xdg();
1428 for( p = cmdline; p && *p; )
1430 token = next_token( &p );
1431 if( !token )
1432 break;
1433 if( !lstrcmpA( token, "-w" ) )
1434 bWait = TRUE;
1435 else if( token[0] == '-' )
1437 WINE_ERR( "unknown option %s\n",token);
1439 else
1441 WCHAR link[MAX_PATH];
1443 MultiByteToWideChar( CP_ACP, 0, token, -1, link, sizeof(link)/sizeof(WCHAR) );
1444 if( !Process_Link( link, bWait ) )
1446 WINE_ERR( "failed to build menu item for %s\n",token);
1447 ret = 1;
1452 return ret;