PaperMoon
PaperMoon

Reputation: 105

gdk_screen_get_default() returns null

Please help with this beginner's program for fetching gdk screen attributes. I have a small C++ program to find out the connected display units. I am using c++ on Linux Debian. gdk_screen_get_default() doesn't return Screen object. If I don't check for screen object then the following error occurs.

Error (process:8023): Gdk-CRITICAL **: gdk_screen_get_monitor_geometry: assertion 'GDK_IS_SCREEN (screen)' failed

I went through related posts and referred to this for the below code snippet.

Thanks for your help. Any pointers/guidelines to resolve this would be helpful.

I have one monitor connected and the display settings are

$ echo $XDG_CURRENT_DESKTOP
GNOME

$ echo $DISPLAY
:0

CODE

    #include <gdk/gdk.h>
    #include <iostream>

    /*
    GTK version 3.14.5
    g++ getScreenInfo.cpp -o getScreenInfo `pkg-config gtk+-3.0 --cflags --libs`
    */

    int main()
    {
       GdkScreen *screen;
       screen = gdk_screen_get_default();           
       int num_monitors;
       int i;

       if (screen)
       {
          num_monitors = gdk_screen_get_n_monitors(screen);

          for (i = 0; i < num_monitors; i++)
          {
              GdkRectangle rect;
              gdk_screen_get_monitor_geometry (screen, i, &rect);
              std::cout << "monitor " << i << ": coordinates (" << rect.x << ","
              <<  rect.y << ", size (" << rect.width << "," << rect.height << ")"
              << std::endl;
           }
       }else
       {
            std::cout << "Couldn't obtain default screen object" << std::endl;
        }
    }

27 Apr 2017 EDIT: RESOLVED

  #include <iostream>
  #include <gdk/gdk.h>
  #include <gtk/gtk.h>

  /*
  GTK version 3.14.5

  To compile:

  g++ getScreenInfo.cpp -o getScreenInfo `pkg-config gtk+-3.0 --cflags --libs`

  */

  int main(int argc, char *argv[])
  {
    gtk_init(&argc, &argv);

    GdkScreen *screen = gdk_screen_get_default();



    int num_monitors;
    int i;

    if (screen)
    {
        num_monitors = gdk_screen_get_n_monitors(screen);

        for (i = 0; i < num_monitors; i++)
        {
            GdkRectangle rect;
            gdk_screen_get_monitor_geometry (screen, i, &rect);
            std::cout << "monitor " << i << ": offsets (" << rect.x << ","
            <<  rect.y << ", size (" << rect.width << "," << rect.height << ")"
            << std::endl;
        }
    }
    else
    {
        std::cout << "Couldn't obtain default screen object" << std::endl;
    }

    // To query primary display properties 

    guint monitor = gdk_screen_get_primary_monitor(screen);
    GdkRectangle screen_geometry = { 0, 0, 0, 0 };
    gdk_screen_get_monitor_geometry(screen, monitor, &screen_geometry);
    std::cout << screen_geometry.x << std::endl;
    std::cout << screen_geometry.y  << std::endl;
    std::cout << screen_geometry.width  << std::endl;
    std::cout << screen_geometry.height << std::endl;
  }

Upvotes: 2

Views: 1948

Answers (1)

PaperMoon
PaperMoon

Reputation: 105

Answering my own question.

Finally, figured out the resolution. gtk_init( ) was missing before fetching the screen. Added that and respective include for gtk/gtk.h and now the code looks like

int main(int argc, char *argv[]) 
{ 
   gtk_init(&argc, &argv);                 // <---- added this

   GdkScreen *screen = gdk_screen_get_default(); 

   : 
   followed by rest of the code shared in the problem description above. 

Upvotes: 6

Related Questions