title: gdk_screen_get_default() returns null
body: <p>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. <code>gdk_screen_get_default()</code> doesn't return <code>Screen</code> object. If I don't check for screen object then the following error occurs.</p>
  
  <blockquote>
    <p><strong>Error</strong>
        (process:8023): Gdk-CRITICAL **: gdk_screen_get_monitor_geometry: assertion 'GDK_IS_SCREEN (screen)' failed</p>
  </blockquote>
  
  <p>I went through related posts and referred to <a href="https://stackoverflow.com/questions/836086/programmatically-determining-individual-screen-widths-heights-in-linux-w-xinera">this</a> for the below code snippet.</p>
  
  <p>Thanks for your help. Any pointers/guidelines to resolve this would be helpful. </p>
  
  <p>I have one monitor connected and the display settings are</p>
  
  <pre><code>$ echo $XDG_CURRENT_DESKTOP
  GNOME
  
  $ echo $DISPLAY
  :0
  </code></pre>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>    #include &lt;gdk/gdk.h&gt;
      #include &lt;iostream&gt;
  
      /*
      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 &lt; num_monitors; i++)
            {
                GdkRectangle rect;
                gdk_screen_get_monitor_geometry (screen, i, &amp;rect);
                std::cout &lt;&lt; "monitor " &lt;&lt; i &lt;&lt; ": coordinates (" &lt;&lt; rect.x &lt;&lt; ","
                &lt;&lt;  rect.y &lt;&lt; ", size (" &lt;&lt; rect.width &lt;&lt; "," &lt;&lt; rect.height &lt;&lt; ")"
                &lt;&lt; std::endl;
             }
         }else
         {
              std::cout &lt;&lt; "Couldn't obtain default screen object" &lt;&lt; std::endl;
          }
      }
  </code></pre>
  
  <p><strong>27 Apr 2017 EDIT: RESOLVED</strong></p>
  
  <pre><code>  #include &lt;iostream&gt;
    #include &lt;gdk/gdk.h&gt;
    #include &lt;gtk/gtk.h&gt;
  
    /*
    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(&amp;argc, &amp;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 &lt; num_monitors; i++)
          {
              GdkRectangle rect;
              gdk_screen_get_monitor_geometry (screen, i, &amp;rect);
              std::cout &lt;&lt; "monitor " &lt;&lt; i &lt;&lt; ": offsets (" &lt;&lt; rect.x &lt;&lt; ","
              &lt;&lt;  rect.y &lt;&lt; ", size (" &lt;&lt; rect.width &lt;&lt; "," &lt;&lt; rect.height &lt;&lt; ")"
              &lt;&lt; std::endl;
          }
      }
      else
      {
          std::cout &lt;&lt; "Couldn't obtain default screen object" &lt;&lt; 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, &amp;screen_geometry);
      std::cout &lt;&lt; screen_geometry.x &lt;&lt; std::endl;
      std::cout &lt;&lt; screen_geometry.y  &lt;&lt; std::endl;
      std::cout &lt;&lt; screen_geometry.width  &lt;&lt; std::endl;
      std::cout &lt;&lt; screen_geometry.height &lt;&lt; std::endl;
    }
  </code></pre>