override_redirect Xlib window attribute does nothing - c

I want to create a non-resizable window for my little game engine. I found out that override_redirect attribute set to true is exactly what I need. So I wrote my sample program:
#include <X11/Xlib.h>
#include <unistd.h>
Display *display;
Window window;
Visual *visual;
XSetWindowAttributes attributes;
int depth;
int screen;
int main(){
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
attributes.background_pixel = XWhitePixel(display,screen);
attributes.override_redirect = True;
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 350, 200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
XMapWindow(display, window);
XFlush(display);
sleep(10);
return 0;
}
However, my window is resizable and there is a title bar on the top of it. How can I get rid of those and why doesn't this code work as intended?

How can I get rid of those and why doesn't this code work as intended?
You forgot to set the CWOverrideRedirect bit in the bitmask:
CWBackPixel means that the background_pixel element in the attributes structure is considered.
You must use CWBackPixel|CWOverrideRedirect if both the elements background_pixel and override_redirect shall be considered:
attributes.background_pixel = XWhitePixel(display,screen);
attributes.override_redirect = True;
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 350, 200, 5, depth, InputOutput,
visual, CWBackPixel|CWOverrideRedirect, &attributes);

Related

Cant fin definition of XMapWindow() in Xlib headers

So i have this simple code that draws a small window using X11
int main(int, char*[])
{
Display* display = XOpenDisplay(NULL);
Window window = XCreateSimpleWindow(
display, XDefaultRootWindow(display),
100, 100, 200, 200, 4, 0, 0);
XEvent event;
XMapWindow(display, window);
XSelectInput(display, window, KeyPressMask | ButtonPressMask | ExposureMask);
while (True) {
XNextEvent(display, &event);
}
return 0;
}
Everything works fine, the window its drawed without problems.
The thing is i really want to understand how X11 works so im reading the source code of the headers but im unable to find the difinition for XMapWindow() i need help.
This was the only similutede i could find in the Xlib.h header file.
extern int XMapWindow(
Display* /* display */,
Window /* w */
);
You can find the implementation of XMapWindow() in the libX11 sources, specifically in src/MapWindow.c:XMapWindow().
It boils down to some locking, and a _XGetRequest(dpy, X_MapWindow, SIZEOF(xResourceReq)) call. That is defined in src/XlibInt.c:_XGetRequest(), and as you can guess by the name, adds the X_MapWindow request to the request queue sent to the X server.
How the X server (which manages the actual display) acts on that, is up to that X server implementation.

Xlib + Unity Only close allowed action

I just want to make a sample app when the only possible action is close.
Is this possible with Unity(Ubuntu) ?
Do i make a mistake ?
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
static void only_close(Display *display, Window window)
{
int retval;
Atom aa = XInternAtom(display, "_NET_WM_ALLOWED_ACTIONS", False);
Atom close = XInternAtom(display, " _NET_WM_ACTION_CLOSE", False);
retval = XChangeProperty(display, window, aa, XA_ATOM, 32, PropModeReplace, (unsigned char*)close, 1);
printf("###### XChangeProperty() reted %d\n", retval);
}
int main()
{
Display *dis;
Window win;
dis = XOpenDisplay(NULL);
win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, \
0, BlackPixel (dis, 0), BlackPixel(dis, 0));
XMapWindow(dis, win);
printf("window %i\n", (int)win);
only_close(dis, win);
XFlush(dis);
sleep(10);
return(0);
}
First, you need (unsigned char*)&close (an address of the data)
Second, you are setting the property too early, before WM has a chance to manage the window. A WM must discard the old value of this property when it first manages the window. Try after the first expose event, or just after a delay of 1 second.
Third, it is not guaranteed to work. On my machine (not Unity) the window actions in the taskbar are indeed disabled, but the window frame still has them the WM still allows them. I don't know if it's a bug in my WM or not.

Why window without borders is always on top

I'm trying to create window without borders (popup?) in xlib. I'm using this code:
#include <stdio.h>
#include <X11/Xlib.h>
int main( int argc, char **argv )
{
Display *display = NULL;
Window window;
XSetWindowAttributes attribs;
display = XOpenDisplay( NULL );
if( !display )
{
printf( "Cannot to open display." );
return 1;
}
attribs.override_redirect = 1;
window = XCreateWindow( display, RootWindow(display, 0), 20, 20, 400, 300, 0, CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect, &attribs );
XSetWindowBackground( display, window, 0x00F0FF );
XClearWindow( display, window );
XMapWindow( display, window );
XFlush( display );
getchar( );
return 0;
}
It creating window without borders, but this window is always on top.
The question is: Why and what to do in xlib to display it as a normal window.
That's how override-redirect windows are meant to behave. They are designed for implementing pop-up menus and similar windows which are transient and stay above other windows.
If that's not what you want, do not use override-redirect flag. Instead, use WM hints. See here for the full list of hints. You want to tell your WM which window type you have (_NET_WM_WINDOW_TYPE_TOOLBAR etc), not how it's decorated. See here for a usage example.
If that's still not what you want, use the (somewhat outdated) Motif WM hints. See for example here.

Make writing and lines appear on a GTK Window ... XLIB and GDK used

What my code (below) does:
Creates a XLIB window with a background color
Draws a string on the window
Draws a line on the window
Creates a GTK+ window
Makes the GTK+ window realise the XLIB window exsists via a GDK window
Display the output of the XLIB window inside the GTK+ window
It works and creates a window of the correct colour but it doesn't draw the string or the line on the screen. Any ideas of how to make it appear or where im going wrong?
The reason I am not using the GTK+ drawing functions is because this a test program in reality all the drawing needs to come from the xlib window.
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void destroy(GtkWidget *widget, gpointer data ) {
gtk_main_quit ();
}
int main(int argc, char** argv) {
GtkWidget *xwindow;
//Open the display
Display *display = XOpenDisplay(NULL);
int screen = DefaultScreen(display);
//Create the window
Window w = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,
200, 100, 20, black, 10201020);
XSelectInput(display, w, StructureNotifyMask);
XMapWindow(display, w);
GC gc = XCreateGC(display, w, 0, NULL);
for(;;) {
XEvent e;
XNextEvent(display, &e);
if (e.type == MapNotify)
break;
}
XDrawString(display, w, gc, 10, 10, "HelloWorld!", 12);
XDrawLine(display, w, gc, 10, 60, 180, 20);
XFlush(display);
//SET UP
gtk_init (&argc, &argv);
//xwindow
xwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect (xwindow, "destroy",
G_CALLBACK (destroy), NULL);
g_signal_connect (xwindow, "destroy",
G_CALLBACK (print), NULL);
gtk_widget_realize(xwindow);
xwindow->window = gdk_window_foreign_new((guint32)w);
//SET UP
gtk_widget_show(xwindow);
gtk_main ();
return 0;
}
I believe this is simply due to a misunderstanding of what "drawing" really means, here.
The Xlib drawing model isn't "state-ful", it won't remember that your particular window has had some text drawn in a particular location, and then a line, and so on. The drawing happens immediately when you request it, and is then considered "done", i.e. forgotten about at the protocol level.
When you wrap the X window in a GTK+ widget, it will likely cause the X window system to attempt to refresh the contents of the X window, but that doesn't do anything, which is why your initial graphics are lost.
In short, you need to be able to respond to requests to redraw the window as needed.

Xlib: window is created in wrong position

I have simple xlib program which creates window. I think it has to show window on the upper-left corner of the screen because I pass 0, 0 to XCreateSimpleWindow function, but it's in upper-middle side. Why ?
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char* argv[])
{
Display* display;
int screen_num;
Window win;
unsigned int display_width,display_height;
unsigned int width, height;
char *display_name = getenv("DISPLAY");
display = XOpenDisplay(display_name);
screen_num = DefaultScreen(display);
display_width = DisplayWidth(display, screen_num);
display_height = DisplayHeight(display, screen_num);
width = (display_width / 3);
height = (display_height / 3);
win = XCreateSimpleWindow(display, RootWindow(display, screen_num), 0, 0, width, height, 1, BlackPixel(display, screen_num), WhitePixel(display, screen_num));
XMapWindow(display, win);
XSync(display, False);
while(1) { }
}
The top level windows are placed (and dimensioned) by the window manager which does whatever it suit it. Often the size is respected but the position not (in order to leave place for decoration, in order to respect placement policy of leaving toolbars clear, ...)
Try on a display without a window manager if you want your request to be respected (use VNC or similar to get such a display, don't try to use your desktop like this)

Resources