Getting all available screen resolutions on Linux

Hey everyone,
I’m trying to fetch all currently available resolutions of the screen. The problem: I’m only getting the active screen resolution (on Linux). the following code works on Mac & Windows but not on Linux:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice display = ge.getDefaultScreenDevice();
DisplayMode[] availableModes = display.getDisplayModes();
		
for (DisplayMode mode : availableModes) {
    System.out.println(mode.getWidth() + "x" + mode.getHeight());
}

Any suggestions? ???

I do pretty much the same. It does work, but results differ between Windows and Ubuntu.

Maybe you could try to retrieve all the display devices instead of just the default one?

Hi

Sorry to say that but AWT has some bugs and limitations, not only under GNU Linux. I get all screen resolutions with NEWT (JOGL 2.0). NEWT will even support XrandR 1.3 soon which means that it will be able to expose a lot more information about virtual and physical screens/monitors. Maybe this limitation has disappeared in JavaFX, look at the class javafx.stage.Screen.

Edit.: I think that your code doesn’t look at all cases, it can be fixed…

Edit.2: Maybe Oskuro is right, you should look at all devices.

My experience is that it is indeed unpredictable, probably requiring some low level wizardry to get actual results, which kind of defeats the multi-platform approach.

I did some research trying to determine the screen rotation angle, which lead me to compiling system-dependent libraries that would query the information from the system.

Something worth noting I found was that even on the official libraries, many low level features are just ignored. The rotation parameter, for example, was ignored as it wasn’t completely reliable (driver issues) and wasn’t very important when implemented.

With this, what I’m trying to say is that, if accessing system configuration parameters is crucial, you can’t really rely on standard multi-platform libraries entirely, if only because time constraints (deadlines) probably mean the developers couldn’t fit everything in.

You can rely on NEWT which is not concerned by your affirmations.

Cool! I’ll have to check it out then!

@myrealityde
Oh, by the way, here’s the output for my laptop running Ubuntu 12.04:



// Recover the Graphics Environment (usually no need to store it, but just being verbose for clarity)
java.awt.GraphicsEnvironment localEnvironment = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();

// Recover Device List
java.awt.GraphicsDevice[] deviceList = localEnvironment.getScreenDevices();

// Recover Display Modes for Device 'i'
java.awt.DisplayMode[] modeList = deviceList[i].getDisplayModes();

// Recover Graphics Configurations for Device 'i'
java.awt.GraphicsConfiguration[] gfxConfigList = deviceList[i].getConfigurations();

This is the result. The first list is a log of all the graphics modes (my laptop only returns a single device), and then the first log of the many graphics configurations (didn’t want to post them all).

Note that I parse the data a bit, you can’t just .toString() this stuff. :stuck_out_tongue:


20130419-223847 - [DEBUG]: -----------------------------------------------Display Info:
Device [0]: :0.0
     nk0.doom4k.system.display.DisplayDevice@70c722ad
  Compatible Modes:
    Mode [0] Size[1920x1080] Depth[-1] Refresh[60]
    Mode [1] Size[1600x1200] Depth[-1] Refresh[60]
    Mode [2] Size[1680x1050] Depth[-1] Refresh[60]
    Mode [3] Size[1280x1024] Depth[-1] Refresh[75]
    Mode [4] Size[1280x1024] Depth[-1] Refresh[60]
    Mode [5] Size[1440x900] Depth[-1] Refresh[75]
    Mode [6] Size[1440x900] Depth[-1] Refresh[60]  ACTIVE
    Mode [7] Size[1280x960] Depth[-1] Refresh[60]
    Mode [8] Size[1280x800] Depth[-1] Refresh[60]
    Mode [9] Size[1152x864] Depth[-1] Refresh[75]
    Mode [10] Size[1024x768] Depth[-1] Refresh[75]
    Mode [11] Size[1024x768] Depth[-1] Refresh[70]
    Mode [12] Size[1024x768] Depth[-1] Refresh[60]
    Mode [13] Size[832x624] Depth[-1] Refresh[75]
    Mode [14] Size[800x600] Depth[-1] Refresh[72]
    Mode [15] Size[800x600] Depth[-1] Refresh[75]
    Mode [16] Size[800x600] Depth[-1] Refresh[60]
    Mode [17] Size[800x600] Depth[-1] Refresh[56]
    Mode [18] Size[640x480] Depth[-1] Refresh[73]
    Mode [19] Size[640x480] Depth[-1] Refresh[75]
    Mode [20] Size[640x480] Depth[-1] Refresh[67]
    Mode [21] Size[640x480] Depth[-1] Refresh[60]
    Mode [22] Size[720x400] Depth[-1] Refresh[70]

20130419-223847 - [DEBUG]: -------------------------------------Device [ :0.0 ] Info:

GraphicsConfig[0]:  DEFAULT
Configuration [ X11GraphicsConfig[dev=X11GraphicsDevice[screen=0],vis=0x21] ]: 
   Bounds:                java.awt.Rectangle[x=0,y=0,width=1440,height=900]
   BufferCapabilities:    sun.awt.X11GraphicsConfig$XDBECapabilities@3cca6f82
   ColorModel:            DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0

   DefaultTransform:      AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
                          Type:        0
                          Scale X:     1.0
                          Scale Y:     1.0
                          Shear X:     0.0
                          Shear Y:     0.0
                          Translate X: 0.0
                          Translate Y: 0.0

   ImageCapabilities:     java.awt.ImageCapabilities@1c78014a

   NormalizingTransform:  AffineTransform[[1.333333333333333, 0.0, 0.0], [0.0, 1.334033613445378, 0.0]]
                          Type:        4
                          Scale X:     1.3333333333333333
                          Scale Y:     1.3340336134453783
                          Shear X:     0.0
                          Shear Y:     0.0
                          Translate X: 0.0
                          Translate Y: 0.0

   isTranslucencyCapable: NO

   ImageCapabilities:     IsAccelerated:  YES
                          IsTrueVolatile: NO


As you can see, a pretty simple code results in a lot of data that you don’t seem to be getting. Probably the drivers are to blame (HP laptop with hybrid Intel/ATI card in my case).

I don’t know what you have chosen to hide when using AWT but NEWT is able to return the size of the physical/virtual monitor in mm, the rotation (more useful for tablets), … and it has a really reliable full screen support under GNU Linux ;D

???

You said “you can’t just .toString()”, didn’t you?

Oh, just meant the fancy formatting of all the data was my doing (as in manually), to avoid anyone thinking that there is some fast method to print it out like that.

Been checking NEWT. It’s part of JOGL right? Is there a stand-alone version that can be plugged in into something else?

Yes NEWT is a “part” of JOGL but you can use GLG2D with it if you want to use Java2D for example. NEWT is very cross-platform, it works under Android too and at least one developer has worked for some months to port it to iOS.

Intriguing. Thanks, it is now part of my “To Check Out” list ;D

Thanks a lot for all the answers! I’ll have a look over my code. :wink:

Thanks a lot for all the answers! I’ll have a look over my code. :wink:

On linux you will often need some mode lines in your xorg config file. Since these days there isn’t one. You end up with just one valid mode, the current one. I have always had to add modelines when i want to use more than the current mode on my linux distros with nvidia drivers.

On linux you will often need some mode lines in your xorg config file. Since these days there isn’t one. You end up with just one valid mode, the current one. I have always had to add modelines when i want to use more than the current mode on my linux distros with nvidia drivers.