/* * config - zavai configuration * * Copyright (C) 2009--2010 Enrico Zini * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ namespace zavai { public class Config { protected Lua.LuaVM lua; protected weak string get_string(string name) { lua.get_global(name); weak string res = lua.to_string(-1); lua.pop(1); return res; } protected string set_string(string name, string? val) { if (val == null) lua.push_nil(); else lua.push_string(val); lua.set_global(name); return val; } protected weak int get_int(string name) { lua.get_global(name); int res = lua.to_integer(-1); lua.pop(1); return res; } protected int set_int(string name, int val) { lua.push_integer(val); lua.set_global(name); return val; } private string _version; public string version { get { return _version; } set { _version = set_string("version", value); } } // "phone" or "laptop" private string _profile; public string profile { get { return _profile; } set { _profile = set_string("profile", value); } } private string _homedir; public string homedir { get { return _homedir; } set { _homedir = set_string("homedir", value); } } private string _icondir; public string icondir { get { return _icondir; } set { _icondir = set_string("icondir", value); } } private int _min_button_height; public int min_button_height { get { return _min_button_height; } set { _min_button_height = set_int("min_button_height", value); } } private string _gpsd_host; public string gpsd_host { get { return _gpsd_host; } set { _gpsd_host = set_string("gpsd_host", value); } } private string _gpsd_port; public string gpsd_port { get { return _gpsd_port; } set { _gpsd_port = set_string("gpsd_port", value); } } private string _gprs_apn; public string gprs_apn { get { return _gprs_apn; } set { _gprs_apn = set_string("gprs_apn", value); } } private string _gprs_user; public string gprs_user { get { return _gprs_user; } set { _gprs_user = set_string("gprs_user", value); } } private string _gprs_pass; public string gprs_pass { get { return _gprs_pass; } set { _gprs_pass = set_string("gprs_pass", value); } } private string _sim_pin; public string sim_pin { get { return _sim_pin; } set { _sim_pin = set_string("sim_pin", value); } } private int _power_button_keycode; public int power_button_keycode { get { return _power_button_keycode; } set { _power_button_keycode = set_int("power_button_keycode", value); } } private int _aux_button_keycode; public int aux_button_keycode { get { return _aux_button_keycode; } set { _aux_button_keycode = set_int("aux_button_keycode", value); } } private string _ringtone_alarm; public string ringtone_alarm { get { return _ringtone_alarm; } set { _ringtone_alarm = set_string("ringtone_alarm", value); } } private string _ringtone_call; public string ringtone_call { get { return _ringtone_call; } set { _ringtone_call = set_string("ringtone_call", value); } } private string _ringtone_sms; public string ringtone_sms { get { return _ringtone_sms; } set { _ringtone_sms = set_string("ringtone_sms", value); } } public int backlight_max { get; set; } private string _argv0; public string argv0 { get { return _argv0; } set { if (value.chr(-1, '/') != null) { if (Path.is_absolute(value)) { _argv0 = value; } else { _argv0 = Path.build_filename(Environment.get_current_dir(), value, null); } } else { _argv0 = Environment.find_program_in_path(value); } zavai.log.debug("ARGV0: " + _argv0); } } /// Reread config values from the Lua VM, to be run after running Lua code protected void refresh_from_lua() { _version = get_string("version"); _profile = get_string("profile"); _homedir = get_string("homedir"); _icondir = get_string("icondir"); _min_button_height = get_int("min_button_height"); _gpsd_host = get_string("gpsd_host"); _gpsd_port = get_string("gpsd_port"); _gprs_apn = get_string("gprs_apn"); _gprs_user = get_string("gprs_user"); _gprs_pass = get_string("gprs_pass"); _sim_pin = get_string("sim_pin"); _power_button_keycode = get_int("power_button_keycode"); _aux_button_keycode = get_int("aux_button_keycode"); _ringtone_alarm = get_string("ringtone_alarm"); _ringtone_call = get_string("ringtone_call"); _ringtone_sms = get_string("ringtone_sms"); } public Config() { lua = new Lua.LuaVM(); lua.open_libs(); // Set defaults version = "0.1"; profile = "phone"; homedir = GLib.Environment.get_home_dir() + "/.zavai"; icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR"); if (icondir == null) icondir = "/usr/share/zavai/icons"; min_button_height = 80; gpsd_host = "localhost"; gpsd_port = "gpsd"; gprs_apn = "general.t-mobile.uk"; gprs_user = "x"; gprs_pass = "x"; sim_pin = "1234"; backlight_max = 15; power_button_keycode = 124; aux_button_keycode = 177; ringtone_alarm = "file:///usr/share/sounds/yue-fso/lec1.ogg"; ringtone_call = "file:///usr/share/sounds/yue-fso/jmf1.ogg"; ringtone_sms = "file:///usr/share/sounds/yue-fso/nothing4.ogg"; // Read config if (lua.do_file(homedir + "/config")) { zavai.log.error("Failed to parse " + homedir + "/config: " + lua.to_string(-1)); } refresh_from_lua(); } /** * Find a zavai script. * * ~/.zavai/NAME is searched first, then /usr/share/zavai/hooks/ * * If the script is not found, NULL is returned */ public string? find_script(string name) { string candidate = homedir + "/" + name; if (FileUtils.test(candidate, FileTest.EXISTS)) return candidate; candidate = "/usr/share/zavai/hooks/" + name; if (FileUtils.test(candidate, FileTest.EXISTS)) return candidate; return null; } public void find_and_run_script(string script, string args) throws SpawnError { string cmd = find_script(script); if (cmd == null) throw new SpawnError.NOENT("hook '" + cmd + "' not found"); run_script(cmd + " " + args); } public void run_script(string command) throws SpawnError { zavai.log.info("Run program: " + command); string[] args = command.split(" "); Pid pid; Process.spawn_async( Environment.get_home_dir(), args, null, SpawnFlags.SEARCH_PATH, null, out pid); } public int run_script_sync(string command, out string std_out, out string std_err) throws SpawnError { int status = -1; zavai.log.info("Run program: " + command); string[] args = command.split(" "); bool res = Process.spawn_sync(Environment.get_home_dir(), args, null, SpawnFlags.SEARCH_PATH, null, out std_out, out std_err, out status); return status; } } public Config config = null; }