📄 ctk-console.c
字号:
/* * Copyright (c) 2006, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This file is part of the Contiki operating system. * * Author: Oliver Schmidt <ol.sc@web.de> * * $Id: ctk-console.c,v 1.5 2006/10/03 11:27:51 oliverschmidt Exp $ */#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <stdlib.h>#include "contiki.h"#include "ctk-console.h"static HANDLE stdinhandle;static HANDLE stdouthandle;static unsigned char width;static unsigned char height;static DWORD saved_inputmode;static DWORD saved_outputmode;static unsigned char saved_color;static char saved_title[1024];static CONSOLE_CURSOR_INFO saved_cursorinfo;static unsigned char color;static unsigned char reversed;static unsigned char blank[1024];static unsigned char hline[1024];static ctk_arch_key_t keys[256];static unsigned char available;static unsigned short xpos;static unsigned short ypos;static unsigned char button;/*-----------------------------------------------------------------------------------*/voidconsole_init(void){ CONSOLE_SCREEN_BUFFER_INFO consoleinfo; CONSOLE_CURSOR_INFO cursorinfo = {1, FALSE}; stdinhandle = GetStdHandle(STD_INPUT_HANDLE); stdouthandle = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleMode(stdinhandle, &saved_inputmode); SetConsoleMode(stdinhandle, ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT); GetConsoleMode(stdouthandle, &saved_outputmode); SetConsoleMode(stdouthandle, ENABLE_PROCESSED_OUTPUT); screensize(&width, &height); GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); saved_color = (unsigned char)consoleinfo.wAttributes; GetConsoleTitle(saved_title, sizeof(saved_title)); SetConsoleTitle("Contiki"); GetConsoleCursorInfo(stdouthandle, &saved_cursorinfo); SetConsoleCursorInfo(stdouthandle, &cursorinfo); atexit(console_exit); memset(blank, ' ', sizeof(blank)); memset(hline, 0xC4, sizeof(hline));}/*-----------------------------------------------------------------------------------*/voidconsole_exit(void){ static unsigned char done; if(done) { return; } done = 1; textcolor(saved_color); revers(0); clrscr(); gotoxy(0, 0); SetConsoleMode(stdinhandle, saved_inputmode); SetConsoleMode(stdouthandle, saved_outputmode); SetConsoleTitle(saved_title); SetConsoleCursorInfo(stdouthandle, &saved_cursorinfo);}/*-----------------------------------------------------------------------------------*/unsigned charconsole_resize(void){ unsigned char new_width; unsigned char new_height; screensize(&new_width, &new_height); if(new_width != width || new_height != height) { width = new_width; height = new_height; return 1; } return 0;}/*-----------------------------------------------------------------------------------*/static voidsetcolor(void){ SetConsoleTextAttribute(stdouthandle, (WORD)(reversed? (color & 0x0F) << 4 | (color & 0xF0) >> 4 : color));}/*-----------------------------------------------------------------------------------*/unsigned charwherex(void){ CONSOLE_SCREEN_BUFFER_INFO consoleinfo; GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); return (unsigned char)consoleinfo.dwCursorPosition.X;}/*-----------------------------------------------------------------------------------*/unsigned charwherey(void){ CONSOLE_SCREEN_BUFFER_INFO consoleinfo; GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); return (unsigned char)consoleinfo.dwCursorPosition.Y;}/*-----------------------------------------------------------------------------------*/voidclrscr(void){ unsigned char i, width, height; screensize(&width, &height); for(i = 0; i < height; ++i) { cclearxy(0, i, width); }}/*-----------------------------------------------------------------------------------*/voidbgcolor(unsigned char c){}/*-----------------------------------------------------------------------------------*/voidbordercolor(unsigned char c){}/*-----------------------------------------------------------------------------------*/voidscreensize(unsigned char *x, unsigned char *y){ CONSOLE_SCREEN_BUFFER_INFO consoleinfo; GetConsoleScreenBufferInfo(stdouthandle, &consoleinfo); *x = consoleinfo.srWindow.Right - consoleinfo.srWindow.Left + 1; *y = consoleinfo.srWindow.Bottom - consoleinfo.srWindow.Top + 1;}/*-----------------------------------------------------------------------------------*/voidrevers(unsigned char c){ reversed = c; setcolor();}/*-----------------------------------------------------------------------------------*/voidconsole_cputc(char c){ DWORD dummy; WriteConsole(stdouthandle, &c, 1, &dummy, NULL);}/*-----------------------------------------------------------------------------------*/voidconsole_cputs(char *str){ DWORD dummy; WriteConsole(stdouthandle, str, (DWORD)strlen(str), &dummy, NULL);}/*-----------------------------------------------------------------------------------*/voidcclear(unsigned char length){ DWORD dummy; WriteConsole(stdouthandle, blank, length, &dummy, NULL);}/*-----------------------------------------------------------------------------------*/voidchline(unsigned char length){ DWORD dummy; WriteConsole(stdouthandle, hline, length, &dummy, NULL);}/*-----------------------------------------------------------------------------------*/voidcvline(unsigned char length){ unsigned char i, x, y; x = wherex(); y = wherey(); for(i = 0; i < length; ++i) { cputcxy(x, (unsigned char)(y + i), (char)0xB3); }}/*-----------------------------------------------------------------------------------*/voidgotoxy(unsigned char x, unsigned char y){ COORD coord = {x, y}; SetConsoleCursorPosition(stdouthandle, coord);}/*-----------------------------------------------------------------------------------*/voidcclearxy(unsigned char x, unsigned char y, unsigned char length){ gotoxy(x, y); cclear(length);}/*-----------------------------------------------------------------------------------*/voidchlinexy(unsigned char x, unsigned char y, unsigned char length){ gotoxy(x, y); chline(length);}/*-----------------------------------------------------------------------------------*/voidcvlinexy(unsigned char x, unsigned char y, unsigned char length){ gotoxy(x, y); cvline(length);}/*-----------------------------------------------------------------------------------*/voidcputsxy(unsigned char x, unsigned char y, char *str){ gotoxy(x, y); console_cputs(str);}/*-----------------------------------------------------------------------------------*/voidcputcxy(unsigned char x, unsigned char y, char c){ gotoxy(x, y); console_cputc(c);}/*-----------------------------------------------------------------------------------*/voidtextcolor(unsigned char c){ color = c; setcolor();}/*-----------------------------------------------------------------------------------*/static voidconsole_readkey(KEY_EVENT_RECORD keyrecord){ ctk_arch_key_t key; if(!keyrecord.bKeyDown) { return; } if(keyrecord.wRepeatCount > (WORD)255 - available) { keyrecord.wRepeatCount = (WORD)255 - available; } key = keyrecord.uChar.AsciiChar; if(key == CTK_CONF_WIDGETDOWN_KEY && keyrecord.dwControlKeyState & SHIFT_PRESSED) { key = CTK_CONF_WIDGETUP_KEY; } if(key == 0) { switch(keyrecord.wVirtualKeyCode) { case VK_TAB: if(keyrecord.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) { key = CTK_CONF_WINDOWSWITCH_KEY; } break; case VK_LEFT: key = CH_CURS_LEFT; break; case VK_UP: key = CH_CURS_UP; break; case VK_RIGHT: key = CH_CURS_RIGHT; break; case VK_DOWN: key = CH_CURS_DOWN; break; case VK_F10: key = CTK_CONF_MENU_KEY; break; } } if(key == 0) { return; } memset(keys + available, key, keyrecord.wRepeatCount); available += (unsigned char)keyrecord.wRepeatCount;}/*-----------------------------------------------------------------------------------*/static voidconsole_readmouse(MOUSE_EVENT_RECORD mouserecord){ xpos = mouserecord.dwMousePosition.X; ypos = mouserecord.dwMousePosition.Y; button = (unsigned char)mouserecord.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED;}/*-----------------------------------------------------------------------------------*/static voidconsole_read(void){ INPUT_RECORD inputrecord; DWORD count; if(!GetNumberOfConsoleInputEvents(stdinhandle, &count) || count == 0) { return; } if(!ReadConsoleInput(stdinhandle, &inputrecord, 1, &count) || count == 0) { return; } switch(inputrecord.EventType) { case KEY_EVENT: console_readkey(inputrecord.Event.KeyEvent); break; case MOUSE_EVENT: console_readmouse(inputrecord.Event.MouseEvent); break; }}/*-----------------------------------------------------------------------------------*/ctk_arch_key_tctk_arch_getkey(void){ console_read(); return keys[--available];}/*-----------------------------------------------------------------------------------*/unsigned charctk_arch_keyavail(void){ console_read(); return available;}/*-----------------------------------------------------------------------------------*/voidctk_mouse_init(void){}/*-----------------------------------------------------------------------------------*/unsigned shortctk_mouse_x(void){ console_read(); return xpos * 8;}/*-----------------------------------------------------------------------------------*/unsigned shortctk_mouse_y(void){ console_read(); return ypos * 8;}/*-----------------------------------------------------------------------------------*/unsigned charctk_mouse_button(void){ console_read(); return button;}/*-----------------------------------------------------------------------------------*/voidctk_mouse_hide(void){}/*-----------------------------------------------------------------------------------*/voidctk_mouse_show(void){}/*-----------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -