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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
/* vim:set ts=8 sw=4 sts=4 et:
*
* main.c * xrootconsole
* Original version: Copyright (C) 1998, 1999 Eric Youngblut
* Current version: Copyright (C) 1999 Eric Youngblut & Bob Galloway
*
* (The transparency stuff was inspired by -- pulled from the still-warm
* body of -- wterm. Many thanks to wterm's "anonymous coder!")
*
* $Id: main.c,v 1.13 2004/02/20 22:31:53 bob Exp $
*
*
* ANSI colors: Copyright (C) 2009, 2010 Julien Viard de Galbert
* | ref: http://en.wikipedia.org/wiki/ANSI_escape_code
* |
* | TODO: allow customizing color table from cmd line ?
* |
* | usage: tail -f /var/log/messages | stdbuf -oL ccze -A | xrootconsole --ansi-color
*
*
*
**************************************************************************
* 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 the original version of this software can be reached
* by e-mail at yngblut@cs.washington.edu. The original version of
* this software can be found at http://www.cs.washington.edu/homes/yngblut/
*
* The author of the current version of this software can be reached
* by e-mail at bgallowa@wso.williams.edu. The latest version of this
* software can be found at http://de-fac.to/bob/xrootconsole/
**************************************************************************/
#include "util.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <X11/Xlocale.h>
#include <time.h>
#ifndef XROOTCONSOLE_VERSION
#define XROOTCONSOLE_VERSION "undefined"
#endif
#define USAGE \
"Usage: xrootconsole [options] [console]\n" \
"Scroll the console to a transparent window placed on the X root window.\n" \
"\n" \
" -geometry GEO the geometry of the window (default 80x10+0+0)\n" \
" -fn FONT the font to use (default fixed)\n" \
" -fg COLOR foreground color of the text (default white)\n" \
" -bg COLOR background AND-mask for shaded transparency (default clear)\n" \
" -bd COLOR border color (default white)\n" \
" -bw WIDTH border width (default 0)\n" \
" -c COLUMNS split window into number of text columns (default 1)\n" \
" --solid make background a solid color, not shaded-transparent\n" \
" --topdown insert lines at the top and scroll the rest down\n" \
" --bottomup start inserting lines at the bottom\n" \
" --wrap wrap long lines, instead of cutting them off\n" \
" --ansi-color enable ANSI escape sequence parsing for color support\n"\
" -h or --help a familiar-looking help screen\n" \
" -v or --version displays the version number\n" \
" [console] filename to read (defaults to standard input)\n" \
"\n" \
"Report bugs to <bob@de-fac.to>.\n"
#define ESC_SEQ_START 0x10000
#define ESC_SEQ_CONT 0x20000
#define ESC_SEQ_DATA_MASK 0xFFFF
#define REND_DEFAULT 0
#define REND_FG_COLOR_MASK 0x0007
#define REND_FG_SET_COLOR 0x0008
#define REND_FG_FULLCOLOR_MASK (REND_FG_COLOR_MASK | REND_FG_SET_COLOR)
#define REND_FG_COLOR(c) (REND_FG_SET_COLOR + ((c) & REND_FG_COLOR_MASK))
#define REND_FG_COLOR_GET(r) (r & REND_FG_COLOR_MASK)
#define REND_BG_COLOR_MASK 0x0070
#define REND_BG_SET_COLOR 0x0080
#define REND_BG_FULLCOLOR_MASK (REND_BG_COLOR_MASK | REND_BG_SET_COLOR)
#define REND_BG_COLOR(c) (REND_BG_SET_COLOR + (((c)<<4) & REND_BG_COLOR_MASK))
#define REND_BG_COLOR_GET(r) ((r & REND_BG_COLOR_MASK)>>4)
#define REND_BOLD 0x0100 /* used as fg high intensity */
#define REND_BLINK 0x0200 /* used as bg high intensity */
#define REND_ULINE 0x0400
#define REND_RVIDEO 0x0800
static const char *def_colorName[] =
{
/* low-intensity colors */
"Black", /* 0: black (#000000) */
"Red3", /* 1: red (#CD0000) */
"Green3", /* 2: green (#00CD00) */
// "Yellow3", /* 3: yellow (#CDCD00) */
"#AB5700", /* 3: brown actually (EGA/DOS ANSY.SYS compatible) */
"Blue3", /* 4: blue (#0000CD) */
"Magenta3", /* 5: magenta (#CD00CD) */
"Cyan3", /* 6: cyan (#00CDCD) */
"AntiqueWhite", /* 7: white (#FAEBD7) */
/* high-intensity colors */
"Grey25", /* 8: bright black (#404040) */
"Red", /* 9: bright red (#FF0000) */
"Green", /* 10: bright green (#00FF00) */
"Yellow", /* 11: bright yellow (#FFFF00) */
"Blue", /* 12: bright blue (#0000FF) */
"Magenta", /* 13: bright magenta (#FF00FF) */
"Cyan", /* 14: bright cyan (#00FFFF) */
"White", /* 15: bright white (#FFFFFF) */
};
#define DEFAULT_X 0
#define DEFAULT_Y 0
#define DEFAULT_COLS 80
#define DEFAULT_ROWS 10
#define DEFAULT_FONT_NAME "fixed"
#define DEFAULT_FOREGROUND_COLOR "white"
#define DEFAULT_BACKGROUND_COLOR "white"
#define DEFAULT_BORDER_COLOR "white"
#define DEFAULT_BORDER_WIDTH 0
#define DEFAULT_TEXT_COLUMNS 1
#define DEFAULT_WRAP False
#define DEFAULT_TOPDOWN False
#define DEFAULT_SOLID False
#define DEFAULT_ANSI False
typedef struct {
const char* fg;
const char* bg;
const char* bd;
const char* geometry;
const char* console_name;
const char* font_name;
int bw;
int tc; /* number of text columns (NOT characters) */
int wrap;
int topdown;
int solid;
int ansi;
} InitOptions;
typedef struct {
int x_root; /* position of the window (in pixels) */
int y_root;
int width; /* size of the window (in pixels) */
int height;
unsigned int cols; /* size of the window (in characters) */
unsigned int rows;
int geometry;
Display* dpy;
Window win;
Window root; /* the detected root window */
Pixmap buf; /* the main drawable, copied to win by exposures */
Pixmap bkg; /* the background pixmap, copied to buf before each drawing */
Pixmap root_pix; /* the pixmap of root window fetched from atom */
XFontSet fn; /* text font */
XFontSetExtents *extent;
short font_ascent;
short font_descent;
GC foregc;
GC backgc;
int colwidth; /* width of a column in characters */
int nlines; /* number of lines to keep in memory */
int intercolgap; /* width between two columns */
XSetWindowAttributes swa;
unsigned long colortable[16];
unsigned long fgcolor;
unsigned long bgcolor;
} WindowSettings;
typedef struct {
const char* console; /* name of the console file */
int fd; /* file descriptor of the console file */
char* text; /* circular buffer to store the characters */
short* rend; /* circular buffer to store the rendition options */
int pos; /* where the actual first line in text is */
int escapeseq; /* we are in an escape sequence */
short currend; /* current renditions options */
short tmprend; /* temporary renditions options (to handle ; )*/
} ConsoleSettings;
/* decode SGR */
static short select_graphic_rendition(short rendition, int code)
{
switch(code) {
case 0:
rendition = REND_DEFAULT;
break;
case 1:
rendition |= REND_BOLD;
break;
case 4:
rendition |= REND_ULINE;
break;
case 5:
rendition |= REND_BLINK;
break;
case 7:
rendition |= REND_RVIDEO;
break;
case 22:
rendition &= ~REND_BOLD;
break;
case 24:
rendition &= ~REND_ULINE;
break;
case 25:
rendition &= ~REND_BLINK;
break;
case 27:
rendition &= ~REND_RVIDEO;
break;
case 30:
case 31: /* set fg color */
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
rendition = (rendition & ~REND_FG_FULLCOLOR_MASK)
| REND_FG_COLOR(code-30);
break;
case 39: /* default fg */
rendition = rendition & ~REND_FG_FULLCOLOR_MASK;
break;
case 40:
case 41: /* set bg color */
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
rendition = (rendition & ~REND_BG_FULLCOLOR_MASK)
| REND_BG_COLOR(code-40);
break;
case 49: /* default bg */
rendition = rendition & ~REND_BG_FULLCOLOR_MASK;
break;
}
return rendition;
}
/*
* put
*/
void put(const char* st, int len, InitOptions *io,
WindowSettings *ws, ConsoleSettings *cs) {
static int x = 0, y = 0;
const unsigned char* s = (const unsigned char*) st;
while ( (len==-1)?(*s != '\0'):(len-->0) ) {
/* For escape sequence we assume we got only supported commands
ie COLOR only */
/* really entering : ESC[ ? */
if (ESC_SEQ_START==cs->escapeseq) {
if (*s == '[') {
/* yes, continue with escape seq */
cs->escapeseq = ESC_SEQ_CONT;
/* handle ESC[m -> ESC[0m special case */
cs->tmprend = REND_DEFAULT;
}
else {
/* no fall back to normal string processing */
cs->escapeseq = False;
}
}
/* inside an escape sequence ? */
if (cs->escapeseq) {
switch (*s) {
case '[':
break; /* already treated */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
cs->escapeseq = ESC_SEQ_CONT | ((cs->escapeseq & ESC_SEQ_DATA_MASK) * 10 + *s - '0');
/* at least one char, so not ESC[m, behave like a modif */
cs->tmprend = cs->currend;
break;
case ';':
cs->tmprend = select_graphic_rendition(cs->tmprend, cs->escapeseq & ESC_SEQ_DATA_MASK);
break;
case 'm':
cs->currend = select_graphic_rendition(cs->tmprend, cs->escapeseq & ESC_SEQ_DATA_MASK);
default:
cs->escapeseq = False;
}
}
else {
switch (*s) {
case '\n':
if (io->topdown) {
cs->pos = (cs->pos + ws->nlines - 1) % ws->nlines;
memset(cs->text + cs->pos * ws->colwidth, ' ', ws->colwidth);
if (io->ansi) {
for(x = 0; x < ws->colwidth; x++) {
cs->rend[cs->pos * ws->colwidth + x] = cs->currend;
}
}
}
else if (y == ws->nlines - 1) {
memset(cs->text + cs->pos * ws->colwidth, ' ', ws->colwidth);
if (io->ansi) {
for(x = 0; x < ws->colwidth; x++) {
cs->rend[cs->pos * ws->colwidth + x] = cs->currend;
}
}
cs->pos = (cs->pos + 1) % ws->nlines;
}
else y++;
x = 0;
break;
case '\t':
x = (x / 8 + 1) * 8;
if (x >= ws->cols && io->wrap && *(s+1)!='\n') put("\n",1,io,ws,cs);
break;
case '\033': /* ESC */
/* enter escape sequence start mode */
if (io->ansi) cs->escapeseq = ESC_SEQ_START;
break;
default:
if (*s >= ' ' && x < ws->colwidth) {
cs->text[x + ((y + cs->pos) % ws->nlines) * ws->colwidth] = *s;
if (io->ansi) {
cs->rend[x + ((y + cs->pos) % ws->nlines) * ws->colwidth] = cs->currend;
}
if (++x == ws->colwidth && io->wrap && *(s+1)!='\n') put("\n",1,io,ws,cs);
}
break;
}
}
s++;
}
}
void draw_textline(InitOptions *io, WindowSettings *ws, ConsoleSettings *cs, int x, int y, int offset, int length)
{
XGCValues values;
int len;
short rend = REND_DEFAULT;
while (length > 0) {
rend = cs->rend[offset];
len = 1;
/* search longer segment with same color */
while ((cs->rend[offset+len] == rend) && (len < length)) len ++;
if (rend & REND_FG_SET_COLOR) {
values.foreground = ws->colortable[REND_FG_COLOR_GET(rend) + ((rend & REND_BOLD)?8:0) ];
}
else {
values.foreground = ws->fgcolor;
}
//printf("len:%3d rend: %04x\n",len, rend);
if (rend & (REND_BG_SET_COLOR | REND_RVIDEO) ) {
if (rend & REND_RVIDEO) {
values.background = values.foreground;
if (rend & REND_BG_SET_COLOR) {
values.foreground = ws->colortable[REND_BG_COLOR_GET(rend) + ((rend & REND_BLINK)?8:0) ];
}
else {
if (io->solid == True) {
values.foreground = ws->bgcolor;
}
else {
/* transparent mode use AND so it make bg darker
* just make the letters black, could be too dark,
* but probably more readable */
values.foreground = ws->colortable[0];
}
}
}
else {
values.background = ws->colortable[REND_BG_COLOR_GET(rend) + ((rend & REND_BLINK)?8:0) ];
}
XChangeGC(ws->dpy, ws->foregc, GCForeground | GCBackground, &values);
XmbDrawImageString(ws->dpy, ws->buf, ws->fn, ws->foregc,
x,
y,
cs->text + offset,
len);
}
else {
XChangeGC(ws->dpy, ws->foregc, GCForeground, &values);
XmbDrawString(ws->dpy, ws->buf, ws->fn, ws->foregc,
x,
y,
cs->text + offset,
len);
}
if (rend & REND_BOLD) {
XmbDrawString(ws->dpy, ws->buf, ws->fn, ws->foregc,
x+1,
y,
cs->text + offset,
len);
}
length -= len;
offset += len;
x += len * ws->extent->max_logical_extent.width; /* can be error !!!! */
}
/* restore rendering options */
if(rend != REND_DEFAULT) {
values.foreground = ws->fgcolor;
XChangeGC(ws->dpy, ws->foregc, GCForeground, &values);
}
}
/*
* write the text in the pixmap
*/
void draw_pixmap(InitOptions *io, WindowSettings *ws, ConsoleSettings *cs) {
int rowc = cs->pos + (io->topdown==True); /* row in caracters */
int rowp = ws->font_ascent; /* row in pixels */
int i,j;
/* setup the background */
XCopyArea(ws->dpy, ws->bkg, ws->buf, ws->foregc, 0, 0,
ws->width, ws->height, 0, 0);
/* write the string */
for (i = ws->rows; i>0; --i) {
for (j = 0; j < io->tc; ++j) {
int x, offset;
x=j * (ws->colwidth * ws->extent->max_logical_extent.width + ws->intercolgap);
offset=((rowc + j*(ws->rows)) % ws->nlines)*(ws->colwidth);
if (io->ansi) {
draw_textline(io, ws, cs,
x, rowp, offset, ws->colwidth);
}
else {
XmbDrawString(ws->dpy, ws->buf, ws->fn, ws->foregc,
x, rowp, cs->text + offset, ws->colwidth);
}
}
rowp += ws->font_ascent + ws->font_descent;
++rowc;
}
}
/*
* init
*/
void init_options(char **argv, InitOptions *io) {
/* initialize InitOptions struct*/
io->console_name = NULL;
io->geometry = NULL;
io->fg = DEFAULT_FOREGROUND_COLOR;
io->bg = DEFAULT_BACKGROUND_COLOR;
io->bd = DEFAULT_BORDER_COLOR;
io->bw = DEFAULT_BORDER_WIDTH;
io->tc = DEFAULT_TEXT_COLUMNS;
io->font_name = DEFAULT_FONT_NAME;
io->wrap = DEFAULT_WRAP;
io->topdown = DEFAULT_TOPDOWN;
io->solid = DEFAULT_SOLID;
io->ansi = DEFAULT_ANSI;
/* Process command-line arguments */
while (*++argv != NULL) {
if (!strcmp(*argv, "-geometry") || !strcmp(*argv, "-geo"))
io->geometry = *++argv;
else if (!strcmp(*argv, "-font") || !strcmp(*argv, "-fn"))
io->font_name = *++argv;
else if (!strcmp(*argv, "-foreground") || !strcmp(*argv, "-fg"))
io->fg = *++argv;
else if (!strcmp(*argv, "-background") || !strcmp(*argv, "-bg"))
io->bg = *++argv;
else if (!strcmp(*argv, "-bordercolor") || !strcmp(*argv, "-bd"))
io->bd = *++argv;
else if (!strcmp(*argv, "-borderwidth") || !strcmp(*argv, "-bw")) {
if (*++argv) {
io->bw = atoi(*argv);
} else {
fprintf(stderr, "No parameter to %s given. Exiting.\n", *--argv);
exit(EXIT_FAILURE);
}
}
else if (!strcmp(*argv, "-columns") || !strcmp(*argv, "-c"))
io->tc = atoi(*++argv);
else if (!strcmp(*argv, "--solid"))
io->solid = True;
else if (!strcmp(*argv, "--wrap"))
io->wrap = True;
else if (!strcmp(*argv, "--topdown"))
io->topdown = True;
else if (!strcmp(*argv, "--bottomup"))
io->topdown = -1234;
else if (!strcmp(*argv, "--ansi-color"))
io->ansi = True;
else if (!strcmp(*argv, "--version") || !strcmp(*argv, "-v")) {
fprintf(stderr, "xrootconsole %s\n", XROOTCONSOLE_VERSION);
exit(EXIT_SUCCESS);
}
else if (!strcmp(*argv, "--help") || !strcmp(*argv, "-h")) {
fprintf(stderr, "xrootconsole %s\n%s", XROOTCONSOLE_VERSION,USAGE);
exit(EXIT_SUCCESS);
}
else {
io->console_name = *argv;
}
/* sanity check! */
if (io->tc < 1) io->tc = DEFAULT_TEXT_COLUMNS;
}
}
int open_console(const char* console) {
int fd;
assert(console!=NULL);
fd = open(console, O_RDONLY|O_NONBLOCK);
if (fd < 0) {
fprintf(stderr,"Console %s can't be opened! ",console);
perror("Error");
exit (EXIT_FAILURE);
}
return fd;
}
void reset_console(ConsoleSettings *cs) {
assert(cs->fd >= 0);
assert((cs->fd >= 1) || (cs->console == NULL));
if (cs->console == NULL) exit(EXIT_SUCCESS);
close(cs->fd);
cs->fd = -1;
cs->escapeseq = False;
cs->currend = REND_DEFAULT;
cs->tmprend = REND_DEFAULT;
}
void init_console(const char *console,
int colwidth, int nlines,
int ansi,
ConsoleSettings *cs) {
/* we never modify the name through a ConsoleSettings*,
so don't bother copying the string */
cs->console = console;
/* Create the text array. We can display "nlines-1" # of rows, and the
extra one is for the line being read. */
cs->text = (char*)malloc(colwidth * nlines);
memset(cs->text, ' ', colwidth * nlines);
/* Create rendering info array */
if (ansi) {
cs->rend = (short*)malloc(colwidth * nlines * sizeof(short));
memset(cs->rend, 0, colwidth * nlines * sizeof(short));
}
else {
cs->rend = NULL;
}
cs->pos = 0;
if (cs->console == NULL) cs->fd = 0; /* STDIN special case, */
else cs->fd = open_console(cs->console); /* or open normal file */
cs->escapeseq = False;
cs->currend = REND_DEFAULT;
cs->tmprend = REND_DEFAULT;
}
/* Return the root window, or a virtual root window if any. */
static Window root_window(Display *display) {
Atom __SWM_VROOT = XInternAtom(display, "__SWM_VROOT", False);
Atom NAUTILUS_DESKTOP_WINDOW_ID = XInternAtom (display, "NAUTILUS_DESKTOP_WINDOW_ID", False);
Window root = DefaultRootWindow (display);
/* First check for Nautilus */
if (NAUTILUS_DESKTOP_WINDOW_ID != None) {
Atom type;
int format;
unsigned long nitems, bytes_after_return;
Window *virtual_root_window;
if (XGetWindowProperty (display, root, NAUTILUS_DESKTOP_WINDOW_ID,
0, 1, False, XA_WINDOW, &type, &format,
&nitems, &bytes_after_return,
(unsigned char **) &virtual_root_window) == Success
&& type == XA_WINDOW) {
Window unused, *windows = 0;
unsigned int count;
if (XQueryTree (display, *(Window *)virtual_root_window, &unused, &unused, &windows, &count)) {
XFree (virtual_root_window);
return windows[count - 1];
}
XFree (virtual_root_window);
}
}
if (__SWM_VROOT != None) {
Window unused, *windows;
unsigned int count;
if (XQueryTree(display, DefaultRootWindow (display), &unused, &unused,
&windows, &count)) {
int i;
for (i=0; i<count; i++) {
Atom type;
int format;
unsigned long nitems, bytes_after_return;
Window *virtual_root_window;
if (XGetWindowProperty(display, windows[i], __SWM_VROOT,
0, 1, False, XA_WINDOW, &type, &format,
&nitems, &bytes_after_return,
(unsigned char **) &virtual_root_window) == Success){
if (type != None) {
if (type == XA_WINDOW) {
XFree(windows);
return *virtual_root_window;
} else {
fprintf(stderr,
"__SWM_VROOT property type mismatch");
}
}
} else {
fprintf(stderr,
"failed to get __SWM_VROOT property on window 0x%lx",
windows[i]);
}
}
if (count)
XFree(windows);
} else {
fprintf(stderr, "Can't query tree on root window 0x%lx",
root);
}
} else {
/* This shouldn't happen. The Xlib documentation is wrong BTW. */
fprintf (stderr, "Can't intern atom __SWM_VROOT");
}
return root;
}
int check_update_background(WindowSettings *ws) {
Atom prop_root, type;
int format;
unsigned long length, after;
unsigned char *data_root;
Pixmap p;
prop_root = XInternAtom(ws->dpy, "_XROOTPMAP_ID", True);
if (prop_root == None) return False;
/* check on the previously detected root window */
XGetWindowProperty(ws->dpy, ws->root, prop_root, 0L, 1L, False,
AnyPropertyType, &type, &format, &length, &after, &data_root);
if (type != XA_PIXMAP) {
/* try default root window */
Window root = DefaultRootWindow (ws->dpy);
XGetWindowProperty(ws->dpy, root, prop_root, 0L, 1L, False,
AnyPropertyType, &type, &format, &length, &after, &data_root);
/* update the detected root window, what's important is where the atom
* is for now! */
if (type == XA_PIXMAP && data_root)
ws->root = root;
}
/* none found, exit */
/* maybe ... if it does not work for some users, I should import the
* full searching code from eterm ... at least if eterm works for them ! */
if (type != XA_PIXMAP || !data_root) return False;
p=*((Pixmap *)data_root);
XFree(data_root);
if (p==None) return False;
/* pixmap not changed ? */
if (ws->root_pix == p) return False;
ws->root_pix = p;
/* take a snapshot for the root pixmap */
XCopyArea(ws->dpy, ws->root_pix, ws->bkg, ws->foregc,
ws->x_root, ws->y_root,
ws->width, ws->height, 0, 0);
/* AND-shade the background */
XFillRectangle(ws->dpy, ws->bkg, ws->backgc, 0, 0,
ws->width, ws->height);
/* background pixmap was updated */
return True;
}
void init_window(InitOptions *io, WindowSettings *ws) {
XGCValues values;
memset(ws,0,sizeof(*ws));
ws->x_root = DEFAULT_X;
ws->y_root = DEFAULT_Y;
ws->cols = DEFAULT_COLS;
ws->rows = DEFAULT_ROWS;
ws->intercolgap = 0;
if (io->geometry) {
ws->geometry = XParseGeometry(io->geometry,
&(ws->x_root), &(ws->y_root), &(ws->cols), &(ws->rows));
}
/* Connect to the X server */
ws->dpy = XOpenDisplay(NULL);
if (ws->dpy == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(EXIT_FAILURE);
}
ws->fn = load_font(io->font_name, ws->dpy);
ws->extent = XExtentsOfFontSet(ws->fn);
/* compute text column geometry */
ws->nlines = ws->rows * io->tc + 1;
ws->colwidth = ws->cols / io->tc;
if (io->tc > 1) {
/* ensure at least a 1 char gap between columns */
if ((ws->cols - ws->colwidth*io->tc)/(io->tc-1) < 1) --(ws->colwidth);
ws->intercolgap = (ws->cols - ws->colwidth*io->tc) *
ws->extent->max_logical_extent.width / (io->tc-1);
}
/* Calculate the position of window on the screen */
ws->font_ascent = ws->extent->max_logical_extent.height * 4 / 5;
ws->font_descent = ws->extent->max_logical_extent.height / 5;
ws->width = ws->cols * ws->extent->max_logical_extent.width;
ws->height = ws->rows * (ws->font_ascent + ws->font_descent);
ws->x_root = (ws->geometry & XNegative) ?
(DisplayWidth(ws->dpy, DefaultScreen(ws->dpy)) -
ws->width + ws->x_root - (2*io->bw)) :
ws->x_root;
ws->y_root = (ws->geometry & YNegative) ?
(DisplayHeight(ws->dpy, DefaultScreen(ws->dpy)) -
ws->height + ws->y_root - (2*io->bw)) :
ws->y_root;
ws->swa.background_pixmap = ParentRelative;
ws->swa.border_pixel = load_color(io->bd, ws->dpy);
ws->swa.event_mask = ExposureMask;
ws->swa.override_redirect = True;
ws->root = root_window(ws->dpy);
ws->win = XCreateWindow(ws->dpy, ws->root,
ws->x_root, ws->y_root,
ws->width, ws->height, io->bw, CopyFromParent, CopyFromParent,
DefaultVisual(ws->dpy, DefaultScreen(ws->dpy)),
CWOverrideRedirect | CWBackPixmap | CWEventMask |
CWBorderPixel, &(ws->swa));
XMapWindow(ws->dpy, ws->win);
/* Create the pixmaps */
ws->bkg = XCreatePixmap(ws->dpy, ws->win, ws->width, ws->height,
DefaultDepth(ws->dpy, DefaultScreen(ws->dpy)));
ws->buf = XCreatePixmap(ws->dpy, ws->win, ws->width, ws->height,
DefaultDepth(ws->dpy, DefaultScreen(ws->dpy)));
/* build colortable */
ws->fgcolor = load_color(io->fg, ws->dpy);
ws->bgcolor = load_color(io->bg, ws->dpy);
if (io->ansi) {
int i;
for(i = 0; i < 16; i ++)
{
ws->colortable[i] = load_color(def_colorName[i], ws->dpy);
}
}
/* Create the foreground GC */
values.foreground = ws->fgcolor;
values.graphics_exposures = 0;
ws->foregc = XCreateGC(ws->dpy, ws->win,
GCForeground | GCGraphicsExposures,
&values);
/* initialize the background pixmap */
values.background = values.foreground;
values.foreground = ws->bgcolor;
if (io->solid == True) {
XLowerWindow(ws->dpy, ws->win);
ws->backgc = XCreateGC(ws->dpy, ws->win,
GCForeground | GCBackground, &values);
XFillRectangle(ws->dpy, ws->bkg, ws->backgc, 0, 0,
ws->width, ws->height);
} else { /* shaded/transparent background */
values.function = GXand;
ws->backgc = XCreateGC(ws->dpy, ws->win,
GCForeground | GCBackground | GCFunction,
&values);
/* fill the window with root pixmap */
XClearWindow(ws->dpy,ws->win);
if (!check_update_background(ws)) {
/* The background pixmap atom was not detected on the root window,
take a snapshot for the root pixmap */
XCopyArea(ws->dpy, ws->win, ws->bkg, ws->foregc, 0, 0,
ws->width, ws->height, 0, 0);
/* AND-shade the background */
XFillRectangle(ws->dpy, ws->bkg, ws->backgc, 0, 0,
ws->width, ws->height);
}
/* register to get informed when background pixmap atom changes,
even if we did not detect it... it might apear later */
XSelectInput(ws->dpy, ws->root, PropertyChangeMask);
/* in order to get the full pixmap we have to wait until the pixmap
is copied before lowering the window */
XLowerWindow(ws->dpy, ws->win);
}
/* prevent the server from redrawing the background */
ws->swa.background_pixmap = None;
XChangeWindowAttributes(ws->dpy, ws->win, CWBackPixmap, &(ws->swa));
}
/*
* handle_expose
*/
void handle_expose(XExposeEvent* ev, WindowSettings *ws) {
XCopyArea(ws->dpy, ws->buf, ws->win, ws->foregc,
ev->x, ev->y, ev->width, ev->height,
ev->x, ev->y );
}
/*
* event_loop
*/
void event_loop(InitOptions *io, WindowSettings *ws, ConsoleSettings *cs) {
fd_set rfds;
fd_set efds;
int Xfd = ConnectionNumber(ws->dpy);
int maxfd;
struct timeval select_timeout, *pselect_timeout;
XEvent ev;
time_t now=0, nofile_time=0;
maxfd = (cs->fd > Xfd ? cs->fd : Xfd) + 1;
if (io->topdown == -1234) {
int i;
io->topdown = False;
for (i=0; i < ws->nlines; i++)
put("\n", 1, io, ws, cs);
}
while (1) {
if ((XPending(ws->dpy) > 0) &&
(XCheckMaskEvent(ws->dpy, PropertyChangeMask, &ev) == True)) {
if (check_update_background(ws)) {
/* background changed request full redraw */
draw_pixmap(io,ws,cs);
/* make an exposure event */
XClearArea(ws->dpy, ws->win, 0, 0, 0, 0, True);
}
}
while ((XPending(ws->dpy) > 0) &&
(XCheckMaskEvent(ws->dpy, ExposureMask, &ev) == True)) {
handle_expose((XExposeEvent*)&ev.xexpose, ws);
}
XLowerWindow(ws->dpy, ws->win);
/* has the file moved from under us? re-open if so. */
if (cs->fd < 0) {
cs->fd = open_console(cs->console);
maxfd = (cs->fd > Xfd ? cs->fd : Xfd) + 1;
}
FD_ZERO(&rfds);
FD_ZERO(&efds);
FD_SET(Xfd, &rfds);
if(nofile_time>now) {
select_timeout.tv_sec = nofile_time-now;
select_timeout.tv_usec = 0;
pselect_timeout = &select_timeout;
}
else {
FD_SET(cs->fd, &rfds);
FD_SET(cs->fd, &efds);
pselect_timeout = NULL;
}
if (select(maxfd, &rfds, NULL, &efds, pselect_timeout) == -1) {
perror("select error");
exit(EXIT_FAILURE);
}
time(&now);
if (FD_ISSET(cs->fd, &rfds)) {
char buf[1024];
int n;
n = read(cs->fd, buf, sizeof(buf));
if (n == -1) {
nofile_time = now+1;
if (errno != EAGAIN) {reset_console(cs);}
} else if (n > 0) {
put(buf,n,io,ws,cs);
draw_pixmap(io,ws,cs);
/* make an exposure event */
XClearArea(ws->dpy, ws->win, 0, 0, 0, 0, True);
} else { /* n == 0 --> EOF */
nofile_time = now+1;
}
}
else if (FD_ISSET(cs->fd, &efds)) {
reset_console(cs);
nofile_time = now+1;
}
} /* end loop */
}
/*
* main
*/
int main(int argc, char** argv) {
InitOptions io;
ConsoleSettings cs;
WindowSettings ws;
setlocale(LC_CTYPE, "");
init_options(argv, &io);
init_window(&io,&ws);
init_console(io.console_name, ws.colwidth, ws.nlines, io.ansi, &cs);
/* Display a message */
put("xrootconsole ",-1,&io,&ws,&cs);
put(XROOTCONSOLE_VERSION,-1,&io,&ws,&cs);
put("\n",1,&io,&ws,&cs);
draw_pixmap(&io,&ws,&cs);
event_loop(&io,&ws,&cs);
XCloseDisplay(ws.dpy);
exit(EXIT_SUCCESS);
}
|