2

I'm trying to make SDL2 use the root X window to display things, but it doesn't seem to work - the window doesn't get changed in any way. Also, the whole program doesn't quit after the SDL_Delay() for some reason. Is it not possible? Am I doing something wrong?

#include <SDL.h>

#include <X11/Xlib.h>

#include <stdio.h>

// clang -lSDL2 -lX11 -I/usr/include/SDL2 -Weverything x11.c -o x11

int main(void)
{
    Display *x11_d;
    int x11_s;
    Window x11_w;
    SDL_Window *w;
    SDL_Renderer *r;

    x11_d = XOpenDisplay(NULL);

    if(!x11_d) {
        fprintf(stderr, "couldn't open display\n");
        return 1;
    }

    x11_s = DefaultScreen(x11_d);
    x11_w = RootWindow(x11_d, x11_s);

    if(SDL_Init(SDL_INIT_VIDEO) != 0) {
        fprintf(stderr, "couldn't initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    w = SDL_CreateWindowFrom((void *)x11_w);

    XCloseDisplay(x11_d);

    if(!w) {
        fprintf(stderr, "couldn't attach to the root X11 window: %s\n", SDL_GetError());
        return 1;
    }

    r = SDL_CreateRenderer(w, -1, 0);

    SDL_SetRenderDrawColor(r, 255, 0, 0, 255);
    SDL_RenderClear(r);
    SDL_RenderPresent(r);

    SDL_Delay(5700);

    SDL_Quit();
    return 0;
}
mio
  • 91
  • 4
  • 9

1 Answers1

1

You're closing the X Display right after you create the SDL window, so you lose the connection. That obviously isn't helping but you also left out about 95% of the code required to get X working. Tutorial here.

Community
  • 1
  • 1
JvO
  • 3,036
  • 2
  • 17
  • 32
  • I'm trying to use as little Xlib as possible, just enough to get the root window, pass it to `SDL_CreateWindowFrom()` and leave the rest to SDL. What exactly, apart from closing the display too early, is my code missing? Creating an X event loop doesn't seem to change anything. – mio Dec 24 '15 at 23:31