Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw a string to a window with a C program including X11/Xlib.h?

Tags:

c

x11

I'm trying to create a Hello World program with X11/Xlib, but when I call XDrawString() it doesn't draw "Hello world!" in the window. What do I need to change to make it do this?

EDIT: My full code is below. StackOverflow won't let me submit the question when the question description is "mostly code" so I'll also explain what the program is intended to do below the code.

Code:

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    //Open default display
    Display *display = XOpenDisplay(NULL);
    if(display != NULL)
    {
        //Get default screen
        Screen *screen = DefaultScreenOfDisplay(display);
        if(screen != NULL)
        {
            //Initialize window
            Window window = XCreateSimpleWindow(display, RootWindowOfScreen(screen), 0, 0, WidthOfScreen(screen), HeightOfScreen(screen), 0, BlackPixelOfScreen(screen), BlackPixelOfScreen(screen));
            XSelectInput(display, window, KeyPressMask || ExposureMask);
            XStoreName(display, window, "Hello World!");
            XMapRaised(display, window);

            //Initialize graphics context
            GC graphics_context = DefaultGCOfScreen(screen);

            //Event handling loop
            XEvent event;
            while(1)
            {
                XNextEvent(display, &event);
                if(event.type == KeyPress)
                {
                    //KeyPress event
                    if(event.xkey.keycode == XKeysymToKeycode(display, XK_Escape))
                    {
                        XDestroyWindow(display, window);
                        XCloseDisplay(display);
                        exit(EXIT_SUCCESS);
                    }
                }
                else if(event.type == Expose)
                {
                    //Expose event (window is visible)
                    char *msg = "Hello world!";
                    XDrawString(display, window, DefaultGCOfScreen(screen), 0, 0, msg, strlen(msg));
                }
            }
            XDestroyWindow(display, window);
        }
        XCloseDisplay(display);
    }
    exit(EXIT_FAILURE);
}

This code is intended to

  • Open the default display;

  • Get the default screen of this display

  • Create a simple window on the default display as a child of the root window of the screen, with x and y zeroed, width and height equal to the default width and height of the screen respectively, a 0-width border, and both the border and window background colored with the color of the black pixel of the screen

  • Select exposure events and keypresses as accepted inputs to the window

  • Store the name "Hello World!" in the window

  • Map and raise the window

  • Get default graphics context of screen

  • Handle events in indefinite infinite loop, exiting with success status when the user presses the Escape key or drawing "Hello world!" to the window on window exposure

  • Exit with failure status if opening the display has failed or the default screen of the display is NULL

like image 293
Joseph Pilliner Avatar asked Nov 30 '25 02:11

Joseph Pilliner


1 Answers

I know this is old, but in case anyone still cares, you're not processing the expose event because you used the wrong operator in your call to XSelectInput. You should have used the binary or operator | instead of the logical or operator ||.

like image 157
climac Avatar answered Dec 01 '25 17:12

climac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!