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
|
/*
* XBoing - An X11 blockout style computer game
*
* (c) Copyright 1993, 1994, 1995, Justin C. Kibell, All Rights Reserved
*
* The X Consortium, and any party obtaining a copy of these files from
* the X Consortium, directly or indirectly, is granted, free of charge, a
* full and unrestricted irrevocable, world-wide, paid up, royalty-free,
* nonexclusive right and license to deal in this software and
* documentation files (the "Software"), including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons who receive
* copies from any such party to do so. This license includes without
* limitation a license to do the foregoing actions under any patents of
* the party supplying this software to the X Consortium.
*
* In no event shall the author be liable to any party for direct, indirect,
* special, incidental, or consequential damages arising out of the use of
* this software and its documentation, even if the author has been advised
* of the possibility of such damage.
*
* The author specifically disclaims any warranties, including, but not limited
* to, the implied warranties of merchantability and fitness for a particular
* purpose. The software provided hereunder is on an "AS IS" basis, and the
* author has no obligation to provide maintenance, support, updates,
* enhancements, or modifications.
*/
/*
* =========================================================================
*
* $Id: misc.c,v 1.1.1.1 1994/12/16 01:36:47 jck Exp $
* $Source: /usr5/legends/jck/xb/master/xboing/misc.c,v $
* $Revision: 1.1.1.1 $
* $Date: 1994/12/16 01:36:47 $
*
* $Log: misc.c,v $
* Revision 1.1.1.1 1994/12/16 01:36:47 jck
* The XBoing distribution requires configuration management. This is why the
* cvs utility is being used. This is the initial import of all source etc..
*
*
* =========================================================================
*/
/*
* Include file dependencies:
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/param.h>
#include <pwd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <netinet/in.h>
#include "init.h"
#include "error.h"
#include "stage.h"
#include "dialogue.h"
#include "misc.h"
/*
* Internal macro definitions:
*/
/*
* Internal type declarations:
*/
/*
* Internal variable declarations:
*/
#ifdef NEED_USLEEP
#if NeedFunctionPrototypes
void usleep(unsigned long usec)
#else
void usleep(usec)
unsigned long usec;
#endif
{
#ifdef SYSV
#ifdef __clipper__
struct timeval tv;
tv.tv_sec=((usec)/1000);
tv.tv_usec=(((usec)%1000)*1000);
select(1,NULL,NULL,NULL,&tv);
#else
poll((struct poll *) 0, (size_t) 0, usec / 1000); /* ms resolution */
#endif
#else
struct timeval timeout;
timeout.tv_usec = usec % (unsigned long) 1000000;
timeout.tv_sec = usec / (unsigned long) 1000000;
select(0, (void *) 0, (void *) 0, (void *) 0, &timeout);
#endif
}
#endif
#if NeedFunctionPrototypes
void sleepSync(Display *display, unsigned long ms)
#else
void sleepSync(display, ms)
Display *display;
unsigned long ms;
#endif
{
struct timeval st, et;
long SyncTime;
static unsigned long accu;
#ifdef __alpha__
ms *= 1000; /* convert from milliseconds to microseconds for usleep */
#endif
gettimeofday(&st, NULL);
XSync(display, False);
gettimeofday(&et, NULL);
SyncTime = (((et.tv_sec - st.tv_sec) * 1000) +
((et.tv_usec - st.tv_usec) / 1000) );
/* if ((ms) > ((1000 / 60) + SyncTime))
usleep(ms - SyncTime);
*/
if ((ms + accu) > ((1000 / 30) + SyncTime))
{
usleep(ms +accu - SyncTime);
accu = 0;
}
else if (ms > SyncTime)
accu += (ms - SyncTime);
}
#if NeedFunctionPrototypes
void DrawLine(Display *display, Window window, int x, int y, int x2, int y2,
int colour, int width)
#else
void DrawLine(display, window, x, y, x2, y2, colour, width)
Display *display;
Window window;
int x;
int y;
int x2;
int y2;
int colour;
int width;
#endif
{
/* Change the width of the line */
XSetLineAttributes(display, gcxor, width, LineSolid, CapProjecting,
JoinMiter);
XSetLineAttributes(display, gcand, width, LineSolid, CapProjecting,
JoinMiter);
/* Set to the desired colours */
XSetBackground(display, gcxor, colour);
XSetForeground(display, gcxor, colour);
/* Now draw the line */
XDrawLine(display, window, gcxor, x, y, x2, y2);
XDrawLine(display, window, gcand, x, y, x2, y2);
XDrawLine(display, window, gcxor, x, y, x2, y2);
}
#if NeedFunctionPrototypes
void DrawShadowCentredText(Display *display, Window window, XFontStruct *font,
char *string, int y, int colour, int width)
#else
void DrawShadowCentredText(display, window, font, string, y, colour, width)
Display *display;
Window window;
XFontStruct *font;
char *string;
int y;
int colour;
int width;
#endif
{
int plen, len, x;
/* String length */
len = strlen(string);
/* Length of string in pixels */
plen = XTextWidth(font, string, len);
/* Start drawing so the text is centered */
x = (width / 2) - (plen / 2);
/* Draw the text with a shadow */
DrawText(display, window, x+2, y + 2, font, black, string, -1);
DrawText(display, window, x, y, font, colour, string, -1);
}
#if NeedFunctionPrototypes
void DrawShadowText(Display *display, Window window, XFontStruct *font,
char *string, int x, int y, int colour)
#else
void DrawShadowText(display, window, font, string, x, y, colour)
Display *display;
Window window;
XFontStruct *font;
char *string;
int x;
int y;
int colour;
#endif
{
int len;
/* String length */
len = strlen(string);
/* Draw the text with a shadow */
DrawText(display, window, x+2, y + 2, font, black, string, -1);
DrawText(display, window, x, y, font, colour, string, -1);
}
#if NeedFunctionPrototypes
void DrawTextFast(Display *display, Window window, int x, int y, XFontStruct *font,
int colour, char *text, int numChar)
#else
void DrawTextFast(display, window, x, y, font, colour, text, numChar)
Display *display;
Window window;
int x;
int y;
XFontStruct *font;
int colour;
char *text;
int numChar;
#endif
{
int len = strlen(text);
/* If numchar is passed > 0 then only draw numChar characters */
if (numChar > 0)
len = numChar;
/* Change to the new font */
XSetFont(display, gccopy, font->fid);
/* Change the drawing function */
XSetForeground(display, gccopy, colour);
/* Draw the string into the drawable */
XDrawString(display, window, gccopy, x, y + font->ascent, text, len);
}
#if NeedFunctionPrototypes
void DrawText(Display *display, Window window, int x, int y, XFontStruct *font,
int colour, char *text, int numChar)
#else
void DrawText(display, window, x, y, font, colour, text, numChar)
Display *display;
Window window;
int x;
int y;
XFontStruct *font;
int colour;
char *text;
int numChar;
#endif
{
int len = strlen(text);
/* If numchar is passed > 0 then only draw numChar characters */
if (numChar > 0)
len = numChar;
/* Change to the new font */
XSetFont(display, gcxor, font->fid);
XSetFont(display, gcand, font->fid);
/* Change the drawing function */
XSetBackground(display, gcxor, colour);
XSetForeground(display, gcxor, colour);
/* Draw the string into the drawable */
XDrawString(display, window, gcxor, x, y + font->ascent, text, len);
XDrawString(display, window, gcand, x, y + font->ascent, text, len);
XDrawString(display, window, gcxor, x, y + font->ascent, text, len);
}
#if NeedFunctionPrototypes
void RenderShape(Display *display, Window window, Pixmap pixmap,
Pixmap mask, int x, int y, int w, int h, int clear)
#else
void RenderShape(display, window, pixmap, mask, x, y, w, h, clear)
Display *display;
Window window;
Pixmap pixmap;
Pixmap mask;
int x;
int y;
int w;
int h;
int clear;
#endif
{
/* Clear the background first? */
if (clear) XClearArea(display, window, x, y, w, h, False);
/* Set to dest x and y clip origin */
XSetClipOrigin(display, gc, x, y);
/* Set the clipping mask */
XSetClipMask(display, gc, mask);
XCopyArea(display, pixmap, window, gc, 0, 0, w, h, x, y);
/* Unset clip (or add a clip gc) */
XSetClipMask(display, gc, None);
}
#if NeedFunctionPrototypes
void FreeMisc(Display *display)
#else
void FreeMisc(display)
Display *display;
#endif
{
}
#if NeedFunctionPrototypes
int ColourNameToPixel(Display *display, Colormap colormap, char *colourName)
#else
int ColourNameToPixel(display, colormap, colourName)
Display *display;
Colormap colormap;
char *colourName;
#endif
{
XColor colour;
/* Obtain the exact colour from the colour name */
if (XParseColor(display, DefaultColormap(display,
XDefaultScreen(display)), colourName, &colour) != 0)
{
/* Now allocate the colour */
if (XAllocColor(display, colormap, &colour) != 0)
{
/* Success - return the pixel id */
return colour.pixel;
}
}
/* Obviously a problem so barf */
ShutDown(display, 1, "Error while parsing colours.");
/* NOT REACHED */
return 1;
}
#if NeedFunctionPrototypes
char *getUsersFullName(void)
#else
char *getUsersFullName()
#endif
{
struct passwd *pass;
char *comma;
char *cp1, *cp2;
static char fullname[80];
/* Get user information from password file */
if (!(pass = getpwuid(getuid())))
return("Anonymous?"); /* Unknown user oops. */
/* find a comma indicating further info after name */
comma = strchr(pass->pw_gecos, ',');
/* NULL out the comma */
if (comma) *comma = '\0';
/* Use the nickname if not null otherwise password file name */
cp1 = pass->pw_gecos;
cp2 = fullname;
/* Search through the gecos field looking for an '&' which on very
* old UNIX systems is supposed to be the users user name with the
* first letter uppercased.
*/
while(*cp1)
{
/* Look for the '&' symbol */
if(*cp1 != '&')
*cp2++ = *cp1++;
else
{
/* A ha. Now copy the users name to be in place of '&' */
strcpy(cp2, pass->pw_name);
/* Convert the first letter to uppercase. */
if(islower(*cp2))
*cp2 = toupper(*cp2);
/* Continue with the remaining string */
while(*cp2) cp2++;
cp1++;
}
}
/* Return their name without any trailing stuff */
return(fullname);
}
#if NeedFunctionPrototypes
char *GetHomeDir(void)
#else
char *GetHomeDir()
#endif
{
int uid;
struct passwd *pw;
register char *ptr;
static char dest[MAXPATHLEN];
/* This function will return the home directory of the user
* by either using the HOME environment variable or constructing
* it through the USER environment variable.
*/
if ((ptr = getenv("HOME")) != NULL)
(void) strncpy(dest, ptr,sizeof(dest)-1);
else
{
/* HOME variable is not present so get USER var */
if ((ptr = getenv("USER")) != NULL)
pw = getpwnam(ptr);
else
{
/* Obtain user id etc. */
uid = getuid();
pw = getpwuid(uid);
}
if (pw)
(void) strncpy(dest, pw->pw_dir, sizeof(dest)-1);
else
*dest = '\0';
}
/* This will be NULL on error or the actual path */
return dest;
}
#if NeedFunctionPrototypes
int ResizeMainWindow(Display *display, Window window, int width, int height)
#else
int ResizeMainWindow(display, window, width, height)
Display *display;
Window window;
int width;
int height;
#endif
{
XWindowChanges values;
unsigned int value_mask;
int screen = XDefaultScreen(display);
values.width = width;
values.height = height;
value_mask = CWWidth | CWHeight;
if (XReconfigureWMWindow(display, window, screen, value_mask, &values) == 0)
return False;
sleepSync(display, 100);
return True;
}
#if NeedFunctionPrototypes
int ObtainWindowWidthHeight(Display *display, Window window,
int *width, int *height)
#else
int ObtainWindowWidthHeight(display, window, width, height)
Display *display;
Window window;
int *width;
int *height;
#endif
{
XWindowAttributes attributes;
if (XGetWindowAttributes(display, window, &attributes) == 0)
ErrorMessage("Unable to obtain window attributes.");
*width = attributes.width;
*height = attributes.height;
}
#if NeedFunctionPrototypes
int ObtainMousePosition(Display *display, Window window, int *x, int *y)
#else
int ObtainMousePosition(display, window, x, y)
Display *display;
Window window;
int *x, *y;
#endif
{
int rx, ry, x1, y1;
unsigned int mask;
Window root, child;
*x = 0;
*y = 0;
/* Obtain the position of the pointer in window */
if (XQueryPointer(display, window, &root, &child,
&rx, &ry, &x1, &y1, &mask) == True)
{
*x = x1;
*y = y1;
return True;
}
return False;
}
#if NeedFunctionPrototypes
int YesNoDialogue(Display *display, char *message)
#else
int YesNoDialogue(display, message)
Display *display;
char *message;
#endif
{
char str[80];
str[0] = '\0';
/* Obtain a string from the user. Should contain NULL or numbers */
strcpy(str, UserInputDialogueMessage(display, message, TEXT_ICON,
YES_NO_ENTRY));
/* Nothing input so just return */
if (str[0] == '\0') return False;
DEBUG("YesNoDialogue() got an answer.");
if (tolower(str[0]) == 'y') return True;
if (tolower(str[0]) == 'n') return False;
/* Just in case */
return False;
}
#if NeedFunctionPrototypes
Pixmap ScalePixmap(Display *display, Window window, Pixmap source,
int swidth, int sheight, int dwidth, int dheight)
#else
Pixmap ScalePixmap(display, window, source, swidth, sheight, dwidth, dheight)
Display *display;
Window window;
Pixmap source;
int swidth, sheight;
int dwidth,dheight;
#endif
{
Pixmap temp, dest;
int j, end;
float i;
float xscale, yscale;
xscale = (float) swidth / (float) dwidth; /* Scaling factors */
yscale = (float) sheight / (float) dheight;
dest = XCreatePixmap(display, window, dwidth, dheight,
DefaultDepth(display, XDefaultScreen(display)));
temp = XCreatePixmap(display, window, dwidth, sheight,
DefaultDepth(display, XDefaultScreen(display)));
j = 0;
end = dwidth * xscale;
/* Scale width of source into temp pixmap */
for(i = 0; i < end; i += xscale)
{
XCopyArea(display, source, temp, gccopy,
(int) i, 0, 1, sheight, j++, 0);
}
j = 0;
end = dheight * yscale;
/* Scale height of temp into dest pixmap */
for(i = 0; i < end; i += yscale)
{
XCopyArea(display, temp, dest, gccopy,
0, (int) i, dwidth, 1, 0, j++);
}
XFreePixmap(display, temp);
return (Pixmap) dest;
}
#if NeedFunctionPrototypes
void Draw4PointCurve(Display *display, Window window, XPoint *p, int num_steps)
#else
void Draw4PointCurve(display, window, p, num_steps)
Display *display;
Window window;
XPoint *p;
int num_steps;
#endif
{
double t, t_sq, t_cb, incr;
double r1, r2, r3, r4;
ushort curve_x, curve_y;
int ptIndex;
long maxLines;
XPoint *pts;
/*
* First check and see if the maximum number of points the X Server can
* draw is less than the number of steps wanted - most likely to be ok.
*/
maxLines = (double) (XMaxRequestSize(display) - 3) / 2.0;
if (num_steps > maxLines)
num_steps = maxLines;
incr = 1.0 / (double) num_steps;
if ((pts = (XPoint *) malloc(num_steps * sizeof(XPoint))) ==
(XPoint *)0)
{
/* Obviously a problem so barf */
ShutDown(display, 1, "Unable to malloc memory for Draw4PointCurve().");
}
ptIndex = 0;
pts[ptIndex].x = p[0].x;
pts[ptIndex].y = p[0].y;
for (t = incr; t <= 1.01; t += incr)
{
t_sq = t * t;
t_cb = t * t_sq;
r1 = (1 - 3*t + 3*t_sq - t_cb) * (double) p[0].x;
r2 = ( 3*t - 6*t_sq + 3*t_cb) * (double) p[1].x;
r3 = ( 3*t_sq - 3*t_cb) * (double) p[2].x;
r4 = ( t_cb) * (double) p[3].x;
curve_x = (ushort) (r1 + r2 + r3 + r4);
r1 = (1 - 3*t + 3*t_sq - t_cb) * (double) p[0].y;
r2 = ( 3*t - 6*t_sq + 3*t_cb) * (double) p[1].y;
r3 = ( 3*t_sq - 3*t_cb) * (double) p[2].y;
r4 = ( t_cb) * (double) p[3].y;
curve_y = (ushort) (r1 + r2 + r3 + r4);
ptIndex++;
pts[ptIndex].x = curve_x;
pts[ptIndex].y = curve_y;
}
/* Now draw the curve based on a whole heap of lines */
XDrawLines(display, window, gc, pts, ptIndex, CoordModeOrigin);
/* Don't forget to free points */
free(pts);
}
|