Finding the resolution of the desktop

BlitzMax Forums/BlitzMax Programming/Finding the resolution of the desktop

ImaginaryHuman(Posted 2006) [#1]
How do I find the resolution (and possibly also depth and hertz) of the desktop, in a fully cross-platform compatible way, and preferably one method that works on all three platforms?

I had an idea to do it, although not very directly...

Open a window the size of the largest resolution available, acquired using CountGraphicsModes() and GraphicsModeExists() etc. The window opens on the desktop even if it is larger than the screen. Doesn't matter where it is positioned.

Next, move the mouse with MoveMouse() to 0,0. Then read the x and y mouse coords. Presumably, if the top left of the window is `off-screen`, the o/s will have cropped the mouse positioning and put it in the top left corner of the screen. The coords should tell you what `offset` you have in the top left corner.

Then position the mouse at the lower right corner of your window with MoveMouse() e.g. MoveMouse(1600,1200). Then read the x and y coords again and if they are less than the window size the o/s should have cropped the mouse's position and it should tell you how much of your window is visible.

Then close the window and open a new window with the visible size that you found. Then restore the mouse pointer to the position it was in originally.

????? I guess it would work. Any reasons why not?

I guess you'd have to take into account that at least on the Mac you can't open a window that covers the title bar, and maybe on the PC you can't open a window that covers the task bar (not sure about Linux) .... so should subtract a bit from the verticle size? Also what about window borders?

Is there a better reliable way?

Also, how to get GraphicsHertz()? Does it work in OpenGL and DirectX?

And how to get the color depth? Draw something with an 8-bit-per-component color resolution, flip the display, set the glReadBuffer() to the front buffer, grab the drawn piece into a 32-bit pixmap, look at the color resolution to see if what you drew came out in 32-bit or 16-bit? Also if it dithered, you might be able to detect some `un-pure` pixels? This would tell you if the desktop is 32-bit or 16-bit or whatever? Would it work?

Any other solutions?


Muttley(Posted 2006) [#2]
Desktop().width
Desktop().height


ImaginaryHuman(Posted 2006) [#3]
Is that a standard BlitzMax function, non-MaxGUI?


ImaginaryHuman(Posted 2006) [#4]
GraphicsHertz() works on my Mac, but Desktop() doesn't exist.


Beaker(Posted 2006) [#5]
Its a MaxGui only command.


ImaginaryHuman(Posted 2006) [#6]
The mouse technique I mentioned does not work ---- if you position the mouse at 0,0, it still reports that it is at 0,0, even though the o/s displays the mouse at an offset. Same with the bottom right corner, it returns the size of the window rather than whether the mouse is actually in the bottom right - ie the o/s shows the mouse within the screen, but reports its position as outside the screen. Oh well.

Maybe I'll have to look into MaxGUI some more, just to get the desktop() command.


d-bug(Posted 2006) [#7]
Okay, Here is a little module hamZta and I once wrote to solve this problem. It only fits the resolution-part of your question, but that is the half way... :D

This one is limited to MacOS and Win32 since nobody could help me with the Linux code. I thought of waiting until the Linux part is done, but now i will provide it to help you out.

There are only two little functions in the module. DesktopWidth() and DesktopHeight(). Both return an Integer.

have fun with it ;)


ImaginaryHuman(Posted 2006) [#8]
Cool i'll give it a try.


Yan(Posted 2006) [#9]
I ripped this out of my Windows screen saver code. I haven't got a clue how you'd do the Mac and Linux stuff. It's a start though. ;o)
Strict

Local w, h, d, r

DesktopGraphics(w, h, d, r)
Print "Desktop : " + h + "x" + w + "x" + d + "@" + r + "Hz"

End 

Function DesktopGraphics(width Var, height Var, depth Var, refresh Var)
?Win32 
	Local hdc = GetDC(GetDesktopWindow())
	If hdc = Null Then Return True
	
	width = GetDeviceCaps(hdc, HORZRES)
	height = GetDeviceCaps(hdc, VERTRES)
	
	depth = GetDeviceCaps(hdc, BITSPIXEL)	
	If depth < 8
		depth = GetDeviceCaps(hdc, PLANES)
	EndIf	
	
	refresh = GetDeviceCaps(hdc, VREFRESH)
	If refresh < 60 Then refresh = -1
?MacOS
	'???
?Linux
	'???
?
End Function



hamZta(Posted 2006) [#10]
@KevinTheKaleidoscopicKoala: This is exactly what d:bug's module does, plus MacOS support.


Yan(Posted 2006) [#11]
I was just going on d:bug's post...
It only fits the resolution-part of your question, but that is the half way.
There are only two little functions in the module. DesktopWidth() and DesktopHeight().
I wasn't trying to step on anyone's toes.


ImaginaryHuman(Posted 2006) [#12]
Well it's a start. I guess I could buy MaxGUI and just use Desktop() gadget which is known to be cross platform compatible.

I need OpenGL based version more than anything, not DX, cus I'm on a Mac.


d-bug(Posted 2006) [#13]
@AngelDaniel
just updated the archive...

- Optimized the Win32 part
- Added DesktopBits() and DesktopHertz()

Sorry, still no Linux stuff

@KevinTheKaleidoscopicKoala
Hey, keep cool... You didn't step on anything. In fact, we helped each other. You showed me how to optimize my Win32 part and you can copy the MacOS part by opening the bmx file from my module, if hamZta will give you the permission to provide his macscreen.c ...

cheers


skidracer(Posted 2006) [#14]
From memory (may be wrong) the GetGraphicsMode with index 0 will return the Linux Desktop resolution.


Yan(Posted 2006) [#15]
@d:bug - Hey, no problem. There's no point having two solutions doing exactly the same thing, so I'd probably just use your mod if I need this in future. :o)

@skidracer - Would it be possible to allow GetGraphicsMode() to return the desktop info on all platforms, possibly using an index of -1 for backwards compatibility?

It'd be a very useful thing to have.


ImaginaryHuman(Posted 2006) [#16]
Yah is that cross-platform feature?
Come to think of it probably not, eh? Cus I get the graphics modes from 0 to the number of modes-1.


hamZta(Posted 2006) [#17]
@KevinTheKaleidoscopicKoala: Sorry, didn't want to sound that rude ;)
Feel free to use "macscreen.c"