Search code examples
c++cxlib

xlib prevent close button to exit the application


is there way to prevent application exit by clicking close button? the following code only hide minimize and maximize buttons but not close button...

Atom window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
long value = XInternAtom(display, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
XChangeProperty(display, window, window_type, XA_ATOM, 32, PropModeReplace, (unsigned char *) &value,1 );

i tried to search via google about overriding _NET_CLOSE_WINDOW but can't really find one... or a way to remove _NET_WM_ACTION_CLOSE from the xprop...


Solution

  • Read ICCCM & EWMH. You might need months (or years) to understand all the details (and the relation to X11 and X protocols and architectures).

    You could in theory do what you want in raw Xlib. But details are so complex that you really should use some toolkit, like Qt or GTK (life is short), or maybe libSDL or SFML. See also this list.

    Mastering X11 at the protocol and Xlib levels take a lot of time (the related documentation has many thousands of pages). Are you sure to willing to spend that?

    So coding at the raw X11 level is like coding in assembler. Nobody does that for real programs today, and for similar reasons (too low level of abstraction; too many details to care about....).

    Notice that the current trend is to replace X with Wayland. You could need several years to develop your thing from scratch in X11, and by the time you'll succeed, X would become obsolete. BTW recent versions of Qt & GTK know about Wayland (so the same code can work on X11 and on Wayland).

    So use some GUI toolkit. They are monster software of millions of lines, and there is a reason for that complexity (also, graphics cards are very complex hardware today; see also this & this). They give you the ability to disable closing a window (because they know all the ICCCM & EWMH details).

    Since you tagged your question with C++, I strongly recommend using Qt (or maybe SFML). The documentation of Qt is excellent.

    BTW, your unclear question looks strange (and lacks motivation and context). It seems that you are wrongly supposing that your application is the only one on the screen, and that is against the philosophy of every windowing system (which are multi-tasking and multi-window, so multi-application, at heart). You should think your application as working with other ones (e.g. for copy & paste, which has no sense without thinking of several applications and several windows involved). And an advanced user could always "terminate" your application (e.g. with kill(1) or xkill). You probably want instead to be notified of window closes (e.g. to show some dialog when that happens). You should not even design an unclosable application (that is against all coding and interface guidelines).