From 47ca5dd8b30661f36156b5c172005f7429258200 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Sat, 15 Jul 2023 19:32:47 -0700 Subject: [PATCH] Initial commit --- .gitignore | 161 +++++++++++++++++++++++++++++++++++++++++++ README.md | 15 ++++ install.sh | 26 +++++++ requirements.txt | 2 + src/away_on_lock.py | 78 +++++++++++++++++++++ src/notify_darwin.py | 31 +++++++++ 6 files changed, 313 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 install.sh create mode 100644 requirements.txt create mode 100644 src/away_on_lock.py create mode 100644 src/notify_darwin.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30b2c92 --- /dev/null +++ b/.gitignore @@ -0,0 +1,161 @@ +.vscode/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..39e8fe5 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +Weechat script to set `/away` on screen lock. + +## Installing + +If your weechat directory is `~/.local/share/weechat` and you want the script +to autoload, you can just run `./install.sh`. Otherwise, copy `away_on_lock.py` +and `notify_$PLATFORM.py` to your weechat `python` directory, and symlink +`away_on_lock.py` to `python/autoload`. + + + +## TODO + +* ~~Find a better way to do IPC with Weechat~~ +* Linux support (KDE/Gnome at least) diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..7f68fc5 --- /dev/null +++ b/install.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +set -e + +config_dir="${XDG_DATA_HOME:-$HOME/.local/share}" +if [ -z "$config_dir" ] ; then + echo "Couldn't guess your XDG_DATA_HOME! Please manually install the script." 2>&1 + exit 1 +fi + +weechat_dir="${config_dir}/weechat" +if [ ! -d "${weechat_dir}" ] ; then + echo "Couldn't find your weechat home at ${weechat_dir}. Please manually install the script." 2>&1 + exit 1 +fi + +mkdir -p "${weechat_dir}/python/autoload" + +echo "Copying script files..." +cp -v "$PWD/src/"*.py "${weechat_dir}/python" +chmod +x "${weechat_dir}/python/away_on_lock.py" + +echo "Setting script to autoload..." +ln -s "../away_on_lock.py" "${weechat_dir}/python/autoload/away_on_lock.py" + +echo "Done!" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2ccd596 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pyobjc-core>='9.0'; sys_platform == 'darwin' +pyobjc-framework-Cocoa>='9.0'; sys_platform == 'darwin' \ No newline at end of file diff --git a/src/away_on_lock.py b/src/away_on_lock.py new file mode 100644 index 0000000..3c4d6f6 --- /dev/null +++ b/src/away_on_lock.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +try: + import weechat + import_ok = True +except ImportError: + import_ok = False +import sys + +try: + import importlib + notify = importlib.import_module("notify_%s" % sys.platform) +except ImportError: + weechat.prnt('', 'No OS-specific module notify_%s found.' % sys.platform) + import_ok = False + + +SCRIPT_AUTHOR = 'snow flurry ' +SCRIPT_NAME = 'away_on_lock' +SCRIPT_VERSION = '0.5' +SCRIPT_LICENSE = 'MIT' +SCRIPT_DESC = 'Sends /away on lock' + +DEFAULT_OPTIONS = { + 'verbose': 'on', + 'message': 'Screen locked', +} + +def locknotify_cb(data, command, return_code, out, err): + if return_code == weechat.WEECHAT_HOOK_PROCESS_ERROR: + weechat.prnt("", "locknotify returned error: %s" % err) + weechat.prnt("", "out: %s" % out) + return weechat.WEECHAT_RC_OK + + verbose = weechat.config_boolean(weechat.config_get_plugin('verbose')) + + if out != "": + out = out.strip() + if out == "lock": + weechat.prnt("", "screen locked, setting away...") if verbose else () + weechat.command("", "/away -all %s" + % weechat.config_get_plugin('message')) + elif out == "unlock": + weechat.prnt("", "screen unlocked, setting back...") if verbose else () + weechat.command("", "/away -all") + else: + weechat.prnt("", "unknown output: %s" % out) + else: + weechat.prnt("", "stderr: %s" % err) + + if return_code != weechat.WEECHAT_HOOK_PROCESS_RUNNING: + spawn_helper() + + return weechat.WEECHAT_RC_OK + +def spawn_helper(): + verbose = weechat.config_boolean(weechat.config_get_plugin('verbose')) + print("spawning '%s'" % proc) if verbose else () + + hook = weechat.hook_process(proc, 0, "locknotify_cb", "") + +if import_ok: + weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", "") + + for key, val in DEFAULT_OPTIONS.items(): + if not weechat.config_is_set_plugin(key): + weechat.config_set_plugin(key, val) + + proc = "%s --selfhost" % __file__ + spawn_helper() + + +elif __name__ == "__main__": + if len(sys.argv) < 2 or sys.argv[1] != "--selfhost": + print('This script must be run from WeeChat.') + sys.exit(1) + + notify.notify_lock(lambda status: print(status)) \ No newline at end of file diff --git a/src/notify_darwin.py b/src/notify_darwin.py new file mode 100644 index 0000000..35a40a2 --- /dev/null +++ b/src/notify_darwin.py @@ -0,0 +1,31 @@ +import Foundation +from PyObjCTools import AppHelper + +class LockNotifier: + def __init__(self, hfunc): + self.hfunc = hfunc + + def onLock_(self, _notification): + self.hfunc("lock") + AppHelper.stopEventLoop() + + def onUnlock_(self, _notification): + self.hfunc("unlock") + AppHelper.stopEventLoop() + +def notify_lock(handler): + notifier = LockNotifier(handler) + dnc = Foundation.NSDistributedNotificationCenter.defaultCenter() + dnc.addObserver_selector_name_object_(notifier, "onLock:", "com.apple.screenIsLocked", None) + dnc.addObserver_selector_name_object_(notifier, "onUnlock:", "com.apple.screenIsUnlocked", None) + AppHelper.runConsoleEventLoop() + +if __name__ == "__main__": + def handler(state): + if state == "lock": + print("lock") + elif state == "unlock": + print("unlock") + AppHelper.stopEventLoop() + + notify_lock(handler) \ No newline at end of file