comparison gtk/win32/win_gaim.c @ 14224:ab8a105eff62

[gaim-migrate @ 16905] First step of getting wingaim working again. libgaim and gtk are compiling. The protocols aren't compiling yet. There are a number of things that are compiling, but should be cleaned up. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Sun, 20 Aug 2006 16:49:37 +0000
parents
children 14933d9055a0
comparison
equal deleted inserted replaced
14223:7c560c01b8f9 14224:ab8a105eff62
1 /*
2 * win_gaim.c
3 *
4 * Date: June, 2002
5 * Description: Entry point for win32 gaim, and various win32 dependant
6 * routines.
7 *
8 * Gaim is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28 /* This is for ATTACH_PARENT_PROCESS */
29 #ifndef _WIN32_WINNT
30 #define _WIN32_WINNT 0x501
31 #endif
32 #include <windows.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37
38 #define WIN32_PROXY_REGKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
39
40 typedef int (CALLBACK* LPFNGAIMMAIN)(HINSTANCE, int, char**);
41 typedef void (CALLBACK* LPFNSETDLLDIRECTORY)(LPCTSTR);
42 typedef BOOL (CALLBACK* LPFNATTACHCONSOLE)(DWORD);
43
44 /*
45 * PROTOTYPES
46 */
47 static LPFNGAIMMAIN gaim_main = NULL;
48 static LPFNSETDLLDIRECTORY MySetDllDirectory = NULL;
49
50
51 static BOOL read_reg_string(HKEY key, char* sub_key, char* val_name, LPBYTE data, LPDWORD data_len) {
52 HKEY hkey;
53 BOOL ret = FALSE;
54 LONG retv;
55
56 if (ERROR_SUCCESS == (retv = RegOpenKeyEx(key, sub_key, 0,
57 KEY_QUERY_VALUE, &hkey))) {
58 if (ERROR_SUCCESS == (retv = RegQueryValueEx(hkey, val_name,
59 NULL, NULL, data, data_len)))
60 ret = TRUE;
61 else {
62 TCHAR szBuf[80];
63
64 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
65 NULL, retv, 0,
66 (LPTSTR) &szBuf, sizeof(szBuf), NULL);
67
68 printf("Could not read reg key '%s' subkey '%s' value: '%s'\nError: (%ld) %s\n",
69 ((key == HKEY_LOCAL_MACHINE) ? "HKLM" :
70 (key == HKEY_CURRENT_USER) ? "HKCU" :
71 "???"),
72 sub_key, val_name, retv, szBuf);
73 }
74 RegCloseKey(hkey);
75 }
76 else {
77 TCHAR szBuf[80];
78
79 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, retv, 0,
80 (LPTSTR) &szBuf, sizeof(szBuf), NULL);
81 printf("Could not open reg subkey: %s\nError: (%ld) %s\n",
82 sub_key, retv, szBuf);
83 }
84
85 return ret;
86 }
87
88 static void dll_prep() {
89 char path[MAX_PATH + 1];
90 HMODULE hmod;
91 HKEY hkey;
92 #ifdef PORTABLE
93 /* We assume that GTK+ is installed under \\path\to\Gaim\..\GTK
94 * First we find \\path\to
95 */
96 if (GetModuleFileName(NULL, path, MAX_PATH) != 0) {
97 char *tmp = path;
98 char *prev = NULL;
99 char *prev2 = NULL;
100
101 while ((tmp = strchr(tmp, '\\'))) {
102 prev2 = prev;
103 prev = tmp;
104 tmp++;
105 }
106
107 if (prev2) {
108 prev2[0] = '\0';
109 }
110 } else {
111 printf("Unable to determine current executable path. \n"
112 "This will prevent the settings dir from being set.\n"
113 "Assuming GTK+ is in the PATH.\n");
114 }
115
116 if (path) {
117 /* Set up the settings dir base to be \\path\to
118 * The actual settings dir will be \\path\to\.gaim */
119 char settingsdir[strlen(path) + strlen("GAIMHOME=") + 1];
120 char aspelldir[strlen(path) + strlen("GAIM_ASPELL_DIR=\\Aspell\\bin") + 1];
121
122 snprintf(settingsdir, sizeof(settingsdir), "GAIMHOME=%s", path);
123 printf("Setting settings dir: %s\n", settingsdir);
124 putenv(settingsdir);
125
126 snprintf(aspelldir, sizeof(aspelldir), "GAIM_ASPELL_DIR=%s\\Aspell\\bin", path);
127 printf(aspelldir);
128 putenv(aspelldir);
129
130 /* set the GTK+ path to be \\path\to\GTK\bin */
131 strcat(path, "\\GTK\\bin");
132 } else
133 return;
134 #else /* PORTABLE */
135 char gtkpath[MAX_PATH + 1];
136 DWORD plen;
137
138 plen = sizeof(gtkpath);
139 hkey = HKEY_CURRENT_USER;
140 if (!read_reg_string(hkey, "SOFTWARE\\GTK\\2.0", "Path",
141 (LPBYTE) &gtkpath, &plen)) {
142 hkey = HKEY_LOCAL_MACHINE;
143 if (!read_reg_string(hkey, "SOFTWARE\\GTK\\2.0", "Path",
144 (LPBYTE) &gtkpath, &plen)) {
145 printf("GTK+ Path Registry Key not found. "
146 "Assuming GTK+ is in the PATH.\n");
147 return;
148 }
149 }
150
151 /* this value is replaced during a successful RegQueryValueEx() */
152 plen = sizeof(path);
153 /* Determine GTK+ dll path .. */
154 if (!read_reg_string(hkey, "SOFTWARE\\GTK\\2.0", "DllPath",
155 (LPBYTE) &path, &plen)) {
156 strcpy(path, gtkpath);
157 strcat(path, "\\bin");
158 }
159 #endif
160 printf("GTK+ path found: %s\n", path);
161
162 if ((hmod = GetModuleHandle("kernel32.dll"))) {
163 MySetDllDirectory = (LPFNSETDLLDIRECTORY) GetProcAddress(
164 hmod, "SetDllDirectoryA");
165 if (!MySetDllDirectory)
166 printf("SetDllDirectory not supported\n");
167 } else
168 printf("Error getting kernel32.dll module handle\n");
169
170 /* For Windows XP SP1+ / Server 2003 we use SetDllDirectory to avoid dll hell */
171 if (MySetDllDirectory) {
172 printf("Using SetDllDirectory\n");
173 MySetDllDirectory(path);
174 }
175
176 /* For the rest, we set the current directory and make sure
177 * SafeDllSearch is set to 0 where needed. */
178 else {
179 OSVERSIONINFO osinfo;
180
181 printf("Setting current directory to GTK+ dll directory\n");
182 SetCurrentDirectory(path);
183 /* For Windows 2000 (SP3+) / WinXP (No SP):
184 * If SafeDllSearchMode is set to 1, Windows system directories are
185 * searched for dlls before the current directory. Therefore we set it
186 * to 0.
187 */
188 osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
189 GetVersionEx(&osinfo);
190 if ((osinfo.dwMajorVersion == 5 &&
191 osinfo.dwMinorVersion == 0 &&
192 strcmp(osinfo.szCSDVersion, "Service Pack 3") >= 0) ||
193 (osinfo.dwMajorVersion == 5 &&
194 osinfo.dwMinorVersion == 1 &&
195 strcmp(osinfo.szCSDVersion, "") >= 0)
196 ) {
197 DWORD regval = 1;
198 DWORD reglen = sizeof(DWORD);
199
200 printf("Using Win2k (SP3+) / WinXP (No SP)... Checking SafeDllSearch\n");
201 read_reg_string(HKEY_LOCAL_MACHINE,
202 "System\\CurrentControlSet\\Control\\Session Manager",
203 "SafeDllSearchMode",
204 (LPBYTE) &regval,
205 &reglen);
206
207 if (regval != 0) {
208 printf("Trying to set SafeDllSearchMode to 0\n");
209 regval = 0;
210 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
211 "System\\CurrentControlSet\\Control\\Session Manager",
212 0, KEY_SET_VALUE, &hkey
213 ) == ERROR_SUCCESS) {
214 if (RegSetValueEx(hkey,
215 "SafeDllSearchMode", 0,
216 REG_DWORD, (LPBYTE) &regval,
217 sizeof(DWORD)
218 ) != ERROR_SUCCESS)
219 printf("Error writing SafeDllSearchMode. Error: %u\n",
220 (UINT) GetLastError());
221 RegCloseKey(hkey);
222 } else
223 printf("Error opening Session Manager key for writing. Error: %u\n",
224 (UINT) GetLastError());
225 } else
226 printf("SafeDllSearchMode is set to 0\n");
227 }/*end else*/
228 }
229 }
230
231 static char* wgaim_lcid_to_posix(LCID lcid) {
232 char *posix = NULL;
233 int lang_id = PRIMARYLANGID(lcid);
234 int sub_id = SUBLANGID(lcid);
235
236 switch (lang_id) {
237 case LANG_ARABIC: break;
238 case LANG_BULGARIAN: posix = "bg"; break;
239 case LANG_CATALAN: posix = "ca"; break;
240 case LANG_CHINESE:
241 switch (sub_id) {
242 case SUBLANG_CHINESE_SIMPLIFIED:
243 posix = "zh_CN"; break;
244 case SUBLANG_CHINESE_TRADITIONAL:
245 posix = "zh_TW"; break;
246 default:
247 posix = "zh"; break;
248 }
249 break;
250 case LANG_CZECH: posix = "cs"; break;
251 case LANG_DANISH: posix = "da"; break;
252 case LANG_GERMAN: posix = "de"; break;
253 case LANG_GREEK: posix = "el"; break;
254 case LANG_ENGLISH:
255 switch (sub_id) {
256 case SUBLANG_ENGLISH_UK:
257 posix = "en_GB"; break;
258 case SUBLANG_ENGLISH_AUS:
259 posix = "en_AU"; break;
260 case SUBLANG_ENGLISH_CAN:
261 posix = "en_CA"; break;
262 default:
263 posix = "en"; break;
264 }
265 break;
266 case LANG_SPANISH: posix = "es"; break;
267 case LANG_FINNISH: posix = "fi"; break;
268 case LANG_FRENCH: posix = "fr"; break;
269 case LANG_HEBREW: posix = "he"; break;
270 case LANG_HUNGARIAN: posix = "hu"; break;
271 case LANG_ICELANDIC: break;
272 case LANG_ITALIAN: posix = "it"; break;
273 case LANG_JAPANESE: posix = "ja"; break;
274 case LANG_KOREAN: posix = "ko"; break;
275 case LANG_DUTCH: posix = "nl"; break;
276 case LANG_NORWEGIAN:
277 switch (sub_id) {
278 case SUBLANG_NORWEGIAN_BOKMAL:
279 posix = "nb"; break;
280 case SUBLANG_NORWEGIAN_NYNORSK:
281 posix = "nn"; break;
282 }
283 break;
284 case LANG_POLISH: posix = "pl"; break;
285 case LANG_PORTUGUESE:
286 switch (sub_id) {
287 case SUBLANG_PORTUGUESE_BRAZILIAN:
288 posix = "pt_BR"; break;
289 default:
290 posix = "pt"; break;
291 }
292 break;
293 case LANG_ROMANIAN: posix = "ro"; break;
294 case LANG_RUSSIAN: posix = "ru"; break;
295 /* LANG_CROATIAN == LANG_SERBIAN for some reason
296 * We'll need to do something here if we ever get a croatian translation */
297 /* case LANG_CROATIAN: posix = "hr"; break;*/
298 case LANG_SERBIAN:
299 switch (sub_id) {
300 case SUBLANG_SERBIAN_LATIN:
301 posix = "sr@Latn"; break;
302 case SUBLANG_SERBIAN_CYRILLIC:
303 posix = "sr"; break;
304 }
305 break;
306 case LANG_SLOVAK: posix = "sk"; break;
307 case LANG_ALBANIAN: posix = "sq"; break;
308 case LANG_SWEDISH: posix = "sv"; break;
309 case LANG_THAI: posix = "th"; break;
310 case LANG_TURKISH: posix = "tr"; break;
311 case LANG_URDU: break;
312 case LANG_INDONESIAN: break;
313 case LANG_UKRAINIAN: posix = "uk"; break;
314 case LANG_BELARUSIAN: break;
315 case LANG_SLOVENIAN: posix = "sl"; break;
316 case LANG_ESTONIAN: posix = "et"; break;
317 case LANG_LATVIAN: break;
318 case LANG_LITHUANIAN: posix = "lt"; break;
319 case LANG_FARSI: break;
320 case LANG_VIETNAMESE: posix = "vi"; break;
321 case LANG_ARMENIAN: break;
322 case LANG_AZERI: posix = "az"; break;
323 case LANG_BASQUE: break;
324 case LANG_MACEDONIAN: posix = "mk"; break;
325 case LANG_AFRIKAANS: break;
326 case LANG_GEORGIAN: posix = "ka"; break;
327 case LANG_FAEROESE: break;
328 case LANG_HINDI: posix = "hi"; break;
329 case LANG_MALAY: break;
330 case LANG_KAZAK: break;
331 case LANG_KYRGYZ: break;
332 case LANG_SWAHILI: break;
333 case LANG_UZBEK: break;
334 case LANG_TATAR: break;
335 case LANG_BENGALI: break;
336 case LANG_PUNJABI: posix = "pa"; break;
337 case LANG_GUJARATI: posix = "gu"; break;
338 case LANG_ORIYA: break;
339 case LANG_TAMIL: posix = "ta"; break;
340 case LANG_TELUGU: break;
341 case LANG_KANNADA: break;
342 case LANG_MALAYALAM: break;
343 case LANG_ASSAMESE: break;
344 case LANG_MARATHI: break;
345 case LANG_SANSKRIT: break;
346 case LANG_MONGOLIAN: break;
347 case LANG_GALICIAN: posix = "gl"; break;
348 case LANG_KONKANI: break;
349 case LANG_MANIPURI: break;
350 case LANG_SINDHI: break;
351 case LANG_SYRIAC: break;
352 case LANG_KASHMIRI: break;
353 case LANG_NEPALI: break;
354 case LANG_DIVEHI: break;
355 }
356
357 /* Deal with exceptions */
358 if (posix == NULL) {
359 switch (lcid) {
360 case 2125: posix = "my_MM"; break; /* Myanmar (Burmese) */
361 case 1076: posix = "xh"; break; /* Xhosa */
362 case 9999: posix = "ku"; break; /* Kurdish (from NSIS) */
363 }
364 }
365
366 return posix;
367 }
368
369 /* Determine and set Gaim locale as follows (in order of priority):
370 - Check GAIMLANG env var
371 - Check NSIS Installer Language reg value
372 - Use default user locale
373 */
374 static const char *wgaim_get_locale() {
375 const char *locale = NULL;
376 LCID lcid;
377 #ifndef PORTABLE
378 char data[10];
379 DWORD datalen = 10;
380 #endif
381
382 /* Check if user set GAIMLANG env var */
383 if ((locale = getenv("GAIMLANG")))
384 return locale;
385
386 #ifndef PORTABLE
387 if (read_reg_string(HKEY_CURRENT_USER, "SOFTWARE\\gaim",
388 "Installer Language", (LPBYTE) &data, &datalen)) {
389 if ((locale = wgaim_lcid_to_posix(atoi(data))))
390 return locale;
391 }
392 #endif
393
394 lcid = GetUserDefaultLCID();
395 if ((locale = wgaim_lcid_to_posix(lcid)))
396 return locale;
397
398 return "en";
399 }
400
401 static void wgaim_set_locale() {
402 const char *locale = NULL;
403 char envstr[25];
404
405 locale = wgaim_get_locale();
406
407 snprintf(envstr, 25, "LANG=%s", locale);
408 printf("Setting locale: %s\n", envstr);
409 putenv(envstr);
410 }
411
412 static BOOL wgaim_set_running() {
413 HANDLE h;
414
415 if ((h = CreateMutex(NULL, FALSE, "gaim_is_running"))) {
416 if (GetLastError() == ERROR_ALREADY_EXISTS) {
417 MessageBox(NULL,
418 "An instance of Gaim is already running",
419 NULL, MB_OK | MB_TOPMOST);
420 return FALSE;
421 }
422 }
423 return TRUE;
424 }
425
426 static void wgaim_set_proxy() {
427 DWORD regval = 1;
428 DWORD reglen = sizeof(DWORD);
429
430 /* If the proxy server environment variables are already set,
431 * we shouldn't override them */
432 if (getenv("HTTP_PROXY") || getenv("http_proxy") || getenv("HTTPPROXY"))
433 return;
434
435 if (read_reg_string(HKEY_CURRENT_USER, WIN32_PROXY_REGKEY,
436 "ProxyEnable",
437 (LPBYTE) &regval, &reglen) && (regval & 1)) {
438 char proxy_server[2048];
439 char *c = NULL;
440 reglen = sizeof(proxy_server);
441
442 if (!read_reg_string(HKEY_CURRENT_USER, WIN32_PROXY_REGKEY,
443 "ProxyServer", (LPBYTE) &proxy_server, &reglen))
444 return;
445
446 if ((reglen > strlen("http="))
447 && (c = strstr(proxy_server, "http="))) {
448 char *d;
449 c += strlen("http=");
450 d = strchr(c, ';');
451 if (d) {
452 *d = '\0';
453 }
454 /* c now points the proxy server (and port) */
455 }
456
457 if (c) {
458 const char envstr_prefix[] = "HTTP_PROXY=http://";
459 char envstr[sizeof(envstr_prefix) + strlen(c) + 1];
460 snprintf(envstr, sizeof(envstr), "%s%s",
461 envstr_prefix, c);
462 printf("Setting HTTP Proxy: %s\n", envstr);
463 putenv(envstr);
464 }
465 }
466
467 }
468
469 #ifdef __GNUC__
470 # ifndef _stdcall
471 # define _stdcall __attribute__((stdcall))
472 # endif
473 #endif
474
475 int _stdcall
476 WinMain (struct HINSTANCE__ *hInstance, struct HINSTANCE__ *hPrevInstance,
477 char *lpszCmdLine, int nCmdShow) {
478 char errbuf[512];
479 char gaimdir[MAX_PATH];
480 HMODULE hmod;
481
482 /* If debug or help or version flag used, create console for output */
483 if (strstr(lpszCmdLine, "-d") || strstr(lpszCmdLine, "-h") || strstr(lpszCmdLine, "-v")) {
484 LPFNATTACHCONSOLE MyAttachConsole = NULL;
485 if ((hmod = GetModuleHandle("kernel32.dll"))) {
486 MyAttachConsole =
487 (LPFNATTACHCONSOLE)
488 GetProcAddress(hmod, "AttachConsole");
489 }
490 if ((MyAttachConsole && MyAttachConsole(ATTACH_PARENT_PROCESS))
491 || AllocConsole())
492 freopen("CONOUT$", "w", stdout);
493 }
494
495 /* Load exception handler if we have it */
496 if (GetModuleFileName(NULL, gaimdir, MAX_PATH) != 0) {
497 char *tmp = gaimdir;
498 char *prev = NULL;
499
500 while ((tmp = strchr(tmp, '\\'))) {
501 prev = tmp;
502 tmp++;
503 }
504
505 if (prev) {
506 prev[0] = '\0';
507 strcat(gaimdir, "\\exchndl.dll");
508 if (LoadLibrary(gaimdir))
509 printf("Loaded exchndl.dll\n");
510 }
511 } else {
512 snprintf(errbuf, 512,
513 "Error getting module filename. Error: %u",
514 (UINT) GetLastError());
515 MessageBox(NULL, errbuf, NULL, MB_OK | MB_TOPMOST);
516 }
517
518 #ifndef PORTABLE
519 if (!getenv("GAIM_NO_DLL_CHECK"))
520 #endif
521 dll_prep();
522
523 wgaim_set_locale();
524 /* If help or version flag used, do not check Mutex */
525 if (!strstr(lpszCmdLine, "-h") && !strstr(lpszCmdLine, "-v"))
526 if (!getenv("GAIM_MULTI_INST") && !wgaim_set_running())
527 return 0;
528
529 wgaim_set_proxy();
530
531 /* Now we are ready for Gaim .. */
532 if ((hmod = LoadLibrary("gtkgaim.dll"))) {
533 gaim_main = (LPFNGAIMMAIN) GetProcAddress(hmod, "gaim_main");
534 }
535
536 if (!gaim_main) {
537 snprintf(errbuf, 512, "Error loading gaim.dll. Error: %u",
538 (UINT) GetLastError());
539 MessageBox(NULL, errbuf, NULL, MB_OK | MB_TOPMOST);
540 return 0;
541 }
542
543 return gaim_main (hInstance, __argc, __argv);
544 }