oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote |
| 15 | * products derived from this software without specific prior |
| 16 | * written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 19 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 24 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * This file is part of the Contiki desktop environment |
| 31 | * |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame^] | 32 | * $Id: ctk-console.c,v 1.4 2004/08/12 22:53:22 oliverschmidt Exp $ |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
| 36 | #define WIN32_LEAN_AND_MEAN |
| 37 | #include <windows.h> |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 38 | #include <stdlib.h> |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 39 | #include <conio.h> |
| 40 | |
| 41 | #include "ctk-console.h" |
| 42 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 43 | static HANDLE stdouthandle; |
| 44 | |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame^] | 45 | static unsigned char width; |
| 46 | static unsigned char height; |
| 47 | |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 48 | static unsigned char saved_color; |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 49 | static char saved_title[1024]; |
| 50 | static DWORD saved_consolemode; |
| 51 | static CONSOLE_CURSOR_INFO saved_cursorinfo; |
| 52 | |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 53 | static unsigned char color; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 54 | static unsigned char reversed; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 55 | |
| 56 | /*-----------------------------------------------------------------------------------*/ |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 57 | static BOOL WINAPI |
| 58 | consolehandler(DWORD event) |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 59 | { |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 60 | if(event == CTRL_C_EVENT || |
| 61 | event == CTRL_BREAK_EVENT) { |
| 62 | console_exit(); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 63 | } |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 64 | |
| 65 | return FALSE; |
| 66 | } |
| 67 | /*-----------------------------------------------------------------------------------*/ |
| 68 | void |
| 69 | console_init(void) |
| 70 | { |
| 71 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 72 | CONSOLE_CURSOR_INFO cursorinfo = {1, FALSE}; |
| 73 | |
| 74 | stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 75 | |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame^] | 76 | screensize(&width, &height); |
| 77 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 78 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 79 | saved_color = consoleinfo.wAttributes; |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 80 | |
| 81 | GetConsoleTitle(saved_title, sizeof(saved_title)); |
| 82 | SetConsoleTitle("Contiki"); |
| 83 | |
| 84 | GetConsoleMode(stdouthandle, &saved_consolemode); |
| 85 | SetConsoleMode(stdouthandle, ENABLE_PROCESSED_OUTPUT); |
| 86 | |
| 87 | GetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); |
| 88 | SetConsoleCursorInfo(stdouthandle, &cursorinfo); |
| 89 | |
| 90 | SetConsoleCtrlHandler(consolehandler, TRUE); |
| 91 | } |
| 92 | /*-----------------------------------------------------------------------------------*/ |
| 93 | void |
| 94 | console_exit(void) |
| 95 | { |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 96 | textcolor(saved_color); |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 97 | revers(0); |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 98 | clrscr(); |
| 99 | gotoxy(0, 0); |
| 100 | |
| 101 | SetConsoleTitle(saved_title); |
| 102 | SetConsoleMode(stdouthandle, saved_consolemode); |
| 103 | SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 104 | } |
| 105 | /*-----------------------------------------------------------------------------------*/ |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame^] | 106 | unsigned char |
| 107 | console_resize(void) |
| 108 | { |
| 109 | unsigned char new_width; |
| 110 | unsigned char new_height; |
| 111 | |
| 112 | screensize(&new_width, &new_height); |
| 113 | |
| 114 | if(new_width != width || |
| 115 | new_height != height) { |
| 116 | width = new_width; |
| 117 | height = new_height; |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | /*-----------------------------------------------------------------------------------*/ |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 124 | static void |
| 125 | setcolor(void) |
| 126 | { |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 127 | SetConsoleTextAttribute(stdouthandle, reversed? (color & 0x0F) << 4 | |
| 128 | (color & 0xF0) >> 4 |
| 129 | : color); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 130 | } |
| 131 | /*-----------------------------------------------------------------------------------*/ |
| 132 | unsigned char |
| 133 | wherex(void) |
| 134 | { |
| 135 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 136 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 137 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 138 | return consoleinfo.dwCursorPosition.X; |
| 139 | } |
| 140 | /*-----------------------------------------------------------------------------------*/ |
| 141 | unsigned char |
| 142 | wherey(void) |
| 143 | { |
| 144 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 145 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 146 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 147 | return consoleinfo.dwCursorPosition.Y; |
| 148 | } |
| 149 | /*-----------------------------------------------------------------------------------*/ |
| 150 | void |
| 151 | clrscr(void) |
| 152 | { |
| 153 | unsigned char i, width, height; |
| 154 | |
| 155 | screensize(&width, &height); |
| 156 | for(i = 0; i < height; ++i) { |
| 157 | cclearxy(0, i, width); |
| 158 | } |
| 159 | } |
| 160 | /*-----------------------------------------------------------------------------------*/ |
| 161 | void |
| 162 | revers(unsigned char c) |
| 163 | { |
| 164 | reversed = c; |
| 165 | setcolor(); |
| 166 | } |
| 167 | /*-----------------------------------------------------------------------------------*/ |
| 168 | void |
| 169 | cclear(unsigned char length) |
| 170 | { |
| 171 | int i; |
| 172 | for(i = 0; i < length; ++i) { |
| 173 | putch(' '); |
| 174 | } |
| 175 | } |
| 176 | /*-----------------------------------------------------------------------------------*/ |
| 177 | void |
| 178 | chline(unsigned char length) |
| 179 | { |
| 180 | int i; |
| 181 | for(i = 0; i < length; ++i) { |
| 182 | putch(0xC4); |
| 183 | } |
| 184 | } |
| 185 | /*-----------------------------------------------------------------------------------*/ |
| 186 | void |
| 187 | cvline(unsigned char length) |
| 188 | { |
| 189 | int i; |
| 190 | for(i = 0; i < length; ++i) { |
| 191 | putch(0xB3); |
| 192 | gotoxy(wherex() - 1, wherey() + 1); |
| 193 | } |
| 194 | } |
| 195 | /*-----------------------------------------------------------------------------------*/ |
| 196 | void |
| 197 | gotoxy(unsigned char x, unsigned char y) |
| 198 | { |
| 199 | COORD coord = {x, y}; |
| 200 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 201 | SetConsoleCursorPosition(stdouthandle, coord); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 202 | } |
| 203 | /*-----------------------------------------------------------------------------------*/ |
| 204 | void |
| 205 | cclearxy(unsigned char x, unsigned char y, unsigned char length) |
| 206 | { |
| 207 | gotoxy(x, y); |
| 208 | cclear(length); |
| 209 | } |
| 210 | /*-----------------------------------------------------------------------------------*/ |
| 211 | void |
| 212 | chlinexy(unsigned char x, unsigned char y, unsigned char length) |
| 213 | { |
| 214 | gotoxy(x, y); |
| 215 | chline(length); |
| 216 | } |
| 217 | /*-----------------------------------------------------------------------------------*/ |
| 218 | void |
| 219 | cvlinexy(unsigned char x, unsigned char y, unsigned char length) |
| 220 | { |
| 221 | gotoxy(x, y); |
| 222 | cvline(length); |
| 223 | } |
| 224 | /*-----------------------------------------------------------------------------------*/ |
| 225 | void |
| 226 | cputsxy(unsigned char x, unsigned char y, char *str) |
| 227 | { |
| 228 | gotoxy(x, y); |
| 229 | cputs(str); |
| 230 | } |
| 231 | /*-----------------------------------------------------------------------------------*/ |
| 232 | void |
| 233 | cputcxy(unsigned char x, unsigned char y, char c) |
| 234 | { |
| 235 | gotoxy(x, y); |
| 236 | putch(c); |
| 237 | } |
| 238 | /*-----------------------------------------------------------------------------------*/ |
| 239 | void |
| 240 | textcolor(unsigned char c) |
| 241 | { |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 242 | color = c; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 243 | setcolor(); |
| 244 | } |
| 245 | /*-----------------------------------------------------------------------------------*/ |
| 246 | void |
| 247 | bgcolor(unsigned char c) |
| 248 | { |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 249 | } |
| 250 | /*-----------------------------------------------------------------------------------*/ |
| 251 | void |
| 252 | bordercolor(unsigned char c) |
| 253 | { |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 254 | } |
| 255 | /*-----------------------------------------------------------------------------------*/ |
| 256 | void |
| 257 | screensize(unsigned char *x, unsigned char *y) |
| 258 | { |
| 259 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 260 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 261 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame^] | 262 | *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1; |
| 263 | *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 264 | } |
| 265 | /*-----------------------------------------------------------------------------------*/ |