Xlib和BadWindow

时间:2018-08-18 12:48:09

标签: c x11 xlib

我正在尝试将X11窗口用作抽象像素图持有人。它可以工作,但是在尝试关闭或取消映射窗口时出现BadWindow错误。当我仅调用XPutImage时,一切正常,仅当我尝试调用XUnmapWindow或XDestroyWindow时,问题才出现。我检查了XUnmapWindow和XDestroyWindow的显示和窗口指针是否正确,但仍无法正常工作。模块代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include "alienModuleApi.h"

#define MOD_PRIV(p) ((struct modulePrivate*)((p)->modulePrivate))

struct nativeBuffer {
    Window window;
    XShmSegmentInfo shminfo;
    bool shmattached;
    XImage *image;
};

struct modulePrivate {
    bool initialized;

    Display *display;
    Visual *visual;

    pthread_t thread_id;
    volatile int thread_running;
};

static bool threadJob(alienDriverPrivatePtr drvPriv){
    struct modulePrivate* modPriv;
    int8_t curMode;
    printf("Thread job started started\n");
    modPriv = drvPriv->modulePrivate;

    while (modPriv->thread_running) {
        XEvent ev;

        if (XPending(modPriv->display)) {
            XNextEvent(modPriv->display, &ev);
            switch(ev.type) {
                case Expose:        break;
                case ButtonPress:
                case KeyPress:      exit(0); break;
            }
        }
    }
}

bool screenInitialize(alienDriverPrivatePtr drvPriv){
    struct modulePrivate* modPriv;
    int8_t curMode;
    printf("Screen initializing started\n");
    modPriv = drvPriv->modulePrivate = malloc (sizeof(struct modulePrivate));

    putenv("DISPLAY=:0");

    if(!XInitThreads()) {
        printf("XInitThreads() failed\n");
        return 0;
    }

    modPriv->display=XOpenDisplay(NULL);
    modPriv->visual=DefaultVisual(modPriv->display, 0);
    if(modPriv->visual->class!=TrueColor)
    {
        fprintf(stderr, "Cannot handle non true color visual ...\n");
        return false;
    }

    drvPriv->randr.modelistClear(drvPriv);
    curMode = drvPriv->randr.modelistAddMode(drvPriv, 512, 512);
    drvPriv->randr.modelistAddMode(drvPriv, 800, 600);

    drvPriv->randr.modeSet(drvPriv, curMode);

    modPriv->initialized = 1;

    modPriv->thread_running = 1;
    pthread_create(&modPriv->thread_id, NULL, threadJob, drvPriv);
    printf("Screen initializing finished\n");
    return true;
}

alienModuleNativeBuffer screenCreatePixmap(alienDriverPrivatePtr drvPriv, uint16_t width, uint16_t height){
    DBG;struct modulePrivate* modPriv = MOD_PRIV(drvPriv);
    printf("Creating %dx%d window\n", width, height);
    struct nativeBuffer *buf = malloc (sizeof (struct nativeBuffer));
    XLockDisplay(modPriv->display);
    buf->window=XCreateSimpleWindow(modPriv->display, RootWindow(modPriv->display, 0), 0, 0, width, height, 1, 0, 0);
    XSelectInput(modPriv->display, buf->window, ButtonPressMask|ExposureMask|KeyPressMask);
    XMapWindow(modPriv->display, buf->window);

    buf->image = XShmCreateImage(   modPriv->display, 
                                    modPriv->visual, 24, ZPixmap, 0, 
                                    &buf->shminfo, width, height
                                );

    buf->shminfo.shmid = shmget(IPC_PRIVATE, buf->image->bytes_per_line * buf->image->height,IPC_CREAT|0777);
    buf->shminfo.shmaddr = buf->image->data = shmat(buf->shminfo.shmid, 0, 0);
    buf->shminfo.readOnly = False;

    XShmAttach(modPriv->display, &buf->shminfo);

    XUnlockDisplay(modPriv->display);
    printf("display is %p\n", modPriv->display);
    printf("Created window is %p\n", buf->window);

    return (alienModuleNativeBuffer) buf;
}

void *screenLockPixmap(alienDriverPrivatePtr drvPriv, alienModuleNativeBuffer buffer){
    struct modulePrivate* modPriv = MOD_PRIV(drvPriv);
    struct nativeBuffer *buf = buffer;

    if (buf) return buf->image->data;

    /* Should never reach this */
    return NULL;
}

void screenUnlockPixmap(alienDriverPrivatePtr drvPriv, alienModuleNativeBuffer buffer){
    return;
}

void screenDestroyPixmap(alienDriverPrivatePtr drvPriv, void *buf){
    struct modulePrivate* modPriv = MOD_PRIV(drvPriv);

    if (!buf)
        return;

    struct nativeBuffer *buffer = buf;

    XLockDisplay(modPriv->display);
    if (buffer && buffer->shmattached) {
        XShmDetach(modPriv->display, &buffer->shminfo);
        buffer->shmattached = 0;
    }
    shmdt(buffer->shminfo.shmaddr);
    XFlush(modPriv->display);
    XUnmapWindow(modPriv->display, buffer->window);
    XDestroyWindow(modPriv->display, buffer->window);
    XUnlockDisplay(modPriv->display);
    free(buffer);
}

void screenImageUpdate(alienDriverPrivatePtr drvPriv, alienModuleNativeBuffer buffer){
    struct modulePrivate* modPriv = MOD_PRIV(drvPriv);
    struct nativeBuffer *buf = buffer;

    XPutImage(modPriv->display, buf->window, DefaultGC(modPriv->display, 0), buf->image, 0, 0, 0, 0, buf->image->width, buf->image->height);
}

void screenFinalize(alienDriverPrivatePtr drvPriv){ 
    printf("Screen finalizing\n");
    struct modulePrivate* modPriv = MOD_PRIV(drvPriv);

    if (!modPriv->initialized) return;

    //screenDestroyPixmap(drvPriv, modPriv->image->data);
    modPriv->thread_running = 0;
    XCloseDisplay(modPriv->display);
    free(modPriv);
    printf("Screen finalized\n");
}

alienModuleFunctions_t moduleFuncs = {
    .screen.initialize = screenInitialize,
    .screen.createNativeBuffer = screenCreatePixmap,
    .screen.lockNativeBuffer = screenLockPixmap,
    .screen.unlockNativeBuffer = screenUnlockPixmap,
    .screen.releaseNativeBuffer = screenDestroyPixmap,
    .screen.imageUpdate = screenImageUpdate,
    .screen.finalize = screenFinalize,
};

alienModuleFunctionsPtr alienModuleInitialize (alienDriverPrivatePtr drvPriv){
    printf("Debuging X11 module loaded\n");
    return &moduleFuncs;
}

错误

X Error of failed request:  BadWindow (invalid Window parameter)
  Major opcode of failed request:  10 (X_UnmapWindow)
  Resource id in failed request:  0x4a00003
  Serial number of failed request:  7
  Current serial number in output stream:  12

1 个答案:

答案 0 :(得分:0)

将来,我相信您应该提供仅包含相关行而不是整个文件的简短代码段。它使其他人更容易发现问题或自己测试代码。

无论如何,如果未定义窗口(即已经销毁或从未创建),XUnmapWindow将抛出BadWindow。

我仔细检查了您的代码,如果我猜想我会说您两次致电screenDestroyPixmap(我看不到曾经在哪里呼叫screenDestroyPixmapmoduleFuncs.screen.releaseNativeBuffer,所以我无法确认)

请记住,XLib是异步的,因此在报告错误之后很可能会发生。有关解决方案,请参见XSync。另外,您可能需要考虑使用XCB;它的定义功能之一是能够检查您运行的每个X调用是否存在错误。它的另一项出色功能是使用XLib and XCB together的功能,因此您可以在选择呼叫时获得xcb的所有好处。

并没有真正回答您的问题,但是我希望我能提供帮助而不会太笼统。