terminal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* $NetBSD: terminal.c,v 1.4 2003/06/11 12:00:23 wiz Exp $ */
  2. /*
  3. * Copyright (c) 1983-2003, Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * + Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * + Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * + Neither the name of the University of California, San Francisco nor
  16. * the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  23. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/cdefs.h>
  33. #ifndef lint
  34. __RCSID("$NetBSD: terminal.c,v 1.4 2003/06/11 12:00:23 wiz Exp $");
  35. #endif /* not lint */
  36. #include <stdarg.h>
  37. #include "hunt.h"
  38. #define TERM_WIDTH 80 /* Assume terminals are 80-char wide */
  39. /*
  40. * cgoto:
  41. * Move the cursor to the given position on the given player's
  42. * terminal.
  43. */
  44. void
  45. cgoto(pp, y, x)
  46. PLAYER *pp;
  47. int y, x;
  48. {
  49. if (x == pp->p_curx && y == pp->p_cury)
  50. return;
  51. sendcom(pp, MOVE, y, x);
  52. pp->p_cury = y;
  53. pp->p_curx = x;
  54. }
  55. /*
  56. * outch:
  57. * Put out a single character.
  58. */
  59. void
  60. outch(pp, ch)
  61. PLAYER *pp;
  62. char ch;
  63. {
  64. if (++pp->p_curx >= TERM_WIDTH) {
  65. pp->p_curx = 0;
  66. pp->p_cury++;
  67. }
  68. (void) putc(ch, pp->p_output);
  69. }
  70. /*
  71. * outstr:
  72. * Put out a string of the given length.
  73. */
  74. void
  75. outstr(pp, str, len)
  76. PLAYER *pp;
  77. const char *str;
  78. int len;
  79. {
  80. pp->p_curx += len;
  81. pp->p_cury += (pp->p_curx / TERM_WIDTH);
  82. pp->p_curx %= TERM_WIDTH;
  83. while (len--)
  84. (void) putc(*str++, pp->p_output);
  85. }
  86. /*
  87. * clrscr:
  88. * Clear the screen, and reset the current position on the screen.
  89. */
  90. void
  91. clrscr(pp)
  92. PLAYER *pp;
  93. {
  94. sendcom(pp, CLEAR);
  95. pp->p_cury = 0;
  96. pp->p_curx = 0;
  97. }
  98. /*
  99. * ce:
  100. * Clear to the end of the line
  101. */
  102. void
  103. ce(pp)
  104. PLAYER *pp;
  105. {
  106. sendcom(pp, CLRTOEOL);
  107. }
  108. #if 0 /* XXX lukem*/
  109. /*
  110. * ref;
  111. * Refresh the screen
  112. */
  113. void
  114. ref(pp)
  115. PLAYER *pp;
  116. {
  117. sendcom(pp, REFRESH);
  118. }
  119. #endif
  120. /*
  121. * sendcom:
  122. * Send a command to the given user
  123. */
  124. void
  125. sendcom(PLAYER *pp, int command, ...)
  126. {
  127. va_list ap;
  128. int arg1, arg2;
  129. va_start(ap, command);
  130. (void) putc(command, pp->p_output);
  131. switch (command & 0377) {
  132. case MOVE:
  133. arg1 = va_arg(ap, int);
  134. arg2 = va_arg(ap, int);
  135. (void) putc(arg1, pp->p_output);
  136. (void) putc(arg2, pp->p_output);
  137. break;
  138. case ADDCH:
  139. case READY:
  140. arg1 = va_arg(ap, int);
  141. (void) putc(arg1, pp->p_output);
  142. break;
  143. }
  144. va_end(ap); /* No return needed for void functions. */
  145. }