Commits

Yuya Nishihara  committed 9e15713

python: try SetConsoleCtrlHandler to capture close event (useless)

  • Participants
  • Parent commits 823cae1

Comments (0)

Files changed (1)

File python/win32consolectrl.py

+import sys, time
+from ctypes import WINFUNCTYPE, windll
+from ctypes.wintypes import BOOL, DWORD, MSG
+from PyQt4.QtCore import QCoreApplication, QProcess, QTimer
+
+PHANDLER_ROUTINE = WINFUNCTYPE(BOOL, DWORD)
+GenerateConsoleCtrlEvent = windll.kernel32.GenerateConsoleCtrlEvent
+SetConsoleCtrlHandler = windll.kernel32.SetConsoleCtrlHandler
+
+CTRL_C_EVENT = 0
+CTRL_CLOSE_EVENT = 2
+
+def _ctrlhandler(ctrltype):
+    print 'ctrl handler', ctrltype
+    sys.stdout.flush()
+    if ctrltype == CTRL_CLOSE_EVENT:
+        GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)
+        return True
+    return False
+
+def slave():
+    print 'slave started'
+    sys.stdout.flush()
+    # keep reference to avoid gc
+    ctrlhandlerfuncp = PHANDLER_ROUTINE(_ctrlhandler)
+    SetConsoleCtrlHandler(ctrlhandlerfuncp, True)
+    sys.stdin.read()
+
+def master():
+    raise NotImplementedError  # XXX not work
+    app = QCoreApplication(sys.argv)
+    proc = QProcess()
+    proc.setProcessChannelMode(QProcess.ForwardedChannels)
+    proc.finished.connect(app.quit)
+    proc.start(sys.executable, [__file__, 'slave'])
+    QTimer.singleShot(2000, proc.terminate)
+    app.exec_()
+
+if __name__ == '__main__':
+    if sys.argv[1:] == ['slave']:
+        slave()
+    else:
+        master()