/branches/RXVT/src/graphics/grxlib.c

# · C · 167 lines · 144 code · 19 blank · 4 comment · 20 complexity · cc911d785c905f76827c4326c5aea3de MD5 · raw file

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6. #ifdef _AIX
  7. # include <termio.h>
  8. #else
  9. # include <termios.h>
  10. #endif
  11. #include "grxlib.h"
  12. /*----------------------------------------------------------------------*/
  13. void Done (void) { putchar (':'); }
  14. void StartLine (long id) { printf ("\033GL%ld", id); }
  15. void StartPoint (long id) { printf ("\033GP%ld", id); }
  16. void StartFill (long id) { printf ("\033GF%ld", id); }
  17. void Extend (int x, int y) { printf (";%d;%d", x, y); }
  18. void FillArea (int x1, int y1, int x2, int y2)
  19. {
  20. printf (";%d;%d;%d;%d", x1, y1, x2, y2);
  21. }
  22. void PlaceText (long id, int x, int y, int mode, char *text)
  23. {
  24. printf ("\033GT%ld;%d;%d;%d;%d:%s", id, x, y, mode, strlen(text), text);
  25. fflush (stdout);
  26. }
  27. void ClearWindow (long id) { printf ("\033GC%ld:", id); }
  28. void ForeColor (int col) { printf ("\033[3%dm", (col<0||col>7)?0:col); }
  29. void DefaultRendition (void) { printf ("\033[m"); }
  30. #define LINESZ 100
  31. static char line [LINESZ];
  32. static FILE *infd = NULL;
  33. long
  34. CreateWin (int x, int y, int w, int h)
  35. {
  36. long id = 0;
  37. fflush (stdout);
  38. printf ("\033GW%d;%d;%d;%d:", x, y, w, h);
  39. fflush (stdout);
  40. while (1)
  41. {
  42. if ((fgets (line, LINESZ, infd) != NULL) &&
  43. (sscanf (line,"\033W%ld", &id) == 1))
  44. break;
  45. }
  46. return id;
  47. }
  48. void
  49. QueryWin (long id, int *nfwidth, int *nfheight)
  50. {
  51. int id1, x, y, width, height, fwidth, fheight;
  52. printf ("\033GG%ld:",id);
  53. fflush (stdout);
  54. while (1)
  55. {
  56. if ((fgets (line, sizeof(line), infd) != NULL) &&
  57. (sscanf (line,"\033G%ld %ld %ld %ld %ld %ld %ld %ld %ld",
  58. &id1, &x, &y, &width, &height,
  59. &fwidth, &fheight, nfwidth, nfheight) != 0))
  60. break;
  61. }
  62. }
  63. int
  64. WaitForCarriageReturn (long *win, int *x, int *y)
  65. {
  66. int i, len;
  67. fgets (line, LINESZ, infd);
  68. line [LINESZ-1] = 0;
  69. len = strlen (line);
  70. for (i = 0; i < len; i++)
  71. {
  72. if (line [i] == '\033')
  73. {
  74. int ret = 1;
  75. i++;
  76. switch (line[i]) {
  77. case 'R': ret++;
  78. /* drop */
  79. case 'P':
  80. sscanf (&line[i+1],"%ld;%d;%d", win, x, y);
  81. return ret;
  82. break;
  83. }
  84. }
  85. }
  86. return 0;
  87. }
  88. static int fno2;
  89. static struct termios ttmode;
  90. int
  91. InitializeGraphics (int scroll_text_up)
  92. {
  93. int fno, i;
  94. char *screen_tty;
  95. struct winsize winsize;
  96. fno = fileno (stdout);
  97. if (!isatty (fno))
  98. {
  99. fprintf (stderr, "stdout must be a tty\n");
  100. return 0;
  101. }
  102. screen_tty = ttyname (fno);
  103. ioctl (fno, TCGETS, (char *)&ttmode);
  104. ttmode.c_lflag &= ~ECHO;
  105. ioctl (fno, TCSETS, (char *)&ttmode);
  106. infd = fopen (screen_tty, "rw");
  107. fno2 = fileno (infd);
  108. ioctl (fno2, TCGETS, (char *)&ttmode);
  109. ttmode.c_lflag &= ~ECHO;
  110. ioctl (fno2, TCSETS, (char *)&ttmode);
  111. /* query rxvt to find if graphics are available */
  112. fflush (stdout);
  113. printf ("\033GQ");
  114. fflush (stdout);
  115. while (1)
  116. {
  117. if ((fgets (line, LINESZ, infd) != NULL) &&
  118. (sscanf (line,"\033G%d", &i) == 1))
  119. {
  120. if (!i)
  121. {
  122. fprintf (stderr, "rxvt graphics not available\n");
  123. CloseGraphics ();
  124. return 0;
  125. }
  126. break;
  127. }
  128. }
  129. if (scroll_text_up)
  130. {
  131. ioctl (fno, TIOCGWINSZ, &winsize);
  132. fflush (stdout);
  133. for (i = 0; i < winsize.ws_row; i++)
  134. putchar ('\n');
  135. fflush (stdout);
  136. }
  137. return i;
  138. }
  139. void
  140. CloseGraphics (void)
  141. {
  142. DefaultRendition ();
  143. fflush (stdout);
  144. ttmode.c_lflag |= ECHO;
  145. ioctl (fno2, TCSETS, (char *)&ttmode);
  146. fclose (infd);
  147. }
  148. /*----------------------- end-of-file (C source) -----------------------*/