~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/osdep/windows/init.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
#include <config.h>
 
20
#include <config-util.h>
 
21
#include <grub/util/misc.h>
 
22
#include <grub/osdep/hostfile.h>
 
23
#include <grub/util/windows.h>
 
24
#include <grub/emu/config.h>
 
25
 
 
26
#include <wincon.h>
 
27
#include <windows.h>
 
28
 
 
29
#include <grub/util/misc.h>
 
30
 
 
31
#include "progname.h"
 
32
 
 
33
struct grub_windows_console_font_infoex {
 
34
  ULONG cbSize;
 
35
  DWORD nFont;
 
36
  COORD dwFontSize;
 
37
  UINT  FontFamily;
 
38
  UINT  FontWeight;
 
39
  WCHAR FaceName[LF_FACESIZE];
 
40
};
 
41
 
 
42
static int
 
43
check_is_raster (HMODULE kernel32, HANDLE hnd)
 
44
{
 
45
  CONSOLE_FONT_INFO console_font_info;
 
46
  BOOL (WINAPI * func_GetCurrentConsoleFont) (HANDLE, BOOL,
 
47
                                              PCONSOLE_FONT_INFO);
 
48
 
 
49
  func_GetCurrentConsoleFont = (void *)
 
50
    GetProcAddress (kernel32, "GetCurrentConsoleFont");
 
51
 
 
52
  if (!func_GetCurrentConsoleFont)
 
53
    return 1;
 
54
 
 
55
  if (!func_GetCurrentConsoleFont (hnd, FALSE, &console_font_info))
 
56
    return 1;
 
57
  return console_font_info.nFont < 12;
 
58
}
 
59
 
 
60
static void
 
61
set_console_unicode_font (void)
 
62
{
 
63
  BOOL (WINAPI * func_SetCurrentConsoleFontEx) (HANDLE, BOOL,
 
64
                                                struct grub_windows_console_font_infoex *);
 
65
  BOOL (WINAPI * func_SetConsoleFont)(HANDLE, DWORD);
 
66
  HMODULE kernel32;
 
67
  HANDLE out_handle = GetStdHandle (STD_OUTPUT_HANDLE);
 
68
  HANDLE err_handle = GetStdHandle (STD_ERROR_HANDLE);
 
69
  int out_raster, err_raster;
 
70
 
 
71
  kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
 
72
  if (!kernel32)
 
73
    return;
 
74
 
 
75
  out_raster = check_is_raster (kernel32, out_handle);
 
76
  err_raster = check_is_raster (kernel32, err_handle);
 
77
 
 
78
  if (!out_raster && !err_raster)
 
79
    return;
 
80
 
 
81
  func_SetCurrentConsoleFontEx = (void *) GetProcAddress (kernel32, "SetCurrentConsoleFontEx");
 
82
 
 
83
  /* Newer windows versions.  */
 
84
  if (func_SetCurrentConsoleFontEx)
 
85
    {
 
86
      struct grub_windows_console_font_infoex new_console_font_info;
 
87
      new_console_font_info.cbSize = sizeof (new_console_font_info);
 
88
      new_console_font_info.nFont = 12;
 
89
      new_console_font_info.dwFontSize.X = 7;
 
90
      new_console_font_info.dwFontSize.Y = 12;
 
91
      new_console_font_info.FontFamily = FF_DONTCARE;
 
92
      new_console_font_info.FontWeight = 400;
 
93
      memcpy (new_console_font_info.FaceName, TEXT("Lucida Console"),
 
94
              sizeof (TEXT("Lucida Console")));
 
95
      if (out_raster)
 
96
        func_SetCurrentConsoleFontEx (out_handle, FALSE,
 
97
                                      &new_console_font_info);
 
98
      if (err_raster)
 
99
        func_SetCurrentConsoleFontEx (err_handle, FALSE,
 
100
                                      &new_console_font_info);
 
101
      return;
 
102
    }
 
103
 
 
104
  /* Fallback for older versions.  */
 
105
  func_SetConsoleFont = (void *) GetProcAddress (kernel32, "SetConsoleFont");
 
106
  if (func_SetConsoleFont)
 
107
    {
 
108
      if (out_raster)
 
109
        func_SetConsoleFont (out_handle, 12);
 
110
      if (err_raster)
 
111
        func_SetConsoleFont (err_handle, 12);
 
112
    }
 
113
}
 
114
 
 
115
static char *grub_util_base_directory;
 
116
static char *locale_dir;
 
117
 
 
118
const char *
 
119
grub_util_get_config_filename (void)
 
120
{
 
121
  static char *value = NULL;
 
122
  if (!value)
 
123
    value = grub_util_path_concat (2, grub_util_base_directory, "grub.cfg");
 
124
  return value;
 
125
}
 
126
 
 
127
const char *
 
128
grub_util_get_pkgdatadir (void)
 
129
{
 
130
  return grub_util_base_directory;
 
131
}
 
132
 
 
133
const char *
 
134
grub_util_get_localedir (void)
 
135
{
 
136
  return locale_dir;
 
137
}
 
138
 
 
139
const char *
 
140
grub_util_get_pkglibdir (void)
 
141
{
 
142
  return grub_util_base_directory;
 
143
}
 
144
 
 
145
void
 
146
grub_util_host_init (int *argc __attribute__ ((unused)),
 
147
                     char ***argv)
 
148
{
 
149
  char *ptr;
 
150
 
 
151
  SetConsoleOutputCP (CP_UTF8);
 
152
  SetConsoleCP (CP_UTF8);
 
153
 
 
154
  set_console_unicode_font ();
 
155
 
 
156
#if SIZEOF_TCHAR == 1
 
157
 
 
158
#elif SIZEOF_TCHAR == 2
 
159
  LPWSTR tcmdline = GetCommandLineW ();
 
160
  int i;
 
161
  LPWSTR *targv;
 
162
 
 
163
  targv = CommandLineToArgvW (tcmdline, argc);
 
164
  *argv = xmalloc ((*argc + 1) * sizeof (argv[0]));
 
165
 
 
166
  for (i = 0; i < *argc; i++)
 
167
    (*argv)[i] = grub_util_tchar_to_utf8 (targv[i]); 
 
168
  (*argv)[i] = NULL;
 
169
#else
 
170
#error "Unsupported TCHAR size"
 
171
#endif
 
172
 
 
173
  grub_util_base_directory = canonicalize_file_name ((*argv)[0]);
 
174
  if (!grub_util_base_directory)
 
175
    grub_util_base_directory = xstrdup ((*argv)[0]);
 
176
  for (ptr = grub_util_base_directory + strlen (grub_util_base_directory) - 1;
 
177
       ptr >= grub_util_base_directory && *ptr != '/' && *ptr != '\\'; ptr--);
 
178
  if (ptr >= grub_util_base_directory)
 
179
    *ptr = '\0';
 
180
 
 
181
  locale_dir = grub_util_path_concat (2, grub_util_base_directory, "locale");
 
182
 
 
183
  set_program_name ((*argv)[0]);
 
184
 
 
185
#if (defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
 
186
  setlocale (LC_ALL, "");
 
187
  bindtextdomain (PACKAGE, locale_dir);
 
188
  textdomain (PACKAGE);
 
189
#endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
 
190
}