Anders Hörnfeldt
/
coniolib
Port of Conio.h
lib/conio.c@0:6c1bc9b3a347, 2010-04-19 (annotated)
- Committer:
- hornfeldt
- Date:
- Mon Apr 19 20:20:10 2010 +0000
- Revision:
- 0:6c1bc9b3a347
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hornfeldt | 0:6c1bc9b3a347 | 1 | /* |
hornfeldt | 0:6c1bc9b3a347 | 2 | conio.c |
hornfeldt | 0:6c1bc9b3a347 | 3 | Standard conio routines. |
hornfeldt | 0:6c1bc9b3a347 | 4 | Part of MicroVGA CONIO library / demo project |
hornfeldt | 0:6c1bc9b3a347 | 5 | Copyright (c) 2008-9 SECONS s.r.o., http://www.MicroVGA.com |
hornfeldt | 0:6c1bc9b3a347 | 6 | |
hornfeldt | 0:6c1bc9b3a347 | 7 | This program is free software: you can redistribute it and/or modify |
hornfeldt | 0:6c1bc9b3a347 | 8 | it under the terms of the GNU General Public License as published by |
hornfeldt | 0:6c1bc9b3a347 | 9 | the Free Software Foundation, either version 3 of the License, or |
hornfeldt | 0:6c1bc9b3a347 | 10 | (at your option) any later version. |
hornfeldt | 0:6c1bc9b3a347 | 11 | |
hornfeldt | 0:6c1bc9b3a347 | 12 | This program is distributed in the hope that it will be useful, |
hornfeldt | 0:6c1bc9b3a347 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hornfeldt | 0:6c1bc9b3a347 | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hornfeldt | 0:6c1bc9b3a347 | 15 | GNU General Public License for more details. |
hornfeldt | 0:6c1bc9b3a347 | 16 | |
hornfeldt | 0:6c1bc9b3a347 | 17 | You should have received a copy of the GNU General Public License |
hornfeldt | 0:6c1bc9b3a347 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hornfeldt | 0:6c1bc9b3a347 | 19 | */ |
hornfeldt | 0:6c1bc9b3a347 | 20 | |
hornfeldt | 0:6c1bc9b3a347 | 21 | #include "../h/conio.h" |
hornfeldt | 0:6c1bc9b3a347 | 22 | #include "../h/ui.h" |
hornfeldt | 0:6c1bc9b3a347 | 23 | #include "../h/kbd.h" |
hornfeldt | 0:6c1bc9b3a347 | 24 | |
hornfeldt | 0:6c1bc9b3a347 | 25 | |
hornfeldt | 0:6c1bc9b3a347 | 26 | |
hornfeldt | 0:6c1bc9b3a347 | 27 | void clrscr(void) |
hornfeldt | 0:6c1bc9b3a347 | 28 | { |
hornfeldt | 0:6c1bc9b3a347 | 29 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 30 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 31 | _putch('2'); |
hornfeldt | 0:6c1bc9b3a347 | 32 | _putch('J'); |
hornfeldt | 0:6c1bc9b3a347 | 33 | } |
hornfeldt | 0:6c1bc9b3a347 | 34 | |
hornfeldt | 0:6c1bc9b3a347 | 35 | void clreol(void) |
hornfeldt | 0:6c1bc9b3a347 | 36 | { |
hornfeldt | 0:6c1bc9b3a347 | 37 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 38 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 39 | _putch('K'); |
hornfeldt | 0:6c1bc9b3a347 | 40 | } |
hornfeldt | 0:6c1bc9b3a347 | 41 | |
hornfeldt | 0:6c1bc9b3a347 | 42 | |
hornfeldt | 0:6c1bc9b3a347 | 43 | void cursoron(void) |
hornfeldt | 0:6c1bc9b3a347 | 44 | { |
hornfeldt | 0:6c1bc9b3a347 | 45 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 46 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 47 | _putch('2'); |
hornfeldt | 0:6c1bc9b3a347 | 48 | _putch('5'); |
hornfeldt | 0:6c1bc9b3a347 | 49 | _putch('h'); |
hornfeldt | 0:6c1bc9b3a347 | 50 | } |
hornfeldt | 0:6c1bc9b3a347 | 51 | |
hornfeldt | 0:6c1bc9b3a347 | 52 | void cursoroff(void) |
hornfeldt | 0:6c1bc9b3a347 | 53 | { |
hornfeldt | 0:6c1bc9b3a347 | 54 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 55 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 56 | _putch('2'); |
hornfeldt | 0:6c1bc9b3a347 | 57 | _putch('5'); |
hornfeldt | 0:6c1bc9b3a347 | 58 | _putch('l'); |
hornfeldt | 0:6c1bc9b3a347 | 59 | } |
hornfeldt | 0:6c1bc9b3a347 | 60 | |
hornfeldt | 0:6c1bc9b3a347 | 61 | void textcolor(int color) |
hornfeldt | 0:6c1bc9b3a347 | 62 | { |
hornfeldt | 0:6c1bc9b3a347 | 63 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 64 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 65 | if (color & 0x8) |
hornfeldt | 0:6c1bc9b3a347 | 66 | _putch('1'); |
hornfeldt | 0:6c1bc9b3a347 | 67 | else _putch('2'); |
hornfeldt | 0:6c1bc9b3a347 | 68 | _putch('m'); |
hornfeldt | 0:6c1bc9b3a347 | 69 | |
hornfeldt | 0:6c1bc9b3a347 | 70 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 71 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 72 | _putch('3'); |
hornfeldt | 0:6c1bc9b3a347 | 73 | _putch(((color&0x7)%10)+'0'); |
hornfeldt | 0:6c1bc9b3a347 | 74 | _putch('m'); |
hornfeldt | 0:6c1bc9b3a347 | 75 | } |
hornfeldt | 0:6c1bc9b3a347 | 76 | |
hornfeldt | 0:6c1bc9b3a347 | 77 | void textbackground(int color) |
hornfeldt | 0:6c1bc9b3a347 | 78 | { |
hornfeldt | 0:6c1bc9b3a347 | 79 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 80 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 81 | if (color & 0x8) |
hornfeldt | 0:6c1bc9b3a347 | 82 | _putch('5'); |
hornfeldt | 0:6c1bc9b3a347 | 83 | else _putch('6'); |
hornfeldt | 0:6c1bc9b3a347 | 84 | _putch('m'); |
hornfeldt | 0:6c1bc9b3a347 | 85 | |
hornfeldt | 0:6c1bc9b3a347 | 86 | _putch('\033'); |
hornfeldt | 0:6c1bc9b3a347 | 87 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 88 | _putch('4'); |
hornfeldt | 0:6c1bc9b3a347 | 89 | _putch((color&0x7)+'0'); |
hornfeldt | 0:6c1bc9b3a347 | 90 | _putch('m'); |
hornfeldt | 0:6c1bc9b3a347 | 91 | } |
hornfeldt | 0:6c1bc9b3a347 | 92 | |
hornfeldt | 0:6c1bc9b3a347 | 93 | void textattr(int attr) |
hornfeldt | 0:6c1bc9b3a347 | 94 | { |
hornfeldt | 0:6c1bc9b3a347 | 95 | textcolor(attr&0xF); |
hornfeldt | 0:6c1bc9b3a347 | 96 | textbackground(attr>>4); |
hornfeldt | 0:6c1bc9b3a347 | 97 | } |
hornfeldt | 0:6c1bc9b3a347 | 98 | |
hornfeldt | 0:6c1bc9b3a347 | 99 | void gotoxy(char x, char y) |
hornfeldt | 0:6c1bc9b3a347 | 100 | { |
hornfeldt | 0:6c1bc9b3a347 | 101 | if (x>MAX_X || y>MAX_Y) |
hornfeldt | 0:6c1bc9b3a347 | 102 | return; |
hornfeldt | 0:6c1bc9b3a347 | 103 | |
hornfeldt | 0:6c1bc9b3a347 | 104 | x--; |
hornfeldt | 0:6c1bc9b3a347 | 105 | y--; |
hornfeldt | 0:6c1bc9b3a347 | 106 | |
hornfeldt | 0:6c1bc9b3a347 | 107 | _putch(0x1B); |
hornfeldt | 0:6c1bc9b3a347 | 108 | _putch('['); |
hornfeldt | 0:6c1bc9b3a347 | 109 | _putch((y/10)+'0'); |
hornfeldt | 0:6c1bc9b3a347 | 110 | _putch((y%10)+'0'); |
hornfeldt | 0:6c1bc9b3a347 | 111 | _putch(';'); |
hornfeldt | 0:6c1bc9b3a347 | 112 | _putch((x/10)+'0'); |
hornfeldt | 0:6c1bc9b3a347 | 113 | _putch((x%10)+'0'); |
hornfeldt | 0:6c1bc9b3a347 | 114 | _putch('f'); |
hornfeldt | 0:6c1bc9b3a347 | 115 | } |
hornfeldt | 0:6c1bc9b3a347 | 116 | |
hornfeldt | 0:6c1bc9b3a347 | 117 | |
hornfeldt | 0:6c1bc9b3a347 | 118 | void _cputs(ROMDEF char *s) |
hornfeldt | 0:6c1bc9b3a347 | 119 | { |
hornfeldt | 0:6c1bc9b3a347 | 120 | while (*s != 0) |
hornfeldt | 0:6c1bc9b3a347 | 121 | _putch(*s++); |
hornfeldt | 0:6c1bc9b3a347 | 122 | } |
hornfeldt | 0:6c1bc9b3a347 | 123 | |
hornfeldt | 0:6c1bc9b3a347 | 124 | char * _cgets(char *s) |
hornfeldt | 0:6c1bc9b3a347 | 125 | { |
hornfeldt | 0:6c1bc9b3a347 | 126 | char len; |
hornfeldt | 0:6c1bc9b3a347 | 127 | int ch; |
hornfeldt | 0:6c1bc9b3a347 | 128 | |
hornfeldt | 0:6c1bc9b3a347 | 129 | len=0; |
hornfeldt | 0:6c1bc9b3a347 | 130 | |
hornfeldt | 0:6c1bc9b3a347 | 131 | while (s[0]>len) |
hornfeldt | 0:6c1bc9b3a347 | 132 | { |
hornfeldt | 0:6c1bc9b3a347 | 133 | ch=_getch(); |
hornfeldt | 0:6c1bc9b3a347 | 134 | |
hornfeldt | 0:6c1bc9b3a347 | 135 | if (ch==KB_ENTER) |
hornfeldt | 0:6c1bc9b3a347 | 136 | break; //enter hit, end of input |
hornfeldt | 0:6c1bc9b3a347 | 137 | |
hornfeldt | 0:6c1bc9b3a347 | 138 | if (ch==KB_ESC) { |
hornfeldt | 0:6c1bc9b3a347 | 139 | s[1]=0; |
hornfeldt | 0:6c1bc9b3a347 | 140 | s[2]=0; |
hornfeldt | 0:6c1bc9b3a347 | 141 | return &s[2]; |
hornfeldt | 0:6c1bc9b3a347 | 142 | } |
hornfeldt | 0:6c1bc9b3a347 | 143 | |
hornfeldt | 0:6c1bc9b3a347 | 144 | |
hornfeldt | 0:6c1bc9b3a347 | 145 | if (ch==KB_BACK) |
hornfeldt | 0:6c1bc9b3a347 | 146 | { |
hornfeldt | 0:6c1bc9b3a347 | 147 | |
hornfeldt | 0:6c1bc9b3a347 | 148 | if (len>0) |
hornfeldt | 0:6c1bc9b3a347 | 149 | { |
hornfeldt | 0:6c1bc9b3a347 | 150 | len--; |
hornfeldt | 0:6c1bc9b3a347 | 151 | //delete char and go back (if some chars left) |
hornfeldt | 0:6c1bc9b3a347 | 152 | _putch(KB_BACK); |
hornfeldt | 0:6c1bc9b3a347 | 153 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 154 | _putch(KB_BACK); |
hornfeldt | 0:6c1bc9b3a347 | 155 | |
hornfeldt | 0:6c1bc9b3a347 | 156 | } |
hornfeldt | 0:6c1bc9b3a347 | 157 | |
hornfeldt | 0:6c1bc9b3a347 | 158 | continue; |
hornfeldt | 0:6c1bc9b3a347 | 159 | } |
hornfeldt | 0:6c1bc9b3a347 | 160 | |
hornfeldt | 0:6c1bc9b3a347 | 161 | if (ch>0x80 || ch <' ') //skip functions keys |
hornfeldt | 0:6c1bc9b3a347 | 162 | continue; |
hornfeldt | 0:6c1bc9b3a347 | 163 | |
hornfeldt | 0:6c1bc9b3a347 | 164 | _putch((char)0xff&ch); //print back to screen |
hornfeldt | 0:6c1bc9b3a347 | 165 | s[len+2]=(char)0xff&ch; |
hornfeldt | 0:6c1bc9b3a347 | 166 | len++; |
hornfeldt | 0:6c1bc9b3a347 | 167 | } |
hornfeldt | 0:6c1bc9b3a347 | 168 | |
hornfeldt | 0:6c1bc9b3a347 | 169 | s[1]=len; |
hornfeldt | 0:6c1bc9b3a347 | 170 | s[len+2]=0; |
hornfeldt | 0:6c1bc9b3a347 | 171 | |
hornfeldt | 0:6c1bc9b3a347 | 172 | return &s[2]; |
hornfeldt | 0:6c1bc9b3a347 | 173 | } |