void InitOutput(ScreenInfo *screenInfo, int argc, char *argv[]) { int i, j; xnestOpenDisplay(argc, argv); screenInfo->imageByteOrder = ImageByteOrder(xnestDisplay); screenInfo->bitmapScanlineUnit = BitmapUnit(xnestDisplay); screenInfo->bitmapScanlinePad = BitmapPad(xnestDisplay); screenInfo->bitmapBitOrder = BitmapBitOrder(xnestDisplay); screenInfo->numPixmapFormats = 0; for (i = 0; i < xnestNumPixmapFormats; i++) for (j = 0; j < xnestNumDepths; j++) if ((xnestPixmapFormats[i].depth == 1) || (xnestPixmapFormats[i].depth == xnestDepths[j])) { screenInfo->formats[screenInfo->numPixmapFormats].depth = xnestPixmapFormats[i].depth; screenInfo->formats[screenInfo->numPixmapFormats].bitsPerPixel = xnestPixmapFormats[i].bits_per_pixel; screenInfo->formats[screenInfo->numPixmapFormats].scanlinePad = xnestPixmapFormats[i].scanline_pad; screenInfo->numPixmapFormats++; break; } xnestWindowPrivateIndex = AllocateWindowPrivateIndex(); xnestGCPrivateIndex = AllocateGCPrivateIndex(); xnestFontPrivateIndex = AllocateFontPrivateIndex(); if (!xnestNumScreens) xnestNumScreens = 1; for (i = 0; i < xnestNumScreens; i++) AddScreen(xnestOpenScreen, argc, argv); xnestNumScreens = screenInfo->numScreens; xnestDoFullGeneration = xnestFullGeneration; }
static void SetupImage (void) { TakedownImage(); #ifdef MITSHM GUI.use_shared_memory = TRUE; int major, minor; Bool shared; if (!XShmQueryVersion(GUI.display, &major, &minor, &shared) || !shared) GUI.image = NULL; else GUI.image = XShmCreateImage(GUI.display, GUI.visual, GUI.depth, ZPixmap, NULL, &GUI.sm_info, SNES_WIDTH * 2, SNES_HEIGHT_EXTENDED * 2); if (!GUI.image) GUI.use_shared_memory = FALSE; else { GUI.sm_info.shmid = shmget(IPC_PRIVATE, GUI.image->bytes_per_line * GUI.image->height, IPC_CREAT | 0777); if (GUI.sm_info.shmid < 0) { XDestroyImage(GUI.image); GUI.use_shared_memory = FALSE; } else { GUI.image->data = GUI.sm_info.shmaddr = (char *) shmat(GUI.sm_info.shmid, 0, 0); if (!GUI.image->data) { XDestroyImage(GUI.image); shmctl(GUI.sm_info.shmid, IPC_RMID, 0); GUI.use_shared_memory = FALSE; } else { GUI.sm_info.readOnly = False; XSetErrorHandler(ErrorHandler); XShmAttach(GUI.display, &GUI.sm_info); XSync(GUI.display, False); // X Error handler might clear GUI.use_shared_memory if XShmAttach failed. if (!GUI.use_shared_memory) { XDestroyImage(GUI.image); shmdt(GUI.sm_info.shmaddr); shmctl(GUI.sm_info.shmid, IPC_RMID, 0); } } } } if (!GUI.use_shared_memory) { fprintf(stderr, "use_shared_memory failed, switching to XPutImage.\n"); #endif GUI.image = XCreateImage(GUI.display, GUI.visual, GUI.depth, ZPixmap, 0, NULL, SNES_WIDTH * 2, SNES_HEIGHT_EXTENDED * 2, BitmapUnit(GUI.display), 0); GUI.image->data = (char *) malloc(GUI.image->bytes_per_line * GUI.image->height); if (!GUI.image || !GUI.image->data) FatalError("XCreateImage failed."); #ifdef MITSHM } #endif #ifdef LSB_FIRST GUI.image->byte_order = LSBFirst; #else GUI.image->byte_order = MSBFirst; #endif GFX.Pitch = SNES_WIDTH * 2 * 2; GUI.snes_buffer = (uint8 *) calloc(GFX.Pitch * ((SNES_HEIGHT_EXTENDED + 4) * 2), 1); if (!GUI.snes_buffer) FatalError("Failed to allocate GUI.snes_buffer."); GFX.Screen = (uint16 *) (GUI.snes_buffer + (GFX.Pitch * 2 * 2)); GUI.filter_buffer = (uint8 *) calloc((SNES_WIDTH * 2) * 2 * (SNES_HEIGHT_EXTENDED * 2), 1); if (!GUI.filter_buffer) FatalError("Failed to allocate GUI.filter_buffer."); if (GUI.depth == 15 || GUI.depth == 16) { GUI.blit_screen_pitch = GUI.image->bytes_per_line; GUI.blit_screen = (uint8 *) GUI.image->data; GUI.need_convert = FALSE; } else { GUI.blit_screen_pitch = (SNES_WIDTH * 2) * 2; GUI.blit_screen = GUI.filter_buffer; GUI.need_convert = TRUE; } S9xGraphicsInit(); }
int main(int argc, char **argv) { Atom atomWMDeleteWindow; int screenNumber; Screen *screen; Window window; XWindowAttributes windowAttributes; Colormap colormap; PaletteInfo paletteInfo; Image image; XImage *xImage; int x, y; int captureFrame; XEvent event; bool sizeChanged; // ProgramExit initialization display = NULL; v4l = -1; captureBuf = NULL; on_exit(ProgramExit, NULL); // Get command line options magnification = 1; if (argc > 1) { magnification = atoi(argv[1]); } // end if magnification = max(1, magnification); printf("Magnification is %i\n", magnification); // Open display if ((display = XOpenDisplay(NULL)) == NULL) { // NULL for DISPLAY printf("Error: XOpenDisplay() failed\n"); exit(1); } // end if screenNumber = DefaultScreen(display); screen = XScreenOfDisplay(display, screenNumber); // Obtain WM protocols atom for ClientMessage exit event if ((atomWMDeleteWindow = XInternAtom(display, AtomWMDeleteWindowName, True)) == None) { printf("Error: %s atom does not exist\n", AtomWMDeleteWindowName); exit(1); } // end if // Create window, inheriting depth and visual from root window window = XCreateSimpleWindow( display, RootWindowOfScreen(screen), 0, // x 0, // y 640, // width 480, // height 0, // border width BlackPixelOfScreen(screen), // border BlackPixelOfScreen(screen) // background ); XStoreName(display, window, "V4L RGB Test"); XGetWindowAttributes(display, window, &windowAttributes); if (((windowAttributes.depth == 8) && (windowAttributes.visual->c_class != PseudoColor)) || ((windowAttributes.depth > 8) && (windowAttributes.visual->c_class != TrueColor))) { printf("Error: Visual not supported\n"); exit(1); } // end if // Create PseudoColor HI240 colormap, if needed if (windowAttributes.depth == 8) { colormap = XCreateColormap(display, window, windowAttributes.visual, AllocAll); paletteInfo.display = display; paletteInfo.colormap = colormap; Hi240BuildPalette((ulong) 0x10000, (Hi240StorePaletteEntry *) StoreColormapEntry, &paletteInfo); XSetWindowColormap(display, window, colormap); } // end if // Create image if (image.Create( display, window, // Defines visual, depth MaxImageWidth, MaxImageHeight, True // MITSHM ) < 0) { printf("Error: image.Create() failed\n"); exit(1); } // end if image.Clear(); #if (1) printf("\nDisplay:\n"); printf("Image byte order = %s\n", ByteOrderName(ImageByteOrder(display))); printf("Bitmap unit = %i\n", BitmapUnit(display)); printf("Bitmap bit order = %s\n", ByteOrderName(BitmapBitOrder(display))); printf("Bitmap pad = %i\n", BitmapPad(display)); printf("\nWindow:\n"); printf("Depth = %i\n", windowAttributes.depth); printf("Visual ID = 0x%02x\n", windowAttributes.visual->visualid); printf("Visual class = %s\n", VisualClassName(windowAttributes.visual->c_class)); printf("Red mask = 0x%08lx\n", windowAttributes.visual->red_mask); printf("Green mask = 0x%08lx\n", windowAttributes.visual->green_mask); printf("Blue mask = 0x%08lx\n", windowAttributes.visual->blue_mask); printf("Bits per R/G/B = %i\n", windowAttributes.visual->bits_per_rgb); // log2 # colors xImage = image.X(); printf("\nImage:\n"); printf("Image byte order = %s\n", ByteOrderName(xImage->byte_order)); printf("Bitmap unit = %i\n", xImage->bitmap_unit); printf("Bitmap bit order = %s\n", ByteOrderName(xImage->bitmap_bit_order)); printf("Bitmap pad = %i\n", xImage->bitmap_pad); printf("Depth = %i\n", xImage->depth); printf("Red mask = 0x%08lx\n", xImage->red_mask); printf("Green mask = 0x%08lx\n", xImage->green_mask); printf("Blue mask = 0x%08lx\n", xImage->blue_mask); printf("Bits per pixel = %i\n", xImage->bits_per_pixel); // ZPixmap printf("Bytes per line = %i\n", xImage->bytes_per_line); printf("IsShared = %s\n", image.IsShared() ? "True" : "False"); printf("HasSharedPixmap = %s\n", image.HasSharedPixmap() ? "True" : "False"); #endif // V4L stuff if ((v4l = open(BigPictureDevice, O_RDWR)) < 0) { printf("Error: Can't open %s: %s\n", BigPictureDevice, strerror(errno)); exit(1); } // end if if (V4LMGetMMInfo(v4l, &v4lMMInfo) < 0) { printf("Error: V4LMGetMMInfo: %s\n", strerror(errno)); exit(1); } // end if #if (0) printf("Capture buffer size = %i\n", v4lMMInfo.size); printf("Capture buffer frames = %i\n", v4lMMInfo.frames); #endif if (v4lMMInfo.frames < 2) { printf("Error: V4LMGetMMInfo: frames < 2\n"); exit(1); } // end if if ((captureBuf = (bits8 *) mmap(0, v4lMMInfo.size, PROT_READ | PROT_WRITE, MAP_SHARED, v4l, 0)) == MAP_FAILED) { printf("Error: mmap(): %s\n", strerror(errno)); exit(1); } // end if if (V4LSetSource(v4l, BigPictureCompositeSource, VIDEO_MODE_NTSC) < 0) { printf("Error: V4LSetSource: %s\n", strerror(errno)); exit(1); } // end if if (V4LGetCaps(v4l, &v4lCaps) < 0) { printf("Error: V4LGetCaps: %s\n", strerror(errno)); exit(1); } // end if // Select V4L RGB capture format to exactly match image/visual (no LUTs!) if ((captureFormat = XImageCaptureFormat(image.X())) < 0) { printf("Error: No match for visual/image\n"); exit(1); } // end if // Initialize capture size based on window size windowWidth = windowAttributes.width; windowHeight = windowAttributes.height;; WindowResize(v4l, windowWidth, windowHeight, magnification); // Does V4LMSetFormat(). // Initialize picture attributes to mid-range V4LSetBrightness(v4l, 65535 / 2); V4LSetContrast(v4l, 65535 / 2); V4LSetSaturation(v4l, 65535 / 2); V4LSetHue(v4l, 65535 / 2); // Ready to start: Display window, select events, and initiate capture sequence XMapRaised(display, window); XSetWMProtocols(display, window, &atomWMDeleteWindow, 1); XSelectInput(display, window, StructureNotifyMask | ExposureMask); captureFrame = 0; if (V4LMCapture(v4l, captureFrame) < 0) { printf("Error: V4LMCapture: %s\n", strerror(errno)); exit(1); } // end if while (1) { if (XPending(display) > 0) { XNextEvent(display, &event); switch (event.type) { case ClientMessage: // From WM if (event.xclient.data.l[0] == atomWMDeleteWindow) { exit(0); } // end if break; case ConfigureNotify: sizeChanged = false; if (event.xconfigure.width != windowWidth) { sizeChanged = true; windowWidth = event.xconfigure.width; } // end if if (event.xconfigure.height != windowHeight) { sizeChanged = true; windowHeight = event.xconfigure.height; } // end if if (sizeChanged) { image.Clear(); XClearWindow(display, window); WindowResize(v4l, windowWidth, windowHeight, magnification); } // end if break; case Expose: if (event.xexpose.count == 0) { Put(window, image); } // end if break; } // end switch } else { // Wait for this frame if (V4LMSync(v4l, captureFrame) < 0) { printf("Error: V4LMSync: %s\n", strerror(errno)); exit(1); } // end if // Start capture for next frame if (V4LMCapture(v4l, 1 - captureFrame) < 0) { printf("Error: V4LMCapture: %s\n", strerror(errno)); exit(1); } // end if Draw(image, captureBuf + v4lMMInfo.offsets[captureFrame], magnification); Put(window, image); captureFrame = 1 - captureFrame; // 0<->1 } // endif } // end while printf("Error: Fell out of event loop!\n"); exit(1); } // end main
int XBitmapUnit(Display *dpy) { return (BitmapUnit(dpy)); }
uint2 MCScreenDC::getunit() { return BitmapUnit(dpy); }