comparison pidgin/win32/winpidgin.c @ 15389:81cfeef96dea

Restoring win32 stuff that was (I assume accidentally) dropped. I took advantage of the opportunity to try the daggy fix stuff.
author Daniel Atallah <daniel.atallah@gmail.com>
date Tue, 23 Jan 2007 15:50:27 +0000
parents 0e17470b47c2
children 75ffc646647f
comparison
equal deleted inserted replaced
15388:937193bf17c3 15389:81cfeef96dea
1 /*
2 * winpidgin.c
3 *
4 * Date: June, 2002
5 * Description: Entry point for win32 pidgin, 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 /* These will hopefully be in the win32api next time it is updated - at which point, we'll remove them */
39 #ifndef LANG_PERSIAN
40 #define LANG_PERSIAN 0x29
41 #endif
42 #ifndef LANG_BOSNIAN
43 #define SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN 0x05
44 #define SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC 0x08
45 #endif
46 #ifndef SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN
47 #define SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN 0x04
48 #endif
49 #ifndef LANG_XHOSA
50 #define LANG_XHOSA 0x34
51 #endif
52
53
54 typedef int (CALLBACK* LPFNGAIMMAIN)(HINSTANCE, int, char**);
55 typedef void (CALLBACK* LPFNSETDLLDIRECTORY)(LPCTSTR);
56 typedef BOOL (CALLBACK* LPFNATTACHCONSOLE)(DWORD);
57
58 /*
59 * PROTOTYPES
60 */
61 static LPFNGAIMMAIN gaim_main = NULL;
62 static LPFNSETDLLDIRECTORY MySetDllDirectory = NULL;
63
64 static const char *get_win32_error_message(DWORD err) {
65 static char err_msg[512];
66
67 FormatMessage(
68 FORMAT_MESSAGE_FROM_SYSTEM,
69 NULL, err,
70 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
71 (LPTSTR) &err_msg, sizeof(err_msg), NULL);
72
73 return err_msg;
74 }
75
76 static BOOL read_reg_string(HKEY key, char* sub_key, char* val_name, LPBYTE data, LPDWORD data_len) {
77 HKEY hkey;
78 BOOL ret = FALSE;
79 LONG retv;
80
81 if (ERROR_SUCCESS == (retv = RegOpenKeyEx(key, sub_key, 0,
82 KEY_QUERY_VALUE, &hkey))) {
83 if (ERROR_SUCCESS == (retv = RegQueryValueEx(hkey, val_name,
84 NULL, NULL, data, data_len)))
85 ret = TRUE;
86 else {
87 const char *err_msg = get_win32_error_message(retv);
88
89 printf("Could not read reg key '%s' subkey '%s' value: '%s'.\nMessage: (%ld) %s\n",
90 ((key == HKEY_LOCAL_MACHINE) ? "HKLM" :
91 (key == HKEY_CURRENT_USER) ? "HKCU" :
92 "???"),
93 sub_key, val_name, retv, err_msg);
94 }
95 RegCloseKey(hkey);
96 }
97 else {
98 TCHAR szBuf[80];
99
100 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, retv, 0,
101 (LPTSTR) &szBuf, sizeof(szBuf), NULL);
102 printf("Could not open reg subkey: %s\nError: (%ld) %s\n",
103 sub_key, retv, szBuf);
104 }
105
106 return ret;
107 }
108
109 static void dll_prep() {
110 char path[MAX_PATH + 1];
111 HMODULE hmod;
112 HKEY hkey;
113 #ifdef PORTABLE
114 /* We assume that GTK+ is installed under \\path\to\Gaim\..\GTK
115 * First we find \\path\to
116 */
117 if (GetModuleFileName(NULL, path, MAX_PATH) != 0) {
118 char *tmp = path;
119 char *prev = NULL;
120 char *prev2 = NULL;
121
122 while ((tmp = strchr(tmp, '\\'))) {
123 prev2 = prev;
124 prev = tmp;
125 tmp++;
126 }
127
128 if (prev2) {
129 prev2[0] = '\0';
130 }
131 } else {
132 printf("Unable to determine current executable path. \n"
133 "This will prevent the settings dir from being set.\n"
134 "Assuming GTK+ is in the PATH.\n");
135 }
136
137 if (path) {
138 /* Set up the settings dir base to be \\path\to
139 * The actual settings dir will be \\path\to\.gaim */
140 char settingsdir[strlen(path) + strlen("GAIMHOME=") + 1];
141 char aspelldir[strlen(path) + strlen("GAIM_ASPELL_DIR=\\Aspell\\bin") + 1];
142
143 snprintf(settingsdir, sizeof(settingsdir), "GAIMHOME=%s", path);
144 printf("Setting settings dir: %s\n", settingsdir);
145 putenv(settingsdir);
146
147 snprintf(aspelldir, sizeof(aspelldir), "GAIM_ASPELL_DIR=%s\\Aspell\\bin", path);
148 printf("%s", aspelldir);
149 putenv(aspelldir);
150
151 /* set the GTK+ path to be \\path\to\GTK\bin */
152 strcat(path, "\\GTK\\bin");
153 } else
154 return;
155 #else /* PORTABLE */
156 char gtkpath[MAX_PATH + 1];
157 DWORD plen;
158
159 plen = sizeof(gtkpath);
160 hkey = HKEY_CURRENT_USER;
161 if (!read_reg_string(hkey, "SOFTWARE\\GTK\\2.0", "Path",
162 (LPBYTE) &gtkpath, &plen)) {
163 hkey = HKEY_LOCAL_MACHINE;
164 if (!read_reg_string(hkey, "SOFTWARE\\GTK\\2.0", "Path",
165 (LPBYTE) &gtkpath, &plen)) {
166 printf("GTK+ Path Registry Key not found. "
167 "Assuming GTK+ is in the PATH.\n");
168 return;
169 }
170 }
171
172 /* this value is replaced during a successful RegQueryValueEx() */
173 plen = sizeof(path);
174 /* Determine GTK+ dll path .. */
175 if (!read_reg_string(hkey, "SOFTWARE\\GTK\\2.0", "DllPath",
176 (LPBYTE) &path, &plen)) {
177 strcpy(path, gtkpath);
178 strcat(path, "\\bin");
179 }
180 #endif
181 printf("GTK+ path found: %s\n", path);
182
183 if ((hmod = GetModuleHandle("kernel32.dll"))) {
184 MySetDllDirectory = (LPFNSETDLLDIRECTORY) GetProcAddress(
185 hmod, "SetDllDirectoryA");
186 if (!MySetDllDirectory)
187 printf("SetDllDirectory not supported\n");
188 } else
189 printf("Error getting kernel32.dll module handle\n");
190
191 /* For Windows XP SP1+ / Server 2003 we use SetDllDirectory to avoid dll hell */
192 if (MySetDllDirectory) {
193 printf("Using SetDllDirectory\n");
194 MySetDllDirectory(path);
195 }
196
197 /* For the rest, we set the current directory and make sure
198 * SafeDllSearch is set to 0 where needed. */
199 else {
200 OSVERSIONINFO osinfo;
201
202 printf("Setting current directory to GTK+ dll directory\n");
203 SetCurrentDirectory(path);
204 /* For Windows 2000 (SP3+) / WinXP (No SP):
205 * If SafeDllSearchMode is set to 1, Windows system directories are
206 * searched for dlls before the current directory. Therefore we set it
207 * to 0.
208 */
209 osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
210 GetVersionEx(&osinfo);
211 if ((osinfo.dwMajorVersion == 5 &&
212 osinfo.dwMinorVersion == 0 &&
213 strcmp(osinfo.szCSDVersion, "Service Pack 3") >= 0) ||
214 (osinfo.dwMajorVersion == 5 &&
215 osinfo.dwMinorVersion == 1 &&
216 strcmp(osinfo.szCSDVersion, "") >= 0)
217 ) {
218 DWORD regval = 1;
219 DWORD reglen = sizeof(DWORD);
220
221 printf("Using Win2k (SP3+) / WinXP (No SP)... Checking SafeDllSearch\n");
222 read_reg_string(HKEY_LOCAL_MACHINE,
223 "System\\CurrentControlSet\\Control\\Session Manager",
224 "SafeDllSearchMode",
225 (LPBYTE) &regval,
226 &reglen);
227
228 if (regval != 0) {
229 printf("Trying to set SafeDllSearchMode to 0\n");
230 regval = 0;
231 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
232 "System\\CurrentControlSet\\Control\\Session Manager",
233 0, KEY_SET_VALUE, &hkey
234 ) == ERROR_SUCCESS) {
235 if (RegSetValueEx(hkey,
236 "SafeDllSearchMode", 0,
237 REG_DWORD, (LPBYTE) &regval,
238 sizeof(DWORD)
239 ) != ERROR_SUCCESS)
240 printf("Error writing SafeDllSearchMode. Error: %u\n",
241 (UINT) GetLastError());
242 RegCloseKey(hkey);
243 } else
244 printf("Error opening Session Manager key for writing. Error: %u\n",
245 (UINT) GetLastError());
246 } else
247 printf("SafeDllSearchMode is set to 0\n");
248 }/*end else*/
249 }
250 }
251
252 static char* wgaim_lcid_to_posix(LCID lcid) {
253 char *posix = NULL;
254 int lang_id = PRIMARYLANGID(lcid);
255 int sub_id = SUBLANGID(lcid);
256
257 switch (lang_id) {
258 case LANG_ARABIC: posix = "ar"; break;
259 case LANG_AZERI: posix = "az"; break;
260 case LANG_BENGALI: posix = "bn"; break;
261 case LANG_BULGARIAN: posix = "bg"; break;
262 case LANG_CATALAN: posix = "ca"; break;
263 case LANG_CHINESE:
264 switch (sub_id) {
265 case SUBLANG_CHINESE_SIMPLIFIED:
266 posix = "zh_CN"; break;
267 case SUBLANG_CHINESE_TRADITIONAL:
268 posix = "zh_TW"; break;
269 default:
270 posix = "zh"; break;
271 }
272 break;
273 case LANG_CZECH: posix = "cs"; break;
274 case LANG_DANISH: posix = "da"; break;
275 case LANG_ESTONIAN: posix = "et"; break;
276 case LANG_PERSIAN: posix = "fa"; break;
277 case LANG_GERMAN: posix = "de"; break;
278 case LANG_GREEK: posix = "el"; break;
279 case LANG_ENGLISH:
280 switch (sub_id) {
281 case SUBLANG_ENGLISH_UK:
282 posix = "en_GB"; break;
283 case SUBLANG_ENGLISH_AUS:
284 posix = "en_AU"; break;
285 case SUBLANG_ENGLISH_CAN:
286 posix = "en_CA"; break;
287 default:
288 posix = "en"; break;
289 }
290 break;
291 case LANG_SPANISH: posix = "es"; break;
292 case LANG_BASQUE: posix = "eu"; break;
293 case LANG_FINNISH: posix = "fi"; break;
294 case LANG_FRENCH: posix = "fr"; break;
295 case LANG_GALICIAN: posix = "gl"; break;
296 case LANG_GUJARATI: posix = "gu"; break;
297 case LANG_HEBREW: posix = "he"; break;
298 case LANG_HINDI: posix = "hi"; break;
299 case LANG_HUNGARIAN: posix = "hu"; break;
300 case LANG_ICELANDIC: break;
301 case LANG_ITALIAN: posix = "it"; break;
302 case LANG_JAPANESE: posix = "ja"; break;
303 case LANG_GEORGIAN: posix = "ka"; break;
304 case LANG_KOREAN: posix = "ko"; break;
305 case LANG_LITHUANIAN: posix = "lt"; break;
306 case LANG_MACEDONIAN: posix = "mk"; break;
307 case LANG_DUTCH: posix = "nl"; break;
308 case LANG_NEPALI: posix = "ne"; break;
309 case LANG_NORWEGIAN:
310 switch (sub_id) {
311 case SUBLANG_NORWEGIAN_BOKMAL:
312 posix = "nb"; break;
313 case SUBLANG_NORWEGIAN_NYNORSK:
314 posix = "nn"; break;
315 }
316 break;
317 case LANG_PUNJABI: posix = "pa"; break;
318 case LANG_POLISH: posix = "pl"; break;
319 case LANG_PORTUGUESE:
320 switch (sub_id) {
321 case SUBLANG_PORTUGUESE_BRAZILIAN:
322 posix = "pt_BR"; break;
323 default:
324 posix = "pt"; break;
325 }
326 break;
327 case LANG_ROMANIAN: posix = "ro"; break;
328 case LANG_RUSSIAN: posix = "ru"; break;
329 /* LANG_CROATIAN == LANG_SERBIAN == LANG_BOSNIAN */
330 case LANG_SERBIAN:
331 switch (sub_id) {
332 case SUBLANG_SERBIAN_LATIN:
333 posix = "sr@Latn"; break;
334 case SUBLANG_SERBIAN_CYRILLIC:
335 posix = "sr"; break;
336 case SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC:
337 case SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN:
338 posix = "bs"; break;
339 case SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN:
340 posix = "hr"; break;
341 }
342 break;
343 case LANG_SLOVAK: posix = "sk"; break;
344 case LANG_SLOVENIAN: posix = "sl"; break;
345 case LANG_ALBANIAN: posix = "sq"; break;
346 case LANG_SWEDISH: posix = "sv"; break;
347 case LANG_TAMIL: posix = "ta"; break;
348 case LANG_TELUGU: posix = "te"; break;
349 case LANG_THAI: posix = "th"; break;
350 case LANG_TURKISH: posix = "tr"; break;
351 case LANG_UKRAINIAN: posix = "uk"; break;
352 case LANG_VIETNAMESE: posix = "vi"; break;
353 case LANG_XHOSA: posix = "xh"; break;
354 case LANG_URDU: break;
355 case LANG_INDONESIAN: break;
356 case LANG_BELARUSIAN: break;
357 case LANG_LATVIAN: break;
358 case LANG_ARMENIAN: break;
359 case LANG_AFRIKAANS: break;
360 case LANG_FAEROESE: break;
361 case LANG_MALAY: break;
362 case LANG_KAZAK: break;
363 case LANG_KYRGYZ: break;
364 case LANG_SWAHILI: break;
365 case LANG_UZBEK: break;
366 case LANG_TATAR: break;
367 case LANG_ORIYA: break;
368 case LANG_KANNADA: break;
369 case LANG_MALAYALAM: break;
370 case LANG_ASSAMESE: break;
371 case LANG_MARATHI: break;
372 case LANG_SANSKRIT: break;
373 case LANG_MONGOLIAN: break;
374 case LANG_KONKANI: break;
375 case LANG_MANIPURI: break;
376 case LANG_SINDHI: break;
377 case LANG_SYRIAC: break;
378 case LANG_KASHMIRI: break;
379 case LANG_DIVEHI: break;
380 }
381
382 /* Deal with exceptions */
383 if (posix == NULL) {
384 switch (lcid) {
385 case 0x0455: posix = "my_MM"; break; /* Myanmar (Burmese) */
386 case 9999: posix = "ku"; break; /* Kurdish (from NSIS) */
387 }
388 }
389
390 return posix;
391 }
392
393 /* Determine and set Gaim locale as follows (in order of priority):
394 - Check GAIMLANG env var
395 - Check NSIS Installer Language reg value
396 - Use default user locale
397 */
398 static const char *wgaim_get_locale() {
399 const char *locale = NULL;
400 LCID lcid;
401 #ifndef PORTABLE
402 char data[10];
403 DWORD datalen = 10;
404 #endif
405
406 /* Check if user set GAIMLANG env var */
407 if ((locale = getenv("GAIMLANG")))
408 return locale;
409
410 #ifndef PORTABLE
411 if (read_reg_string(HKEY_CURRENT_USER, "SOFTWARE\\gaim",
412 "Installer Language", (LPBYTE) &data, &datalen)) {
413 if ((locale = wgaim_lcid_to_posix(atoi(data))))
414 return locale;
415 }
416 #endif
417
418 lcid = GetUserDefaultLCID();
419 if ((locale = wgaim_lcid_to_posix(lcid)))
420 return locale;
421
422 return "en";
423 }
424
425 static void wgaim_set_locale() {
426 const char *locale = NULL;
427 char envstr[25];
428
429 locale = wgaim_get_locale();
430
431 snprintf(envstr, 25, "LANG=%s", locale);
432 printf("Setting locale: %s\n", envstr);
433 putenv(envstr);
434 }
435
436 #define WM_FOCUS_REQUEST (WM_APP + 13)
437
438 static BOOL wgaim_set_running() {
439 HANDLE h;
440
441 if ((h = CreateMutex(NULL, FALSE, "gaim_is_running"))) {
442 if (GetLastError() == ERROR_ALREADY_EXISTS) {
443 HWND msg_win;
444
445 if((msg_win = FindWindow(TEXT("WingaimMsgWinCls"), NULL)))
446 if(SendMessage(msg_win, WM_FOCUS_REQUEST, (WPARAM) NULL, (LPARAM) NULL))
447 return FALSE;
448
449 /* If we get here, the focus request wasn't successful */
450
451 MessageBox(NULL,
452 "An instance of Gaim is already running",
453 NULL, MB_OK | MB_TOPMOST);
454
455 return FALSE;
456 }
457 }
458 return TRUE;
459 }
460
461
462 #ifdef __GNUC__
463 # ifndef _stdcall
464 # define _stdcall __attribute__((stdcall))
465 # endif
466 #endif
467
468 int _stdcall
469 WinMain (struct HINSTANCE__ *hInstance, struct HINSTANCE__ *hPrevInstance,
470 char *lpszCmdLine, int nCmdShow) {
471 char errbuf[512];
472 char gaimdir[MAX_PATH];
473 HMODULE hmod;
474
475 /* If debug or help or version flag used, create console for output */
476 if (strstr(lpszCmdLine, "-d") || strstr(lpszCmdLine, "-h") || strstr(lpszCmdLine, "-v")) {
477 /* If stdout hasn't been redirected to a file, alloc a console
478 * (_istty() doesn't work for stuff using the GUI subsystem) */
479 if (_fileno(stdout) == -1) {
480 LPFNATTACHCONSOLE MyAttachConsole = NULL;
481 if ((hmod = GetModuleHandle("kernel32.dll"))) {
482 MyAttachConsole =
483 (LPFNATTACHCONSOLE)
484 GetProcAddress(hmod, "AttachConsole");
485 }
486 if ((MyAttachConsole && MyAttachConsole(ATTACH_PARENT_PROCESS))
487 || AllocConsole()) {
488 freopen("CONOUT$", "w", stdout);
489 freopen("CONOUT$", "w", stderr);
490 }
491 }
492 }
493
494 /* Load exception handler if we have it */
495 if (GetModuleFileName(NULL, gaimdir, MAX_PATH) != 0) {
496 char *tmp = gaimdir;
497 char *prev = NULL;
498
499 while ((tmp = strchr(tmp, '\\'))) {
500 prev = tmp;
501 tmp++;
502 }
503
504 if (prev) {
505 prev[0] = '\0';
506 strcat(gaimdir, "\\exchndl.dll");
507 if (LoadLibrary(gaimdir))
508 printf("Loaded exchndl.dll\n");
509 }
510 } else {
511 DWORD dw = GetLastError();
512 const char *err_msg = get_win32_error_message(dw);
513 snprintf(errbuf, 512,
514 "Error getting module filename.\nError: (%u) %s",
515 (UINT) dw, err_msg);
516 printf("%s", errbuf);
517 MessageBox(NULL, errbuf, NULL, MB_OK | MB_TOPMOST);
518 }
519
520 #ifndef PORTABLE
521 if (!getenv("GAIM_NO_DLL_CHECK"))
522 #endif
523 dll_prep();
524
525 wgaim_set_locale();
526 /* If help or version flag used, do not check Mutex */
527 if (!strstr(lpszCmdLine, "-h") && !strstr(lpszCmdLine, "-v"))
528 if (!getenv("GAIM_MULTI_INST") && !wgaim_set_running())
529 return 0;
530
531 /* Now we are ready for Gaim .. */
532 if ((hmod = LoadLibrary("pidgin.dll"))) {
533 gaim_main = (LPFNGAIMMAIN) GetProcAddress(hmod, "gaim_main");
534 }
535
536 if (!gaim_main) {
537 DWORD dw = GetLastError();
538 BOOL mod_not_found = (dw == ERROR_MOD_NOT_FOUND || dw == ERROR_DLL_NOT_FOUND);
539 const char *err_msg = get_win32_error_message(dw);
540
541 snprintf(errbuf, 512, "Error loading pidgin.dll.\nError: (%u) %s%s%s",
542 (UINT) dw, err_msg,
543 mod_not_found ? "\n" : "",
544 mod_not_found ? "This probably means that GTK+ can't be found." : "");
545 printf("%s", errbuf);
546 MessageBox(NULL, errbuf, TEXT("Error"), MB_OK | MB_TOPMOST);
547
548 return 0;
549 }
550
551 return gaim_main (hInstance, __argc, __argv);
552 }