Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen resolution in c++ for linux [closed]

i write a program who would have to interact with the mouse, so I would like to get the screen/monitor widht and height in c++, for linux. I search on google and here and didn't find anything. Thank you

like image 568
MelvinFrohike42 Avatar asked Dec 26 '22 04:12

MelvinFrohike42


1 Answers

You can use XLibs functions to get the size of a display.

For example, for de default display :

#include <X11/Xlib.h>

Display* d = XOpenDisplay(NULL);
Screen*  s = DefaultScreenOfDisplay(d);

XOpenDisplay(NULL) to get the main Display of your X server (assuming you have a basic X config with only one display...)

Then get the screen you want the resolution from. For the main screen, use DefaultScreenOfDisplay, otherwise use : ScreenOfDisplay(display, screen_nb).

Then you get your Screen * structure.

You can access to the height and the width member to get the resolution !

s->height;

s->width;

This is better than using WidthOfScreen/HeightOfScreen because it take only one request to populate the Screen struct. And you probably aldready have it if you are aldready using Xlib in your program and you have the choice of the screen (in case of multiple screen).

You can get the number of screens running on your display by using ScreenCount(display) function

like image 148
Thomas Leclercq Avatar answered Dec 31 '22 13:12

Thomas Leclercq