35 #define WIN32_LEAN_AND_MEAN
42 #include "ctk-console.h"
44 static HANDLE stdinhandle;
45 static HANDLE stdouthandle;
47 static unsigned char width;
48 static unsigned char height;
50 static DWORD saved_inputmode;
51 static DWORD saved_outputmode;
52 static unsigned char saved_color;
53 static char saved_title[1024];
54 static CONSOLE_CURSOR_INFO saved_cursorinfo;
56 static unsigned char color;
57 static unsigned char reversed;
59 static unsigned char blank[1024];
60 static unsigned char hline[1024];
63 static unsigned char available;
65 static unsigned short xpos;
66 static unsigned short ypos;
67 static unsigned char button;
71 ctrlhandler(DWORD ctrltype)
73 if(ctrltype == CTRL_C_EVENT) {
83 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
84 CONSOLE_CURSOR_INFO cursorinfo = {1,
FALSE};
85 static unsigned char done;
92 stdinhandle = GetStdHandle(STD_INPUT_HANDLE);
93 stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE);
95 GetConsoleMode(stdinhandle, &saved_inputmode);
96 SetConsoleMode(stdinhandle, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
98 GetConsoleMode(stdouthandle, &saved_outputmode);
99 SetConsoleMode(stdouthandle, ENABLE_PROCESSED_OUTPUT);
101 screensize(&width, &height);
103 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
104 saved_color = (
unsigned char)consoleinfo.wAttributes;
106 GetConsoleTitle(saved_title,
sizeof(saved_title));
107 SetConsoleTitle(
"Contiki");
109 GetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);
110 SetConsoleCursorInfo(stdouthandle, &cursorinfo);
112 SetConsoleCtrlHandler(ctrlhandler,
TRUE);
113 atexit(console_exit);
115 memset(blank,
' ',
sizeof(blank));
116 memset(hline,
'-',
sizeof(hline));
122 static unsigned char done;
129 textcolor(saved_color);
134 SetConsoleMode(stdinhandle, saved_inputmode);
135 SetConsoleMode(stdouthandle, saved_outputmode);
136 SetConsoleTitle(saved_title);
137 SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);
143 unsigned char new_width;
144 unsigned char new_height;
146 screensize(&new_width, &new_height);
148 if(new_width != width ||
149 new_height != height) {
161 SetConsoleTextAttribute(stdouthandle, (WORD)(reversed? (color & 0x0F) << 4 |
169 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
171 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
172 return (
unsigned char)consoleinfo.dwCursorPosition.X;
178 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
180 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
181 return (
unsigned char)consoleinfo.dwCursorPosition.Y;
187 unsigned char i, width, height;
189 screensize(&width, &height);
190 for(i = 0; i < height; ++i) {
191 cclearxy(0, i, width);
196 bgcolor(
unsigned char c)
203 bordercolor(
unsigned char c)
210 screensize(
unsigned char *x,
unsigned char *y)
212 CONSOLE_SCREEN_BUFFER_INFO consoleinfo;
214 GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo);
215 *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1;
216 *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1;
220 revers(
unsigned char c)
227 console_cputc(
char c)
231 WriteConsole(stdouthandle, &c, 1, &dummy,
NULL);
235 console_cputs(
char *str)
239 WriteConsole(stdouthandle, str, (DWORD)strlen(str), &dummy,
NULL);
243 cclear(
unsigned char length)
247 WriteConsole(stdouthandle, blank, length, &dummy,
NULL);
251 chline(
unsigned char length)
255 WriteConsole(stdouthandle, hline, length, &dummy,
NULL);
259 cvline(
unsigned char length)
261 unsigned char i, x, y;
266 for(i = 0; i < length; ++i) {
267 cputcxy(x, (
unsigned char)(y + i),
'|');
272 gotoxy(
unsigned char x,
unsigned char y)
274 COORD coord = {x, y};
276 SetConsoleCursorPosition(stdouthandle, coord);
280 cclearxy(
unsigned char x,
unsigned char y,
unsigned char length)
287 chlinexy(
unsigned char x,
unsigned char y,
unsigned char length)
294 cvlinexy(
unsigned char x,
unsigned char y,
unsigned char length)
301 cputsxy(
unsigned char x,
unsigned char y,
char *str)
308 cputcxy(
unsigned char x,
unsigned char y,
char c)
315 textcolor(
unsigned char c)
322 console_readkey(KEY_EVENT_RECORD keyrecord)
326 if(!keyrecord.bKeyDown) {
330 if(keyrecord.wRepeatCount > (WORD)255 - available) {
331 keyrecord.wRepeatCount = (WORD)255 - available;
334 key = keyrecord.uChar.AsciiChar;
335 if(key == CTK_CONF_WIDGETDOWN_KEY && keyrecord.dwControlKeyState & SHIFT_PRESSED) {
336 key = CTK_CONF_WIDGETUP_KEY;
339 switch(keyrecord.wVirtualKeyCode) {
341 if(keyrecord.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
342 key = CTK_CONF_WINDOWSWITCH_KEY;
358 key = CTK_CONF_MENU_KEY;
367 memset(keys + available, key, keyrecord.wRepeatCount);
368 available += (
unsigned char)keyrecord.wRepeatCount;
372 console_readmouse(MOUSE_EVENT_RECORD mouserecord)
374 xpos = mouserecord.dwMousePosition.X;
375 ypos = mouserecord.dwMousePosition.Y;
377 button = (
unsigned char)mouserecord.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED;
383 INPUT_RECORD inputrecord;
386 if(!GetNumberOfConsoleInputEvents(stdinhandle, &count) || count == 0) {
389 if(!ReadConsoleInput(stdinhandle, &inputrecord, 1, &count) || count == 0) {
393 switch(inputrecord.EventType) {
395 console_readkey(inputrecord.Event.KeyEvent);
398 console_readmouse(inputrecord.Event.MouseEvent);
407 return keys[--available];
437 ctk_mouse_xtoc(
unsigned short x)
443 ctk_mouse_ytoc(
unsigned short y)
449 ctk_mouse_button(
void)
char ctk_arch_key_t
The keyboard character type of the system.
#define NULL
The null pointer.
unsigned char ctk_arch_keyavail(void)
Check if there is a keypress in the keyboard input queue.
#define TRUE
An alias for one, used for clarity.
ctk_arch_key_t ctk_arch_getkey(void)
Get a keypress from the keyboard input queue.
#define FALSE
An alias for zero, used for clarity.