Added copyright notices to files
[apps/madtty.git] / rote.h
1 /* ROTE - Our Own Terminal Emulation library 
2  * Copyright (c) 2004 Bruno T. C. de Oliveira
3  * All rights reserved
4  *
5  * 2004-08-25
6  */
7
8 /*
9 LICENSE INFORMATION:
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License (LGPL) as published by the Free Software Foundation.
13
14 Please refer to the COPYING file for more information.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24
25 Copyright (c) 2004 Bruno T. C. de Oliveira
26 */
27
28
29 #ifndef btco_ROTE_rote_h
30 #define btco_ROTE_rote_h
31
32 #include <ncurses.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36
37 /* Color codes: 0 = black, 1 = red, 2 = green, 3 = yellow, 4 = blue,
38  *              5 = magenta, 6 = cyan, 7 = white. 
39  *
40  * An 'attribute' as used in this library means an 8-bit value that conveys
41  * a foreground color code, a background color code, and the bold
42  * and blink bits. Each cell in the virtual terminal screen is associated
43  * with an attribute that specifies its appearance. The bits of an attribute,
44  * from most significant to least significant, are 
45  *
46  *  bit:      7 6 5 4 3 2 1 0
47  *  content:  S F F F H B B B
48  *            | `-,-' | `-,-'
49  *            |   |   |   |
50  *            |   |   |   `----- 3-bit background color code (0 - 7)
51  *            |   |   `--------- blink bit (if on, text is blinking)
52  *            |   `------------- 3-bit foreground color code (0 - 7)
53  *            `----------------- bold bit
54  *
55  * It is however recommended that you use the provided macros rather
56  * than dealing with this format directly.
57  *
58  * Sometimes we will call the 'SFFF' nibble above the 'extended
59  * foreground color code', and the 'HBBB' nibble the 'extended background
60  * color code'. So the extended color codes are just the regular
61  * color codes except that they have an additional bit (the most significant
62  * bit) indicating bold/blink.
63  */
64
65 /* retrieve attribute fields */
66 #define ROTE_ATTR_BG(attr)              ((attr) & 0x07)
67 #define ROTE_ATTR_FG(attr)              (((attr) & 0x70) >> 4)
68
69 /* retrieve 'extended' color codes (see above for info) */
70 #define ROTE_ATTR_XBG(attr)             ((attr) & 0x0F)
71 #define ROTE_ATTR_XFG(attr)             (((attr) & 0xF0) >> 4)
72
73 /* set attribute fields. This requires attr to be an lvalue, and it will
74  * be evaluated more than once. Use with care. */
75 #define ROTE_ATTR_MOD_BG(attr, newbg)    attr &= 0xF8, attr |= (newbg)
76 #define ROTE_ATTR_MOD_FG(attr, newfg)    attr &= 0x8F, attr |= ((newfg) << 4)
77 #define ROTE_ATTR_MOD_XBG(attr, newxbg)  attr &= 0xF0, attr |= (newxbg)
78 #define ROTE_ATTR_MOD_XFG(attr, newxfg)  attr &= 0x0F, attr |= ((newxfg) << 4)
79 #define ROTE_ATTR_MOD_BOLD(attr, boldbit) \
80                                attr &= 0x7F, attr |= (boldbit)?0x80:0x00
81 #define ROTE_ATTR_MOD_BLINK(attr, blinkbit) \
82                                attr &= 0xF7, attr |= (boldbit)?0x08:0x00
83
84 /* these return non-zero for 'yes', zero for 'no'. Don't rely on them being 
85  * any more specific than that (e.g. being exactly 1 for 'yes' or whatever). */
86 #define ROTE_ATTR_BOLD(attr)            ((attr) & 0x80)
87 #define ROTE_ATTR_BLINK(attr)           ((attr) & 0x08)
88
89 /* Represents each of the text cells in the terminal screen */
90 typedef struct RoteCell_ {
91    unsigned char ch;    /* >= 32, that is, control characters are not
92                          * allowed to be on the virtual screen */
93
94    unsigned char attr;  /* a color attribute, as described previously */
95 } RoteCell;
96
97 /* Declaration of opaque rote_Term_Private structure */
98 typedef struct RoteTermPrivate_ RoteTermPrivate;
99
100 /* Represents a virtual terminal. You may directly access the fields
101  * of this structure, but please pay close attention to the fields
102  * marked read-only or with special usage notes. */
103 typedef struct RoteTerm_ {
104    int rows, cols;              /* terminal dimensions, READ-ONLY. You
105                                  * can't resize the terminal by changing
106                                  * this (a segfault is about all you will 
107                                  * accomplish). */
108
109    RoteCell **cells;            /* matrix of cells. This
110                                  * matrix is indexed as cell[row][column]
111                                  * where 0 <= row < rows and
112                                  *       0 <= col < cols
113                                  *
114                                  * You may freely modify the contents of
115                                  * the cells.
116                                  */
117
118    int crow, ccol;              /* cursor coordinates. READ-ONLY. */
119
120    unsigned char curattr;       /* current attribute, that is the attribute
121                                  * that will be used for newly inserted
122                                  * characters */
123
124    pid_t childpid;              /* pid of the child process running in the
125                                  * terminal; 0 for none. This is READ-ONLY. */
126
127    RoteTermPrivate *pd;         /* private state data */
128
129    /* --- dirtiness flags: the following flags will be raised when the
130     * corresponding items are modified. They can only be unset by YOU
131     * (when, for example, you redraw the term or something) --- */
132    bool curpos_dirty;           /* whether cursor location has changed */
133    bool *line_dirty;            /* whether each row is dirty  */
134    /* --- end dirtiness flags */
135 } RoteTerm;
136
137 /* Creates a new virtual terminal with the given dimensions. You
138  * must destroy it with rote_vt_destroy after you are done with it.
139  * The terminal will be initially blank and the cursor will
140  * be at the top-left corner. 
141  *
142  * Returns NULL on error.
143  */
144 RoteTerm *rote_vt_create(int rows, int cols);
145
146 /* Destroys a virtual terminal previously created with
147  * rote_vt_create. If rt == NULL, does nothing. */
148 void rote_vt_destroy(RoteTerm *rt);
149
150 /* Starts a forked process in the terminal. The <command> parameter
151  * is a shell command to execute (it will be interpreted by '/bin/sh -c') 
152  * Returns the pid of the forked process. 
153  *
154  * Some useful reminders: If you want to be notified when child processes exit,
155  * you should handle the SIGCHLD signal.  If, on the other hand, you want to
156  * ignore exitting child processes, you should set the SIGCHLD handler to
157  * SIG_IGN to prevent child processes from hanging around the system as 'zombie
158  * processes'. 
159  *
160  * Continuing to write to a RoteTerm whose child process has died does not
161  * accomplish a lot, but is not an error and should not cause your program
162  * to crash or block indefinitely or anything of that sort :-)
163  * If, however, you want to be tidy and inform the RoteTerm that its
164  * child has died, call rote_vt_forsake_child when appropriate.
165  *
166  * If there is an error, returns -1. Notice that passing an invalid
167  * command will not cause an error at this level: the shell will try
168  * to execute the command and will exit with status 127. You can catch
169  * that by installing a SIGCHLD handler if you want.
170  */
171 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command);
172
173 /* Disconnects the RoteTerm from its forked child process. This function
174  * should be called when the child process dies or something of the sort.
175  * It is not strictly necessary to call this function, but it is
176  * certainly tidy. */
177 void rote_vt_forsake_child(RoteTerm *rt);
178
179 /* Does some data plumbing, that is, sees if the sub process has
180  * something to write to the terminal, and if so, write it. If you
181  * called rote_vt_fork to start a forked process, you must call
182  * this function regularly to update the terminal. 
183  *
184  * This function will not block, that is, if there is no data to be
185  * read from the child process it will return immediately. */
186 void rote_vt_update(RoteTerm *rt);
187
188 /* Puts data into the terminal: if there is a forked process running,
189  * the data will be sent to it. If there is no forked process,
190  * the data will simply be injected into the terminal (as in
191  * rote_vt_inject) */
192 void rote_vt_write(RoteTerm *rt, const char *data, int length);
193
194 /* Inject data into the terminal. <data> needs NOT be 0-terminated:
195  * its length is solely determined by the <length> parameter. Please
196  * notice that this writes directly to the terminal, that is,
197  * this function does NOT send the data to the forked process
198  * running in the terminal (if any). For that, you might want
199  * to use rote_vt_write.
200  */
201 void rote_vt_inject(RoteTerm *rt, const char *data, int length);
202
203 /* Paints the virtual terminal screen on the given window, putting
204  * the top-left corner at the given position. The cur_set_attr
205  * function must set the curses attributes given a Rote attribute
206  * byte. It should, for example, do wattrset(win, COLOR_PAIR(n)) where
207  * n is the colorpair appropriate for the attribute and such.
208  *
209  * If you pass NULL for cur_set_attr, the default implementation will
210  * set the color pair given by (bg * 8 + 7 - fg), which seems to be
211  * a common mapping, and the bold and blink attributes will be mapped 
212  * to A_BOLD and A_BLINK.
213  *
214  * At the end of the function, the cursor will be left where the virtual 
215  * cursor of the terminal is supposed to be.
216  *
217  * This function does not call wrefresh(win); you have to do that yourself.
218  * This function automatically calls rote_vt_update prior to drawing
219  * so that the drawn contents are accurate.
220  */
221 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int startrow, int startcol,
222                   void (*cur_set_attr)(WINDOW *win, unsigned char attr));
223
224 /* Indicates to the terminal that the given key has been pressed.
225  * This will cause the terminal to rote_vt_write() the appropriate
226  * escape sequence for that key (that is, the escape sequence
227  * that the linux text-mode console would produce for it). The argument,
228  * keycode, must be a CURSES EXTENDED KEYCODE, the ones you get
229  * when you use keypad(somewin, TRUE) (see man page). */
230 void rote_vt_keypress(RoteTerm *rt, int keycode);
231
232 /* Takes a snapshot of the current contents of the terminal and
233  * saves them to a dynamically allocated buffer. Returns a pointer
234  * to the newly created buffer, which you can pass to
235  * rote_vt_restore_snapshot. Caller is responsible for free()'ing when
236  * the snapshot is no longer needed. */
237 void *rote_vt_take_snapshot(RoteTerm *rt);
238
239 /* Restores a snapshot previously taken with rote_vt_take_snapshot.
240  * This function does NOT free() the passed buffer */
241 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf);
242
243 /* Declaration of custom escape sequence callback type. See the
244  * rote_vt_add_es_handler function for more info */
245 typedef int (*rote_es_handler_t)(RoteTerm *rt, const char *es);
246
247 /* Installs a custom escape sequence handler for the given RoteTerm.
248  * The handler will be called by the library every time it tries to
249  * recognize an escape sequence; depending on the return value of the
250  * handler, it will proceed in a different manner. See the description
251  * of the possible return values (ROTE_HANDLERESULT_* constants) below
252  * for more info.
253  *
254  * This handler will be called EACH TIME THE ESCAPE SEQUENCE BUFFER
255  * RECEIVES A CHARACTER. Therefore, it must execute speedily in order
256  * not to create too heavy a performance penalty. In particular, the
257  * writer of the handler should take care to quickly test for invalid
258  * or incomplete escape sequences before trying to do more elaborate
259  * parsing.
260  *
261  * The handler will NOT be called with an empty escape sequence (i.e.
262  * one in which only the initial ESC was received).
263  *
264  * The custom handler receives the terminal it pertains to and the
265  * escape sequence as a string (without the initial escape character).
266  *
267  * The handler may of course modify the terminal as it sees fit, taking 
268  * care not to corrupt it of course (in particular, it should appropriately 
269  * raise the line_dirty[] and curpos_dirty flags to indicate what it has 
270  * changed).
271  */
272 void rote_vt_install_handler(RoteTerm *rt, rote_es_handler_t handler);
273                             
274 /* Possible return values for the custom handler function and their
275  * meanings: */
276 #define ROTE_HANDLERESULT_OK 0      /* means escape sequence was handled */
277
278 #define ROTE_HANDLERESULT_NOTYET 1  /* means the escape sequence was not
279                                      * recognized yet, but there is hope that
280                                      * it still will once more characters
281                                      * arrive (i.e. it is not yet complete).
282                                      *
283                                      * The library will thus continue collecting
284                                      * characters and calling the handler as
285                                      * each character arrives until
286                                      * either OK or NOWAY is returned.
287                                      */
288
289 #define ROTE_HANDLERESULT_NOWAY 2   /* means the escape sequence was not
290                                      * recognized, and there is no chance
291                                      * that it will even if more characters
292                                      * are added to it. */
293
294 #endif
295