After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 601712 - Add API for determining primary monitor
Add API for determining primary monitor
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Widget: Other
unspecified
Other All
: Normal enhancement
: ---
Assigned To: Cody Russell
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2009-11-12 16:01 UTC by Cody Russell
Modified: 2011-02-04 16:12 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch (1.47 KB, patch)
2009-11-12 16:02 UTC, Cody Russell
none Details | Review
Updated patch (4.04 KB, patch)
2009-11-21 16:05 UTC, Cody Russell
none Details | Review
Add support to GdkDisplay::monitors-changed signal (6.33 KB, patch)
2009-11-21 16:53 UTC, Cody Russell
needs-work Details | Review
Win32 implementation (untested) (1.92 KB, patch)
2009-11-21 17:22 UTC, Cody Russell
none Details | Review
Sets primary_monitor = -1 unless it's set otherwise by xrandr. (6.58 KB, patch)
2009-12-03 01:37 UTC, Cody Russell
none Details | Review

Description Cody Russell 2009-11-12 16:01:39 UTC
Title says it all.  Do we also need API to *set* the primary monitor?

Here's an untested patch for querying the primary monitor that I wrote really quick while in a boring meeting. :)  I'll test when I get home, and write a corresponding 'set' function if one is wanted.
Comment 1 Cody Russell 2009-11-12 16:02:06 UTC
Created attachment 147585 [details] [review]
Patch
Comment 2 Christian Dywan 2009-11-12 16:26:42 UTC
Looks like you forgot the part of the patch that adds the function?
Comment 3 Cody Russell 2009-11-21 16:05:58 UTC
Created attachment 148232 [details] [review]
Updated patch

I'm trying to test this, but I can't find any UI anywhere for how to actually *set* the primary monitor. :/
Comment 4 Cody Russell 2009-11-21 16:53:35 UTC
Created attachment 148234 [details] [review]
Add support to GdkDisplay::monitors-changed signal

Okay, I figured out how to test using xrandr command-line utility and the patch worked as expected.  For my machine the commands to switch between the two different monitors were:
  xrandr --output LVDS1 --primary
  xrandr --output VGA1 --primary

This version of the patch changes GdkDisplay to emit "monitors-changed" when the primary monitor changes (see the change in _gdk_x11_screen_size_changed).

testxinerama is also updated to display the primary monitor, and to listen to "monitors-changed" and show the primary monitor.
Comment 5 Cody Russell 2009-11-21 17:22:35 UTC
Created attachment 148238 [details] [review]
Win32 implementation (untested)

I don't have my Win32 build setup anymore, so I can't test this right now.  But here it is if there's anyone who's interested.
Comment 6 Matthias Clasen 2009-11-23 14:32:03 UTC
Does this actually ever set primary_monitor to something other than 0 ?

I thought Xrandr was supposed to sort the primary monitory first ?
Comment 7 Cody Russell 2009-11-23 21:30:24 UTC
When I plug my Thinkpad into my LCD, I can set the primary monitor to either VGA1 or LVDS1 and this function will return either 0 or 1.  Try running testxinerama and modify your primary display as I commented above and you'll see the number change.
Comment 8 Cody Russell 2009-11-23 21:44:40 UTC
The only thing is that the Gnome display setup util doesn't have any UI for setting the primary monitor, so to be honest I'm not sure if this will ever return other than 0 in practice as things stand right now.

My motivation for looking into this is that on Ubuntu we have this stupid splash screen and the login sequence looks kind of weird:
 1/ splash screen on monitor #0
 2/ gdm greeter on monitor #1
 3/ splash screen on monitor #0
 4/ desktop starts, panel on monitor #0

I haven't looked into why gdm is starting on #1 yet, but it seemed like it would be nice to have some real API that can be used by any apps that might need it (such as gnome-shell, gnome-panel, gdm greeter, xsplash, etc) and then separately add support to the display util for setting the value.
Comment 9 Cody Russell 2009-11-23 21:49:59 UTC
It looks like on NVIDIA drivers it will report multiple monitors through the API, but the command-line xrandr utility only shows one so there is not any obvious way to set the primary monitor to something other than 0 as far as I can tell.
Comment 11 Matthias Clasen 2009-11-25 23:31:04 UTC
+  primary_output = XRRGetOutputPrimary (screen_x11->xdisplay,
+                                        screen_x11->xroot_window);

XRRGetOutputPrimary is a 1.3 addition, but this is probably ok since we only define HAVE_XRANDR if we have at least 1.3 client-side.

Other than that, looks good to commit. Might be good to add a sentence to the docs explaining what 'primary' means... if you have any idea what to say there.
Comment 12 Matthias Clasen 2009-11-25 23:31:52 UTC
I'll let tml review the win32 implementation.
Comment 13 Matthias Clasen 2009-11-25 23:33:38 UTC
Oh, one thing I just notice now that I'm rereading what the randr spec says about 'primary': XRRGetOutputPrimary may return None. Does your implementation handle that, and is it documented ?
Comment 14 Cody Russell 2009-11-27 20:56:18 UTC
Right now screen_x11->primary_monitor gets initialized to 0, and if it discovers that there is a different monitor which is primary then it sets it.  So if XRRGetOutputPrimary is None, that will never happen and the return value of this will be 0.

Does that seem reasonable, or do you think we should actually return a different value if there is no default monitor?  I kind of hate to do something like return -1 for no default though.  If we want to provide the ability to differentiate between "primary is 0" and "no primary" then maybe we could do:

gboolean gdk_screen_get_primary_monitor (GdkScreen *, int *);

And have the return value specify whether the int* has any particular meaning.

The problem with all this, in my opinion, is that if you know there are multiple monitors and you are an app like a panel that cares what the primary monitor is, then xrandr is basically shifting the decision of "what is the primary monitor?" to you by not having a value.  I kind of feel like GTK+ could make an opinionated choice here and say that if there is no default monitor set by xrandr, then monitor 0 is considered the primary one.
Comment 15 Christian Dywan 2009-11-27 21:30:49 UTC
I think -1 would be consitent with for example gtk_menu_get_monitor which does return -1. How useful is returning 0 if you have no idea what monitor that is? On my Intel card, 0 has a different meaning depending on whether I boot with or without an external monitor.
Comment 16 Cody Russell 2009-12-03 01:37:35 UTC
Created attachment 148976 [details] [review]
Sets primary_monitor = -1 unless it's set otherwise by xrandr. 

I still don't really like the idea of returning -1 if the primary monitor isn't set.  As I said before, I think this is basically just shifting the responsibility back to the application developer to determine what monitor to use.  Anyone using this API would now have to check gdk_screen_get_primary_display(), and if the return value is -1 then use 0 instead.

Another option could be gboolean gdk_screen_has_primary_monitor()?  I don't envision that being very useful, but at least it would resolve this issue.
Comment 17 Matthias Clasen 2009-12-08 14:17:50 UTC
So, looking about this again, I think returning 0 in the 'no primary monitor' case is fine. But it should be documented.

Returns: the index of the primary monitor, or 0, if no primary monitor has been set.

Or so. Also, adding one sentence about the purpose of 'primary monitor' would be good. Something like:

The primary monitor is the one where the 'main desktop' user interface is
displayed. The primary monitor is the best monitor to display a new window on,
if you have no other clues where the window should be shown.
Comment 18 Matthias Clasen 2009-12-08 14:20:41 UTC
Can you commit the version that always returns a valid monitor, with the doc improvements ? 

Still waiting for tml to review the win32 part.
Comment 19 Cody Russell 2009-12-08 17:29:26 UTC
Thanks, pushed to master.