diff --git a/contrib/meson.build b/contrib/meson.build new file mode 100644 index 0000000..13fae7f --- /dev/null +++ b/contrib/meson.build @@ -0,0 +1 @@ +subdir('openrc') diff --git a/contrib/openrc/meson.build b/contrib/openrc/meson.build new file mode 100644 index 0000000..2ee9330 --- /dev/null +++ b/contrib/openrc/meson.build @@ -0,0 +1,19 @@ +openrc_config_data = configuration_data() +openrc_config_data.set('SBINDIR', join_paths(get_option('prefix'), get_option('sbindir'))) +openrc_config_data.set('LIBEXECDIR', join_paths(get_option('prefix'), get_option('libexecdir'), meson.project_name())) + +openrc_config_data_file = configure_file( + input: 'tufmanager.in', + output: 'tufmanager', + configuration: openrc_config_data +) + +if openrc + install_data( + openrc_config_data_file, + install_dir: join_paths(get_option('sysconfdir'), 'init.d') + ) + + permissions = find_program('chmod') + meson.add_install_script('permission.sh', join_paths(get_option('sysconfdir'), 'init.d')) +endif diff --git a/contrib/openrc/permission.sh b/contrib/openrc/permission.sh new file mode 100755 index 0000000..9df1e04 --- /dev/null +++ b/contrib/openrc/permission.sh @@ -0,0 +1,4 @@ +#!/bin/sh -eu + +echo "Make OpenRC script executable..." +chmod 755 "${DESTDIR}${1}/tufmanager" diff --git a/contrib/openrc/tufmanager b/contrib/openrc/tufmanager.in similarity index 96% rename from contrib/openrc/tufmanager rename to contrib/openrc/tufmanager.in index d570daa..dc8d3f8 100644 --- a/contrib/openrc/tufmanager +++ b/contrib/openrc/tufmanager.in @@ -11,11 +11,11 @@ # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -command=@PKG_PREFIX@/libexec/tuf-manager/tuf-server +command=@LIBEXECDIR@/tuf-server pidfile=/var/run/tufmanager/tufmanager.pid name="TUF Manager daemon" depend() { - need dbus + need dbus } diff --git a/data/dbus/meson.build b/data/dbus/meson.build index 611365b..3f017cc 100644 --- a/data/dbus/meson.build +++ b/data/dbus/meson.build @@ -1,10 +1,10 @@ dbus_config_data = configuration_data() -dbus_config_data.set('LIBEXEC_DIR', join_paths(get_option('prefix'), get_option('libexecdir'), meson.project_name())) +dbus_config_data.set('LIBEXECDIR', join_paths(get_option('prefix'), get_option('libexecdir'), meson.project_name())) dbus_config_data_file = configure_file( input: 'org.tuf.manager.server.service.in', - output: 'org.tuf.manager.server.service', - configuration: dbus_config_data + output: 'org.tuf.manager.server.service', + configuration: dbus_config_data ) install_data( diff --git a/data/dbus/org.tuf.manager.server.conf b/data/dbus/org.tuf.manager.server.conf index 1b94af7..e009d14 100644 --- a/data/dbus/org.tuf.manager.server.conf +++ b/data/dbus/org.tuf.manager.server.conf @@ -2,22 +2,22 @@ - - - - + + + + - - - + + + - - - - + + + + diff --git a/data/dbus/org.tuf.manager.server.service.in b/data/dbus/org.tuf.manager.server.service.in index 3a3c5f4..c286727 100644 --- a/data/dbus/org.tuf.manager.server.service.in +++ b/data/dbus/org.tuf.manager.server.service.in @@ -1,4 +1,4 @@ [D-BUS Service] Name=org.tuf.manager.server -Exec=@LIBEXEC_DIR@/tuf-server +Exec=@LIBEXECDIR@/tuf-server User=root diff --git a/data/gschema/meson.build b/data/gschema/meson.build index 81987e4..7e46cd4 100644 --- a/data/gschema/meson.build +++ b/data/gschema/meson.build @@ -1,12 +1,12 @@ # server doesn't need gschema, only gui or cli if build_gui or build_cli - gnome = import('gnome') - gschemas = files( - 'org.tuf.manager.gschema.xml' - ) - gnome.compile_schemas( - build_by_default: true, - depend_files: gschemas - ) - install_data('org.tuf.manager.gschema.xml', install_dir: join_paths(get_option('prefix'), get_option('datadir'), 'glib-2.0', 'schemas')) + gnome = import('gnome') + gschemas = files( + 'org.tuf.manager.gschema.xml' + ) + gnome.compile_schemas( + build_by_default: true, + depend_files: gschemas + ) + install_data('org.tuf.manager.gschema.xml', install_dir: join_paths(get_option('prefix'), get_option('datadir'), 'glib-2.0', 'schemas')) endif diff --git a/data/gschema/org.tuf.manager.gschema.xml b/data/gschema/org.tuf.manager.gschema.xml index 776496c..5d8b9a8 100644 --- a/data/gschema/org.tuf.manager.gschema.xml +++ b/data/gschema/org.tuf.manager.gschema.xml @@ -1,49 +1,49 @@ - - - false - Should TUF Manager restore previous settings when run - - Should TUF Manager restore previous settings when run - - - - 0 - Fan mode - - The saved fan mode - 0 is balanced - 1 is turbo - 2 is silent - - - - 0 - Keyboard mode - - The saved keyboard mode - 0 is static - 1 is breathing - 2 is color cycle - 3 is strobing - - - - 0 - Keyboard speed - - The saved keyboard speed - 0 is slow - 1 is medium - 2 is fast - - - - "rgb(255,0,0)" - Keyboard color - - The saved keyboard color in rgb format - - - + + + false + Should TUF Manager restore previous settings when run + + Should TUF Manager restore previous settings when run + + + + 0 + Fan mode + + The saved fan mode + 0 is balanced + 1 is turbo + 2 is silent + + + + 0 + Keyboard mode + + The saved keyboard mode + 0 is static + 1 is breathing + 2 is color cycle + 3 is strobing + + + + 0 + Keyboard speed + + The saved keyboard speed + 0 is slow + 1 is medium + 2 is fast + + + + "rgb(255,0,0)" + Keyboard color + + The saved keyboard color in rgb format + + + diff --git a/data/meson.build b/data/meson.build index 5d521af..92a4312 100644 --- a/data/meson.build +++ b/data/meson.build @@ -1,12 +1,12 @@ if build_gui - i18n.merge_file( - input: 'tuf-manager.desktop.in', - output: 'tuf-manager.desktop', - po_dir: join_paths(meson.source_root(), 'po'), - type: 'desktop', - install: true, - install_dir: join_paths(get_option('datadir'), 'applications') - ) + i18n.merge_file( + input: 'tuf-manager.desktop.in', + output: 'tuf-manager.desktop', + po_dir: join_paths(meson.source_root(), 'po'), + type: 'desktop', + install: true, + install_dir: join_paths(get_option('datadir'), 'applications') + ) endif subdir('dbus') diff --git a/data/polkit/meson.build b/data/polkit/meson.build index ee2dabd..50e29cb 100644 --- a/data/polkit/meson.build +++ b/data/polkit/meson.build @@ -1,10 +1,10 @@ if not always_authenticated - i18n.merge_file( - input: 'org.tuf.manager.policy.in', - output: 'org.tuf.manager.policy', - po_dir: join_paths(meson.source_root(), 'po'), - type: 'xml', - install: true, - install_dir: join_paths(get_option('datadir'), 'polkit-1', 'actions') - ) + i18n.merge_file( + input: 'org.tuf.manager.policy.in', + output: 'org.tuf.manager.policy', + po_dir: join_paths(meson.source_root(), 'po'), + type: 'xml', + install: true, + install_dir: join_paths(get_option('datadir'), 'polkit-1', 'actions') + ) endif diff --git a/data/polkit/org.tuf.manager.policy.in b/data/polkit/org.tuf.manager.policy.in index 348f486..c95ad34 100644 --- a/data/polkit/org.tuf.manager.policy.in +++ b/data/polkit/org.tuf.manager.policy.in @@ -1,17 +1,17 @@ + "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" + "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd"> - TUF Manager - https://git.cromer.cl/cromer/tuf-manager/ - package-x-generic - - Authentication is required - - no - auth_admin_keep - auth_admin_keep - - + TUF Manager + https://git.cromer.cl/cromer/tuf-manager/ + tuf-manager + + Authentication is required + + no + auth_admin_keep + auth_admin_keep + + diff --git a/docs/meson.build b/docs/meson.build index 2e7c013..db18318 100644 --- a/docs/meson.build +++ b/docs/meson.build @@ -1,44 +1,44 @@ docs_enabled = get_option('valadocs') if docs_enabled - valadoc = find_program('valadoc', required: docs_enabled) - vala_doc_sources = server_vala_sources - if build_cli - vala_doc_sources += cli_vala_sources - endif - if build_gui - vala_doc_sources += gui_vala_sources - endif - vala_doc_sources += error_vala_sources - vala_doc_sources += config_data_file - valadocs_deps = get_option('valadocs-deps') - docs_deps = [ - '--pkg=polkit-gobject-1', - '--pkg=posix', - '--pkg=gtk+-3.0' - ] - if valadocs_deps - docs_deps += ['--deps'] - endif - custom_target( - 'valadoc', - input: vala_doc_sources, - output: 'valadoc', - command: [ valadoc, - docs_deps, - '--doclet=html', - '--internal', - '--private', - '--force', - '--package-name=@0@'.format(meson.project_name()), - '--package-version=@0@'.format(meson.project_version()), - '--vapidir=@0@'.format(join_paths(meson.source_root(), 'vapi')), - '--directory=@OUTDIR@/valadoc', - vala_doc_sources - ], - build_by_default: true, - install: true, - install_dir: join_paths(get_option('datadir'), 'doc', meson.project_name()) - ) + valadoc = find_program('valadoc', required: docs_enabled) + vala_doc_sources = server_vala_sources + if build_cli + vala_doc_sources += cli_vala_sources + endif + if build_gui + vala_doc_sources += gui_vala_sources + endif + vala_doc_sources += error_vala_sources + vala_doc_sources += config_data_file + valadocs_deps = get_option('valadocs-deps') + docs_deps = [ + '--pkg=polkit-gobject-1', + '--pkg=posix', + '--pkg=gtk+-3.0' + ] + if valadocs_deps + docs_deps += ['--deps'] + endif + custom_target( + 'valadoc', + input: vala_doc_sources, + output: 'valadoc', + command: [ valadoc, + docs_deps, + '--doclet=html', + '--internal', + '--private', + '--force', + '--package-name=@0@'.format(meson.project_name()), + '--package-version=@0@'.format(meson.project_version()), + '--vapidir=@0@'.format(join_paths(meson.source_root(), 'vapi')), + '--directory=@OUTDIR@/valadoc', + vala_doc_sources + ], + build_by_default: true, + install: true, + install_dir: join_paths(get_option('datadir'), 'doc', meson.project_name()) + ) endif install_data('../LICENSE', install_dir: join_paths(get_option('datadir'), 'doc', meson.project_name())) diff --git a/meson.build b/meson.build index fcd918c..98c89b2 100644 --- a/meson.build +++ b/meson.build @@ -1,29 +1,31 @@ project( - 'tuf-manager', - [ - 'c', - 'vala' - ], - version: '0.1.0', - license: 'BSD-3', - default_options: - [ - 'b_ndebug=if-release', - 'c_std=c18', - 'warning_level=3' - ] + 'tuf-manager', + [ + 'c', + 'vala' + ], + version: '0.1.0', + license: 'BSD-3', + default_options: + [ + 'b_ndebug=if-release', + 'c_std=c18', + 'warning_level=3' + ] ) add_global_arguments( - '-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name()), - language: 'c' + '-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name()), + language: 'c' ) always_authenticated = get_option('always-authenticated') build_cli = get_option('build-cli') build_gui = get_option('build-gui') +openrc = get_option('openrc') subdir('po') subdir('data') subdir('src') subdir('docs') subdir('script') +subdir('contrib') diff --git a/meson_options.txt b/meson_options.txt index e1d003d..8436a15 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,30 +1,36 @@ option( - 'valadocs', - type: 'boolean', - value: true, - description: 'Build valadocs' + 'valadocs', + type: 'boolean', + value: true, + description: 'Build valadocs' ) option( - 'valadocs-deps', - type: 'boolean', - value: false, - description: 'Build the valadocs of the dependencies' + 'valadocs-deps', + type: 'boolean', + value: false, + description: 'Build the valadocs of the dependencies' ) option( - 'build-cli', - type : 'boolean', - value: true, - description: 'Build the command line interface to TUF Manager' + 'build-cli', + type : 'boolean', + value: true, + description: 'Build the command line interface to TUF Manager' ) option( - 'build-gui', - type : 'boolean', - value: true, - description: 'Build the graphical user interface to TUF Manager' + 'build-gui', + type : 'boolean', + value: true, + description: 'Build the graphical user interface to TUF Manager' ) option( - 'always-authenticated', - type : 'boolean', - value: true, - description: 'User is always authenticated, so no password is necessary' + 'always-authenticated', + type : 'boolean', + value: true, + description: 'User is always authenticated, so no password is necessary' +) +option( + 'openrc', + type : 'boolean', + value: false, + description: 'Install OpenRC script' ) diff --git a/po/meson.build b/po/meson.build index 1a5f02e..7adc3cb 100644 --- a/po/meson.build +++ b/po/meson.build @@ -1,9 +1,9 @@ i18n = import('i18n') i18n.gettext( - meson.project_name(), - args: - [ - '--directory=' + meson.source_root(), - '--from-code=UTF-8' - ] + meson.project_name(), + args: + [ + '--directory=' + meson.source_root(), + '--from-code=UTF-8' + ] ) diff --git a/script/meson.build b/script/meson.build index 3ea9361..c124b90 100644 --- a/script/meson.build +++ b/script/meson.build @@ -1,5 +1,5 @@ # only gui or cli need gschema if build_gui or build_cli - valadoc = find_program('glib-compile-schemas') + glib_compile_schemas = find_program('glib-compile-schemas') meson.add_install_script('compile_schemas.sh') endif diff --git a/src/cli.vala b/src/cli.vala index 32a8d96..6de940c 100644 --- a/src/cli.vala +++ b/src/cli.vala @@ -16,406 +16,524 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The CLI namespace handles all command line related usage - */ - namespace CLI { - public class TUFManagerApp : Application { + /** + * The CLI namespace handles all command line related usage + */ + namespace CLI { + /** + * This class contains the app that runs on the command line + */ + public class TUFManagerApp : Application { #if ALWAYS_AUTHENTICATED #else - /** - * The subprocess that will contain our tty polkit agent - */ - Subprocess? pkttyagent = null; + /** + * The subprocess that will contain our tty polkit agent + */ + Subprocess? pkttyagent = null; #endif - private enum FanMode { - BALANCED, - TURBO, - SILENT - } + /** + * The possible modes the fan can be in + */ + private enum FanMode { + /** + * This is the default fan mode + */ + BALANCED, + /** + * This mode makes the fans run at full power + */ + TURBO, + /** + * This mode trys to keep the fans as quiet as possible + */ + SILENT + } - private enum KeyboardMode { - STATIC, - BREATHING, - COLOR_CYCLE, - STROBING - } + /** + * The possible modes the keyboard lighting can be set to + */ + private enum KeyboardMode { + /** + * This mode makes the keyboard lights stay a constant color + */ + STATIC, + /** + * This mode makes the keyboard lights turn on and off at a variable speed + */ + BREATHING, + /** + * This mode makes the keyboard lights cycle through various colors at a variable speed + */ + COLOR_CYCLE, + /** + * This modes makes the keyboad lights strobe fast + */ + STROBING + } - private enum KeyboardSpeed { - SLOW, - MEDIUM, - FAST - } + /** + * The possible speeds that can bet set for the keyboard + */ + private enum KeyboardSpeed { + /** + * Slow speed + */ + SLOW, + /** + * Medium speed + */ + MEDIUM, + /** + * Fast speed + */ + FAST + } - private bool invalid = false; + /** + * This flag is set if the command line arguments are invalid + */ + private bool invalid = false; - private bool help = false; - private bool info = false; - private bool version = false; + /** + * This flag is set if the user wants to see help + */ + private bool help = false; - private bool fan = false; - private FanMode? fan_mode = null; + /** + * This flag is set if the user wants to see info + */ + private bool info = false; - private bool lighting = false; - private KeyboardMode? keyboard_mode = null; + /** + * This flag is set if the user wants to see the version + */ + private bool version = false; - private bool speed = false; - private KeyboardSpeed? keyboard_speed = null; + /** + * This flag is set if the user wants to set the fan mode + */ + private bool fan = false; - private bool color = false; - private Gdk.RGBA? rgba = null; + /** + * This contains the mode the user want to set for the fan + */ + private FanMode? fan_mode = null; - public TUFManagerApp () { - Object (application_id: "cl.cromer.tuf.manager", flags: ApplicationFlags.HANDLES_COMMAND_LINE); - set_inactivity_timeout (1000); - } + /** + * This flag is set if the user wants to set the keyboard lighting mode + */ + private bool lighting = false; - public override void activate () { - this.hold (); - this.release (); - } + /** + * This contains the mode the user want to set for the keyboard + */ + private KeyboardMode? keyboard_mode = null; - private int _command_line (ApplicationCommandLine command_line) { - try { - connect_dbus (); - } - catch (TUFError e) { - command_line.printerr (_ ("Error: ") + e.message + "\n"); - } + /** + * This flag is set if the user wants to change the speed of the lights on the keyboard + */ + private bool speed = false; - string[] args = command_line.get_arguments (); + /** + * This contains the speed of the lights on the keyboard that the user wants to set + */ + private KeyboardSpeed? keyboard_speed = null; - if (args.length == 1) { - help = true; - } - else if (args.length > 1) { - switch (args[1]) { - case "version": - version = true; - check_second_argument (args); - break; - case "help": - help = true; - check_second_argument (args); - break; - case "info": - info = true; - check_second_argument (args); - break; - case "fan": - fan = true; - break; - case "lighting": - lighting = true; - break; - case "speed": - speed = true; - break; - case "color": - color = true; - break; - default: - invalid = true; - break; - } + /** + * This flag is set if the user wants to change the keyboard color + */ + private bool color = false; - if (args.length > 2) { - if (fan) { - switch (args[2]) { - case "balanced": - fan_mode = FanMode.BALANCED; - break; - case "turbo": - fan_mode = FanMode.TURBO; - break; - case "silent": - fan_mode = FanMode.SILENT; - break; - default: - invalid = true; - break; - } - } + /** + * This contains the color in rgba format to set + */ + private Gdk.RGBA? rgba = null; - if (lighting) { - switch (args[2]) { - case "static": - keyboard_mode = KeyboardMode.STATIC; - break; - case "breath": - keyboard_mode = KeyboardMode.BREATHING; - break; - case "cycle": - keyboard_mode = KeyboardMode.COLOR_CYCLE; - break; - case "strobe": - keyboard_mode = KeyboardMode.STROBING; - break; - default: - invalid = true; - break; - } - } + /** + * Initializes the command line app and sets a timeout so that the process can finish before return is called + */ + public TUFManagerApp () { + Object (application_id: "cl.cromer.tuf.manager", flags: ApplicationFlags.HANDLES_COMMAND_LINE); + set_inactivity_timeout (1000); + } - if (speed) { - switch (args[2]) { - case "slow": - keyboard_speed = KeyboardSpeed.SLOW; - break; - case "medium": - keyboard_speed = KeyboardSpeed.MEDIUM; - break; - case "fast": - keyboard_speed = KeyboardSpeed.FAST; - break; - default: - invalid = true; - break; - } - } + /** + * This is called when the application gets activated + */ + public override void activate () { + this.hold (); + this.release (); + } - if (color) { - try { - // Make sure it's a valid hex color - Regex regex = new Regex ("^#[0-9A-F]{6}$"); + /** + * This is the logic that controls the command lines options used + * + * @param command_line The command line that is in use + * @return Returns 0 on success or an error code if failure + */ + private int _command_line (ApplicationCommandLine command_line) { + try { + connect_tuf_server (); + } + catch (TUFError e) { + command_line.printerr (_ ("Error: ") + e.message + "\n"); + } - if (!regex.match (args[2].up ())) { - invalid = true; - } - } - catch (RegexError e) { - command_line.printerr (_ ("Error: ") + e.message + "\n"); - } - finally { - rgba = Gdk.RGBA (); - rgba.parse (args[2]); - } - } - } + string[] args = command_line.get_arguments (); - if (fan && fan_mode == null) { - invalid = true; - } + if (args.length == 1) { + // If no arguments are passed show help + help = true; + } + else if (args.length > 1) { + // Find out what the first argument is + switch (args[1]) { + case "version": + version = true; + check_second_argument (args); + break; + case "help": + help = true; + check_second_argument (args); + break; + case "info": + info = true; + check_second_argument (args); + break; + case "fan": + fan = true; + break; + case "lighting": + lighting = true; + break; + case "speed": + speed = true; + break; + case "color": + color = true; + break; + default: + invalid = true; + break; + } - if (lighting && keyboard_mode == null) { - invalid = true; - } + if (args.length > 2) { + // If the first argument requires a second argument, look for it here + if (fan) { + switch (args[2]) { + case "balanced": + fan_mode = FanMode.BALANCED; + break; + case "turbo": + fan_mode = FanMode.TURBO; + break; + case "silent": + fan_mode = FanMode.SILENT; + break; + default: + invalid = true; + break; + } + } - if (speed && keyboard_speed == null) { - invalid = true; - } + if (lighting) { + switch (args[2]) { + case "static": + keyboard_mode = KeyboardMode.STATIC; + break; + case "breath": + keyboard_mode = KeyboardMode.BREATHING; + break; + case "cycle": + keyboard_mode = KeyboardMode.COLOR_CYCLE; + break; + case "strobe": + keyboard_mode = KeyboardMode.STROBING; + break; + default: + invalid = true; + break; + } + } - if (color && rgba == null) { - invalid = true; - } - } + if (speed) { + switch (args[2]) { + case "slow": + keyboard_speed = KeyboardSpeed.SLOW; + break; + case "medium": + keyboard_speed = KeyboardSpeed.MEDIUM; + break; + case "fast": + keyboard_speed = KeyboardSpeed.FAST; + break; + default: + invalid = true; + break; + } + } - if (invalid) { - command_line.printerr (_ ("Invalid arguments!\n\n")); - print_usage (command_line); - release_cli (); - return 1; - } - else if (version) { - command_line.print (_ ("Version: ") + VERSION + "\n"); - release_cli (); - return 0; - } - else if (help) { - print_usage (command_line); - release_cli (); - return 0; - } - else if (info) { - command_line.print (_ ("Client version: ") + VERSION + "\n"); - command_line.print (_ ("Server version: ") + get_server_version () + "\n"); - var current_setting = get_fan_mode (); - switch (current_setting) { - case 0: - command_line.print (_ ("Current fan mode: Balanced\n")); - break; - case 1: - command_line.print (_ ("Current fan mode: Turbo\n")); - break; - case 2: - command_line.print (_ ("Current fan mode: Silent\n")); - break; - default: - command_line.printerr (_ ("Error: Could not get current fan mode!\n")); - break; - } - current_setting = get_keyboard_mode (); - switch (current_setting) { - case 0: - command_line.print (_ ("Current keyboard lighting: Static\n")); - break; - case 1: - command_line.print (_ ("Current keyboard lighting: Breathing\n")); - break; - case 2: - command_line.print (_ ("Current keyboard lighting: Color Cycle\n")); - break; - case 3: - command_line.print (_ ("Current keyboard lighting: Strobing\n")); - break; - default: - command_line.printerr (_ ("Error: Could not get current keyboard mode!\n")); - break; - } - current_setting = get_keyboard_speed (); - switch (current_setting) { - case 0: - command_line.print (_ ("Current keyboard speed: Slow\n")); - break; - case 1: - command_line.print (_ ("Current keyboard speed: Medium\n")); - break; - case 2: - command_line.print (_ ("Current keyboard speed: Fast\n")); - break; - default: - command_line.printerr (_ ("Error: Could not get current fan mode!\n")); - break; - } - var current_color = get_keyboard_color (); - var color_hex = "#%02x%02x%02x".printf ( - (uint) (Math.round (current_color.red * 255)), - (uint) (Math.round (current_color.green * 255)), - (uint) (Math.round (current_color.blue * 255)) - ).up (); - command_line.print (_ ("Current keyboard color: " + color_hex + "\n")); - release_cli (); - return 0; - } - else if (fan) { + if (color) { + try { + // Make sure it's a valid hex color + Regex regex = new Regex ("^#[0-9A-F]{6}$"); + + if (!regex.match (args[2].up ())) { + invalid = true; + } + } + catch (RegexError e) { + command_line.printerr (_ ("Error: ") + e.message + "\n"); + } + finally { + rgba = Gdk.RGBA (); + rgba.parse (args[2]); + } + } + } + + if (fan && fan_mode == null) { + invalid = true; + } + + if (lighting && keyboard_mode == null) { + invalid = true; + } + + if (speed && keyboard_speed == null) { + invalid = true; + } + + if (color && rgba == null) { + invalid = true; + } + } + + if (invalid) { + command_line.printerr (_ ("Invalid arguments!\n\n")); + print_usage (command_line); + release_cli (); + return 1; + } + else if (version) { + command_line.print (_ ("Version: ") + VERSION + "\n"); + release_cli (); + return 0; + } + else if (help) { + print_usage (command_line); + release_cli (); + return 0; + } + else if (info) { + command_line.print (_ ("Client version: ") + VERSION + "\n"); + command_line.print (_ ("Server version: ") + get_server_version () + "\n"); + var current_setting = get_fan_mode (); + switch (current_setting) { + case 0: + command_line.print (_ ("Current fan mode: Balanced\n")); + break; + case 1: + command_line.print (_ ("Current fan mode: Turbo\n")); + break; + case 2: + command_line.print (_ ("Current fan mode: Silent\n")); + break; + default: + command_line.printerr (_ ("Error: Could not get current fan mode!\n")); + break; + } + current_setting = get_keyboard_mode (); + switch (current_setting) { + case 0: + command_line.print (_ ("Current keyboard lighting: Static\n")); + break; + case 1: + command_line.print (_ ("Current keyboard lighting: Breathing\n")); + break; + case 2: + command_line.print (_ ("Current keyboard lighting: Color Cycle\n")); + break; + case 3: + command_line.print (_ ("Current keyboard lighting: Strobing\n")); + break; + default: + command_line.printerr (_ ("Error: Could not get current keyboard mode!\n")); + break; + } + current_setting = get_keyboard_speed (); + switch (current_setting) { + case 0: + command_line.print (_ ("Current keyboard speed: Slow\n")); + break; + case 1: + command_line.print (_ ("Current keyboard speed: Medium\n")); + break; + case 2: + command_line.print (_ ("Current keyboard speed: Fast\n")); + break; + default: + command_line.printerr (_ ("Error: Could not get current fan mode!\n")); + break; + } + var current_color = get_keyboard_color (); + var color_hex = "#%02x%02x%02x".printf ( + (uint) (Math.round (current_color.red * 255)), + (uint) (Math.round (current_color.green * 255)), + (uint) (Math.round (current_color.blue * 255)) + ).up (); + command_line.print (_ ("Current keyboard color: " + color_hex + "\n")); + release_cli (); + return 0; + } + else if (fan) { #if ALWAYS_AUTHENTICATED - int mode = fan_mode; - tuf_server.procedure_finished.connect (release_cli); - set_fan_mode (mode); + int mode = fan_mode; + tuf_server.procedure_finished.connect (release_cli); + set_fan_mode (mode); #else - try { - pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); - Timeout.add (200, () => { - int mode = fan_mode; - tuf_server.procedure_finished.connect (release_cli); - set_fan_mode (mode); - return false; - }); - } - catch (Error e) { - command_line.printerr (_ ("Error: ") + e.message + "\n"); - } + try { + pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); + Timeout.add (200, () => { + int mode = fan_mode; + tuf_server.procedure_finished.connect (release_cli); + set_fan_mode (mode); + return false; + }); + } + catch (Error e) { + command_line.printerr (_ ("Error: ") + e.message + "\n"); + } #endif - return 0; - } - else if (lighting) { + return 0; + } + else if (lighting) { #if ALWAYS_AUTHENTICATED - int mode = keyboard_mode; - tuf_server.procedure_finished.connect (release_cli); - set_keyboard_mode (mode); + int mode = keyboard_mode; + tuf_server.procedure_finished.connect (release_cli); + set_keyboard_mode (mode); #else - try { - pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); + try { + pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); - Timeout.add (200, () => { - int mode = keyboard_mode; - tuf_server.procedure_finished.connect (release_cli); - set_keyboard_mode (mode); - return false; - }); - } - catch (Error e) { - command_line.printerr (_ ("Error: ") + e.message + "\n"); - } + Timeout.add (200, () => { + int mode = keyboard_mode; + tuf_server.procedure_finished.connect (release_cli); + set_keyboard_mode (mode); + return false; + }); + } + catch (Error e) { + command_line.printerr (_ ("Error: ") + e.message + "\n"); + } #endif - return 0; - } - else if (speed) { + return 0; + } + else if (speed) { #if ALWAYS_AUTHENTICATED - int set_speed = keyboard_speed; - tuf_server.procedure_finished.connect (release_cli); - set_keyboard_speed (set_speed); + int set_speed = keyboard_speed; + tuf_server.procedure_finished.connect (release_cli); + set_keyboard_speed (set_speed); #else - try { - pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); + try { + pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); - Timeout.add (200, () => { - int set_speed = keyboard_speed; - tuf_server.procedure_finished.connect (release_cli); - set_keyboard_speed (set_speed); - return false; - }); - } - catch (Error e) { - command_line.printerr (_ ("Error: ") + e.message + "\n"); - } - #endif - return 0; - } - else if (color) { + Timeout.add (200, () => { + int set_speed = keyboard_speed; + tuf_server.procedure_finished.connect (release_cli); + set_keyboard_speed (set_speed); + return false; + }); + } + catch (Error e) { + command_line.printerr (_ ("Error: ") + e.message + "\n"); + } + #endif + return 0; + } + else if (color) { #if ALWAYS_AUTHENTICATED - tuf_server.procedure_finished.connect (release_cli); - set_keyboard_color (rgba); + tuf_server.procedure_finished.connect (release_cli); + set_keyboard_color (rgba); #else - try { - pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); + try { + pkttyagent = new Subprocess.newv ({"pkttyagent"}, SubprocessFlags.NONE); - Timeout.add (200, () => { - tuf_server.procedure_finished.connect (release_cli); - set_keyboard_color (rgba); - return false; - }); - } - catch (Error e) { - command_line.printerr (_ ("Error: ") + e.message + "\n"); - } + Timeout.add (200, () => { + tuf_server.procedure_finished.connect (release_cli); + set_keyboard_color (rgba); + return false; + }); + } + catch (Error e) { + command_line.printerr (_ ("Error: ") + e.message + "\n"); + } #endif - return 0; - } - return 0; - } + return 0; + } + return 0; + } - private void check_second_argument (string[] args) { - if (args.length > 2) { - invalid = true; - } - } + /** + * If there are more arguments than there should be we need to invalidate + * TODO: Change this to something better later + * + * @param args The arguments to check the length on + */ + private void check_second_argument (string[] args) { + if (args.length > 2) { + invalid = true; + } + } - private void print_usage (ApplicationCommandLine command_line) { - command_line.print ("Usage: tuf-cli COMMAND [SUBCOMMAND] ...\n\n"); - command_line.print (" version Print the version of tuf-cli\n"); - command_line.print (" help Show this help screen\n"); - command_line.print (" fan [balanced, turbo, silent] Set the fan mode\n"); - command_line.print (" lighting [static, breath, cycle, stobe] Set the keyboard lighting\n"); - command_line.print (" speed [slow, medium, fast] Set the keyboard lighting speed\n"); - command_line.print (" color [\"#XXXXXX\"] Set the keyboard color\n"); - command_line.print (" info Show the current config\n\n"); - command_line.print ("Examples:\n"); - command_line.print (" Silence fan: tuf-cli fan silent\n"); - command_line.print (" Change RGB color: tuf-cli color \"#FF0000\"\n"); - } + /** + * Print the usage for the user if help is called or they do something invalid + * + * @param command_line The command line currently in use to print to + */ + private void print_usage (ApplicationCommandLine command_line) { + command_line.print ("Usage: tuf-cli COMMAND [SUBCOMMAND] ...\n\n"); + command_line.print (" version Print the version of tuf-cli\n"); + command_line.print (" help Show this help screen\n"); + command_line.print (" fan [balanced, turbo, silent] Set the fan mode\n"); + command_line.print (" lighting [static, breath, cycle, stobe] Set the keyboard lighting\n"); + command_line.print (" speed [slow, medium, fast] Set the keyboard lighting speed\n"); + command_line.print (" color [\"#XXXXXX\"] Set the keyboard color\n"); + command_line.print (" info Show the current config\n\n"); + command_line.print ("Examples:\n"); + command_line.print (" Silence fan: tuf-cli fan silent\n"); + command_line.print (" Change RGB color: tuf-cli color \"#FF0000\"\n"); + } - /** - * This method releases the command line program from it's loop - */ - public void release_cli () { + /** + * This method releases the command line program from it's hold + * This will should be called when by a signal from the server to release the program + */ + public void release_cli () { #if ALWAYS_AUTHENTICATED #else - if (pkttyagent != null) { - pkttyagent.force_exit (); - } + if (pkttyagent != null) { + pkttyagent.force_exit (); + } #endif - this.release (); - } + this.release (); + } - public override int command_line (ApplicationCommandLine command_line) { - // keep the application running until we are done with this commandline - this.hold (); - int res = _command_line (command_line); - return res; - } - } - } + /** + * The command line application starts here, we hold it in a loop until + * the serve responds and releases + * + * @param command_line The command line that is going to be used + * @return Returns the status code from our command line program + */ + public override int command_line (ApplicationCommandLine command_line) { + // keep the application running until we are done with this commandline + this.hold (); + int res = _command_line (command_line); + return res; + } + } + } } diff --git a/src/common.vala b/src/common.vala index 2a666d6..6fd7154 100644 --- a/src/common.vala +++ b/src/common.vala @@ -16,133 +16,193 @@ * The TUF Manager namespace */ namespace TUFManager { - private TUFServerInterface tuf_server; - private BusName bus_name; + /** + * The TUF Server interface that is running in the background + */ + private TUFServerInterface tuf_server; - private void connect_dbus () throws TUFError { - bus_name = new BusName ("org.tuf.manager"); - connect_tuf_server (); - if (get_server_version () != VERSION) { - throw new TUFError.UNMATCHED_VERSIONS ("The server and client versions do not match!"); - } - } + /** + * The bus name to send to the server to identify itself + */ + private BusName bus_name; - private void connect_tuf_server () { - try { - tuf_server = Bus.get_proxy_sync (BusType.SYSTEM, "org.tuf.manager.server", "/org/tuf/manager/server"); - } - catch (IOError e) { - stderr.printf ("Error: %s\n", e.message); - } - } + /** + * Connect to the TUF Server daemon via dbus + * + * @throws TUFError Thrown when the server and the client versions don't match + */ + private void connect_tuf_server () throws TUFError { + bus_name = new BusName ("org.tuf.manager"); - private string? get_server_version () { - try { - return tuf_server.get_server_version (); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - return null; - } + try { + tuf_server = Bus.get_proxy_sync (BusType.SYSTEM, "org.tuf.manager.server", "/org/tuf/manager/server"); + } + catch (IOError e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } - private int get_fan_mode () { - try { - return tuf_server.get_fan_mode (); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - return -3; - } + string? server_version = get_server_version (); + if (server_version == null) { + return; + } - private void set_fan_mode (int mode) { - try { - tuf_server.set_fan_mode (mode, bus_name); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } + if (server_version != VERSION) { + throw new TUFError.UNMATCHED_VERSIONS (_ ("The server and client versions do not match!")); + } + } - private Gdk.RGBA get_keyboard_color () { - try { - return tuf_server.get_keyboard_color (); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - return Gdk.RGBA (); - } + /** + * Get the server version from the running daemon + * + * @return Returns a string containing the server version or null on failure + */ + private string? get_server_version () { + try { + return tuf_server.get_server_version (); + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + return null; + } - private void set_keyboard_color (Gdk.RGBA color) { - try { - tuf_server.set_keyboard_color (color, bus_name); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } + /** + * Get the mode the fan is in + * + * @return Returns the fan mode or -3 on error + */ + private int get_fan_mode () { + try { + return tuf_server.get_fan_mode (); + } + catch (TUFError e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + return -3; + } - private int get_keyboard_mode () { - try { - return tuf_server.get_keyboard_mode (); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - return -3; - } + /** + * Set the fan mode + * + * @param mode The new mode to set + */ + private void set_fan_mode (int mode) { + try { + tuf_server.set_fan_mode (mode, bus_name); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } - private void set_keyboard_mode (int mode) { - try { - tuf_server.set_keyboard_mode (mode, bus_name); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } + /** + * Get the current keyboard color + * + * @return Returns an RGBA struct containing the keyboard color or an initialized RGBA struct on error + */ + private Gdk.RGBA get_keyboard_color () { + try { + return tuf_server.get_keyboard_color (); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + return Gdk.RGBA (); + } - private int get_keyboard_speed () { - try { - return tuf_server.get_keyboard_speed (); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - return -3; - } + /** + * Set a new keyboard color + * + * @param color An RGBA struct containing the new color to set + */ + private void set_keyboard_color (Gdk.RGBA color) { + try { + tuf_server.set_keyboard_color (color, bus_name); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } - private void set_keyboard_speed (int speed) { - try { - tuf_server.set_keyboard_speed (speed, bus_name); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } + /** + * Get the current lighting mode of the keyboard + * + * @return Returns the current keyboard lighting mode or -3 on error + */ + private int get_keyboard_mode () { + try { + return tuf_server.get_keyboard_mode (); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + return -3; + } + + /** + * Set the keyboard lighting mode + * + * @param mode The new mode to set for the keyboard lighting + */ + private void set_keyboard_mode (int mode) { + try { + tuf_server.set_keyboard_mode (mode, bus_name); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } + + /** + * Get the current keyboard lighting speed + * + * @return Returns the current speed or -3 on error + */ + private int get_keyboard_speed () { + try { + return tuf_server.get_keyboard_speed (); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + return -3; + } + + /** + * Set a new keyboard lighting speed + * + * @param speed The new speed to set + */ + private void set_keyboard_speed (int speed) { + try { + tuf_server.set_keyboard_speed (speed, bus_name); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } } diff --git a/src/config.vala.in b/src/config.vala.in index 9320782..629021b 100644 --- a/src/config.vala.in +++ b/src/config.vala.in @@ -16,23 +16,53 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The package name used for translations - */ - public const string GETTEXT_PACKAGE = "@GETTEXT_PACKAGE@"; - /** - * The version of the application - */ - public const string VERSION = "@VERSION@"; - /** - * The location of the thermal control - */ - public const string THERMAL_PATH = "/sys/devices/platform/faustus/throttle_thermal_policy"; - public const string RED_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_red"; - public const string GREEN_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_green"; - public const string BLUE_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_blue"; - public const string KEYBOARD_MODE_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_mode"; - public const string KEYBOARD_FLAGS_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_flags"; - public const string KEYBOARD_SPEED_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_speed"; - public const string KEYBOARD_SET_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_set"; + /** + * The package name used for translations + */ + public const string GETTEXT_PACKAGE = "@GETTEXT_PACKAGE@"; + + /** + * The version of the application + */ + public const string VERSION = "@VERSION@"; + + /** + * The location of the thermal control + */ + public const string THERMAL_PATH = "/sys/devices/platform/faustus/throttle_thermal_policy"; + + /** + * The location of the red hue control + */ + public const string RED_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_red"; + + /** + * The location of the green hue control + */ + public const string GREEN_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_green"; + + /** + * The location of the blue hue control + */ + public const string BLUE_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_blue"; + + /** + * The location of the keyboard mode control + */ + public const string KEYBOARD_MODE_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_mode"; + + /** + * The location of the flags for the keyboard control + */ + public const string KEYBOARD_FLAGS_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_flags"; + + /** + * The location of the keyboard lighting speed control + */ + public const string KEYBOARD_SPEED_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_speed"; + + /** + * The location of the keyboard set control + */ + public const string KEYBOARD_SET_PATH = "/sys/devices/platform/faustus/kbbl/kbbl_set"; } diff --git a/src/error.vala b/src/error.vala index 1c91436..edc1256 100644 --- a/src/error.vala +++ b/src/error.vala @@ -16,29 +16,29 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * This contains errors related to TUF Manager - */ - public errordomain TUFError { - /** - * The stream failed to open - */ - STREAM, - /** - * An invalid value was returned - */ - INVALID_VALUE, - /** - * The server is busy and can't do another task yet - */ - BUSY, - /** - * The client and serer version don't match - */ - UNMATCHED_VERSIONS, - /** - * The user is unauthorized to perform that action - */ - UNAUTHORIZED - } + /** + * This contains errors related to TUF Manager + */ + public errordomain TUFError { + /** + * The stream failed to open + */ + STREAM, + /** + * An invalid value was returned + */ + INVALID_VALUE, + /** + * The server is busy and can't do another task yet + */ + BUSY, + /** + * The client and serer version don't match + */ + UNMATCHED_VERSIONS, + /** + * The user is unauthorized to perform that action + */ + UNAUTHORIZED + } } diff --git a/src/gui-window.vala b/src/gui-window.vala index ce2324e..924a892 100644 --- a/src/gui-window.vala +++ b/src/gui-window.vala @@ -16,232 +16,254 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The GUI namespace contains a GTK based interface to interact with the TUF laptop - */ - namespace GUI { - /** - * The main window to show to the user - */ - [GtkTemplate (ui = "/cl/cromer/tuf-manager/tuf.manager.window.ui")] - public class MainWindow : Gtk.ApplicationWindow { - /** - * This is used to make sure that the sysfs files are not written to until everything is initialized - */ - private bool initialized = false; - private Settings settings; + /** + * The GUI namespace contains a GTK based interface to interact with the TUF laptop + */ + namespace GUI { + /** + * The main window to show to the user + */ + [GtkTemplate (ui = "/cl/cromer/tuf-manager/tuf.manager.window.ui")] + public class MainWindow : Gtk.ApplicationWindow { + /** + * This is used to make sure that the sysfs files are not written to until everything is initialized + */ + private bool initialized = false; - /** - * The fan mode combo box - */ - [GtkChild] - private Gtk.ComboBoxText fan_mode; + /** + * The settings object from gschema/dconf + */ + private Settings settings; - [GtkChild] - private Gtk.ComboBoxText keyboard_mode; + /** + * The fan mode combo box + */ + [GtkChild] + private Gtk.ComboBoxText fan_mode; - [GtkChild] - private Gtk.ComboBoxText keyboard_speed; + /** + * They keyboard mode combo box + */ + [GtkChild] + private Gtk.ComboBoxText keyboard_mode; - /** - * The color chooser widget - */ - [GtkChild] - private Gtk.ColorChooserWidget keyboard_color; + /** + * The keyboard speed combox box + */ + [GtkChild] + private Gtk.ComboBoxText keyboard_speed; - /** - * Create the main window - * @param application The application used to make the GLib object - */ - public MainWindow (Gtk.Application application) { - Object (application: application); - } + /** + * The color chooser widget + */ + [GtkChild] + private Gtk.ColorChooserWidget keyboard_color; - public void initialize () { - settings = new Settings ("org.tuf.manager"); - try { - connect_dbus (); - } - catch (TUFError e) { - stderr.printf ("Error: %s:\n", e.message); - if (e.code == TUFError.UNMATCHED_VERSIONS) { - Gtk.MessageDialog msg; - msg = new Gtk.MessageDialog (this, - Gtk.DialogFlags.MODAL, - Gtk.MessageType.ERROR, - Gtk.ButtonsType.CLOSE, - _ ("The current running tuf-server version doesn't match the gui version!")); - msg.response.connect ((response_id) => { - msg.destroy (); - this.close (); - }); - msg.show (); - } - } - finally { - print (_ ("Client version: ") + VERSION + "\n"); - print (_ ("Server version: ") + get_server_version () + "\n"); + /** + * Create the main window + * @param application The application used to make the GLib object + */ + public MainWindow (Gtk.Application application) { + Object (application: application); + } - if (settings.get_boolean ("restore")) { - restore (); - } - else { - // Get the fan speed - // 0 - normal - // 1 - boost - // 2 - silent - var mode = get_fan_mode (); - if (mode >= 0) { - fan_mode.set_active (mode); - print (_ ("Current fan mode: ") + fan_mode.get_active_text () + "\n"); - } - else { - stderr.printf ("%s\n", _ ("Error: Could not get current fan mode!")); - } + /** + * This is called after the window is created to initialize it's interface + */ + public void initialize () { + settings = new Settings ("org.tuf.manager"); + try { + connect_tuf_server (); + } + catch (TUFError e) { + stderr.printf ("Error: %s:\n", e.message); + if (e.code == TUFError.UNMATCHED_VERSIONS) { + Gtk.MessageDialog msg; + msg = new Gtk.MessageDialog (this, + Gtk.DialogFlags.MODAL, + Gtk.MessageType.ERROR, + Gtk.ButtonsType.CLOSE, + _ ("The current running tuf-server version doesn't match the gui version!")); + msg.response.connect ((response_id) => { + msg.destroy (); + this.close (); + }); + msg.show (); + } + } + finally { + print (_ ("Client version: ") + VERSION + "\n"); + print (_ ("Server version: ") + get_server_version () + "\n"); - // Get the keyboard mode - mode = get_keyboard_mode (); - if (mode >= 0) { - keyboard_mode.set_active (mode); - if (mode == 2) { - keyboard_color.sensitive = false; - } - if (mode == 0 || mode == 3) { - keyboard_speed.sensitive = false; - } - print (_ ("Current keyboard mode: ") + keyboard_mode.get_active_text () + "\n"); - } - else { - stderr.printf ("%s\n", _ ("Error: Could not get current keyboard mode!")); - } + if (settings.get_boolean ("restore")) { + restore (); + } + else { + // Get the fan speed + var mode = get_fan_mode (); + if (mode >= 0) { + fan_mode.set_active (mode); + print (_ ("Current fan mode: ") + fan_mode.get_active_text () + "\n"); + } + else { + stderr.printf ("%s\n", _ ("Error: Could not get current fan mode!")); + } - // Get the keyboard speed - var speed = get_keyboard_speed (); - if (speed >= 0) { - keyboard_speed.set_active (mode); - print (_ ("Current keyboard speed: ") + keyboard_speed.get_active_text () + "\n"); - } - else { - stderr.printf ("%s\n", _ ("Error: Could not get current keyboard speed!")); - } + // Get the keyboard mode + mode = get_keyboard_mode (); + if (mode >= 0) { + keyboard_mode.set_active (mode); + if (mode == 2) { + keyboard_color.sensitive = false; + } + if (mode == 0 || mode == 3) { + keyboard_speed.sensitive = false; + } + print (_ ("Current keyboard mode: ") + keyboard_mode.get_active_text () + "\n"); + } + else { + stderr.printf ("%s\n", _ ("Error: Could not get current keyboard mode!")); + } - // Get the keyboard color - var color = get_keyboard_color (); - keyboard_color.set_rgba (color); - print (_ ("Current keyboard color: ") + color.to_string () + "\n"); - } + // Get the keyboard speed + var speed = get_keyboard_speed (); + if (speed >= 0) { + keyboard_speed.set_active (mode); + print (_ ("Current keyboard speed: ") + keyboard_speed.get_active_text () + "\n"); + } + else { + stderr.printf ("%s\n", _ ("Error: Could not get current keyboard speed!")); + } - initialized = true; - } - } + // Get the keyboard color + var color = get_keyboard_color (); + keyboard_color.set_rgba (color); + print (_ ("Current keyboard color: ") + color.to_string () + "\n"); + } - public void restore () { - var mode = settings.get_int ("fan-mode"); - if (mode >= 0 && mode <= 2) { - if (get_fan_mode () != mode) { - set_fan_mode (mode); - } - fan_mode.set_active (mode); - } - print (_ ("Current fan mode: ") + fan_mode.get_active_text () + "\n"); + initialized = true; + } + } - mode = settings.get_int ("keyboard-mode"); - if (mode >= 0 && mode <= 3) { - if (get_keyboard_mode () != mode) { - set_keyboard_mode (mode); - } - keyboard_mode.set_active (mode); - if (mode == 2) { - keyboard_color.sensitive = false; - } - if (mode == 0 || mode == 3) { - keyboard_speed.sensitive = false; - } - } - print (_ ("Current keyboard mode: ") + keyboard_mode.get_active_text () + "\n"); + /** + * Used to restore the previous config from donf + * TODO: Move this to a status bar app and/or user daemon + */ + public void restore () { + var mode = settings.get_int ("fan-mode"); + if (mode >= 0 && mode <= 2) { + if (get_fan_mode () != mode) { + set_fan_mode (mode); + } + fan_mode.set_active (mode); + } + print (_ ("Current fan mode: ") + fan_mode.get_active_text () + "\n"); - var speed = settings.get_int ("keyboard-speed"); - if (speed >= 0 && speed <= 2) { - if (get_keyboard_speed () != speed) { - set_keyboard_speed (speed); - } - keyboard_speed.set_active (speed); - } - print (_ ("Current keyboard speed: ") + keyboard_speed.get_active_text () + "\n"); + mode = settings.get_int ("keyboard-mode"); + if (mode >= 0 && mode <= 3) { + if (get_keyboard_mode () != mode) { + set_keyboard_mode (mode); + } + keyboard_mode.set_active (mode); + if (mode == 2) { + keyboard_color.sensitive = false; + } + if (mode == 0 || mode == 3) { + keyboard_speed.sensitive = false; + } + } + print (_ ("Current keyboard mode: ") + keyboard_mode.get_active_text () + "\n"); - var color = settings.get_string ("keyboard-color"); - var rgba = Gdk.RGBA (); - rgba.parse (color); - if (!get_keyboard_color ().equal (rgba)) { - set_keyboard_color (rgba); - } - keyboard_color.set_rgba (rgba); - print (_ ("Current keyboard color: ") + color.to_string () + "\n"); - } + var speed = settings.get_int ("keyboard-speed"); + if (speed >= 0 && speed <= 2) { + if (get_keyboard_speed () != speed) { + set_keyboard_speed (speed); + } + keyboard_speed.set_active (speed); + } + print (_ ("Current keyboard speed: ") + keyboard_speed.get_active_text () + "\n"); - /** - * Called when the user changes the fan mode - * - * * 1 - normal mode - * * 2 - boost mode - * * 3 - silent mode - * @param combo_box The combo box that changed - */ - [GtkCallback] - public void on_fan_mode_changed (Gtk.ComboBox combo_box) { - if (initialized) { - int mode = combo_box.get_active (); - set_fan_mode (mode); + var color = settings.get_string ("keyboard-color"); + var rgba = Gdk.RGBA (); + rgba.parse (color); + if (!get_keyboard_color ().equal (rgba)) { + set_keyboard_color (rgba); + } + keyboard_color.set_rgba (rgba); + print (_ ("Current keyboard color: ") + color.to_string () + "\n"); + } - settings.set_int ("fan-mode", mode); - } - } + /** + * Called when the user changes the fan mode + * + * @param combo_box The combo box that changed + */ + [GtkCallback] + public void on_fan_mode_changed (Gtk.ComboBox combo_box) { + if (initialized) { + int mode = combo_box.get_active (); + set_fan_mode (mode); - /** - * Called when the user changes the keyboard lighting mode - * @param combo_box The combo box that changed - */ - [GtkCallback] - public void on_keyboard_mode_changed (Gtk.ComboBox combo_box) { - if (initialized) { - int mode = combo_box.get_active (); - if (mode == 2) { - keyboard_color.sensitive = false; - } - else { - keyboard_color.sensitive = true; - } - if (mode == 1 || mode == 2) { - keyboard_speed.sensitive = true; - } - else { - keyboard_speed.sensitive = false; - } - set_keyboard_mode (mode); + settings.set_int ("fan-mode", mode); + } + } - settings.set_int ("keyboard-mode", mode); - } - } + /** + * Called when the user changes the keyboard lighting mode + * + * @param combo_box The combo box that changed + */ + [GtkCallback] + public void on_keyboard_mode_changed (Gtk.ComboBox combo_box) { + if (initialized) { + int mode = combo_box.get_active (); + if (mode == 2) { + keyboard_color.sensitive = false; + } + else { + keyboard_color.sensitive = true; + } + if (mode == 1 || mode == 2) { + keyboard_speed.sensitive = true; + } + else { + keyboard_speed.sensitive = false; + } + set_keyboard_mode (mode); - [GtkCallback] - public void on_speed_changed (Gtk.ComboBox combo_box) { - if (initialized) { - int speed = combo_box.get_active (); - set_keyboard_speed (speed); + settings.set_int ("keyboard-mode", mode); + } + } - settings.set_int ("keyboard-speed", speed); - } - } + /** + * Called when the user changes the keyboard lighting speed + * + * @param combo_box The combo box that changed + */ + [GtkCallback] + public void on_speed_changed (Gtk.ComboBox combo_box) { + if (initialized) { + int speed = combo_box.get_active (); + set_keyboard_speed (speed); - [GtkCallback] - public void on_set_color_clicked (Gtk.Button button) { - if (initialized) { - Gdk.RGBA rgba = keyboard_color.get_rgba (); + settings.set_int ("keyboard-speed", speed); + } + } - set_keyboard_color (rgba); - settings.set_string ("keyboard-color", rgba.to_string ()); - } - } - } - } + /** + * Called when the user clicks the set color button + * + * @param button The button that was clicked + */ + [GtkCallback] + public void on_set_color_clicked (Gtk.Button button) { + if (initialized) { + Gdk.RGBA rgba = keyboard_color.get_rgba (); + + set_keyboard_color (rgba); + settings.set_string ("keyboard-color", rgba.to_string ()); + } + } + } + } } diff --git a/src/gui.vala b/src/gui.vala index 22b2f38..a67fc83 100644 --- a/src/gui.vala +++ b/src/gui.vala @@ -16,45 +16,45 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The GUI namespace contains a GTK based interface to interact with the TUF laptop - */ - namespace GUI { + /** + * The GUI namespace contains a GTK based interface to interact with the TUF laptop + */ + namespace GUI { - /** - * The application class - */ - public class TUFManagerApp : Gtk.Application { - /** - * The main application window - */ - private MainWindow window; + /** + * The GUI application class + */ + public class TUFManagerApp : Gtk.Application { + /** + * The main application window + */ + private MainWindow window; - /** - * Initialize the application and set it's id - */ - public TUFManagerApp () { - application_id = "cl.cromer.tuf.manager"; - } + /** + * Initialize the application and set it's id + */ + public TUFManagerApp () { + application_id = "cl.cromer.tuf.manager"; + } - /** - * Run when the application is activated - */ - public override void activate () { - window = new MainWindow (this); - window.icon = new Gtk.Image.from_resource ("/cl/cromer/tuf-manager/pixdata/tuf-manager.png").get_pixbuf (); - window.show_all (); - window.initialize (); - } + /** + * Run when the application is activated, we call and show the window here + */ + public override void activate () { + window = new MainWindow (this); + window.icon = new Gtk.Image.from_resource ("/cl/cromer/tuf-manager/pixdata/tuf-manager.png").get_pixbuf (); + window.show_all (); + window.initialize (); + } - /** - * Run when the application starts - */ - public override void startup () { - Intl.textdomain (GETTEXT_PACKAGE); - Intl.setlocale (LocaleCategory.ALL, ""); - base.startup (); - } - } - } + /** + * Run when the application starts, we set the language here + */ + public override void startup () { + Intl.textdomain (GETTEXT_PACKAGE); + Intl.setlocale (LocaleCategory.ALL, ""); + base.startup (); + } + } + } } diff --git a/src/main.vala.in b/src/main.vala.in index 157a5f3..65b1b52 100644 --- a/src/main.vala.in +++ b/src/main.vala.in @@ -16,24 +16,24 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The entry point to the TUF Manager application - * - * @param args Arguments passed from the command line - * @return Returns the status from the application - */ - public static int main (string[] args) { - var application = new @APP@.TUFManagerApp (); - try { - application.register (); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - if (application.get_is_remote ()) { - stderr.printf ("Error: %s\n", _ ("Another instance of TUF Manager is already running!")); - return -1; - } - return application.run (args); - } + /** + * The entry point to the TUF Manager UI applications + * + * @param args Arguments passed from the command line + * @return Returns the status from the application + */ + public static int main (string[] args) { + var application = new @APP@.TUFManagerApp (); + try { + application.register (); + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + if (application.get_is_remote ()) { + stderr.printf (_ ("Error: %s\n"), _ ("Another instance of TUF Manager is already running!")); + return -1; + } + return application.run (args); + } } diff --git a/src/meson.build b/src/meson.build index c2edcc2..517c62e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,31 +1,31 @@ server_dependencies = [ - dependency('glib-2.0'), - dependency('gio-unix-2.0'), - dependency('gtk+-3.0', version: '>=3.0.0'), - meson.get_compiler('c').find_library('m', required: true), - meson.get_compiler('vala').find_library('posix') + dependency('glib-2.0'), + dependency('gio-unix-2.0'), + dependency('gtk+-3.0', version: '>=3.0.0'), + meson.get_compiler('c').find_library('m', required: true), + meson.get_compiler('vala').find_library('posix') ] # if not always authenticated then polkit will be used for authentication if not always_authenticated - server_dependencies += [dependency('polkit-gobject-1')] + server_dependencies += [dependency('polkit-gobject-1')] endif if build_cli - cli_dependencies = [ - dependency('glib-2.0'), - dependency('gtk+-3.0', version: '>=3.0.0'), - meson.get_compiler('c').find_library('m', required: true) - ] + cli_dependencies = [ + dependency('glib-2.0'), + dependency('gtk+-3.0', version: '>=3.0.0'), + meson.get_compiler('c').find_library('m', required: true) + ] endif if build_gui - gui_dependencies = [ - dependency('glib-2.0'), - dependency('gobject-2.0'), - dependency('gtk+-3.0', version: '>=3.10.0'), - dependency('gmodule-2.0', version: '>=2.0') # gmodule-export-2.0 is needed to connect the handlers from glade ui - ] + gui_dependencies = [ + dependency('glib-2.0'), + dependency('gobject-2.0'), + dependency('gtk+-3.0', version: '>=3.10.0'), + dependency('gmodule-2.0', version: '>=2.0') # gmodule-export-2.0 is needed to connect the handlers from glade ui + ] endif config_data = configuration_data() @@ -33,62 +33,62 @@ config_data.set('VERSION', meson.project_version()) config_data.set('GETTEXT_PACKAGE', meson.project_name()) config_data_file = configure_file( - input: 'config.vala.in', - output: 'config.vala', - configuration: config_data + input: 'config.vala.in', + output: 'config.vala', + configuration: config_data ) if build_cli - config_data = configuration_data() - config_data.set('APP', 'CLI') - cli_main_file = configure_file( - input: 'main.vala.in', - output: 'cli-main.vala', - configuration: config_data - ) + config_data = configuration_data() + config_data.set('APP', 'CLI') + cli_main_file = configure_file( + input: 'main.vala.in', + output: 'cli-main.vala', + configuration: config_data + ) endif if build_gui - config_data = configuration_data() - config_data.set('APP', 'GUI') - gui_main_file = configure_file( - input: 'main.vala.in', - output: 'gui-main.vala', - configuration: config_data - ) + config_data = configuration_data() + config_data.set('APP', 'GUI') + gui_main_file = configure_file( + input: 'main.vala.in', + output: 'gui-main.vala', + configuration: config_data + ) endif error_vala_sources = files( - 'error.vala' + 'error.vala' ) # The sources containing a main function definition are separated to prevent # conflict errors when building valadoc. valadoc doesn't like multiple entry # points even if they are in different namespaces. server_vala_sources_main = files( - 'server-main.vala' + 'server-main.vala' ) server_vala_sources = files( - 'server.vala', - 'server-interface.vala' + 'server.vala', + 'server-interface.vala' ) if build_cli - cli_vala_sources = files( - 'cli.vala', - 'server-interface.vala', - 'common.vala' - ) + cli_vala_sources = files( + 'cli.vala', + 'server-interface.vala', + 'common.vala' + ) endif if build_gui - gui_vala_sources = files( - 'gui.vala', - 'gui-window.vala', - 'server-interface.vala', - 'common.vala' - ) + gui_vala_sources = files( + 'gui.vala', + 'gui-window.vala', + 'server-interface.vala', + 'common.vala' + ) endif server_sources = server_vala_sources @@ -97,61 +97,61 @@ server_sources += error_vala_sources server_sources += config_data_file if build_cli - cli_sources = cli_vala_sources - cli_sources += cli_main_file - cli_sources += error_vala_sources - cli_sources += config_data_file + cli_sources = cli_vala_sources + cli_sources += cli_main_file + cli_sources += error_vala_sources + cli_sources += config_data_file endif if build_gui - gui_sources = gui_vala_sources - gui_sources += gui_main_file - gui_sources += error_vala_sources - gui_sources += [tuf_manager_gresource] - gui_sources += config_data_file + gui_sources = gui_vala_sources + gui_sources += gui_main_file + gui_sources += error_vala_sources + gui_sources += [tuf_manager_gresource] + gui_sources += config_data_file endif server_vala_args = [] if always_authenticated - server_vala_args += ['--define=ALWAYS_AUTHENTICATED'] + server_vala_args += ['--define=ALWAYS_AUTHENTICATED'] endif if build_cli - cli_vala_args = [] - if always_authenticated - cli_vala_args += ['--define=ALWAYS_AUTHENTICATED'] - endif + cli_vala_args = [] + if always_authenticated + cli_vala_args += ['--define=ALWAYS_AUTHENTICATED'] + endif endif if build_gui - gui_vala_args = ['--gresources='+join_paths(meson.source_root(), 'data/ui/tuf.manager.gresource.xml')] + gui_vala_args = ['--gresources='+join_paths(meson.source_root(), 'data/ui/tuf.manager.gresource.xml')] endif server_exe = executable( - 'tuf-server', - server_sources, - dependencies: server_dependencies, - vala_args: server_vala_args, - install: true, - install_dir: join_paths(get_option('libexecdir'), meson.project_name()) + 'tuf-server', + server_sources, + dependencies: server_dependencies, + vala_args: server_vala_args, + install: true, + install_dir: join_paths(get_option('libexecdir'), meson.project_name()) ) if build_cli - cli_exe = executable( - 'tuf-cli', - cli_sources, - vala_args: cli_vala_args, - dependencies: cli_dependencies, - install: true - ) + cli_exe = executable( + 'tuf-cli', + cli_sources, + vala_args: cli_vala_args, + dependencies: cli_dependencies, + install: true + ) endif if build_gui - gui_exe = executable( - 'tuf-gui', - gui_sources, - dependencies: gui_dependencies, - vala_args: gui_vala_args, - install: true - ) + gui_exe = executable( + 'tuf-gui', + gui_sources, + dependencies: gui_dependencies, + vala_args: gui_vala_args, + install: true + ) endif diff --git a/src/server-interface.vala b/src/server-interface.vala index b96a56e..12865e7 100644 --- a/src/server-interface.vala +++ b/src/server-interface.vala @@ -16,20 +16,111 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * This interface defines the dbus daemon provided by the server - */ - [DBus (name = "org.tuf.manager.server")] - public interface TUFServerInterface : Object { - public abstract string get_server_version () throws Error; - public abstract int get_fan_mode () throws Error, TUFError; - public abstract void set_fan_mode (int mode, BusName sender) throws Error, TUFError; - public abstract Gdk.RGBA get_keyboard_color () throws Error, TUFError; - public abstract void set_keyboard_color (Gdk.RGBA color, BusName sender) throws Error, TUFError; - public abstract int get_keyboard_mode () throws Error, TUFError; - public abstract void set_keyboard_mode (int mode, BusName sender) throws Error, TUFError; - public abstract int get_keyboard_speed () throws Error, TUFError; - public abstract void set_keyboard_speed (int speed, BusName sender) throws Error, TUFError; - public signal void procedure_finished (); - } + /** + * This interface defines the dbus daemon provided by the server + */ + [DBus (name = "org.tuf.manager.server")] + public interface TUFServerInterface : Object { + /** + * This signal is emited when a procedure finishes + */ + public signal void procedure_finished (); + + /** + * Get the version of the currently running server + * + * @return Returns a string containing the version + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + */ + public abstract string get_server_version () throws Error; + + /** + * Get the current fan mode + * + * @return Returns the current fan mode + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem reading a value from the stream + */ + public abstract int get_fan_mode () throws Error, TUFError; + + /** + * Set a new fan mode + * + * * 0 - balanced mode + * * 1 - turbo mode + * * 2 - silent mode + * + * @param mode The new mode to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract void set_fan_mode (int mode, BusName sender) throws Error, TUFError; + + /** + * Get the current keyboard color + * + * @return Returns an RGBA struct containing the color + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract Gdk.RGBA get_keyboard_color () throws Error, TUFError; + + /** + * Set the keyboard color + * + * @param color The new RGBA color to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract void set_keyboard_color (Gdk.RGBA color, BusName sender) throws Error, TUFError; + + /** + * Get the current keyboard mode + * + * @return Returns the mode + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract int get_keyboard_mode () throws Error, TUFError; + + /** + * Set a new keyboard mode + * + * * 0 - static + * * 1 - breathing + * * 2 - color cycle + * * 3 - strobing + * + * @param mode The new mode to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract void set_keyboard_mode (int mode, BusName sender) throws Error, TUFError; + + /** + * Get the current keyboard lighting speed + * + * @return The current speed + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract int get_keyboard_speed () throws Error, TUFError; + + /** + * Set a new keyboard speed + * + * * 0 - slow + * * 1 - medium + * * 2 - fast + * + * @param speed The new speed to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public abstract void set_keyboard_speed (int speed, BusName sender) throws Error, TUFError; + } } diff --git a/src/server-main.vala b/src/server-main.vala index 89ff5dc..03b0391 100644 --- a/src/server-main.vala +++ b/src/server-main.vala @@ -16,86 +16,97 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The server namespace contains anything releated to working as a dbus daemon - * and handling root related tasks - */ - namespace Server { - private static bool foreground = false; + /** + * The server namespace contains anything releated to working as a dbus daemon + * and handling root related tasks + */ + namespace Server { + /** + * Run the process in the foreground + */ + private static bool foreground = false; - private const OptionEntry[] options = { - { "foreground", 'f', 0, OptionArg.NONE, ref foreground, N_ ("Run the daemon in the foreground"), null }, - { null } - }; + /** + * The command line arguments available + */ + private const OptionEntry[] options = { + { "foreground", 'f', 0, OptionArg.NONE, ref foreground, N_ ("Run the daemon in the foreground"), null }, + { null } + }; - private static void on_exit (int signum) { - if (loop != null) { - loop.quit (); - } - } + /** + * Called when the server should exit + * + * @param signum The signal that caused the server to exit + */ + private static void on_exit (int signum) { + if (loop != null) { + loop.quit (); + } + } - /** - * The entry point to the server launches a system dbus daemon - * - * @param args Arguments passed from the command line - * @return Returns 0 on success - */ - public static int main (string[] args) { - Intl.setlocale (LocaleCategory.ALL, ""); - Intl.bindtextdomain (GETTEXT_PACKAGE, "/usr/share/locale"); - Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - Intl.textdomain (GETTEXT_PACKAGE); + /** + * The entry point to the server launches a system dbus daemon + * + * @param args Arguments passed from the command line + * @return Returns 0 on success + */ + public static int main (string[] args) { + Intl.setlocale (LocaleCategory.ALL, ""); + Intl.bindtextdomain (GETTEXT_PACKAGE, "/usr/share/locale"); + Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + Intl.textdomain (GETTEXT_PACKAGE); - try { - var opt_context = new OptionContext (""); - opt_context.set_translation_domain (GETTEXT_PACKAGE); - opt_context.set_help_enabled (true); - opt_context.add_main_entries (options, null); - opt_context.parse (ref args); - } - catch (OptionError e) { - print (_ ("Error: %s\n"), e.message); - print (_ ("Run '%s --help' to see a full list of available command line options.\n"), args[0]); - return 1; - } + try { + var opt_context = new OptionContext (""); + opt_context.set_translation_domain (GETTEXT_PACKAGE); + opt_context.set_help_enabled (true); + opt_context.add_main_entries (options, null); + opt_context.parse (ref args); + } + catch (OptionError e) { + print (_ ("Error: %s\n"), e.message); + print (_ ("Run '%s --help' to see a full list of available command line options.\n"), args[0]); + return 1; + } - if (!foreground) { - var pid = Posix.fork (); - if (pid < 0) { - Posix.exit (Posix.EXIT_FAILURE); - } - else if (pid > 0) { - Posix.exit (Posix.EXIT_SUCCESS); - } + if (!foreground) { + var pid = Posix.fork (); + if (pid < 0) { + Posix.exit (Posix.EXIT_FAILURE); + } + else if (pid > 0) { + Posix.exit (Posix.EXIT_SUCCESS); + } - Posix.umask (0); + Posix.umask (0); - var sid = Posix.setsid (); - if (sid < 0) { - Posix.exit (Posix.EXIT_FAILURE); - } + var sid = Posix.setsid (); + if (sid < 0) { + Posix.exit (Posix.EXIT_FAILURE); + } - if (Posix.chdir ("/") < 0) { - Posix.exit (Posix.EXIT_FAILURE); - } - } + if (Posix.chdir ("/") < 0) { + Posix.exit (Posix.EXIT_FAILURE); + } + } - Process.signal (ProcessSignal.INT, on_exit); - Process.signal (ProcessSignal.TERM, on_exit); + Process.signal (ProcessSignal.INT, on_exit); + Process.signal (ProcessSignal.TERM, on_exit); - Bus.own_name (BusType.SYSTEM, - "org.tuf.manager.server", - BusNameOwnerFlags.NONE, - on_bus_acquired, - () => {}, - () => { - stderr.printf (_ ("Could not acquire bus name\n")); - }); + Bus.own_name (BusType.SYSTEM, + "org.tuf.manager.server", + BusNameOwnerFlags.NONE, + on_bus_acquired, + () => {}, + () => { + stderr.printf (_ ("Could not acquire bus name\n")); + }); - loop = new MainLoop (); - loop.run (); + loop = new MainLoop (); + loop.run (); - return 0; - } - } + return 0; + } + } } diff --git a/src/server.vala b/src/server.vala index a909853..0bb2012 100644 --- a/src/server.vala +++ b/src/server.vala @@ -16,505 +16,612 @@ * The TUF Manager namespace */ namespace TUFManager { - /** - * The server namespace contains anything releated to working as a dbus daemon - * and handling root related tasks - */ - namespace Server { - /** - * The global instance of the server running as a dbus daemon - */ - TUFServer tuf_server; - /** - * The loop of the dbus daemon running in the background - */ - MainLoop? loop = null; + /** + * The server namespace contains anything releated to working as a dbus daemon + * and handling root related tasks + */ + namespace Server { + /** + * The global instance of the server running as a dbus daemon + */ + TUFServer tuf_server; - /** - * Register the bus after the name has been aquired - * - * @param conn The connection to register with - */ - private void on_bus_acquired (DBusConnection conn) { - try { - tuf_server = new TUFManager.Server.TUFServer (); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - try { - conn.register_object ("/org/tuf/manager/server", tuf_server); - } - catch (IOError e) { - stderr.printf ("Could not register service\n"); - if (loop != null) { - loop.quit (); - } - } - } + /** + * The loop of the dbus daemon running in the background + */ + MainLoop? loop = null; - /** - * The TUF Server is a dbus service that runs in the background and uses - * root priveleges via polkit to make system changes - */ - [DBus (name = "org.tuf.manager.server")] - public class TUFServer : Object { - private Mutex locked; - private Mutex authorization; - private GenericSet authorized_senders; - private MainContext context; - public signal void procedure_finished (); + /** + * Register the bus after the name has been aquired + * + * @param conn The connection to register with + */ + private void on_bus_acquired (DBusConnection conn) { + try { + tuf_server = new TUFManager.Server.TUFServer (); + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + try { + conn.register_object ("/org/tuf/manager/server", tuf_server); + } + catch (IOError e) { + stderr.printf (_ ("Error: Could not register service\n")); + if (loop != null) { + loop.quit (); + } + } + } - /** - * The server class initialization - * - * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error - */ - public TUFServer () throws Error { - locked = Mutex (); - authorization = Mutex (); - authorized_senders = new GenericSet (str_hash, str_equal); - context = MainContext.ref_thread_default (); - } + /** + * The TUF Server is a dbus service that runs in the background and uses + * root priveleges via polkit to make system changes + */ + [DBus (name = "org.tuf.manager.server")] + public class TUFServer : Object { + /** + * We use this mutex to lock procedures + */ + private Mutex locked; - /** - * Get authorization to run root tasks via polkit - * - * @param sender The bus that sent the request - * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error - */ - private async bool get_authorization (BusName sender) throws Error { #if ALWAYS_AUTHENTICATED - return true; #else - bool authorized = false; - authorization.lock (); - bool fast_authorized = authorized_senders.contains (sender); - authorization.unlock (); - if (fast_authorized) { - var idle = new IdleSource (); - idle.set_priority (Priority.DEFAULT); - idle.set_callback (() => { - return false; - }); - idle.attach (context); - return true; - } - try { - Polkit.Authority authority = yield Polkit.Authority.get_async (); - Polkit.Subject subject = new Polkit.SystemBusName (sender); - var result = yield authority.check_authorization ( - subject, - "org.tuf.manager.save", - null, - Polkit.CheckAuthorizationFlags.ALLOW_USER_INTERACTION); - authorized = result.get_is_authorized (); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - /*if (!authorized) { - stderr.printf ("%s\n", _ ("Authentication failed!")); - } - else { - stderr.printf ("%s\n", _ ("Authentication passed!")); - }*/ - return authorized; + /** + * This mutex is used to lock the authorization process via polkit + */ + private Mutex authorization; + + /** + * A list of authorized senders so that they don't need to reauthenicate constantly + */ + private GenericSet authorized_senders; + + /** + * The main thread context used in fast authentication when reauthenticating + */ + private MainContext context; #endif - } - /** - * Get the version of the currently running server - * - * @return Returns a string containing the version - * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error - */ - public string get_server_version () throws Error { - return VERSION; - } + /** + * This signal is emited when a procedure finishes + */ + public signal void procedure_finished (); - /** - * Get the current fan mode - * - * @return Returns the current fan mode - * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error - * @throws TUFError Thrown if there is a problem reading a value from the stream - */ - public int get_fan_mode () throws Error, TUFError { - var stream = FileStream.open (THERMAL_PATH, "r"); - if (stream == null) { - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + /** + * The server class initialization + * + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + */ + public TUFServer () throws Error { +#if ALWAYS_AUTHENTICATED +#else + authorization = Mutex (); + authorized_senders = new GenericSet (str_hash, str_equal); + context = MainContext.ref_thread_default (); +#endif + locked = Mutex (); + } - var line = stream.read_line (); - if (line == null) { - throw new TUFError.INVALID_VALUE (_ ("File is empty!")); - } + /** + * Get authorization to run root tasks via polkit + * + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + */ + private async bool get_authorization (BusName sender) throws Error { +#if ALWAYS_AUTHENTICATED + return true; +#else + bool authorized = false; + authorization.lock (); + bool fast_authorized = authorized_senders.contains (sender); + authorization.unlock (); + if (fast_authorized) { + var idle = new IdleSource (); + idle.set_priority (Priority.DEFAULT); + idle.set_callback (() => { + return false; + }); + idle.attach (context); + return true; + } + try { + Polkit.Authority authority = yield Polkit.Authority.get_async (); + Polkit.Subject subject = new Polkit.SystemBusName (sender); + var result = yield authority.check_authorization ( + subject, + "org.tuf.manager.save", + null, + Polkit.CheckAuthorizationFlags.ALLOW_USER_INTERACTION); + authorized = result.get_is_authorized (); + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + return authorized; +#endif + } - var mode = int.parse (line); - if (mode < 0 || mode > 2) { - throw new TUFError.INVALID_VALUE (_ ("File contains invalid value!")); - } + /** + * Get the version of the currently running server + * + * @return Returns a string containing the version + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + */ + public string get_server_version () throws Error { + return VERSION; + } - return mode; - } + /** + * Get the current fan mode + * + * @return Returns the current fan mode + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem reading a value from the stream + */ + public int get_fan_mode () throws Error, TUFError { + var stream = FileStream.open (THERMAL_PATH, "r"); + if (stream == null) { + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - /** - * Set a new fan mode - * - * * 1 - normal mode - * * 2 - boost mode - * * 3 - silent mode - * - * @param mode The new mode to set - * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error - * @throws TUFError Thrown if there is a problem writing a value to the stream - */ - public void set_fan_mode (int mode, BusName sender) throws Error, TUFError { - get_authorization.begin (sender, (obj, res) => { - bool authorized = false; - try { - authorized = get_authorization.end (res); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } + var line = stream.read_line (); + if (line == null) { + throw new TUFError.INVALID_VALUE (_ ("File is empty!")); + } - if (authorized) { - try { - set_fan_mode_authorized (mode, sender); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } - else { - // Not authorized, so let's end this - procedure_finished (); - } - }); - } + var mode = int.parse (line); + if (mode < 0 || mode > 2) { + throw new TUFError.INVALID_VALUE (_ ("File contains invalid value!")); + } - private void set_fan_mode_authorized (int mode, BusName sender) throws Error, TUFError { - locked.lock (); - var stream = FileStream.open (THERMAL_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + return mode; + } - if (mode < 0 || mode > 2) { - locked.unlock (); - throw new TUFError.INVALID_VALUE (_ ("Invalid value!")); - } + /** + * Set a new fan mode + * + * * 0 - balanced mode + * * 1 - turbo mode + * * 2 - silent mode + * + * @param mode The new mode to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public void set_fan_mode (int mode, BusName sender) throws Error, TUFError { + get_authorization.begin (sender, (obj, res) => { + bool authorized = false; + try { + authorized = get_authorization.end (res); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } - stream.puts (mode.to_string ()); - locked.unlock (); - procedure_finished (); - } + if (authorized) { + try { + set_fan_mode_authorized (mode, sender); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } + else { + // Not authorized, so let's end this + procedure_finished (); + } + }); + } - public Gdk.RGBA get_keyboard_color () throws Error, TUFError { - string color = "#"; + /** + * The user was authorized to set the mode, here we do it + * + * @param mode The new mode to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + private void set_fan_mode_authorized (int mode, BusName sender) throws Error, TUFError { + locked.lock (); + var stream = FileStream.open (THERMAL_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - // Get red - var stream = FileStream.open (RED_PATH, "r"); - if (stream == null) { - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + if (mode < 0 || mode > 2) { + locked.unlock (); + throw new TUFError.INVALID_VALUE (_ ("Invalid value!")); + } - var line = stream.read_line (); - if (line == null) { - throw new TUFError.INVALID_VALUE (_ ("File is empty!")); - } + stream.puts (mode.to_string ()); + locked.unlock (); + procedure_finished (); + } - color += line; + /** + * Get the current keyboard color + * + * @return Returns an RGBA struct containing the color + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public Gdk.RGBA get_keyboard_color () throws Error, TUFError { + string color = "#"; - // Get green - stream = FileStream.open (GREEN_PATH, "r"); - if (stream == null) { - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + // Get red + var stream = FileStream.open (RED_PATH, "r"); + if (stream == null) { + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - line = stream.read_line (); - if (line == null) { - throw new TUFError.INVALID_VALUE (_ ("File is empty!")); - } + var line = stream.read_line (); + if (line == null) { + throw new TUFError.INVALID_VALUE (_ ("File is empty!")); + } - color += line; + color += line; - // Get blue - stream = FileStream.open (BLUE_PATH, "r"); - if (stream == null) { - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + // Get green + stream = FileStream.open (GREEN_PATH, "r"); + if (stream == null) { + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - line = stream.read_line (); - if (line == null) { - throw new TUFError.INVALID_VALUE (_ ("File is empty!")); - } + line = stream.read_line (); + if (line == null) { + throw new TUFError.INVALID_VALUE (_ ("File is empty!")); + } - color += line; + color += line; - var rgba = Gdk.RGBA (); - rgba.parse (color); + // Get blue + stream = FileStream.open (BLUE_PATH, "r"); + if (stream == null) { + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - return rgba; - } + line = stream.read_line (); + if (line == null) { + throw new TUFError.INVALID_VALUE (_ ("File is empty!")); + } - public void set_keyboard_color (Gdk.RGBA color, BusName sender) throws Error, TUFError { - get_authorization.begin (sender, (obj, res) => { - bool authorized = false; - try { - authorized = get_authorization.end (res); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } + color += line; - if (authorized) { - try { - set_keyboard_color_authorized (color, sender); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } - else { - // Not authorized, so let's end this - procedure_finished (); - } - }); - } + var rgba = Gdk.RGBA (); + rgba.parse (color); - private void set_keyboard_color_authorized (Gdk.RGBA color, BusName sender) throws Error, TUFError { - locked.lock (); + return rgba; + } - var red = "%02x".printf ((uint) (Math.round (color.red * 255))).up (); - var green = "%02x".printf ((uint) (Math.round (color.green * 255))).up (); - var blue = "%02x".printf ((uint) (Math.round (color.blue * 255))).up (); - var keyboard_set = "1"; - var keyboard_flags = "2a"; + /** + * Set the keyboard color + * + * @param color The new RGBA color to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public void set_keyboard_color (Gdk.RGBA color, BusName sender) throws Error, TUFError { + get_authorization.begin (sender, (obj, res) => { + bool authorized = false; + try { + authorized = get_authorization.end (res); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } - var stream = FileStream.open (RED_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (red); + if (authorized) { + try { + set_keyboard_color_authorized (color, sender); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } + else { + // Not authorized, so let's end this + procedure_finished (); + } + }); + } - stream = FileStream.open (GREEN_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (green); + /** + * The user was authorized to set the color, here we do it + * + * @param color The new RGBA color to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + private void set_keyboard_color_authorized (Gdk.RGBA color, BusName sender) throws Error, TUFError { + locked.lock (); - stream = FileStream.open (BLUE_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (blue); + var red = "%02x".printf ((uint) (Math.round (color.red * 255))).up (); + var green = "%02x".printf ((uint) (Math.round (color.green * 255))).up (); + var blue = "%02x".printf ((uint) (Math.round (color.blue * 255))).up (); + var keyboard_set = "1"; + var keyboard_flags = "2a"; - stream = FileStream.open (KEYBOARD_FLAGS_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (keyboard_flags); + var stream = FileStream.open (RED_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (red); - stream = FileStream.open (KEYBOARD_SET_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (keyboard_set); + stream = FileStream.open (GREEN_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (green); - locked.unlock (); - procedure_finished (); - } + stream = FileStream.open (BLUE_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (blue); - public int get_keyboard_mode () throws Error, TUFError { - var stream = FileStream.open (KEYBOARD_MODE_PATH, "r"); - if (stream == null) { - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + stream = FileStream.open (KEYBOARD_FLAGS_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (keyboard_flags); - var line = stream.read_line (); - if (line == null) { - throw new TUFError.INVALID_VALUE (_ ("File is empty!")); - } + stream = FileStream.open (KEYBOARD_SET_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (keyboard_set); - var mode = int.parse (line); - if (mode < 0 || mode > 3) { - throw new TUFError.INVALID_VALUE (_ ("File contains invalid value!")); - } + locked.unlock (); + procedure_finished (); + } - return mode; - } + /** + * Get the current keyboard mode + * + * @return Returns the mode + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public int get_keyboard_mode () throws Error, TUFError { + var stream = FileStream.open (KEYBOARD_MODE_PATH, "r"); + if (stream == null) { + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - public void set_keyboard_mode (int mode, BusName sender) throws Error, TUFError { - get_authorization.begin (sender, (obj, res) => { - bool authorized = false; - try { - authorized = get_authorization.end (res); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } + var line = stream.read_line (); + if (line == null) { + throw new TUFError.INVALID_VALUE (_ ("File is empty!")); + } - if (authorized) { - try { - set_keyboard_mode_authorized (mode, sender); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } - else { - // Not authorized, so let's end this - procedure_finished (); - } - }); - } + var mode = int.parse (line); + if (mode < 0 || mode > 3) { + throw new TUFError.INVALID_VALUE (_ ("File contains invalid value!")); + } - private void set_keyboard_mode_authorized (int mode, BusName sender) throws Error, TUFError { - locked.lock (); - var keyboard_set = "1"; - var keyboard_flags = "2a"; + return mode; + } - var stream = FileStream.open (KEYBOARD_MODE_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + /** + * Set a new keyboard mode + * + * * 0 - static + * * 1 - breathing + * * 2 - color cycle + * * 3 - strobing + * + * @param mode The new mode to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public void set_keyboard_mode (int mode, BusName sender) throws Error, TUFError { + get_authorization.begin (sender, (obj, res) => { + bool authorized = false; + try { + authorized = get_authorization.end (res); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } - if (mode < 0 || mode > 3) { - locked.unlock (); - throw new TUFError.INVALID_VALUE (_ ("Invalid value!")); - } - stream.puts (mode.to_string ()); + if (authorized) { + try { + set_keyboard_mode_authorized (mode, sender); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } + else { + // Not authorized, so let's end this + procedure_finished (); + } + }); + } - stream = FileStream.open (KEYBOARD_FLAGS_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (keyboard_flags); + /** + * The user was authorized to set the mode, here we do it + * + * @param mode The new mode to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + private void set_keyboard_mode_authorized (int mode, BusName sender) throws Error, TUFError { + locked.lock (); + var keyboard_set = "1"; + var keyboard_flags = "2a"; - stream = FileStream.open (KEYBOARD_SET_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (keyboard_set); + var stream = FileStream.open (KEYBOARD_MODE_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - locked.unlock (); - procedure_finished (); - } + if (mode < 0 || mode > 3) { + locked.unlock (); + throw new TUFError.INVALID_VALUE (_ ("Invalid value!")); + } + stream.puts (mode.to_string ()); - public int get_keyboard_speed () throws Error, TUFError { - var stream = FileStream.open (KEYBOARD_SPEED_PATH, "r"); - if (stream == null) { - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + stream = FileStream.open (KEYBOARD_FLAGS_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (keyboard_flags); - var line = stream.read_line (); - if (line == null) { - throw new TUFError.INVALID_VALUE (_ ("File is empty!")); - } + stream = FileStream.open (KEYBOARD_SET_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (keyboard_set); - var mode = int.parse (line); - if (mode < 0 || mode > 2) { - throw new TUFError.INVALID_VALUE (_ ("File contains invalid value!")); - } + locked.unlock (); + procedure_finished (); + } - return mode; - } + /** + * Get the current keyboard lighting speed + * + * @return The current speed + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public int get_keyboard_speed () throws Error, TUFError { + var stream = FileStream.open (KEYBOARD_SPEED_PATH, "r"); + if (stream == null) { + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - public void set_keyboard_speed (int speed, BusName sender) throws Error, TUFError { - get_authorization.begin (sender, (obj, res) => { - bool authorized = false; - try { - authorized = get_authorization.end (res); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } + var line = stream.read_line (); + if (line == null) { + throw new TUFError.INVALID_VALUE (_ ("File is empty!")); + } - if (authorized) { - try { - set_keyboard_speed_authorized (speed, sender); - } - catch (TUFError e) { - stderr.printf ("Error: %s\n", e.message); - } - catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - } - else { - // Not authorized, so let's end this - procedure_finished (); - } - }); - } + var speed = int.parse (line); + if (speed < 0 || speed > 2) { + throw new TUFError.INVALID_VALUE (_ ("File contains invalid value!")); + } - private void set_keyboard_speed_authorized (int speed, BusName sender) throws Error, TUFError { - locked.lock (); - var keyboard_set = "1"; - var keyboard_flags = "2a"; + return speed; + } - var stream = FileStream.open (KEYBOARD_SPEED_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } + /** + * Set a new keyboard speed + * + * * 0 - slow + * * 1 - medium + * * 2 - fast + * + * @param speed The new speed to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + public void set_keyboard_speed (int speed, BusName sender) throws Error, TUFError { + get_authorization.begin (sender, (obj, res) => { + bool authorized = false; + try { + authorized = get_authorization.end (res); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } - if (speed < 0 || speed > 2) { - locked.unlock (); - throw new TUFError.INVALID_VALUE (_ ("Invalid value!")); - } - stream.puts (speed.to_string ()); + if (authorized) { + try { + set_keyboard_speed_authorized (speed, sender); + } + catch (TUFError e) { + stderr.printf ("Error: %s\n", e.message); + } + catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + } + else { + // Not authorized, so let's end this + procedure_finished (); + } + }); + } - stream = FileStream.open (KEYBOARD_FLAGS_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (keyboard_flags); + /** + * The user was authorized to set the speed, here we do it + * + * @param speed The new speed to set + * @param sender The bus that sent the request + * @throws Error Thrown if there is a problem connecting to a dbus session or an IO error + * @throws TUFError Thrown if there is a problem writing a value to the stream + */ + private void set_keyboard_speed_authorized (int speed, BusName sender) throws Error, TUFError { + locked.lock (); + var keyboard_set = "1"; + var keyboard_flags = "2a"; - stream = FileStream.open (KEYBOARD_SET_PATH, "w"); - if (stream == null) { - locked.unlock (); - throw new TUFError.STREAM (_ ("Failed to open stream!")); - } - stream.puts (keyboard_set); + var stream = FileStream.open (KEYBOARD_SPEED_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } - locked.unlock (); - procedure_finished (); - } - } - } + if (speed < 0 || speed > 2) { + locked.unlock (); + throw new TUFError.INVALID_VALUE (_ ("Invalid value!")); + } + stream.puts (speed.to_string ()); + + stream = FileStream.open (KEYBOARD_FLAGS_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (keyboard_flags); + + stream = FileStream.open (KEYBOARD_SET_PATH, "w"); + if (stream == null) { + locked.unlock (); + throw new TUFError.STREAM (_ ("Failed to open stream!")); + } + stream.puts (keyboard_set); + + locked.unlock (); + procedure_finished (); + } + } + } }