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.