Skip to content

Commit

Permalink
Use os.pipe() socket pair on Widows
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Dec 28, 2022
1 parent d698fa8 commit c4ca22c
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions efck/gui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import atexit
import logging
import os
import signal
import socket
from pathlib import Path

from . import IS_MACOS, IS_WIDOWS
Expand Down Expand Up @@ -363,20 +363,18 @@ def reset_window_position(self):
self.setGeometry(QRect(*top_left + geometry))

def install_sigusr1_handler(self):
self._socket_pair = (rsock, wsock) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
self._notifier = notifier = QSocketNotifier(rsock.fileno(), QSocketNotifier.Type.Read, self)
r_fd, w_fd = os.pipe() if IS_WIDOWS or IS_MACOS else os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC)
atexit.register(os.close, r_fd)
atexit.register(os.close, w_fd)
self._notifier = notifier = QSocketNotifier(r_fd, QSocketNotifier.Type.Read, self)
# https://stackoverflow.com/questions/4938723/what-is-the-correct-way-to/37229299#37229299
wsock.setblocking(False)
signal.set_wakeup_fd(wsock.fileno())
signal.set_wakeup_fd(w_fd)
signal.signal(OUR_SIGUSR1, lambda sig, frame: None)
# Avoid ResourceWarning on exit
atexit.register(rsock.close)
atexit.register(wsock.close)

def sigusr1_received():
nonlocal notifier, self, rsock
nonlocal notifier, self, r_fd
notifier.setEnabled(False)
signum = ord(rsock.recv(1))
signum = ord(os.read(r_fd, 1))
if signum == OUR_SIGUSR1:
logger.info('Handled SIGUSR1. Showing up!')
self.reset_window_position()
Expand Down

0 comments on commit c4ca22c

Please sign in to comment.