blob: d225b1656348cca9c1ca7baa23f04d40adf1cd03 [file] [log] [blame]
oliverschmidt06437bc2004-07-15 00:31:10 +00001/*
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 *
oliverschmidtb8bb1122004-07-31 14:55:17 +000032 * $Id: ctk-console.c,v 1.2 2004/07/31 14:55:17 oliverschmidt Exp $
oliverschmidt06437bc2004-07-15 00:31:10 +000033 *
34 */
35
36#define WIN32_LEAN_AND_MEAN
37#include <windows.h>
oliverschmidtb8bb1122004-07-31 14:55:17 +000038#include <stdlib.h>
oliverschmidt06437bc2004-07-15 00:31:10 +000039#include <conio.h>
40
41#include "ctk-console.h"
42
oliverschmidtb8bb1122004-07-31 14:55:17 +000043static HANDLE stdouthandle;
44
45static unsigned char saved_foreground;
46static unsigned char saved_background;
47static char saved_title[1024];
48static DWORD saved_consolemode;
49static CONSOLE_CURSOR_INFO saved_cursorinfo;
50
oliverschmidt06437bc2004-07-15 00:31:10 +000051static unsigned char reversed;
52static unsigned char foreground;
53static unsigned char background;
54
55/*-----------------------------------------------------------------------------------*/
oliverschmidtb8bb1122004-07-31 14:55:17 +000056static BOOL WINAPI
57consolehandler(DWORD event)
oliverschmidt06437bc2004-07-15 00:31:10 +000058{
oliverschmidtb8bb1122004-07-31 14:55:17 +000059 if(event == CTRL_C_EVENT ||
60 event == CTRL_BREAK_EVENT) {
61 console_exit();
oliverschmidt06437bc2004-07-15 00:31:10 +000062 }
oliverschmidtb8bb1122004-07-31 14:55:17 +000063
64 return FALSE;
65}
66/*-----------------------------------------------------------------------------------*/
67void
68console_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/*-----------------------------------------------------------------------------------*/
91void
92console_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);
oliverschmidt06437bc2004-07-15 00:31:10 +0000103}
104/*-----------------------------------------------------------------------------------*/
105static void
106setcolor(void)
107{
oliverschmidtb8bb1122004-07-31 14:55:17 +0000108 SetConsoleTextAttribute(stdouthandle, reversed? background + foreground * 0x10
109 : foreground + background * 0x10);
oliverschmidt06437bc2004-07-15 00:31:10 +0000110}
111/*-----------------------------------------------------------------------------------*/
112unsigned char
113wherex(void)
114{
115 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
116
oliverschmidtb8bb1122004-07-31 14:55:17 +0000117 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000118 return consoleinfo.dwCursorPosition.X;
119}
120/*-----------------------------------------------------------------------------------*/
121unsigned char
122wherey(void)
123{
124 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
125
oliverschmidtb8bb1122004-07-31 14:55:17 +0000126 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000127 return consoleinfo.dwCursorPosition.Y;
128}
129/*-----------------------------------------------------------------------------------*/
130void
131clrscr(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/*-----------------------------------------------------------------------------------*/
141void
142revers(unsigned char c)
143{
144 reversed = c;
145 setcolor();
146}
147/*-----------------------------------------------------------------------------------*/
148void
149cclear(unsigned char length)
150{
151 int i;
152 for(i = 0; i < length; ++i) {
153 putch(' ');
154 }
155}
156/*-----------------------------------------------------------------------------------*/
157void
158chline(unsigned char length)
159{
160 int i;
161 for(i = 0; i < length; ++i) {
162 putch(0xC4);
163 }
164}
165/*-----------------------------------------------------------------------------------*/
166void
167cvline(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/*-----------------------------------------------------------------------------------*/
176void
177gotoxy(unsigned char x, unsigned char y)
178{
179 COORD coord = {x, y};
180
oliverschmidtb8bb1122004-07-31 14:55:17 +0000181 SetConsoleCursorPosition(stdouthandle, coord);
oliverschmidt06437bc2004-07-15 00:31:10 +0000182}
183/*-----------------------------------------------------------------------------------*/
184void
185cclearxy(unsigned char x, unsigned char y, unsigned char length)
186{
187 gotoxy(x, y);
188 cclear(length);
189}
190/*-----------------------------------------------------------------------------------*/
191void
192chlinexy(unsigned char x, unsigned char y, unsigned char length)
193{
194 gotoxy(x, y);
195 chline(length);
196}
197/*-----------------------------------------------------------------------------------*/
198void
199cvlinexy(unsigned char x, unsigned char y, unsigned char length)
200{
201 gotoxy(x, y);
202 cvline(length);
203}
204/*-----------------------------------------------------------------------------------*/
205void
206cputsxy(unsigned char x, unsigned char y, char *str)
207{
208 gotoxy(x, y);
209 cputs(str);
210}
211/*-----------------------------------------------------------------------------------*/
212void
213cputcxy(unsigned char x, unsigned char y, char c)
214{
215 gotoxy(x, y);
216 putch(c);
217}
218/*-----------------------------------------------------------------------------------*/
219void
220textcolor(unsigned char c)
221{
222 foreground = c;
223 setcolor();
224}
225/*-----------------------------------------------------------------------------------*/
226void
227bgcolor(unsigned char c)
228{
229 background = c;
230 setcolor();
231}
232/*-----------------------------------------------------------------------------------*/
233void
234bordercolor(unsigned char c)
235{
oliverschmidt06437bc2004-07-15 00:31:10 +0000236}
237/*-----------------------------------------------------------------------------------*/
238void
239screensize(unsigned char *x, unsigned char *y)
240{
241 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
242
oliverschmidtb8bb1122004-07-31 14:55:17 +0000243 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000244 *x = consoleinfo.dwMaximumWindowSize.X;
245 *y = consoleinfo.dwMaximumWindowSize.Y;
246}
247/*-----------------------------------------------------------------------------------*/