chiark / gitweb /
*** empty log message ***
[sympathy.git] / apps / sympathyd.c
1 /*
2  * sympathy.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] =
10   "$Id$";
11
12 /*
13  * $Log$
14  * Revision 1.7  2008/02/14 02:46:44  james
15  * *** empty log message ***
16  *
17  * Revision 1.6  2008/02/14 00:57:58  james
18  * *** empty log message ***
19  *
20  * Revision 1.5  2008/02/13 18:05:06  james
21  * *** empty log message ***
22  *
23  * Revision 1.4  2008/02/13 17:21:55  james
24  * *** empty log message ***
25  *
26  * Revision 1.3  2008/02/08 15:06:52  james
27  * *** empty log message ***
28  *
29  * Revision 1.2  2008/02/07 15:42:49  james
30  * *** empty log message ***
31  *
32  * Revision 1.1  2008/02/05 14:25:49  james
33  * *** empty log message ***
34  *
35  */
36
37 #include <sympathy.h>
38
39 #include "client.h"
40 #include "clients.h"
41
42
43 static void
44 send_history (History * h, Client * c)
45 {
46   int rptr = h->wptr;
47
48   HISTORY_INC (h, rptr);
49
50   HISTORY_INC (h, rptr);
51   while (rptr != h->wptr)
52     {
53       History_ent *l = &h->lines[rptr];
54       if (l->valid)
55         {
56
57           if (ipc_msg_send_history (c->s, l))
58             c->dead++;
59
60         }
61       HISTORY_INC (h, rptr);
62     }
63 }
64
65 static void
66 send_vt102 (VT102 * v, Client * c)
67 {
68   if (ipc_msg_send_vt102 (c->s, v))
69     c->dead++;
70
71 }
72
73 int
74 main (int argc, char *argv[])
75 {
76   fd_set rfds, wfds;
77 //  ANSI a = { 0 };
78   Context c;
79   Socket *s, *cs;
80   Clients *clients;
81
82
83   s = socket_listen ("socket");
84
85   c.t = ptty_open (NULL, NULL);
86   c.v = vt102_new ();
87   c.h = history_new (200);
88   c.l = file_log_new ("log");
89   c.k = keydis_vt102_new (&c);
90
91 #if 0
92   terminal_register_handlers ();
93   a.terminal = terminal_open (0, 1);
94
95   ansi_reset (&a, NULL);
96 #endif
97
98   clients = clients_new ();
99
100   for (;;)
101     {
102       struct timeval tv = { 10, 0 };
103
104       FD_ZERO (&rfds);
105       FD_ZERO (&wfds);
106
107       tty_pre_select (c.t, &rfds, &wfds);
108 #if 0
109       tty_pre_select (a.terminal, &rfds, &wfds);
110 #endif
111
112       FD_SET (s->fd, &rfds);
113
114       socket_pre_select (s, &rfds, &wfds);
115
116       clients_pre_select (clients, &rfds, &wfds);
117
118       select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
119
120       if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
121         {
122           {
123             Client *cl;
124             /*New client connexion */
125             cl = clients_new_client (clients, cs, &c);
126
127             send_history (c.h, cl);
128             send_vt102 (c.v, cl);
129
130           }
131         }
132
133
134       clients_post_select (clients, &c, &rfds, &wfds);
135
136       if (FD_ISSET (c.t->rfd, &rfds))
137         {
138           char buf[IPC_MAX_BUF];
139           int red;
140
141           red = c.t->recv (c.t, buf, sizeof (buf));
142
143           if (red < 0)
144             break;
145
146           if (red)
147             {
148               clients_output (clients, buf, red);
149               vt102_parse (&c, buf, red);
150             }
151         }
152
153 #if 0
154       ansi_dispatch (&a, &c);
155       ansi_update (&a, &c);
156 #endif
157
158
159     }
160
161   clients_shutdown (clients);
162 #if 0
163   ansi_terminal_reset (&a);
164 #endif
165   terminal_atexit ();
166   printf ("QUAT\n");
167 }