summaryrefslogtreecommitdiffstats
path: root/grub-core/osdep/windows/init.c
blob: 6297de6326af066b7e83f47c46c3cfaa5fd6c68e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <config-util.h>
#include <grub/util/misc.h>
#include <grub/osdep/hostfile.h>
#include <grub/util/windows.h>
#include <grub/emu/config.h>

#include <wincon.h>
#include <windows.h>

#include <grub/util/misc.h>

#include "progname.h"

struct grub_windows_console_font_infoex {
  ULONG cbSize;
  DWORD nFont;
  COORD dwFontSize;
  UINT  FontFamily;
  UINT  FontWeight;
  WCHAR FaceName[LF_FACESIZE];
};

static int
check_is_raster (HMODULE kernel32, HANDLE hnd)
{
  CONSOLE_FONT_INFO console_font_info;
  BOOL (WINAPI * func_GetCurrentConsoleFont) (HANDLE, BOOL,
					      PCONSOLE_FONT_INFO);

  func_GetCurrentConsoleFont = (void *)
    GetProcAddress (kernel32, "GetCurrentConsoleFont");

  if (!func_GetCurrentConsoleFont)
    return 1;

  if (!func_GetCurrentConsoleFont (hnd, FALSE, &console_font_info))
    return 1;
  return console_font_info.nFont < 12;
}

static void
set_console_unicode_font (void)
{
  BOOL (WINAPI * func_SetCurrentConsoleFontEx) (HANDLE, BOOL,
						struct grub_windows_console_font_infoex *);
  BOOL (WINAPI * func_SetConsoleFont)(HANDLE, DWORD);
  HMODULE kernel32;
  HANDLE out_handle = GetStdHandle (STD_OUTPUT_HANDLE);
  HANDLE err_handle = GetStdHandle (STD_ERROR_HANDLE);
  int out_raster, err_raster;

  kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  if (!kernel32)
    return;

  out_raster = check_is_raster (kernel32, out_handle);
  err_raster = check_is_raster (kernel32, err_handle);

  if (!out_raster && !err_raster)
    return;

  func_SetCurrentConsoleFontEx = (void *) GetProcAddress (kernel32, "SetCurrentConsoleFontEx");

  /* Newer windows versions.  */
  if (func_SetCurrentConsoleFontEx)
    {
      struct grub_windows_console_font_infoex new_console_font_info;
      new_console_font_info.cbSize = sizeof (new_console_font_info);
      new_console_font_info.nFont = 12;
      new_console_font_info.dwFontSize.X = 7;
      new_console_font_info.dwFontSize.Y = 12;
      new_console_font_info.FontFamily = FF_DONTCARE;
      new_console_font_info.FontWeight = 400;
      memcpy (new_console_font_info.FaceName, TEXT("Lucida Console"),
	      sizeof (TEXT("Lucida Console")));
      if (out_raster)
	func_SetCurrentConsoleFontEx (out_handle, FALSE,
				      &new_console_font_info);
      if (err_raster)
	func_SetCurrentConsoleFontEx (err_handle, FALSE,
				      &new_console_font_info);
      return;
    }

  /* Fallback for older versions.  */
  func_SetConsoleFont = (void *) GetProcAddress (kernel32, "SetConsoleFont");
  if (func_SetConsoleFont)
    {
      if (out_raster)
	func_SetConsoleFont (out_handle, 12);
      if (err_raster)
	func_SetConsoleFont (err_handle, 12);
    }
}

static char *grub_util_base_directory;
static char *locale_dir;

const char *
grub_util_get_config_filename (void)
{
  static char *value = NULL;
  if (!value)
    value = grub_util_path_concat (2, grub_util_base_directory, "grub.cfg");
  return value;
}

const char *
grub_util_get_pkgdatadir (void)
{
  return grub_util_base_directory;
}

const char *
grub_util_get_localedir (void)
{
  return locale_dir;
}

const char *
grub_util_get_pkglibdir (void)
{
  return grub_util_base_directory;
}

void
grub_util_host_init (int *argc __attribute__ ((unused)),
		     char ***argv)
{
  char *ptr;

  SetConsoleOutputCP (CP_UTF8);
  SetConsoleCP (CP_UTF8);

  set_console_unicode_font ();

#if SIZEOF_TCHAR == 1

#elif SIZEOF_TCHAR == 2
  LPWSTR tcmdline = GetCommandLineW ();
  int i;
  LPWSTR *targv;

  targv = CommandLineToArgvW (tcmdline, argc);
  *argv = xcalloc (*argc + 1, sizeof (argv[0]));

  for (i = 0; i < *argc; i++)
    (*argv)[i] = grub_util_tchar_to_utf8 (targv[i]); 
  (*argv)[i] = NULL;
#else
#error "Unsupported TCHAR size"
#endif

  grub_util_base_directory = grub_canonicalize_file_name ((*argv)[0]);
  if (!grub_util_base_directory)
    grub_util_base_directory = xstrdup ((*argv)[0]);
  for (ptr = grub_util_base_directory + strlen (grub_util_base_directory) - 1;
       ptr >= grub_util_base_directory && *ptr != '/' && *ptr != '\\'; ptr--);
  if (ptr >= grub_util_base_directory)
    *ptr = '\0';

  locale_dir = grub_util_path_concat (2, grub_util_base_directory, "locale");

  set_program_name ((*argv)[0]);

#if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, locale_dir);
  textdomain (PACKAGE);
#endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
}