Adding upstream version 3.61.
[syslinux-debian/hramrach.git] / com32 / lib / sys / vesacon_write.c
blobbae52652212e1e9e803e80f27cc9f13b31259fac
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
29 * vesacon_write.c
31 * Write to the screen using ANSI control codes (about as capable as
32 * DOS' ANSI.SYS.)
35 #include <errno.h>
36 #include <string.h>
37 #include <com32.h>
38 #include <minmax.h>
39 #include <colortbl.h>
40 #include <console.h>
41 #include <klibc/compiler.h>
42 #include "ansi.h"
43 #include "file.h"
44 #include "vesa/video.h"
46 static void vesacon_erase(const struct term_state *, int, int, int, int);
47 static void vesacon_write_char(int, int, uint8_t, const struct term_state *);
48 static void vesacon_showcursor(const struct term_state *);
49 static void vesacon_scroll_up(const struct term_state *);
51 static struct term_state ts;
52 static struct ansi_ops op = {
53 .erase = vesacon_erase,
54 .write_char = vesacon_write_char,
55 .showcursor = vesacon_showcursor,
56 .set_cursor = __vesacon_set_cursor, /* in drawtxt.c */
57 .scroll_up = vesacon_scroll_up,
60 static struct term_info ti =
62 .cols = TEXT_PIXEL_COLS/FONT_WIDTH,
63 .disabled = 0,
64 .ts = &ts,
65 .op = &op
68 /* Reference counter to the screen, to keep track of if we need reinitialization. */
69 static int vesacon_counter = 0;
71 /* Common setup */
72 int __vesacon_open(struct file_info *fp)
74 static com32sys_t ireg; /* Auto-initalized to all zero */
75 com32sys_t oreg;
77 (void)fp;
79 if (!vesacon_counter) {
80 /* Are we disabled? */
81 ireg.eax.w[0] = 0x000b;
82 __intcall(0x22, &ireg, &oreg);
84 if ( (signed char)oreg.ebx.b[1] < 0 ) {
85 ti.disabled = 1;
86 } else {
87 /* Switch mode */
88 if (__vesacon_init()) {
89 vesacon_counter = -1;
90 return EAGAIN;
93 /* Initial state */
94 __ansi_init(&ti);
95 ti.rows = __vesacon_text_rows;
97 } else if (vesacon_counter == -1) {
98 return EAGAIN;
101 fp->o.rows = ti.rows;
102 fp->o.cols = ti.cols;
104 vesacon_counter++;
105 return 0;
108 int __vesacon_close(struct file_info *fp)
110 (void)fp;
112 vesacon_counter--;
113 return 0;
116 /* Erase a region of the screen */
117 static void vesacon_erase(const struct term_state *st,
118 int x0, int y0, int x1, int y1)
120 __vesacon_erase(x0, y0, x1, y1, st->cindex);
123 /* Draw text on the screen */
124 static void vesacon_write_char(int x, int y, uint8_t ch,
125 const struct term_state *st)
127 __vesacon_write_char(x, y, ch, st->cindex);
130 /* Show or hide the cursor */
131 static void vesacon_showcursor(const struct term_state *st)
133 __vesacon_set_cursor(st->xy.x, st->xy.y, st->cursor);
136 static void vesacon_scroll_up(const struct term_state *st)
138 __vesacon_scroll_up(1, st->cindex);
141 ssize_t __vesacon_write(struct file_info *fp, const void *buf, size_t count)
143 const unsigned char *bufp = buf;
144 size_t n = 0;
146 (void)fp;
148 if ( ti.disabled )
149 return n; /* Nothing to do */
151 /* This only updates the shadow text buffer... */
152 while ( count-- ) {
153 __ansi_putchar(&ti, *bufp++);
154 n++;
157 /* This actually draws it */
158 __vesacon_doit();
160 return n;
163 const struct output_dev dev_vesacon_w = {
164 .dev_magic = __DEV_MAGIC,
165 .flags = __DEV_TTY | __DEV_OUTPUT,
166 .fileflags = O_WRONLY | O_CREAT | O_TRUNC | O_APPEND,
167 .write = __vesacon_write,
168 .close = __vesacon_close,
169 .open = __vesacon_open,
170 .fallback = &dev_ansicon_w,