我正在用C编写我的第一个X11应用程序,在尝试检索应用程序窗口的大小时遇到了一个主要问题。
temp.c:30:2 warning: passing argument 3 of 'XGetGeometry' makes pointer from integer without a cast
我知道这只是一个警告,但它仍然会导致segfault,这并不好玩这是我的代码:

static void loop();
static void initialize();
static void cleanUp();
static void run();

/* Variables */

static int screenNumber;
unsigned long White;
unsigned long Black;
long eventMask;
static Display *currentDisplay;
static Window currentWindow;
static unsigned int windowWidth;
static unsigned int windowHeight;
static GC graphicsController;
static XEvent XEvents;

void loop() {
        XGetGeometry(currentDisplay, currentWindow, DefaultRootWindow(currentDisplay), NULL, NULL, &windowWidth, &windowHeight, NULL, NULL);

        XDrawLine(currentDisplay, currentWindow, graphicsController, 0, 0, (int)windowWidth, (int)windowHeight);
        XDrawLine(currentDisplay, currentWindow, graphicsController, 0, (int)windowHeight, (int)windowWidth, 0);
}

void initialize() {
        currentDisplay = XOpenDisplay(NULL);
        screenNumber = DefaultScreen(currentDisplay);

        White = WhitePixel(currentDisplay, screenNumber);
        Black = BlackPixel(currentDisplay, screenNumber);

        currentWindow = XCreateSimpleWindow(    currentDisplay,
                                                DefaultRootWindow(currentDisplay),
                                                0, 0,
                                                500, 500,
                                                0, Black,
                                                White);

        XMapWindow(currentDisplay, currentWindow);
        XStoreName(currentDisplay, currentWindow, "rGot - X11");

        eventMask = StructureNotifyMask;
        XSelectInput(currentDisplay, currentWindow, eventMask);

        do{
                XNextEvent(currentDisplay, &XEvents);
        }while(XEvents.type != MapNotify);

        graphicsController = XCreateGC(currentDisplay, currentWindow, 0, NULL);

        XSetForeground(currentDisplay, graphicsController, Black);
}

void run() {
        eventMask = ButtonPressMask|ButtonReleaseMask;
        XSelectInput(currentDisplay, currentWindow, eventMask);
        do{
                XNextEvent(currentDisplay, &XEvents);
                loop();
        }while(1==1);
}

void cleanUp() {
        XDestroyWindow(currentDisplay, currentWindow);
        XCloseDisplay(currentDisplay);
}

int main(){
        initialize();
        run();
        cleanUp();
        return 0;
}

我知道我的指针有点问题,但我对这个还不太熟悉。。。这是我的设置:
Ubuntu 12.04 LTS公司
编译时使用:gcc tempc -o temp -lX11
对于那些后来发现这一点的人来说——我最初试图利用XGetGeometry是完全错误的!
要正确使用它,我必须执行以下操作:
XGetGeometry(currentDisplay, currentWindow, &currentRoot, &windowOffsetX, &windowOffsetY, &windowWidth, &windowHeight, &windowBorderWidth, &windowDepth);
这是基于我的发现。

最佳答案

基于这两个函数的文档,DefaultRootWindow()返回一个Window,而XGetGeometry()期望第三个参数返回一个Window*因此,正如警告所说,您传递的是一个普通类型,其中应该有指向该类型的指针。

10-02 02:19