Created
January 7, 2024 15:16
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
The program includes the
X11/Xlib.h
header file, which provides the necessary functions and data types for working with the X Window System.In the
main
function, anXEvent
structure is declared to handle events, and a pointer to the display (Display*
) is obtained usingXOpenDisplay(NULL)
.A window (
Window w
) is created usingXCreateSimpleWindow
. 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.The window is mapped (made visible) on the screen using
XMapWindow
.XSelectInput
is called to specify that the program is interested in receivingExpose
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).The program enters an infinite loop using
for (;;)
.Inside the loop,
XNextEvent
waits for an event to occur and stores it in theevent
structure.If the event type is
Expose
, the program draws the string "Thanks for Watching!" at coordinates (100, 100) within the window usingXDrawString
. The string is rendered using the default graphics context (DefaultGC
) and the font specified by the X server.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.