How to get the DPI value?

Linux specific forum
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

How to get the DPI value?

Post by Little John »

Hi,

unfortunately I'm not familiar with Linux API programming.
I just tried the following (on Linux Mint 17.2 Cinnamon with PB 5.40 beta 6) in order to get the DPI value of the current screen:

Code: Select all

ImportC ""
   gdk_screen_get_resolution (*screen.GdkScreen)
EndImport


Define *screen.GdkScreen

*screen = gdk_screen_get_default_()
Debug *screen
Debug gdk_screen_get_resolution(*screen)
But that doesn't work, the output is
0
0
Any hints?
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: How to get the DPI value?

Post by Keya »

You just need to call gdk_init first :) as i only discovered last week heehee
PB calls that automatically for you when you create a new window, and also i think when you use a requester like messagebox - im assuming just once at program startup.
It's usually called like: gdk_init(argc,argv)
I havent figured out yet how to correctly pass argc and argv, but gdk_init_(0,0) works :P

for related example see my Hello World GTK SDK demo, although in that one im calling gtk_init instead of gdk_init as im only using gtk_ funcs :)
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to get the DPI value?

Post by Little John »

That's a step into the right direction. :-) Thank you.

Now I have this:

Code: Select all

ImportC ""
   gdk_screen_get_resolution(*screen.GdkScreen)
EndImport


Define *screen.GdkScreen

gdk_init_(0, 0)
*screen = gdk_screen_get_default_()
If *screen
   Debug gdk_screen_get_resolution(*screen)
EndIf
The output is: 1 :?
That value should be somewhat bigger, I think. :-)
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: How to get the DPI value?

Post by Keya »

Hrm, well https://developer.gnome.org/gdk3/stable ... resolution says it returns a Double, but even with that it's still returning just "1.0" on my Mint.
(Unless the way it's currently declared = Integer and not Double?)
But the doc suggests it's normally 96, or -1 if not set ... :(
*screen\resolution = 0 also
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: How to get the DPI value?

Post by Keya »

Yes, it's just the declaration ... needs to be declared as Double:
gdk_screen_get_resolution.d (*screen.GdkScreen)

Then we get the correct return value of -1.0 (unset) ... which still isn't much help heehee :)
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: How to get the DPI value?

Post by Oma »

Hi,

the current dpi-value you could get this way:

Code: Select all

ImportC ""
	g_object_get_property(*widget.GtkWidget, property.p-utf8, *gval)
	g_value_get_uint(gval)
EndImport

Global.i gEvent, gQuit
#G_TYPE_UINT  = 28

Procedure GetCurrentDpi()
   Protected   gVal.GValue
   Protected.i Ret
   
   g_value_init_(@gval, #G_TYPE_UINT)
   g_object_get_property(gtk_settings_get_default_(), "gtk-xft-dpi", @gval)
   Ret= g_value_get_uint(@gval)
   g_value_unset_(@gval)
 ProcedureReturn Ret
EndProcedure

If OpenWindow(0, 300, 200, 500, 200, "Get current dpi", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

   Debug GetCurrentDpi()/1024
   
   Repeat
      If WaitWindowEvent() = #PB_Event_CloseWindow
         gQuit= #True
      EndIf
   Until gQuit
EndIf
If you want further information about the monitor:

Code: Select all

EnableExplicit

ImportC ""
	gtk_widget_get_window(*widget.GtkWidget)
EndImport

ImportC ""
	gdk_window_get_screen(*Window)
	gdk_screen_get_monitor_height_mm(*screen.GdkScreen, monitor_num)
	gdk_screen_get_monitor_width_mm(*screen.GdkScreen, monitor_num)
	gdk_screen_get_monitor_plug_name(*screen.GdkScreen, monitor_num)
EndImport

; Object constants
#MainWin  = 0
#But1 = 0

#WinX=300
#WinY=200
#WinW=200
#WinH=200

Global.i gEvent, gQuit
Global *screen.GdkScreen


Procedure.i GetGdkWindow(Window)
	ProcedureReturn gtk_widget_get_window(WindowID(Window))
EndProcedure

Procedure GetWindowScreen(Window)
	ProcedureReturn gdk_window_get_screen(GetGdkWindow(Window))
EndProcedure

If OpenWindow(#MainWin, #WinX, #WinY, #WinW, #WinH, "Get Screen data", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget)
	ButtonGadget(#But1, 5, 5, 190, 26, "Debug screen data")
	
	Repeat
		gEvent= WaitWindowEvent()
		
		Select gEvent
			Case #PB_Event_CloseWindow
				gQuit= #True
			Case #PB_Event_Gadget
				If EventGadget()= #But1
					*screen= GetWindowScreen(#MainWin)
					Debug "Screen-adress                  : " + Str(*screen)
					Debug "Screen-index                   : " + Str(gdk_screen_get_number_(*screen))
					Debug "Display-Adress from Screen     : " + Str(gdk_screen_get_display_(*screen))
					Debug "Screen width                   : " + Str(gdk_screen_get_width_(*screen))
					Debug "Screen height                  : " + Str(gdk_screen_get_height_(*screen))
					Debug "Screen width [mm]              : " + Str(gdk_screen_get_width_mm_(*screen))
					Debug "Screen height [mm]             : " + Str(gdk_screen_get_height_mm_(*screen))
					Debug "Monitor number from Screen     : " + Str(gdk_screen_get_n_monitors_(*screen))
					Debug "Monitor width [mm] from Screen : " + Str(gdk_screen_get_monitor_width_mm(*screen, 0))
					Debug "Monitor height [mm] from Screen: " + Str(gdk_screen_get_monitor_height_mm(*screen, 0))
					Debug "Monitor plug name from Screen  : " + PeekS(gdk_screen_get_monitor_plug_name(*screen, 0), -1, #PB_Ascii)
				EndIf
		EndSelect
		
	Until gQuit
EndIf
Bye, Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to get the DPI value?

Post by Little John »

Hi Charly,

I was hoping that you would chime in here. :-)
Great!
Your code works fine on my system, Thank you very much!
Post Reply