|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T m
Length: 12957 (0x329d)
Types: TextFile
Names: »main.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
└─⟦this⟧ »EUUGD18/X/Dragon/main.c«
/******************************************************************************
* Dragon - a version of Mah-Jongg for X Windows
*
* Author: Gary E. Barnes March 1989
*
* main.c - The mainline code.
******************************************************************************/
#define _MAIN_C_
#include "main.h"
#include <X11/ShellP.h>
#include "ic/background.ic"
#include "ic/gray.ic"
#include "ic/shadow.ic"
extern void Icon_Setup();
extern void Board_Setup();
\f
/******************************************************************************
* Dragon Resources
******************************************************************************/
static int def_DCTime = 250; /* ms for double click */
static Boolean def_FALSE = FALSE;
static Boolean def_TRUE = TRUE;
#define offset(field) XtOffset( Dragon_Resources_Ptr, field )
#define BOOLEAN(name,class,field,default) \
{name, class, XtRBoolean, sizeof(Boolean), offset(field), XtRBoolean, \
(caddr_t)default}
#define CURSOR(name,class,field,default) \
{name, class, XtRCursor, sizeof(Cursor), offset(field), \
XtRString, (caddr_t)default}
#define FONT(name,class,field,default) \
{name, class, XtRFontStruct, sizeof(XFontStruct*), offset(field), \
XtRString, (caddr_t)default}
#define INT(name,class,field,default) \
{name, class, XtRInt, sizeof(int), offset(field), XtRInt, (caddr_t)default}
#define PIXEL(name,class,field,default) \
{name, class, XtRPixel, sizeof(Pixel), \
offset(field), XtRString, (caddr_t)default}
#define STRING(name,class,field,default) \
{name, class, XtRString, sizeof(String), offset(field), XtRString, \
(caddr_t)default}
static XtResource dragon_resources[] = {
PIXEL ( XtNbackground, XtCBackground, Background,
XtExtdefaultbackground ),
CURSOR ( "cursor", XtCCursor, Cursor, "hand2" ),
BOOLEAN( "debug", "Debug", Debug, &def_FALSE ),
INT ( "doubleClick", "DoubleClick", Double_Click_Time, &def_DCTime ),
PIXEL ( XtNforeground, XtCForeground, Foreground,
XtExtdefaultforeground ),
STRING ( XtNgeometry, XtCGeometry, Geometry, "450x340" ),
STRING ( "iconGeometry", "IconGeometry", Icon_Geometry, "64x64" ),
BOOLEAN( "iconic", "Iconic", Iconic, &def_FALSE ),
STRING ( XtNiconName, XtCIconName, Icon_Name, NULL ),
BOOLEAN( XtNreverseVideo, XtCReverseVideo, Reverse_Video, &def_FALSE ),
BOOLEAN( "stickyTile", "StickyTile", Sticky_Tile, &def_TRUE ),
BOOLEAN( "tileShadows", "TileShadows", Tile_Shadows, &def_TRUE ),
STRING ( "tileSides", "TileSides", Tile_Sides, "gray" ),
};
#undef offset
#undef BOOLEAN
#undef FONT
#undef INT
#undef STRING
\f
/******************************************************************************
* Dragon Command Line Options
******************************************************************************/
static XrmOptionDescRec command_line_options[] = {
{"-cursor", ".cursor", XrmoptionSepArg, (caddr_t)NULL },
{"-debug", ".debug", XrmoptionNoArg, (caddr_t)"on" },
{"-double", ".doubleClick", XrmoptionSepArg, (caddr_t)NULL },
{"-geometry", ".geometry", XrmoptionSepArg, (caddr_t)NULL },
{"-icongeometry",".iconGeometry", XrmoptionSepArg, (caddr_t)NULL },
{"-iconic", ".iconic", XrmoptionNoArg, (caddr_t)"on" },
{"+iconic", ".iconic", XrmoptionNoArg, (caddr_t)"off" },
{"-iconname", ".iconName", XrmoptionSepArg, (caddr_t)NULL },
{"-reverse", "*reverseVideo", XrmoptionNoArg, (caddr_t)"on" },
{"+reverse", "*reverseVideo", XrmoptionNoArg, (caddr_t)"off" },
{"-shadows", ".tileShadows", XrmoptionNoArg, (caddr_t)"off" },
{"+shadows", ".tileShadows", XrmoptionNoArg, (caddr_t)"on" },
{"-sides", ".tileSides", XrmoptionSepArg, (caddr_t)"line"},
{"-sticky", ".stickyTile", XrmoptionNoArg, (caddr_t)"off" },
{"+sticky", ".stickyTile", XrmoptionNoArg, (caddr_t)"on" },
};
typedef struct {
String name;
String desc;
} Option_Help;
static Option_Help command_line_help[] = {
{"-help", "print out this message"},
{"-display displayname", "X server to contact"},
{"-cursor name", "name of the cursor to use"},
{"-double time", "double-click time limit in milliseconds"},
{"-font name", "font to use for buttons and messages"},
{"-geometry WxH+X+Y", "size (in pixels) and position of the board"},
{"-icongeometry WxH+X+Y","size (in pixels) and position of the program icon"},
{"-/+iconic", "begin program as an icon"},
{"-iconname name", "name to use with the program icon"},
{"-/+reverse", "turn on/off reverse video"},
{"-/+shadows", "turn on/off tile shadows"},
{"-sides line/black/gray","set the style for tile sides"},
{"-/+sticky", "first tile selected sticky"},
{ NULL, NULL }
};
\f
static void Command_Line_Usage( Bad_Option )
String Bad_Option;
/******************************************************************************
* Bad_Option - Specifies the unfathomable command line option we received
*
* Called when we find an unrecognized command line option. We spit out a
* Unix-sytle usage message and die.
******************************************************************************/
{
Option_Help *opt;
int col;
(void)fprintf( stderr, "%s: unrecognized command line option [%s]\n\n",
Program_Name, Bad_Option );
(void)fprintf( stderr, "usage: %s", Program_Name );
col = 8 + strlen(Program_Name);
for (opt = command_line_help; opt->name; opt++) {
int len = 3 + strlen(opt->name); /* space[string] */
if (col + len > 79) {
(void)fprintf (stderr, "\n "); /* 3 spaces */
col = 3;
}
(void)fprintf (stderr, " [%s]", opt->name);
col += len;
}
(void)fprintf( stderr, "\n\nType %s -help for a more help.\n",
Program_Name );
exit( 1 );
} /* Command_Line_Usage */
\f
static void Command_Line_Help()
/******************************************************************************
* Called when the -help switch is used. Put out our extended help message.
******************************************************************************/
{
Option_Help *opt;
(void)fprintf( stderr, "usage:\n %s [options]\n\n",
Program_Name);
(void)fprintf( stderr, "Where the options are:\n");
for (opt = command_line_help; opt->name; opt++) {
(void)fprintf( stderr, " %-24s %s\n", opt->name, opt->desc );
}
(void)fprintf( stderr, "\n" );
exit( 0 );
} /* Command_Line_Help */
\f
static void GC_Setup()
/******************************************************************************
* Set up the GC's that we will be using for drawing.
******************************************************************************/
{
XGCValues gcv;
Pixel pix;
/*--Xor_GC - fg = black, bg = white, func = xor; flops colors between the
* foreground and the background when applied. */
gcv.function = GXxor;
gcv.foreground = Dragon_Resources.Foreground ^ Dragon_Resources.Background;
gcv.background = (Pixel)0; /* pixel value with all bits "off" */
Xor_GC = XtGetGC( Dragon,
GCFunction|GCForeground|GCBackground,
&gcv );
/*--Normal_GC - fg = black, bg = white, func = copy */
gcv.function = GXcopy;
gcv.foreground = Dragon_Resources.Foreground;
gcv.background = Dragon_Resources.Background;
Normal_GC = XtGetGC( Dragon, GCFunction|GCForeground|GCBackground, &gcv );
/*--Gray_GC - fg = black, bg = white, func = copy, tile = Gray */
gcv.tile =
XCreatePixmapFromBitmapData
( XtDisplay(Board), XtWindow(Board),
gray_bits, gray_width, gray_height,
Dragon_Resources.Foreground, Dragon_Resources.Background,
DefaultDepthOfScreen(XtScreen(Board)) );
if (gcv.tile == (Pixmap)0L) {
(void)fprintf( stderr, "Can't allocate gray pixmap.\n" );
exit(1);
}
gcv.fill_style = FillTiled;
Gray_GC = XtGetGC( Dragon,
GCFunction|GCForeground|GCBackground|GCTile|GCFillStyle,
&gcv );
/*--Over_GC - fg = black xor white, bg = 0, func = copy */
gcv.function = GXcopy;
gcv.stipple = /* stipple operations are always depth 1 */
XCreateBitmapFromData
( XtDisplay(Board), XtWindow(Board),
shadow_bits, shadow_width, shadow_height );
if (gcv.stipple == (Pixmap)0L) {
(void)fprintf( stderr, "Can't allocate shadow pixmap.\n" );
exit(1);
}
gcv.fill_style = FillStippled;
Over_GC = XtGetGC( Dragon,
GCFunction|GCForeground|GCBackground|
GCStipple|GCFillStyle,
&gcv );
/*--Reverse_GC - fg = white, bg = black, func = copy */
gcv.function = GXcopy;
gcv.background = Dragon_Resources.Foreground;
gcv.foreground = Dragon_Resources.Background;
Reverse_GC = XtGetGC( Dragon, GCFunction|GCForeground|GCBackground, &gcv );
} /* GC_Setup */
\f
int main (argc, argv)
int argc;
String *argv;
/******************************************************************************
* Our Main-Line Code.
******************************************************************************/
{
Arg args[40];
int argi = 0;
#undef SETARG
#define SETARG(name,value) \
DEBUG_ERROR(argi >= XtNumber(args), "main, args overrun!\n" ); \
XtSetArg( args[argi], name, (XtArgVal)value ); ++argi;
/*--Start up the toolkit. This creates a shell widget. Unfortunately the
* only way the toolkit allows you to specify default application resources
* is by having a /usr/lib/X11/app-defaults/Application resouce file. I'm
* not interested in doing this. If some site wants to create one in order
* to customize the program then that is fine. However, I want my program
* to be "standalone"; ie. if you have the binary executable then you have
* all you *must-have*. So, I will Destroy this widget shortly. */
Program_Name = argv[0];
Dragon =
XtInitialize ( "dragon", "Dragon",
command_line_options, XtNumber(command_line_options),
&argc, argv );
/*--See if there is anything left on the command line. */
for (++argv, --argc; argc > 0; ++argv, --argc) {
if (strcmp( argv[0], "-help" ) == 0) { Command_Line_Help(); }
Command_Line_Usage( argv[0] );
}
/*--Now get the application resources. */
DEBUG_OTHER(main,XtGetApplicationResources);
XtGetApplicationResources( Dragon, (caddr_t)&Dragon_Resources,
dragon_resources, XtNumber(dragon_resources),
(ArgList)NULL, (Cardinal)0 );
/*--Set up our icon. */
DEBUG_OTHER(main,Icon_Setup);
Icon_Setup( args, (int)XtNumber(args), &argi );
/*--Now set up our "real" application shell. */
DEBUG_OTHER(main,XtDestroyWidget);
XtDestroyWidget( Dragon );
/*--Set up various other controls on our top level widget that we require. */
SETARG( XtNminWidth, 450 );
SETARG( XtNminHeight, 340 );
SETARG( XtNwidthInc, 210 );
SETARG( XtNheightInc, 160 );
SETARG( XtNminAspectX, 1333 );
SETARG( XtNminAspectY, 1000 );
SETARG( XtNmaxAspectX, 1333 );
SETARG( XtNmaxAspectY, 1000 );
SETARG( XtNgeometry, Dragon_Resources.Geometry );
SETARG( XtNforeground, Dragon_Resources.Foreground );
SETARG( XtNbackground, Dragon_Resources.Background );
SETARG( XtNallowShellResize, TRUE );
SETARG( XtNinput, TRUE );
if (argi > XtNumber(args)) {
(void)fprintf(stderr, "assert: args?\n");
exit(1);
}
DEBUG_OTHER(main,XtCreateApplicationShell);
Dragon = XtCreateApplicationShell( "dragon",
topLevelShellWidgetClass,
args, (Cardinal)argi );
argi = 0;
/*--Check the tile controls. */
Tile_Control = 0;
if (Dragon_Resources.Tile_Shadows) { Tile_Control |= SHADOW; }
if (strcmp( Dragon_Resources.Tile_Sides, "black" ) == 0) {
Tile_Control |= BLACKSIDE;
} else if (strcmp( Dragon_Resources.Tile_Sides, "gray" ) == 0) {
Tile_Control |= BLACKSIDE | GRAYSIDE;
} else if (strcmp( Dragon_Resources.Tile_Sides, "line" ) != 0) {
(void)fprintf( stderr,
"-side option not given line, gray, or black value.\n");
}
/*--Create the board that we will be using; realize it too. */
DEBUG_OTHER(main,Board_Setup);
Board_Setup();
/*--Set up our GC's. */
DEBUG_OTHER(main,GC_Setup);
GC_Setup();
/*--Now make it all appear in front of the user. */
{ Pixmap pix;
pix =
XCreatePixmapFromBitmapData
( XtDisplay(Board), XtWindow(Board),
background_bits, background_width, background_height,
Dragon_Resources.Foreground, Dragon_Resources.Background,
DefaultDepthOfScreen(XtScreen(Board)) );
if (pix == (Pixmap)0L) {
(void)fprintf( stderr, "Can't allocate background pixmap.\n" );
exit(1);
}
XSetWindowBackgroundPixmap( XtDisplay(Board), XtWindow(Board), pix );
}
/*--Now do the real thing. */
DEBUG_OTHER(main,XtMainLoop);
XtMainLoop();
} /* main */