source: trunk/minix/commands/talk/screen.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.6 KB
Line 
1/* screen.c Copyright Michael Temari 08/01/1996 All Rights Reserved */
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <stdio.h>
6#include <signal.h>
7#include <ctype.h>
8#include <string.h>
9#include <unistd.h>
10#include <curses.h>
11
12#include "screen.h"
13
14_PROTOTYPE(void gotsig, (int sig));
15_PROTOTYPE(static char *delword, (WINDOW *w));
16
17struct {
18 WINDOW *win;
19 char erase;
20 char kill;
21 char werase;
22} window[2];
23
24static char line[80+1];
25
26int ScreenDone = 0;
27
28static WINDOW *dwin;
29
30void gotsig(sig)
31int sig;
32{
33 ScreenDone = 1;
34 signal(sig, gotsig);
35}
36
37int ScreenInit()
38{
39int i;
40
41 if(initscr() == (WINDOW *)NULL) {
42 fprintf(stderr, "talk: Could not initscr\n");
43 return(-1);
44 }
45 signal(SIGINT, gotsig);
46 signal(SIGQUIT, gotsig);
47 signal(SIGPIPE, gotsig);
48 signal(SIGHUP, gotsig);
49 clear();
50 refresh();
51 noecho();
52 cbreak();
53
54 /* local window */
55 window[LOCALWIN].win = newwin(LINES / 2, COLS, 0, 0);
56 scrollok(window[LOCALWIN].win, TRUE);
57 wclear(window[LOCALWIN].win);
58
59 /* divider between windows */
60 dwin = newwin(1, COLS, LINES / 2, 0);
61 i = COLS;
62 while(i-- > 0)
63 waddch(dwin, '-');
64 wrefresh(dwin);
65
66 /* remote window */
67 window[REMOTEWIN].win = newwin(LINES - (LINES / 2) - 1, COLS, LINES / 2 + 1, 0);
68 scrollok(window[REMOTEWIN].win, TRUE);
69 wclear(window[REMOTEWIN].win);
70
71 return(0);
72}
73
74void ScreenMsg(msg)
75char *msg;
76{
77WINDOW *w;
78
79 w =window[LOCALWIN].win;
80
81 wmove(w, 0, 0);
82
83 if(*msg != '\0') {
84 wprintw(w, "[%s]", msg);
85 wclrtoeol(w);
86 } else
87 werase(w);
88
89 wrefresh(w);
90}
91
92void ScreenWho(user, host)
93char *user;
94char *host;
95{
96 if(*host != '\0') {
97 wmove(dwin, 0, (COLS - (1 + strlen(user) + 1 + strlen(host) + 1)) / 2);
98 wprintw(dwin, " %s@%s ", user, host);
99 } else {
100 wmove(dwin, 0, (COLS - (1 + strlen(user) + 1)) / 2);
101 wprintw(dwin, " %s ", user);
102 }
103 wrefresh(dwin);
104}
105
106void ScreenEdit(lcc, rcc)
107char lcc[];
108char rcc[];
109{
110 window[LOCALWIN].erase = lcc[0];
111 window[LOCALWIN].kill = lcc[1];
112 window[LOCALWIN].werase = lcc[2];
113 window[REMOTEWIN].erase = rcc[0];
114 window[REMOTEWIN].kill = rcc[1];
115 window[REMOTEWIN].werase = rcc[2];
116}
117
118void ScreenPut(data, len, win)
119char *data;
120int len;
121int win;
122{
123WINDOW *w;
124unsigned char ch;
125int r, c;
126
127 w = window[win].win;
128
129 while(len-- > 0) {
130 ch = *data++;
131 /* new line CR, NL */
132 if(ch == '\r' || ch == '\n') {
133 waddch(w, '\n');
134 } else
135 /* erase a character, BS, DEL */
136 if(ch == 0x08 || ch == 0x7f || ch == window[win].erase) {
137 getyx(w, r, c);
138 if(c > 0)
139 c--;
140 wmove(w, r, c);
141 waddch(w, ' ');
142 wmove(w, r, c);
143 } else
144 /* erase line CTL-U */
145 if(ch == 0x15 || ch == window[win].kill) {
146 getyx(w, r, c);
147 wmove(w, r, 0);
148 wclrtoeol(w);
149 } else
150 /* refresh CTL-L */
151 if(ch == 0x0c) {
152 if(win == LOCALWIN) {
153 touchwin(w);
154 wrefresh(w);
155 touchwin(window[REMOTEWIN].win);
156 wrefresh(window[REMOTEWIN].win);
157 }
158 } else
159 /* bell CTL-G */
160 if(ch == 0x07) {
161 putchar(ch);
162 }
163 else
164 /* erase last word CTL-W */
165 if(ch == 0x17 || ch == window[win].werase) {
166 (void) delword(w);
167 } else {
168 getyx(w, r, c);
169 if(1 || isprint(ch)) {
170 if(ch != ' ' && c == (COLS - 1))
171 wprintw(w, "\n%s", delword(w));
172 waddch(w, ch);
173 }
174 }
175 }
176 wrefresh(w);
177}
178
179static char *delword(w)
180WINDOW *w;
181{
182int r, c;
183int i = 0;
184char ch;
185char *p = &line[80];
186
187 *p-- = '\0';
188 getyx(w, r, c);
189 if(c == 0) return;
190 while(c >= 0) {
191 c--;
192 ch = mvwinch(w, r, c);
193 if(ch == ' ') break;
194 *p-- = ch;
195 i = 1;
196 waddch(w, ' ');
197 }
198 c += i;
199 wmove(w, r, c);
200 return(++p);
201}
202
203void ScreenEnd()
204{
205 move(LINES - 1, 0);
206 refresh();
207 endwin();
208}
Note: See TracBrowser for help on using the repository browser.