blob: b532f50d5f793290332323ba163b686888740617 [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 *
oliverschmidte80365d2004-08-12 22:53:22 +000032 * $Id: ctk-console.c,v 1.4 2004/08/12 22:53:22 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
oliverschmidte80365d2004-08-12 22:53:22 +000045static unsigned char width;
46static unsigned char height;
47
oliverschmidt89963452004-08-12 22:09:34 +000048static unsigned char saved_color;
oliverschmidtb8bb1122004-07-31 14:55:17 +000049static char saved_title[1024];
50static DWORD saved_consolemode;
51static CONSOLE_CURSOR_INFO saved_cursorinfo;
52
oliverschmidt89963452004-08-12 22:09:34 +000053static unsigned char color;
oliverschmidt06437bc2004-07-15 00:31:10 +000054static unsigned char reversed;
oliverschmidt06437bc2004-07-15 00:31:10 +000055
56/*-----------------------------------------------------------------------------------*/
oliverschmidtb8bb1122004-07-31 14:55:17 +000057static BOOL WINAPI
58consolehandler(DWORD event)
oliverschmidt06437bc2004-07-15 00:31:10 +000059{
oliverschmidtb8bb1122004-07-31 14:55:17 +000060 if(event == CTRL_C_EVENT ||
61 event == CTRL_BREAK_EVENT) {
62 console_exit();
oliverschmidt06437bc2004-07-15 00:31:10 +000063 }
oliverschmidtb8bb1122004-07-31 14:55:17 +000064
65 return FALSE;
66}
67/*-----------------------------------------------------------------------------------*/
68void
69console_init(void)
70{
71 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
72 CONSOLE_CURSOR_INFO cursorinfo = {1, FALSE};
73
74 stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE);
75
oliverschmidte80365d2004-08-12 22:53:22 +000076 screensize(&width, &height);
77
oliverschmidtb8bb1122004-07-31 14:55:17 +000078 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt89963452004-08-12 22:09:34 +000079 saved_color = consoleinfo.wAttributes;
oliverschmidtb8bb1122004-07-31 14:55:17 +000080
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/*-----------------------------------------------------------------------------------*/
93void
94console_exit(void)
95{
oliverschmidt89963452004-08-12 22:09:34 +000096 textcolor(saved_color);
oliverschmidtb8bb1122004-07-31 14:55:17 +000097 revers(0);
oliverschmidtb8bb1122004-07-31 14:55:17 +000098 clrscr();
99 gotoxy(0, 0);
100
101 SetConsoleTitle(saved_title);
102 SetConsoleMode(stdouthandle, saved_consolemode);
103 SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000104}
105/*-----------------------------------------------------------------------------------*/
oliverschmidte80365d2004-08-12 22:53:22 +0000106unsigned char
107console_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/*-----------------------------------------------------------------------------------*/
oliverschmidt06437bc2004-07-15 00:31:10 +0000124static void
125setcolor(void)
126{
oliverschmidt89963452004-08-12 22:09:34 +0000127 SetConsoleTextAttribute(stdouthandle, reversed? (color & 0x0F) << 4 |
128 (color & 0xF0) >> 4
129 : color);
oliverschmidt06437bc2004-07-15 00:31:10 +0000130}
131/*-----------------------------------------------------------------------------------*/
132unsigned char
133wherex(void)
134{
135 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
136
oliverschmidtb8bb1122004-07-31 14:55:17 +0000137 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000138 return consoleinfo.dwCursorPosition.X;
139}
140/*-----------------------------------------------------------------------------------*/
141unsigned char
142wherey(void)
143{
144 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
145
oliverschmidtb8bb1122004-07-31 14:55:17 +0000146 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000147 return consoleinfo.dwCursorPosition.Y;
148}
149/*-----------------------------------------------------------------------------------*/
150void
151clrscr(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/*-----------------------------------------------------------------------------------*/
161void
162revers(unsigned char c)
163{
164 reversed = c;
165 setcolor();
166}
167/*-----------------------------------------------------------------------------------*/
168void
169cclear(unsigned char length)
170{
171 int i;
172 for(i = 0; i < length; ++i) {
173 putch(' ');
174 }
175}
176/*-----------------------------------------------------------------------------------*/
177void
178chline(unsigned char length)
179{
180 int i;
181 for(i = 0; i < length; ++i) {
182 putch(0xC4);
183 }
184}
185/*-----------------------------------------------------------------------------------*/
186void
187cvline(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/*-----------------------------------------------------------------------------------*/
196void
197gotoxy(unsigned char x, unsigned char y)
198{
199 COORD coord = {x, y};
200
oliverschmidtb8bb1122004-07-31 14:55:17 +0000201 SetConsoleCursorPosition(stdouthandle, coord);
oliverschmidt06437bc2004-07-15 00:31:10 +0000202}
203/*-----------------------------------------------------------------------------------*/
204void
205cclearxy(unsigned char x, unsigned char y, unsigned char length)
206{
207 gotoxy(x, y);
208 cclear(length);
209}
210/*-----------------------------------------------------------------------------------*/
211void
212chlinexy(unsigned char x, unsigned char y, unsigned char length)
213{
214 gotoxy(x, y);
215 chline(length);
216}
217/*-----------------------------------------------------------------------------------*/
218void
219cvlinexy(unsigned char x, unsigned char y, unsigned char length)
220{
221 gotoxy(x, y);
222 cvline(length);
223}
224/*-----------------------------------------------------------------------------------*/
225void
226cputsxy(unsigned char x, unsigned char y, char *str)
227{
228 gotoxy(x, y);
229 cputs(str);
230}
231/*-----------------------------------------------------------------------------------*/
232void
233cputcxy(unsigned char x, unsigned char y, char c)
234{
235 gotoxy(x, y);
236 putch(c);
237}
238/*-----------------------------------------------------------------------------------*/
239void
240textcolor(unsigned char c)
241{
oliverschmidt89963452004-08-12 22:09:34 +0000242 color = c;
oliverschmidt06437bc2004-07-15 00:31:10 +0000243 setcolor();
244}
245/*-----------------------------------------------------------------------------------*/
246void
247bgcolor(unsigned char c)
248{
oliverschmidt06437bc2004-07-15 00:31:10 +0000249}
250/*-----------------------------------------------------------------------------------*/
251void
252bordercolor(unsigned char c)
253{
oliverschmidt06437bc2004-07-15 00:31:10 +0000254}
255/*-----------------------------------------------------------------------------------*/
256void
257screensize(unsigned char *x, unsigned char *y)
258{
259 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
260
oliverschmidtb8bb1122004-07-31 14:55:17 +0000261 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidte80365d2004-08-12 22:53:22 +0000262 *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1;
263 *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1;
oliverschmidt06437bc2004-07-15 00:31:10 +0000264}
265/*-----------------------------------------------------------------------------------*/