0
votes

I am learning X11 programming with C. I have written this small code which just creates a window:

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

int main(int argc, char *argv[]) {
    Display *display;
    int screen;
    Window win;
    XEvent event;

    display = XOpenDisplay((char*)0);
    if(display == NULL) {
        fprintf(stderr, "Cannot open display\n");
        exit(1);
    }
    screen = DefaultScreen(display);
    win=XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 1000, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen));
    XSelectInput(display, win, ExposureMask | KeyPressMask);
    XMapWindow(display, win);
    
    while(1) {
        XNextEvent(display, &event);
    }

    return 0;
}

I have got a Linux PC (Ubuntu distro) and have Gnome desktop environment installed. I had successfully compiled and have run the program from the graphical terminal in Gnome.
As far as I know, X11 programs should run without the help of a desktop environment. So, I switched to a TTY (ALT+F3) and tried to run the program from there. But it says "Cannot open display".
After reading some posts, I have learnt that I should do export DISPLAY=:0. But after doing this, the program creates the window inside the existing Gnome desktop and not on the current TTY.
This question might seem stupid. I want to know how to run this program directly from the TTY without any desktop environment running.

1
Well, ya can't. Do you know what all this X stuff does? It connects to the desktop and puts a window on the desktop. The X11 system is basically the desktop. If you like, it's possible to start another bare-bones X11 system with no desktop stuff, and run your program in that. Or, on some configurations you can use a framebuffer device to draw directly on the screen. Do any of those suggestions interest you? - user253751
Yes, I want to know those things. Tell you the truth, I want to know how the desktop environments run on X and create windows. - Puspam

1 Answers

2
votes

You can't run X11 programs without X11. Your computer is already running an instance of X11 (which is called a display), and your desktop environment is connected to it which is how it displays all the desktop stuff.

All the stuff that runs within your desktop environment has an environment variable set, called DISPLAY, which tells it which X11 display (instance) to connect to. In a different TTY, that's not set already. If you set it to the same value as your desktop programs have, then you can run X11 programs which will connect to that display.

You can also start another X11. It's actually quite simple, though you might have to set a few command-line parameters. However, I think it's off-topic here, since it's not a programming question but a Linux question.

Depending on your kernel configuration, you might be able to use a framebuffer device (fbdev) to draw directly on the screen. It's a separate system from X11 which is most useful for simple graphics (i.e. directly drawing pixels onto the screen). If you want to know how to use fbdev, that should be a separate question.

In recent versions of Linux, X11 actually uses the framebuffer device, and so you can do everything through the framebuffer device that X11 can do, theoretically. It uses a whole lot more features to enable access to the graphics card and so on. Keywords are "Kernel Modesetting" (KMS) and "Direct Rendering Infrastructure" (DRI). As far as I'm aware, no programs other than the X11 server - and Wayland, which is like a differently designed X11 server - use these.