Xlib: DisplayWidth / DisplayHeight
Vladimir Dergachev
volodya at mindspring.com
Fri Sep 8 10:05:31 UTC 2023
On Tue, 5 Sep 2023, Zbigniew wrote:
>> To help you see our point of view, imagine someone complaining to you that
>> even though the computer science course talked about trees and leaves
>> there were no actual trees anywhere in sight, and how exactly does that
>> help with CO2 emissions ?
>>
>> Display and Screen structures are abstractions that in some cases
>> correspond to physical devices and in some cases (many more cases
>> nowadays) do not. They are named "Display" and "Screen" but what they
>> actually are is determined by how the code actually works.
>
> The code I pasted had 14 lines (including two empty ones). So how it
> „actually works”, according to you?
I meant Xlib and Xserver code, not the code you wrote. Read the source of
the software you are using.
Considering your code:
#include <X11/Xlib.h>
#include <stdio.h>
int main(int argc,char **argv) {
Display *display = XOpenDisplay(NULL);
int screen_number = DefaultScreen(display);
int height = DisplayHeight(display, screen_number);
int width = DisplayWidth(display, screen_number);
XCloseDisplay(display);
printf("Height: %d\n", height);
printf("Width: %d\n", width);
return 0;
}
The code you wrote will return the width and height of the default screen.
This might or might not correspond to a physical display.
If you are doing this in a commercial setting you can use this as is, and
in the documentation for your program specify that it only supports
computers with one physical screen and no panning. And then start thinking
of a "Pro" version that makes use of xrandr.
If you are doing this for open source project, you should change your code
to:
#include <stdio.h>
typedef struct {
int x;
int y;
int width;
int height;
} ACTIVE_REGION;
void get_active_region(ACTIVE_REGION *ar, int display, int screen_number)
{
#ifdef HAVE_XRANDR
fprintf(stderr, "Xrandr support not implemented yet\n");
exit(-1);
#else
ar->x=0;
ar->y=0;
ar->width=DisplayWidth(display, screen_number);
ar->height=DisplayHeight(display, screen_number);
#endif
}
int main(int argc,char **argv) {
Display *display = XOpenDisplay(NULL);
/* TODO: add support for other screens and xrandr later */
int screen_number = DefaultScreen(display);
ACTIVE_REGION ar;
get_active_region(&ar, display, screen_number);
/* URGENT TODO: use "gengetopt" to provide command line parameters
to override values in get_active_region */
XCloseDisplay(display);
printf("Screen area: %dx%d+%d+%d\n", ar.width, ar.height, ar.x,
ar.y);
return 0;
}
This is assuming you want just one monitor, such as for screen sharing for
a Zoom or Skype-like application. There could be a situation when you want
to know the location of all physical screens.
It is important to let users override your choice, to enable less common
use cases such as purely virtual screens. Plain Xvnc is ok, but for
some of my headless machines I configured X to have a hardcoded display
with resolution slightly smaller than 4K. This way acceleration works for
compositing and one can use vnc to access a window without full-screen.
>
> To help you see my point of view, here's a follow-up question: and how
> do you think, reading my posts — and a code snippet, you've cut out so
> conveniently — what kind of „Display and Screen structure” I had in
> mind? A physical device, or the one from „some case”?
To be honest, I got a perception of an enthusiastic (and not overly
polite) teenager, who, unfortunately, grew up in a system that did not
teach geometric proofs or any other courses with proper mathematical
rigor. Its not really your fault - other people screwed things up
spontaneously and on purpose, but it is a giant gap you need to fill.
As a result you struggle to define what you mean. I was trying to
lead you to an "Aha" moment when you properly formulate what you are
trying to achieve.
Oh, and no I don't actually know what you mean, because it depends on what
application you are writing.
If you do teleconferencing, you might want to capture either the entire
screen, or some window.
If you want to record a movie of a game playing fullscreen, than you
probably need the position and dimensions of the game window, because
games often change video mode while keeping original virtual screen
intact.
If you want to make a better Xvnc, you probably need the code above and
you might not need xrandr.
If you are doing something else - who knows what you mean ?
best
Vladimir Dergachev
> --
> regards,
> Zbigniew
>
More information about the xorg
mailing list