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 | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 32 | * $Id: ctk-console.c,v 1.2 2004/07/31 14:55:17 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 | |
| 45 | static unsigned char saved_foreground; |
| 46 | static unsigned char saved_background; |
| 47 | static char saved_title[1024]; |
| 48 | static DWORD saved_consolemode; |
| 49 | static CONSOLE_CURSOR_INFO saved_cursorinfo; |
| 50 | |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 51 | static unsigned char reversed; |
| 52 | static unsigned char foreground; |
| 53 | static unsigned char background; |
| 54 | |
| 55 | /*-----------------------------------------------------------------------------------*/ |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 56 | static BOOL WINAPI |
| 57 | consolehandler(DWORD event) |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 58 | { |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 59 | if(event == CTRL_C_EVENT || |
| 60 | event == CTRL_BREAK_EVENT) { |
| 61 | console_exit(); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 62 | } |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 63 | |
| 64 | return FALSE; |
| 65 | } |
| 66 | /*-----------------------------------------------------------------------------------*/ |
| 67 | void |
| 68 | console_init(void) |
| 69 | { |
| 70 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 71 | CONSOLE_CURSOR_INFO cursorinfo = {1, FALSE}; |
| 72 | |
| 73 | stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 74 | |
| 75 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
| 76 | saved_foreground = consoleinfo.wAttributes % 0x10; |
| 77 | saved_background = consoleinfo.wAttributes / 0x10; |
| 78 | |
| 79 | GetConsoleTitle(saved_title, sizeof(saved_title)); |
| 80 | SetConsoleTitle("Contiki"); |
| 81 | |
| 82 | GetConsoleMode(stdouthandle, &saved_consolemode); |
| 83 | SetConsoleMode(stdouthandle, ENABLE_PROCESSED_OUTPUT); |
| 84 | |
| 85 | GetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); |
| 86 | SetConsoleCursorInfo(stdouthandle, &cursorinfo); |
| 87 | |
| 88 | SetConsoleCtrlHandler(consolehandler, TRUE); |
| 89 | } |
| 90 | /*-----------------------------------------------------------------------------------*/ |
| 91 | void |
| 92 | console_exit(void) |
| 93 | { |
| 94 | revers(0); |
| 95 | textcolor(saved_foreground); |
| 96 | bgcolor(saved_background); |
| 97 | clrscr(); |
| 98 | gotoxy(0, 0); |
| 99 | |
| 100 | SetConsoleTitle(saved_title); |
| 101 | SetConsoleMode(stdouthandle, saved_consolemode); |
| 102 | SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 103 | } |
| 104 | /*-----------------------------------------------------------------------------------*/ |
| 105 | static void |
| 106 | setcolor(void) |
| 107 | { |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 108 | SetConsoleTextAttribute(stdouthandle, reversed? background + foreground * 0x10 |
| 109 | : foreground + background * 0x10); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 110 | } |
| 111 | /*-----------------------------------------------------------------------------------*/ |
| 112 | unsigned char |
| 113 | wherex(void) |
| 114 | { |
| 115 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 116 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 117 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 118 | return consoleinfo.dwCursorPosition.X; |
| 119 | } |
| 120 | /*-----------------------------------------------------------------------------------*/ |
| 121 | unsigned char |
| 122 | wherey(void) |
| 123 | { |
| 124 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 125 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 126 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 127 | return consoleinfo.dwCursorPosition.Y; |
| 128 | } |
| 129 | /*-----------------------------------------------------------------------------------*/ |
| 130 | void |
| 131 | clrscr(void) |
| 132 | { |
| 133 | unsigned char i, width, height; |
| 134 | |
| 135 | screensize(&width, &height); |
| 136 | for(i = 0; i < height; ++i) { |
| 137 | cclearxy(0, i, width); |
| 138 | } |
| 139 | } |
| 140 | /*-----------------------------------------------------------------------------------*/ |
| 141 | void |
| 142 | revers(unsigned char c) |
| 143 | { |
| 144 | reversed = c; |
| 145 | setcolor(); |
| 146 | } |
| 147 | /*-----------------------------------------------------------------------------------*/ |
| 148 | void |
| 149 | cclear(unsigned char length) |
| 150 | { |
| 151 | int i; |
| 152 | for(i = 0; i < length; ++i) { |
| 153 | putch(' '); |
| 154 | } |
| 155 | } |
| 156 | /*-----------------------------------------------------------------------------------*/ |
| 157 | void |
| 158 | chline(unsigned char length) |
| 159 | { |
| 160 | int i; |
| 161 | for(i = 0; i < length; ++i) { |
| 162 | putch(0xC4); |
| 163 | } |
| 164 | } |
| 165 | /*-----------------------------------------------------------------------------------*/ |
| 166 | void |
| 167 | cvline(unsigned char length) |
| 168 | { |
| 169 | int i; |
| 170 | for(i = 0; i < length; ++i) { |
| 171 | putch(0xB3); |
| 172 | gotoxy(wherex() - 1, wherey() + 1); |
| 173 | } |
| 174 | } |
| 175 | /*-----------------------------------------------------------------------------------*/ |
| 176 | void |
| 177 | gotoxy(unsigned char x, unsigned char y) |
| 178 | { |
| 179 | COORD coord = {x, y}; |
| 180 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 181 | SetConsoleCursorPosition(stdouthandle, coord); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 182 | } |
| 183 | /*-----------------------------------------------------------------------------------*/ |
| 184 | void |
| 185 | cclearxy(unsigned char x, unsigned char y, unsigned char length) |
| 186 | { |
| 187 | gotoxy(x, y); |
| 188 | cclear(length); |
| 189 | } |
| 190 | /*-----------------------------------------------------------------------------------*/ |
| 191 | void |
| 192 | chlinexy(unsigned char x, unsigned char y, unsigned char length) |
| 193 | { |
| 194 | gotoxy(x, y); |
| 195 | chline(length); |
| 196 | } |
| 197 | /*-----------------------------------------------------------------------------------*/ |
| 198 | void |
| 199 | cvlinexy(unsigned char x, unsigned char y, unsigned char length) |
| 200 | { |
| 201 | gotoxy(x, y); |
| 202 | cvline(length); |
| 203 | } |
| 204 | /*-----------------------------------------------------------------------------------*/ |
| 205 | void |
| 206 | cputsxy(unsigned char x, unsigned char y, char *str) |
| 207 | { |
| 208 | gotoxy(x, y); |
| 209 | cputs(str); |
| 210 | } |
| 211 | /*-----------------------------------------------------------------------------------*/ |
| 212 | void |
| 213 | cputcxy(unsigned char x, unsigned char y, char c) |
| 214 | { |
| 215 | gotoxy(x, y); |
| 216 | putch(c); |
| 217 | } |
| 218 | /*-----------------------------------------------------------------------------------*/ |
| 219 | void |
| 220 | textcolor(unsigned char c) |
| 221 | { |
| 222 | foreground = c; |
| 223 | setcolor(); |
| 224 | } |
| 225 | /*-----------------------------------------------------------------------------------*/ |
| 226 | void |
| 227 | bgcolor(unsigned char c) |
| 228 | { |
| 229 | background = c; |
| 230 | setcolor(); |
| 231 | } |
| 232 | /*-----------------------------------------------------------------------------------*/ |
| 233 | void |
| 234 | bordercolor(unsigned char c) |
| 235 | { |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 236 | } |
| 237 | /*-----------------------------------------------------------------------------------*/ |
| 238 | void |
| 239 | screensize(unsigned char *x, unsigned char *y) |
| 240 | { |
| 241 | CONSOLE_SCREEN_BUFFER_INFO consoleinfo; |
| 242 | |
oliverschmidt | b8bb112 | 2004-07-31 14:55:17 +0000 | [diff] [blame^] | 243 | GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); |
oliverschmidt | 06437bc | 2004-07-15 00:31:10 +0000 | [diff] [blame] | 244 | *x = consoleinfo.dwMaximumWindowSize.X; |
| 245 | *y = consoleinfo.dwMaximumWindowSize.Y; |
| 246 | } |
| 247 | /*-----------------------------------------------------------------------------------*/ |