diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..68fae5f --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,5 @@ +option('enable-xscreensaver-plugin', + type: 'boolean', + value: true, + description: 'Build plugin for XScreenSaver support' +) diff --git a/plugins/XScreenSaverPlugin/Main.vala b/plugins/XScreenSaverPlugin/Main.vala new file mode 100644 index 0000000..0a95617 --- /dev/null +++ b/plugins/XScreenSaverPlugin/Main.vala @@ -0,0 +1,115 @@ +class Caffeine.Plugins.XScreenSaver : Peas.ExtensionBase, Peas.Activatable { + private uint source_id = 0U; + private int timeout_duration = 0; + private int timeout_offset = -5; + private File xscreensaver_preferences_file; + + public Object object { owned get; construct; } + + public XScreenSaver () { + Object (); + } + + construct { + xscreensaver_preferences_file = File.new_build_filename ( + Environment.get_home_dir (), ".xscreensaver"); + + if (!xscreensaver_preferences_file.query_exists ()) + error ("~/.xscreensaver not found."); + + var xscreensaver_preferences_file_monitor = xscreensaver_preferences_file.monitor_file (FileMonitorFlags.NONE, null); + + xscreensaver_preferences_file_monitor.changed.connect ((src, dest, event) => { + if (event == FileMonitorEvent.CHANGED) + on_xscreensaver_preferences_file_updated (); + }); + + on_xscreensaver_preferences_file_updated (); + + this.notify["timeout_duration"].connect (() => { + if (source_id == 0U) + deactivate_screensaver (); + }); + } + + public void activate () { + deactivate_screensaver (); + } + + public void deactivate () { + if (source_id != 0U) { + Source.remove (source_id); + source_id = 0U; + } + } + + public void update_state () {} + + private void deactivate_screensaver (int? interval = null) { + if (interval == null) + interval = timeout_duration; + + if (source_id != 0U) + Source.remove (source_id); + + source_id = Timeout.add_seconds (interval + timeout_offset, () => { + var success = false; + string[] spawn_args = { "xscreensaver-command", "-deactivate" }; + string[] spawn_env = Environ.get (); + + try { + Process.spawn_sync ( + Environment.get_home_dir (), + spawn_args, + spawn_env, + SpawnFlags.SEARCH_PATH, + null, + null); + + success = true; + } catch (Error err) { + error (@"unable to spawn process"); + } + + return success; + }); + } + + private void on_xscreensaver_preferences_file_updated () { + try { + var dis = new DataInputStream ( + xscreensaver_preferences_file.read ()); + + string line; + while ((line = dis.read_line (null)) != null) { + string[] preference = line.split (":", 2); + unowned string key = preference[0]; + + if (key.strip () == "timeout") { + int timeout_duration = -1; + string[] time = preference[1].split (":"); + + for (int i = 0 ; i < time.length ; i++) + timeout_duration += int.parse (time[i].strip ()) * (i == (time.length - 1) ? 1 : 60); + + if (timeout_duration != -1) + this.timeout_duration = timeout_duration; + + break; + } + } + } catch (Error err) { + error ("unable to parse preferences file"); + } + } + +} + +[ModuleInit] +public void peas_register_types (GLib.TypeModule module) { + Peas.ObjectModule object_module = module as Peas.ObjectModule; + + object_module.register_extension_type ( + typeof (Peas.Activatable), + typeof (Caffeine.Plugins.XScreenSaver)); +} diff --git a/plugins/XScreenSaverPlugin/meson.build b/plugins/XScreenSaverPlugin/meson.build new file mode 100644 index 0000000..fc2be51 --- /dev/null +++ b/plugins/XScreenSaverPlugin/meson.build @@ -0,0 +1,19 @@ +shared_library('xscreensaver', + [ + 'Main.vala' + ], + dependencies: [ + libpeas_dep + ], + install: true, + install_dir: join_paths(pkgpluginslibdir, 'xscreensaver'), +) + +i18n.merge_file( + type: 'desktop', + input: 'xscreensaver.plugin.in', + output: 'xscreensaver.plugin', + po_dir: po_dir, + install: true, + install_dir: join_paths(pkgpluginslibdir, 'xscreensaver') +) diff --git a/plugins/XScreenSaverPlugin/xscreensaver.plugin.in b/plugins/XScreenSaverPlugin/xscreensaver.plugin.in new file mode 100644 index 0000000..811c091 --- /dev/null +++ b/plugins/XScreenSaverPlugin/xscreensaver.plugin.in @@ -0,0 +1,6 @@ +[Plugin] +Module=xscreensaver.so +Name=XScreenSaver +Description=XScreenSaver support for Caffeine. +Authors=Payson Wallach +Copyright=Copyright © 2020 Payson Wallach diff --git a/plugins/meson.build b/plugins/meson.build new file mode 100644 index 0000000..168ff3f --- /dev/null +++ b/plugins/meson.build @@ -0,0 +1,3 @@ +if get_option('enable-xscreensaver-plugin') + subdir('XScreenSaverPlugin') +endif