user32/tests: Fix typedefs & function definitions so that the code compiles in visual...
[wine/multimedia.git] / programs / winebrowser / main.c
blob0164a0b651dff8db520a92c38e2e15f655340d27
1 /*
2 * winebrowser - winelib app to launch native OS browser or mail client.
4 * Copyright (C) 2004 Chris Morgan
5 * Copyright (C) 2005 Hans Leidekker
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES:
22 * Winebrowser is a winelib application that will start the appropriate
23 * native browser or mail client for a wine installation that lacks a
24 * windows browser/mail client. For example, you will be able to open
25 * urls via native mozilla if no browser has yet been installed in wine.
27 * The application to launch is chosen from a default set or, if set,
28 * taken from a registry key.
30 * The argument may be a regular Windows file name, a file URL, an
31 * URL or a mailto URL. In the first three cases the argument
32 * will be fed to a web browser. In the last case the argument is fed
33 * to a mail client. A mailto URL is composed as follows:
35 * mailto:[E-MAIL]?subject=[TOPIC]&cc=[E-MAIL]&bcc=[E-MAIL]&body=[TEXT]
38 #define WIN32_LEAN_AND_MEAN
40 #include "config.h"
41 #include "wine/port.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
45 #include <windows.h>
46 #include <shlwapi.h>
47 #include <ddeml.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <errno.h>
52 WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
54 typedef LPSTR (*wine_get_unix_file_name_t)(LPCWSTR unixname);
56 static const WCHAR browser_key[] =
57 {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
58 'W','i','n','e','B','r','o','w','s','e','r',0};
60 static char *strdup_unixcp( const WCHAR *str )
62 char *ret;
63 int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
64 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
65 WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
66 return ret;
69 /* try to launch a unix app from a comma separated string of app names */
70 static int launch_app( WCHAR *candidates, const WCHAR *argv1 )
72 char *app, *applist, *cmdline;
73 const char *argv_new[3];
75 if (!(applist = strdup_unixcp( candidates ))) return 1;
76 if (!(cmdline = strdup_unixcp( argv1 )))
78 HeapFree( GetProcessHeap(), 0, applist );
79 return 1;
81 app = strtok( applist, "," );
82 while (app)
84 WINE_TRACE( "Considering: %s\n", wine_dbgstr_a(app) );
85 WINE_TRACE( "argv[1]: %s\n", wine_dbgstr_a(cmdline) );
87 argv_new[0] = app;
88 argv_new[1] = cmdline;
89 argv_new[2] = NULL;
91 spawnvp( _P_OVERLAY, app, argv_new ); /* only returns on error */
92 app = strtok( NULL, "," ); /* grab the next app */
94 WINE_ERR( "could not find a suitable app to run\n" );
96 HeapFree( GetProcessHeap(), 0, applist );
97 HeapFree( GetProcessHeap(), 0, cmdline );
98 return 1;
101 static int open_http_url( const WCHAR *url )
103 static const WCHAR defaultbrowsers[] =
104 {'x','d','g','-','o','p','e','n',',','f','i','r','e','f','o','x',',',
105 'k','o','n','q','u','e','r','o','r',',','m','o','z','i','l','l','a',',',
106 'n','e','t','s','c','a','p','e',',','g','a','l','e','o','n',',',
107 'o','p','e','r','a',',','d','i','l','l','o',0};
108 static const WCHAR browsersW[] =
109 {'B','r','o','w','s','e','r','s',0};
111 WCHAR browsers[256];
112 DWORD length, type;
113 HKEY key;
114 LONG r;
116 length = sizeof(browsers);
117 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
118 if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
120 r = RegQueryValueExW( key, browsersW, 0, &type, (LPBYTE)browsers, &length );
121 RegCloseKey( key );
123 if (r != ERROR_SUCCESS)
124 strcpyW( browsers, defaultbrowsers );
126 return launch_app( browsers, url );
129 static int open_mailto_url( const WCHAR *url )
131 static const WCHAR defaultmailers[] =
132 {'x','d','g','-','e','m','a','i','l',',',
133 'm','o','z','i','l','l','a','-','t','h','u','n','d','e','r','b','i','r','d',',',
134 't','h','u','n','d','e','r','b','i','r','d',',',
135 'e','v','o','l','u','t','i','o','n',0};
136 static const WCHAR mailersW[] =
137 {'M','a','i','l','e','r','s',0};
139 WCHAR mailers[256];
140 DWORD length, type;
141 HKEY key;
142 LONG r;
144 length = sizeof(mailers);
145 /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
146 if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, browser_key, &key )))
148 r = RegQueryValueExW( key, mailersW, 0, &type, (LPBYTE)mailers, &length );
149 RegCloseKey( key );
151 if (r != ERROR_SUCCESS)
152 strcpyW( mailers, defaultmailers );
154 return launch_app( mailers, url );
157 /*****************************************************************************
158 * DDE helper functions.
161 static WCHAR *ddeString = NULL;
162 static HSZ hszTopic = 0, hszReturn = 0;
163 static DWORD ddeInst = 0;
165 /* Dde callback, save the execute or request string for processing */
166 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
167 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
168 ULONG_PTR dwData1, ULONG_PTR dwData2)
170 DWORD size = 0, ret = 0;
172 WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
173 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
175 switch (uType)
177 case XTYP_CONNECT:
178 if (!DdeCmpStringHandles(hsz1, hszTopic))
179 return (HDDEDATA)TRUE;
180 return (HDDEDATA)FALSE;
182 case XTYP_EXECUTE:
183 if (!(size = DdeGetData(hData, NULL, 0, 0)))
184 WINE_ERR("DdeGetData returned zero size of execute string\n");
185 else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, size)))
186 WINE_ERR("Out of memory\n");
187 else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
188 WINE_WARN("DdeGetData did not return %d bytes\n", size);
189 DdeFreeDataHandle(hData);
190 return (HDDEDATA)DDE_FACK;
192 case XTYP_REQUEST:
193 ret = -3; /* error */
194 if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
195 WINE_ERR("DdeQueryString returned zero size of request string\n");
196 else if (!(ddeString = HeapAlloc(GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR))))
197 WINE_ERR("Out of memory\n");
198 else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
199 WINE_WARN("DdeQueryString did not return %d characters\n", size);
200 else
201 ret = -2; /* acknowledgment */
202 return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
203 hszReturn, CF_TEXT, 0);
205 default:
206 return NULL;
210 static WCHAR *get_url_from_dde(void)
212 static const WCHAR szApplication[] = {'I','E','x','p','l','o','r','e',0};
213 static const WCHAR szTopic[] = {'W','W','W','_','O','p','e','n','U','R','L',0};
214 static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
216 HSZ hszApplication = 0;
217 UINT_PTR timer = 0;
218 int rc;
219 WCHAR *ret = NULL;
221 rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
222 if (rc != DMLERR_NO_ERROR)
224 WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
225 goto done;
228 hszApplication = DdeCreateStringHandleW(ddeInst, szApplication, CP_WINUNICODE);
229 if (!hszApplication)
231 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
232 goto done;
235 hszTopic = DdeCreateStringHandleW(ddeInst, szTopic, CP_WINUNICODE);
236 if (!hszTopic)
238 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
239 goto done;
242 hszReturn = DdeCreateStringHandleW(ddeInst, szReturn, CP_WINUNICODE);
243 if (!hszReturn)
245 WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
246 goto done;
249 if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
251 WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
252 goto done;
255 timer = SetTimer(NULL, 0, 5000, NULL);
256 if (!timer)
258 WINE_ERR("SetTimer failed to create timer\n");
259 goto done;
262 while (!ddeString)
264 MSG msg;
265 if (!GetMessage(&msg, NULL, 0, 0)) break;
266 if (msg.message == WM_TIMER) break;
267 DispatchMessage(&msg);
270 if (ddeString)
272 if (*ddeString == '"')
274 WCHAR *endquote = strchrW(ddeString + 1, '"');
275 if (!endquote)
277 WINE_ERR("Unabled to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
278 goto done;
280 *endquote = 0;
281 ret = ddeString+1;
283 else
284 ret = ddeString;
287 done:
288 if (timer) KillTimer(NULL, timer);
289 if (ddeInst)
291 if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
292 if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
293 if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
294 if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
295 DdeUninitialize(ddeInst);
297 return ret;
300 /*****************************************************************************
301 * Main entry point. This is a console application so we have a wmain() not a
302 * winmain().
304 int wmain(int argc, WCHAR *argv[])
306 static const WCHAR nohomeW[] = {'-','n','o','h','o','m','e',0};
307 static const WCHAR mailtoW[] = {'m','a','i','l','t','o',':',0};
308 static const WCHAR fileW[] = {'f','i','l','e',':',0};
310 WCHAR *url = argv[1];
311 wine_get_unix_file_name_t wine_get_unix_file_name_ptr;
312 int ret = 1;
314 /* DDE used only if -nohome is specified; avoids delay in printing usage info
315 * when no parameters are passed */
316 if (url && !strcmpiW( url, nohomeW ))
317 url = argc > 2 ? argv[2] : get_url_from_dde();
319 if (!url)
321 WINE_ERR( "Usage: winebrowser URL\n" );
322 goto done;
325 /* handle an RFC1738 file URL */
326 if (!strncmpiW( url, fileW, 5 ))
328 WCHAR *p;
329 DWORD len = strlenW( url ) + 1;
331 if (UrlUnescapeW( url, NULL, &len, URL_UNESCAPE_INPLACE ) != S_OK)
333 WINE_ERR( "unescaping URL failed: %s\n", wine_dbgstr_w(url) );
334 goto done;
337 /* look for a Windows path after 'file:' */
338 p = url + 5;
339 while (*p)
341 if (isalphaW( p[0] ) && (p[1] == ':' || p[1] == '|')) break;
342 p++;
344 if (!*p)
346 WINE_ERR( "no valid Windows path in: %s\n", wine_dbgstr_w(url) );
347 goto done;
350 if (p[1] == '|') p[1] = ':';
351 url = p;
353 while (*p)
355 if (*p == '/') *p = '\\';
356 p++;
360 /* check if the argument is a local file */
361 wine_get_unix_file_name_ptr = (wine_get_unix_file_name_t)
362 GetProcAddress( GetModuleHandle( "KERNEL32" ), "wine_get_unix_file_name" );
364 if (wine_get_unix_file_name_ptr == NULL)
366 WINE_ERR( "cannot get the address of 'wine_get_unix_file_name'\n" );
368 else
370 char *unixpath;
371 if ((unixpath = wine_get_unix_file_name_ptr( url )))
373 struct stat dummy;
374 if (stat( unixpath, &dummy ) >= 0)
376 int len;
377 WCHAR *unixpathW;
379 len = MultiByteToWideChar( CP_UNIXCP, 0, unixpath, -1, NULL, 0 );
380 if ((unixpathW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
381 MultiByteToWideChar( CP_UNIXCP, 0, unixpath, -1, unixpathW, len );
383 ret = open_http_url( unixpathW );
384 HeapFree( GetProcessHeap(), 0, unixpathW );
385 goto done;
390 if (!strncmpiW( url, mailtoW, 7 ))
391 ret = open_mailto_url( url );
392 else
393 /* let the browser decide how to handle the given url */
394 ret = open_http_url( url );
396 done:
397 HeapFree(GetProcessHeap(), 0, ddeString);
398 return ret;