Is there any way to get screen dimensions of a display via Xlib using C? I've tried referring to the X.org docs (https://tronche.com/gui/x/xlib/display/screen-information.html), and it only gives me macros which do not accept integers, so I'm at a loss.

I specifically used these functions in this way:

int sx;
int sy;


move_window(sx / 4)
move_window(sy / 4)

I'm a noob to C programming, and am working on my first project.

  • 1
    Your program has to open the display. Reading the source for xdpyinfo would be a start. – Thomas Dickey Mar 15 at 23:55
  • 1
    I'm modding a window manager, so it does this for me. sowm to be exact. (github.com/dylanaraps/sowm) Should I specify this in the question? – user396530 Mar 15 at 23:58
  • @user396530 Yes, you'd better give more complete information in your question regarding your situation. Also, describe in more details any research you've done prior to asking this question, and what you have attempted and how those attempts failed. Otherwise, I believe, X.org and working with it through C API must be on-topic here. – imz -- Ivan Zakharyaschev Mar 19 at 18:59

This is a simple example, which will print the display size and move the window of the terminal emulator it runs in (if correctly set in the WINDOWID environment variable) to width / 4, height / 4:

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

int main(int ac, char **av){
        char *wid; Display *dpy; Window w;
        int width, height, snum;
        if(!(dpy = XOpenDisplay(0)))
                errx(1, "cannot open display '%s'", XDisplayName(0));
        snum = DefaultScreen(dpy);
        width = DisplayWidth(dpy, snum);
        height = DisplayHeight(dpy, snum);
        printf("display size is %d x %d\n", width, height);
        if((wid = getenv("WINDOWID")) && (w = strtoul(wid, 0, 0))){
                XMoveWindow(dpy, w, width / 4, height / 4);
                XSync(dpy, False);
        return 0;

My advice would be a copy of The X11 Programming Manual, which is a single page html which you can search through and contains everything about X11 programming.

Notice that there may be more friendly or portable libraries, like gtk, qt, sdl, etc. You may want to use one of those instead of libX11 directy.

| improve this answer | |
  • Just another quick question: how do you obtain dpy programmatically? – user396530 Mar 16 at 0:13
  • 1
    Via XOpenDisplay, as in my example ;-) – mosvy Mar 16 at 0:13
  • Thank you for your helpful code sample and recommendation. :D – user396530 Mar 16 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.