Skip to content

Instantly share code, notes, and snippets.

@vadz
Created October 29, 2018 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadz/074d3059b35017f79c2edb754deaa25e to your computer and use it in GitHub Desktop.
Save vadz/074d3059b35017f79c2edb754deaa25e to your computer and use it in GitHub Desktop.
Show display sizes and DPI using various GTK+ functions
// Compile using the following command
//
// g++ -Wall -Wno-deprecated-declarations gtkdpi.cpp `pkg-config --cflags --libs gtk+-3.0`
//
// And then run ./a.out
#include <gtk/gtk.h>
static void
print_sizes_and_dpi(int w, int h, int wmm, int hmm)
{
printf(": %d*%d (%d*%dmm) => %.2f*%.2f DPI\n",
w, h, wmm, hmm, 25.4*w/wmm, 25.4*h/hmm);
}
static void
activate (GtkApplication* app,
gpointer user_data)
{
GdkScreen* const s = gdk_screen_get_default();
printf("gdk_screen_get_{width,height}");
print_sizes_and_dpi(gdk_screen_get_width(s), gdk_screen_get_height(s),
gdk_screen_get_width_mm(s), gdk_screen_get_height_mm(s));
GdkRectangle r;
const gint n = gdk_screen_get_n_monitors(s);
for (int i = 0; i < n; ++i) {
gdk_screen_get_monitor_geometry(s, i, &r);
printf("gdk_screen_get_monitor(%d)", i);
print_sizes_and_dpi(r.width, r.height,
gdk_screen_get_monitor_width_mm(s, i),
gdk_screen_get_monitor_height_mm(s, i));
}
GdkDisplay* const d = gdk_display_get_default();
GdkMonitor* const m = gdk_display_get_primary_monitor(d);
gdk_monitor_get_geometry(m, &r);
printf("gdk_monitor_get_geometry");
print_sizes_and_dpi(r.width, r.height,
gdk_monitor_get_width_mm(m),
gdk_monitor_get_height_mm(m));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment