📄 xlib.c
字号:
/* * XaoS, a fast portable realtime fractal zoomer * Copyright (C) 1996,1997 by * * Jan Hubicka (hubicka@paru.cas.cz) * Thomas Marsh (tmarsh@austin.ibm.com) * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "aconfig.h"#ifdef X11_DRIVER#include <X11/Xlib.h>#include <X11/Xutil.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <config.h>#ifndef NO_MALLOC_H#include <malloc.h>#endif#include "xlib.h"#ifdef AMIGA#define XFlush(x) while(0)#endif#define chkalloc(n) if (!n) fprintf(stderr, "out of memory\n"), exit(-1)extern int prog_argc;extern char **prog_argv;Atom wmDeleteWindow;intxupdate_size (xdisplay * d){ int tmp; Window wtmp; unsigned int width = d->width, height = d->height; XSync (d->display, False); XGetGeometry (d->display, d->window, &wtmp, &tmp, &tmp, &d->width, &d->height, (unsigned int *) &tmp, (unsigned int *) &tmp); if (d->width != width || d->height != height) return 1; return 0;}voidxflip_buffers (xdisplay * d){ d->back = d->vbuffs[d->current]; d->current ^= 1; d->vbuff = d->vbuffs[d->current];}voiddraw_screen (xdisplay * d){#ifdef MITSHM if (d->SharedMemFlag) { XShmPutImage (d->display, d->window, d->gc, d->image[d->current], 0, 0, 0, 0, d->bwidth, d->bheight, True); } else#endif XPutImage (d->display, d->window, d->gc, d->image[d->current], 0, 0, 0, 0, d->bwidth, d->bheight);/*XFlush(d->display); *//*gives small rest to X but degrades perofrmance too much */ d->screen_changed = 0;}#ifdef MITSHMintalloc_shm_image (xdisplay * new){ register char *ptr; int temp, size = 0, i; ptr = DisplayString (new->display); if (!ptr || (*ptr == ':') || !strncmp (ptr, "localhost:", 10) || !strncmp (ptr, "unix:", 5) || !strncmp (ptr, "local:", 6)) { new->SharedMemOption = XQueryExtension (new->display, "MIT-SHM", &temp, &temp, &temp); } else { new->SharedMemOption = False; return 0; } new->SharedMemFlag = False; if (new->SharedMemFlag) { XShmDetach (new->display, &new->xshminfo[0]); XShmDetach (new->display, &new->xshminfo[1]); new->image[0]->data = (char *) NULL; new->image[1]->data = (char *) NULL; shmdt (new->xshminfo[0].shmaddr); shmdt (new->xshminfo[1].shmaddr); } for (i = 0; i < 2; i++) { if (new->SharedMemOption) { new->SharedMemFlag = False; new->image[i] = XShmCreateImage (new->display, new->visual, new->depth, new->depth == 1 ? XYBitmap : ZPixmap, NULL, &new->xshminfo[i], new->width, new->height); if (new->image[i]) { temp = new->image[i]->bytes_per_line * (new->image[i]->height + 150); new->linewidth = new->image[i]->bytes_per_line; if (temp > size) size = temp; new->xshminfo[i].shmid = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777); if (new->xshminfo[i].shmid != -1) { errno = 0; new->xshminfo[i].shmaddr = (char *) shmat (new->xshminfo[i].shmid, 0, 0); if (!errno) { new->image[i]->data = new->xshminfo[i].shmaddr; new->data[i] = new->vbuffs[i] = (char *) new->image[i]->data; new->xshminfo[i].readOnly = True; new->SharedMemFlag = XShmAttach (new->display, &new->xshminfo[i]); XSync (new->display, False); if (!new->SharedMemFlag) { XDestroyImage (new->image[i]); new->image[i] = (XImage *) NULL; new->SharedMemFlag = 0; return 0; } } /* Always Destroy Shared Memory Ident */ shmctl (new->xshminfo[i].shmid, IPC_RMID, 0); } if (!new->SharedMemFlag) { XDestroyImage (new->image[i]); new->image[i] = (XImage *) NULL; new->SharedMemFlag = 0; return 0; } } else { new->SharedMemFlag = 0; return 0; } } else { new->SharedMemFlag = 0; return 0; } } new->current = 0; xflip_buffers (new); return 1;}voidfree_shm_image (xdisplay * d){ if (d->SharedMemFlag) { XDestroyImage (d->image[0]); XDestroyImage (d->image[1]); XShmDetach (d->display, &d->xshminfo[0]); XShmDetach (d->display, &d->xshminfo[1]); shmdt (d->xshminfo[0].shmaddr); shmdt (d->xshminfo[1].shmaddr); }}#endifintalloc_image (xdisplay * d){ int i; d->bwidth = d->width; d->bheight = d->height;#ifdef MITSHM if (!d->params->nomitshm && d->depth != 1 && alloc_shm_image (d)) { return 1; }#endif for (i = 0; i < 2; i++) { d->image[i] = XCreateImage (d->display, d->visual, d->depth, d->depth == 1 ? XYBitmap : ZPixmap, 0, NULL, d->width, d->height, 32, 0); if (d->image[i] == NULL) { printf ("Out of memory for image..exiting\n"); exit (-1); } d->image[i]->data = malloc (d->image[i]->bytes_per_line * d->height); if (d->image[i]->data == NULL) { printf ("Out of memory for image buffers..exiting\n"); exit (-1); } { unsigned char c[4]; int byteexact = 0; *(unsigned short *) c = 0xff; if ((!(d->image[i]->red_mask & ~0xffU) || !(d->image[i]->red_mask & ~0xff00U) || !(d->image[i]->red_mask & ~0xff0000U) || !(d->image[i]->red_mask & ~0xff000000U)) && (!(d->image[i]->green_mask & ~0xffU) || !(d->image[i]->green_mask & ~0xff00U) || !(d->image[i]->green_mask & ~0xff0000U) || !(d->image[i]->green_mask & ~0xff000000U)) && (!(d->image[i]->blue_mask & ~0xffU) || !(d->image[i]->blue_mask & ~0xff00U) || !(d->image[i]->blue_mask & ~0xff0000U) || !(d->image[i]->blue_mask & ~0xff000000U))) byteexact = 1; if (!byteexact) { /*Make endianity correct */ if (c[0] == (unsigned char) 0xff) { if (d->image[i]->byte_order != LSBFirst) { d->image[i]->byte_order = LSBFirst; /*XInitImage(d->image[i]); */ } } else { if (d->image[i]->byte_order != MSBFirst) { d->image[i]->byte_order = MSBFirst; /*XInitImage(d->image[i]); */ } } } } d->data[i] = d->vbuffs[i] = (char *) d->image[i]->data; d->linewidth = d->image[i]->bytes_per_line; } xflip_buffers (d); return 1;}voidfree_image (xdisplay * d){#ifdef MITSHM if (d->SharedMemFlag) { free_shm_image (d); return; }#endif XDestroyImage (d->image[0]); XDestroyImage (d->image[1]);}#define MAX(x,y) ((x)>(y)?(x):(y))xdisplay *xalloc_display (CONST char *s, int x, int y, xlibparam * params){ xdisplay *new; Visual *defaultvisual; XVisualInfo vis; int found; int i; XClassHint classHint; XWMHints *hints; char **faked_argv; new = (xdisplay *) calloc (sizeof (xdisplay), 1); chkalloc (new); new->display = XOpenDisplay (params->display); if (!new->display) { free ((void *) new); return NULL; } new->screen = DefaultScreen (new->display); new->attributes = (XSetWindowAttributes *) malloc (sizeof (XSetWindowAttributes)); chkalloc (new->attributes); new->attributes->background_pixel = BlackPixel (new->display, new->screen); new->attributes->border_pixel = BlackPixel (new->display, new->screen); new->attributes->event_mask = ButtonPressMask | StructureNotifyMask | ButtonReleaseMask | PointerMotionMask | KeyPressMask | ExposureMask | KeyReleaseMask; new->attr_mask = CWBackPixel | CWEventMask; if (params->fullscreen || params->rootwindow) { new->attributes->override_redirect = True; new->attr_mask |= CWOverrideRedirect; } else new->attr_mask |= CWBorderPixel; new->class = InputOutput; new->xcolor.n = 0; new->parent_window = RootWindow (new->display, new->screen); defaultvisual = DefaultVisual (new->display, new->screen); new->params = params; new->visual = defaultvisual; vis.depth = new->depth = DefaultDepth (new->display, new->screen); found = 0; for (i = 32; i > 13 && !found; i--) if (XMatchVisualInfo (new->display, new->screen, i, TrueColor, &vis)) { found = 1; } if (defaultvisual->class != StaticGray && defaultvisual->class != GrayScale) { for (i = 8; i && !found; i--) if (XMatchVisualInfo (new->display, new->screen, i, PseudoColor, &vis)) { found = 1; } for (i = 8; i && !found; i--) if (XMatchVisualInfo (new->display, new->screen, i, StaticColor, &vis)) { found = 1; } for (i = 8; i && !found; i--) if (XMatchVisualInfo (new->display, new->screen, i, TrueColor, &vis)) { found = 1; } } if (!found
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -