term.c 2.85 KB
Newer Older
okuji's avatar
okuji committed
1
/*
2
 *  GRUB  --  GRand Unified Bootloader
3
 *  Copyright (C) 2002,2003,2005,2007,2008,2009  Free Software Foundation, Inc.
okuji's avatar
okuji committed
4
 *
5
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
8 9
 *  (at your option) any later version.
 *
10
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
11 12 13 14 15
 *  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
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
17 18
 */

19 20 21 22
#include <grub/term.h>
#include <grub/err.h>
#include <grub/mm.h>
#include <grub/misc.h>
23
#include <grub/env.h>
24
#include <grub/time.h>
okuji's avatar
okuji committed
25

26 27
struct grub_term_output *grub_term_outputs_disabled;
struct grub_term_input *grub_term_inputs_disabled;
28
struct grub_term_output *grub_term_outputs;
29
struct grub_term_input *grub_term_inputs;
30

31
/* Current color state.  */
32 33
grub_uint8_t grub_term_normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR;
grub_uint8_t grub_term_highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR;
34

35
void (*grub_term_poll_usb) (int wait_for_completion) = NULL;
36
void (*grub_net_poll_cards_idle) (void) = NULL;
37

38
/* Put a Unicode character.  */
39 40 41
static void
grub_putcode_dumb (grub_uint32_t code,
		   struct grub_term_output *term)
okuji's avatar
okuji committed
42
{
43 44 45 46 47 48
  struct grub_unicode_glyph c =
    {
      .base = code,
      .variant = 0,
      .attributes = 0,
      .ncomb = 0,
49
      .estimated_width = 1
50 51
    };

52
  if (code == '\t' && term->getxy)
okuji's avatar
okuji committed
53 54
    {
      int n;
55

56
      n = GRUB_TERM_TAB_WIDTH - ((term->getxy (term).x)
57
				 % GRUB_TERM_TAB_WIDTH);
okuji's avatar
okuji committed
58
      while (n--)
59
	grub_putcode_dumb (' ', term);
okuji's avatar
okuji committed
60 61 62

      return;
    }
63

64
  (term->putchar) (term, &c);
65
  if (code == '\n')
66
    grub_putcode_dumb ('\r', term);
67 68
}

69 70
static void
grub_xputs_dumb (const char *str)
71
{
72
  for (; *str; str++)
73
    {
74
      grub_term_output_t term;
75 76 77
      grub_uint32_t code = *str;
      if (code > 0x7f)
	code = '?';
78

79
      FOR_ACTIVE_TERM_OUTPUTS(term)
80
	grub_putcode_dumb (code, term);
81
    }
okuji's avatar
okuji committed
82 83
}

84 85
void (*grub_xputs) (const char *str) = grub_xputs_dumb;

86
int
87
grub_getkey_noblock (void)
88 89 90
{
  grub_term_input_t term;

91
  if (grub_term_poll_usb)
92
    grub_term_poll_usb (0);
93

94 95 96
  if (grub_net_poll_cards_idle)
    grub_net_poll_cards_idle ();

97 98
  FOR_ACTIVE_TERM_INPUTS(term)
  {
99 100 101
    int key = term->getkey (term);
    if (key != GRUB_TERM_NO_KEY)
      return key;
102 103
  }

104
  return GRUB_TERM_NO_KEY;
105
}
okuji's avatar
okuji committed
106

107 108 109
int
grub_getkey (void)
{
110 111
  int ret;

112 113
  grub_refresh ();

114
  while (1)
115
    {
116 117 118
      ret = grub_getkey_noblock ();
      if (ret != GRUB_TERM_NO_KEY)
	return ret;
119 120 121 122
      grub_cpu_idle ();
    }
}

123
void
124
grub_refresh (void)
125
{
126
  struct grub_term_output *term;
127

128 129
  FOR_ACTIVE_TERM_OUTPUTS(term)
    grub_term_refresh (term);
130
}