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 | 260ddbb | 2007-09-05 12:43:20 +0000 | [diff] [blame] | 32 | * $Id: ctk-console.c,v 1.7 2007/09/05 12:43:20 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 | 7f0b22b | 2005-01-29 23:10:43 +0000 | [diff] [blame] | 60 | console_exit(); |
| 61 | exit(EXIT_SUCCESS); |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 62 | } |
| 63 | /*-----------------------------------------------------------------------------------*/ |
| 64 | void |
| 65 | console_init(void) |
| 66 | { |
| 67 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 68 | CONSOLE_CURSOR_INFO cursorinfo = {1, FALSE}; |
| 69 | |
| 70 | stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 71 | |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame] | 72 | screensize(&width, &height); |
| 73 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 74 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 75 | saved_color = (unsigned char)consoleinfo.wAttributes; |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 76 | |
| 77 | GetConsoleTitle(saved_title, sizeof(saved_title)); |
| 78 | SetConsoleTitle("Contiki"); |
| 79 | |
| 80 | GetConsoleMode(stdouthandle, &saved_consolemode); |
| 81 | SetConsoleMode(stdouthandle, ENABLE_PROCESSED_OUTPUT); |
| 82 | |
| 83 | GetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); |
| 84 | SetConsoleCursorInfo(stdouthandle, &cursorinfo); |
| 85 | |
| 86 | SetConsoleCtrlHandler(consolehandler, TRUE); |
| 87 | } |
| 88 | /*-----------------------------------------------------------------------------------*/ |
| 89 | void |
| 90 | console_exit(void) |
| 91 | { |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 92 | textcolor(saved_color); |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 93 | revers(0); |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 94 | clrscr(); |
| 95 | gotoxy(0, 0); |
| 96 | |
| 97 | SetConsoleTitle(saved_title); |
| 98 | SetConsoleMode(stdouthandle, saved_consolemode); |
| 99 | SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 100 | } |
| 101 | /*-----------------------------------------------------------------------------------*/ |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame] | 102 | unsigned char |
| 103 | console_resize(void) |
| 104 | { |
| 105 | unsigned char new_width; |
| 106 | unsigned char new_height; |
| 107 | |
| 108 | screensize(&new_width, &new_height); |
| 109 | |
| 110 | if(new_width != width || |
| 111 | new_height != height) { |
| 112 | width = new_width; |
| 113 | height = new_height; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | /*-----------------------------------------------------------------------------------*/ |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 120 | static void |
| 121 | setcolor(void) |
| 122 | { |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 123 | SetConsoleTextAttribute(stdouthandle, (WORD)(reversed? (color & 0x0F) << 4 | |
| 124 | (color & 0xF0) >> 4 |
| 125 | : color)); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 126 | } |
| 127 | /*-----------------------------------------------------------------------------------*/ |
| 128 | unsigned char |
| 129 | wherex(void) |
| 130 | { |
| 131 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 132 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 133 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 134 | return (unsigned char)consoleinfo.dwCursorPosition.X; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 135 | } |
| 136 | /*-----------------------------------------------------------------------------------*/ |
| 137 | unsigned char |
| 138 | wherey(void) |
| 139 | { |
| 140 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 141 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 142 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 143 | return (unsigned char)consoleinfo.dwCursorPosition.Y; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 144 | } |
| 145 | /*-----------------------------------------------------------------------------------*/ |
| 146 | void |
| 147 | clrscr(void) |
| 148 | { |
| 149 | unsigned char i, width, height; |
| 150 | |
| 151 | screensize(&width, &height); |
| 152 | for(i = 0; i < height; ++i) { |
| 153 | cclearxy(0, i, width); |
| 154 | } |
| 155 | } |
| 156 | /*-----------------------------------------------------------------------------------*/ |
| 157 | void |
| 158 | revers(unsigned char c) |
| 159 | { |
| 160 | reversed = c; |
| 161 | setcolor(); |
| 162 | } |
| 163 | /*-----------------------------------------------------------------------------------*/ |
| 164 | void |
| 165 | cclear(unsigned char length) |
| 166 | { |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 167 | unsigned char i; |
| 168 | |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 169 | for(i = 0; i < length; ++i) { |
| 170 | putch(' '); |
| 171 | } |
| 172 | } |
| 173 | /*-----------------------------------------------------------------------------------*/ |
| 174 | void |
| 175 | chline(unsigned char length) |
| 176 | { |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 177 | unsigned char i; |
| 178 | |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 179 | for(i = 0; i < length; ++i) { |
oliverschmidt | 260ddbb | 2007-09-05 12:43:20 +0000 | [diff] [blame] | 180 | putch('-'); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | /*-----------------------------------------------------------------------------------*/ |
| 184 | void |
| 185 | cvline(unsigned char length) |
| 186 | { |
oliverschmidt | 81c744e | 2005-02-06 21:13:59 +0000 | [diff] [blame] | 187 | unsigned char i, x, y; |
| 188 | |
| 189 | x = wherex(); |
| 190 | y = wherey(); |
| 191 | |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 192 | for(i = 0; i < length; ++i) { |
oliverschmidt | 260ddbb | 2007-09-05 12:43:20 +0000 | [diff] [blame] | 193 | cputcxy(x, (unsigned char)(y + i), '|'); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | /*-----------------------------------------------------------------------------------*/ |
| 197 | void |
| 198 | gotoxy(unsigned char x, unsigned char y) |
| 199 | { |
| 200 | COORD coord = {x, y}; |
| 201 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 202 | SetConsoleCursorPosition(stdouthandle, coord); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 203 | } |
| 204 | /*-----------------------------------------------------------------------------------*/ |
| 205 | void |
| 206 | cclearxy(unsigned char x, unsigned char y, unsigned char length) |
| 207 | { |
| 208 | gotoxy(x, y); |
| 209 | cclear(length); |
| 210 | } |
| 211 | /*-----------------------------------------------------------------------------------*/ |
| 212 | void |
| 213 | chlinexy(unsigned char x, unsigned char y, unsigned char length) |
| 214 | { |
| 215 | gotoxy(x, y); |
| 216 | chline(length); |
| 217 | } |
| 218 | /*-----------------------------------------------------------------------------------*/ |
| 219 | void |
| 220 | cvlinexy(unsigned char x, unsigned char y, unsigned char length) |
| 221 | { |
| 222 | gotoxy(x, y); |
| 223 | cvline(length); |
| 224 | } |
| 225 | /*-----------------------------------------------------------------------------------*/ |
| 226 | void |
| 227 | cputsxy(unsigned char x, unsigned char y, char *str) |
| 228 | { |
| 229 | gotoxy(x, y); |
| 230 | cputs(str); |
| 231 | } |
| 232 | /*-----------------------------------------------------------------------------------*/ |
| 233 | void |
| 234 | cputcxy(unsigned char x, unsigned char y, char c) |
| 235 | { |
| 236 | gotoxy(x, y); |
| 237 | putch(c); |
| 238 | } |
| 239 | /*-----------------------------------------------------------------------------------*/ |
| 240 | void |
| 241 | textcolor(unsigned char c) |
| 242 | { |
oliverschmidt | 8996345 | 2004-08-12 22:09:34 +0000 | [diff] [blame] | 243 | color = c; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 244 | setcolor(); |
| 245 | } |
| 246 | /*-----------------------------------------------------------------------------------*/ |
| 247 | void |
| 248 | bgcolor(unsigned char c) |
| 249 | { |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 250 | } |
| 251 | /*-----------------------------------------------------------------------------------*/ |
| 252 | void |
| 253 | bordercolor(unsigned char c) |
| 254 | { |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 255 | } |
| 256 | /*-----------------------------------------------------------------------------------*/ |
| 257 | void |
| 258 | screensize(unsigned char *x, unsigned char *y) |
| 259 | { |
| 260 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 261 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame] | 262 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | e80365d | 2004-08-12 22:53:22 +0000 | [diff] [blame] | 263 | *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1; |
| 264 | *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1; |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 265 | } |
| 266 | /*-----------------------------------------------------------------------------------*/ |