blob: 5467cbe86712fa9821a81502ce5fab473caa885d [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 *
oliverschmidt7f0b22b2005-01-29 23:10:43 +000032 * $Id: ctk-console.c,v 1.5 2005/01/29 23:11:09 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);
oliverschmidt89963452004-08-12 22:09:34 +000075 saved_color = 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{
oliverschmidt89963452004-08-12 22:09:34 +0000123 SetConsoleTextAttribute(stdouthandle, 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);
oliverschmidt06437bc2004-07-15 00:31:10 +0000134 return consoleinfo.dwCursorPosition.X;
135}
136/*-----------------------------------------------------------------------------------*/
137unsigned char
138wherey(void)
139{
140 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
141
oliverschmidtb8bb1122004-07-31 14:55:17 +0000142 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidt06437bc2004-07-15 00:31:10 +0000143 return consoleinfo.dwCursorPosition.Y;
144}
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{
167 int i;
168 for(i = 0; i < length; ++i) {
169 putch(' ');
170 }
171}
172/*-----------------------------------------------------------------------------------*/
173void
174chline(unsigned char length)
175{
176 int i;
177 for(i = 0; i < length; ++i) {
178 putch(0xC4);
179 }
180}
181/*-----------------------------------------------------------------------------------*/
182void
183cvline(unsigned char length)
184{
185 int i;
186 for(i = 0; i < length; ++i) {
187 putch(0xB3);
188 gotoxy(wherex() - 1, wherey() + 1);
189 }
190}
191/*-----------------------------------------------------------------------------------*/
192void
193gotoxy(unsigned char x, unsigned char y)
194{
195 COORD coord = {x, y};
196
oliverschmidtb8bb1122004-07-31 14:55:17 +0000197 SetConsoleCursorPosition(stdouthandle, coord);
oliverschmidt06437bc2004-07-15 00:31:10 +0000198}
199/*-----------------------------------------------------------------------------------*/
200void
201cclearxy(unsigned char x, unsigned char y, unsigned char length)
202{
203 gotoxy(x, y);
204 cclear(length);
205}
206/*-----------------------------------------------------------------------------------*/
207void
208chlinexy(unsigned char x, unsigned char y, unsigned char length)
209{
210 gotoxy(x, y);
211 chline(length);
212}
213/*-----------------------------------------------------------------------------------*/
214void
215cvlinexy(unsigned char x, unsigned char y, unsigned char length)
216{
217 gotoxy(x, y);
218 cvline(length);
219}
220/*-----------------------------------------------------------------------------------*/
221void
222cputsxy(unsigned char x, unsigned char y, char *str)
223{
224 gotoxy(x, y);
225 cputs(str);
226}
227/*-----------------------------------------------------------------------------------*/
228void
229cputcxy(unsigned char x, unsigned char y, char c)
230{
231 gotoxy(x, y);
232 putch(c);
233}
234/*-----------------------------------------------------------------------------------*/
235void
236textcolor(unsigned char c)
237{
oliverschmidt89963452004-08-12 22:09:34 +0000238 color = c;
oliverschmidt06437bc2004-07-15 00:31:10 +0000239 setcolor();
240}
241/*-----------------------------------------------------------------------------------*/
242void
243bgcolor(unsigned char c)
244{
oliverschmidt06437bc2004-07-15 00:31:10 +0000245}
246/*-----------------------------------------------------------------------------------*/
247void
248bordercolor(unsigned char c)
249{
oliverschmidt06437bc2004-07-15 00:31:10 +0000250}
251/*-----------------------------------------------------------------------------------*/
252void
253screensize(unsigned char *x, unsigned char *y)
254{
255 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
256
oliverschmidtb8bb1122004-07-31 14:55:17 +0000257 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
oliverschmidte80365d2004-08-12 22:53:22 +0000258 *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1;
259 *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1;
oliverschmidt06437bc2004-07-15 00:31:10 +0000260}
261/*-----------------------------------------------------------------------------------*/