aboutsummaryrefslogtreecommitdiffstats
path: root/src/terminal.c
diff options
context:
space:
mode:
authorjames <>2008-02-12 22:36:46 +0000
committerjames <>2008-02-12 22:36:46 +0000
commit8688a9519c349b6ee8664be6ce2897a59c0f52be (patch)
treeacfb9332cedbc05b114aa5c5d70fc08741ae8ce7 /src/terminal.c
parent5ee4b614cdc64a13aeeb00bf0e0ecbc234e8852d (diff)
downloadsympathy-8688a9519c349b6ee8664be6ce2897a59c0f52be.tar.gz
sympathy-8688a9519c349b6ee8664be6ce2897a59c0f52be.tar.bz2
sympathy-8688a9519c349b6ee8664be6ce2897a59c0f52be.zip
*** empty log message ***
Diffstat (limited to 'src/terminal.c')
-rw-r--r--src/terminal.c189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/terminal.c b/src/terminal.c
new file mode 100644
index 0000000..a4cd31b
--- /dev/null
+++ b/src/terminal.c
@@ -0,0 +1,189 @@
+/*
+ * terminal.c:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+static char rcsid[] = "$Id$";
+
+/*
+ * $Log$
+ * 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;
+
+
+static void
+terminal_close (TTY * _t)
+{
+ 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_blocking(t->rfd);
+ set_blocking(t->wfd);
+
+ free (t);
+}
+
+
+
+static int
+terminal_read (TTY * _t, void *buf, int len)
+{
+ TERMINAL *t = (TERMINAL *) _t;
+ int red, done = 0;
+
+ do
+ {
+
+ red = wrap_read (t->fd, 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;
+
+ do
+ {
+
+ writ = wrap_write (t->fd, buf, len);
+ if (writ < 0)
+ return -1;
+ if (!writ)
+ sleep (1);
+
+ buf += writ;
+ len -= writ;
+ done += writ;
+ }
+ while (len);
+
+
+ return done;
+}
+
+TTY *
+terminal_open (int rfd,int wfd)
+{
+ TERMINAL *t;
+ pid_t child;
+ char name[1024];
+ struct termios termios;
+
+ t = (TERMINAL *) malloc (sizeof (TERMINAL));
+
+ t->rfd = rfd;
+ t->wfd = wfd;
+
+ tcgetattr(wfd,&t->orig_termios);
+
+ t->next=terminal_list;
+ terminal_list=t;
+
+ tcgetattr(tfd,&termios);
+
+ set_nonblocking (rfd);
+ set_nonblocking (wfd);
+
+
+ raw_termios (&termios);
+
+ tcsetattr(wfd,TCSANOW,&termios);
+
+ t->read = terminal_read;
+ t->write = terminal_write;
+ t->close = terminal_close;
+
+
+ return (TTY *) t;
+}
+
+void
+terminal_getsize (TTY *_t,CRT_POS *pos)
+{
+TERMINAL *t=(TTY *) _t;
+ struct winsize sz = { 0 };
+
+if ((!t) || (!pos)) return;
+
+if (ioctl (a->wfd, TIOCGWINSZ, &sz))
+ {
+ pos->x = CRT_COLS;
+ pos->y = CRT_ROWS;
+ }
+ else
+ {
+ pos->x = sz.ws_col;
+ pos->y = sz.ws_row;
+ }
+}
+
+
+void terminal_atexit(void)
+{
+while (terminal_list)
+ terminal_close(terminal_list);
+}
+
+