Purpose of this page
The purpose of this page is to
- Describe the bits and pieces of the X11 selections and OS X pasteboard as they currently function SEPARATELY on OS X Leopard
- Describe how they CURRENTLY INTERACT on OS X Leopard
- Propose better FUTURE INTERACTION
Current implementation
For entirely boring technical reasons, the proxying of clipboard contents is mostly (entirely?) handled inside the closed-source quartz-wm. However, the code that actually performs that functionality is not sensitive, so it's been posted here: XDarwin_Clipboard_Code. It would be nice to eventually integrate this into Xquartz itself. Until then, the code is there as a reference to help with understanding / interpreting / fixing the current behavior.
Separate, and not equal, clipboards
Note: It's NOT YET been generally agreed to that all these points below are correct, or that all the points that ought to be there are there - Nathan
- On OS X "pasteboard" is an intermediate place. When you copy something, it gets copied to this intermediate place. When you paste something, it's copied out of that intermediate place. As stated by Ben Byer, "...when you copy something [in Cocoa], it copies all of that data to the NSPasteboard, and when you paste, it just pulls the data from the NSPasteboard"
The X11 clipboard paradigm is very different see http://www.jwz.org/doc/x-cut-and-paste.html
- The X11 does not have an intermediate place that stuff gets copied to like the one in OS X
X11 has multiple selections. At any one time, any selection has at most one owner (an X11 client). Only the owner of the selection knows its contents.
X11's primary selection is a pointer to some stuff, usually set by highlighting some text. The owner will be the client whose window the text is highlighted in.
When the X11 primary selection is asked to be "pasted," usually by pressing the middle mouse button, the owner of the primary selection is requested to provide a copy of the selection. The resulting string is then copied directly to the insertion point (no intermediate clipboard place!)
X11's clipboard selection works similarly. It usually set by selecting "copy" in some menu. The client will typically take a copy of its current selection and store it away, in order to provide it when requested in the future.
When the X11 clipboard selection is asked to be "pasted," usually by selecting "paste" in some menu, the clipboard selection owner is requested to provide the contents which is then copied directly to the insertion point (no intermediate clipboard place!)
Current interaction between clipboards
Note: It's NOT YET been generally agreed to that all these points below are correct, or that all the points that ought to be there are there - Nathan
- Copy in OS X, switch to X11...
when X11 gains focus: The X11 primary and clipboard selections are acquired by the OS X pasteboard, unless the pasteboard was last changed by quartz-wm itself. More precisely, quartz-wm, which is also an X11 client, claims ownership of both selections. Henceforth, whenever quartz-wm is asked to provide the contents of either selection, it returns the current contents of the pasteboard, not necessarely as it was at the time of the focus shift. (Note that the pasteboard may change even if X11 retains focus, though this rarely happens in practical use.)
paste primary selection: Pastes what the X11 primary selection points to (the OS X pasteboard if no other X11 client has claimed ownership since X11 gained focus)
paste clipboard selection: Pastes what the X11 clipboard selection points to (the OS X pasteboard if no other X11 client has claimed ownership since X11 gained focus)
- Switch from X11 to another OS X application ...
- If the user has never selected Copy from the X11 menu, and if an X11 client other than quartz-wm owns the clipboard selection, the clipboard selection is copied to the pasteboard. If the user has ever selected Copy from the X11 menu, this automatic copying of the clipboard will never again happen while the current X11 session is running.
- User selects Copy in X11 ...
- quartz-wm requests the current primary selection, if owned by a client other than itself, and copies it to the pasteboard. At the same time, it claims ownership of the clipboard selection.
Harald pretty much doubts the statements below. In fact, he is unable to reproduce the claims.
Create primary selection in X11, switch to OS X...
when OS X gains focus: Its clipboard is unchanged from before a "primary selection" was made.
paste: The clipboard is pasted.
Create clipboard selection in X11, switch to OS X...
when OS X gains focus: The clipboard contains a "promise to copy" from X11
paste: Whatever is selected in X11 at the moment of pasting is pasted. This may be different from what was selected at the moment of COPYing, and this is the most contentious behavior about which people complain.
- Thus, if nothing is selected in X11, nothing is pasted
- If something new is selected in X11, the new thing is pasted
Switch back to X11: Now, the evaluated promise is copied into X11's clipboard, but the primary selection remains selected.
Proposed future interaction between clipboards
Adam's proposal
- No change to Cocoa behavior
- When command-C or the "copy" command from menu is issued within X11:
- the contents of the X11 clipboard are immediately copied in whole to the Cocoa pasteboard; no promise involved.
- If the X11 primary selection and clipboard differ, the primary selection is copied to the X11 clipboard and then to the Cocoa pasteboard
Nathan's first shot at a proposal
Note that this assumes that X11 can detect gaining/losing focus and can set either selection pointer to point to the OS X pasteboard and that both the OS X pasteboard and X11 selections can keep track of events that have happened since the last gain/loss of X11's focus- Nathan
- Copy in OS X, switch to X11...
when X11 gains focus: Both the primary and clipboard selections are set to point to OS X's pasteboard iff the OS X pasteboard obtained new content since X11 last lost focus.
paste primary selection: primary selection is pasted
paste clipboard selection: clipboard selection is pasted
Create primary selection in X11, switch to OS X...
when OS X gains focus: primary selection is copied into OS X pasteboard iff the primary selection has been set since the last time X11 gained focus AND the primary selection is the latest selection to be set.
paste: OS X pasteboard content is pasted
Create clipboard selection in X11, switch to OS X...
when OS X gains focus: clipboard selection is copied into OS X pasteboard iff the clipboard selection has been set since the last time X11 gained focus AND the clipboard selection is the latest selection to be set.
paste: OS X pasteboard content is pasted
Nicholas's proposal
Create primary selection: Examine current pasteboard contents to determine if it was created by a X11 primary selection. If current pasteboard contents were not created by an X primary selection, save the current pasteboard contents in memory. Write X11 primary selection to OS X pasteboard with custom flavor to indicate presence of primary selection.
Remove primary selection (deselect): Restore saved pasteboard contents.
Choose Copy from Edit menu: Remove "this is a primary selection" flavor if present. (This ensures the primary selection persists in the Mac pasteboard; it may be necessary to deselect to get it to paste, but it won't be lost.)
Create clipboard selection: Write X11 clipboard selection to OS X pasteboard with custom flavor to indicate presence of clipboard selection.
Paste primary selection: If no X11 primary selection, use OS X pasteboard contents.
Paste clipboard selection: If OS X pasteboard does not contain a custom flavor written by X11, use OS X pasteboard, otherwise use X11 clipboard selection.
Reasons why the other proposals don't work for me:
- It should be possible to copy the primary selection to a Mac app without having to select from a menu; "enable key equivalents under X11" is not compatible with the apps I use (primarily Emacs).
- Only bridging the clipboard on a focus switch ignores several use cases.
- People (like me!) use utilities like Synergy and teleport so they can control multiple Macs with a single keyboard and mouse. Your mouse can be on another machine without X11 ever losing focus and I often want to paste there.
- Apple's Screen Sharing and Remote Desktop have similar clipboard-bridging features.
- Various utilities (which I don't use) support a "clipboard history". If X11 never updates the system pasteboard, this history will not be updated either (however with the primary selection being updated, it may create a lot of updates).


