00001 //--------------------------------------------------------------------------------// 00002 //- kb_utils.c -// 00003 // -// 00004 //- Copyright (C) Julien Tharin, K-Team S.A. 2011 -// 00005 //- This library is free software; you can redistribute it and/or -// 00006 //- modify it under the terms of the GNU Lesser General Public -// 00007 //- License as published by the Free Software Foundation; either -// 00008 //- version 2.1 of the License, or any later version. -// 00009 //- -// 00010 //- This library is distributed in the hope that it will be useful, -// 00011 //- but WITHOUT ANY WARRANTY; without even the implied warranty of -// 00012 //- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// 00013 //- Lesser General Public License for more details. -// 00014 //- -// 00015 //- You should have received a copy of the GNU Lesser General Public -// 00016 //- License along with this library; if not, write to the Free Software -// 00017 //- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// 00018 //- -// 00019 //- __ __ ________ -// 00020 //- K-Team S.A. | |/ /|__ __|___ _____ ___ ___ -// 00021 //- Rue Galilee 9. Y-Park, | / __ | | _____|/ _ \| \/ | -// 00022 //- 1400 Yverdon-les-Bains | | \ | | ____|/ /_\ | | -// 00023 //- Switzerland |__|\__\ |__|______|_/ \_|__|\/|__| -// 00024 //- jtharin@k-team.com tel:+41 24 423 89 75 fax:+41 24 423 8960 -// 00025 //- -// 00026 //--------------------------------------------------------------------------------// 00027 00029 00032 00033 00034 00035 /* ---- Include Files ---------------------------------------------------- */ 00036 00037 #include <stdio.h> 00038 #include <termio.h> 00039 #include <unistd.h> 00040 00041 #include "kb_utils.h" 00042 00043 /* ---- Exported Functions------------------------------------------------ */ 00044 00045 /*--------------------------------------------------------------------*/ 00052 int kb_kbhit(void) 00053 { 00054 int cnt = 0; 00055 int error; 00056 static struct termios Otty, Ntty; 00057 00058 00059 tcgetattr( 0, &Otty); 00060 Ntty = Otty; 00061 00062 Ntty.c_iflag = 0; /* input mode */ 00063 Ntty.c_oflag = 0; /* output mode */ 00064 Ntty.c_lflag &= ~ICANON; /* raw mode */ 00065 Ntty.c_cc[VMIN] = CMIN; /* minimum time to wait */ 00066 Ntty.c_cc[VTIME] = CTIME; /* minimum characters to wait for */ 00067 00068 if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) { 00069 error += ioctl(0, FIONREAD, &cnt); 00070 error += tcsetattr(0, TCSANOW, &Otty); 00071 } 00072 00073 return ( error == 0 ? cnt : -1 ); 00074 } 00075 00076 00077 /*--------------------------------------------------------------------*/ 00085 void kb_change_term_mode(int dir) 00086 { 00087 static struct termios oldt, newt; 00088 00089 if ( dir == 1 ) 00090 { 00091 tcgetattr( STDIN_FILENO, &oldt); 00092 newt = oldt; 00093 newt.c_lflag &= ~( ICANON | ECHO ); 00094 tcsetattr( STDIN_FILENO, TCSANOW, &newt); 00095 } 00096 else 00097 tcsetattr( STDIN_FILENO, TCSANOW, &oldt); 00098 } 00099 00100 00101 /*--------------------------------------------------------------------*/ 00105 void kb_clrscr(void) 00106 { 00107 printf("\033[2J"); /* erase the whole console */ 00108 printf("\033[1;1f"); /* Move cursor to the top left */ 00109 } 00110 00111 00112 00113 /*--------------------------------------------------------------------*/ 00118 void kb_erase_line(int line) 00119 { 00120 kb_move_cursor(1,line); 00121 printf("\033[K"); //erase to end of line 00122 } 00123 00124 /*--------------------------------------------------------------------*/ 00130 void kb_move_cursor(int c, int l) // column,line 00131 { 00132 printf("\033[%d;%df",l,c); 00133 } 00134 00135 /*--------------------------------------------------------------------*/ 00140 void kb_move_cursor_column(int c) // column 00141 { 00142 printf("\033[%d`",c); // Move the cursor to column c. 00143 } 00144 00145 /*--------------------------------------------------------------------*/ 00150 void kb_move_cursor_line(int l) // line 00151 { 00152 printf("\033[%dd",l); // Move the cursor line l 00153 }