2 * ion/mod_statusbar/ion-statusd/ion-statusd.c
4 * Copyright (c) Tuomo Valkonen 2004-2006.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
14 #include <sys/types.h>
17 #include <libtu/util.h>
18 #include <libtu/optparser.h>
19 #include <libtu/errorlog.h>
20 #include <libtu/locale.h>
21 #include <libtu/misc.h>
22 #include <libextl/readconfig.h>
23 #include <libmainloop/select.h>
24 #include <libmainloop/signal.h>
25 #include <libmainloop/defer.h>
31 #include "../../version.h"
34 static OptParserOpt ion_opts[]={
35 /*{OPT_ID('d'), "display", OPT_ARG, "host:dpy.scr",
36 DUMMY_TR("X display to use")},*/
38 {'c', "conffile", OPT_ARG, "config_file",
39 DUMMY_TR("Configuration file")},
41 {'s', "searchdir", OPT_ARG, "dir",
42 DUMMY_TR("Add directory to search path")},
44 /*{OPT_ID('s'), "session", OPT_ARG, "session_name",
45 DUMMY_TR("Name of session (affects savefiles)")},*/
47 {'h', "help", 0, NULL,
48 DUMMY_TR("Show this help")},
50 {'V', "version", 0, NULL,
51 DUMMY_TR("Show program version")},
53 {OPT_ID('a'), "about", 0, NULL,
54 DUMMY_TR("Show about text")},
56 {'q', "quiet", 0, NULL,
57 DUMMY_TR("Quiet mode")},
59 {'m', "meter", OPT_ARG, "meter_module",
60 DUMMY_TR("Load a meter module")},
66 static const char statusd_copy[]=
67 "Ion-statusd " ION_VERSION ", copyright (c) Tuomo Valkonen 2004-2005.";
70 static const char statusd_license[]=DUMMY_TR(
71 "This program is free software; you can redistribute it and/or\n"
72 "modify it under the terms of the GNU Lesser General Public\n"
73 "License as published by the Free Software Foundation; either\n"
74 "version 2.1 of the License, or (at your option) any later version.\n"
76 "This program is distributed in the hope that it will be useful,\n"
77 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
78 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
79 "Lesser General Public License for more details.\n");
82 /* new_informs=TRUE because we should always print period when
83 * initialisation is done.
85 static bool new_informs=TRUE;
86 static ExtlTab configtab;
91 printf(TR("Usage: %s [options]\n\n"), libtu_progname());
92 for(i=0; ion_opts[i].descr!=NULL; i++)
93 ion_opts[i].descr=TR(ion_opts[i].descr);
94 optparser_printhelp(OPTP_MIDLONG, ion_opts);
99 static void flush_informs()
109 static void mainloop()
113 sigemptyset(&trapset);
114 sigaddset(&trapset, SIGALRM);
115 sigaddset(&trapset, SIGCHLD);
116 mainloop_trap_signals(&trapset);
119 int kill_sig=mainloop_check_signals();
120 if(kill_sig!=0 && kill_sig!=SIGUSR1){
121 if(kill_sig==SIGTERM)
124 kill(getpid(), kill_sig);
127 mainloop_execute_deferred();
136 extern bool statusd_register_exports();
137 extern void statusd_unregister_exports();
140 static void stdout_closed(int fd, void *data)
146 int main(int argc, char*argv[])
148 const char *mod=NULL;
155 if(setlocale(LC_ALL, "")==NULL)
156 warn("setlocale() call failed.");
159 configtab=extl_table_none();
164 if(!statusd_register_exports())
167 extl_add_searchdir(EXTRABINDIR);
168 extl_add_searchdir(MODULEDIR);
169 extl_add_searchdir(ETCDIR);
170 extl_add_searchdir(SHAREDIR);
171 extl_add_searchdir(LCDIR);
172 extl_set_userdirs("ion3");
174 optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
176 extl_read_config("ioncore_luaext", NULL, TRUE);
178 while((opt=optparser_get_opt())){
181 display=optparser_get_arg();
184 extl_add_searchdir(optparser_get_arg());
187 extl_set_sessiondir(optparser_get_arg());
193 printf("%s\n", ION_VERSION);
196 printf("%s\n\n%s", statusd_copy, TR(statusd_license));
201 const char *f=optparser_get_arg();
202 if(extl_read_savefile(f, &t)){
203 extl_unref_table(configtab);
206 warn(TR("Unable to load configuration file %s"), f);
214 mod=optparser_get_arg();
215 if(strchr(mod, '/')==NULL && strchr(mod, '.')==NULL){
216 mod2=scat("statusd_", mod);
221 if(extl_read_config(mod, NULL, !quiet))
227 warn(TR("Invalid command line."));
233 if(loaded==0 && !quiet){
234 warn(TR("No meters loaded."));
245 * Inform that meter \var{name} has value \var{value}.
248 void statusd_inform(const char *name, const char *value)
250 printf("%s: %s\n", name, value);
256 * Get configuration table for module \var{name}
259 ExtlTab statusd_get_config(const char *name)
262 return extl_ref_table(configtab);
265 if(extl_table_gets_t(configtab, name, &t))
268 return extl_create_table();
274 * Get last file modification time.
277 double statusd_last_modified(const char *fname)
284 if(stat(fname, &st)!=0){
285 /*warn_err_obj(fname);*/
289 return (double)(st.st_mtime>st.st_ctime ? st.st_mtime : st.st_ctime);
294 ExtlTab statusd_getloadavg()
296 ExtlTab t=extl_create_table();
303 extl_table_sets_d(t, "1min", l[0]);
305 extl_table_sets_d(t, "5min", l[1]);
307 extl_table_sets_d(t, "15min", l[2]);