Annotation of embedaddon/trafshow/screen.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (c) 2004 Rinet Corp., Novosibirsk, Russia
3: *
4: * Redistribution and use in source forms, with and without modification,
5: * are permitted provided that this entire comment appears intact.
6: *
7: * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
8: */
9:
10: #ifdef HAVE_CONFIG_H
11: #include <config.h>
12: #endif
13:
14: #ifdef HAVE_SLCURSES
15: #include <slcurses.h>
16: #elif HAVE_NCURSES
17: #include <ncurses.h>
18: #else
19: #include <curses.h>
20: #endif
21: #include <sys/types.h>
22: #include <sys/ioctl.h>
23: #ifdef HAVE_SYS_TERMIOS_H
24: #include <sys/termios.h>
25: #endif
26: #include <fcntl.h>
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
30: #include <unistd.h>
31: #include <stdarg.h>
32:
33: #include "screen.h"
34: #include "colormask.h"
35:
36: #ifndef TIOCGWINSZ
37: #define TIOCGWINSZ 104
38: #endif
39:
40: int use_colors = 0;
41: int prompt_mode = 0;
42: double line_factor = 1;
43:
44:
45: /*
46: * Initialize curses.
47: */
48: int
49: screen_open(resize)
50: int resize;
51: {
52: if (!resize) {
53: if (initscr() == (WINDOW *)ERR) {
54: fprintf(stderr, "Can't initialize terminal -- unknown terminal type?\n");
55: return -1;
56: }
57: #ifdef HAVE_HAS_COLORS
58: use_colors = has_colors();
59: #ifdef HAVE_SLCURSES
60: SLtt_Use_Ansi_Colors = 1; /* force color mode */
61: use_colors = 1;
62: #endif
63: if (use_colors) {
64: start_color();
65: if (init_colormask() < 0) {
66: endwin();
67: return -1;
68: }
69: }
70: #endif /* HAVE_HAS_COLORS */
71: cbreak();
72: noecho();
73: nonl();
74:
75: } else { /* resize terminal */
76: int fd, new_rows = 0, new_cols = 0;
77: struct winsize ws;
78: char *cp;
79:
80: if ((fd = open("/dev/tty", 0)) != -1) {
81: if (ioctl(fd, TIOCGWINSZ, &ws) != -1) {
82: new_rows = ws.ws_row;
83: new_cols = ws.ws_col;
84: }
85: close(fd);
86: }
87: if (!new_rows) {
88: if ((cp = getenv("LINES")) != NULL)
89: new_rows = atoi(cp);
90: else new_rows = DEFAULT_LINES;
91: }
92: if (!new_cols) {
93: if ((cp = getenv("COLUMNS")) != NULL)
94: new_cols = atoi(cp);
95: else new_cols = DEFAULT_COLUMNS;
96: }
97: #ifdef HAVE_RESIZETERM
98: resizeterm(new_rows, new_cols);
99: #elif HAVE_SLCURSES
100: SLtt_Screen_Rows = new_rows;
101: SLtt_Screen_Cols = new_cols;
102: SLcurses_delwin(stdscr);
103: SLsmg_reset_smg();
104: SLsmg_init_smg();
105: stdscr = SLcurses_newwin(0, 0, 0, 0);
106: #else /* assume it work on all curses */
107: endwin();
108: initscr();
109: cbreak();
110: noecho();
111: nonl();
112: #endif
113: }
114: clear();
115:
116: prompt_mode = 0;
117: screen_dock_cursor(LINES-1, COLS-1);
118:
119: if (LINES < MINPAGESIZE) {
120: screen_status("Too small LINES (%d) on screen", LINES);
121: return -1;
122: }
123: if (COLS < MINPAGESIZE * 2) {
124: screen_status("Too small COLUMNS (%d) on screen", COLS);
125: return -1;
126: }
127:
128: line_factor = (double)COLS / (double)DEFAULT_COLUMNS;
129: return 0;
130: }
131:
132: /*
133: * Return terminal original settings.
134: */
135: void
136: screen_close()
137: {
138: attrset(A_NORMAL);
139: move(LINES-1, 0);
140: clrtoeol();
141: refresh();
142: endwin();
143: }
144:
145: void
146: screen_clear()
147: {
148: attrset(A_NORMAL);
149: clear();
150: refresh();
151: }
152:
153: static int curs_dock_x = 0, curs_dock_y = 0;
154:
155: void
156: screen_update()
157: {
158: move(curs_dock_y, curs_dock_x);
159: refresh();
160: }
161:
162: void
163: screen_dock_cursor(y, x)
164: int y, x;
165: {
166: curs_dock_x = x ? x : COLS - 1;
167: curs_dock_y = y ? y : LINES - 1;
168: }
169:
170: void
171: screen_status(const char *fmt, ...)
172: {
173: va_list ap;
174: char buf[1024];
175:
176: va_start(ap, fmt);
177: (void)strcpy(buf, "[ ");
178: vsprintf(buf+2, fmt, ap);
179: buf[COLS-4] = '\0';
180: (void)strcat(buf, " ]");
181:
182: attrset(A_STANDOUT);
183: mvaddstr(LINES-2, COLS/2 - strlen(buf)/2, buf);
184: attrset(A_NORMAL);
185:
186: screen_update();
187: (void)sleep(1);
188: }
189:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>