aboutsummaryrefslogtreecommitdiffstats
path: root/src/terminal.c
blob: 9a3290eeee645e81057f6e326ce84ceaa732b8a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * terminal.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/*
 * $Log$
 * Revision 1.9  2008/02/15 03:32:07  james
 * *** empty log message ***
 *
 * Revision 1.8  2008/02/14 10:39:14  james
 * *** empty log message ***
 *
 * Revision 1.7  2008/02/14 01:55:57  james
 * *** empty log message ***
 *
 * Revision 1.6  2008/02/14 00:57:58  james
 * *** empty log message ***
 *
 * Revision 1.5  2008/02/13 18:05:06  james
 * *** empty log message ***
 *
 * Revision 1.4  2008/02/13 16:57:29  james
 * *** empty log message ***
 *
 * Revision 1.3  2008/02/13 09:12:21  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/13 01:08:18  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/12 22:36:46  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/09 15:47:28  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/07 11:11:14  staffcvs
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/07 01:02:52  james
 * *** empty log message ***
 *
 * Revision 1.3  2008/02/06 17:53:28  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/02/04 02:05:06  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/04 01:32:39  james
 * *** empty log message ***
 *
 */

#include "project.h"


typedef struct TERMINAL_struct
{
  TTY_SIGNATURE;
  struct termios orig_termios;
  struct TERMINAL_struct *next;
} TERMINAL;


static TERMINAL *terminal_list = NULL;
int terminal_winches;



static void
terminal_close (TTY * _t)
{
  char buf[32];
  int i;

  TERMINAL *t = (TERMINAL *) _t;
  TERMINAL **ptr = &terminal_list;

  if (!t)
    return;

  /* Take out of cleanup list */
  while (*ptr && (*ptr != t))
    ptr = &((*ptr)->next);

  if (*ptr)
    *ptr = t->next;

  tcsetattr (t->wfd, TCSANOW, &t->orig_termios);

  set_nonblocking (t->wfd);


  t->xmit (_t, "\033[r", 3);
  t->xmit (_t, "\033[0m", 4);
  i = sprintf (buf, "\033[%d;%dH", CRT_ROWS + 1, 1);
  t->xmit (_t, buf, i);
  t->xmit (_t, "\033[J", 3);

  set_blocking (t->rfd);
  set_blocking (t->wfd);

  free (t);
}


void
terminal_atexit (void)
{
  while (terminal_list)
    terminal_close ((TTY *) terminal_list);
}

static void
sigint (int dummy)
{
  terminal_atexit ();
  exit (-1);
}

static void
sigwinch (int not)
{
  terminal_winches++;
}


void
terminal_getsize (TTY * _t)
{
  TERMINAL *t = (TERMINAL *) _t;
  struct winsize sz = { 0 };

  if (!t)
    return;

  if (ioctl (t->wfd, TIOCGWINSZ, &sz))
    {
      t->size.x = CRT_COLS;
      t->size.y = CRT_ROWS;
    }
  else
    {
      t->size.x = sz.ws_col;
      t->size.y = sz.ws_row;
    }
}


void
terminal_dispatch (void)
{
  TERMINAL *t;


  if (!terminal_winches)
    return;

  terminal_winches = 0;

  for (t = terminal_list; t; t = t->next)
    terminal_getsize ((TTY *) t);

}


static int
terminal_read (TTY * _t, void *buf, int len)
{
  TERMINAL *t = (TERMINAL *) _t;
  int red, done = 0;

  terminal_dispatch ();
  set_nonblocking (t->rfd);

  do
    {

      red = wrap_read (t->rfd, buf, len);
      if (red < 0)
        return -1;
      if (!red)
        return done;

      buf += red;
      len -= red;
      done += red;
    }
  while (len);


  return done;
}


static int
terminal_write (TTY * _t, void *buf, int len)
{
  int writ, done = 0;
  TERMINAL *t = (TERMINAL *) _t;

  terminal_dispatch ();

  set_blocking (t->wfd);

  do
    {

      writ = wrap_write (t->wfd, buf, len);
      if (writ < 0)
        return -1;

      if (!writ)
        usleep (1000);

      buf += writ;
      len -= writ;
      done += writ;
    }
  while (len);


  return done;
}


void
terminal_register_handlers (void)
{
  struct sigaction sa = { 0 };

  sa.sa_handler = sigwinch;
  sa.sa_flags = SA_RESTART;
  sigaction (SIGWINCH, &sa, NULL);

  sa.sa_handler = sigint;
  sa.sa_flags = SA_RESTART;
  sigaction (SIGINT, &sa, NULL);
}


TTY *
terminal_open (int rfd, int wfd)
{
  TERMINAL *t;
  pid_t child;
  struct termios termios;

  t = (TERMINAL *) malloc (sizeof (TERMINAL));

  strcpy (t->name, "terminal");
  t->rfd = rfd;
  t->wfd = wfd;

  tcgetattr (wfd, &t->orig_termios);

  t->next = terminal_list;
  terminal_list = t;

  tcgetattr (wfd, &termios);

  set_nonblocking (rfd);
  set_nonblocking (wfd);


  cfmakeraw (&termios);
  //raw_termios (&termios);

  tcsetattr (wfd, TCSANOW, &termios);

  t->recv = terminal_read;
  t->xmit = terminal_write;
  t->close = terminal_close;
  t->blocked = 0;


  terminal_getsize ((TTY *) t);

  return (TTY *) t;
}