Skip to main content
AShva.1
Associate III
Apr 16, 2020 1:12 PM UTC
Solved

Xlib: I can not connect to the X server

Hi!

I'm in search of a better GUI programming path :) Now trying Xlib.

Here is a simple program from the Internet

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#include <X11/Xlib.h>

extern int errno;

int main( void ) {

  Display *d;

  Window w;

  XEvent e;

  char *msg = "Hello, World!";

  int s;

  if( ( d = XOpenDisplay( getenv("DISPLAY" ) ) ) == NULL )

  { // Connect X server

     printf( "Can't connect X server: %s\n", strerror( errno ) );

     exit( 1 );

  }

  s = DefaultScreen( d );

  w = XCreateSimpleWindow( d, RootWindow( d, s ), 10, 10, 200, 200, 1, BlackPixel( d, s ), WhitePixel( d, s ) );

  XSelectInput( d, w, ExposureMask | KeyPressMask ); 

  XMapWindow( d, w );                                

  while( 1 )

  {                                       

     XNextEvent( d, &e );

     if( e.type == Expose )

     {                        

        XFillRectangle( d, w, DefaultGC( d, s ), 20, 20, 10, 10 );

        XDrawString( d, w, DefaultGC( d, s ), 50, 50, msg, strlen( msg ) );

     }

     if( e.type == KeyPress ) break;

  }

  XCloseDisplay( d );                                

  return 0;

}

If you build and run on PC Ubuntu - it works fine

On the STM32MP157C-DK2 board - not working :(

Message in terminal:

Can't connect X server: Success

What am I doing wrong?

Best regards

Alexander

This topic has been closed for replies.
mleo
mleoBest answer
Visitor II
Apr 17, 2020 10:52 AM UTC

Hi Alexander,

Do you use the default distibution?

If yes, it is based on Wayland (not X11).

If you want to create one based on X11:

https://wiki.st.com/stm32mpu/wiki/How_to_create_your_own_image

BR,

Milan

2 replies

mleo
mleoBest answer
Visitor II
Apr 17, 2020 10:52 AM UTC

Hi Alexander,

Do you use the default distibution?

If yes, it is based on Wayland (not X11).

If you want to create one based on X11:

https://wiki.st.com/stm32mpu/wiki/How_to_create_your_own_image

BR,

Milan

AShva.1
AShva.1Author
Associate III
Apr 17, 2020 11:15 AM UTC

Hi Milan!

Thanks.

I understood.

I use the default distibution.

I will try to understand further :)

Best regards!