2 * ion/mod_statusbar/main.c
4 * Copyright (c) Tuomo Valkonen 1999-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.
13 #include <sys/types.h>
18 #include <libtu/minmax.h>
19 #include <libextl/readconfig.h>
20 #include <libmainloop/exec.h>
21 #include <libmainloop/select.h>
22 #include <ioncore/saveload.h>
23 #include <ioncore/bindmaps.h>
24 #include <ioncore/global.h>
26 #include "statusbar.h"
30 #define CF_STATUSD_TIMEOUT_SEC 5
33 /*{{{ Module information */
36 #include "../version.h"
38 char mod_statusbar_ion_api_version[]=ION_API_VERSION;
47 WBindmap *mod_statusbar_statusbar_bindmap=NULL;
53 /*{{{ Statusd launch helper */
59 static bool process_pipe(int fd, ExtlFn fn,
60 bool *doneseen, bool *eagain)
68 n=read(fd, buf, BL-1);
71 if(errno==EAGAIN || errno==EINTR){
72 *eagain=(errno==EAGAIN);
75 warn_err_obj(TR("reading a pipe"));
80 return extl_call(fn, "s", "b", &buf, doneseen);
90 static bool wait_statusd_init(int outfd, int errfd, ExtlFn dh, ExtlFn eh)
93 struct timeval tv, endtime, now;
94 int nfds=maxof(outfd, errfd);
96 bool dummy, doneseen, eagain=FALSE;
97 const char *timeout_msg=TR("ion-statusd launch timeout.");
99 if(gettimeofday(&endtime, NULL)!=0){
105 endtime.tv_sec+=CF_STATUSD_TIMEOUT_SEC;
110 /* Calculate remaining time */
111 if(now.tv_sec>endtime.tv_sec){
113 }else if(now.tv_sec==endtime.tv_sec){
114 if(now.tv_usec>=endtime.tv_usec)
117 tv.tv_usec=endtime.tv_usec-now.tv_usec;
119 tv.tv_usec=USEC+endtime.tv_usec-now.tv_usec;
120 tv.tv_sec=-1+endtime.tv_sec-now.tv_sec;
121 /* Kernel lameness tuner: */
122 tv.tv_sec+=tv.tv_usec/USEC;
126 FD_SET(outfd, &rfds);
127 FD_SET(errfd, &rfds);
129 retval=select(nfds+1, &rfds, NULL, NULL, &tv);
131 if(FD_ISSET(errfd, &rfds)){
132 if(!process_pipe(errfd, eh, &dummy, &eagain))
135 if(FD_ISSET(outfd, &rfds)){
136 if(!process_pipe(outfd, dh, &doneseen, &eagain))
139 /* Read rest of errors. */
142 ok=process_pipe(errfd, eh, &dummy, &eagain);
143 }while(ok && !eagain);
151 if(gettimeofday(&now, NULL)!=0){
160 warn(TR("ion-statusd timed out."));
166 int mod_statusbar__launch_statusd(const char *cmd,
167 ExtlFn initdatahandler,
168 ExtlFn initerrhandler,
173 int outfd=-1, errfd=-1;
178 pid=mainloop_do_spawn(cmd, NULL, NULL,
179 NULL, &outfd, &errfd);
184 if(!wait_statusd_init(outfd, errfd, initdatahandler, initerrhandler))
187 if(!mainloop_register_input_fd_extlfn(outfd, datahandler))
190 if(!mainloop_register_input_fd_extlfn(errfd, errhandler))
196 mainloop_unregister_input_fd(outfd);
210 static bool is_systray(WClientWin *cwin)
212 static Atom atom__kde_net_wm_system_tray_window_for=None;
213 Atom actual_type=None;
215 unsigned long nitems;
216 unsigned long bytes_after;
221 if(extl_table_gets_s(cwin->proptab, "statusbar", &dummy)){
226 if(atom__kde_net_wm_system_tray_window_for==None){
227 atom__kde_net_wm_system_tray_window_for=XInternAtom(ioncore_g.dpy,
228 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
231 if(XGetWindowProperty(ioncore_g.dpy, cwin->win,
232 atom__kde_net_wm_system_tray_window_for, 0,
233 sizeof(Atom), False, AnyPropertyType,
234 &actual_type, &actual_format, &nitems,
235 &bytes_after, &prop)==Success){
236 if(actual_type!=None){
246 static bool clientwin_do_manage_hook(WClientWin *cwin, const WManageParams *param)
250 if(!is_systray(cwin))
253 sb=mod_statusbar_find_suitable(cwin, param);
257 return region_manage_clientwin((WRegion*)sb, cwin, param,
258 MANAGE_REDIR_PREFER_NO);
265 /*{{{ Init & deinit */
268 void mod_statusbar_deinit()
270 hook_remove(clientwin_do_manage_alt,
271 (WHookDummy*)clientwin_do_manage_hook);
273 if(mod_statusbar_statusbar_bindmap!=NULL){
274 ioncore_free_bindmap("WStatusBar", mod_statusbar_statusbar_bindmap);
275 mod_statusbar_statusbar_bindmap=NULL;
278 ioncore_unregister_regclass(&CLASSDESCR(WStatusBar));
280 mod_statusbar_unregister_exports();
284 bool mod_statusbar_init()
286 mod_statusbar_statusbar_bindmap=ioncore_alloc_bindmap("WStatusBar", NULL);
288 if(mod_statusbar_statusbar_bindmap==NULL)
291 if(!ioncore_register_regclass(&CLASSDESCR(WStatusBar),
292 (WRegionLoadCreateFn*) statusbar_load)){
293 mod_statusbar_deinit();
297 if(!mod_statusbar_register_exports()){
298 mod_statusbar_deinit();
302 hook_add(clientwin_do_manage_alt,
303 (WHookDummy*)clientwin_do_manage_hook);
305 /*ioncore_read_config("cfg_statusbar", NULL, TRUE);*/