0

I'm trying to create a window that has "Hello, world" printed in it, and the window is working but the text is not working or is not showing up.

I've tried changing the colors of the text and background, but to no avail. I've also made many efforts to searching this problem online but since I'm using Kali Linux its a very niece problem and as such hard to find.

#include <X11/Xlib.h>

int main()
{
    // Initialize the X display
    Display* display = XOpenDisplay(NULL);

    // Create a window
    Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 640, 480, 0, 0, WhitePixel(display, 0));

    // Set the window title
    XStoreName(display, window, "My Window");

    // Map the window to the screen
    XMapWindow(display, window);

    // Wait for the window to be mapped
    XEvent event;
    do {
        XNextEvent(display, &event);
    } while (event.type != MapNotify || event.xmap.event != window);

    // Create a graphics context
    GC gc = XCreateGC(display, window, 0, NULL);

    // Set the font and color
    XSetFont(display, gc, XLoadFont(display, "fixed"));
    XSetForeground(display, gc, BlackPixel(display, 0));

    // Draw the text
    XDrawString(display, window, gc, 10, 20, "Hello, World!", 13);

    // Flush the display
    XFlush(display);

    // Wait for events
    while (1)
    {
        XNextEvent(display, &event);
    }

    // Close the display
    XCloseDisplay(display);

    return 0;
}


ALX
  • 1
  • 2
  • Double check that you are actually receiving the `MapNotify` event. IIRC, to receive that you first have to ask for the event to be notified, setting the proper bit in the `event_mask` of the window (I forgot the exact function call). Do this before calling `XMapWindow` or you might lose the event. Also always flush before waiting for events. – chi Apr 02 '23 at 23:46

1 Answers1

0

try this :

#include <X11/Xlib.h>

int main()
{
// Initialize the X display
Display* display = XOpenDisplay(NULL);

// Create a window
Window window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 640, 480, 0, 0, WhitePixel(display, 0));

// Set the window title
XStoreName(display, window, "My Window");

// Map the window to the screen
XMapWindow(display, window);

// Wait for the window to be mapped
XEvent event;
do {
    XNextEvent(display, &event);
} while (event.type != MapNotify || event.xmap.event != window);

// Create a graphics context
GC gc = XCreateGC(display, window, 0, NULL);

// Set the font and color
XSetFont(display, gc, XLoadFont(display, "9x15"));
XSetForeground(display, gc, BlackPixel(display, 0));

// Draw the text
XDrawString(display, window, gc, 10, 20, "Hello, World!", 13);

// Flush the display
XFlush(display);

// Wait for events
while (1)
{
    XNextEvent(display, &event);
}

// Close the display
XCloseDisplay(display);

return 0;
}
cipher
  • 1