c - 带参数和不带参数的函数 XOpenDisplay

标签 c linux xlib

我对 XOpenDisplay 函数没有什么问题。在学校我可以运行程序并且在使用 XOpenDisplay("ip:0") 时它运行良好,但是在我在家里的本地机器上运行程序时(更改当前的 ip)出现“段错误(核心转储)”,但是empy string XOpenDisplay("") 它工作正常。我需要能够使用 ip。使用主机 +,但没有任何变化。 我的系统是 Kubuntu 14.04.1: 3.16.0-30-generic#40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015

程序代码如下:

#include <X11/Xlib.h>
#include <X11/X.h>
#include <stdio.h>

Display *mydisplay;
Window mywindow;
XSetWindowAttributes mywindowattributes;
XGCValues mygcvalues;
GC mygc;
Visual *myvisual;
int mydepth;
int myscreen;
Colormap mycolormap;
XColor mycolor,mycolor1,dummy;
int i;

main()

{
  mydisplay = XOpenDisplay("192.168.0.12:0");
  myscreen = DefaultScreen(mydisplay);
  myvisual = DefaultVisual(mydisplay,myscreen);
  mydepth = DefaultDepth(mydisplay,myscreen);
  mywindowattributes.background_pixel = XWhitePixel(mydisplay,myscreen);
  mywindowattributes.override_redirect = True;

  mywindow = XCreateWindow(mydisplay,XRootWindow(mydisplay,myscreen),
                        0,0,500,500,10,mydepth,InputOutput,
                        myvisual,CWBackPixel|CWOverrideRedirect,
                        &mywindowattributes);

  mycolormap = DefaultColormap(mydisplay,myscreen);                 

  XAllocNamedColor(mydisplay,mycolormap,"cyan",&mycolor,&dummy);

    XAllocNamedColor(mydisplay,mycolormap,"red",&mycolor1,&dummy);                 

  XMapWindow(mydisplay,mywindow);

  mygc = DefaultGC(mydisplay,myscreen);

  XSetForeground(mydisplay,mygc,mycolor.pixel);

  XFillRectangle(mydisplay,mywindow,mygc,100,100,300,300);

  XSetForeground(mydisplay,mygc,mycolor1.pixel);

  XSetFunction(mydisplay,mygc,GXcopy);

  XSetLineAttributes(mydisplay,mygc,10,LineSolid,CapProjecting,JoinMiter);

  XDrawLine(mydisplay,mywindow,mygc,100,100,400,400);

  XDrawLine(mydisplay,mywindow,mygc,100,400,400,100);

  XFlush(mydisplay);

  sleep(10);

  XCloseDisplay(mydisplay);

  exit(0);
}

我只能猜测需要设置一些东西,但不知道那个选项在哪里。

最佳答案

您应始终检查函数是否成功返回。它不是 Haskell,所有的检查都是由 monad 为你完成的,它是 C。至于你的特殊情况,问题是函数 XOpenDisplay失败并为您返回 null。在下一行中,您尝试使用 DefaultScreen结果。 DefaultScreen定义为

#define DefaultScreen(dpy)  ((dpy)->default_screen)

即它只是一个宏,它使用第一个参数作为指针。在你的情况下它确实是 ((0)->default_screen) ,即取消引用空指针,这会导致您看到的段错误。

此外,关于 XOpenDisplay("192.168.0.12:0"); — 您没有提到您正在尝试连接到另一台 PC,因此,如果它是运行该应用程序的同一台计算机,请尝试将该函数调用为 XOpenDisplay("127.0.0.1:0");

UPD: 好的,我尝试在我的 PC 上运行代码,但该功能对我也不起作用。为了找到原因,我在 strace 下启动了代码应用程序,并看到

…
connect(3, {sa_family=AF_INET, sin_port=htons(6000), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 ECONNREFUSED (Connection refused)
…

啊哈!因此,应用程序尝试连接到 XServer,但 Xserver 拒绝连接。实际上,默认情况下禁用它是有安全原因的——因此,除非您特别允许,否则没有人会从网络连接到您的 XServer。要使该功能正常工作,您需要使用允许此类连接的选项启动 XServer。现在 DisplayManager 是那些管理 xsessions 的人,所以你需要根据你的 DM 设置一些选项。

lightdm 的解决方案

打开/etc/lightdm/lightdm.conf , 并粘贴 xserver-allow-tcp=true 行在 [SeatDefaults] 部分(你会看到它)

gdm 的解决方案

编辑文件 /etc/gdm/gdm.schemas , 你会发现类似

<schema>
<key>security/DisallowTCP</key>
<signature>b</signature>
<default>true</default>
</schema>

更改 truefalse .

关于c - 带参数和不带参数的函数 XOpenDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30514218/

相关文章:

c语言解释,涉及整数计算

c - 在 visual studio 中构建程序

linux - 内部 Linux 内核接口(interface)

c++ - 我是否需要断开从 XGetXCBConnection 获得的 xcb_connection_t?

c++ - Xlib XSendEvent 单击事件在 Ubuntu 12.04 的某些窗口中不起作用

c - 指向结构的指针在 C 中返回不正确的值

c - 对 gcc pthreads C 中函数的 undefined reference

linux - 重新启动 Apache 服务器的问题

linux - Linux 文件描述符的值是否总是小于打开的文件限制?

c++ - 创建带有帧缓冲区的 xlib 窗口,我可以直接绘制并使用 XPutImage