36 #define WIN32_LEAN_AND_MEAN
43 #include "ctk-console.h"
45 static HANDLE stdinhandle;
46 static HANDLE stdouthandle;
48 static unsigned char width;
49 static unsigned char height;
51 static DWORD saved_inputmode;
52 static DWORD saved_outputmode;
53 static unsigned char saved_color;
54 static char saved_title[1024];
55 static CONSOLE_CURSOR_INFO saved_cursorinfo;
57 static unsigned char color;
58 static unsigned char reversed;
60 static unsigned char blank[1024];
61 static unsigned char hline[1024];
64 static unsigned char available;
66 static unsigned short xpos;
67 static unsigned short ypos;
68 static unsigned char button;
72 ctrlhandler(DWORD ctrltype)
74 if(ctrltype == CTRL_C_EVENT) {
84 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
85 CONSOLE_CURSOR_INFO cursorinfo = {1,
FALSE};
86 static unsigned char done;
93 stdinhandle = GetStdHandle(STD_INPUT_HANDLE);
94 stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE);
96 GetConsoleMode(stdinhandle, &saved_inputmode);
97 SetConsoleMode(stdinhandle, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
99 GetConsoleMode(stdouthandle, &saved_outputmode);
100 SetConsoleMode(stdouthandle, ENABLE_PROCESSED_OUTPUT);
102 screensize(&width, &height);
104 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
105 saved_color = (
unsigned char)consoleinfo.wAttributes;
107 GetConsoleTitle(saved_title,
sizeof(saved_title));
108 SetConsoleTitle(
"Contiki");
110 GetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);
111 SetConsoleCursorInfo(stdouthandle, &cursorinfo);
113 SetConsoleCtrlHandler(ctrlhandler,
TRUE);
114 atexit(console_exit);
116 memset(blank,
' ',
sizeof(blank));
117 memset(hline, 0xC4,
sizeof(hline));
123 static unsigned char done;
130 textcolor(saved_color);
135 SetConsoleMode(stdinhandle, saved_inputmode);
136 SetConsoleMode(stdouthandle, saved_outputmode);
137 SetConsoleTitle(saved_title);
138 SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);
144 unsigned char new_width;
145 unsigned char new_height;
147 screensize(&new_width, &new_height);
149 if(new_width != width ||
150 new_height != height) {
162 SetConsoleTextAttribute(stdouthandle, (WORD)(reversed? (color & 0x0F) << 4 |
170 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
172 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
173 return (
unsigned char)consoleinfo.dwCursorPosition.X;
179 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
181 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
182 return (
unsigned char)consoleinfo.dwCursorPosition.Y;
188 unsigned char i, width, height;
190 screensize(&width, &height);
191 for(i = 0; i < height; ++i) {
192 cclearxy(0, i, width);
197 bgcolor(
unsigned char c)
204 bordercolor(
unsigned char c)
211 screensize(
unsigned char *x,
unsigned char *y)
213 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
215 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
216 *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1;
217 *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1;
221 revers(
unsigned char c)
228 console_cputc(
char c)
232 WriteConsole(stdouthandle, &c, 1, &dummy,
NULL);
236 console_cputs(
char *str)
240 WriteConsole(stdouthandle, str, (DWORD)strlen(str), &dummy,
NULL);
244 cclear(
unsigned char length)
248 WriteConsole(stdouthandle, blank, length, &dummy,
NULL);
252 chline(
unsigned char length)
256 WriteConsole(stdouthandle, hline, length, &dummy,
NULL);
260 cvline(
unsigned char length)
262 unsigned char i, x, y;
267 for(i = 0; i < length; ++i) {
268 cputcxy(x, (
unsigned char)(y + i), (
char)0xB3);
273 gotoxy(
unsigned char x,
unsigned char y)
275 COORD coord = {x, y};
277 SetConsoleCursorPosition(stdouthandle, coord);
281 cclearxy(
unsigned char x,
unsigned char y,
unsigned char length)
288 chlinexy(
unsigned char x,
unsigned char y,
unsigned char length)
295 cvlinexy(
unsigned char x,
unsigned char y,
unsigned char length)
302 cputsxy(
unsigned char x,
unsigned char y,
char *str)
309 cputcxy(
unsigned char x,
unsigned char y,
char c)
316 textcolor(
unsigned char c)
323 console_readkey(KEY_EVENT_RECORD keyrecord)
327 if(!keyrecord.bKeyDown) {
331 if(keyrecord.wRepeatCount > (WORD)255 - available) {
332 keyrecord.wRepeatCount = (WORD)255 - available;
335 key = keyrecord.uChar.AsciiChar;
336 if(key == CTK_CONF_WIDGETDOWN_KEY && keyrecord.dwControlKeyState & SHIFT_PRESSED) {
337 key = CTK_CONF_WIDGETUP_KEY;
340 switch(keyrecord.wVirtualKeyCode) {
342 if(keyrecord.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
343 key = CTK_CONF_WINDOWSWITCH_KEY;
359 key = CTK_CONF_MENU_KEY;
368 memset(keys + available, key, keyrecord.wRepeatCount);
369 available += (
unsigned char)keyrecord.wRepeatCount;
373 console_readmouse(MOUSE_EVENT_RECORD mouserecord)
375 xpos = mouserecord.dwMousePosition.X;
376 ypos = mouserecord.dwMousePosition.Y;
378 button = (
unsigned char)mouserecord.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED;
384 INPUT_RECORD inputrecord;
387 if(!GetNumberOfConsoleInputEvents(stdinhandle, &count) || count == 0) {
390 if(!ReadConsoleInput(stdinhandle, &inputrecord, 1, &count) || count == 0) {
394 switch(inputrecord.EventType) {
396 console_readkey(inputrecord.Event.KeyEvent);
399 console_readmouse(inputrecord.Event.MouseEvent);
408 return keys[--available];
438 ctk_mouse_xtoc(
unsigned short x)
444 ctk_mouse_ytoc(
unsigned short y)
450 ctk_mouse_button(
void)