Using GTK+ on the X Window System

## Using GTK+ on the X Window System Using GTK+ on the X Window System — X11 aspects of using GTK+ |

## GTK+ for the X Window System

On UNIX, the X backend is the default build for GTK+. So you don't need to do anything special when compiling it, and everything should "just work."

To mix low-level Xlib routines into a GTK program, see GDK X Window System interaction in the GDK manual.

### X11-specific commandline options

The X backend understands some additional command line arguments.

`--display `display``. The name of the X display to open instead of the one specified in the `DISPLAY` environment variable.

`--screen `screen_number``. The number of the screen within the default display. This overrides any screen number specified in the display name specified by by he `--display` command line option or the `DISPLAY` environment variable. If this screen cannot be opened, then GTK+ will fall back to the screen specified in the display name. This option is not useful interactively; the intended purposes is that when a program registers its command line with a session manager for later restarting, it can save the screen it is on, without having to worry if it might be restarted on a different display.

`--sync`. Makes all X requests synchronously. This is a useful option for debugging, but it will slow down the performance considerably.

`--gxid-host `host``. The host to contact the gxid daemon on; overrides the `GXID_HOST` environment variable.

`--gxid-port `port``. The port for the connection to gxid; overrides the `GXID_PORT` environment variable. This option is only available if GTK+ has been configured with `--gdk-target=x11`.

### X11-specific environment variables

The X backend can be influenced with some additional environment variables.

`GXID_HOST`, `GXID_PORT`. The host and port to contact the gxid daemon on. gxid is only necessary on X servers which don't support using the pointer and extension devices at once, and is only built if GTK+ is configured with `--with-xinput=gxi`. The XFree86 and Xorg X servers don't have this restriction.

`GDK_USE_XFT`. If this variable is set to 1, GTK+ will use the Pango Xft backend instead of the X backend when possible (i.e. when the X server supports the XRender extension and Pango has been built with Xft support).

## Understanding the X11 architecture

People coming from a Windows or MacOS background often find certain aspects of the X Window System surprising. This section introduces some basic X concepts at a high level. For more details, the book most people use is called the Xlib Programming Manual by Adrian Nye; this book is volume one in the O'Reilly X Window System series.

Standards are another important resource if you're poking in low-level X11 details, in particular the ICCCM and the Extended Window Manager Hints specifications. freedesktop.org has links to many relevant specifications.

The GDK manual covers using Xlib in a GTK program.

### Server, client, window manager

Other window systems typically put all their functionality in the application itself. With X, each application involves three different programs: the X server, the application (called a client because it's a client of the X server), and a special client called the window manager.

The X server is in charge of managing resources, processing drawing requests, and dispatching events such as keyboard and mouse events to interested applications. So client applications can ask the X server to create a window, draw a circle, or move windows around.

The window manager is in charge of rendering the frame or borders around windows; it also has final say on the size of each window, and window states such as minimized, maximized, and so forth. On Windows and MacOS the application handles most of this. On X11, if you wish to modify the window's state, or change its frame, you must ask the window manager to do so on your behalf, using an established convention.

GTK+ has functions for asking the window manager to do various things; see for example gtk_window_iconify() or gtk_window_maximize() or gtk_window_set_decorated(). Keep in mind that gtk_window_move() and window sizing are ultimately controlled by the window manager as well and most window managers will ignore certain requests from time to time, in the interests of good user interface.