2013-06-27 24 views
1

我正在用C编写我的第一个X11应用程序,并且在尝试检索我的应用程序窗口的大小时遇到​​了一个主要问题。“使指针不受整型影响” - 如何满足GCC?

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

对于那些后来发现这一点 - 我利用初步尝试210是完全错误的!

要正确地使用它,我必须做到以下几点:

XGetGeometry(currentDisplay, currentWindow, &currentRoot, &windowOffsetX, &windowOffsetY, &windowWidth, &windowHeight, &windowBorderWidth, &windowDepth);

这是基于从我的调查结果here的。

+1

您需要回答的问题是*为什么*您要调用'XGetGeometry'。如果没有弄清楚你想用它做什么,你就无法使电话正确。 –

+1

@ n.m。,我的目标是获得我窗户的大小。我能够获得屏幕的大小,但我需要知道我的实际窗口的大小。 – FreeSnow

+1

对不起,我误解了你的代码。以某种方式错过了'&windowWidth,&windowHeight'。所以这部分是正确的,我只是让自己看起来很愚蠢。唯一的问题是根窗口是返回参数。 –

回答

7

基于该文档的两个功能,DefaultRootWindow()返回一个WindowXGetGeometry()预计,第三个参数一个Window*。正如警告所说,您正在传递一个正常类型,其中指向该类型的指针是预期的。

+2

特别是,'XGetGeometry'的第三个参数应该是一个指向变量的指针,它会将根窗口的窗口句柄写入 - 函数的输出,而不是输入。 –

+0

+1,我一直忘记X11很大程度上是基于'XID',它是一个无符号的长整型,错误信息并不像一个单独的类型那样具有说服力:) –

+0

非常感谢!无可否认,出于某种原因,我一直认为我的&windowWidth参数是错误的。感谢您澄清错误信息! :) – FreeSnow