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