Search code examples
xlib

Listen for XSetSelectionOwner


Is possible to be notified that there is new selection owner? Does XSetSelectionOwner method cause any event broadcast? I wonder how x2x works. If there is no any way to be notified that there is new selection owner then x2x has to ask X servers in loop who is selection owner.

I suspect that: x2x uses XGetSelectionOwner to ask both displays about current owner. If owner was changed on Display 1 then x2x uses XSetSelectionOwner to become selection owner on Display 2. If user want paste data on Display 2, x2x on display 2 works as intermediate and sends all received request to original client in Display 1. Am I right?


Solution

  • Yes, when you get selection ownership previous owner receive SelectionClear event:

    SelectionClear

    owner: WINDOW

    selection: ATOM

    time: TIMESTAMP

    This event is reported to the current owner of a selection and is generated when a new owner is being defined by means of SetSelectionOwner. The timestamp is the last-change time recorded for the selection. The owner argument is the window that was specified by the current owner in its SetSelectionOwner request.

    Update: if you don't own selection, you can get notifications about selection change using XFixes extension:

    6. Selection Tracking
    
    Applications wishing to monitor the contents of current selections must
    poll for selection changes.  XFIXES improves this by providing an event
    delivered whenever the selection ownership changes.
    
    6.2 Requests
    
    SelectSelectionInput
    
            window:             Window
            selection:          Atom
            event-mask:         SETofSELECTIONEVENT
    
        Selects for events to be delivered to window when various causes of
        ownership of selection occur.  Subtype indicates the cause of the
        selection ownership change.  Owner is set to the current selection
        owner, or None.  Timestamp indicates the time the event was
        generated while selection-timestamp indicates the timestamp used to
        own the selection.