Skip to content

Commit

Permalink
Merge pull request #1306 from benoit-pierre/bsd_support
Browse files Browse the repository at this point in the history
add FreeBSD/OpenBSD support
  • Loading branch information
benoit-pierre authored May 14, 2021
2 parents c4ad3f8 + 53732a0 commit e707ba2
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 81 deletions.
1 change: 1 addition & 0 deletions news.d/feature/1306.core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add FreeBSD/OpenBSD support.
5 changes: 2 additions & 3 deletions plover/dist_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

import os
import sys
import subprocess

from plover.oslayer.config import CONFIG_DIR, PLUGINS_PLATFORM
from plover.oslayer.config import CONFIG_DIR, PLATFORM, PLUGINS_PLATFORM


def main():
Expand All @@ -13,7 +12,7 @@ def main():
args.remove('--no-user-plugins')
args.insert(1, '-s')
os.environ['PYTHONUSERBASE'] = os.path.join(CONFIG_DIR, 'plugins', PLUGINS_PLATFORM)
if sys.platform.startswith('win32'):
if PLATFORM == 'win':
# Workaround https://bugs.python.org/issue19066
subprocess.Popen(args, cwd=os.getcwd())
sys.exit(0)
Expand Down
6 changes: 2 additions & 4 deletions plover/gui_qt/engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

import sys

from PyQt5.QtCore import (
QThread,
QVariant,
pyqtSignal,
)

from plover.engine import StenoEngine
from plover.oslayer.config import PLATFORM


class Engine(StenoEngine, QThread):
Expand Down Expand Up @@ -50,7 +48,7 @@ def join(self):
return self.code

def run(self):
if sys.platform.startswith('darwin'):
if PLATFORM == 'mac':
import appnope
appnope.nope()
super().run()
Expand Down
8 changes: 4 additions & 4 deletions plover/gui_qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from plover import _, log
from plover.oslayer import wmctrl
from plover.oslayer.config import CONFIG_DIR
from plover.oslayer.config import CONFIG_DIR, PLATFORM
from plover.registry import registry
from plover.resource import resource_filename

Expand Down Expand Up @@ -243,11 +243,11 @@ def on_configure(self):
self._configure()

def on_open_config_folder(self):
if sys.platform.startswith('win'):
if PLATFORM == 'win':
os.startfile(CONFIG_DIR)
elif sys.platform.startswith('linux'):
elif PLATFORM == 'linux':
subprocess.call(['xdg-open', CONFIG_DIR])
elif sys.platform.startswith('darwin'):
elif PLATFORM == 'mac':
subprocess.call(['open', CONFIG_DIR])

def on_reconnect(self):
Expand Down
5 changes: 2 additions & 3 deletions plover/gui_qt/trayicon.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sys

from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMessageBox, QSystemTrayIcon

from plover import _, __name__ as __software_name__
from plover import log
from plover.oslayer.config import PLATFORM
from plover.machine.base import (
STATE_STOPPED,
STATE_INITIALIZING,
Expand Down Expand Up @@ -76,7 +75,7 @@ def enable(self):
self._trayicon = QSystemTrayIcon()
# On OS X, the context menu is activated with either mouse buttons,
# and activation messages are still sent, so ignore those...
if not sys.platform.startswith('darwin'):
if PLATFORM != 'mac':
self._trayicon.activated.connect(self._on_activated)
if self._context_menu is not None:
self._trayicon.setContextMenu(self._context_menu)
Expand Down
11 changes: 5 additions & 6 deletions plover/i18n.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import os
import sys
import locale
import gettext

import pkg_resources

from plover.oslayer.config import CONFIG_DIR
from plover.oslayer.config import CONFIG_DIR, PLATFORM


def get_language():
env_vars = ['LANGUAGE']
if sys.platform.startswith('linux'):
if PLATFORM in {'linux', 'bsd'}:
env_vars.extend(('LC_ALL', 'LC_MESSAGES', 'LANG'))
for var in env_vars:
lang = os.environ.get(var)
if lang is not None:
return lang
if sys.platform.startswith('linux'):
if PLATFORM in {'linux', 'bsd'}:
lang, enc = locale.getdefaultlocale()
elif sys.platform.startswith('darwin'):
elif PLATFORM == 'mac':
from AppKit import NSLocale
lang_list = NSLocale.preferredLanguages()
lang = lang_list[0] if lang_list else None
elif sys.platform.startswith('win'):
elif PLATFORM == 'win':
from ctypes import windll
lang = locale.windows_locale[windll.kernel32.GetUserDefaultUILanguage()]
if lang is None:
Expand Down
8 changes: 4 additions & 4 deletions plover/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from logging.handlers import RotatingFileHandler
from logging import DEBUG, INFO, WARNING, ERROR

from plover.oslayer.config import CONFIG_DIR
from plover.oslayer.config import CONFIG_DIR, PLATFORM


LOG_FORMAT = '%(asctime)s [%(threadName)s] %(levelname)s: %(message)s'
Expand Down Expand Up @@ -84,13 +84,13 @@ def setup_platform_handler(self):
return
handler_class = None
try:
if sys.platform.startswith('linux'):
if PLATFORM == 'linux':
from plover.oslayer.log_dbus import DbusNotificationHandler
handler_class = DbusNotificationHandler
elif sys.platform.startswith('darwin'):
elif PLATFORM == 'mac':
from plover.oslayer.log_osx import OSXNotificationHandler
handler_class = OSXNotificationHandler
elif sys.platform.startswith('win32'):
elif PLATFORM == 'win':
from plover.oslayer.log_plyer import PlyerNotificationHandler
handler_class = PlyerNotificationHandler
except Exception:
Expand Down
6 changes: 2 additions & 4 deletions plover/macro/undo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

import sys

from plover.translation import Translation
from plover.oslayer.config import PLATFORM


if sys.platform.startswith('darwin'):
if PLATFORM == 'mac':
BACK_STRING = '{#Alt_L(BackSpace)}{^}'
else:
BACK_STRING = '{#Control_L(BackSpace)}{^}'
Expand Down
12 changes: 5 additions & 7 deletions plover/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@

import pkg_resources

if sys.platform.startswith('darwin'):
import appnope

from plover.config import Config
from plover.oslayer import processlock
from plover.oslayer.config import CONFIG_DIR, CONFIG_FILE
from plover.oslayer.config import CONFIG_DIR, CONFIG_FILE, PLATFORM
from plover.registry import registry
from plover import log
from plover import __name__ as __software_name__
Expand Down Expand Up @@ -61,7 +58,7 @@ def main():
log.info('Plover %s', __version__)
log.info('configuration directory: %s', CONFIG_DIR)

if sys.platform == 'darwin':
if PLATFORM == 'mac':
# Fixes PyQt issue on macOS Big Sur.
os.environ['QT_MAC_WANTS_LAYER'] = '1'

Expand Down Expand Up @@ -121,7 +118,8 @@ def main():

# Ensure only one instance of Plover is running at a time.
with processlock.PloverLock():
if sys.platform.startswith('darwin'):
if PLATFORM == 'mac':
import appnope
appnope.nope()
init_config_dir()
# This must be done after calling init_config_dir, so
Expand All @@ -144,7 +142,7 @@ def main():
args[0:1] = [sys.executable, '-m', __spec__.name]
# Execute atexit handlers.
atexit._run_exitfuncs()
if sys.platform.startswith('win32'):
if PLATFORM == 'win':
# Workaround https://bugs.python.org/issue19066
subprocess.Popen(args, cwd=os.getcwd())
code = 0
Expand Down
22 changes: 13 additions & 9 deletions plover/oslayer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@
import pkg_resources


if sys.platform.startswith('darwin'):
PLATFORM = 'mac'
elif sys.platform.startswith('linux'):
PLATFORM = 'linux'
elif sys.platform.startswith('win'):
PLATFORM = 'win'
elif sys.platform.startswith(('freebsd', 'openbsd')):
PLATFORM = 'bsd'
else:
PLATFORM = None

# If the program's working directory has a plover.cfg file then run in
# "portable mode", i.e. store all data in the same directory. This allows
# keeping all Plover files in a portable drive.
#
# Note: the special case when run from an app bundle on macOS.
if sys.platform.startswith('darwin') and '.app/' in os.path.realpath(__file__):
if PLATFORM == 'mac' and '.app/' in os.path.realpath(__file__):
PROGRAM_DIR = os.path.abspath(os.path.join(os.path.dirname(sys.executable),
*[os.path.pardir] * 3))
else:
Expand All @@ -38,14 +49,7 @@
CONFIG_FILE = os.path.join(CONFIG_DIR, CONFIG_BASENAME)

# Setup plugins directory.
if sys.platform.startswith('darwin'):
PLUGINS_PLATFORM = 'mac'
elif sys.platform.startswith('linux'):
PLUGINS_PLATFORM = 'linux'
elif sys.platform.startswith('win'):
PLUGINS_PLATFORM = 'win'
else:
PLUGINS_PLATFORM = None
PLUGINS_PLATFORM = PLATFORM

plover_dist = pkg_resources.working_set.by_key['plover']

Expand Down
10 changes: 5 additions & 5 deletions plover/oslayer/keyboardcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
"""

import sys
from plover.oslayer.config import PLATFORM

KEYBOARDCONTROL_NOT_FOUND_FOR_OS = \
"No keyboard control module was found for os %s" % sys.platform
"No keyboard control module was found for platform: %s" % PLATFORM

if sys.platform.startswith('linux'):
if PLATFORM in {'linux', 'bsd'}:
from plover.oslayer import xkeyboardcontrol as keyboardcontrol
elif sys.platform.startswith('win32'):
elif PLATFORM == 'win':
from plover.oslayer import winkeyboardcontrol as keyboardcontrol
elif sys.platform.startswith('darwin'):
elif PLATFORM == 'mac':
from plover.oslayer import osxkeyboardcontrol as keyboardcontrol
else:
raise Exception(KEYBOARDCONTROL_NOT_FOUND_FOR_OS)
Expand Down
5 changes: 2 additions & 3 deletions plover/oslayer/log_plyer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import os
import sys
import logging

from plyer import notification

from plover import log, __name__ as __software_name__
from plover.oslayer.config import ASSETS_DIR
from plover.oslayer.config import ASSETS_DIR, PLATFORM


APPNAME = __software_name__.capitalize()

if sys.platform.startswith('win32'):
if PLATFORM == 'win':
APPICON = os.path.join(ASSETS_DIR, 'plover.ico')
else:
APPICON = os.path.join(ASSETS_DIR, 'plover_32x32.png')
Expand Down
4 changes: 2 additions & 2 deletions plover/oslayer/processlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

"""Global lock to ensure plover only runs once."""

import sys
from plover.oslayer.config import PLATFORM


class LockNotAcquiredException(Exception):
pass


if sys.platform.startswith('win32'):
if PLATFORM == 'win':

from ctypes import windll

Expand Down
9 changes: 4 additions & 5 deletions plover/oslayer/wmctrl.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from plover.oslayer.config import PLATFORM

import sys


if sys.platform.startswith('win32'):
if PLATFORM == 'win':

from ctypes import windll, wintypes

Expand All @@ -17,7 +16,7 @@
SetForegroundWindow.restype = wintypes.BOOL


elif sys.platform.startswith('darwin'):
elif PLATFORM == 'mac':

from Cocoa import NSWorkspace, NSRunningApplication, NSApplicationActivateIgnoringOtherApps

Expand All @@ -28,7 +27,7 @@ def SetForegroundWindow(pid):
target_window = NSRunningApplication.runningApplicationWithProcessIdentifier_(pid)
target_window.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

elif sys.platform.startswith('linux'):
elif PLATFORM in {'linux', 'bsd'}:

from plover.oslayer.xwmctrl import WmCtrl

Expand Down
Loading

0 comments on commit e707ba2

Please sign in to comment.