Skip to content

Instantly share code, notes, and snippets.

@nir9
Created January 7, 2024 15:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nir9/098d83c754460540ec6050f7f8d7ffb6 to your computer and use it in GitHub Desktop.
Save nir9/098d83c754460540ec6050f7f8d7ffb6 to your computer and use it in GitHub Desktop.
Simple X11 Window Creation in C - Not for production, only for fun (gcc gui.c -lX11)
#include <X11/Xlib.h>
int main() {
XEvent event;
Display* display = XOpenDisplay(NULL);
Window w = XCreateSimpleWindow(display, DefaultRootWindow(display), 50, 50, 250, 250, 1, BlackPixel(display, 0), WhitePixel(display, 0));
XMapWindow(display, w);
XSelectInput(display, w, ExposureMask);
for (;;) {
XNextEvent(display, &event);
if (event.type == Expose) {
XDrawString(display, w, DefaultGC(display, 0), 100, 100, "Thanks for Watching!", 20);
}
}
return 0;
}
@HoneyBeaver506
Copy link

The provided code is a C program that creates a simple graphical window using the X Window System (X11) library. Here's a breakdown of what the code does:

  1. The program includes the X11/Xlib.h header file, which provides the necessary functions and data types for working with the X Window System.

  2. In the main function, an XEvent structure is declared to handle events, and a pointer to the display (Display*) is obtained using XOpenDisplay(NULL).

  3. A window (Window w) is created using XCreateSimpleWindow. The window has a size of 250x250 pixels and is positioned at (50, 50) coordinates relative to the root window. The window has a black border and a white background.

  4. The window is mapped (made visible) on the screen using XMapWindow.

  5. XSelectInput is called to specify that the program is interested in receiving Expose events for the window. Expose events occur when a portion of the window needs to be redrawn (e.g., after being obscured and then revealed).

  6. The program enters an infinite loop using for (;;).

  7. Inside the loop, XNextEvent waits for an event to occur and stores it in the event structure.

  8. If the event type is Expose, the program draws the string "Thanks for Watching!" at coordinates (100, 100) within the window using XDrawString. The string is rendered using the default graphics context (DefaultGC) and the font specified by the X server.

  9. The loop continues indefinitely, waiting for Expose events and redrawing the string whenever the window needs to be redrawn.

In summary, this program creates a simple window and continuously displays the message "Thanks for Watching!" in the window. It demonstrates the basic usage of the X Window System library in C for creating a graphical user interface (GUI) application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment