| 23 | |
| 24 | |
| 25 | def inputhook(context): |
| 26 | global _appref, _eventloop, _timer |
| 27 | app = QtCore.QCoreApplication.instance() |
| 28 | if not app: |
| 29 | if sys.platform == 'linux': |
| 30 | if not os.environ.get('DISPLAY') \ |
| 31 | and not os.environ.get('WAYLAND_DISPLAY'): |
| 32 | import warnings |
| 33 | global _already_warned |
| 34 | if not _already_warned: |
| 35 | _already_warned = True |
| 36 | warnings.warn( |
| 37 | 'The DISPLAY or WAYLAND_DISPLAY environment variable is ' |
| 38 | 'not set or empty and Qt5 requires this environment ' |
| 39 | 'variable. Deactivate Qt5 code.' |
| 40 | ) |
| 41 | return |
| 42 | try: |
| 43 | QtCore.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
| 44 | except AttributeError: # Only for Qt>=5.6, <6. |
| 45 | pass |
| 46 | try: |
| 47 | QtCore.QApplication.setHighDpiScaleFactorRoundingPolicy( |
| 48 | QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough |
| 49 | ) |
| 50 | except AttributeError: # Only for Qt>=5.14. |
| 51 | pass |
| 52 | _appref = app = QtGui.QApplication([" "]) |
| 53 | |
| 54 | # "reclaim" IPython sys.excepthook after event loop starts |
| 55 | # without this, it defaults back to BaseIPythonApplication.excepthook |
| 56 | # and exceptions in the Qt event loop are rendered without traceback |
| 57 | # formatting and look like "bug in IPython". |
| 58 | QtCore.QTimer.singleShot(0, _reclaim_excepthook) |
| 59 | |
| 60 | if _eventloop is None: |
| 61 | _eventloop = QtCore.QEventLoop(app) |
| 62 | |
| 63 | if sys.platform == 'win32': |
| 64 | # The QSocketNotifier method doesn't appear to work on Windows. |
| 65 | # Use polling instead. |
| 66 | if _timer is None: |
| 67 | _timer = QtCore.QTimer() |
| 68 | _timer.timeout.connect(_eventloop.quit) |
| 69 | while not context.input_is_ready(): |
| 70 | # NOTE: run the event loop, and after 10 ms, call `quit` to exit it. |
| 71 | _timer.start(10) # 10 ms |
| 72 | _exec(_eventloop) |
| 73 | _timer.stop() |
| 74 | else: |
| 75 | # On POSIX platforms, we can use a file descriptor to quit the event |
| 76 | # loop when there is input ready to read. |
| 77 | notifier = QtCore.QSocketNotifier( |
| 78 | context.fileno(), enum_helper("QtCore.QSocketNotifier.Type").Read |
| 79 | ) |
| 80 | try: |
| 81 | # connect the callback we care about before we turn it on |
| 82 | # lambda is necessary as PyQT inspect the function signature to know |