wsdisplay_vcons.c revision 1.14 1 /* $NetBSD: wsdisplay_vcons.c,v 1.14 2007/08/06 03:11:32 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.14 2007/08/06 03:11:32 macallan Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/buf.h>
43 #include <sys/device.h>
44 #include <sys/ioctl.h>
45 #include <sys/malloc.h>
46 #include <sys/mman.h>
47 #include <sys/tty.h>
48 #include <sys/conf.h>
49 #include <sys/proc.h>
50 #include <sys/kthread.h>
51 #include <sys/tprintf.h>
52
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wsfont/wsfont.h>
56 #include <dev/rasops/rasops.h>
57
58 #include <dev/wscons/wsdisplay_vconsvar.h>
59
60 #include "opt_wsemul.h"
61 #include "opt_wsdisplay_compat.h"
62 #include "opt_vcons.h"
63
64 static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
65 long *);
66
67 static int vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
68 static int vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
69 int *, int *, long *);
70 static void vcons_free_screen(void *, void *);
71 static int vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
72 void *);
73
74 #ifdef WSDISPLAY_SCROLLSUPPORT
75 static void vcons_scroll(void *, void *, int);
76 static void vcons_do_scroll(struct vcons_screen *);
77 #endif
78
79 static void vcons_do_switch(struct vcons_data *);
80
81 /* methods that work only on text buffers */
82 static void vcons_copycols_buffer(void *, int, int, int, int);
83 static void vcons_erasecols_buffer(void *, int, int, int, long);
84 static void vcons_copyrows_buffer(void *, int, int, int);
85 static void vcons_eraserows_buffer(void *, int, int, long);
86 static void vcons_putchar_buffer(void *, int, int, u_int, long);
87
88 /*
89 * actual wrapper methods which call both the _buffer ones above and the
90 * driver supplied ones to do the drawing
91 */
92 static void vcons_copycols(void *, int, int, int, int);
93 static void vcons_erasecols(void *, int, int, int, long);
94 static void vcons_copyrows(void *, int, int, int);
95 static void vcons_eraserows(void *, int, int, long);
96 static void vcons_putchar(void *, int, int, u_int, long);
97 static void vcons_cursor(void *, int, int, int);
98
99 /* support for reading/writing text buffers. For wsmoused */
100 static int vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
101 static int vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
102
103 static void vcons_lock(struct vcons_screen *);
104 static void vcons_unlock(struct vcons_screen *);
105
106 #ifdef VCONS_SWITCH_ASYNC
107 static void vcons_kthread(void *);
108 #endif
109
110 int
111 vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
112 struct wsdisplay_accessops *ao)
113 {
114
115 /* zero out everything so we can rely on untouched fields being 0 */
116 memset(vd, 0, sizeof(struct vcons_data));
117
118 vd->cookie = cookie;
119
120 vd->init_screen = vcons_dummy_init_screen;
121 vd->show_screen_cb = NULL;
122
123 /* keep a copy of the accessops that we replace below with our
124 * own wrappers */
125 vd->ioctl = ao->ioctl;
126
127 /* configure the accessops */
128 ao->ioctl = vcons_ioctl;
129 ao->alloc_screen = vcons_alloc_screen;
130 ao->free_screen = vcons_free_screen;
131 ao->show_screen = vcons_show_screen;
132 #ifdef WSDISPLAY_SCROLLSUPPORT
133 ao->scroll = vcons_scroll;
134 #endif
135
136 LIST_INIT(&vd->screens);
137 vd->active = NULL;
138 vd->wanted = NULL;
139 vd->currenttype = def;
140 callout_init(&vd->switch_callout, 0);
141
142 /*
143 * a lock to serialize access to the framebuffer.
144 * when switching screens we need to make sure there's no rasops
145 * operation in progress
146 */
147 #ifdef DIAGNOSTIC
148 vd->switch_poll_count = 0;
149 #endif
150 #ifdef VCONS_SWITCH_ASYNC
151 kthread_create(PRI_NONE, 0, NULL, vcons_kthread, vd,
152 &vd->redraw_thread, "vcons_draw");
153 #endif
154 return 0;
155 }
156
157 static void
158 vcons_lock(struct vcons_screen *scr)
159 {
160 #ifdef VCONS_PARANOIA
161 int s;
162
163 s = splhigh();
164 #endif
165 SCREEN_BUSY(scr);
166 #ifdef VCONS_PARANOIA
167 splx(s);
168 #endif
169 }
170
171 static void
172 vcons_unlock(struct vcons_screen *scr)
173 {
174 #ifdef VCONS_PARANOIA
175 int s;
176
177 s = splhigh();
178 #endif
179 SCREEN_IDLE(scr);
180 #ifdef VCONS_PARANOIA
181 splx(s);
182 #endif
183 #ifdef VCONS_SWITCH_ASYNC
184 wakeup(&scr->scr_vd->done_drawing);
185 #endif
186 }
187
188 static void
189 vcons_dummy_init_screen(void *cookie,
190 struct vcons_screen *scr, int exists,
191 long *defattr)
192 {
193
194 /*
195 * default init_screen() method.
196 * Needs to be overwritten so we bitch and whine in case anyone ends
197 * up in here.
198 */
199 printf("vcons_init_screen: dummy function called. Your driver is "
200 "supposed to supply a replacement for proper operation\n");
201 }
202
203 int
204 vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
205 int existing, long *defattr)
206 {
207 struct rasops_info *ri = &scr->scr_ri;
208 int cnt, i;
209
210 scr->scr_cookie = vd->cookie;
211 scr->scr_vd = scr->scr_origvd = vd;
212 scr->scr_busy = 0;
213
214 /*
215 * call the driver-supplied init_screen function which is expected
216 * to set up rasops_info, override cursor() and probably others
217 */
218 vd->init_screen(vd->cookie, scr, existing, defattr);
219
220 /*
221 * save the non virtual console aware rasops and replace them with
222 * our wrappers
223 */
224 vd->eraserows = ri->ri_ops.eraserows;
225 vd->copyrows = ri->ri_ops.copyrows;
226 vd->erasecols = ri->ri_ops.erasecols;
227 vd->copycols = ri->ri_ops.copycols;
228 vd->putchar = ri->ri_ops.putchar;
229 vd->cursor = ri->ri_ops.cursor;
230
231 ri->ri_ops.eraserows = vcons_eraserows;
232 ri->ri_ops.copyrows = vcons_copyrows;
233 ri->ri_ops.erasecols = vcons_erasecols;
234 ri->ri_ops.copycols = vcons_copycols;
235 ri->ri_ops.putchar = vcons_putchar;
236 ri->ri_ops.cursor = vcons_cursor;
237 ri->ri_hw = scr;
238
239 /*
240 * we allocate both chars and attributes in one chunk, attributes first
241 * because they have the (potentially) bigger alignment
242 */
243 #ifdef WSDISPLAY_SCROLLSUPPORT
244 cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
245 scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
246 scr->scr_current_line = 0;
247 scr->scr_line_wanted = 0;
248 scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
249 scr->scr_current_offset = scr->scr_offset_to_zero;
250 #else
251 cnt = ri->ri_rows * ri->ri_cols;
252 #endif
253 scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
254 sizeof(uint16_t)), M_DEVBUF, M_WAITOK);
255 if (scr->scr_attrs == NULL)
256 return ENOMEM;
257
258 scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt];
259
260 ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
261 scr->scr_defattr = *defattr;
262
263 /*
264 * fill the attribute buffer with *defattr, chars with 0x20
265 * since we don't know if the driver tries to mimic firmware output or
266 * reset everything we do nothing to VRAM here, any driver that feels
267 * the need to clear screen or something will have to do it on its own
268 * Additional screens will start out in the background anyway so
269 * cleaning or not only really affects the initial console screen
270 */
271 for (i = 0; i < cnt; i++) {
272 scr->scr_attrs[i] = *defattr;
273 scr->scr_chars[i] = 0x20;
274 }
275
276 if(vd->active == NULL) {
277 vd->active = scr;
278 SCREEN_VISIBLE(scr);
279 }
280
281 if (existing) {
282 SCREEN_VISIBLE(scr);
283 vd->active = scr;
284 } else {
285 SCREEN_INVISIBLE(scr);
286 }
287
288 LIST_INSERT_HEAD(&vd->screens, scr, next);
289 return 0;
290 }
291
292 static void
293 vcons_do_switch(struct vcons_data *vd)
294 {
295 struct vcons_screen *scr, *oldscr;
296
297 scr = vd->wanted;
298 if (!scr) {
299 printf("vcons_switch_screen: disappeared\n");
300 vd->switch_cb(vd->switch_cb_arg, EIO, 0);
301 return;
302 }
303 oldscr = vd->active; /* can be NULL! */
304
305 /*
306 * if there's an old, visible screen we mark it invisible and wait
307 * until it's not busy so we can safely switch
308 */
309 if (oldscr != NULL) {
310 SCREEN_INVISIBLE(oldscr);
311 if (SCREEN_IS_BUSY(oldscr)) {
312 callout_reset(&vd->switch_callout, 1,
313 (void(*)(void *))vcons_do_switch, vd);
314 #ifdef DIAGNOSTIC
315 /* bitch if we wait too long */
316 vd->switch_poll_count++;
317 if (vd->switch_poll_count > 100) {
318 panic("vcons: screen still busy");
319 }
320 #endif
321 return;
322 }
323 /* invisible screen -> no visible cursor image */
324 oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
325 #ifdef DIAGNOSTIC
326 vd->switch_poll_count = 0;
327 #endif
328 }
329
330 if (scr == oldscr)
331 return;
332
333 #ifdef DIAGNOSTIC
334 if (SCREEN_IS_VISIBLE(scr))
335 printf("vcons_switch_screen: already active");
336 #endif
337
338 #ifdef notyet
339 if (vd->currenttype != type) {
340 vcons_set_screentype(vd, type);
341 vd->currenttype = type;
342 }
343 #endif
344
345 SCREEN_VISIBLE(scr);
346 vd->active = scr;
347 vd->wanted = NULL;
348
349 if (vd->show_screen_cb != NULL)
350 vd->show_screen_cb(scr);
351
352 if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
353 vcons_redraw_screen(scr);
354
355 if (vd->switch_cb)
356 vd->switch_cb(vd->switch_cb_arg, 0, 0);
357 }
358
359 void
360 vcons_redraw_screen(struct vcons_screen *scr)
361 {
362 uint16_t *charptr = scr->scr_chars;
363 long *attrptr = scr->scr_attrs;
364 struct rasops_info *ri = &scr->scr_ri;
365 int i, j, offset;
366
367 vcons_lock(scr);
368 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
369
370 /*
371 * only clear the screen when RI_FULLCLEAR is set since we're
372 * going to overwrite every single character cell anyway
373 */
374 if (ri->ri_flg & RI_FULLCLEAR) {
375 scr->scr_vd->eraserows(ri, 0, ri->ri_rows,
376 scr->scr_defattr);
377 }
378
379 /* redraw the screen */
380 #ifdef WSDISPLAY_SCROLLSUPPORT
381 offset = scr->scr_current_offset;
382 #else
383 offset = 0;
384 #endif
385 for (i = 0; i < ri->ri_rows; i++) {
386 for (j = 0; j < ri->ri_cols; j++) {
387 /*
388 * no need to use the wrapper function - we
389 * don't change any characters or attributes
390 * and we already made sure the screen we're
391 * working on is visible
392 */
393 scr->scr_vd->putchar(ri, i, j,
394 charptr[offset], attrptr[offset]);
395 offset++;
396 }
397 }
398 ri->ri_flg &= ~RI_CURSOR;
399 scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
400 }
401 vcons_unlock(scr);
402 }
403
404 static int
405 vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
406 struct lwp *l)
407 {
408 struct vcons_data *vd = v;
409 int error;
410
411 switch (cmd) {
412 case WSDISPLAYIO_GETWSCHAR:
413 error = vcons_getwschar((struct vcons_screen *)vs,
414 (struct wsdisplay_char *)data);
415 break;
416
417 case WSDISPLAYIO_PUTWSCHAR:
418 error = vcons_putwschar((struct vcons_screen *)vs,
419 (struct wsdisplay_char *)data);
420 break;
421
422 default:
423 if (vd->ioctl != NULL)
424 error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
425 else
426 error = EPASSTHROUGH;
427 }
428
429 return error;
430 }
431
432 static int
433 vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
434 int *curxp, int *curyp, long *defattrp)
435 {
436 struct vcons_data *vd = v;
437 struct vcons_screen *scr;
438 int ret;
439
440 scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
441 if (scr == NULL)
442 return ENOMEM;
443
444 scr->scr_flags = 0;
445 scr->scr_status = 0;
446 scr->scr_busy = 0;
447 scr->scr_type = type;
448
449 ret = vcons_init_screen(vd, scr, 0, defattrp);
450 if (ret != 0) {
451 free(scr, M_DEVBUF);
452 return ret;
453 }
454
455 if (vd->active == NULL) {
456 SCREEN_VISIBLE(scr);
457 vd->active = scr;
458 vd->currenttype = type;
459 }
460
461 *cookiep = scr;
462 *curxp = scr->scr_ri.ri_ccol;
463 *curyp = scr->scr_ri.ri_crow;
464 return 0;
465 }
466
467 static void
468 vcons_free_screen(void *v, void *cookie)
469 {
470 struct vcons_data *vd = v;
471 struct vcons_screen *scr = cookie;
472
473 vcons_lock(scr);
474 /* there should be no rasops activity here */
475
476 LIST_REMOVE(scr, next);
477
478 if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
479 free(scr->scr_attrs, M_DEVBUF);
480 free(scr, M_DEVBUF);
481 } else {
482 /*
483 * maybe we should just restore the old rasops_info methods
484 * and free the character/attribute buffer here?
485 */
486 #ifdef VCONS_DEBUG
487 panic("vcons_free_screen: console");
488 #else
489 printf("vcons_free_screen: console\n");
490 #endif
491 }
492
493 if (vd->active == scr)
494 vd->active = NULL;
495 }
496
497 static int
498 vcons_show_screen(void *v, void *cookie, int waitok,
499 void (*cb)(void *, int, int), void *cb_arg)
500 {
501 struct vcons_data *vd = v;
502 struct vcons_screen *scr;
503
504 scr = cookie;
505 if (scr == vd->active)
506 return 0;
507
508 vd->wanted = scr;
509 vd->switch_cb = cb;
510 vd->switch_cb_arg = cb_arg;
511 #ifdef VCONS_SWITCH_ASYNC
512 wakeup(&vd->start_drawing);
513 return EAGAIN;
514 #else
515 if (cb) {
516 callout_reset(&vd->switch_callout, 0,
517 (void(*)(void *))vcons_do_switch, vd);
518 return EAGAIN;
519 }
520
521 vcons_do_switch(vd);
522 return 0;
523 #endif
524 }
525
526 /* wrappers for rasops_info methods */
527
528 static void
529 vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
530 {
531 struct rasops_info *ri = cookie;
532 struct vcons_screen *scr = ri->ri_hw;
533 int from = srccol + row * ri->ri_cols;
534 int to = dstcol + row * ri->ri_cols;
535
536 #ifdef WSDISPLAY_SCROLLSUPPORT
537 int offset;
538 offset = scr->scr_offset_to_zero;
539
540 memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from],
541 ncols * sizeof(long));
542 memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from],
543 ncols * sizeof(uint16_t));
544 #else
545 memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
546 ncols * sizeof(long));
547 memmove(&scr->scr_chars[to], &scr->scr_chars[from],
548 ncols * sizeof(uint16_t));
549 #endif
550 }
551
552 static void
553 vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
554 {
555 struct rasops_info *ri = cookie;
556 struct vcons_screen *scr = ri->ri_hw;
557
558 vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
559
560 vcons_lock(scr);
561 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
562 scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
563 }
564 vcons_unlock(scr);
565 }
566
567 static void
568 vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
569 {
570 struct rasops_info *ri = cookie;
571 struct vcons_screen *scr = ri->ri_hw;
572 int start = startcol + row * ri->ri_cols;
573 int end = start + ncols, i;
574
575 #ifdef WSDISPLAY_SCROLLSUPPORT
576 int offset;
577 offset = scr->scr_offset_to_zero;
578
579 for (i = start; i < end; i++) {
580 scr->scr_attrs[offset + i] = fillattr;
581 scr->scr_chars[offset + i] = 0x20;
582 }
583 #else
584 for (i = start; i < end; i++) {
585 scr->scr_attrs[i] = fillattr;
586 scr->scr_chars[i] = 0x20;
587 }
588 #endif
589 }
590
591 static void
592 vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
593 {
594 struct rasops_info *ri = cookie;
595 struct vcons_screen *scr = ri->ri_hw;
596
597 vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
598
599 vcons_lock(scr);
600 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
601 scr->scr_vd->erasecols(cookie, row, startcol, ncols,
602 fillattr);
603 }
604 vcons_unlock(scr);
605 }
606
607 static void
608 vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
609 {
610 struct rasops_info *ri = cookie;
611 struct vcons_screen *scr = ri->ri_hw;
612 int from, to, len;
613
614 #ifdef WSDISPLAY_SCROLLSUPPORT
615 int offset;
616 offset = scr->scr_offset_to_zero;
617
618 /* do we need to scroll the back buffer? */
619 if (dstrow == 0) {
620 from = ri->ri_cols * srcrow;
621 to = ri->ri_cols * dstrow;
622
623 memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
624 scr->scr_offset_to_zero * sizeof(long));
625 memmove(&scr->scr_chars[to], &scr->scr_chars[from],
626 scr->scr_offset_to_zero * sizeof(uint16_t));
627 }
628 from = ri->ri_cols * srcrow + offset;
629 to = ri->ri_cols * dstrow + offset;
630 len = ri->ri_cols * nrows;
631
632 #else
633 from = ri->ri_cols * srcrow;
634 to = ri->ri_cols * dstrow;
635 len = ri->ri_cols * nrows;
636 #endif
637 memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
638 len * sizeof(long));
639 memmove(&scr->scr_chars[to], &scr->scr_chars[from],
640 len * sizeof(uint16_t));
641 }
642
643 static void
644 vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
645 {
646 struct rasops_info *ri = cookie;
647 struct vcons_screen *scr = ri->ri_hw;
648
649 vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
650
651 vcons_lock(scr);
652 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
653 scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
654 }
655 vcons_unlock(scr);
656 }
657
658 static void
659 vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
660 {
661 struct rasops_info *ri = cookie;
662 struct vcons_screen *scr = ri->ri_hw;
663 int start, end, i;
664
665 #ifdef WSDISPLAY_SCROLLSUPPORT
666 int offset;
667 offset = scr->scr_offset_to_zero;
668
669 start = ri->ri_cols * row + offset;
670 end = ri->ri_cols * (row + nrows) + offset;
671 #else
672 start = ri->ri_cols * row;
673 end = ri->ri_cols * (row + nrows);
674 #endif
675
676 for (i = start; i < end; i++) {
677 scr->scr_attrs[i] = fillattr;
678 scr->scr_chars[i] = 0x20;
679 }
680 }
681
682 static void
683 vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
684 {
685 struct rasops_info *ri = cookie;
686 struct vcons_screen *scr = ri->ri_hw;
687
688 vcons_eraserows_buffer(cookie, row, nrows, fillattr);
689
690 vcons_lock(scr);
691 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
692 scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
693 }
694 vcons_unlock(scr);
695 }
696
697 static void
698 vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
699 {
700 struct rasops_info *ri = cookie;
701 struct vcons_screen *scr = ri->ri_hw;
702 int pos;
703
704 #ifdef WSDISPLAY_SCROLLSUPPORT
705 int offset;
706 offset = scr->scr_offset_to_zero;
707
708 if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
709 (col < ri->ri_cols)) {
710 pos = col + row * ri->ri_cols;
711 scr->scr_attrs[pos + offset] = attr;
712 scr->scr_chars[pos + offset] = c;
713 }
714 #else
715 if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
716 (col < ri->ri_cols)) {
717 pos = col + row * ri->ri_cols;
718 scr->scr_attrs[pos] = attr;
719 scr->scr_chars[pos] = c;
720 }
721 #endif
722 }
723
724 static void
725 vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
726 {
727 struct rasops_info *ri = cookie;
728 struct vcons_screen *scr = ri->ri_hw;
729
730 vcons_putchar_buffer(cookie, row, col, c, attr);
731
732 vcons_lock(scr);
733 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
734 scr->scr_vd->putchar(cookie, row, col, c, attr);
735 }
736 vcons_unlock(scr);
737 }
738
739 static void
740 vcons_cursor(void *cookie, int on, int row, int col)
741 {
742 struct rasops_info *ri = cookie;
743 struct vcons_screen *scr = ri->ri_hw;
744
745 vcons_lock(scr);
746 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
747 scr->scr_vd->cursor(cookie, on, row, col);
748 } else {
749 scr->scr_ri.ri_crow = row;
750 scr->scr_ri.ri_ccol = col;
751 }
752 vcons_unlock(scr);
753 }
754
755 /* methods to read/write characters via ioctl() */
756
757 static int
758 vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
759 {
760 long attr;
761 struct rasops_info *ri;
762
763 KASSERT(scr != NULL && wsc != NULL);
764
765 ri = &scr->scr_ri;
766
767 if (__predict_false((unsigned int)wsc->col > ri->ri_cols ||
768 (unsigned int)wsc->row > ri->ri_rows))
769 return (EINVAL);
770
771 if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
772 (wsc->col < ri->ri_cols)) {
773
774 ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
775 wsc->flags, &attr);
776 vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
777 #ifdef VCONS_DEBUG
778 printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col,
779 wsc->letter, attr);
780 #endif
781 return 0;
782 } else
783 return EINVAL;
784 }
785
786 static int
787 vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
788 {
789 int offset;
790 long attr;
791 struct rasops_info *ri;
792
793 KASSERT(scr != NULL && wsc != NULL);
794
795 ri = &scr->scr_ri;
796
797 if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
798 (wsc->col < ri->ri_cols)) {
799
800 offset = ri->ri_cols * wsc->row + wsc->col;
801 #ifdef WSDISPLAY_SCROLLSUPPORT
802 offset += scr->scr_offset_to_zero;
803 #endif
804 wsc->letter = scr->scr_chars[offset];
805 attr = scr->scr_attrs[offset];
806
807 /*
808 * this is ugly. We need to break up an attribute into colours and
809 * flags but there's no rasops method to do that so we must rely on
810 * the 'canonical' encoding.
811 */
812 #ifdef VCONS_DEBUG
813 printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row,
814 wsc->col, wsc->letter, attr);
815 #endif
816 wsc->foreground = (attr >> 24) & 0xff;
817 wsc->background = (attr >> 16) & 0xff;
818 wsc->flags = attr & 0xff;
819 return 0;
820 } else
821 return EINVAL;
822 }
823
824 #ifdef WSDISPLAY_SCROLLSUPPORT
825
826 static void
827 vcons_scroll(void *cookie, void *vs, int where)
828 {
829 struct vcons_screen *scr = vs;
830
831 if (where == 0) {
832 scr->scr_line_wanted = 0;
833 } else {
834 scr->scr_line_wanted = scr->scr_line_wanted - where;
835 if (scr->scr_line_wanted < 0)
836 scr->scr_line_wanted = 0;
837 if (scr->scr_line_wanted > scr->scr_lines_in_buffer)
838 scr->scr_line_wanted = scr->scr_lines_in_buffer;
839 }
840
841 if (scr->scr_line_wanted != scr->scr_current_line) {
842
843 vcons_do_scroll(scr);
844 }
845 }
846
847 static void
848 vcons_do_scroll(struct vcons_screen *scr)
849 {
850 int dist, from, to, num;
851 int r_offset, r_start;
852 int i, j;
853
854 if (scr->scr_line_wanted == scr->scr_current_line)
855 return;
856 dist = scr->scr_line_wanted - scr->scr_current_line;
857 scr->scr_current_line = scr->scr_line_wanted;
858 scr->scr_current_offset = scr->scr_ri.ri_cols *
859 (scr->scr_lines_in_buffer - scr->scr_current_line);
860 if (abs(dist) >= scr->scr_ri.ri_rows) {
861 vcons_redraw_screen(scr);
862 return;
863 }
864 /* scroll and redraw only what we really have to */
865 if (dist > 0) {
866 /* we scroll down */
867 from = 0;
868 to = dist;
869 num = scr->scr_ri.ri_rows - dist;
870 /* now the redraw parameters */
871 r_offset = scr->scr_current_offset;
872 r_start = 0;
873 } else {
874 /* scrolling up */
875 to = 0;
876 from = -dist;
877 num = scr->scr_ri.ri_rows + dist;
878 r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
879 r_start = num;
880 }
881 scr->scr_vd->copyrows(scr, from, to, num);
882 for (i = 0; i < abs(dist); i++) {
883 for (j = 0; j < scr->scr_ri.ri_cols; j++) {
884 scr->scr_vd->putchar(scr, i + r_start, j,
885 scr->scr_chars[r_offset],
886 scr->scr_attrs[r_offset]);
887 r_offset++;
888 }
889 }
890
891 if (scr->scr_line_wanted == 0) {
892 /* this was a reset - need to draw the cursor */
893 scr->scr_ri.ri_flg &= ~RI_CURSOR;
894 scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow,
895 scr->scr_ri.ri_ccol);
896 }
897 }
898
899 #endif /* WSDISPLAY_SCROLLSUPPORT */
900
901 /* async drawing using a kernel thread */
902
903 #ifdef VCONS_SWITCH_ASYNC
904 static void
905 vcons_kthread(void *cookie)
906 {
907 struct vcons_data *vd = cookie;
908 struct vcons_screen *scr;
909 int sec = hz;
910
911 while (1) {
912
913 tsleep(&vd->start_drawing, 0, "vc_idle", sec);
914 if ((vd->wanted != vd->active) && (vd->wanted != NULL)) {
915 /*
916 * we need to switch screens
917 * so first we mark the active screen as invisible
918 * and wait until it's idle
919 */
920 scr = vd->wanted;
921 SCREEN_INVISIBLE(vd->active);
922 while (SCREEN_IS_BUSY(vd->active)) {
923
924 tsleep(&vd->done_drawing, 0, "vc_wait", sec);
925 }
926 /*
927 * now we mark the wanted screen busy so nobody
928 * messes around while we redraw it
929 */
930 vd->active = scr;
931 vd->wanted = NULL;
932 SCREEN_VISIBLE(scr);
933
934 if (vd->show_screen_cb != NULL)
935 vd->show_screen_cb(scr);
936
937 if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
938 vcons_redraw_screen(scr);
939
940 if (vd->switch_cb)
941 vd->switch_cb(vd->switch_cb_arg, 0, 0);
942 }
943 }
944 }
945 #endif /* VCONS_SWITCH_ASYNC */
946