2 * ion/mod_statusbar/main.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
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;
98 if(gettimeofday(&endtime, NULL)!=0){
104 endtime.tv_sec+=CF_STATUSD_TIMEOUT_SEC;
109 /* Calculate remaining time */
110 if(now.tv_sec>endtime.tv_sec){
112 }else if(now.tv_sec==endtime.tv_sec){
113 if(now.tv_usec>=endtime.tv_usec)
116 tv.tv_usec=endtime.tv_usec-now.tv_usec;
118 tv.tv_usec=USEC+endtime.tv_usec-now.tv_usec;
119 tv.tv_sec=-1+endtime.tv_sec-now.tv_sec;
120 /* Kernel lameness tuner: */
121 tv.tv_sec+=tv.tv_usec/USEC;
125 FD_SET(outfd, &rfds);
126 FD_SET(errfd, &rfds);
128 retval=select(nfds+1, &rfds, NULL, NULL, &tv);
130 if(FD_ISSET(errfd, &rfds)){
131 if(!process_pipe(errfd, eh, &dummy, &eagain))
134 if(FD_ISSET(outfd, &rfds)){
135 if(!process_pipe(outfd, dh, &doneseen, &eagain))
138 /* Read rest of errors. */
141 ok=process_pipe(errfd, eh, &dummy, &eagain);
142 }while(ok && !eagain);
150 if(gettimeofday(&now, NULL)!=0){
159 warn(TR("ion-statusd timed out."));
165 int mod_statusbar__launch_statusd(const char *cmd,
166 ExtlFn initdatahandler,
167 ExtlFn initerrhandler,
172 int outfd=-1, errfd=-1;
177 pid=mainloop_do_spawn(cmd, NULL, NULL,
178 NULL, &outfd, &errfd);
183 if(!wait_statusd_init(outfd, errfd, initdatahandler, initerrhandler))
186 if(!mainloop_register_input_fd_extlfn(outfd, datahandler))
189 if(!mainloop_register_input_fd_extlfn(errfd, errhandler))
195 mainloop_unregister_input_fd(outfd);
209 static bool is_systray(WClientWin *cwin)
211 static Atom atom__kde_net_wm_system_tray_window_for=None;
212 Atom actual_type=None;
214 unsigned long nitems;
215 unsigned long bytes_after;
220 if(extl_table_gets_s(cwin->proptab, "statusbar", &dummy)){
225 if(atom__kde_net_wm_system_tray_window_for==None){
226 atom__kde_net_wm_system_tray_window_for=XInternAtom(ioncore_g.dpy,
227 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
230 if(XGetWindowProperty(ioncore_g.dpy, cwin->win,
231 atom__kde_net_wm_system_tray_window_for, 0,
232 sizeof(Atom), False, AnyPropertyType,
233 &actual_type, &actual_format, &nitems,
234 &bytes_after, &prop)==Success){
235 if(actual_type!=None){
245 static bool clientwin_do_manage_hook(WClientWin *cwin, const WManageParams *param)
249 if(!is_systray(cwin))
252 sb=mod_statusbar_find_suitable(cwin, param);
256 return region_manage_clientwin((WRegion*)sb, cwin, param,
257 MANAGE_REDIR_PREFER_NO);
264 /*{{{ Init & deinit */
267 void mod_statusbar_deinit()
269 hook_remove(clientwin_do_manage_alt,
270 (WHookDummy*)clientwin_do_manage_hook);
272 if(mod_statusbar_statusbar_bindmap!=NULL){
273 ioncore_free_bindmap("WStatusBar", mod_statusbar_statusbar_bindmap);
274 mod_statusbar_statusbar_bindmap=NULL;
277 ioncore_unregister_regclass(&CLASSDESCR(WStatusBar));
279 mod_statusbar_unregister_exports();
283 bool mod_statusbar_init()
285 mod_statusbar_statusbar_bindmap=ioncore_alloc_bindmap("WStatusBar", NULL);
287 if(mod_statusbar_statusbar_bindmap==NULL)
290 if(!ioncore_register_regclass(&CLASSDESCR(WStatusBar),
291 (WRegionLoadCreateFn*) statusbar_load)){
292 mod_statusbar_deinit();
296 if(!mod_statusbar_register_exports()){
297 mod_statusbar_deinit();
301 hook_add(clientwin_do_manage_alt,
302 (WHookDummy*)clientwin_do_manage_hook);
304 /*ioncore_read_config("cfg_statusbar", NULL, TRUE);*/