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
|
/*
* setroot.c -- set the screen background image of a running twin
*
* Copyright (C) 2000 by Massimiliano Ghilardi
*
* 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.
*
*/
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include "Tw/Tw.h"
#include "Tw/Twerrno.h"
byte *name;
void usage(void) {
fprintf(stderr, "Usage: %s [-padx <X>] [-pady <Y>] [-aa <ascii art file>]\n", name);
}
void panic(void) {
fprintf(stderr, "%s: Out of memory!\n", name);
exit(1);
}
hwattr *load_ascii_art(FILE *aaFILE, uldat *x, uldat *y, uldat padX, uldat padY);
TW_DECL_MAGIC(setroot_magic);
int main(int argc, char *argv[]) {
hwattr *image;
uldat X, Y, padX = 0, padY = 0, err;
enum {def, aa, padx, pady} state = def;
byte *aafile = NULL;
name = *argv;
while (*++argv) {
switch (state) {
case def:
if (!strcmp(*argv, "-aa"))
state = aa;
else if (!strcmp(*argv, "-padx"))
state = padx;
else if (!strcmp(*argv, "-pady"))
state = pady;
break;
case aa:
aafile = *argv;
state = def;
break;
case padx:
padX = atoi(*argv);
state = def;
break;
case pady:
padY = atoi(*argv);
state = def;
break;
default:
usage();
return 0;
}
}
if (state != def) {
usage();
return 0;
}
if (TwCheckMagic(setroot_magic) && TwOpen(NULL)) {
FILE *aaFILE = aafile ? fopen(aafile, "r") : stdin;
if (!aafile) aafile = "(stdin)";
if (aaFILE) {
if ((image = load_ascii_art(aaFILE, &X, &Y, padX, padY))) {
TwBgImageScreen(TwFirstScreen(), X, Y, image);
TwFlush();
TwClose();
return 0;
} else if (X || Y)
fprintf(stderr, "%s: error reading `%s': %s\n", name, aafile, strerror(errno));
else
fprintf(stderr, "%s: `%s' contains no ascii-art image!\n", name, aafile);
} else
fprintf(stderr, "%s: cannot open `%s': %s\n", name, aafile, strerror(errno));
TwClose();
}
if ((err = TwErrno))
fprintf(stderr, "%s: libTw error: %s%s\n", name,
TwStrError(err), TwStrErrorDetail(err, TwErrnoDetail));
return 1;
}
#define DefColor COL(WHITE,BLACK)
#define Underline COL(HIGH|WHITE,BLACK)
#define HalfInten COL(HIGH|BLACK,BLACK)
static hwattr *image;
static hwcol ColText = DefColor, Color = DefColor;
static uldat X, Y, Xmax, Ymax, Xreal, Yreal, max;
static enum {ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars} State = ESnormal;
#define NPAR 16
uldat nPar, Par[NPAR];
udat Effects;
#define EFF_INTENSITY ((udat)0x0001)
#define EFF_HALFINTENS ((udat)0x0002)
#define EFF_UNDERLINE ((udat)0x0004)
#define EFF_BLINK ((udat)0x0008)
#define EFF_REVERSE ((udat)0x0010)
TW_INLINE void update_eff(void) {
udat effects = Effects;
hwcol fg = COLFG(ColText), bg = COLBG(ColText);
if (effects & EFF_UNDERLINE)
fg = COLFG(Underline);
else if (effects & EFF_HALFINTENS)
fg = COLFG(HalfInten);
if (effects & EFF_REVERSE) {
hwcol tmp = COL(bg & ~HIGH, fg & ~HIGH) | COL(fg & HIGH, bg & HIGH);
fg = COLFG(tmp);
bg = COLBG(tmp);
}
if (effects & EFF_INTENSITY)
fg ^= HIGH;
if (effects & EFF_BLINK)
bg ^= HIGH;
Color = COL(fg, bg);
}
TW_INLINE void csi_m(void) {
udat i;
udat effects = Effects;
hwcol fg = COLFG(ColText), bg = COLBG(ColText);
for (i=0; i<=nPar; i++) switch (Par[i]) {
case 0:
/* all attributes off */
fg = COLFG(DefColor);
bg = COLBG(DefColor);
effects = 0;
break;
case 1:
effects &= ~EFF_HALFINTENS;
effects |= EFF_INTENSITY;
break;
case 2:
effects &= ~EFF_INTENSITY;
effects |= EFF_HALFINTENS;
break;
case 4:
effects |= EFF_UNDERLINE;
break;
case 5:
effects |= EFF_BLINK;
break;
case 7:
effects |= EFF_REVERSE;
break;
case 21:
case 22:
effects &= ~(EFF_HALFINTENS|EFF_INTENSITY);
break;
case 24:
effects &= ~EFF_UNDERLINE;
break;
case 25:
effects &= ~EFF_BLINK;
break;
case 27:
effects &= ~EFF_REVERSE;
break;
case 38: /* ANSI X3.64-1979 (SCO-ish?)
* Enables underscore, white foreground
* with white underscore
* (Linux - use default foreground).
*/
fg = COLFG(DefColor);
effects |= EFF_UNDERLINE;
break;
case 39: /* ANSI X3.64-1979 (SCO-ish?)
* Disable underline option.
* Reset colour to default? It did this
* before...
*/
fg = COLFG(DefColor);
effects &= ~EFF_UNDERLINE;
break;
case 49: /* restore default bg */
bg = COLBG(DefColor);
break;
default:
if (Par[i] >= 30 && Par[i] <= 37)
Par[i] -= 30,
fg = ANSI2VGA(Par[i]);
else if (Par[i] >= 40 && Par[i] <= 47)
Par[i] -= 40,
bg = ANSI2VGA(Par[i]);
break;
}
Effects = effects;
ColText = COL(fg, bg);
update_eff();
}
TW_INLINE void Fill(hwattr *t, hwattr h, uldat count) {
while (count--)
*t++ = h;
}
TW_INLINE void Xgrow(void) {
uldat n, newXmax;
newXmax = X * 2 + 2;
n = newXmax * Ymax;
if (!n) {
Xmax = newXmax;
return;
}
if (!(image = TwReAllocMem(image, n * sizeof(hwattr))))
panic();
max = n;
n = Ymax;
while (n--) {
TwMoveMem(image + n * Xmax, image + n * newXmax, Xmax * sizeof(hwattr));
Fill(image + n * newXmax + Xmax, HWATTR(Color, ' '), newXmax - Xmax);
}
Xmax = newXmax;
}
TW_INLINE void Ygrow(void) {
uldat n, newYmax;
newYmax = Y * 2 + 2;
n = Xmax * newYmax;
if (!n) {
Ymax = newYmax;
return;
}
if (!(image = TwReAllocMem(image, n * sizeof(hwattr))))
panic();
Fill(image + max, HWATTR(Color, ' '), n - max);
max = n;
Ymax = newYmax;
}
void addc(byte c) {
if (X >= Xmax) Xgrow();
if (Y >= Ymax) Ygrow();
if (Xreal <= X) Xreal = X + 1;
if (Yreal <= Y) Yreal = Y + 1;
image[X + Y * Xmax] = HWATTR(Color, c);
X++;
}
void finalize(void) {
uldat i;
if (!image || !Xreal || !Yreal)
return;
for (i = 1; i < Yreal; i++)
TwMoveMem(image + i * Xmax, image + i * Xreal, Xreal * sizeof(hwattr));
}
#define goto_xy(x, y) (X = (x), Y = (y))
hwattr *load_ascii_art(FILE *aaFILE, uldat *x, uldat *y, uldat padX, uldat padY) {
int c;
if (!(image = TwAllocMem(max * sizeof(hwattr))))
panic();
while ((c = fgetc(aaFILE)) != EOF) {
switch (State) {
case ESnormal:
switch ((byte)c) {
case 8:
if (X) X--;
break;
case 9: /* TAB */
do {
addc(' ');
} while (X & 7);
break;
case 10: case 11: case 12:
X = 0;
Y++;
break;
case 24: case 26:
State = ESnormal;
break;
case 27:
State = ESesc;
break;
case 127:
if (X < Xmax && Y < Ymax)
image[X + Y * Xmax] = HWATTR(Color, ' ');
if (X) X--;
break;
case 128+27:
State = ESsquare;
break;
default:
addc(c);
break;
}
break;
case ESesc:
switch (c) {
case '[':
State = ESsquare;
break;
default:
break;
}
break;
case ESsquare:
Par[0] = nPar = 0;
State = ESgetpars;
/* FALLTHROUGH */
case ESgetpars:
if (c==';' && nPar<NPAR-1) {
Par[++nPar] = 0;
break;
} else if (c>='0' && c<='9') {
Par[nPar] *= 10;
Par[nPar] += c-'0';
break;
} else
State = ESgotpars;
/* FALLTHROUGH */
case ESgotpars:
switch (c) {
case 'm':
csi_m();
break;
case 'H': case 'f':
if (Par[0]) Par[0]--;
if (!nPar)
Par[1] = nPar;
else if (Par[1]) Par[1]--;
goto_xy(Par[1],Par[0]);
break;
case 'G': case '`':
if (Par[0]) Par[0]--;
goto_xy(Par[0], Y);
break;
case 'A':
if (!Par[0]) Par[0]++;
goto_xy(X, Y - Par[0]);
break;
case 'B': case 'e':
if (!Par[0]) Par[0]++;
goto_xy(X, Y + Par[0]);
break;
case 'C': case 'a':
if (!Par[0]) Par[0]++;
goto_xy(X + Par[0], Y);
break;
case 'D':
if (!Par[0]) Par[0]++;
goto_xy(X - Par[0], Y);
break;
case 'E':
if (!Par[0]) Par[0]++;
goto_xy(0, Y + Par[0]);
break;
case 'F':
if (!Par[0]) Par[0]++;
goto_xy(0, Y - Par[0]);
break;
case 'd':
if (Par[0]) Par[0]--;
goto_xy(X, Par[0]);
break;
default:
break;
}
State = ESnormal;
break;
default:
State = ESnormal;
break;
}
}
if (padX || padY) {
goto_xy(Xreal + padX - 1, Yreal + padY - 1);
addc(' ');
}
finalize();
*x = Xreal;
*y = Yreal;
return image;
}
|