1

我正在用 C 语言编写我的第一个 X11 应用程序,但在尝试检索应用程序窗口的大小时遇到​​了一个重大问题。

temp.c:30:2 warning: passing argument 3 of 'XGetGeometry' makes pointer from integer without a cast

我知道这只是一个警告,但它仍然会导致不好玩的段错误。这是我的代码:

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);

这是基于我在这里的发现。

4

1 回答 1

7

根据这两个函数的文档,DefaultRootWindow()返回 a WindowwhileXGetGeometry()期望Window*第三个参数。因此,正如警告所说,您正在传递一个普通类型,其中需要指向该类型的指针。

于 2013-06-27T06:08:32.887 回答