File: main.c

package info (click to toggle)
xrootconsole 990119-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 88 kB
  • ctags: 38
  • sloc: ansic: 324; makefile: 61
file content (366 lines) | stat: -rw-r--r-- 8,351 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/***************************************************************** -*- C -*- **
 *
 * main.c
 * xrootconsole
 * Copyright (C) 1998, 1999  Eric Youngblut
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The author of this software can be reached by e-mail at
 * yngblut@cs.washington.edu.  The latest version of this software can be
 * found at http://www.cs.washington.edu/homes/yngblut/
 */


#include "console.h"
#include "util.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define USAGE \
"Usage: xrootconsole [OPTION]...\n" \
"Redirect the console to a transparent window placed on the X root window.\n" \
"\n" \
"  -geometry GEO   the geometry of the window (default 80x25+0+0)\n" \
"  -f FILE         monitor a specific device\n" \
"  -fn FONT        the font to use (default fixed)\n" \
"  -fg COLOR       foreground color of the text (default white)\n" \
"  -bd COLOR       border color (default white)\n" \
"  -bw WIDTH       border width (default 0)\n" \
"  --topdown       insert lines at the top and scroll the rest down\n" \
"  --wrap          wrap long lines, instead of cutting them off\n" \
"\n" \
"This program may have to be run as root.\n" \
"\n" \
"Report bugs to <yngblut@cs.washington.edu>.\n"


#define DEFAULT_X		0
#define DEFAULT_Y		0
#define DEFAULT_COLS		80
#define DEFAULT_ROWS		25
#define DEFAULT_FONT		"fixed"
#define DEFAULT_COLOR		"white"
#define DEFAULT_BORDER_WIDTH	0
#define DEFAULT_BORDER_COLOR	"white"


Display* dpy;
char* text;			/* circular buffer to store the characters */
int pos = 0;			/* where the actual first line in text is */

Window win;
XFontStruct* fn = NULL;
GC gc;

char* filename = NULL;

int wrap = False;
int topdown = False;

int cols = DEFAULT_COLS;	/* size of the window (in characters) */
int rows = DEFAULT_ROWS;


static void init(char** argv);
static void handle_expose(XExposeEvent* xexpose);
static void put(const char* s);
static void event_loop(int fd);


/*
 * main
 */

#include <fcntl.h>

int main(int argc, char** argv)
{
    int fd;
    
    init(argv);

    if (filename)
        fd = open(filename, O_RDONLY | O_NONBLOCK, 0);
    else
        fd = open_console();

    if (fd == -1)
    {
	perror("open");
	exit(EXIT_FAILURE);
    }

    event_loop(fd);

    XCloseDisplay(dpy);
    exit(EXIT_SUCCESS);
}


/*
 * init
 */

void init(char** argv)
{
    XSetWindowAttributes a;
    XGCValues values;
    int geo_mask = 0;
    int x_root = DEFAULT_X, y_root = DEFAULT_Y;
    const char* fg = NULL;
    const char* bd = NULL;
    int width, height;
    int bw = 0;
    
    /* Connect to the X server */
    dpy = XOpenDisplay(NULL);
    if (dpy == NULL)
    {
	fprintf(stderr, "Cannot open display\n");
	exit(EXIT_FAILURE);
    }

    /* Process command-line arguments */
    while (*++argv != NULL)
    {
	if (!strcmp(*argv, "-geometry") || !strcmp(*argv, "-geo"))
	{
	    geo_mask = XParseGeometry(*++argv, &x_root, &y_root,
				      (unsigned*)&cols, (unsigned*)&rows);
	}
	else if (!strcmp(*argv, "-file") || !strcmp(*argv, "-f"))
	    filename = *++argv;
	else if (!strcmp(*argv, "-font") || !strcmp(*argv, "-fn"))
	    fn = load_font(*++argv);
	else if (!strcmp(*argv, "-foreground") || !strcmp(*argv, "-fg"))
	    fg = *++argv;
	else if (!strcmp(*argv, "-bordercolor") || !strcmp(*argv, "-bd"))
	    bd = *++argv;
	else if (!strcmp(*argv, "-borderwidth") || !strcmp(*argv, "-bw"))
	    bw = atoi(*++argv);
	else if (!strcmp(*argv, "--wrap"))
	    wrap = True;
	else if (!strcmp(*argv, "--topdown"))
	    topdown = True;
	else
	{
	    fprintf(stderr, "%s", USAGE);
	    exit(EXIT_FAILURE);
	}
    }

    /* Create the 2d array We can display "rows" # of rows, and the extra one
       is for the line being read. */
    text = (char*)malloc(cols * (rows + 1));
    memset(text, ' ', cols * (rows + 1));

    /* Load the default font if none has already been loaded */
    if (fn == NULL)
	fn = load_font(DEFAULT_FONT);

    /* Calculate the position of window on the screen */
    width = cols * fn->max_bounds.width;
    height = rows * (fn->ascent + fn->descent);

    x_root = (geo_mask & XNegative) ?
	(DisplayWidth(dpy, DefaultScreen(dpy)) - width - x_root) : x_root;
    y_root = (geo_mask & YNegative) ?
	(DisplayHeight(dpy, DefaultScreen(dpy)) - height - y_root) : y_root;

    /* Create the window */
    a.background_pixmap = ParentRelative;
    a.border_pixel = load_color((bd != NULL) ? bd : DEFAULT_BORDER_COLOR);
    a.event_mask = ExposureMask;
    a.override_redirect = True;
 
    win = XCreateWindow(dpy, DefaultRootWindow(dpy), x_root, y_root,
			width, height, bw, CopyFromParent, CopyFromParent,
			DefaultVisual(dpy, DefaultScreen(dpy)),
			CWOverrideRedirect | CWBackPixmap | CWEventMask |
			CWBorderPixel, &a);
    XMapWindow(dpy, win);
    XLowerWindow(dpy, win);

    /* Create the GC */
    values.font = fn->fid;
    values.foreground = load_color((fg != NULL) ? fg : DEFAULT_COLOR);
    gc = XCreateGC(dpy, win, GCFont | GCForeground, &values);

#ifdef STARTUP_MESSAGE
    /* Display a message */
    put(STARTUP_MESSAGE);
#endif
}


/*
 * handle_expose
 */

void handle_expose(XExposeEvent* ev)
{
    int col, width;
    int row, height;
    int x_text, y_text;
    
    XClearArea(dpy, win, ev->x, ev->y, ev->width, ev->height, False);

    col = ev->x / fn->max_bounds.width;
    width = (ev->x + ev->width - 1) / fn->max_bounds.width - col + 1;
    assert(width > 0 && col + width <= cols);
	
    row = ev->y / (fn->ascent + fn->descent);
    height = (ev->y + ev->height - 1) / (fn->ascent + fn->descent) - row + 1;
    assert(height > 0 && row + height <= rows);
	
    x_text = col * fn->max_bounds.width;
    y_text = row * (fn->ascent + fn->descent) + fn->ascent;

    /* row is the row index into text */
    row += pos + (topdown ? 1 : rows);
    while (height--)
    {
	XDrawString(dpy, win, gc, x_text, y_text,
		    text + (row % rows) * cols + col, width);

	row++;
	y_text += fn->ascent + fn->descent;
    }
}


/*
 * put
 */

void put(const char* s)
{
    static int x = 0, y = 0;
    
    while (*s != '\0')
    {
	switch (*s)
	{
	case '\n':
	    x = 0;
	    if (topdown)
	    {
		pos = (pos + rows - 1) % rows;
		memset(text + pos * cols, ' ', cols);
	    }
	    else if (y == rows - 1)
	    {
		memset(text + pos * cols, ' ', cols);
		pos = (pos + 1) % rows;
	    }
	    else
		y++;
	    break;

	case '\t':
	    x = (x / 8 + 1) * 8;
	    if (x >= cols && wrap)
		put("\n");
	    break;

	default:
	    if (*s >= ' ' && x < cols)
	    {
		text[x + ((y + pos) % rows) * cols] = *s;
		if (++x == cols && wrap)
		    put("\n");
	    }
	    break;
	}

	s++;
    }
}


/*
 * event_loop
 */

void event_loop(int Cfd)
{
    fd_set rfds;
    fd_set efds;
    int Xfd = ConnectionNumber(dpy);
    int maxfd = (Cfd > Xfd ? Cfd : Xfd) + 1;
    
    FD_ZERO(&rfds);
    FD_ZERO(&efds);
    
    for (;;)
    {
	while (XPending(dpy) > 0)
	{
	    XEvent ev;
	    XMaskEvent(dpy, ExposureMask, &ev);
	    handle_expose((XExposeEvent*)&ev.xexpose);
	}

	FD_SET(Xfd, &rfds);
	FD_SET(Cfd, &rfds);
	FD_SET(Cfd, &efds);
	if (select(maxfd, &rfds, NULL, &efds, NULL) == -1)
	{
	    perror("select");
	    exit(EXIT_FAILURE);
	}

	if (FD_ISSET(Cfd, &efds))
	{
	    close(Cfd);
	    return;
	}

	if (FD_ISSET(Cfd, &rfds))
	{
	    char buf[1024];
	    int n;

	    do
	    {
		n = read(Cfd, buf, sizeof(buf) - 1);
		if (n == -1)
		{
		    if (errno != EAGAIN)
		    {
			close(Cfd);
			return;
		    }
		}
		else if (n > 0)
		{
		    buf[n] = '\0';
		    put(buf);
		}
	    }
	    while (n > 0);

	    XClearArea(dpy, win, 0, 0, 0, 0, True);
	}
    }
}