2 * config - zavai configuration
4 * Copyright (C) 2009--2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 protected Lua.LuaVM lua;
26 protected weak string get_string(string name)
29 weak string res = lua.to_string(-1);
33 protected string set_string(string name, string? val)
42 protected weak int get_int(string name)
45 int res = lua.to_integer(-1);
49 protected int set_int(string name, int val)
51 lua.push_integer(val);
56 private string _version;
59 get { return _version; }
60 set { _version = set_string("version", value); }
63 // "phone" or "laptop"
64 private string _profile;
67 get { return _profile; }
68 set { _profile = set_string("profile", value); }
71 private string _homedir;
74 get { return _homedir; }
75 set { _homedir = set_string("homedir", value); }
78 private string _icondir;
81 get { return _icondir; }
82 set { _icondir = set_string("icondir", value); }
85 private int _min_button_height;
86 public int min_button_height
88 get { return _min_button_height; }
89 set { _min_button_height = set_int("min_button_height", value); }
92 private string _gpsd_host;
93 public string gpsd_host
95 get { return _gpsd_host; }
96 set { _gpsd_host = set_string("gpsd_host", value); }
99 private string _gpsd_port;
100 public string gpsd_port
102 get { return _gpsd_port; }
103 set { _gpsd_port = set_string("gpsd_port", value); }
106 private string _gprs_apn;
107 public string gprs_apn
109 get { return _gprs_apn; }
110 set { _gprs_apn = set_string("gprs_apn", value); }
113 private string _gprs_user;
114 public string gprs_user
116 get { return _gprs_user; }
117 set { _gprs_user = set_string("gprs_user", value); }
120 private string _gprs_pass;
121 public string gprs_pass
123 get { return _gprs_pass; }
124 set { _gprs_pass = set_string("gprs_pass", value); }
127 private string _sim_pin;
128 public string sim_pin
130 get { return _sim_pin; }
131 set { _sim_pin = set_string("sim_pin", value); }
134 private int _power_button_keycode;
135 public int power_button_keycode
137 get { return _power_button_keycode; }
138 set { _power_button_keycode = set_int("power_button_keycode", value); }
141 private int _aux_button_keycode;
142 public int aux_button_keycode
144 get { return _aux_button_keycode; }
145 set { _aux_button_keycode = set_int("aux_button_keycode", value); }
148 private string _ringtone_alarm;
149 public string ringtone_alarm
151 get { return _ringtone_alarm; }
152 set { _ringtone_alarm = set_string("ringtone_alarm", value); }
155 public int backlight_max
161 private string _argv0;
162 public string argv0 {
163 get { return _argv0; }
165 if (value.chr(-1, '/') != null)
167 if (Path.is_absolute(value))
171 _argv0 = Path.build_filename(Environment.get_current_dir(), value, null);
174 _argv0 = Environment.find_program_in_path(value);
176 zavai.log.debug("ARGV0: " + _argv0);
180 /// Reread config values from the Lua VM, to be run after running Lua code
181 protected void refresh_from_lua()
183 _version = get_string("version");
184 _profile = get_string("profile");
185 _homedir = get_string("homedir");
186 _icondir = get_string("icondir");
187 _min_button_height = get_int("min_button_height");
188 _gpsd_host = get_string("gpsd_host");
189 _gpsd_port = get_string("gpsd_port");
190 _gprs_apn = get_string("gprs_apn");
191 _gprs_user = get_string("gprs_user");
192 _gprs_pass = get_string("gprs_pass");
193 _sim_pin = get_string("sim_pin");
194 _power_button_keycode = get_int("power_button_keycode");
195 _aux_button_keycode = get_int("aux_button_keycode");
196 _ringtone_alarm = get_string("ringtone_alarm");
201 lua = new Lua.LuaVM();
207 homedir = GLib.Environment.get_home_dir() + "/.zavai";
208 icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR");
210 icondir = "/usr/share/zavai/icons";
211 min_button_height = 80;
212 gpsd_host = "localhost";
214 gprs_apn = "general.t-mobile.uk";
219 power_button_keycode = 124;
220 aux_button_keycode = 177;
221 ringtone_alarm = "file:///usr/share/sounds/yue-fso/lec1.ogg";
224 if (lua.do_file(homedir + "/config"))
226 zavai.log.error("Failed to parse " + homedir + "/config: " + lua.to_string(-1));
232 * Find a zavai script.
234 * ~/.zavai/NAME is searched first, then /usr/share/zavai/hooks/
236 * If the script is not found, NULL is returned
238 public string find_script(string name)
240 string candidate = homedir + "/" + name;
241 if (FileUtils.test(candidate, FileTest.EXISTS))
243 candidate = "/usr/share/zavai/hooks/" + name;
244 if (FileUtils.test(candidate, FileTest.EXISTS))
249 public void run_script(string command)
251 zavai.log.info("Run program: " + command);
252 string[] args = command.split(" ");
256 Environment.get_home_dir(),
259 SpawnFlags.SEARCH_PATH,
262 } catch (SpawnError e) {
263 zavai.log.error("Running " + command + ": " + e.message);
267 public int run_script_sync(string command, out string std_out, out string std_err)
270 zavai.log.info("Run program: " + command);
271 string[] args = command.split(" ");
273 bool res = Process.spawn_sync(Environment.get_home_dir(), args, null, SpawnFlags.SEARCH_PATH, null, out std_out, out std_err, out status);
274 } catch (SpawnError e) {
275 zavai.log.error("Running " + command + ": " + e.message);
281 public Config config = null;