Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the screen size by Perl?

54 views
Skip to first unread message

kaiwa...@unlv.edu

unread,
Jul 26, 2015, 12:53:38 PM7/26/15
to
Hi to all,
I use the code to get screen size by perl in windows system:
$size=`wmic DESKTOPMONITOR get screenwidth,screenheight`;

But this code cannot get the screen size in Unix system. How to get screen size by perl in any operation system?
Thanks!

George Mpouras

unread,
Jul 26, 2015, 3:59:58 PM7/26/15
to
this gnome/kde question "/usr/bin/xinput list --short" etc etc

gamo

unread,
Jul 26, 2015, 4:05:21 PM7/26/15
to
El 26/07/15 a las 18:53, kaiwa...@unlv.edu escribió:
Simply you need the output of `tput cols` and `tput lines`
HTH

--
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance

T

unread,
Jul 26, 2015, 5:21:48 PM7/26/15
to
No monitor size here (Xfce 4.8):

$ /usr/bin/xinput list --short
⎡ Virtual core pointer id=2 [master pointer
(3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer
(2)]
⎜ ↳ Macintosh mouse button emulation id=9 [slave pointer
(2)]
⎜ ↳ MOSART Semi. 2.4G Wireless Mouse id=10 [slave pointer
(2)]
⎜ ↳ Logitech USB Laser Mouse id=11 [slave pointer
(2)]
⎣ Virtual core keyboard id=3 [master keyboard
(2)]
↳ Virtual core XTEST keyboard id=5 [slave
keyboard (3)]
↳ AT Translated Set 2 keyboard id=6 [slave
keyboard (3)]
↳ Power Button id=7 [slave
keyboard (3)]
↳ Power Button id=8 [slave
keyboard (3)]

T

unread,
Jul 26, 2015, 5:32:52 PM7/26/15
to
Hi Kaiwang,

Although it does not answer your question about all OS'es, this
would be for Linux. Maybe it will help until you can come up with
a universal call?

Bash script:
$ xdpyinfo | grep 'dimensions:'
dimensions: 1920x1080 pixels (519x292 millimeters)

Perl with a system call:
$ perl -e 'print (grep /dimensions:/, (qx (xdpyinfo))), "\n"'
dimensions: 1920x1080 pixels (519x292 millimeters)

HTH,
-T

Jens Thoms Toerring

unread,
Jul 27, 2015, 11:25:34 AM7/27/15
to
kaiwa...@unlv.edu wrote:
> I use the code to get screen size by perl in windows system:
> $size=`wmic DESKTOPMONITOR get screenwidth,screenheight`;

This is a call to a Windows utility - with the backticks
you make perl execute that command and store the returned
string in the '$size' variable.

I don't know what this Windows uility returns, I just
can guess that it's the width and height in pixels of
the screen.

> But this code cannot get the screen size in Unix system. How to get screen
> size by perl in any operation system?

There is none. The problem starts with that there is no
necessity that there's a screen at all. This is rather
unlikely to happen under Windows but, for example, many
Unix servers don't even have a graphics card (and no
keyboard or mouse either) - you log into them using e.g.
ssh and input and output is done via a remote terminal.
On the other hand, there are machines with more than one
monitor, which can be managed to appear as a single
"screen", but also as several ones. For whch of those
do you want the screen size?

If you're on a Unix machine that has a monitor and has
the X Windows system runnig (which is not always the
case, some may just have a console, i.e. a kind of
terminal). then, as 'T' pointed out, the information
about the screen dimensions can be obtained with the
'xdpyinfo' utility. Its output has a line with the
dimensions for each screen attached. But also that in-
formation might be a bit off if someone is using
'xrandr' to rotate the screen...

And then there are other operating systems...

If you write a a program that uses graphics (and otherwise,
why would you need to know about the screen size?) you're
rather likely be using some module for doing the graphics
stuff. And the best bet is that this module has some func-
tion that lets you ask for the screen size in a system-
independent way.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

kaiwa...@unlv.edu

unread,
Jul 27, 2015, 3:25:35 PM7/27/15
to
> why would you need to know about the screen size


If I could know the size of client screen,I can popup out an application window locating at any position of client screen,such as using the following code:
my $mw=MainWindow->new(-title=>'My App');
my $W=400;
my $H=400;
$xPos=($ScreenWidth-$W)/2;#this can locate the mw at any position I want to place
$yPos=($ScreenHeight-$H)/2;
$mw->geometry("$Wx$H+$xPos+$yPos");

Jens Thoms Toerring

unread,
Jul 27, 2015, 6:30:13 PM7/27/15
to
kaiwa...@unlv.edu wrote:
> > why would you need to know about the screen size

I didn't question your need for knowing the screen size
(except for the case that you aren't writing a GUI program;-)

> If I could know the size of client screen,I can popup out an application
> window locating at any position of client screen,such as using the following
> code:

> my $mw=MainWindow->new(-title=>'My App');

Assuming that you use the Tk module (what you have there
looks quite familiar) you have a simple, system-indendent
method for getting at the screen width and height:

my $ScreenWidth = $mw->screenwidth( );
my $ScreenHeight = $mw->screenheight( );

And there's no invocation of any external, typically system-
dependent, programs involved. You can now proceed as before
with

> my $W=400;
> my $H=400;
> $xPos=($ScreenWidth-$W)/2;#this can locate the mw at any position I want to place
> $yPos=($ScreenHeight-$H)/2;
> $mw->geometry("$Wx$H+$xPos+$yPos");

All graphical libraries/modules/whatever I've ever used had
functions for getting such basic information about the dis-
play system (since they need that information themselves any-
way). So if you don't use Tk but something else I'd be rather
disappointed if there'd be nothing similar for you to use!

Best regards, Jens

kaiwa...@unlv.edu

unread,
Jul 27, 2015, 6:42:29 PM7/27/15
to
Thank you very much!
I can use that code to get the client screen size for my application.

Jürgen Exner

unread,
Jul 27, 2015, 7:37:48 PM7/27/15
to
kaiwa...@unlv.edu wrote:
>> why would you need to know about the screen size
>
>
>If I could know the size of client screen,I can popup out an application window locating at any position of client screen,such as using the following code:

How would you do that on a virtual TTY?

jue

C.DeRykus

unread,
Jul 27, 2015, 10:08:16 PM7/27/15
to
perldoc -q screen

...

How do I get the screen size?
If you have "Term::ReadKey" module installed from CPAN, you can use it
to fetch the width and height in characters and in pixels:

...

--
Charles DeRykus

use Term::ReadKey;
($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
0 new messages