GadgetSeed  0.9.6
grconsole.c
[詳解]
1 /** @file
2  @brief グラフィックコンソールドライバ
3 
4  グラフィックコンソールドライバはグラフィックライブラリを使用したコ
5  ンソールドライバです。<br>
6 
7  register()実行時にコンソールに使用するフォント名を指定することが出
8  来ます。省略時はフォント名"8x16"を使用ます。<br>
9 
10  register()時に指定されたフォントにより行幅、行高さが決定されます。
11 
12  グラフィックコンソールドライバは実験的なドライバであり未完成でです。
13 
14  @date 2008.01.04
15  @author Takashi SHUDO
16 
17  @todo 漢字表示を可能にする。
18 */
19 
20 #include "device.h"
21 #include "graphics.h"
22 #include "font.h"
23 #include "tkprintf.h"
24 
25 //#define DEBUGKLVL 1
26 #include "dkprintf.h"
27 
28 
29 #define SCREEN_WIDTH GRAPH_WIDTH
30 #define SCREEN_HEIGHT GRAPH_HEIGHT
31 #define CONSOLE_WIDTH (SCREEN_WIDTH/fnt_width)
32 #define CONSOLE_HEIGHT (SCREEN_HEIGHT/fnt_height)
33 
34 static short screen_width;
35 static short screen_height;
36 static short console_width;
37 static short console_height;
38 
39 static short fnt_width;
40 static short fnt_height;
41 static long grc_addr;
42 static short cur_x;
43 static short cur_y;
44 static unsigned char esc;
45 static struct st_fontset *fset;
46 static struct st_rect screct;
47 static struct st_rect blrect;
48 #ifdef GSC_FONTS_ENABLE_KANJI
49 #ifdef GSC_FONTS_ENABLE_FONT_JISKAN16
50 static const char def_font[] = "jiskan16";
51 #else
52 static const char def_font[] = "8x16";
53 #endif
54 #else
55 static const char def_font[] = "8x16";
56 #endif
57 
58 extern const struct st_device grconsole_device;
59 
60 static int grconsole_register(struct st_device *dev, char *param)
61 {
62  grc_addr = 0;
63  cur_x = 0;
64  cur_y = 0;
65  esc = 0;
66  char *fname = (char *)def_font;
67 
68  if(param != 0) {
69  fname = param;
70  }
71 
72  fset = set_font_by_name(fname);
73 
74  if(fset == 0) {
75  SYSERR_PRINT("Cannot set font \"%s\".\n", fname);
76  return -1;
77  }
78 
79  fnt_width = fset->font->width;
80  fnt_height = fset->font->height;
81 
82  get_screen_info(&screen_width, &screen_height);
83  console_width = screen_width/fnt_width;
84  console_height = screen_height/fnt_height;
85 
86  screct.left = 0;
87  screct.right = screen_width;
88  screct.top = 0;
89  screct.bottom = screen_height;
90 
91  blrect.left = 0;
92  blrect.right = screen_width;
93  blrect.top = fnt_height * (console_height - 1);
94  blrect.bottom = fnt_height * console_height;
95 
96  tkprintf("Console \"%s\" %dx%d, font \"%s\"\n", grconsole_device.name,
97  console_width, console_height, fname);
98 
99  return 0;
100 }
101 
102 static void draw_cursor(unsigned char on)
103 {
104  struct st_rect rect;
105 
106  rect.left = cur_x * fnt_width;
107  rect.right = (cur_x + 1) * fnt_width;
108  rect.top = cur_y * fnt_height;
109  rect.bottom = (cur_y + 1) * fnt_height;
110 
111  if(on) {
113  } else {
115  }
116 
117  draw_fill_rect(&rect);
118 }
119 
120 static void next_line(void)
121 {
122  cur_y ++;
123  if(cur_y >= console_height) {
124  cur_y = (console_height - 1);
125  clear_clip_rect();
126  scroll_rect_v(&screct, -fnt_height);
128  draw_fill_rect(&blrect);
130  }
131 }
132 
133 static void draw_ch(unsigned short x, unsigned short y, unsigned short ch)
134 {
135  draw_char(x * fnt_width, y * fnt_height, ch);
136 }
137 
138 static void put_char(unsigned char ch)
139 {
140  switch(ch) {
141  case '\r':
142  draw_cursor(0);
143  cur_x = 0;
144  return;
145  break;
146 
147  case '\n':
148  cur_x = 0;
149  next_line();
150  break;
151 
152  case 0x08: // CTRL-H
153  //draw_cursor(0);
154  cur_x --;
155  if(cur_x < 0) {
156  cur_x = (console_width - 1);
157  cur_y --;
158  if(cur_y < 0) {
159  cur_y = 0;
160  }
161  }
162  break;
163 
164  default:
165  if(cur_x >= (console_width-1)) {
166  cur_x = 0;
167  next_line();
168  }
169  draw_ch(cur_x, cur_y, ch);
170  cur_x ++;
171  if(cur_x >= console_width) {
172  cur_x = 0;
173  next_line();
174  }
175  }
176 
177  draw_cursor(1);
178 }
179 
180 /*
181  *
182  */
183 
184 static int grconsole_open(struct st_device *dev)
185 {
186  draw_cursor(1);
187 
188  return 0;
189 }
190 
191 static int grconsole_ioctl(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
192 {
193  switch(com) {
194  case 0x2000:
195  grconsole_register(dev, (char *)param);
196  break;
197  }
198 
199  return 0;
200 }
201 
202 static int grconsole_putc(struct st_device *dev, unsigned char data)
203 {
204  int i;
205 
206  switch(esc) {
207  case 0:
208  if(data == 0x1b) { // ESC
209  esc = 1;
210  } else {
211  put_char(data);
212  }
213  break;
214  case 1:
215  if(data == '[') {
216  esc = 2;
217  } else {
218  esc = 0;
219  }
220  break;
221 
222  case 2:
223  switch(data) {
224  case 'K': // 右消す
225  draw_cursor(1);
226  for(i=cur_x; i<console_width; i++) {
227  draw_ch(i, cur_y, ' ');
228  }
229  draw_cursor(1);
230  break;
231  }
232  esc = 0;
233  break;
234  }
235 
236 // #include "tprintf.h"
237 // tprintf("x=%d, y=%d\n", cur_x, cur_y);
238 
239  return 1;
240 }
241 
242 static int grconsole_seek(struct st_device *dev, int offset, int whence)
243 {
244  switch(whence) {
245  case SEEK_SET:
246  grc_addr = offset;
247  break;
248 
249  case SEEK_CUR:
250  grc_addr += offset;
251  break;
252 
253  case SEEK_END:
254  default:
255  return -1;
256  }
257 
258  cur_x = grc_addr % console_width;
259  cur_y = grc_addr / console_height;
260 
261  return grc_addr;
262 }
263 
264 const struct st_device grconsole_device = {
265  .name = "grcon",
266  .explan = "Graphic console",
267  .register_dev = grconsole_register,
268  .open = grconsole_open,
269  .ioctl = grconsole_ioctl,
270  .putc = grconsole_putc,
271  .seek = grconsole_seek,
272 };
void clear_clip_rect(void)
クリッピングエリアを無効にする
Definition: graphics.c:254
#define GRP_DRAWMODE_NORMAL
Draw the foreground with fore color. Draw the background with back color.
Definition: graphics.h:60
矩形
Definition: graphics.h:64
short top
左上頂点のY座標
Definition: graphics.h:66
short bottom
右下頂点のY座標
Definition: graphics.h:68
short left
左上頂点のX座標
Definition: graphics.h:65
フォントセット
Definition: fontdata.h:23
#define GRP_DRAWMODE_REVERSE
Draw the foreground with back color. Draw the background with fore color.
Definition: graphics.h:61
void get_screen_info(short *width, short *height)
スクリーンのサイズ情報を取得する
Definition: graphics.c:329
unsigned short height
1文字の高さ(ピクセル数)
Definition: fontdata.h:17
int tkprintf(const char *fmt,...)
非タスクコンテキスト実行用メッセージ出力
Definition: tkprintf.c:100
short right
右下頂点のX座標
Definition: graphics.h:67
void scroll_rect_v(struct st_rect *rect, short pixcel)
矩形範囲を縦方向にスクロールする
Definition: graphics.c:1882
カーネル用機能限定printf
struct st_fontset * set_font_by_name(char *name)
カレントフォントセットをフォント名で設定する
Definition: font.c:226
void draw_fill_rect(struct st_rect *rect)
塗りつぶした矩形を描画する
Definition: graphics.c:821
void set_draw_mode(unsigned char mode)
描画モードを設定する
Definition: graphics.c:414
unsigned short draw_char(short x, short y, ushort ch)
文字を描画する
Definition: font.c:433
struct st_font * font
半角フォントデータポインタ
Definition: fontdata.h:25
#define SEEK_SET
設定
Definition: device.h:21
unsigned short width
1文字の幅(ピクセル数)
Definition: fontdata.h:16
デバイスドライバAPI
デバイスドライバ構造体
Definition: device.h:25
カーネル、ドライバ(非タスク)デバッグ用マクロ
#define SEEK_END
ファイルサイズに加算
Definition: device.h:23
char name[MAX_DEVNAMELRN]
デバイス名文字列
Definition: device.h:26
グラフィックライブラリ
フォント
#define SEEK_CUR
現在値に加算
Definition: device.h:22