curses_scr.c 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "../include/curses.h"
  2. #include <termios.h>
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #define ESC 27
  6. #define MODE 3
  7. unsigned int _COLOUR_PAIRS = 4;
  8. unsigned int _COLOURS = 8;
  9. unsigned short _COLS = 80;
  10. unsigned short _LINES = 25;
  11. static struct termios original, runtime;
  12. void _init_screen( struct _curses_screen *scr __unused ) {
  13. tcgetattr(fileno(stdin),&original);
  14. tcgetattr(fileno(stdin),&runtime);
  15. runtime.c_lflag &= ~(ICANON|ECHO);
  16. tcsetattr(fileno(stdin),TCSANOW,&runtime);
  17. //printf("%c[=%dh",ESC,MODE);
  18. LINES = 25; COLS = 80;
  19. }
  20. void _exit_screen( struct _curses_screen *scr __unused ) {
  21. printf("%c[1;1H",ESC);
  22. printf("%c[2J",ESC);
  23. tcsetattr(fileno(stdin),TCSANOW,&original);
  24. }
  25. void _movetoyx( struct _curses_screen *scr __unused, unsigned int y, unsigned int x ) {
  26. printf( "%c[%d;%dH", ESC, y+1, x+1 );
  27. }
  28. void _putc( struct _curses_screen *scr __unused, chtype c ) {
  29. unsigned short pairno;
  30. pairno = (unsigned short)(( c & A_COLOUR ) >> CPAIR_SHIFT);
  31. // print rendition (colour and attrs)
  32. //printf( "%c[%d;%d",ESC,
  33. // cpairs[pairno][0], cpairs[pairno][1] );
  34. // print rendition (character)
  35. //printf("char is \"%d\"", c );
  36. putchar( c );
  37. fflush(stdout); // There must be a better way to do this...
  38. }
  39. int _getc( struct _curses_screen *scr __unused ) {
  40. return getchar();
  41. }
  42. bool _peek( struct _curses_screen *scr __unused ) {
  43. int c;
  44. if ( ( c = getchar() ) != EOF ) {
  45. ungetc( c, stdin );
  46. return TRUE;
  47. } else { return FALSE; }
  48. }
  49. SCREEN _curscr = {
  50. .init = _init_screen,
  51. .exit = _exit_screen,
  52. .movetoyx = _movetoyx,
  53. .putc = _putc,
  54. .getc = _getc,
  55. .peek = _peek,
  56. };