ScrUtil (C)
Manejo de consola
 Todo Estructuras de Datos Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones 'defines'
scrutil.c
Ir a la documentaciĆ³n de este archivo.
1 /*
2  Manejo de la consola
3 */
4 
5 #include "scrutil.h"
6 
7 #include <string.h>
8 #include <stdio.h>
9 
10 /* Detectar el sistema operativo */
11 #ifdef _WIN32
12  #define SO_WINDOWS
13 #else
14  #ifdef __WIN32__
15  #define SO_WINDOWS
16  #else
17  #ifdef __WINDOWS__
18  #define SO_WINDOWS
19  #else
20  #define SO_UNIX
21  #endif
22  #endif
23 #endif
24 
25 #ifdef SO_WINDOWS
26 #include <windows.h>
27 
28 /* WinColors */
30 static short int winInkColors[ scrUndefinedColor + 1 ];
32 static short int winPaperColors[ scrUndefinedColor + 1 ];
33 
34 static void initWindowsColors()
40 {
41  winInkColors[ scrBlack ] = 0;
42  winInkColors[ scrBlue ] = FOREGROUND_BLUE;
43  winInkColors[ scrRed ] = FOREGROUND_RED;
44  winInkColors[ scrMagenta ] = FOREGROUND_BLUE | FOREGROUND_RED;
45  winInkColors[ scrGreen ] = FOREGROUND_GREEN;
46  winInkColors[ scrCyan ] = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
47  winInkColors[ scrYellow ] = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
48  winInkColors[ scrWhite ] = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
49  winInkColors[ scrUndefinedColor ] = 0;
50 
51  winPaperColors[ scrBlack ] = 0;
52  winPaperColors[ scrBlue ] = BACKGROUND_BLUE;
53  winPaperColors[ scrRed ] = BACKGROUND_RED;
54  winPaperColors[ scrMagenta ] = BACKGROUND_BLUE | BACKGROUND_RED;
55  winPaperColors[ scrGreen ] = BACKGROUND_GREEN;
56  winPaperColors[ scrCyan ] = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
57  winPaperColors[ scrYellow ] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
58  winPaperColors[ scrWhite ] = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
59  winPaperColors[ scrUndefinedColor ] = 0;
60 }
61 #else
62  // Commands
63  static const short int MaxCmdBuffer = 32;
65  static const char * CSI = "\33[";
66  static const char * CmdClearScreen = "2J";
67  static char cmd[MaxCmdBuffer];
68 
69  // Colors
71  static char cmdUnixInkColors[ scrUndefinedColor + 1 ][MaxCmdBuffer];
73  static char cmdUnixPaperColors[ scrUndefinedColor + 1 ][MaxCmdBuffer];
75  static const short int UnixLastLine = 25;
76  static const short int UnixLastColumn = 80;
77 
78 
79  static void initUnixColors()
84  {
85  sprintf( cmdUnixInkColors[ scrBlack ], "%s%s", CSI, "30m" );
86  sprintf( cmdUnixInkColors[ scrBlue ], "%s%s", CSI, "34m" );
87  sprintf( cmdUnixInkColors[ scrRed ], "%s%s", CSI, "31m" );
88  sprintf( cmdUnixInkColors[ scrMagenta ], "%s%s", CSI, "35m" );
89  sprintf( cmdUnixInkColors[ scrGreen ], "%s%s", CSI, "32m" );
90  sprintf( cmdUnixInkColors[ scrCyan ], "%s%s", CSI, "36m" );
91  sprintf( cmdUnixInkColors[ scrYellow ], "%s%s", CSI, "93m" );
92  sprintf( cmdUnixInkColors[ scrWhite ], "%s%s", CSI, "37m" );
93  sprintf( cmdUnixInkColors[ scrUndefinedColor ], "%s%s", CSI, "30m" );
94 
95  sprintf( cmdUnixPaperColors[ scrBlack ], "%s%s", CSI, "40m" );
96  sprintf( cmdUnixPaperColors[ scrBlue ], "%s%s", CSI, "44m" );
97  sprintf( cmdUnixPaperColors[ scrRed ], "%s%s", CSI, "41m" );
98  sprintf( cmdUnixPaperColors[ scrMagenta ], "%s%s", CSI, "45m" );
99  sprintf( cmdUnixPaperColors[ scrGreen ], "%s%s", CSI, "42m" );
100  sprintf( cmdUnixPaperColors[ scrCyan ], "%s%s", CSI, "46m" );
101  sprintf( cmdUnixPaperColors[ scrYellow ], "%s%s", CSI, "103m" );
102  sprintf( cmdUnixPaperColors[ scrWhite ], "%s%s", CSI, "47m" );
103  sprintf( cmdUnixPaperColors[ scrUndefinedColor ], "%s%s", CSI, "40m" );
104  }
105 #endif
106 
107 static scrAttributes libAttrs;
108 static bool inited = false;
109 
110 static inline void scrInit()
111 {
112  if ( !inited ) {
113  #ifdef SO_WINDOWS
114  initWindowsColors();
115  #else
116  initUnixColors();
117  #endif
118 
119  libAttrs.ink = scrWhite;
120  libAttrs.paper = scrBlack;
121  inited = true;
122  }
123 
124  return;
125 }
126 
127 void scrClear()
128 {
129  scrInit();
130 
131  #ifdef SO_WINDOWS
132  COORD pos = { 0, 0 };
133  DWORD cars;
134  HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
135  CONSOLE_SCREEN_BUFFER_INFO csbi;
136  DWORD dwConSize;
137 
138  if( hStdOut != INVALID_HANDLE_VALUE
139  && GetConsoleScreenBufferInfo( hStdOut, &csbi ) )
140  {
141  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
142 
143  FillConsoleOutputCharacter(
144  hStdOut,
145  ' ',
146  dwConSize,
147  pos,
148  &cars
149  );
150 
151  FillConsoleOutputAttribute(
152  hStdOut,
153  winPaperColors[ libAttrs.paper ] | winInkColors[ libAttrs.ink ],
154  dwConSize,
155  pos,
156  &cars
157  );
158  }
159  #else
160  strcpy( cmd, CSI );
161  strcat( cmd, CmdClearScreen );
162  printf( "%s", cmd );
163  #endif
164 
165  scrMoveCursorTo( 0, 0 );
166 }
167 
169 {
170  scrInit();
171 
172  libAttrs.paper = colors.paper;
173  libAttrs.ink = colors.ink;
174 
175  #ifdef SO_WINDOWS
176  SetConsoleTextAttribute(
177  GetStdHandle( STD_OUTPUT_HANDLE ),
178  winPaperColors[ libAttrs.paper ] | winInkColors[ libAttrs.ink ]
179  );
180  #else
181  printf( "%s%s",
182  cmdUnixInkColors[ colors.ink ],
183  cmdUnixPaperColors[ colors.paper ]
184  );
185  #endif
186 }
187 
188 void scrSetColors(Color tinta, Color papel)
189 {
190  scrAttributes atrs;
191 
192  atrs.paper = papel;
193  atrs.ink = tinta;
194 
195  scrSetColorsWithAttr( atrs );
196 }
197 
199 {
200  scrMoveCursorTo( pos.row, pos.column );
201 }
202 
203 void scrMoveCursorTo(unsigned short int fila, unsigned short int columna)
204 {
205  scrInit();
206 
207  #ifdef SO_WINDOWS
208  COORD pos;
209 
210  pos.X = columna;
211  pos.Y = fila;
212 
213  SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), pos );
214  #else
215  printf( "%s%d;%dH", CSI, fila + 1, columna + 1 );
216  #endif
217 }
218 
220 {
221  scrPosition pos;
222 
223  pos.row = pos.column = -1;
224  scrInit();
225 
226  #ifdef SO_WINDOWS
227  CONSOLE_SCREEN_BUFFER_INFO infoPantalla;
228  GetConsoleScreenBufferInfo(
229  GetStdHandle( STD_OUTPUT_HANDLE ), &infoPantalla
230  );
231 
232  pos.row = infoPantalla.srWindow.Bottom + 1;
233  pos.column = infoPantalla.srWindow.Right + 1;
234  #else
235  pos.row = UnixLastLine;
236  pos.column = UnixLastColumn;
237  #endif
238 
239  return pos;
240 }
241 
243 {
244  scrInit();
245  return libAttrs;
246 }
247 
248 unsigned short int scrGetMaxRows()
249 {
250  return scrGetConsoleSize().row;
251 }
252 
253 unsigned short int scrGetMaxColumns()
254 {
255  return scrGetConsoleSize().column;
256 }
257 
259 {
260  scrPosition toret;
261 
262  scrInit();
263  memset( &toret, 0, sizeof( scrPosition ) );
264 
265  #ifdef SO_WINDOWS
266  CONSOLE_SCREEN_BUFFER_INFO infoPantalla;
267  GetConsoleScreenBufferInfo(
268  GetStdHandle( STD_OUTPUT_HANDLE ),
269  &infoPantalla
270  );
271 
272  toret.row = infoPantalla.dwCursorPosition.Y;
273  toret.column = infoPantalla.dwCursorPosition.X;
274  #else
275  toret.row = -1;
276  toret.column = -1;
277  #endif
278 
279  return toret;
280 }
281 
282 void scrShowCursor(bool see)
283 {
284  scrInit();
285 
286  #ifdef SO_WINDOWS
287  CONSOLE_CURSOR_INFO info;
288 
289  info.dwSize = 10;
290  info.bVisible = (BOOL) see;
291  SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &info );
292  #else
293  printf( "%s?25%c", CSI, see ? 'h' : 'l' );
294  #endif
295 }