Koala Library
|
00001 //--------------------------------------------------------------------------------// 00002 //- KOALA( Koala extension board ) -// 00003 // -// 00004 //- Copyright (C) Julien Tharin, K-Team S.A. 2013 -// 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 //- Chemin des Plans-Praz 28, | / __ | | _____|/ _ \| \/ | -// 00022 //- 1337 Vallorbe | | \ | | ____|/ /_\ | | -// 00023 //- Switzerland |__|\__\ |__|______|_/ \_|__|\/|__| -// 00024 //- jtharin@k-team.com tel:+41 24 423 89 56 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 "koala_utils.h" 00042 00043 /* ---- Exported Functions------------------------------------------------ */ 00044 00045 /*--------------------------------------------------------------------*/ 00052 int koala_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 koala_change_term_mode(int dir) 00086 { 00087 static struct termios oldt, newt; 00088 static int called=0; // avoid to change if not called 00089 00090 if ( dir == 1 ) 00091 { 00092 tcgetattr( STDIN_FILENO, &oldt); 00093 newt = oldt; 00094 newt.c_lflag &= ~( ICANON | ECHO ); 00095 tcsetattr( STDIN_FILENO, TCSANOW, &newt); 00096 called=1; 00097 } 00098 else 00099 if (called) 00100 { 00101 tcsetattr( STDIN_FILENO, TCSANOW, &oldt); 00102 called=0; 00103 } 00104 } 00105 00106 00107 /*--------------------------------------------------------------------*/ 00111 void koala_clrscr(void) 00112 { 00113 printf("\033[2J"); /* erase the whole console */ 00114 printf("\033[1;1f"); /* Move cursor to the top left */ 00115 } 00116 00117 00118 00119 /*--------------------------------------------------------------------*/ 00124 void koala_erase_line(int line) 00125 { 00126 koala_move_cursor(1,line); 00127 printf("\033[K"); //erase to end of line 00128 } 00129 00130 /*--------------------------------------------------------------------*/ 00136 void koala_move_cursor(int c, int l) // column,line 00137 { 00138 printf("\033[%d;%df",l,c); 00139 } 00140 00141 /*--------------------------------------------------------------------*/ 00146 void koala_move_cursor_column(int c) // column 00147 { 00148 printf("\033[%d`",c); // Move the cursor to column c. 00149 } 00150 00151 /*--------------------------------------------------------------------*/ 00156 void koala_move_cursor_line(int l) // line 00157 { 00158 printf("\033[%dd",l); // Move the cursor line l 00159 } 00160 00161 00173 long long 00174 koala_timeval_diff(struct timeval *difference, 00175 struct timeval *end_time, 00176 struct timeval *start_time 00177 ) 00178 { 00179 struct timeval temp_diff; 00180 00181 if(difference==NULL) 00182 { 00183 difference=&temp_diff; 00184 } 00185 00186 difference->tv_sec =end_time->tv_sec -start_time->tv_sec ; 00187 difference->tv_usec=end_time->tv_usec-start_time->tv_usec; 00188 00189 /* Using while instead of if below makes the code slightly more robust. */ 00190 00191 while(difference->tv_usec<0) 00192 { 00193 difference->tv_usec+=1000000; 00194 difference->tv_sec -=1; 00195 } 00196 00197 return 1000000LL*difference->tv_sec+ 00198 difference->tv_usec; 00199 00200 } /* timeval_diff() */