2 * ion/mod_statusbar/main.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
10 #include <sys/types.h>
15 #include <libtu/minmax.h>
16 #include <libextl/readconfig.h>
17 #include <libmainloop/exec.h>
18 #include <libmainloop/select.h>
19 #include <ioncore/saveload.h>
20 #include <ioncore/bindmaps.h>
21 #include <ioncore/global.h>
23 #include "statusbar.h"
27 #define CF_STATUSD_TIMEOUT_SEC 5
30 /*{{{ Module information */
33 #include "../version.h"
35 char mod_statusbar_ion_api_version[]=ION_API_VERSION;
44 WBindmap *mod_statusbar_statusbar_bindmap=NULL;
50 /*{{{ Statusd launch helper */
56 static bool process_pipe(int fd, ExtlFn fn,
57 bool *doneseen, bool *eagain)
65 n=read(fd, buf, BL-1);
68 if(errno==EAGAIN || errno==EINTR){
69 *eagain=(errno==EAGAIN);
72 warn_err_obj(TR("reading a pipe"));
77 return extl_call(fn, "s", "b", &buf, doneseen);
87 static bool wait_statusd_init(int outfd, int errfd, ExtlFn dh, ExtlFn eh)
90 struct timeval tv, endtime, now;
91 int nfds=maxof(outfd, errfd);
93 bool dummy, doneseen, eagain=FALSE;
95 if(gettimeofday(&endtime, NULL)!=0){
101 endtime.tv_sec+=CF_STATUSD_TIMEOUT_SEC;
106 /* Calculate remaining time */
107 if(now.tv_sec>endtime.tv_sec){
109 }else if(now.tv_sec==endtime.tv_sec){
110 if(now.tv_usec>=endtime.tv_usec)
113 tv.tv_usec=endtime.tv_usec-now.tv_usec;
115 tv.tv_usec=USEC+endtime.tv_usec-now.tv_usec;
116 tv.tv_sec=-1+endtime.tv_sec-now.tv_sec;
117 /* Kernel lameness tuner: */
118 tv.tv_sec+=tv.tv_usec/USEC;
122 FD_SET(outfd, &rfds);
123 FD_SET(errfd, &rfds);
125 retval=select(nfds+1, &rfds, NULL, NULL, &tv);
127 if(FD_ISSET(errfd, &rfds)){
128 if(!process_pipe(errfd, eh, &dummy, &eagain))
131 if(FD_ISSET(outfd, &rfds)){
132 if(!process_pipe(outfd, dh, &doneseen, &eagain))
135 /* Read rest of errors. */
138 ok=process_pipe(errfd, eh, &dummy, &eagain);
139 }while(ok && !eagain);
147 if(gettimeofday(&now, NULL)!=0){
156 warn(TR("ion-statusd timed out."));
162 int mod_statusbar__launch_statusd(const char *cmd,
163 ExtlFn initdatahandler,
164 ExtlFn initerrhandler,
169 int outfd=-1, errfd=-1;
174 pid=mainloop_do_spawn(cmd, NULL, NULL,
175 NULL, &outfd, &errfd);
180 if(!wait_statusd_init(outfd, errfd, initdatahandler, initerrhandler))
183 if(!mainloop_register_input_fd_extlfn(outfd, datahandler))
186 if(!mainloop_register_input_fd_extlfn(errfd, errhandler))
192 mainloop_unregister_input_fd(outfd);
206 static bool is_systray(WClientWin *cwin)
208 static Atom atom__kde_net_wm_system_tray_window_for=None;
209 Atom actual_type=None;
211 unsigned long nitems;
212 unsigned long bytes_after;
217 if(extl_table_gets_s(cwin->proptab, "statusbar", &dummy)){
222 if(atom__kde_net_wm_system_tray_window_for==None){
223 atom__kde_net_wm_system_tray_window_for=XInternAtom(ioncore_g.dpy,
224 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
227 if(XGetWindowProperty(ioncore_g.dpy, cwin->win,
228 atom__kde_net_wm_system_tray_window_for, 0,
229 sizeof(Atom), False, AnyPropertyType,
230 &actual_type, &actual_format, &nitems,
231 &bytes_after, &prop)==Success){
232 if(actual_type!=None){
242 static bool clientwin_do_manage_hook(WClientWin *cwin, const WManageParams *param)
246 if(!is_systray(cwin))
249 sb=mod_statusbar_find_suitable(cwin, param);
253 return region_manage_clientwin((WRegion*)sb, cwin, param,
254 MANAGE_PRIORITY_NONE);
261 /*{{{ Init & deinit */
264 void mod_statusbar_deinit()
266 hook_remove(clientwin_do_manage_alt,
267 (WHookDummy*)clientwin_do_manage_hook);
269 if(mod_statusbar_statusbar_bindmap!=NULL){
270 ioncore_free_bindmap("WStatusBar", mod_statusbar_statusbar_bindmap);
271 mod_statusbar_statusbar_bindmap=NULL;
274 ioncore_unregister_regclass(&CLASSDESCR(WStatusBar));
276 mod_statusbar_unregister_exports();
280 bool mod_statusbar_init()
282 mod_statusbar_statusbar_bindmap=ioncore_alloc_bindmap("WStatusBar", NULL);
284 if(mod_statusbar_statusbar_bindmap==NULL)
287 if(!ioncore_register_regclass(&CLASSDESCR(WStatusBar),
288 (WRegionLoadCreateFn*) statusbar_load)){
289 mod_statusbar_deinit();
293 if(!mod_statusbar_register_exports()){
294 mod_statusbar_deinit();
298 hook_add(clientwin_do_manage_alt,
299 (WHookDummy*)clientwin_do_manage_hook);
301 /*ioncore_read_config("cfg_statusbar", NULL, TRUE);*/