1/*	$NetBSD: ww.h,v 1.18 2009/04/14 08:50:06 lukem Exp $	*/
2
3/*
4 * Copyright (c) 1983, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)ww.h	8.1 (Berkeley) 6/6/93
35 */
36
37#ifndef __WW_H__
38#define __WW_H__
39
40#include <sys/types.h>
41#ifdef OLD_TTY
42#include <sgtty.h>
43#else
44#include <termios.h>
45#endif
46#include <setjmp.h>
47#include <stdarg.h>
48#include <stdio.h>
49#include <unistd.h>
50
51#ifndef EXTERN
52#define EXTERN extern
53#endif
54
55#define NWW	30		/* maximum number of windows */
56
57/* Macros to clear/set/test flags. */
58#define	SET(t, f)	(t) |= (f)
59#define	CLR(t, f)	(t) &= ~(f)
60#define	ISSET(t, f)	((t) & (f))
61
62	/* a rectangle */
63struct ww_dim {
64	int nr;			/* number of rows */
65	int nc;			/* number of columns */
66	int t, b;		/* top, bottom */
67	int l, r;		/* left, right */
68};
69
70	/* a coordinate */
71struct ww_pos {
72	int r;			/* row */
73	int c;			/* column */
74};
75
76	/* the window structure */
77struct ww {
78	int ww_flags;
79
80		/* general flags and states */
81	int ww_state;		/* state of window */
82#define WWS_INITIAL	0	/* just opened */
83#define WWS_HASPROC	1	/* has process on pty */
84#define WWS_DEAD	3	/* child died */
85#define	ww_oflags	ww_flags
86#define WWO_REVERSE	0x0001	/* make it all reverse video */
87#define WWO_GLASS	0x0002	/* make it all glass */
88#define WWO_FRAME	0x0004	/* this is a frame window */
89#define	WWO_ALLFLAGS	0x0007
90
91		/* information for overlap */
92	struct ww *ww_forw;	/* doubly linked list, for overlapping info */
93	struct ww *ww_back;
94	unsigned char ww_index;	/* the window index, for wwindex[] */
95#define WWX_NOBODY	NWW
96	int ww_order;		/* the overlapping order */
97
98		/* sizes and positions */
99	struct ww_dim ww_w;	/* window size and pos */
100	struct ww_dim ww_b;	/* buffer size and pos */
101	struct ww_dim ww_i;	/* the part inside the screen */
102	struct ww_pos ww_cur;	/* the cursor position, relative to ww_w */
103
104		/* arrays */
105	char **ww_win;		/* the window */
106	union ww_char **ww_buf;	/* the buffer */
107	char **ww_fmap;		/* map for frame and box windows */
108	short *ww_nvis;		/* how many ww_buf chars are visible per row */
109
110		/* information for wwwrite() and company */
111	int ww_wstate;		/* state for outputting characters */
112	char ww_modes;		/* current display modes */
113#define	ww_wflags	ww_flags
114#define	WWW_INSERT	0x0008	/* insert mode */
115#define	WWW_MAPNL	0x0010	/* map \n to \r\n */
116#define	WWW_NOUPDATE	0x0020	/* don't do updates in wwwrite() */
117#define	WWW_UNCTRL	0x0040	/* expand control characters */
118#define	WWW_NOINTR	0x0080	/* wwwrite() not interruptable */
119#define	WWW_HASCURSOR	0x0100	/* has fake cursor */
120
121		/* things for the window process and io */
122	int ww_type;
123#define	WWT_PTY		0	/* pty */
124#define	WWT_SOCKET	1	/* socket pair */
125#define	WWT_INTERNAL	2
126#define	ww_pflags	ww_flags
127#define	WWP_STOPPED	0x0200	/* output stopped */
128	int ww_pty;		/* file descriptor of pty or socket pair */
129	int ww_socket;		/* other end of socket pair */
130	int ww_pid;		/* pid of process, if WWS_HASPROC true */
131	char ww_ttyname[11];	/* "/dev/ttyp?" */
132	char *ww_ob;		/* output buffer */
133	char *ww_obe;		/* end of ww_ob */
134	char *ww_obp;		/* current read position in ww_ob */
135	char *ww_obq;		/* current write position in ww_ob */
136
137		/* things for the user, they really don't belong here */
138	int ww_id;		/* the user window id */
139#define	ww_uflags	ww_flags
140#define	WWU_CENTER	0x0400	/* center the label */
141#define	WWU_HASFRAME	0x0800	/* frame it */
142#define	WWU_KEEPOPEN	0x1000	/* keep it open after the process dies */
143#define	WWU_ALLFLAGS	0x1c00
144	char *ww_label;		/* the user supplied label */
145	struct ww_dim ww_alt;	/* alternate position and size */
146};
147
148	/* state of a tty */
149struct ww_tty {
150#ifdef OLD_TTY
151	struct sgttyb ww_sgttyb;
152	struct tchars ww_tchars;
153	struct ltchars ww_ltchars;
154	int ww_lmode;
155	int ww_ldisc;
156#else
157	struct termios ww_termios;
158#endif
159};
160
161union ww_char {
162	short c_w;		/* as a word */
163	struct {
164#if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
165		char C_c;	/* the character part */
166		char C_m;	/* the mode part */
167#endif
168#if BYTE_ORDER == BIG_ENDIAN
169		char C_m;	/* the mode part */
170		char C_c;	/* the character part */
171#endif
172	} c_un;
173};
174#define c_c c_un.C_c
175#define c_m c_un.C_m
176
177	/* for display update */
178struct ww_update {
179	int best_gain;
180	int best_col;
181	int gain;
182};
183
184	/* parts of ww_char */
185#define WWC_CMASK	0x00ff
186#define WWC_MMASK	0xff00
187#define WWC_MSHIFT	8
188
189	/* c_m bits */
190#define WWM_REV		0x01	/* reverse video */
191#define WWM_BLK		0x02	/* blinking */
192#define WWM_UL		0x04	/* underlined */
193#define WWM_GRP		0x08	/* graphics */
194#define WWM_DIM		0x10	/* half intensity */
195#define WWM_USR		0x20	/* user specified mode */
196#define WWM_GLS		0x40	/* window only, glass, i.e., transparent */
197
198	/* flags for ww_fmap */
199#define WWF_U		0x01
200#define WWF_R		0x02
201#define WWF_D		0x04
202#define WWF_L		0x08
203#define WWF_MASK	(WWF_U|WWF_R|WWF_D|WWF_L)
204#define WWF_LABEL	0x40
205#define WWF_TOP		0x80
206
207	/* error codes */
208#define WWE_NOERR	0
209#define WWE_SYS		1		/* system error */
210#define WWE_NOMEM	2		/* out of memory */
211#define WWE_TOOMANY	3		/* too many windows */
212#define WWE_NOPTY	4		/* no more ptys */
213#define WWE_SIZE	5		/* bad window size */
214#define WWE_BADTERM	6		/* bad terminal type */
215#define WWE_CANTDO	7		/* dumb terminal */
216
217	/* wwtouched[] bits, there used to be more than one */
218#define WWU_TOUCHED	0x01		/* touched */
219
220	/* the window structures */
221EXTERN struct ww wwhead;
222EXTERN struct ww *wwindex[NWW + 1];	/* last location is for wwnobody */
223EXTERN struct ww wwnobody;
224
225	/* tty things */
226EXTERN struct ww_tty wwoldtty;		/* the old (saved) terminal settings */
227EXTERN struct ww_tty wwnewtty;		/* the new (current) terminal settings */
228EXTERN struct ww_tty wwwintty;		/* the terminal settings for windows */
229EXTERN char *wwterm;			/* the terminal name */
230EXTERN char wwtermcap[1024];		/* place for the termcap */
231
232	/* generally useful variables */
233EXTERN int wwnrow, wwncol;		/* the screen size */
234EXTERN char wwavailmodes;		/* actually supported modes */
235EXTERN char wwcursormodes;		/* the modes for the fake cursor */
236EXTERN char wwwrap;			/* terminal has auto wrap around */
237EXTERN int wwdtablesize;		/* result of getdtablesize() call */
238EXTERN unsigned char **wwsmap;		/* the screen map */
239EXTERN union ww_char **wwos;		/* the old (current) screen */
240EXTERN union ww_char **wwns;		/* the new (desired) screen */
241EXTERN union ww_char **wwcs;		/* the checkpointed screen */
242EXTERN char *wwtouched;			/* wwns changed flags */
243EXTERN struct ww_update *wwupd;		/* for display update */
244EXTERN int wwospeed;			/* output baud rate, copied from wwoldtty */
245EXTERN int wwbaud;			/* wwospeed converted into actual number */
246EXTERN int wwcursorrow, wwcursorcol;	/* where we want the cursor to be */
247EXTERN int wwerrno;			/* error number */
248
249	/* statistics */
250EXTERN int wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc;
251EXTERN int wwnwwr, wwnwwra, wwnwwrc;
252EXTERN int wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc;
253EXTERN int wwnupdate, wwnupdline, wwnupdmiss;
254EXTERN int wwnupdscan, wwnupdclreol, wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline;
255EXTERN int wwnread, wwnreade, wwnreadz;
256EXTERN int wwnreadc, wwnreadack, wwnreadnack, wwnreadstat, wwnreadec;
257EXTERN int wwnwread, wwnwreade, wwnwreadz, wwnwreadd, wwnwreadc, wwnwreadp;
258EXTERN int wwnselect, wwnselecte, wwnselectz;
259
260	/* quicky macros */
261#define wwsetcursor(r,c) (wwcursorrow = (r), wwcursorcol = (c))
262#define wwcurtowin(w)	wwsetcursor((w)->ww_cur.r, (w)->ww_cur.c)
263#define wwunbox(w)	wwunframe(w)
264#define wwclreol(w,r,c)	wwclreol1((w), (r), (c), 0)
265#define wwredrawwin(w)	wwredrawwin1((w), (w)->ww_i.t, (w)->ww_i.b, 0)
266#define wwupdate()	wwupdate1(0, wwnrow);
267
268	/* things for handling input */
269EXTERN struct ww *wwcurwin;	/* window to copy input into */
270EXTERN char *wwib;		/* input (keyboard) buffer */
271EXTERN char *wwibe;		/* wwib + sizeof buffer */
272EXTERN char *wwibp;		/* current read position in buffer */
273EXTERN char *wwibq;		/* current write position in buffer */
274#define wwmaskc(c)	((c) & 0x7f)
275#define wwgetc()	(wwibp < wwibq ? wwmaskc(*wwibp++) : -1)
276#define wwpeekc()	(wwibp < wwibq ? wwmaskc(*wwibp) : -1)
277#define wwungetc(c)	(wwibp > wwib ? *--wwibp = (c) : -1)
278
279	/* things for short circuiting wwiomux() */
280EXTERN char wwintr;		/* interrupting */
281EXTERN char wwsetjmp;		/* want a longjmp() from wwrint() and wwchild() */
282EXTERN jmp_buf wwjmpbuf;	/* jmpbuf for above */
283#define wwinterrupt()	wwintr
284#define wwsetintr()	do { wwintr = 1; if (wwsetjmp) longjmp(wwjmpbuf, 1); } \
285			while (0)
286#define wwclrintr()	(wwintr = 0)
287
288	/* checkpointing */
289EXTERN int wwdocheckpoint;
290
291	/* the window virtual terminal */
292#define WWT_TERM	"window-v2"
293#define WWT_TERMCAP	"WW|window-v2|window program version 2:\
294	:am:bs:da:db:ms:pt:cr=^M:nl=^J:bl=^G:ta=^I:\
295	:cm=\\EY%+ %+ :le=^H:nd=\\EC:up=\\EA:do=\\EB:ho=\\EH:\
296	:cd=\\EJ:ce=\\EK:cl=\\EE:me=\\Er^?:"
297#define WWT_REV		"se=\\ErA:so=\\EsA:mr=\\EsA:"
298#define WWT_BLK		"BE=\\ErB:BS=\\EsB:mb=\\EsB:"
299#define WWT_UL		"ue=\\ErD:us=\\EsD:"
300#define WWT_GRP		"ae=\\ErH:as=\\EsH:"
301#define WWT_DIM		"HE=\\ErP:HS=\\EsP:mh=\\EsP:"
302#define WWT_USR		"XE=\\Er`:XS=\\Es`:"
303#define WWT_ALDL	"al=\\EL:dl=\\EM:"
304#define WWT_IMEI	"im=\\E@:ei=\\EO:ic=:mi:" /* XXX, ic for emacs bug */
305#define WWT_IC		"ic=\\EP:"
306#define WWT_DC		"dc=\\EN:"
307EXTERN char wwwintermcap[1024];	/* terminal-specific but window-independent
308				   part of the window termcap */
309#ifdef TERMINFO
310	/* where to put the temporary terminfo directory */
311EXTERN char wwterminfopath[1024];
312#endif
313
314struct ww *wwopen(int, int, int, int, int, int, int);
315void	wwadd(struct ww *, struct ww *);
316void	wwaddcap(const char *, char **);
317void	wwaddcap1(const char *, char **);
318void	wwalarm(int);
319char  **wwalloc(int, int, int, int, int);
320void	wwbell(void);
321void	wwbox(struct ww *, int, int, int, int);
322void	wwcheckpoint(void);
323void	wwchild(int);
324void	wwclose(struct ww *);
325void	wwclreol1(struct ww *, int, int, char);
326void	wwclreos(struct ww *, int, int);
327void	wwcopyscreen(union ww_char **s1, union ww_char **s2);
328void	wwcursor(struct ww *, int);
329void	wwdelchar(struct ww *, int, int);
330void	wwdelete(struct ww *);
331void	wwdelete1(struct ww *, int, int, int, int);
332void	wwdelline(struct ww *, int);
333void	wwdumpns(void);
334void	wwdumpnvis(struct ww *);
335void	wwdumpos(void);
336void	wwdumpsmap(void);
337void	wwdumpwin(struct ww *);
338void	wwend(int);
339int	wwenviron(struct ww *);
340const char *
341	wwerror(void);
342void	wwflush(void);
343void	wwframe(struct ww *, struct ww *);
344void	wwframec(struct ww *, int, int, char);
345void	wwfree(char **, int);
346int	wwgetpty(struct ww *);
347int	wwgettty(int, struct ww_tty *);
348int	wwgetttysize(int, int *, int *);
349void	wwgets(char *, int, struct ww *);
350int	wwinit(void);
351void	wwinschar(struct ww *, int, int, char, char);
352void	wwinsline(struct ww *, int);
353void	wwiomux(void);
354void	wwlabel(struct ww *, struct ww *, int, char *, int);
355void	wwmove(struct ww *, int, int);
356void	wwprintf(struct ww *, const char *, ...) __printflike(2, 3);
357void	wwputc(char, struct ww *);
358void	wwputs(const char *, struct ww *);
359void	wwredraw(void);
360void	wwredrawwin1(struct ww *,int, int, int);
361void	wwquit(int) __dead;
362void	wwreset(void);
363void	wwrint(void);
364void	wwscroll(struct ww *, int);
365int	wwscroll1(struct ww *, int, int, int, int);
366void	wwsetcursormodes(int);
367int	wwsettty(int, struct ww_tty *);
368int	wwsetttysize(int, int, int);
369int	wwsize(struct ww *, int, int);
370int	wwspawn(struct ww *, char *, char **);
371void	wwstart(void);
372void	wwstart1(void);
373int	wwstarttty(int);
374int	wwstoptty(int);
375void	wwsuspend(void);
376void	wwunframe(struct ww *);
377void	wwupdate1(int, int);
378int	wwvisible(struct ww *);
379void	wwvprintf(struct ww *, const char *, va_list);
380int	wwwrite(struct ww *, const char *, int);
381#ifdef TERMINFO
382int	wwterminfoinit(void);
383int	wwterminfoend(void);
384#endif
385
386#undef MIN
387#undef MAX
388#define MIN(x, y)	((x) > (y) ? (y) : (x))
389#define MAX(x, y)	((x) > (y) ? (x) : (y))
390
391#endif /* __WW_H__ */
392