init.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <config-util.h>
  20. #include <grub/util/misc.h>
  21. #include <grub/osdep/hostfile.h>
  22. #include <grub/util/windows.h>
  23. #include <grub/emu/config.h>
  24. #include <wincon.h>
  25. #include <windows.h>
  26. #include <grub/util/misc.h>
  27. #include "progname.h"
  28. struct grub_windows_console_font_infoex {
  29. ULONG cbSize;
  30. DWORD nFont;
  31. COORD dwFontSize;
  32. UINT FontFamily;
  33. UINT FontWeight;
  34. WCHAR FaceName[LF_FACESIZE];
  35. };
  36. static int
  37. check_is_raster (HMODULE kernel32, HANDLE hnd)
  38. {
  39. CONSOLE_FONT_INFO console_font_info;
  40. BOOL (WINAPI * func_GetCurrentConsoleFont) (HANDLE, BOOL,
  41. PCONSOLE_FONT_INFO);
  42. func_GetCurrentConsoleFont = (void *)
  43. GetProcAddress (kernel32, "GetCurrentConsoleFont");
  44. if (!func_GetCurrentConsoleFont)
  45. return 1;
  46. if (!func_GetCurrentConsoleFont (hnd, FALSE, &console_font_info))
  47. return 1;
  48. return console_font_info.nFont < 12;
  49. }
  50. static void
  51. set_console_unicode_font (void)
  52. {
  53. BOOL (WINAPI * func_SetCurrentConsoleFontEx) (HANDLE, BOOL,
  54. struct grub_windows_console_font_infoex *);
  55. BOOL (WINAPI * func_SetConsoleFont)(HANDLE, DWORD);
  56. HMODULE kernel32;
  57. HANDLE out_handle = GetStdHandle (STD_OUTPUT_HANDLE);
  58. HANDLE err_handle = GetStdHandle (STD_ERROR_HANDLE);
  59. int out_raster, err_raster;
  60. kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  61. if (!kernel32)
  62. return;
  63. out_raster = check_is_raster (kernel32, out_handle);
  64. err_raster = check_is_raster (kernel32, err_handle);
  65. if (!out_raster && !err_raster)
  66. return;
  67. func_SetCurrentConsoleFontEx = (void *) GetProcAddress (kernel32, "SetCurrentConsoleFontEx");
  68. /* Newer windows versions. */
  69. if (func_SetCurrentConsoleFontEx)
  70. {
  71. struct grub_windows_console_font_infoex new_console_font_info;
  72. new_console_font_info.cbSize = sizeof (new_console_font_info);
  73. new_console_font_info.nFont = 12;
  74. new_console_font_info.dwFontSize.X = 7;
  75. new_console_font_info.dwFontSize.Y = 12;
  76. new_console_font_info.FontFamily = FF_DONTCARE;
  77. new_console_font_info.FontWeight = 400;
  78. memcpy (new_console_font_info.FaceName, TEXT("Lucida Console"),
  79. sizeof (TEXT("Lucida Console")));
  80. if (out_raster)
  81. func_SetCurrentConsoleFontEx (out_handle, FALSE,
  82. &new_console_font_info);
  83. if (err_raster)
  84. func_SetCurrentConsoleFontEx (err_handle, FALSE,
  85. &new_console_font_info);
  86. return;
  87. }
  88. /* Fallback for older versions. */
  89. func_SetConsoleFont = (void *) GetProcAddress (kernel32, "SetConsoleFont");
  90. if (func_SetConsoleFont)
  91. {
  92. if (out_raster)
  93. func_SetConsoleFont (out_handle, 12);
  94. if (err_raster)
  95. func_SetConsoleFont (err_handle, 12);
  96. }
  97. }
  98. static char *grub_util_base_directory;
  99. static char *locale_dir;
  100. const char *
  101. grub_util_get_config_filename (void)
  102. {
  103. static char *value = NULL;
  104. if (!value)
  105. value = grub_util_path_concat (2, grub_util_base_directory, "grub.cfg");
  106. return value;
  107. }
  108. const char *
  109. grub_util_get_pkgdatadir (void)
  110. {
  111. return grub_util_base_directory;
  112. }
  113. const char *
  114. grub_util_get_localedir (void)
  115. {
  116. return locale_dir;
  117. }
  118. const char *
  119. grub_util_get_pkglibdir (void)
  120. {
  121. return grub_util_base_directory;
  122. }
  123. void
  124. grub_util_host_init (int *argc __attribute__ ((unused)),
  125. char ***argv)
  126. {
  127. char *ptr;
  128. SetConsoleOutputCP (CP_UTF8);
  129. SetConsoleCP (CP_UTF8);
  130. set_console_unicode_font ();
  131. #if SIZEOF_TCHAR == 1
  132. #elif SIZEOF_TCHAR == 2
  133. LPWSTR tcmdline = GetCommandLineW ();
  134. int i;
  135. LPWSTR *targv;
  136. targv = CommandLineToArgvW (tcmdline, argc);
  137. *argv = xmalloc ((*argc + 1) * sizeof (argv[0]));
  138. for (i = 0; i < *argc; i++)
  139. (*argv)[i] = grub_util_tchar_to_utf8 (targv[i]);
  140. (*argv)[i] = NULL;
  141. #else
  142. #error "Unsupported TCHAR size"
  143. #endif
  144. grub_util_base_directory = grub_canonicalize_file_name ((*argv)[0]);
  145. if (!grub_util_base_directory)
  146. grub_util_base_directory = xstrdup ((*argv)[0]);
  147. for (ptr = grub_util_base_directory + strlen (grub_util_base_directory) - 1;
  148. ptr >= grub_util_base_directory && *ptr != '/' && *ptr != '\\'; ptr--);
  149. if (ptr >= grub_util_base_directory)
  150. *ptr = '\0';
  151. locale_dir = grub_util_path_concat (2, grub_util_base_directory, "locale");
  152. set_program_name ((*argv)[0]);
  153. #if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  154. setlocale (LC_ALL, "");
  155. bindtextdomain (PACKAGE, locale_dir);
  156. textdomain (PACKAGE);
  157. #endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
  158. }