blob: 4cf32657f4eaba2f02fcee3b50b60d12de2d1715 [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 *
oliverschmidt260ddbb2007-09-05 12:43:20 +000032 * $Id: ctk-console.c,v 1.7 2007/09/05 12:43:20 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{
oliverschmidt7f0b22b2005-01-29 23:10:43 +000060 console_exit();
61 exit(EXIT_SUCCESS);
oliverschmidtb8bb1122004-07-31 14:55:17 +000062}
63/*-----------------------------------------------------------------------------------*/
64void
65console_init(void)
66{
67 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
68 CONSOLE_CURSOR_INFO cursorinfo = {1, FALSE};
69
70 stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE);
71
oliverschmidte80365d2004-08-12 22:53:22 +000072 screensize(&width, &height);
73
oliverschmidtb8bb1122004-07-31 14:55:17 +000074 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt81c744e2005-02-06 21:13:59 +000075 saved_color = (unsigned char)consoleinfo.wAttributes;
oliverschmidtb8bb1122004-07-31 14:55:17 +000076
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/*-----------------------------------------------------------------------------------*/
89void
90console_exit(void)
91{
oliverschmidt89963452004-08-12 22:09:34 +000092 textcolor(saved_color);
oliverschmidtb8bb1122004-07-31 14:55:17 +000093 revers(0);
oliverschmidtb8bb1122004-07-31 14:55:17 +000094 clrscr();
95 gotoxy(0, 0);
96
97 SetConsoleTitle(saved_title);
98 SetConsoleMode(stdouthandle, saved_consolemode);
99 SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000100}
101/*-----------------------------------------------------------------------------------*/
oliverschmidte80365d2004-08-12 22:53:22 +0000102unsigned char
103console_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/*-----------------------------------------------------------------------------------*/
oliverschmidt06437bc2004-07-15 00:31:10 +0000120static void
121setcolor(void)
122{
oliverschmidt81c744e2005-02-06 21:13:59 +0000123 SetConsoleTextAttribute(stdouthandle, (WORD)(reversed? (color & 0x0F) << 4 |
124 (color & 0xF0) >> 4
125 : color));
oliverschmidt06437bc2004-07-15 00:31:10 +0000126}
127/*-----------------------------------------------------------------------------------*/
128unsigned char
129wherex(void)
130{
131 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
132
oliverschmidtb8bb1122004-07-31 14:55:17 +0000133 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt81c744e2005-02-06 21:13:59 +0000134 return (unsigned char)consoleinfo.dwCursorPosition.X;
oliverschmidt06437bc2004-07-15 00:31:10 +0000135}
136/*-----------------------------------------------------------------------------------*/
137unsigned char
138wherey(void)
139{
140 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
141
oliverschmidtb8bb1122004-07-31 14:55:17 +0000142 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt81c744e2005-02-06 21:13:59 +0000143 return (unsigned char)consoleinfo.dwCursorPosition.Y;
oliverschmidt06437bc2004-07-15 00:31:10 +0000144}
145/*-----------------------------------------------------------------------------------*/
146void
147clrscr(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/*-----------------------------------------------------------------------------------*/
157void
158revers(unsigned char c)
159{
160 reversed = c;
161 setcolor();
162}
163/*-----------------------------------------------------------------------------------*/
164void
165cclear(unsigned char length)
166{
oliverschmidt81c744e2005-02-06 21:13:59 +0000167 unsigned char i;
168
oliverschmidt06437bc2004-07-15 00:31:10 +0000169 for(i = 0; i < length; ++i) {
170 putch(' ');
171 }
172}
173/*-----------------------------------------------------------------------------------*/
174void
175chline(unsigned char length)
176{
oliverschmidt81c744e2005-02-06 21:13:59 +0000177 unsigned char i;
178
oliverschmidt06437bc2004-07-15 00:31:10 +0000179 for(i = 0; i < length; ++i) {
oliverschmidt260ddbb2007-09-05 12:43:20 +0000180 putch('-');
oliverschmidt06437bc2004-07-15 00:31:10 +0000181 }
182}
183/*-----------------------------------------------------------------------------------*/
184void
185cvline(unsigned char length)
186{
oliverschmidt81c744e2005-02-06 21:13:59 +0000187 unsigned char i, x, y;
188
189 x = wherex();
190 y = wherey();
191
oliverschmidt06437bc2004-07-15 00:31:10 +0000192 for(i = 0; i < length; ++i) {
oliverschmidt260ddbb2007-09-05 12:43:20 +0000193 cputcxy(x, (unsigned char)(y + i), '|');
oliverschmidt06437bc2004-07-15 00:31:10 +0000194 }
195}
196/*-----------------------------------------------------------------------------------*/
197void
198gotoxy(unsigned char x, unsigned char y)
199{
200 COORD coord = {x, y};
201
oliverschmidtb8bb1122004-07-31 14:55:17 +0000202 SetConsoleCursorPosition(stdouthandle, coord);
oliverschmidt06437bc2004-07-15 00:31:10 +0000203}
204/*-----------------------------------------------------------------------------------*/
205void
206cclearxy(unsigned char x, unsigned char y, unsigned char length)
207{
208 gotoxy(x, y);
209 cclear(length);
210}
211/*-----------------------------------------------------------------------------------*/
212void
213chlinexy(unsigned char x, unsigned char y, unsigned char length)
214{
215 gotoxy(x, y);
216 chline(length);
217}
218/*-----------------------------------------------------------------------------------*/
219void
220cvlinexy(unsigned char x, unsigned char y, unsigned char length)
221{
222 gotoxy(x, y);
223 cvline(length);
224}
225/*-----------------------------------------------------------------------------------*/
226void
227cputsxy(unsigned char x, unsigned char y, char *str)
228{
229 gotoxy(x, y);
230 cputs(str);
231}
232/*-----------------------------------------------------------------------------------*/
233void
234cputcxy(unsigned char x, unsigned char y, char c)
235{
236 gotoxy(x, y);
237 putch(c);
238}
239/*-----------------------------------------------------------------------------------*/
240void
241textcolor(unsigned char c)
242{
oliverschmidt89963452004-08-12 22:09:34 +0000243 color = c;
oliverschmidt06437bc2004-07-15 00:31:10 +0000244 setcolor();
245}
246/*-----------------------------------------------------------------------------------*/
247void
248bgcolor(unsigned char c)
249{
oliverschmidt06437bc2004-07-15 00:31:10 +0000250}
251/*-----------------------------------------------------------------------------------*/
252void
253bordercolor(unsigned char c)
254{
oliverschmidt06437bc2004-07-15 00:31:10 +0000255}
256/*-----------------------------------------------------------------------------------*/
257void
258screensize(unsigned char *x, unsigned char *y)
259{
260 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
261
oliverschmidtb8bb1122004-07-31 14:55:17 +0000262 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidte80365d2004-08-12 22:53:22 +0000263 *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1;
264 *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1;
oliverschmidt06437bc2004-07-15 00:31:10 +0000265}
266/*-----------------------------------------------------------------------------------*/