⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uwm.c

📁 最简单的窗口管理器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Header: uwm.c  88/08/09 08:59:29 Xusr 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. *//* * MODIFICATION HISTORY * * 000 -- M. Gancarz, DEC Ultrix Engineering Group * 001 -- Loretta Guarino Reid, DEC Ultrix Engineering Group, *  Western Software Lab. Convert to X11. */#ifndef lintstatic char *sccsid = "%W%	%G%";#endif#include "uwm.h"#ifdef PROFIL#include <signal.h>/* * Dummy handler for profiling. */ptrap(){    exit(0);}#endif#define gray_width 16#define gray_height 16static unsigned char gray_bits[] = {   0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,   0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,   0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,   0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa};Bool NeedRootInput=FALSE;Bool ChkMline();char *sfilename;extern FILE *yyin;/* * Main program. */main(argc, argv, environ)int argc;char **argv;char **environ;{    int hi;			/* Button event high detail. */    int lo;			/* Button event low detail. */    int x, y;                   /* Mouse X and Y coordinates. */    int root_x, root_y;         /* Mouse root X and Y coordinates. */    int cur_x, cur_y;		/* Current mouse X and Y coordinates. */    int down_x, down_y;		/* mouse X and Y at ButtonPress. */    int str_width;              /* Width in pixels of output string. */    int pop_width, pop_height;  /* Pop up window width and height. */    int context;		/* Root, window, or icon context. */    unsigned int ptrmask;		/* for QueryPointer */    Bool func_stat;		/* If true, function swallowed a ButtonUp. */    Bool delta_done;		/* If true, then delta functions are done. */    Bool local;			/* If true, then do not use system defaults. */    register Binding *bptr;	/* Pointer to Bindings list. */    char *root_name;		/* Root window name. */    char *display = NULL;	/* Display name pointer. */    char message[128];		/* Error message buffer. */    char *rc_file;		/* Pointer to $HOME/.uwmrc. */    Window event_win;           /* Event window. */    Window sub_win;		/* Subwindow for XUpdateMouse calls. */    Window root;		/* Root window for QueryPointer. */    XWindowAttributes event_info;	/* Event window info. */    XEvent button_event; 	 /* Button input event. */    GC gc;			/* graphics context for gray background */    XImage grayimage;		/* for gray background */    XGCValues xgc;		/* to create font GCs */    char *malloc();    Bool fallbackMFont = False,	/* using default GC font for menus, */         fallbackPFont = False,	/* popups, */         fallbackIFont = False;	/* icons */    XSetWindowAttributes attr;	/* for setting save under flag */#ifdef PROFIL    signal(SIGTERM, ptrap);#endif    /*     * Set up internal defaults.     */    SetVarDefaults();    /*      * Parse the command line arguments.     */    Argv = argv;    Environ = environ;    argc--, argv++;    while (argc) {        if (**argv == '-') {            if (strcmp(*argv, "-display") == 0 || strcmp(*argv, "-d") == 0) {		argc--, argv++;		if (argc <= 0)		    Usage ();		display = *argv;	    } else if (strcmp(*argv, "-f") == 0) {                argc--, argv++;                if ((argc == 0) || (Startup_File[0] != '\0'))                    Usage();                strncpy(Startup_File, *argv, NAME_LEN);            } else if (strcmp(*argv, "-b") == 0)                local = TRUE;            else 		Usage();        } else	  Usage ();	argc--, argv++;    }    /*     * Initialize the default bindings.     */    if (!local)        InitBindings();    /*     * Read in and parse $HOME/.uwmrc, if it exists.     */    sfilename = rc_file = malloc(NAME_LEN);    sprintf(rc_file, "%s/.uwmrc", getenv("HOME"));    if ((yyin = fopen(rc_file, "r")) != NULL) {        Lineno = 1;        yyparse();        fclose(yyin);        if (Startup_File_Error)            Error("Bad .uwmrc file...aborting");    }    /*      * Read in and parse the startup file from the command line, if     * specified.     */    if (Startup_File[0] != '\0') {        sfilename = Startup_File;        if ((yyin = fopen(Startup_File, "r")) == NULL) {    	sprintf(message, "Cannot open startup file '%s'", Startup_File);            Error(message);        }        Lineno = 1;        yyparse();        fclose(yyin);        if (Startup_File_Error)            Error("Bad startup file...aborting");    }    /*     * Verify the menu bindings.     */    VerifyMenuBindings();    if (Startup_File_Error)        Error("Bad startup file...aborting");    /*     * Verify the menu variables.     */    VerifyMenuVariables();    /*      * Open the display.     */    if ((dpy = XOpenDisplay(display)) == NULL)        Error("Unable to open display");    scr = DefaultScreen(dpy); /*    XSynchronize(dpy, 1); */    /*     * Set XErrorFunction to be non-terminating.     */    XSetErrorHandler(XError);    /*     * Force child processes to disinherit the TCP file descriptor.     * This helps shell commands forked and exec'ed from menus     * to work properly.     */    if ((status = fcntl(ConnectionNumber(dpy), F_SETFD, 1)) == -1) {        perror("uwm: child cannot disinherit TCP fd");        Error("TCP file descriptor problems");    }    /*     * If the root window has not been named, name it.     */    status = XFetchName(dpy, RootWindow(dpy, scr), &root_name);    if (root_name == NULL)     	XStoreName(dpy, RootWindow(dpy, scr), " X Root Window ");    else free(root_name);    ScreenHeight = DisplayHeight(dpy, scr);    ScreenWidth = DisplayWidth(dpy, scr);    /*     * Create and store the icon background pixmap.     */    GrayPixmap = (Pixmap)XCreatePixmap(dpy, RootWindow(dpy, scr),     	gray_width, gray_height, DefaultDepth(dpy,scr));    xgc.foreground = BlackPixel(dpy, scr);    xgc.background = WhitePixel(dpy, scr);    gc = XCreateGC(dpy, GrayPixmap, GCForeground+GCBackground, &xgc);    grayimage.height = gray_width;    grayimage.width = gray_height;    grayimage.xoffset = 0;    grayimage.format = XYBitmap;    grayimage.data = (char *)gray_bits;    grayimage.byte_order = LSBFirst;    grayimage.bitmap_unit = 8;    grayimage.bitmap_bit_order = LSBFirst;    grayimage.bitmap_pad = 16;    grayimage.bytes_per_line = 2;    grayimage.depth = 1;    XPutImage(dpy, GrayPixmap, gc, &grayimage, 0, 0,	 0, 0, gray_width, gray_height);    XFreeGC(dpy, gc);        /*     * Set up icon window, icon cursor and pop-up window color parameters.     */    if (Reverse) {        IBorder = WhitePixel(dpy, scr);        IBackground =  GrayPixmap;        ITextForground = WhitePixel(dpy, scr);        ITextBackground = BlackPixel(dpy, scr);        PBorder = BlackPixel(dpy, scr);        PBackground = WhitePixel(dpy, scr);        PTextForground = BlackPixel(dpy, scr);        PTextBackground = WhitePixel(dpy, scr);        MBorder = WhitePixel(dpy, scr);        MBackground = BlackPixel(dpy, scr);        MTextForground = WhitePixel(dpy, scr);        MTextBackground = BlackPixel(dpy, scr);    }    else {        IBorder = BlackPixel(dpy, scr);        IBackground = GrayPixmap;        ITextForground = BlackPixel(dpy, scr);        ITextBackground = WhitePixel(dpy, scr);        PBorder = WhitePixel(dpy, scr);        PBackground = BlackPixel(dpy, scr);        PTextForground = WhitePixel(dpy, scr);        PTextBackground = BlackPixel(dpy, scr);        MBorder = BlackPixel(dpy, scr);        MBackground = WhitePixel(dpy, scr);        MTextForground = BlackPixel(dpy, scr);        MTextBackground = WhitePixel(dpy, scr);    }    /*     * Store all the cursors.     */    StoreCursors();    /*      * grab the mouse buttons according to the map structure     */    Grab_Buttons();    /*     * Set initial focus to PointerRoot.     */    XSetInputFocus(dpy, PointerRoot, None, CurrentTime);    /*      * watch for initial window mapping and window destruction     */    XSelectInput(dpy, RootWindow(dpy, scr),       SubstructureNotifyMask|SubstructureRedirectMask|FocusChangeMask|      (NeedRootInput ? EVENTMASK|OwnerGrabButtonMask : 0));    /*     * Retrieve the information structure for the specifed fonts and     * set the global font information pointers.     */    IFontInfo = XLoadQueryFont(dpy, IFontName);    if (IFontInfo == NULL) {        fprintf(stderr, "uwm: Unable to open icon font '%s', using server default.\n",                IFontName);		IFontInfo = XQueryFont(dpy,XGContextFromGC(DefaultGC(dpy, scr)));	fallbackIFont = True;    }    PFontInfo = XLoadQueryFont(dpy, PFontName);    if (PFontInfo == NULL) {        fprintf(stderr, "uwm: Unable to open resize font '%s', using server default.\n",                PFontName);	if (fallbackIFont)	    PFontInfo = IFontInfo;	else	    PFontInfo = XQueryFont(dpy,XGContextFromGC(DefaultGC(dpy, scr)));	fallbackPFont = True;    }    MFontInfo = XLoadQueryFont(dpy, MFontName);    if (MFontInfo == NULL) {        fprintf(stderr, "uwm: Unable to open menu font '%s', using server default.\n",                MFontName);	if (fallbackIFont || fallbackPFont)	    MFontInfo = fallbackPFont ? PFontInfo : IFontInfo;	else	    MFontInfo = XQueryFont(dpy,XGContextFromGC(DefaultGC(dpy, scr)));	fallbackMFont = True;    }    /*     * Calculate size of the resize pop-up window.     */    str_width = XTextWidth(PFontInfo, PText, strlen(PText));    pop_width = str_width + (PPadding << 1);    PWidth = pop_width + (PBorderWidth << 1);    pop_height = PFontInfo->ascent + PFontInfo->descent + (PPadding << 1);    PHeight = pop_height + (PBorderWidth << 1);    /*     * Create the pop-up window.  Create it at (0, 0) for now.  We will     * move it where we want later.     */    Pop = XCreateSimpleWindow(dpy, RootWindow(dpy, scr),                        0, 0,                        pop_width, pop_height,                        PBorderWidth,                        PBorder, PBackground);    if (Pop == FAILURE) Error("Can't create pop-up dimension display window.");    attr.save_under = True;    XChangeWindowAttributes (dpy, Pop, CWSaveUnder, &attr);    /*     * Create the menus for later use.     */    CreateMenus();    /*     * Create graphics context.     */    xgc.foreground = ITextForground;    xgc.background = ITextBackground;    xgc.font = IFontInfo->fid;    xgc.graphics_exposures = FALSE;    IconGC = XCreateGC(dpy,     	RootWindow(dpy, scr),	GCForeground+GCBackground+GCGraphicsExposures		       +(fallbackIFont ? 0 : GCFont), &xgc);    xgc.foreground = MTextForground;    xgc.background = MTextBackground;    xgc.font = MFontInfo->fid;    MenuGC = XCreateGC(dpy,     	RootWindow(dpy, scr),	GCForeground+GCBackground+(fallbackMFont ? 0 : GCFont), &xgc);    xgc.function = GXinvert;    xgc.plane_mask = MTextForground ^ MTextBackground;    MenuInvGC = XCreateGC(dpy,     	RootWindow(dpy, scr), GCForeground+GCFunction+GCPlaneMask, &xgc);    xgc.foreground = PTextForground;    xgc.background = PTextBackground;    xgc.font = PFontInfo->fid;    PopGC = XCreateGC(dpy,     	RootWindow(dpy, scr),	GCForeground+GCBackground+(fallbackPFont ? 0 : GCFont), &xgc);    xgc.line_width = DRAW_WIDTH;    xgc.foreground = DRAW_VALUE;    xgc.function = DRAW_FUNC;    xgc.subwindow_mode = IncludeInferiors;    DrawGC = XCreateGC(dpy, RootWindow(dpy, scr),       GCLineWidth+GCForeground+GCFunction+GCSubwindowMode, &xgc);    /*     * Tell the user we're alive and well.     */    XBell(dpy, VOLUME_PERCENTAGE(Volume));    /*      * Main command loop.     */    while (TRUE) {        delta_done = func_stat = FALSE;        /*         * Get the next mouse button event.  Spin our wheels until         * a ButtonPressed event is returned.         * Note that mouse events within an icon window are handled         * in the "GetButton" function or by the icon's owner if         * it is not uwm.         */        while (TRUE) {            if (!GetButton(&button_event)) continue;            if (button_event.type == ButtonPress) break;        }	/* save mouse coords in case we want them later for a delta action */	down_x = ((XButtonPressedEvent *)&button_event)->x;	down_y = ((XButtonPressedEvent *)&button_event)->y;        /*         * Okay, determine the event window and mouse coordinates.         */        status = XTranslateCoordinates(dpy, 				    RootWindow(dpy, scr), RootWindow(dpy, scr),                                    ((XButtonPressedEvent *)&button_event)->x, 				    ((XButtonPressedEvent *)&button_event)->y,                                    &x, &y,                                    &event_win);        if (status == FAILURE) continue;        /*         * Determine the event window and context.         */        if (event_win == 0) {                event_win = RootWindow(dpy, scr);                context = ROOT;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -