~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to kern/term.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2002,2003,2005  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 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program 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, write to the Free Software
 
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 */
 
19
 
 
20
#include <grub/term.h>
 
21
#include <grub/err.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/misc.h>
 
24
#include <grub/env.h>
 
25
 
 
26
/* The list of terminals.  */
 
27
static grub_term_t grub_term_list;
 
28
 
 
29
/* The current terminal.  */
 
30
static grub_term_t grub_cur_term;
 
31
 
 
32
/* The amount of lines counted by the pager.  */
 
33
static int grub_more_lines;
 
34
 
 
35
/* If the more pager is active.  */
 
36
static int grub_more;
 
37
 
 
38
/* The current cursor state.  */
 
39
static int cursor_state = 1;
 
40
 
 
41
void
 
42
grub_term_register (grub_term_t term)
 
43
{
 
44
  term->next = grub_term_list;
 
45
  grub_term_list = term;
 
46
}
 
47
 
 
48
void
 
49
grub_term_unregister (grub_term_t term)
 
50
{
 
51
  grub_term_t *p, q;
 
52
  
 
53
  for (p = &grub_term_list, q = *p; q; p = &(q->next), q = q->next)
 
54
    if (q == term)
 
55
      {
 
56
        *p = q->next;
 
57
        break;
 
58
      }
 
59
}
 
60
 
 
61
void
 
62
grub_term_iterate (int (*hook) (grub_term_t term))
 
63
{
 
64
  grub_term_t p;
 
65
  
 
66
  for (p = grub_term_list; p; p = p->next)
 
67
    if (hook (p))
 
68
      break;
 
69
}
 
70
 
 
71
grub_err_t
 
72
grub_term_set_current (grub_term_t term)
 
73
{
 
74
  if (grub_cur_term && grub_cur_term->fini)
 
75
    if ((grub_cur_term->fini) () != GRUB_ERR_NONE)
 
76
      return grub_errno;
 
77
 
 
78
  if (term->init)
 
79
    if ((term->init) () != GRUB_ERR_NONE)
 
80
      return grub_errno;
 
81
  
 
82
  grub_cur_term = term;
 
83
  grub_cls ();
 
84
  grub_setcursor (grub_getcursor ());
 
85
  return GRUB_ERR_NONE;
 
86
}
 
87
 
 
88
grub_term_t
 
89
grub_term_get_current (void)
 
90
{
 
91
  return grub_cur_term;
 
92
}
 
93
 
 
94
/* Put a Unicode character.  */
 
95
void
 
96
grub_putcode (grub_uint32_t code)
 
97
{
 
98
  int height = grub_getwh () & 255;
 
99
 
 
100
  if (code == '\t' && grub_cur_term->getxy)
 
101
    {
 
102
      int n;
 
103
      
 
104
      n = 8 - ((grub_getxy () >> 8) & 7);
 
105
      while (n--)
 
106
        grub_putcode (' ');
 
107
 
 
108
      return;
 
109
    }
 
110
  
 
111
  (grub_cur_term->putchar) (code);
 
112
  
 
113
  if (code == '\n')
 
114
    {
 
115
      grub_putcode ('\r');
 
116
 
 
117
      grub_more_lines++;
 
118
 
 
119
      if (grub_more && grub_more_lines == height - 1)
 
120
        {
 
121
          char key;
 
122
          int pos = grub_getxy ();
 
123
 
 
124
          /* Show --MORE-- on the lower left side of the screen.  */
 
125
          grub_gotoxy (1, height - 1);
 
126
          grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
 
127
          grub_printf ("--MORE--");
 
128
          grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
 
129
 
 
130
          key = grub_getkey ();
 
131
          
 
132
          /* Remove the message.  */
 
133
          grub_gotoxy (1, height - 1);
 
134
          grub_printf ("        ");
 
135
          grub_gotoxy (pos >> 8, pos & 0xFF);
 
136
          
 
137
          /* Scroll one lines or an entire page, depending on the key.  */
 
138
          if (key == '\r' || key =='\n')
 
139
            grub_more_lines--;
 
140
          else
 
141
            grub_more_lines = 0;
 
142
        }
 
143
    }
 
144
}
 
145
 
 
146
/* Put a character. C is one byte of a UTF-8 stream.
 
147
   This function gathers bytes until a valid Unicode character is found.  */
 
148
void
 
149
grub_putchar (int c)
 
150
{
 
151
  static grub_size_t size = 0;
 
152
  static grub_uint8_t buf[6];
 
153
  grub_uint32_t code;
 
154
  grub_ssize_t ret;
 
155
 
 
156
  buf[size++] = c;
 
157
  ret = grub_utf8_to_ucs4 (&code, buf, size);
 
158
  
 
159
  if (ret > 0)
 
160
    {
 
161
      size = 0;
 
162
      grub_putcode (code);
 
163
    }
 
164
  else if (ret < 0)
 
165
    {
 
166
      size = 0;
 
167
      grub_putcode ('?');
 
168
    }
 
169
}
 
170
 
 
171
/* Return the number of columns occupied by the character code CODE.  */
 
172
grub_ssize_t
 
173
grub_getcharwidth (grub_uint32_t code)
 
174
{
 
175
  return (grub_cur_term->getcharwidth) (code);
 
176
}
 
177
 
 
178
int
 
179
grub_getkey (void)
 
180
{
 
181
  return (grub_cur_term->getkey) ();
 
182
}
 
183
 
 
184
int
 
185
grub_checkkey (void)
 
186
{
 
187
  return (grub_cur_term->checkkey) ();
 
188
}
 
189
 
 
190
grub_uint16_t
 
191
grub_getxy (void)
 
192
{
 
193
  return (grub_cur_term->getxy) ();
 
194
}
 
195
 
 
196
grub_uint16_t
 
197
grub_getwh (void)
 
198
{
 
199
  return (grub_cur_term->getwh) ();
 
200
}
 
201
 
 
202
void
 
203
grub_gotoxy (grub_uint8_t x, grub_uint8_t y)
 
204
{
 
205
  (grub_cur_term->gotoxy) (x, y);
 
206
}
 
207
 
 
208
void
 
209
grub_cls (void)
 
210
{
 
211
  if ((grub_cur_term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
 
212
    {
 
213
      grub_putchar ('\n');
 
214
      grub_refresh ();
 
215
    }
 
216
  else
 
217
    (grub_cur_term->cls) ();
 
218
}
 
219
 
 
220
void
 
221
grub_setcolorstate (grub_term_color_state state)
 
222
{
 
223
  if (grub_cur_term->setcolorstate)
 
224
    (grub_cur_term->setcolorstate) (state);
 
225
}
 
226
 
 
227
void
 
228
grub_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
 
229
{
 
230
  if (grub_cur_term->setcolor)
 
231
    (grub_cur_term->setcolor) (normal_color, highlight_color);
 
232
}
 
233
 
 
234
int
 
235
grub_setcursor (int on)
 
236
{
 
237
  int ret = cursor_state;
 
238
 
 
239
  if (grub_cur_term->setcursor)
 
240
    {
 
241
      (grub_cur_term->setcursor) (on);
 
242
      cursor_state = on;
 
243
    }
 
244
  
 
245
  return ret;
 
246
}
 
247
 
 
248
int
 
249
grub_getcursor (void)
 
250
{
 
251
  return cursor_state;
 
252
}
 
253
 
 
254
void
 
255
grub_refresh (void)
 
256
{
 
257
  if (grub_cur_term->refresh)
 
258
    (grub_cur_term->refresh) ();
 
259
}
 
260
 
 
261
void
 
262
grub_set_more (int onoff)
 
263
{
 
264
  if (onoff == 1)
 
265
    grub_more++;
 
266
  else
 
267
    grub_more--;
 
268
 
 
269
  grub_more_lines = 0;
 
270
}