| 1 | /* |
|---|
| 2 | * libcaca Colour ASCII-Art library |
|---|
| 3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This library is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * This file contains the libcaca DOS/conio.h input and output driver |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | #include "common.h" |
|---|
| 21 | |
|---|
| 22 | #if defined(USE_CONIO) |
|---|
| 23 | |
|---|
| 24 | #include <dos.h> |
|---|
| 25 | #include <conio.h> |
|---|
| 26 | #if defined(SCREENUPDATE_IN_PC_H) |
|---|
| 27 | # include <pc.h> |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | #include <stdlib.h> |
|---|
| 31 | |
|---|
| 32 | #include "caca.h" |
|---|
| 33 | #include "caca_internals.h" |
|---|
| 34 | #include "cucul.h" |
|---|
| 35 | #include "cucul_internals.h" |
|---|
| 36 | |
|---|
| 37 | struct driver_private |
|---|
| 38 | { |
|---|
| 39 | struct text_info ti; |
|---|
| 40 | char *screen; |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | static int conio_init_graphics(caca_display_t *dp) |
|---|
| 44 | { |
|---|
| 45 | dp->drv.p = malloc(sizeof(struct driver_private)); |
|---|
| 46 | |
|---|
| 47 | _wscroll = 0; |
|---|
| 48 | _setcursortype(_NOCURSOR); |
|---|
| 49 | clrscr(); |
|---|
| 50 | |
|---|
| 51 | gettextinfo(&dp->drv.p->ti); |
|---|
| 52 | dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth |
|---|
| 53 | * dp->drv.p->ti.screenheight * sizeof(char)); |
|---|
| 54 | if(dp->drv.p->screen == NULL) |
|---|
| 55 | return -1; |
|---|
| 56 | # if defined(SCREENUPDATE_IN_PC_H) |
|---|
| 57 | ScreenRetrieve(dp->drv.p->screen); |
|---|
| 58 | # else |
|---|
| 59 | /* FIXME */ |
|---|
| 60 | # endif |
|---|
| 61 | __cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth, |
|---|
| 62 | dp->drv.p->ti.screenheight); |
|---|
| 63 | return 0; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | static int conio_end_graphics(caca_display_t *dp) |
|---|
| 67 | { |
|---|
| 68 | _wscroll = 1; |
|---|
| 69 | textcolor((enum COLORS)WHITE); |
|---|
| 70 | textbackground((enum COLORS)BLACK); |
|---|
| 71 | gotoxy(dp->cv->width, dp->cv->height); |
|---|
| 72 | cputs("\r\n"); |
|---|
| 73 | _setcursortype(_NORMALCURSOR); |
|---|
| 74 | |
|---|
| 75 | free(dp->drv.p->screen); |
|---|
| 76 | free(dp->drv.p); |
|---|
| 77 | |
|---|
| 78 | return 0; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | static int conio_set_display_title(caca_display_t *dp, char const *title) |
|---|
| 82 | { |
|---|
| 83 | return -1; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | static unsigned int conio_get_display_width(caca_display_t const *dp) |
|---|
| 87 | { |
|---|
| 88 | /* Fallback to a 6x10 font */ |
|---|
| 89 | return dp->cv->width * 6; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | static unsigned int conio_get_display_height(caca_display_t const *dp) |
|---|
| 93 | { |
|---|
| 94 | /* Fallback to a 6x10 font */ |
|---|
| 95 | return dp->cv->height * 10; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | static void conio_display(caca_display_t *dp) |
|---|
| 99 | { |
|---|
| 100 | char *screen = dp->drv.p->screen; |
|---|
| 101 | uint32_t *attrs = dp->cv->attrs; |
|---|
| 102 | uint32_t *chars = dp->cv->chars; |
|---|
| 103 | unsigned int n; |
|---|
| 104 | |
|---|
| 105 | for(n = dp->cv->height * dp->cv->width; n--; ) |
|---|
| 106 | { |
|---|
| 107 | char ch = cucul_utf32_to_cp437(*chars++); |
|---|
| 108 | if(n && *chars == CUCUL_MAGIC_FULLWIDTH) |
|---|
| 109 | { |
|---|
| 110 | *screen++ = '['; |
|---|
| 111 | *screen++ = cucul_attr_to_ansi(*attrs++); |
|---|
| 112 | ch = ']'; |
|---|
| 113 | chars++; |
|---|
| 114 | n--; |
|---|
| 115 | } |
|---|
| 116 | *screen++ = ch; |
|---|
| 117 | *screen++ = cucul_attr_to_ansi(*attrs++); |
|---|
| 118 | } |
|---|
| 119 | # if defined(SCREENUPDATE_IN_PC_H) |
|---|
| 120 | ScreenUpdate(dp->drv.p->screen); |
|---|
| 121 | # else |
|---|
| 122 | /* FIXME */ |
|---|
| 123 | # endif |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | static void conio_handle_resize(caca_display_t *dp) |
|---|
| 127 | { |
|---|
| 128 | /* We know nothing about our window */ |
|---|
| 129 | dp->resize.w = dp->cv->width; |
|---|
| 130 | dp->resize.h = dp->cv->height; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev) |
|---|
| 134 | { |
|---|
| 135 | unsigned char ch; |
|---|
| 136 | caca_privevent_t release; |
|---|
| 137 | |
|---|
| 138 | if(!_conio_kbhit()) |
|---|
| 139 | { |
|---|
| 140 | ev->type = CACA_EVENT_NONE; |
|---|
| 141 | return 0; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | ch = getch(); |
|---|
| 145 | |
|---|
| 146 | ev->type = CACA_EVENT_KEY_PRESS; |
|---|
| 147 | ev->data.key.ch = ch; |
|---|
| 148 | ev->data.key.utf32 = (uint32_t)ch; |
|---|
| 149 | ev->data.key.utf8[0] = ch; |
|---|
| 150 | ev->data.key.utf8[1] = '\0'; |
|---|
| 151 | |
|---|
| 152 | release = *ev; |
|---|
| 153 | release.type = CACA_EVENT_KEY_RELEASE; |
|---|
| 154 | _push_event(dp, &release); |
|---|
| 155 | |
|---|
| 156 | return 1; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /* |
|---|
| 160 | * Driver initialisation |
|---|
| 161 | */ |
|---|
| 162 | |
|---|
| 163 | int conio_install(caca_display_t *dp) |
|---|
| 164 | { |
|---|
| 165 | dp->drv.driver = CACA_DRIVER_CONIO; |
|---|
| 166 | |
|---|
| 167 | dp->drv.init_graphics = conio_init_graphics; |
|---|
| 168 | dp->drv.end_graphics = conio_end_graphics; |
|---|
| 169 | dp->drv.set_display_title = conio_set_display_title; |
|---|
| 170 | dp->drv.get_display_width = conio_get_display_width; |
|---|
| 171 | dp->drv.get_display_height = conio_get_display_height; |
|---|
| 172 | dp->drv.display = conio_display; |
|---|
| 173 | dp->drv.handle_resize = conio_handle_resize; |
|---|
| 174 | dp->drv.get_event = conio_get_event; |
|---|
| 175 | dp->drv.set_mouse = NULL; |
|---|
| 176 | dp->drv.set_cursor = NULL; |
|---|
| 177 | |
|---|
| 178 | return 0; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | #endif /* USE_CONIO */ |
|---|
| 182 | |
|---|