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
|
/*
* $XConsortium: misc.c,v 1.92 92/03/13 17:02:08 gildea Exp $
*/
/*
* Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital Equipment
* Corporation not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
*
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include "x_ptyxP.h" /* X headers included here. */
#include "../machine.h"
#include <X11/Xos.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <pwd.h>
#include <errno.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Shell.h>
#include <X11/Xmu/Error.h>
#include <X11/Xmu/SysUtil.h>
#include <X11/Xmu/WinUtil.h>
#include "x_data.h"
#include "x_error.h"
#include "x_menu.h"
#include "All-extern-x.h"
#ifndef X_NOT_STDC_ENV
#include <stdlib.h>
#else
extern char *malloc();
extern char *getenv();
#endif
#if defined(macII) && !defined(__STDC__) /* stdlib.h fails to define
* these */
char *malloc();
#endif /* macII */
static void DoSpecialEnterNotify();
static void DoSpecialLeaveNotify();
void x_events()
{
XEvent event;
register TScreen *screen = &term->screen;
if (screen->scroll_amt)
FlushScroll(screen);
if (!XPending(screen->display))
/* protect against events/errors being swallowed by us or Xlib */
{
return;
}
do
{
if (waitingForTrackInfo)
{
return;
}
XNextEvent(screen->display, &event);
/* Hack to get around problems with the toolkit throwing away eventing
* during the exclusive grab of the menu popup. By looking at the event
* ourselves we make sure that we can do the right thing. */
if (event.type == EnterNotify &&
(event.xcrossing.window == XtWindow(XtParent(term))))
DoSpecialEnterNotify(&event.xcrossing);
else if (event.type == LeaveNotify &&
(event.xcrossing.window == XtWindow(XtParent(term))))
DoSpecialLeaveNotify(&event.xcrossing);
if (!event.xany.send_event ||
screen->allowSendEvents ||
((event.xany.type != KeyPress) &&
(event.xany.type != KeyRelease) &&
(event.xany.type != ButtonPress) &&
(event.xany.type != ButtonRelease)))
{
XtDispatchEvent(&event);
}
} while (QLength(screen->display) > 0);
}
Cursor make_colored_cursor(cursorindex, fg, bg)
int cursorindex; /* index into font */
unsigned long fg, bg; /* pixel value */
{
register TScreen *screen = &term->screen;
Cursor c;
register Display *dpy = screen->display;
c = XCreateFontCursor(dpy, cursorindex);
if (c == (Cursor) 0)
return (c);
recolor_cursor(c, fg, bg);
return (c);
}
/* ARGSUSED */
void HandleKeyPressed(w, event, params, nparams)
Widget w;
XEvent *event;
String *params;
Cardinal *nparams;
{
register TScreen *screen = &term->screen;
#ifdef ACTIVEWINDOWINPUTONLY
if (w == (Widget) term)
#endif
Input(&term->keyboard, screen, &event->xkey, False);
}
/* ARGSUSED */
void HandleEightBitKeyPressed(w, event, params, nparams)
Widget w;
XEvent *event;
String *params;
Cardinal *nparams;
{
register TScreen *screen = &term->screen;
#ifdef ACTIVEWINDOWINPUTONLY
if (w == (Widget) term)
#endif
Input(&term->keyboard, screen, &event->xkey, True);
}
/* ARGSUSED */
void HandleStringEvent(w, event, params, nparams)
Widget w;
XEvent *event;
String *params;
Cardinal *nparams;
{
register TScreen *screen = &term->screen;
#ifdef ACTIVEWINDOWINPUTONLY
if (w != (Widget) term)
return;
#endif
if (*nparams != 1)
return;
if ((*params)[0] == '0' && (*params)[1] == 'x' && (*params)[2] != '\0')
{
char c, *p, hexval[2];
hexval[0] = hexval[1] = 0;
for (p = *params + 2; (c = *p); p++)
{
hexval[0] *= 16;
if (isupper(c))
c = tolower(c);
if (c >= '0' && c <= '9')
hexval[0] += c - '0';
else if (c >= 'a' && c <= 'f')
hexval[0] += c - 'a' + 10;
else
break;
}
if (c == '\0')
StringInput(screen, hexval, 1);
} else
{
StringInput(screen, *params, strlen(*params));
}
}
static void DoSpecialEnterNotify(ev)
register XEnterWindowEvent *ev;
{
register TScreen *screen = &term->screen;
#ifdef ACTIVEWINDOWINPUTONLY
if (ev->window == XtWindow(XtParent((Widget) term)))
#endif
if (((ev->detail) != NotifyInferior) &&
ev->focus &&
!(screen->select & FOCUS))
selectwindow(screen, INWINDOW);
}
/*ARGSUSED*/
void HandleEnterWindow(w, eventdata, event)
Widget w;
register XEnterWindowEvent *event;
caddr_t eventdata;
{
/* NOP since we handled it above */
}
static void DoSpecialLeaveNotify(ev)
register XEnterWindowEvent *ev;
{
register TScreen *screen = &term->screen;
#ifdef ACTIVEWINDOWINPUTONLY
if (ev->window == XtWindow(XtParent((Widget) term)))
#endif
if (((ev->detail) != NotifyInferior) &&
ev->focus &&
!(screen->select & FOCUS))
unselectwindow(screen, INWINDOW);
}
/*ARGSUSED*/
void HandleLeaveWindow(w, eventdata, event)
Widget w;
register XEnterWindowEvent *event;
caddr_t eventdata;
{
/* NOP since we handled it above */
}
/*ARGSUSED*/
void HandleFocusChange(w, eventdata, event)
Widget w;
register XFocusChangeEvent *event;
caddr_t eventdata;
{
register TScreen *screen = &term->screen;
if (event->type == FocusIn)
selectwindow(screen,
(event->detail == NotifyPointer) ? INWINDOW :
FOCUS);
else
{
unselectwindow(screen,
(event->detail == NotifyPointer) ? INWINDOW :
FOCUS);
if (screen->grabbedKbd && (event->mode == NotifyUngrab))
{
XBell(screen->display, 100);
ReverseVideo(term);
screen->grabbedKbd = FALSE;
}
}
}
void selectwindow(screen, flag)
register TScreen *screen;
register int flag;
{
if (screen->cursor_state &&
(screen->cursor_col != screen->cur_col ||
screen->cursor_row != screen->cur_row))
HideCursor();
screen->select |= flag;
if (screen->cursor_state)
ShowCursor();
return;
}
void unselectwindow(screen, flag)
register TScreen *screen;
register int flag;
{
if (screen->always_highlight)
return;
screen->select &= ~flag;
if (screen->cursor_state &&
(screen->cursor_col != screen->cur_col ||
screen->cursor_row != screen->cur_row))
HideCursor();
if (screen->cursor_state)
ShowCursor();
}
static long lastBellTime; /* in milliseconds */
void Bell()
{
extern XtermWidget term;
register TScreen *screen = &term->screen;
struct timeval curtime;
long now_msecs;
/* has enough time gone by that we are allowed to ring the bell again? */
if (screen->bellSuppressTime)
{
if (screen->bellInProgress)
{
if (QLength(screen->display) > 0 ||
GetBytesAvailable(ConnectionNumber(screen->display)) > 0)
x_events();
if (screen->bellInProgress)
{ /* even after new events? */
return;
}
}
gettimeofday(&curtime, NULL);
now_msecs = 1000 * curtime.tv_sec + curtime.tv_usec / 1000;
if (lastBellTime != 0 && now_msecs - lastBellTime >= 0 &&
now_msecs - lastBellTime < screen->bellSuppressTime)
{
return;
}
lastBellTime = now_msecs;
}
if (screen->visualbell)
VisualBell();
else
XBell(screen->display, 0);
if (screen->bellSuppressTime)
{
/* now we change a property and wait for the notify event to come back.
* If the server is suspending operations while the bell is being emitted
* (problematic for audio bell), this lets us know when the previous bell
* has finished */
Widget w = (Widget) term;
XChangeProperty(XtDisplay(w), XtWindow(w),
XA_NOTICE, XA_NOTICE, 8, PropModeAppend,(unsigned char*) NULL, 0);
screen->bellInProgress = TRUE;
}
}
void VisualBell()
{
extern XtermWidget term;
register TScreen *screen = &term->screen;
register Pixel xorPixel = screen->foreground ^ term->core.background_pixel;
XGCValues gcval;
GC visualGC;
gcval.function = GXxor;
gcval.foreground = xorPixel;
visualGC = XtGetGC((Widget) term, GCFunction + GCForeground, &gcval);
XFillRectangle(
screen->display,
VWindow(screen),
visualGC,
0, 0,
(unsigned) FullWidth(screen),
(unsigned) FullHeight(screen));
XFlush(screen->display);
XFillRectangle(
screen->display,
VWindow(screen),
visualGC,
0, 0,
(unsigned) FullWidth(screen),
(unsigned) FullHeight(screen));
}
/* ARGSUSED */
void HandleBellPropertyChange(w, data, ev, more)
Widget w;
XtPointer data;
XEvent *ev;
Boolean *more;
{
register TScreen *screen = &term->screen;
if (ev->xproperty.atom == XA_NOTICE)
{
screen->bellInProgress = FALSE;
}
}
void Redraw()
{
extern XtermWidget term;
register TScreen *screen = &term->screen;
XExposeEvent event;
event.type = Expose;
event.display = screen->display;
event.x = 0;
event.y = 0;
event.count = 0;
if (VWindow(screen))
{
event.window = VWindow(screen);
event.width = term->core.width;
event.height = term->core.height;
(*term->core.widget_class->core_class.expose)
((Widget) term, (XEvent *) & event, NULL);
if (screen->scrollbar)
(*screen->scrollWidget->core.widget_class->core_class.expose)
(screen->scrollWidget, (XEvent *) & event, NULL);
}
}
void do_osc(func)
int (*func) ();
{
register int mode, c;
register char *cp;
char buf[512];
char *bufend = &buf[(sizeof buf) - 1]; /* leave room for null */
Bool okay = True;
/* lines should be of the form <ESC> ] number ; string <BEL>
*
* where number is one of 0, 1, 2, or 46 */
mode = 0;
while (isdigit(c = (*func) ()))
mode = 10 * mode + (c - '0');
if (c != ';')
okay = False;
cp = buf;
while (isprint((c = (*func) ()) & 0x7f) && cp < bufend)
*cp++ = c;
if (c != 7)
okay = False;
*cp = 0;
if (okay)
switch (mode)
{
case 0: /* new icon name and title */
Changename(buf);
Changetitle(buf);
break;
case 1: /* new icon name only */
Changename(buf);
break;
case 2: /* new title only */
Changetitle(buf);
break;
case 46: /* ignore log file */
Bell();
break;
case 50:
SetVTFont(fontMenu_fontescape, True, buf, (char *)0);
break;
/* One could write code to send back the display and host names, but
* that could potentially open a fairly nasty security hole. */
}
}
static void ChangeGroup(attribute, value)
String attribute;
XtArgVal value;
{
extern Widget toplevel;
Arg args[1];
XtSetArg(args[0], attribute, value);
XtSetValues(toplevel, args, 1);
}
void Changename(name)
register char *name;
{
ChangeGroup(XtNiconName, (XtArgVal) name);
}
void Changetitle(name)
register char *name;
{
ChangeGroup(XtNtitle, (XtArgVal) name);
}
#ifndef DEBUG
/* ARGSUSED */
#endif
void Panic(s, a)
char *s;
int a;
{
#ifdef DEBUG
if (debug)
{
fprintf(stderr, "%s: PANIC! ", xterm_name);
fprintf(stderr, s, a);
fputs("\r\n", stderr);
fflush(stderr);
}
#endif /* DEBUG */
}
/** copied from wf_fig.h **/
/* #include "wf_fig.h" */ /** for sys_errlist **/
#ifndef linux
#if !defined(__bsdi__) && !defined(__NetBSD__) && !defined(__APPLE__) && !defined(__FreeBSD__)
extern int errno;
extern int sys_nerr;
#if (! (defined(BSD) && (BSD >= 199306))) && !defined(__FreeBSD__) && !defined(__APPLE__)
extern char *sys_errlist[];
#endif
#endif
#endif
static char UE[]="unknown error";
char *SysErrorMsg(n)
int n;
{
return ((n >= 0 && n < sys_nerr) ? sys_errlist[n] : UE );
}
void SysError(i)
int i;
{
int oerrno;
oerrno = errno;
/* perror(3) write(2)s to file descriptor 2 */
fprintf(stderr, "%s: Error %d, errno %d: ", xterm_name, i, oerrno);
fprintf(stderr, "%s\n", SysErrorMsg(oerrno));
Cleanup(i);
}
void Error(i)
int i;
{
fprintf(stderr, "%s: Error %d\n", xterm_name, i);
Cleanup(i);
}
/*
* cleanup by sending SIGHUP to client processes
*/
void Cleanup(code)
int code;
{
extern XtermWidget term;
register TScreen *screen;
screen = &term->screen;
if (screen->pid > 1)
{
(void) kill_process_group((int) screen->pid, SIGHUP);
}
sci_clear_and_exit(code);
}
/*
* returns a pointer to the first occurrence of s2 in s1,
* or NULL if there are none.
*/
char *strindex(s1, s2)
register char *s1, *s2;
{
register char *s3;
int s2len = strlen(s2);
while ((s3 = index(s1, *s2)) != NULL)
{
if (strncmp(s3, s2, s2len) == 0)
return (s3);
s1 = ++s3;
}
return (NULL);
}
/*ARGSUSED*/
void xerror(d, ev)
Display *d;
register XErrorEvent *ev;
{
#ifndef NDEBUG
fprintf(stderr, "%s: warning, error event receieved:\n", xterm_name);
(void) XmuPrintDefaultErrorMessage(d, ev, stderr);
/*sci_clear_and_exit(ERROR_XERROR);*/
#endif
}
/*ARGSUSED*/
void xioerror(dpy)
Display *dpy;
{
(void)
fprintf(stderr,
"%s: fatal IO error %d (%s) or KillClient on X server \"%s\"\r\n",
xterm_name, errno, SysErrorMsg(errno),
DisplayString(dpy));
sci_clear_and_exit(ERROR_XIOERROR);
}
/**
static void withdraw_window(dpy, w, scr)
Display *dpy;
Window w;
int scr;
{
(void) XmuUpdateMapHints(dpy, w, (XSizeHints *)0);
XWithdrawWindow(dpy, w, scr);
return;
}
**/
void set_vt_visibility(on)
Boolean on;
{
extern Widget toplevel; /* JPC */
register TScreen *screen = &term->screen;
if (on)
{
if (!screen->Vshow && term)
{
VTInit1(toplevel); /* JPC */
XtMapWidget(term->core.parent);
screen->Vshow = TRUE;
}
}
}
|