XOpenDisplay failing - c++

I'm writing a simple window class which is failing to return a display. Here's the short version:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
class WindowImpl
{
public:
WindowImpl()
{
open = true;
}
WindowImpl(float width, float height)
{
if(!create(width, height))
{
fprintf(stderr, "Could not open display\n");
exit(1);
}
open = true;
}
~WindowImpl()
{
XCloseDisplay(display);
};
bool create(float width, float height)
{
display = XOpenDisplay(NULL);
if(display == NULL)
return false;
int displayID = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, displayID), 10, 10, width, height, 1, BlackPixel(display, displayID), WhitePixel(display, displayID));
XMapWindow(display, window);
return true;
}
bool isOpen()
{
return open;
}
void close()
{
open == false;
}
private:
Display* display;
Window window;
bool open;
};
int main()
{
WindowImpl myWindow(1920, 1080);
char* cmd;
while(myWindow.isOpen())
{
if(gets(cmd) == "close")
myWindow.close();
}
return 0;
}
WindowImpl::create fails, XOpenDisplay is returning NULL but I'm not sure why. Hopefully someone could shed some light on the problem here.
Edit: Changing WindowImpl::create to return true and false instead of 0 and 1 causes it to go through but the window still doesn't open;
For clarification:
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
Display* display;
Window window;
XEvent event;
char* message = "Hello";
int screenSize;
display = XOpenDisplay(NULL);
if(display == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
screenSize = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screenSize), 10, 10, 1920, 1080, 1, BlackPixel(display, screenSize), WhitePixel(display, screenSize));
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
KeySym keysym = XK_Escape;
KeyCode keycode = XKeysymToKeycode(display, keysym);
while(true)
{
XNextEvent(display, &event);
if(event.type == KeyPress && event.xkey.keycode == keycode)
break;
}
XCloseDisplay(display);
return 0;
}
Compiles and runs just fine.

When you create a windows, you need to let the X server to handle output buffer. Normally this is done when you enter into the window event loop, i.e. calling any function that has EVENT in its name.
Once you are not dealing with events in your code, another way to flush the output buffer is calling XFlush, as said in its manual:
The XFlush function flushes the output buffer. Most client applications need not use this function because the output buffer is automatically flushed as needed by calls to XPending, XNextEvent, and XWindowEvent. Events generated by the server may be enqueued into the library's event queue.
The XSync function flushes the output buffer and then waits until all requests have been received and processed by the X server.
So, to solve your issue, I suggest to put this line of code:
window = XCreateSimpleWindow(display, RootWindow(display, displayID), 10, 10, width, height, 1, BlackPixel(display, displayID), WhitePixel(display, displayID));
XMapWindow(display, window);
XFlush(display); // <------------
return true;

Related

How can I handle input in SDL?

My SDL program wont work. It was supposed to change the image when I pressed Up. However, it changes the image only when I click on the x in the Window
#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
int main(int argc, char* argv[])
{
enum KeyPressSurfaces {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_TOTAL
};
SDL_Init(SDL_INIT_VIDEO);
SDL_Event e;
SDL_Window* window = SDL_CreateWindow("antena1", // window's title
10, 25, // coordinates on the screen, in pixels, of the window's upper left corner
640, 420, // window's length and height in pixels
SDL_WINDOW_OPENGL);
SDL_Surface* key_press_surface[KEY_PRESS_SURFACE_TOTAL];
SDL_Surface* gImage = NULL;
SDL_Surface* gScreenSurface = NULL;
bool quit = false;
key_press_surface[KEY_PRESS_SURFACE_UP] = SDL_LoadBMP("hello_world.bmp");
gScreenSurface = SDL_GetWindowSurface(window);
gImage = SDL_LoadBMP("nick.bmp");
if (gImage == NULL) {
printf("Erro", SDL_GetError);
}
SDL_BlitSurface(gImage, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(window);
gScreenSurface = SDL_GetWindowSurface(window);
while (!quit) {
while (SDL_PollEvent(&e) == 0) {
if (e.type == SDL_QUIT) {
quit = true;
//SDL_DestroyWindow(window);#
}
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_LEFT:
gImage = key_press_surface[KEY_PRESS_SURFACE_UP];
break;
default:
gScreenSurface = NULL;
break;
}
}
}
};
SDL_BlitSurface(gImage, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(30000);
return 0;
}
https://wiki.libsdl.org/SDL_PollEvent
SDL_PollEvent returns 0 if there are no events available. So per the example in the above article, in order to get into the while statement for SDL_PollEvent, there must be events in the queue. However, your code only goes into the loop if there are NO events in the queue. So anything that happens once you get there is undefined.
IOW, just remove the "== 0".

Why is an XImage's data pointer null when capturing using XShmGetImage?

I'm writing a program to rapidly capture images from one window, modify them, and output them to another window using Xlib with C++. I have this working via XGetImage:
#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
const int TEST_SIZE = 512;
int main()
{
// Open default display
Display *display = XOpenDisplay(nullptr);
int screen = DefaultScreen(display);
Window rootWin = RootWindow(display, screen);
GC graphicsContext = DefaultGC(display, screen);
// Create new window and subscribe to events
long blackPixel = BlackPixel(display, screen);
long whitePixel = WhitePixel(display, screen);
Window newWin = XCreateSimpleWindow(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, 1, blackPixel, whitePixel);
XMapWindow(display, newWin);
XSelectInput(display, newWin, ExposureMask | KeyPressMask);
// Main event loop for new window
XImage *image;
XEvent event;
bool exposed = false;
bool killWindow = false;
while (!killWindow)
{
// Handle pending events
if (XPending(display) > 0)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
exposed = true;
} else if (event.type == NoExpose)
{
exposed = false;
} else if (event.type == KeyPress)
{
killWindow = true;
}
}
// Capture the original image
image = XGetImage(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, AllPlanes, ZPixmap);
// Modify the image
if (image->data != nullptr)
{
long pixel = 0;
for (int x = 0; x < image->width; x++)
{
for (int y = 0; y < image->height; y++)
{
// Invert the color of each pixel
pixel = XGetPixel(image, x, y);
XPutPixel(image, x, y, ~pixel);
}
}
}
// Output the modified image
if (exposed && killWindow == false)
{
XPutImage(display, newWin, graphicsContext, image, 0, 0, 0, 0, TEST_SIZE, TEST_SIZE);
}
XDestroyImage(image);
}
// Goodbye
XCloseDisplay(display);
}
It generates output like this: https://streamable.com/hovg9
I'm happy to have gotten this far, but from what I've read the performance isn't going to scale very well because it has to allocate space for a new image at every frame. In fact, without the call to XDestroyImage at the end of the loop this program fills up all 16GB of memory on my machine in a matter of seconds!
It seems the recommended approach here is to to set up a shared memory space where X can write the contents of each frame and my program can subsequently read and modify them without the need for any extra allocation. Since the call to XShmGetImage blocks and waits for IPC I believe this means I won't have to worry about any concurrency issues in the shared space.
I've attempted to implement the XShmGetImage approach with the following code:
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <X11/Xutil.h>
const int TEST_SIZE = 512;
int main()
{
// Open default display
Display *display = XOpenDisplay(nullptr);
int screen = DefaultScreen(display);
Window rootWin = RootWindow(display, screen);
GC graphicsContext = DefaultGC(display, screen);
// Create new window and subscribe to events
long blackPixel = BlackPixel(display, screen);
long whitePixel = WhitePixel(display, screen);
Window newWin = XCreateSimpleWindow(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, 1, blackPixel, whitePixel);
XMapWindow(display, newWin);
XSelectInput(display, newWin, ExposureMask | KeyPressMask);
// Allocate shared memory for image capturing
Visual *visual = DefaultVisual(display, 0);
XShmSegmentInfo shminfo;
int depth = DefaultDepth(display, screen);
XImage *image = XShmCreateImage(display, visual, depth, ZPixmap, nullptr, &shminfo, TEST_SIZE, TEST_SIZE);
shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0666);
shmat(shminfo.shmid, nullptr, 0);
shminfo.shmaddr = image->data;
shminfo.readOnly = False;
XShmAttach(display, &shminfo);
// Main event loop for new window
XEvent event;
bool exposed = false;
bool killWindow = false;
while (!killWindow)
{
// Handle pending events
if (XPending(display) > 0)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
exposed = true;
} else if (event.type == NoExpose)
{
exposed = false;
} else if (event.type == KeyPress)
{
killWindow = true;
}
}
// Capture the original image
XShmGetImage(display, rootWin, image, 0, 0, AllPlanes);
// Modify the image
if (image->data != nullptr) // NEVER TRUE. DATA IS ALWAYS NULL!
{
long pixel = 0;
for (int x = 0; x < image->width; x++)
{
for (int y = 0; y < image->height; y++)
{
// Invert the color of each pixel
pixel = XGetPixel(image, x, y);
XPutPixel(image, x, y, ~pixel);
}
}
}
// Output the modified image
if (exposed && killWindow == false)
{
XShmPutImage(display, newWin, graphicsContext, image, 0, 0, 0, 0, 512, 512, false);
}
}
// Goodbye
XFree(image);
XCloseDisplay(display);
}
Somehow, the images are still being captured and sent to the new window, but the data pointer within the XImage is always null. I can't modify any of the contents and any usage of the XGetPixel and XPutPixel macros will fail. Notice in this video that none of the colors are being inverted like before: https://streamable.com/dckyv
This doesn't make any sense to me. Clearly the data is still being transferred between the windows, but where is it in the XImage structure? How can I access it via code?
It turns out I wasn't reading the signature for shmat correctly. It doesn't have a void return type, it returns a void pointer. This needs to be assigned directly to the XImage's data pointer in order for the data pointer to do anything.
In the call to XShmPutImage the shared memory location is referenced internally rather than the XImage's data pointer which is why this was still working even without utilizing the return value from shmat.
Here's the working code, along with a few other additions which I made for error handling and teardown:
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <X11/Xutil.h>
const int TEST_SIZE = 512;
static int handleXError(Display *display, XErrorEvent *event)
{
printf("XErrorEvent triggered!\n");
printf("error_code: %d", event->error_code);
printf("minor_code: %d", event->minor_code);
printf("request_code: %d", event->request_code);
printf("resourceid: %lu", event->resourceid);
printf("serial: %d", event->error_code);
printf("type: %d", event->type);
return 0;
}
int main()
{
// Open default display
XSetErrorHandler(handleXError);
Display *display = XOpenDisplay(nullptr);
int screen = DefaultScreen(display);
Window rootWin = RootWindow(display, screen);
GC graphicsContext = DefaultGC(display, screen);
// Create new window and subscribe to events
long blackPixel = BlackPixel(display, screen);
long whitePixel = WhitePixel(display, screen);
Window newWin = XCreateSimpleWindow(display, rootWin, 0, 0, TEST_SIZE, TEST_SIZE, 1, blackPixel, whitePixel);
XMapWindow(display, newWin);
XSelectInput(display, newWin, ExposureMask | KeyPressMask);
// Allocate shared memory for image capturing
XShmSegmentInfo shminfo;
Visual *visual = DefaultVisual(display, screen);
int depth = DefaultDepth(display, screen);
XImage *image = XShmCreateImage(display, visual, depth, ZPixmap, nullptr, &shminfo, TEST_SIZE, TEST_SIZE);
shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0777);
image->data = (char*)shmat(shminfo.shmid, 0, 0);
shminfo.shmaddr = image->data;
shminfo.readOnly = False;
XShmAttach(display, &shminfo);
XSync(display, false);
shmctl(shminfo.shmid, IPC_RMID, 0);
// Main event loop for new window
XEvent event;
bool exposed = false;
bool killWindow = false;
while (!killWindow)
{
// Handle pending events
if (XPending(display) > 0)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
exposed = true;
} else if (event.type == NoExpose)
{
exposed = false;
} else if (event.type == KeyPress)
{
killWindow = true;
}
}
// Capture the original image
XShmGetImage(display, rootWin, image, 0, 0, AllPlanes);
// Modify the image
if(image->data != nullptr) // NEVER TRUE. DATA IS ALWAYS NULL!
{
long pixel = 0;
for (int x = 0; x < image->width; x++)
{
for (int y = 0; y < image->height; y++)
{
// Invert the color of each pixel
pixel = XGetPixel(image, x, y);
XPutPixel(image, x, y, ~pixel);
}
}
}
// Output the modified image
if (exposed && killWindow == false)
{
XShmPutImage(display, newWin, graphicsContext, image, 0, 0, 0, 0, 512, 512, false);
}
}
// Goodbye
XShmDetach(display, &shminfo);
XDestroyImage(image);
shmdt(shminfo.shmaddr);
XCloseDisplay(display);
}

Xlib: Closing window always causes fatal IO error?

I'm not sure why this happens, but any window I create using Xlib in C++ gives outputs an error to the terminal when I try to close is using the X button. I can close it programmatically with no errors, it's just the X button that does it.
The error is the following:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 483 requests (483 known processed) with 0 events remaining.
The number of requests is different every time, but there's always 0 events remaining. Why does this happen? The cause doesn't seem to be my code, since it does this no matter what and sends no close events to the queue. I've tried intercepting the Atom WM_WINDOW_DELETE, and it doesn't run over the expected code when I close the window.
Edit: Added event loop code.
while(XPending(display)) {
XNextEvent(display, &event);
pthread_mutex_unlock(&mutex);
if(event.type == Expose) {
XWindowAttributes getWindowAttributes;
pthread_mutex_lock(&mutex);
XGetWindowAttributes(display, window, &getWindowAttributes);
if(state.currentState == STATE_NORMAL) {
state.normX = getWindowAttributes.x;
state.normY = getWindowAttributes.y;
state.normWidth = getWindowAttributes.width;
state.normHeight = getWindowAttributes.height;
}
pthread_mutex_unlock(&mutex);
glViewport(0, 0, getWindowAttributes.width, getWindowAttributes.height);
} else if(event.type == KeyPress) {
return false;
} else if(event.type == ClientMessage) {
std::cout<<"X Button pressed"<<std::endl; //Never run when X-ing window
if(event.xclient.message_type == XInternAtom(display, "WM_DELETE_WINDOW", True)) {
return false;
}
} else if(event.type == ButtonPress) {
if(state.currentState != STATE_FULLSCREEN) {
fullscreen();
} else {
normalize();
}
} else if(!handleEvent(event)){
return false;
}
pthread_mutex_lock(&mutex);
}
In addition to WM_WINDOW_DELETE you need to listen for and handle the ClientMessage event.
Modifying the example from Rosetta Code to illustrate:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
Display *d;
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1, BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
// I support the WM_DELETE_WINDOW protocol
Atom WM_DELETE_WINDOW = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &WM_DELETE_WINDOW, 1);
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), 10, 50, msg, strlen(msg));
}
else if (e.type == KeyPress)
break;
else if (e.type == ClientMessage)
// TODO Should check here for other client message types -
// however as the only protocol registered above is WM_DELETE_WINDOW
// it is safe for this small example.
break;
}
XCloseDisplay(d);
return 0;
}

I want to make splash screen and now I have two problems?

1: I want to have a splash screen but I only have a window?so,how to do with sth like parm
2: I've used a while(!done) to draw the window so how to break out with a function or sth else
here is my code and much thx to you
g++ -o m_splash m_splash.cpp -lX11 -lImlib2
#include <stdio.h>
#include <X11/Xlib.h>
#include <Imlib2.h>
#include <unistd.h>
int main()
{
Imlib_Image m_img;
Display *m_dpy;
Pixmap m_pix;
Window m_root;
Screen *scn;
int m_width, m_height;
const char *filename = "/home/ang/so_zt/w.png";
m_img = imlib_load_image(filename);
if(!m_img)
{
printf("%s\n","init m_img faild");
}
imlib_context_set_image(m_img);
m_width = imlib_image_get_width();
m_height = imlib_image_get_height();
m_dpy = XOpenDisplay(NULL);
if(!m_dpy)
{
printf("%s\n","open display failed");
}
scn = DefaultScreenOfDisplay(m_dpy);
int s = DefaultScreen(m_dpy);
m_root = XCreateSimpleWindow(m_dpy, RootWindow(m_dpy,s),10,10,m_width,m_height,0,
BlackPixel(m_dpy, s), WhitePixel(m_dpy, s));
m_pix = XCreatePixmap(m_dpy, m_root, m_width, m_height, DefaultDepthOfScreen(scn));
imlib_context_set_display(m_dpy);
imlib_context_set_visual(DefaultVisualOfScreen(scn));
imlib_context_set_colormap(DefaultColormapOfScreen(scn));
imlib_context_set_drawable(m_pix);
imlib_render_image_on_drawable(0,0);
XSetWindowBackgroundPixmap(m_dpy, m_root, m_pix);
XClearWindow(m_dpy, m_root);
Atom wmDeleteMessage = XInternAtom(m_dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(m_dpy, m_root, &wmDeleteMessage, 1);
XSelectInput(m_dpy, m_root, ExposureMask | KeyPressMask | StructureNotifyMask);
XMapWindow(m_dpy, m_root);
bool done = false;
while (!done)
{
XEvent m_ev;
XNextEvent(m_dpy, &m_ev);
/* draw or redraw the window */
if (m_ev.type == Expose)
{
XFillRectangle(m_dpy, m_root, DefaultGC(m_dpy, DefaultScreen(m_dpy)), 20, 20, 10, 10);
}
/* exit on key press */
//usleep(1000000);
//done = true;
switch(m_ev.type)
{
case KeyPress:
XDestroyWindow(m_dpy, m_root);
break;
case DestroyNotify:
done = true;
break;
case ClientMessage:
if (m_ev.xclient.data.l[0] == wmDeleteMessage)
{
done = true;
}
break;
}
}
//XFreePixmap(m_dpy, m_pix);
//imlib_free_image();
//XCloseDisplay(m_dpy);
}
To make it a splash screen, use extended window manager hints.
#include <X11/Xatom.h>
Atom type = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE", False);
Atom value = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(m_dpy, m_root, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
The window then appears without decorations and stays until clicked.
When clicked you get an UnmapNotify event, so this what you should use to set done.
To avoid having to get events, add
XFlush(m_dpy);
after mapping the window to display it and
XUnmapWindow(m_dpy, m_root);
when you want to get rid of it.
In this example the program just sleeps for 5 seconds before continuing:
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <Imlib2.h>
#include <unistd.h>
int main()
{
Imlib_Image m_img;
Display *m_dpy;
Pixmap m_pix;
Window m_root;
Screen *scn;
int m_width, m_height;
const char *filename = "w.png";
m_img = imlib_load_image(filename);
if(!m_img)
{
printf("%s\n","init m_img faild");
}
imlib_context_set_image(m_img);
m_width = imlib_image_get_width();
m_height = imlib_image_get_height();
m_dpy = XOpenDisplay(NULL);
if(!m_dpy)
{
printf("%s\n","open display failed");
}
scn = DefaultScreenOfDisplay(m_dpy);
int s = DefaultScreen(m_dpy);
m_root = XCreateSimpleWindow(m_dpy, RootWindow(m_dpy,s),10,10,m_width,m_height,0,
BlackPixel(m_dpy, s), WhitePixel(m_dpy, s));
m_pix = XCreatePixmap(m_dpy, m_root, m_width, m_height, DefaultDepthOfScreen(scn));
Atom type = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE", False);
Atom value = XInternAtom(m_dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(m_dpy, m_root, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
imlib_context_set_display(m_dpy);
imlib_context_set_visual(DefaultVisualOfScreen(scn));
imlib_context_set_colormap(DefaultColormapOfScreen(scn));
imlib_context_set_drawable(m_pix);
imlib_render_image_on_drawable(0,0);
XSetWindowBackgroundPixmap(m_dpy, m_root, m_pix);
XClearWindow(m_dpy, m_root);
Atom wmDeleteMessage = XInternAtom(m_dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(m_dpy, m_root, &wmDeleteMessage, 1);
XMapWindow(m_dpy, m_root);
XFlush(m_dpy);
sleep(5);
XUnmapWindow(m_dpy, m_root);
}
and this program can have a splash with hello world alse I do not know how to jump out of the while(!done) so much thx to U
g++ -o test_chr test_chr.cpp -lX11
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
Display *d;
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;
bool done = false;
/* open connection with the server */
d = XOpenDisplay(NULL);
if (d == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
/* create window */
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 480, 320, 0,BlackPixel(d, s), WhitePixel(d, s));
Atom type = XInternAtom(d,"_NET_WM_WINDOW_TYPE", False);
Atom value = XInternAtom(d,"_NET_WM_WINDOW_TYPE_SPLASH", False);
XChangeProperty(d, w, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
/* register interest in the delete window message */
Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &wmDeleteMessage, 1);
/* select kind of events we are interested in */
XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);
/* map (show) the window */
XMapWindow(d, w);
/* event loop */
while (!done)
{
XNextEvent(d, &e);
/* draw or redraw the window */
if (e.type == Expose)
{
XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
}
/* exit on key press */
switch(e.type)
{
case KeyPress:
XDestroyWindow(d, w);
break;
case DestroyNotify:
done = true;
break;
case ClientMessage:
if (e.xclient.data.l[0] == wmDeleteMessage)
{
done = true;
}
break;
}
}
/* close connection to server */
XCloseDisplay(d);
return 0;
}

How to allow a worker thread to update an X11 Window?

I have an application that that I was trying to modify so that a worker thread can tell a window to update itself with new data.
Variables are defined as follows:
Display *d;
Window w;
XEvent exppp;
The window is started with the following code:
XEvent e;
d = XOpenDisplay(NULL);
if (d == NULL)
return 0;
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 800, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose || e.type == KeyPress) {
// redraw routine goes here
}
}
What I was attempting to use to get the window to redraw is a function that can be called by another thread:
void graphical_out::redraw()
{
exppp.type = Expose;
XSendEvent(d, w, false, Expose, &exppp);
}
And the window only updates itself when it is re-sized or receives a key-press. This seems a bit like a newbie question but Google has failed me on this one. Any suggestions to what I am doing wrong?
Your arguments to XSendEvent are wrong. You need to pass the mask (ExposureMask), not the event type.
exppp.xexpose.window = w; is mandatory (the window argument of XSendEvent is not the window of the XEvent structure).
Clean the event before sending it: memset(&exppp, 0, sizeof(exppp));, just in case.
Xlib is not thread-safe and calling its functions from more than one thread may be dangerous.
UPDATE In a multithreaded program one needs to call XFlush here and there (though multithreading is never guaranteed to work with Xlib).
This code works for me:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <memory.h>
Display *d;
Window w;
int s;
void* tp (void* q)
{
XEvent exppp;
while (1)
{
sleep (3);
printf ("Sending event of type Expose\n");
memset(&exppp, 0, sizeof(exppp));
exppp.type = Expose;
exppp.xexpose.window = w;
XSendEvent(d,w,False,ExposureMask,&exppp);
XFlush(d);
}
return NULL;
}
int main ()
{
XEvent e;
pthread_t thread;
d = XOpenDisplay(NULL);
if (d == NULL)
return 0;
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 800, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
pthread_create(&thread, NULL, tp, NULL);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
printf ("Got Expose event%s\n", e.xexpose.send_event ? " (SendEvent)" : "");
}
else if (e.type == KeyPress) {
printf ("Got KeyPress event%s\n", e.xkey.send_event ? " (SendEvent)" : "");
}
}
}
It might work for you, or it might bomb. Xlib is not thread safe, use at your own risk.