ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
(Generate patch)

Comparing root-tail/root-tail.c (file contents):
Revision 1.50 by chris_moore, Wed Apr 7 03:57:00 2004 UTC vs.
Revision 1.63 by chris_moore, Thu Apr 15 00:38:13 2004 UTC

1/* 1/*
2 * Copyright 2001 by Marco d'Itri <md@linux.it> 2 * Copyright 2001 by Marco d'Itri <md@linux.it>
3 * Copyright 2000,2001,2002,2003,2004 3 * Copyright 2000,2001,2002,2003,2004
4 * Marc Lehmann <pcg@goof.com>, 4 * Marc Lehmann <pcg@goof.com>,
5 * Chris Moore <chris.moore@mail.com>,
5 * and many others, see README 6 * and many others, see README
6 * 7 *
7 * Original version by Mike Baker. 8 * Original version by Mike Baker.
8 * 9 *
9 * This program is free software; you can redistribute it and/or modify 10 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 11 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 12 * the Free Software Foundation; either version 2 of the License, or
46#endif 47#endif
47 48
48#define SHADE_X 2 49#define SHADE_X 2
49#define SHADE_Y 2 50#define SHADE_Y 2
50 51
52/* some italic fonts still go over the margin - this margin of error cleans up the mess */
53#define MARGIN_OF_ERROR 2
54
51/* data structures */ 55/* data structures */
52struct logfile_entry 56struct logfile_entry
53{ 57{
54 struct logfile_entry *next; 58 struct logfile_entry *next;
55 59
56 char *fname; /* name of file */ 60 char *fname; /* name of file */
57 char *desc; /* alternative description */ 61 char *desc; /* alternative description */
58 char *buf; /* text read but not yet displayed */ 62 char *buf; /* text read but not yet displayed */
63 const char *fontname;
64 XFontSet fontset;
65 int font_height;
66 int font_ascent;
59 FILE *fp; /* FILE struct associated with file */ 67 FILE *fp; /* FILE struct associated with file */
60 ino_t inode; /* inode of the file opened */ 68 ino_t inode; /* inode of the file opened */
61 off_t last_size; /* file size at the last check */ 69 off_t last_size; /* file size at the last check */
62 unsigned long color; /* color to be used for printing */ 70 unsigned long color; /* color to be used for printing */
71 const char *colorname; /* color name/string */
63 int partial; /* true if the last line isn't complete */ 72 int partial; /* true if the last line isn't complete */
64 int lastpartial; /* true if the previous output wasn't complete */ 73 int lastpartial; /* true if the previous output wasn't complete */
65 struct line_node *last; /* last line we output */ 74 struct line_node *last; /* last line we output */
66 int modified; /* true if line is modified & needs displaying */ 75 int modified; /* true if line is modified & needs displaying */
67}; 76};
90 99
91struct displaymatrix 100struct displaymatrix
92{ 101{
93 char *line; 102 char *line;
94 int len; 103 int len;
104 int offset;
95 int buffer_size; 105 int buffer_size;
96 unsigned long color; 106 unsigned long color;
97}; 107};
98 108
99/* global variables */ 109/* global variables */
100int debug = 1;
101
102struct line_node *linelist = NULL, *linelist_tail = NULL; 110struct line_node *linelist = NULL, *linelist_tail = NULL;
103struct displaymatrix *display; 111struct displaymatrix *display;
104int continuation_width = -1; 112int continuation_width = -1;
105int continuation_color; 113int continuation_color;
106int continuation_length; 114int continuation_length;
107 115
116/* HACK - ideally listlen will start at however many '~'s will fit on
117 * the screen */
108int width = STD_WIDTH, height = STD_HEIGHT, listlen; 118int width = STD_WIDTH, height = STD_HEIGHT, listlen = 50;
109int win_x = LOC_X, win_y = LOC_Y; 119int win_x = LOC_X, win_y = LOC_Y;
110int font_ascent, font_height;
111int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */ 120int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */
112int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */ 121int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
113int do_reopen; 122int do_reopen;
114struct timeval interval = { 2, 400000 }; 123struct timeval interval = { 2, 400000 };
115XFontSet fontset;
116 124
117/* command line options */ 125/* command line options */
118int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, 126int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
119 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap, 127 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
120 opt_justify, geom_mask, reload = 0; 128 opt_justify, geom_mask, opt_minspace, reload;
121const char *command = NULL, 129const char *command = NULL,
122 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, 130 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
123 *continuation = "|| ", *cont_color = DEF_CONT_COLOR; 131 *continuation = "|| ", *cont_color = DEF_CONT_COLOR;
124 132
125struct logfile_entry *loglist = NULL, *loglist_tail = NULL; 133struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
144/* prototypes */ 152/* prototypes */
145void list_files (int); 153void list_files (int);
146void force_reopen (int); 154void force_reopen (int);
147void force_refresh (int); 155void force_refresh (int);
148void blank_window (int); 156void blank_window (int);
157#ifdef USE_TOON_GET_ROOT_WINDOW
158Window ToonGetRootWindow(Display *, int, Window *);
159#endif /* USE_TOON_GET_ROOT_WINDOW */
149 160
150void InitWindow (void); 161void InitWindow (void);
151unsigned long GetColor (const char *); 162unsigned long GetColor (const char *);
152void redraw (int); 163void redraw (int);
153void refresh (int, int, int, int); 164void refresh (int, int, int, int);
191} 202}
192 203
193void 204void
194blank_window (int dummy) 205blank_window (int dummy)
195{ 206{
196 XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False); 207 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False);
197 XFlush (disp); 208 XFlush (disp);
198 exit (0); 209 exit (0);
199} 210}
200 211
201/* X related functions */ 212/* X related functions */
214 fprintf (stderr, "can't allocate %s\n", ColorName); 225 fprintf (stderr, "can't allocate %s\n", ColorName);
215 226
216 return Color.pixel; 227 return Color.pixel;
217} 228}
218 229
230#ifndef USE_TOON_GET_ROOT_WINDOW
219static Window 231static void
220root_window (Display * display, int screen_number) 232find_root_window (Display *display, int screen_number)
221{ 233{
234 if (!root)
235 {
222 Atom SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False); 236 Atom SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False);
237 Atom NAUTILUS_DESKTOP_WINDOW_ID = XInternAtom (display, "NAUTILUS_DESKTOP_WINDOW_ID", False);
223 Window real_root_window = RootWindow (display, screen_number); 238 root = RootWindow (display, screen_number);
224 239
225 if (root) /* root window set via option */
226 return root;
227
228 if (SWM_VROOT != None)
229 {
230 Window unused, *windows; 240 Window unused, *windows = 0;
231 unsigned int count; 241 unsigned int count;
232 242
243 Atom type;
244 int format;
245 unsigned long nitems, bytes_after_return;
246 unsigned char *virtual_root_window;
247
248 if (XGetWindowProperty (display, root, NAUTILUS_DESKTOP_WINDOW_ID,
249 0, 1, False, XA_WINDOW, &type, &format,
250 &nitems, &bytes_after_return,
251 &virtual_root_window) == Success
252 && type == XA_WINDOW)
253 {
233 if (XQueryTree (display, real_root_window, &unused, &unused, &windows, 254 if (XQueryTree (display, *(Window *)virtual_root_window, &unused, &unused, &windows, &count))
234 &count)) 255 root = windows[count - 1];
256
257 XFree (virtual_root_window);
258 }
259 else if (XQueryTree (display, root, &unused, &unused, &windows, &count))
235 { 260 {
236 int i; 261 int i;
237 262
238 for (i = 0; i < count; i++) 263 for (i = 0; i < count; i++)
239 { 264 {
240 Atom type;
241 int format;
242 unsigned long nitems, bytes_after_return;
243 unsigned char *virtual_root_window;
244
245 if (XGetWindowProperty (display, windows[i], SWM_VROOT, 265 if (XGetWindowProperty (display, windows[i], SWM_VROOT,
246 0, 1, False, XA_WINDOW, &type, &format, 266 0, 1, False, XA_WINDOW, &type, &format,
247 &nitems, &bytes_after_return, 267 &nitems, &bytes_after_return,
248 &virtual_root_window) == Success) 268 &virtual_root_window) == Success
269 && type == XA_WINDOW)
249 { 270 {
250 if (type != None)
251 {
252 if (type == XA_WINDOW)
253 {
254 XFree (windows);
255 return (Window) virtual_root_window; 271 root = *(Window *)virtual_root_window;
256 } 272 XFree (virtual_root_window);
257 else
258 fprintf (stderr,
259 "__SWM_VROOT property type mismatch");
260 } 273 break;
261 } 274 }
262 else 275 }
263 fprintf (stderr,
264 "failed to get __SWM_VROOT property on window 0x%lx",
265 windows[i]);
266 }
267 276
268 if (count)
269 XFree (windows); 277 XFree (windows);
270 } 278 }
271 else 279 else
272 fprintf (stderr, "Can't query tree on root window 0x%lx", 280 fprintf (stderr, "Can't query tree on root window 0x%lx", root);
273 real_root_window);
274 } 281 }
275 else
276 /* This shouldn't happen. The Xlib documentation is wrong BTW. */
277 fprintf (stderr, "Can't intern atom __SWM_VROOT");
278
279 return real_root_window;
280} 282}
283#endif /* USE_TOON_GET_ROOT_WINDOW */
281 284
282void 285void
283InitWindow (void) 286InitWindow (void)
284{ 287{
285 XGCValues gcv; 288 XGCValues gcv;
286 unsigned long gcm; 289 unsigned long gcm;
287 int screen, ScreenWidth, ScreenHeight; 290 int screen, ScreenWidth, ScreenHeight;
291 struct logfile_entry *e;
288 292
289 if (!(disp = XOpenDisplay (dispname))) 293 if (!(disp = XOpenDisplay (dispname)))
290 { 294 {
291 fprintf (stderr, "Can't open display %s.\n", dispname); 295 fprintf (stderr, "Can't open display %s.\n", dispname);
292 exit (1); 296 exit (1);
294 298
295 screen = DefaultScreen (disp); 299 screen = DefaultScreen (disp);
296 ScreenHeight = DisplayHeight (disp, screen); 300 ScreenHeight = DisplayHeight (disp, screen);
297 ScreenWidth = DisplayWidth (disp, screen); 301 ScreenWidth = DisplayWidth (disp, screen);
298 302
299 root = root_window (disp, screen); 303 find_root_window (disp, screen);
300 304
301 gcm = GCBackground; 305 gcm = GCBackground;
302 gcv.graphics_exposures = True; 306 gcv.graphics_exposures = True;
303 WinGC = XCreateGC (disp, root, gcm, &gcv); 307 WinGC = XCreateGC (disp, root, gcm, &gcv);
304 XMapWindow (disp, root); 308 XMapWindow (disp, root);
309
305 XSetForeground (disp, WinGC, GetColor (DEF_COLOR)); 310 XSetForeground (disp, WinGC, GetColor (DEF_COLOR));
306 311
312 for (e = loglist; e; e = e->next)
307 { 313 {
308 char **missing_charset_list; 314 char **missing_charset_list;
309 int missing_charset_count; 315 int missing_charset_count;
310 char *def_string; 316 char *def_string;
311 317
312 fontset = XCreateFontSet (disp, fontname, 318 e->fontset = XCreateFontSet (disp, e->fontname,
313 &missing_charset_list, &missing_charset_count, 319 &missing_charset_list, &missing_charset_count,
314 &def_string); 320 &def_string);
315 321
316 if (missing_charset_count) 322 if (missing_charset_count)
323 {
324 fprintf (stderr,
325 "Missing charsets in String to FontSet conversion (%s)\n",
326 missing_charset_list[0]);
327 XFreeStringList (missing_charset_list);
328 }
329
330 if (!e->fontset)
331 {
332 fprintf (stderr, "unable to create fontset for font '%s', exiting.\n", e->fontname);
333 exit (1);
334 }
335
317 { 336 {
318 fprintf (stderr, 337 XFontSetExtents *xfe = XExtentsOfFontSet (e->fontset);
319 "Missing charsets in String to FontSet conversion (%s)\n", 338
320 missing_charset_list[0]); 339 e->font_height = xfe->max_logical_extent.height;
321 XFreeStringList (missing_charset_list); 340 e->font_ascent = -xfe->max_logical_extent.y;
322 } 341 }
323 }
324 342
325 if (!fontset) 343 if (e->font_height > height - effect_y_space)
344 {
345 fprintf(stderr, "\n the display isn't tall enough to display a single line in font '%s'\n",
346 e->fontname);
347 fprintf(stderr, "\n the geometry in use is %d pixels tall\n", height);
348 fprintf(stderr, "\n font '%s' is %d pixels tall\n", e->fontname, e->font_height);
349 if (effect_y_space)
350 fprintf(stderr, "\n the shade or outline options need an extra %d pixel%s of vertical space\n",
351 effect_y_space, effect_y_space == 1 ? "" : "s");
352 fprintf(stderr, "\n");
353 exit(1);
354 }
326 { 355 }
327 fprintf (stderr, "unable to create fontset, exiting.\n");
328 exit (1);
329 }
330
331 {
332 XFontSetExtents *xfe = XExtentsOfFontSet (fontset);
333
334 font_height = xfe->max_logical_extent.height;
335 font_ascent = -xfe->max_logical_extent.y;
336 }
337 356
338 if (geom_mask & XNegative) 357 if (geom_mask & XNegative)
339 win_x = win_x + ScreenWidth - width; 358 win_x = win_x + ScreenWidth - width;
340 if (geom_mask & YNegative) 359 if (geom_mask & YNegative)
341 win_y = win_y + ScreenHeight - height; 360 win_y = win_y + ScreenHeight - height;
342 361
343 if (opt_outline)
344 { 362 {
345 /* adding outline increases the total width and height by 2 363 struct logfile_entry *e;
346 pixels each, and offsets the text one pixel right and one 364
347 pixel down */ 365 for (e = loglist; e; e = e->next)
348 effect_x_space = effect_y_space = 2; 366 e->color = GetColor (e->colorname);
349 effect_x_offset = effect_y_offset = 1;
350 } 367 }
351 else if (opt_shade)
352 {
353 /* adding a shadow increases the space used */
354 effect_x_space = abs (SHADE_X);
355 effect_y_space = abs (SHADE_Y);
356 /* if the shadow is to the right and below then we don't need
357 * to move the text to make space for it, but shadows to the left
358 * and above need accomodating */
359 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
360 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
361 }
362 else
363 {
364 effect_x_space = effect_y_space = 0;
365 effect_x_offset = effect_y_offset = 0;
366 }
367
368 /* if we are using -shade or -outline, there will be less usable
369 * space for output */
370 listlen = (height - effect_y_space) / font_height;
371
372 if (!listlen)
373 {
374 fprintf (stderr, "height too small for a single line, setting to %d\n",
375 font_height);
376 listlen = 1;
377 }
378 368
379 XSelectInput (disp, root, ExposureMask | FocusChangeMask); 369 XSelectInput (disp, root, ExposureMask | FocusChangeMask);
380} 370}
381 371
382/* 372/*
392{ 382{
393 XSetClipMask (disp, WinGC, None); 383 XSetClipMask (disp, WinGC, None);
394 refresh (0, 32768, 1, redraw_all); 384 refresh (0, 32768, 1, redraw_all);
395} 385}
396 386
397void draw_text (Display *disp, Window root, XFontSet fontset, GC WinGC, int x, int y, struct line_node *line, int foreground) 387void draw_text (Display *disp, Window root, GC WinGC, int x, int y, struct line_node *line, int foreground)
398{ 388{
399 if (line->wrapped_right && opt_justify && line->breaks) 389 if (line->wrapped_right && opt_justify && line->breaks)
400 { 390 {
401 int i; 391 int i;
402 for (i = 0; i < line->num_words; i++) { 392 for (i = 0; i < line->num_words; i++)
403 XmbDrawString (disp, root, fontset, WinGC, 393 XmbDrawString (disp, root, line->logfile->fontset, WinGC,
404 x + line->breaks[i].width + ((i * line->free_pixels) / (line->num_words - 1)) + continuation_width * line->wrapped_left, y, 394 x + line->breaks[i].width + ((i * line->free_pixels) / (line->num_words - 1))
395 + continuation_width * line->wrapped_left, y,
405 line->line + line->breaks[i].index, 396 line->line + line->breaks[i].index,
406 line->breaks[i].len); 397 line->breaks[i].len);
407 } 398
408 if (line->wrapped_left) 399 if (line->wrapped_left)
409 { 400 {
410 if (foreground) XSetForeground (disp, WinGC, continuation_color); 401 if (foreground) XSetForeground (disp, WinGC, continuation_color);
411 XmbDrawString (disp, root, fontset, WinGC, x, y, continuation, continuation_length); 402 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x, y, continuation, continuation_length);
412 } 403 }
413 } 404 }
414 else 405 else
415 { 406 {
416 XmbDrawString (disp, root, fontset, WinGC, x + continuation_width * line->wrapped_left, y, line->line, line->len); 407 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x + continuation_width * line->wrapped_left,
408 y, line->line, line->len);
409
417 if (line->wrapped_left) 410 if (line->wrapped_left)
418 { 411 {
419 if (foreground) XSetForeground (disp, WinGC, continuation_color); 412 if (foreground) XSetForeground (disp, WinGC, continuation_color);
420 XmbDrawString (disp, root, fontset, WinGC, x, y, continuation, continuation_length); 413 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x, y, continuation, continuation_length);
421 } 414 }
422 } 415 }
423} 416}
424 417
425/* Just redraw everything without clearing (i.e. after an EXPOSE event) */ 418/* Just redraw everything without clearing (i.e. after an EXPOSE event) */
426void 419void
427refresh (int miny, int maxy, int clear, int refresh_all) 420refresh (int miny, int maxy, int clear, int refresh_all)
428{ 421{
429 int lin = 0; 422 int lin = 0;
423 int space = height;
430 int offset; 424 int offset;
431 unsigned long black_color = GetColor ("black"); 425 unsigned long black_color = GetColor ("black");
432 struct line_node *line; 426 struct line_node *line;
433 int step_per_line; 427 int step_per_line;
434 int foreground = 0; 428 int foreground = 0;
435 429
436 if (opt_reverse) 430 if (opt_reverse)
437 {
438 step_per_line = font_height;
439 offset = font_ascent + effect_y_offset; 431 offset = effect_y_offset;
440 }
441 else 432 else
442 { 433 offset = height + effect_y_offset;
443 step_per_line = -font_height;
444 offset = (listlen - 1) * font_height + font_ascent + effect_y_offset;
445 }
446 434
447 miny -= win_y + font_height; 435 miny -= win_y;
448 maxy -= win_y - font_height; 436 maxy -= win_y;
449 437
450 if (clear && !opt_noflicker) 438 if (clear && !opt_noflicker)
451 XClearArea (disp, root, win_x, win_y, width, height, False); 439 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False);
452 440
453 for (line = linelist; line && lin < listlen; line = line->next, lin++, offset += step_per_line) 441 for (line = linelist; line; line = line->next, lin++)
454 { 442 {
455 struct displaymatrix *display_line = display + lin; 443 struct displaymatrix *display_line;
456 444
445 if (opt_noflicker && lin >= listlen)
446 {
447 int i = listlen;
448 listlen *= 1.5;
449 display = xrealloc(display, listlen * sizeof(struct displaymatrix));
450 for (; i < listlen; i++)
451 {
452 display[i].line = xstrdup ("");
453 display[i].len = 0;
454 display[i].offset = 0;
455 display[i].buffer_size = 0;
456 }
457 }
458
459 display_line = display + lin;
460
461 step_per_line = line->logfile->font_height + effect_y_space;
462 if (step_per_line > space)
463 break;
464
465 if (!opt_reverse)
466 offset -= step_per_line;
467
468 offset += line->logfile->font_ascent;
469
470 miny -= line->logfile->font_height;
471 maxy += line->logfile->font_height;
472
457 if (offset < miny || offset > maxy) 473 if (offset >= miny && offset <= maxy)
458 continue; 474 {
459
460 /* if this line is a different than it was, then it 475 /* if this line is a different than it was, then it
461 * needs displaying */ 476 * needs displaying */
462 if (!opt_noflicker 477 if (!opt_noflicker
463 || refresh_all 478 || refresh_all
464 || display_line->len != line->len 479 || display_line->len != line->len
465 || display_line->color != line->logfile->color 480 || display_line->color != line->logfile->color
481 || display_line->offset != offset
466 || memcmp (display_line->line, line->line, line->len)) 482 || memcmp (display_line->line, line->line, line->len))
467 { 483 {
468 /* don't bother updating the record of what has been 484 /* don't bother updating the record of what has been
469 * displayed if -noflicker isn't in effect, since we redraw 485 * displayed if -noflicker isn't in effect, since we redraw
470 * the whole display every time anyway */ 486 * the whole display every time anyway */
471 if (opt_noflicker) 487 if (opt_noflicker)
472 {
473 /* update the record of what has been displayed;
474 * first make sure the buffer is big enough */
475 if (display_line->buffer_size < line->len)
476 { 488 {
489 /* update the record of what has been displayed;
490 * first make sure the buffer is big enough */
491 if (display_line->buffer_size < line->len)
492 {
477 display_line->buffer_size = line->len; 493 display_line->buffer_size = line->len;
478 display_line->line = xrealloc (display_line->line, display_line->buffer_size); 494 display_line->line = xrealloc (display_line->line, display_line->buffer_size);
495 }
496
497 display_line->len = line->len;
498 display_line->color = line->logfile->color;
499 display_line->offset = offset;
500 memcpy (display_line->line, line->line, line->len);
501
502 if (clear)
503 {
504#ifdef DEBUG
505 static int toggle;
506 toggle = 1 - toggle;
507 XSetForeground (disp, WinGC, toggle ? GetColor ("cyan") : GetColor ("yellow"));
508 XFillRectangle (disp, root, WinGC, win_x, win_y + offset - line->logfile->font_ascent,
509 width, step_per_line);
510#else /* DEBUG */
511 XClearArea (disp, root, win_x, win_y + offset - line->logfile->font_ascent,
512 width + MARGIN_OF_ERROR, step_per_line, False);
513#endif /* DEBUG */
514 }
479 } 515 }
480 516
481 display_line->len = line->len;
482 display_line->color = line->logfile->color;
483 memcpy (display_line->line, line->line, line->len);
484
485 if (clear)
486 XClearArea (disp, root, win_x, win_y + offset - font_ascent,
487 width + effect_x_space, font_height + effect_y_space, False);
488 }
489
490 if (opt_outline) 517 if (opt_outline)
491 { 518 {
492 int x, y; 519 int x, y;
493 XSetForeground (disp, WinGC, black_color); 520 XSetForeground (disp, WinGC, black_color);
494 521
495 for (x = -1; x <= 1; x += 2) 522 for (x = -1; x <= 1; x += 2)
496 for (y = -1; y <= 1; y += 2) 523 for (y = -1; y <= 1; y += 2)
497 draw_text (disp, root, fontset, WinGC, 524 draw_text (disp, root, WinGC,
498 win_x + effect_x_offset + x, 525 win_x + effect_x_offset + x,
499 win_y + y + offset, line, foreground = 0); 526 win_y + y + offset, line, foreground = 0);
500 } 527 }
501 else if (opt_shade) 528 else if (opt_shade)
502 { 529 {
503 XSetForeground (disp, WinGC, black_color); 530 XSetForeground (disp, WinGC, black_color);
504 draw_text (disp, root, fontset, WinGC, 531 draw_text (disp, root, WinGC,
505 win_x + effect_x_offset + SHADE_X, 532 win_x + effect_x_offset + SHADE_X,
506 win_y + offset + SHADE_Y, line, foreground = 0); 533 win_y + offset + SHADE_Y, line, foreground = 0);
507 } 534 }
508 535
509 XSetForeground (disp, WinGC, line->logfile->color); 536 XSetForeground (disp, WinGC, line->logfile->color);
510 draw_text (disp, root, fontset, WinGC, 537 draw_text (disp, root, WinGC,
511 win_x + effect_x_offset, 538 win_x + effect_x_offset,
512 win_y + offset, line, foreground = 1); 539 win_y + offset, line, foreground = 1);
513 } 540 }
541 }
542
543 if (opt_reverse)
544 offset += step_per_line;
545 offset -= line->logfile->font_ascent;
546
547 miny += line->logfile->font_height;
548 maxy -= line->logfile->font_height;
549
550 space -= step_per_line;
551 }
552
553 if (space > 0 && clear)
514 } 554 {
555#ifdef DEBUG
556 XSetForeground (disp, WinGC, GetColor ("orange"));
557 XFillRectangle (disp, root, WinGC, win_x, win_y + offset - (opt_reverse ? 0 : space),
558 width, space);
559#else /* DEBUG */
560 XClearArea (disp, root, win_x, win_y + offset - (opt_reverse ? 0 : space),
561 width + MARGIN_OF_ERROR, space, False);
562#endif
563 }
564
565 /* at least one of the lines must fit in the allocated area. we've
566 * already checked at initialisation time that all the fonts are small
567 * enough to fit at least one line in the display area, but assert it
568 * again here to be sure */
569 assert(line != linelist);
515 570
516 /* any lines that didn't just get looked at are never going to be, so break the chain */ 571 /* any lines that didn't just get looked at are never going to be, so break the chain */
517 if (line) line->prev->next = 0; 572 if (line) line->prev->next = 0;
518 573
519 /* and throw them all away */ 574 /* and throw them all away */
529 } 584 }
530 585
531 if (opt_frame) 586 if (opt_frame)
532 { 587 {
533 XSetForeground (disp, WinGC, GetColor (def_color)); 588 XSetForeground (disp, WinGC, GetColor (def_color));
589 /* note that XDrawRectangle() draws a rectangle one pixel bigger
590 * in both dimensions than you ask for, hence the subtractions.
591 * XFillRectangle() doesn't suffer from this problem */
534 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1); 592 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
535 } 593 }
536} 594}
537 595
538#if HAS_REGEX 596#if HAS_REGEX
565 623
566 printf ("regexp was matched by '%s' - replace with '%s'\n", s, transform_to); 624 printf ("regexp was matched by '%s' - replace with '%s'\n", s, transform_to);
567 printf ("match is from %d to %d\n", match_start, match_end); 625 printf ("match is from %d to %d\n", match_start, match_end);
568 if (new_len > old_len) 626 if (new_len > old_len)
569 s = xrealloc (s, old_whole_len + new_len - old_len); 627 s = xrealloc (s, old_whole_len + new_len - old_len);
570 628
571 if (new_len != old_len) 629 if (new_len != old_len)
572 { 630 {
573 memcpy (s + match_end + new_len - old_len, 631 memcpy (s + match_end + new_len - old_len,
574 s + match_end, 632 s + match_end,
575 old_whole_len - match_end); 633 old_whole_len - match_end);
592 * otherwise allocates a new string and copies p2 to it 650 * otherwise allocates a new string and copies p2 to it
593 */ 651 */
594char * 652char *
595concat_line (char *p1, const char *p2) 653concat_line (char *p1, const char *p2)
596{ 654{
597 assert(p2);
598
599 int l1 = p1 ? strlen (p1) : 0; 655 int l1 = p1 ? strlen (p1) : 0;
600 int l2 = strlen (p2); 656 int l2 = strlen (p2);
601 char *r; 657 char *r;
658
659 assert (p2);
602 660
603 if (p1) 661 if (p1)
604 r = xrealloc(p1, l1 + l2 + 1); 662 r = xrealloc(p1, l1 + l2 + 1);
605 else 663 else
606 r = xmalloc (l2 + 1); 664 r = xmalloc (l2 + 1);
666 logfile->lastpartial = logfile->partial; 724 logfile->lastpartial = logfile->partial;
667 /* there are 3 ways we could have exited the loop: reading '\n', 725 /* there are 3 ways we could have exited the loop: reading '\n',
668 * reaching EOF, or filling the buffer; the 2nd and 3rd of these 726 * reaching EOF, or filling the buffer; the 2nd and 3rd of these
669 * both result in a partial line */ 727 * both result in a partial line */
670 logfile->partial = ch != '\n'; 728 logfile->partial = ch != '\n';
671 729
672 if (logfile->partial && opt_whole) 730 if (logfile->partial && opt_whole)
673 return 0; 731 return 0;
674 732
675#if HAS_REGEX 733#if HAS_REGEX
676 transform_line (logfile->buf); 734 transform_line (logfile->buf);
704 else 762 else
705 file->inode = stats.st_ino; 763 file->inode = stats.st_ino;
706 764
707 if (opt_noinitial) 765 if (opt_noinitial)
708 fseek (file->fp, 0, SEEK_END); 766 fseek (file->fp, 0, SEEK_END);
709 else if (stats.st_size > (listlen + 1) * width) 767 else /* if (stats.st_size > (listlen + 1) * width)
710 /* HACK - 'width' is in pixels - how are we to know how much text will fit? */ 768 * HACK - 'width' is in pixels - how are we to know how much text will fit?
711 fseek (file->fp, -((listlen + 2) * width/10), SEEK_END); 769 * fseek (file->fp, -((listlen + 2) * width/10), SEEK_END); */
770 fseek (file->fp, -5000, SEEK_END);
712 771
713 file->last_size = stats.st_size; 772 file->last_size = stats.st_size;
714 return file->fp; 773 return file->fp;
715} 774}
716 775
841 900
842 /* only calculate the continuation's width once */ 901 /* only calculate the continuation's width once */
843 if (continuation_width == -1) 902 if (continuation_width == -1)
844 { 903 {
845 continuation_length = strlen (continuation); 904 continuation_length = strlen (continuation);
846 continuation_width = XmbTextEscapement (fontset, continuation, continuation_length); 905 continuation_width = XmbTextEscapement (log->fontset, continuation, continuation_length);
847 continuation_color = GetColor (cont_color); 906 continuation_color = GetColor (cont_color);
848 907
849 /* make an array to store information about the location of 908 /* make an array to store information about the location of
850 * spaces in the line */ 909 * spaces in the line */
851 if (opt_justify) 910 if (opt_justify)
861 int start_w = log->last->wrapped_left ? continuation_width : 0; 920 int start_w = log->last->wrapped_left ? continuation_width : 0;
862 int w = start_w; 921 int w = start_w;
863 int wrapped = 0; 922 int wrapped = 0;
864 char *break_p = NULL; 923 char *break_p = NULL;
865 int width_at_break_p = 0; 924 int width_at_break_p = 0;
925 int prefix_len;
926
866 spaces = 0; 927 spaces = 0;
867 928
868 if (opt_justify) 929 if (opt_justify)
869 breaks[spaces].index = breaks[spaces].width = 0; 930 breaks[spaces].index = breaks[spaces].width = 0;
870 931
876 len = mblen (p, l); 937 len = mblen (p, l);
877 if (len <= 0) 938 if (len <= 0)
878 len = 1; /* ignore (don't skip) illegal character sequences */ 939 len = 1; /* ignore (don't skip) illegal character sequences */
879 940
880 /* find the width in pixels of the next character */ 941 /* find the width in pixels of the next character */
881 cw = XmbTextEscapement (fontset, p, len); 942 cw = XmbTextEscapement (log->fontset, p, len);
882
883 if (opt_wordwrap && len == 1 && p[0] == ' ' && p != break_p + 1) 943 if (opt_wordwrap && len == 1 && p[0] == ' ' && p != break_p + 1)
884 { 944 {
885 break_p = p; 945 break_p = p;
886 width_at_break_p = w; 946 width_at_break_p = w;
887 spaces++; 947 spaces++;
928 /* if we're wrapping at spaces, and the line is long enough to 988 /* if we're wrapping at spaces, and the line is long enough to
929 * wrap, and we've seen a space already, and the space wasn't 989 * wrap, and we've seen a space already, and the space wasn't
930 * the first character on the line, then wrap at the space */ 990 * the first character on the line, then wrap at the space */
931 if (!wrapped) 991 if (!wrapped)
932 break; 992 break;
933
934 int prefix_len;
935 993
936 /* choose where to break the line */ 994 /* choose where to break the line */
937 if (opt_wordwrap && break_p && break_p != beg) 995 if (opt_wordwrap && break_p && break_p != beg)
938 { 996 {
939 prefix_len = break_p - beg; 997 prefix_len = break_p - beg;
954 breaks[spaces].len--; 1012 breaks[spaces].len--;
955 } 1013 }
956 } 1014 }
957 else 1015 else
958 prefix_len = p - beg; 1016 prefix_len = p - beg;
959 1017
960 /* make a copy of the tail end of the string */ 1018 /* make a copy of the tail end of the string */
961 p = xstrdup (p); 1019 p = xstrdup (p);
962 1020
963 /* and reduce the size of the head of the string */ 1021 /* and reduce the size of the head of the string */
964 log->last->line = xrealloc (log->last->line, prefix_len + 1); 1022 log->last->line = xrealloc (log->last->line, prefix_len + 1);
969 log->last->wrapped_right = 1; 1027 log->last->wrapped_right = 1;
970 1028
971 /* 'spaces' includes any space we broke on; we can only justify 1029 /* 'spaces' includes any space we broke on; we can only justify
972 * if there's at least one other space */ 1030 * if there's at least one other space */
973 if (opt_justify && spaces && 1031 if (opt_justify && spaces &&
974 width - effect_x_space - width_at_break_p < spaces * font_height) 1032 width - effect_x_space - width_at_break_p < spaces * log->font_height)
975 { 1033 {
976 int i; 1034 int i;
977 log->last->free_pixels = width - w; 1035 log->last->free_pixels = width - effect_x_space - w;
978 log->last->num_words = spaces + 1; 1036 log->last->num_words = spaces + 1;
979 log->last->breaks = malloc (log->last->num_words * sizeof (struct breakinfo)); 1037 log->last->breaks = malloc (log->last->num_words * sizeof (struct breakinfo));
980 for (i = 0; i < log->last->num_words; i++) 1038 for (i = 0; i < log->last->num_words; i++)
981 log->last->breaks[i] = breaks[i]; 1039 log->last->breaks[i] = breaks[i];
982 } 1040 }
983 1041
984 line = new_line_node (log); 1042 line = new_line_node (log);
985 line->line = p; 1043 line->line = p;
986 l = line->len = strlen (p); 1044 l = line->len = strlen (p);
987 1045
988 /* note that the tail end of the string is wrapped at its left */ 1046 /* note that the tail end of the string is wrapped at its left */
1020 1078
1021 new = concat_line (old, str); 1079 new = concat_line (old, str);
1022 free (str); 1080 free (str);
1023 log->last->line = new; 1081 log->last->line = new;
1024 log->last->len = strlen (new); 1082 log->last->len = strlen (new);
1025 possibly_split_long_line (log); 1083 possibly_split_long_line (log);
1026} 1084}
1027 1085
1028static void 1086static void
1029main_loop (void) 1087main_loop (void)
1030{ 1088{
1059 linelist_tail->next = e; 1117 linelist_tail->next = e;
1060 linelist_tail = e; 1118 linelist_tail = e;
1061 1119
1062 display[lin].line = xstrdup (""); 1120 display[lin].line = xstrdup ("");
1063 display[lin].len = 0; 1121 display[lin].len = 0;
1122 display[lin].offset = 0;
1064 display[lin].buffer_size = 0; 1123 display[lin].buffer_size = 0;
1065 } 1124 }
1066 1125
1067 for (;;) 1126 for (;;)
1068 { 1127 {
1082 * output was partial, and that partial line is not 1141 * output was partial, and that partial line is not
1083 * too close to the top of the screen, then update 1142 * too close to the top of the screen, then update
1084 * that partial line */ 1143 * that partial line */
1085 if (opt_update && current->lastpartial && current->last) 1144 if (opt_update && current->lastpartial && current->last)
1086 { 1145 {
1087 // int idx = current->index;
1088 append_to_existing_line (current->buf, current); 1146 append_to_existing_line (current->buf, current);
1089 // current->index = idx;
1090 current->buf = 0; 1147 current->buf = 0;
1091 continue; 1148 continue;
1092 } 1149 }
1093 1150
1094 /* if all we just read was a newline ending a line that we've already displayed, skip it */ 1151 /* if all we just read was a newline ending a line that we've already displayed, skip it */
1095 if (current->buf[0] == '\0' && current->lastpartial) 1152 if (current->buf[0] == '\0' && current->lastpartial)
1096 { 1153 {
1154 free(current->buf);
1097 current->buf = 0; 1155 current->buf = 0;
1098 continue; 1156 continue;
1099 } 1157 }
1100 1158
1101 /* print filename if any, and if last line was from 1159 /* print filename if any, and if last line was from
1102 * different file */ 1160 * different file */
1103 if (!opt_nofilename && lastprinted != current && current->desc[0]) 1161 if (lastprinted != current)
1104 { 1162 {
1105 current->last = 0; 1163 current->last = 0;
1164 if (!opt_nofilename && current->desc[0])
1165 {
1106 insert_new_line (xstrdup ("["), current); 1166 insert_new_line (xstrdup ("["), current);
1107 append_to_existing_line (xstrdup (current->desc), current); 1167 append_to_existing_line (xstrdup (current->desc), current);
1108 append_to_existing_line (xstrdup ("]"), current); 1168 append_to_existing_line (xstrdup ("]"), current);
1109 } 1169 }
1170 }
1110 1171
1111 /* if we're dealing with partial lines, and the last 1172 /* if we're dealing with partial lines, and the last
1112 * time we showed the line it wasn't finished ... */ 1173 * time we showed the line it wasn't finished ... */
1113 if (!opt_whole && current->lastpartial) 1174 if (!opt_whole && current->lastpartial)
1114 { 1175 {
1128 else 1189 else
1129 /* otherwise just make a plain and simple new line */ 1190 /* otherwise just make a plain and simple new line */
1130 insert_new_line (current->buf, current); 1191 insert_new_line (current->buf, current);
1131 1192
1132 current->buf = 0; 1193 current->buf = 0;
1133 // current->index = listlen - 1;
1134 lastprinted = current; 1194 lastprinted = current;
1135 } 1195 }
1136 } 1196 }
1137 1197
1138 if (need_update) 1198 if (need_update)
1222#if HAS_REGEX 1282#if HAS_REGEX
1223 char *transform = NULL; 1283 char *transform = NULL;
1224#endif 1284#endif
1225 1285
1226 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */ 1286 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
1227
1228 /* window needs to be initialized before colorlookups can be done */
1229 /* just a dummy to get the color lookups right */
1230 geom_mask = NoValue;
1231 InitWindow ();
1232 1287
1233 for (i = 1; i < argc; i++) 1288 for (i = 1; i < argc; i++)
1234 { 1289 {
1235 const char *arg = argv[i]; 1290 const char *arg = argv[i];
1236 1291
1272 } 1327 }
1273 else if (!strcmp (arg, "-shade")) 1328 else if (!strcmp (arg, "-shade"))
1274 opt_shade = 1; 1329 opt_shade = 1;
1275 else if (!strcmp (arg, "-outline")) 1330 else if (!strcmp (arg, "-outline"))
1276 opt_outline = 1; 1331 opt_outline = 1;
1332 else if (!strcmp (arg, "-minspace"))
1333 opt_minspace = 1;
1277 else if (!strcmp (arg, "-noflicker")) 1334 else if (!strcmp (arg, "-noflicker"))
1278 opt_noflicker = 1; 1335 opt_noflicker = 1;
1279 else if (!strcmp (arg, "-frame")) 1336 else if (!strcmp (arg, "-frame"))
1280 opt_frame = 1; 1337 opt_frame = 1;
1281 else if (!strcmp (arg, "-no-filename")) 1338 else if (!strcmp (arg, "-no-filename"))
1295 else if (!strcmp (arg, "-color")) 1352 else if (!strcmp (arg, "-color"))
1296 def_color = argv[++i]; 1353 def_color = argv[++i];
1297 else if (!strcmp (arg, "-noinitial")) 1354 else if (!strcmp (arg, "-noinitial"))
1298 opt_noinitial = 1; 1355 opt_noinitial = 1;
1299 else if (!strcmp (arg, "-id")) 1356 else if (!strcmp (arg, "-id"))
1300 root = atoi (argv[++i]); 1357 {
1358 unsigned long id;
1359
1360 if (sscanf (argv[++i], "%li", &id) == 1 && id)
1361 root = id;
1362 }
1301 else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i")) 1363 else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i"))
1302 { 1364 {
1303 double iv = atof (argv[++i]); 1365 double iv = atof (argv[++i]);
1304 1366
1305 interval.tv_sec = (int) iv; 1367 interval.tv_sec = (int) iv;
1335 } 1397 }
1336 1398
1337 e = xmalloc (sizeof (struct logfile_entry)); 1399 e = xmalloc (sizeof (struct logfile_entry));
1338 e->partial = 0; 1400 e->partial = 0;
1339 e->buf = 0; 1401 e->buf = 0;
1340 // e->index = -1;
1341 1402
1342 if (arg[0] == '-' && arg[1] == '\0') 1403 if (arg[0] == '-' && arg[1] == '\0')
1343 { 1404 {
1344 if ((e->fp = fdopen (0, "r")) == NULL) 1405 if ((e->fp = fdopen (0, "r")) == NULL)
1345 perror ("fdopen"), exit (1); 1406 perror ("fdopen"), exit (1);
1346 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0) 1407 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
1347 perror ("fcntl"), exit (1); 1408 perror ("fcntl"), exit (1);
1348 1409
1349 e->fname = NULL; 1410 e->fname = NULL;
1350 e->inode = 0; 1411 e->inode = 0;
1412 if (desc == arg)
1351 e->desc = xstrdup ("stdin"); 1413 e->desc = xstrdup ("stdin");
1414 else
1415 e->desc = xstrdup (desc);
1352 } 1416 }
1353 else 1417 else
1354 { 1418 {
1355 e->fname = xstrdup (fname); 1419 e->fname = xstrdup (fname);
1356 1420
1358 perror (fname), exit (1); 1422 perror (fname), exit (1);
1359 1423
1360 e->desc = xstrdup (desc); 1424 e->desc = xstrdup (desc);
1361 } 1425 }
1362 1426
1363 e->color = GetColor (fcolor); 1427 e->colorname = fcolor;
1364 e->partial = 0; 1428 e->partial = 0;
1429 e->fontname = fontname;
1365 e->last = NULL; 1430 e->last = NULL;
1366 e->next = NULL; 1431 e->next = NULL;
1367 1432
1368 if (!loglist) 1433 if (!loglist)
1369 loglist = e; 1434 loglist = e;
1428 else 1493 else
1429 printf ("compiled '%s' OK to %x\n", transform, (int)transformre); 1494 printf ("compiled '%s' OK to %x\n", transform, (int)transformre);
1430 } 1495 }
1431#endif 1496#endif
1432 1497
1498 if (opt_outline && !opt_minspace)
1499 {
1500 /* adding outline increases the total width and height by 2
1501 pixels each, and offsets the text one pixel right and one
1502 pixel down */
1503 effect_x_space = effect_y_space = 2;
1504 effect_x_offset = effect_y_offset = 1;
1505 }
1506 else if (opt_shade && !opt_minspace)
1507 {
1508 /* adding a shadow increases the space used */
1509 effect_x_space = abs (SHADE_X);
1510 effect_y_space = abs (SHADE_Y);
1511
1512 /* if the shadow is to the right and below then we don't need
1513 * to move the text to make space for it, but shadows to the left
1514 * and above need accomodating */
1515 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
1516 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
1517 }
1518 else
1519 {
1520 effect_x_space = effect_y_space = 0;
1521 effect_x_offset = effect_y_offset = 0;
1522 }
1523
1433 InitWindow (); 1524 InitWindow ();
1434 1525
1435 install_signal (SIGINT, blank_window); 1526 install_signal (SIGINT, blank_window);
1436 install_signal (SIGQUIT, blank_window); 1527 install_signal (SIGQUIT, blank_window);
1437 install_signal (SIGTERM, blank_window); 1528 install_signal (SIGTERM, blank_window);
1503} 1594}
1504 1595
1505void 1596void
1506display_help (char *myname) 1597display_help (char *myname)
1507{ 1598{
1508 printf ("Usage: %s [options] file1[,color[,desc]] " 1599 printf ("Usage: %s [options] file1[,color[,desc]]"
1509 "[file2[,color[,desc]] ...]\n", myname); 1600 "[options] [file2[,color[,desc]] ...]\n", myname);
1510 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n" 1601 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1511 " -color color use color $color as default\n" 1602 " -color color use color $color as default\n"
1512 " -reload sec command reload after $sec and run command\n" 1603 " -reload sec command reload after $sec and run command\n"
1513 " -id id window id to use instead of the root window\n" 1604 " -id id window id to use instead of the root window\n"
1514 " -font FONTSPEC (-fn) font to use\n" 1605 " -font FONTSPEC (-fn) font to use\n"
1516 " -reverse print new lines at the top\n" 1607 " -reverse print new lines at the top\n"
1517 " -whole wait for \\n before showing a line\n" 1608 " -whole wait for \\n before showing a line\n"
1518 " -partial show lines even if they don't end with a \\n\n" 1609 " -partial show lines even if they don't end with a \\n\n"
1519 " -update allow updates to old partial lines\n" 1610 " -update allow updates to old partial lines\n"
1520 " -cont string to prefix continued partial lines with\n" 1611 " -cont string to prefix continued partial lines with\n"
1612 " defaults to \"|| \"\n"
1521 " -wordwrap wrap long lines at spaces to avoid breaking words\n" 1613 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1522 " defaults to \"[+]\"\n"
1523 " -shade add shading to font\n" 1614 " -shade add shading to font\n"
1615 " -outline add black outline to font\n"
1616 " -minspace force minimum line spacing\n"
1524 " -noinitial don't display the last file lines on\n" 1617 " -noinitial don't display the last file lines on\n"
1525 " startup\n" 1618 " startup\n"
1526 " -i | -interval seconds interval between checks (fractional\n" 1619 " -i | -interval seconds interval between checks (fractional\n"
1527 " values o.k.). Default 2.4 seconds\n" 1620 " values o.k.). Default 2.4 seconds\n"
1528 " -V display version information and exit\n" 1621 " -V display version information and exit\n"
1558 if (setsid () == -1) 1651 if (setsid () == -1)
1559 return -1; 1652 return -1;
1560 1653
1561 return 0; 1654 return 0;
1562} 1655}
1563
1564/* todo - get reverse display working again */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines