Reputation: 101
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;
}
Upvotes: 2
Views: 3266
Reputation: 174
No amount of code can fix what appears to be a bug in SDL which they do not plan on fixing. But I was able to find out how to create an X window and then have SDL draw to it, and even though it's not entirely the answer here, I want to post it here for anyone in search of an alternative (since there doesn't seem to be any examples online).
Display *x11_d;
int x11_s;
Window x11_w;
x11_d = XOpenDisplay(NULL);
SDL_Window *window;
if (!x11_d)
{
fprintf(stderr, "couldn't open display\n");
return;
}
x11_s = DefaultScreen(x11_d);
x11_w = XCreateSimpleWindow(x11_d, DefaultRootWindow(x11_d), 0, 0, 640, 480, 0, 0, 0);
if (None == x11_w)
{
fprintf(stderr, "Failed to create window");
XCloseDisplay(x11_d);
return;
}
XMapWindow(x11_d, x11_w);
Atom wm_delete_window = XInternAtom(x11_d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(x11_d, x11_w, &wm_delete_window, 1);
window = SDL_CreateWindowFrom((void *)x11_w);
Upvotes: 0
Reputation: 3106
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.
Upvotes: 1