|
| 1 | +#!/usr/bin/python3 |
| 2 | +# |
| 3 | +# examples/xfixes-selection-notify.py -- demonstrate the XFIXES extension |
| 4 | +# SelectionNotify event. |
| 5 | +# |
| 6 | +# Copyright (C) 2019 |
| 7 | +# Tony Crisci <tony@dubstepdish.com> |
| 8 | +# |
| 9 | +# This library is free software; you can redistribute it and/or |
| 10 | +# modify it under the terms of the GNU Lesser General Public License |
| 11 | +# as published by the Free Software Foundation; either version 2.1 |
| 12 | +# of the License, or (at your option) any later version. |
| 13 | +# |
| 14 | +# This library is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +# See the GNU Lesser General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU Lesser General Public |
| 20 | +# License along with this library; if not, write to the |
| 21 | +# Free Software Foundation, Inc., |
| 22 | +# 59 Temple Place, |
| 23 | +# Suite 330, |
| 24 | +# Boston, MA 02111-1307 USA |
| 25 | + |
| 26 | +# Python 2/3 compatibility. |
| 27 | +from __future__ import print_function |
| 28 | + |
| 29 | +import sys |
| 30 | +import os |
| 31 | +import time |
| 32 | + |
| 33 | +# Change path so we find Xlib |
| 34 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 35 | + |
| 36 | +from Xlib.display import Display |
| 37 | +from Xlib.ext import xfixes |
| 38 | + |
| 39 | +def main(argv): |
| 40 | + if len(sys.argv) != 2: |
| 41 | + sys.exit('usage: {0} SELECTION\n\n' |
| 42 | + 'SELECTION is typically PRIMARY, SECONDARY or CLIPBOARD.\n' |
| 43 | + .format(sys.argv[0])) |
| 44 | + |
| 45 | + display = Display() |
| 46 | + |
| 47 | + sel_name = sys.argv[1] |
| 48 | + sel_atom = display.get_atom(sel_name) |
| 49 | + |
| 50 | + if not display.has_extension('XFIXES'): |
| 51 | + if display.query_extension('XFIXES') is None: |
| 52 | + print('XFIXES extension not supported', file=sys.stderr) |
| 53 | + return 1 |
| 54 | + |
| 55 | + xfixes_version = display.xfixes_query_version() |
| 56 | + print('Found XFIXES version %s.%s' % ( |
| 57 | + xfixes_version.major_version, |
| 58 | + xfixes_version.minor_version, |
| 59 | + ), file=sys.stderr) |
| 60 | + |
| 61 | + screen = display.screen() |
| 62 | + |
| 63 | + mask = xfixes.XFixesSetSelectionOwnerNotifyMask | \ |
| 64 | + xfixes.XFixesSelectionWindowDestroyNotifyMask | \ |
| 65 | + xfixes.XFixesSelectionClientCloseNotifyMask |
| 66 | + |
| 67 | + display.xfixes_select_selection_input(screen.root, sel_atom, mask) |
| 68 | + |
| 69 | + while True: |
| 70 | + e = display.next_event() |
| 71 | + print(e) |
| 72 | + |
| 73 | + if (e.type, e.sub_code) == display.extension_event.SetSelectionOwnerNotify: |
| 74 | + print('SetSelectionOwner: owner=0x{0:08x}'.format(e.owner.id)) |
| 75 | + elif (e.type, e.sub_code) == display.extension_event.SelectionWindowDestroyNotify: |
| 76 | + print('SelectionWindowDestroy: owner=0x{0:08x}'.format(e.owner.id)) |
| 77 | + elif (e.type, e.sub_code) == display.extension_event.SelectionClientCloseNotify: |
| 78 | + print('SelectionClientClose: owner=0x{0:08x}'.format(e.owner.id)) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + sys.exit(main(sys.argv)) |
0 commit comments