Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 1 | /* Public Domain Curses */ |
| 2 | |
| 3 | #include <curspriv.h> |
| 4 | |
| 5 | RCSID("$Id: kernel.c,v 1.78 2008/07/15 17:13:26 wmcbrine Exp $") |
| 6 | |
| 7 | /*man-start************************************************************** |
| 8 | |
| 9 | Name: kernel |
| 10 | |
| 11 | Synopsis: |
| 12 | int def_prog_mode(void); |
| 13 | int def_shell_mode(void); |
| 14 | int reset_prog_mode(void); |
| 15 | int reset_shell_mode(void); |
| 16 | int resetty(void); |
| 17 | int savetty(void); |
| 18 | int ripoffline(int line, int (*init)(WINDOW *, int)); |
| 19 | int curs_set(int visibility); |
| 20 | int napms(int ms); |
| 21 | |
| 22 | int draino(int ms); |
| 23 | int resetterm(void); |
| 24 | int fixterm(void); |
| 25 | int saveterm(void); |
| 26 | |
| 27 | Description: |
| 28 | def_prog_mode() and def_shell_mode() save the current terminal |
| 29 | modes as the "program" (in curses) or "shell" (not in curses) |
| 30 | state for use by the reset_prog_mode() and reset_shell_mode() |
| 31 | functions. This is done automatically by initscr(). |
| 32 | |
| 33 | reset_prog_mode() and reset_shell_mode() restore the terminal to |
| 34 | "program" (in curses) or "shell" (not in curses) state. These |
| 35 | are done automatically by endwin() and doupdate() after an |
| 36 | endwin(), so they would normally not be called before these |
| 37 | functions. |
| 38 | |
| 39 | savetty() and resetty() save and restore the state of the |
| 40 | terminal modes. savetty() saves the current state in a buffer, |
| 41 | and resetty() restores the state to what it was at the last call |
| 42 | to savetty(). |
| 43 | |
| 44 | curs_set() alters the appearance of the cursor. A visibility of |
| 45 | 0 makes it disappear; 1 makes it appear "normal" (usually an |
| 46 | underline) and 2 makes it "highly visible" (usually a block). |
| 47 | |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 48 | ripoffline() reduces the size of stdscr by one line. If the |
| 49 | "line" parameter is positive, the line is removed from the top |
| 50 | of the screen; if negative, from the bottom. Up to 5 lines can |
| 51 | be ripped off stdscr by calling ripoffline() repeatedly. The |
| 52 | function argument, init, is called from within initscr() or |
| 53 | newterm(), so ripoffline() must be called before either of these |
| 54 | functions. The init function receives a pointer to a one-line |
| 55 | WINDOW, and the width of the window. Calling ripoffline() with a |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 56 | NULL init function pointer is an error. |
| 57 | |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 58 | napms() suspends the program for the specified number of |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 59 | milliseconds. draino() is an archaic equivalent. |
| 60 | |
| 61 | resetterm(), fixterm() and saveterm() are archaic equivalents |
| 62 | for reset_shell_mode(), reset_prog_mode() and def_prog_mode(), |
| 63 | respectively. |
| 64 | |
| 65 | Return Value: |
| 66 | All functions return OK on success and ERR on error, except |
| 67 | curs_set(), which returns the previous visibility. |
| 68 | |
| 69 | Portability X/Open BSD SYS V |
| 70 | def_prog_mode Y Y Y |
| 71 | def_shell_mode Y Y Y |
| 72 | reset_prog_mode Y Y Y |
| 73 | reset_shell_mode Y Y Y |
| 74 | resetty Y Y Y |
| 75 | savetty Y Y Y |
| 76 | ripoffline Y - 3.0 |
| 77 | curs_set Y - 3.0 |
| 78 | napms Y Y Y |
| 79 | draino - |
| 80 | resetterm - |
| 81 | fixterm - |
| 82 | saveterm - |
| 83 | |
| 84 | **man-end****************************************************************/ |
| 85 | |
| 86 | #include <string.h> |
| 87 | |
| 88 | RIPPEDOFFLINE linesripped[5]; |
| 89 | char linesrippedoff = 0; |
| 90 | |
| 91 | static struct cttyset |
| 92 | { |
| 93 | bool been_set; |
| 94 | SCREEN saved; |
| 95 | } ctty[3]; |
| 96 | |
| 97 | enum { PDC_SH_TTY, PDC_PR_TTY, PDC_SAVE_TTY }; |
| 98 | |
| 99 | static void _save_mode(int i) |
| 100 | { |
| 101 | ctty[i].been_set = TRUE; |
| 102 | |
| 103 | memcpy(&(ctty[i].saved), SP, sizeof(SCREEN)); |
| 104 | |
| 105 | PDC_save_screen_mode(i); |
| 106 | } |
| 107 | |
| 108 | static int _restore_mode(int i) |
| 109 | { |
| 110 | if (ctty[i].been_set == TRUE) |
| 111 | { |
| 112 | memcpy(SP, &(ctty[i].saved), sizeof(SCREEN)); |
| 113 | |
| 114 | if (ctty[i].saved.raw_out) |
| 115 | raw(); |
| 116 | |
| 117 | PDC_restore_screen_mode(i); |
| 118 | |
| 119 | if ((LINES != ctty[i].saved.lines) || |
| 120 | (COLS != ctty[i].saved.cols)) |
| 121 | resize_term(ctty[i].saved.lines, ctty[i].saved.cols); |
| 122 | |
| 123 | PDC_curs_set(ctty[i].saved.visibility); |
| 124 | |
| 125 | PDC_gotoyx(ctty[i].saved.cursrow, ctty[i].saved.curscol); |
| 126 | } |
| 127 | |
| 128 | return ctty[i].been_set ? OK : ERR; |
| 129 | } |
| 130 | |
| 131 | int def_prog_mode(void) |
| 132 | { |
| 133 | PDC_LOG(("def_prog_mode() - called\n")); |
| 134 | |
| 135 | _save_mode(PDC_PR_TTY); |
| 136 | |
| 137 | return OK; |
| 138 | } |
| 139 | |
| 140 | int def_shell_mode(void) |
| 141 | { |
| 142 | PDC_LOG(("def_shell_mode() - called\n")); |
| 143 | |
| 144 | _save_mode(PDC_SH_TTY); |
| 145 | |
| 146 | return OK; |
| 147 | } |
| 148 | |
| 149 | int reset_prog_mode(void) |
| 150 | { |
| 151 | PDC_LOG(("reset_prog_mode() - called\n")); |
| 152 | |
| 153 | _restore_mode(PDC_PR_TTY); |
| 154 | PDC_reset_prog_mode(); |
| 155 | |
| 156 | return OK; |
| 157 | } |
| 158 | |
| 159 | int reset_shell_mode(void) |
| 160 | { |
| 161 | PDC_LOG(("reset_shell_mode() - called\n")); |
| 162 | |
| 163 | _restore_mode(PDC_SH_TTY); |
| 164 | PDC_reset_shell_mode(); |
| 165 | |
| 166 | return OK; |
| 167 | } |
| 168 | |
| 169 | int resetty(void) |
| 170 | { |
| 171 | PDC_LOG(("resetty() - called\n")); |
| 172 | |
| 173 | return _restore_mode(PDC_SAVE_TTY); |
| 174 | } |
| 175 | |
| 176 | int savetty(void) |
| 177 | { |
| 178 | PDC_LOG(("savetty() - called\n")); |
| 179 | |
| 180 | _save_mode(PDC_SAVE_TTY); |
| 181 | |
| 182 | return OK; |
| 183 | } |
| 184 | |
| 185 | int curs_set(int visibility) |
| 186 | { |
| 187 | int ret_vis; |
| 188 | |
| 189 | PDC_LOG(("curs_set() - called: visibility=%d\n", visibility)); |
| 190 | |
| 191 | if ((visibility < 0) || (visibility > 2)) |
| 192 | return ERR; |
| 193 | |
| 194 | ret_vis = PDC_curs_set(visibility); |
| 195 | |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 196 | /* If the cursor is changing from invisible to visible, update |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 197 | its position */ |
| 198 | |
| 199 | if (visibility && !ret_vis) |
| 200 | PDC_gotoyx(SP->cursrow, SP->curscol); |
| 201 | |
| 202 | return ret_vis; |
| 203 | } |
| 204 | |
| 205 | int napms(int ms) |
| 206 | { |
| 207 | PDC_LOG(("napms() - called: ms=%d\n", ms)); |
| 208 | |
| 209 | if (ms) |
| 210 | PDC_napms(ms); |
| 211 | |
| 212 | return OK; |
| 213 | } |
| 214 | |
| 215 | int ripoffline(int line, int (*init)(WINDOW *, int)) |
| 216 | { |
| 217 | PDC_LOG(("ripoffline() - called: line=%d\n", line)); |
| 218 | |
| 219 | if (linesrippedoff < 5 && line && init) |
| 220 | { |
| 221 | linesripped[(int)linesrippedoff].line = line; |
| 222 | linesripped[(int)linesrippedoff++].init = init; |
| 223 | |
| 224 | return OK; |
| 225 | } |
| 226 | |
| 227 | return ERR; |
| 228 | } |
| 229 | |
| 230 | int draino(int ms) |
| 231 | { |
| 232 | PDC_LOG(("draino() - called\n")); |
| 233 | |
| 234 | return napms(ms); |
| 235 | } |
| 236 | |
| 237 | int resetterm(void) |
| 238 | { |
| 239 | PDC_LOG(("resetterm() - called\n")); |
| 240 | |
| 241 | return reset_shell_mode(); |
| 242 | } |
| 243 | |
| 244 | int fixterm(void) |
| 245 | { |
| 246 | PDC_LOG(("fixterm() - called\n")); |
| 247 | |
| 248 | return reset_prog_mode(); |
| 249 | } |
| 250 | |
| 251 | int saveterm(void) |
| 252 | { |
| 253 | PDC_LOG(("saveterm() - called\n")); |
| 254 | |
| 255 | return def_prog_mode(); |
| 256 | } |