1 | #!/usr/bin/env python |
---|
2 | # This file is part of Xpra. |
---|
3 | # Copyright (C) 2017-2018 Antoine Martin <antoine@xpra.org> |
---|
4 | # Xpra is released under the terms of the GNU GPL v2, or, at your option, any |
---|
5 | # later version. See the file COPYING for details. |
---|
6 | |
---|
7 | from ctypes import wintypes, WINFUNCTYPE |
---|
8 | import signal |
---|
9 | import ctypes |
---|
10 | import mmap |
---|
11 | import sys |
---|
12 | |
---|
13 | # Function prototype for the handler function. Returns BOOL, takes a DWORD. |
---|
14 | HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD) |
---|
15 | |
---|
16 | def _ctrl_handler(sig): |
---|
17 | """Handle a sig event and return 0 to terminate the process""" |
---|
18 | if sig == signal.CTRL_C_EVENT: |
---|
19 | print("CTRL_C_EVENT") |
---|
20 | elif sig == signal.CTRL_BREAK_EVENT: |
---|
21 | print("CTRL_BREAK_EVENT") |
---|
22 | else: |
---|
23 | print("UNKNOWN EVENT") |
---|
24 | return 0 |
---|
25 | |
---|
26 | ctrl_handler = HandlerRoutine(_ctrl_handler) |
---|
27 | |
---|
28 | |
---|
29 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler |
---|
30 | SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL) |
---|
31 | SetConsoleCtrlHandler.restype = wintypes.BOOL |
---|
32 | |
---|
33 | if __name__ == "__main__": |
---|
34 | # Add our console control handling function with value 1 |
---|
35 | if not SetConsoleCtrlHandler(ctrl_handler, 1): |
---|
36 | print("Unable to add SetConsoleCtrlHandler") |
---|
37 | exit(-1) |
---|
38 | |
---|
39 | try: |
---|
40 | import glib |
---|
41 | except ImportError: |
---|
42 | import gi |
---|
43 | from gi.repository import GLib #@UnresolvedImport |
---|
44 | if gi.version_info<(3, 11): |
---|
45 | GLib.threads_init() |
---|
46 | glib = GLib |
---|
47 | |
---|
48 | loop = glib.MainLoop() |
---|
49 | try: |
---|
50 | loop.run() |
---|
51 | except: |
---|
52 | print("exception!") |
---|
53 | raise |
---|