You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

curses_scr.c 1.9KB

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