wmclockmon: update change-log
[dockapps.git] / wmfemon / wmfemon.c
blob1c0ada599c3e85a0e46e5c3c687a85a8b6325633
1 //WindowMaker DVB frontend monitor dock applet version 1. MaxCompress
2 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <getopt.h>
8 #include <unistd.h>
9 #include <time.h>
10 #include <stdint.h>
11 #include <unistd.h>
13 #include <X11/X.h>
14 #include <X11/Xlib.h>
15 #include <X11/Xutil.h>
16 #include <X11/extensions/shape.h>
17 #include <X11/xpm.h>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
22 #include <linux/dvb/frontend.h>
23 #define FRONTENDDEVICE "/dev/dvb/adapter%d/frontend%d"
25 static Atom delete_win;
26 Font f1,f2;
27 Window W1,Wicon,W2;
28 Window W2=0;
29 Display *XD;
30 GC xgc;
31 unsigned int d[7][4]={{22, 2,19,9},{2, 2,19,9},{2,12,19,9},
32 {2,22,19,9},{22,12,19,9},{2,32,19,9},{32,32,42,9}};
34 // #define DEBUG
36 unsigned long getcolor(char *color_name)
38 /* getcolor("darkblue") veya getcolor("rgb:52/55/52") olabilir. */
39 XColor color;
41 if (!XParseColor(XD, DefaultColormap(XD, DefaultScreen(XD)), color_name, &color))
42 fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
44 if (!XAllocColor(XD, DefaultColormap(XD, DefaultScreen(XD)), &color)) {
45 fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
46 return BlackPixel(XD, DefaultScreen(XD)); }
48 return color.pixel;
51 void xmon(int fefd)
53 fe_status_t status;
54 uint16_t snr, signal, level;
55 //uint32_t ber, uncorrected_blocks;
56 struct dvb_frontend_parameters dfp;
57 XWindowAttributes xwattr;
58 char a[250];
59 XTextItem xti;
60 int freq;
62 ioctl(fefd, FE_READ_STATUS, &status);
63 ioctl(fefd, FE_READ_SIGNAL_STRENGTH, &signal);
64 ioctl(fefd, FE_READ_SNR, &snr);
65 //ioctl(fefd, FE_READ_BER, &ber);
66 //ioctl(fefd, FE_READ_UNCORRECTED_BLOCKS, &uncorrected_blocks);
67 ioctl(fefd, FE_GET_FRONTEND, &dfp);
68 if (dfp.frequency+10600000 >= 11700000) freq=dfp.frequency+10600000;
69 else freq=dfp.frequency+ 9750000;
70 XGetWindowAttributes(XD,Wicon,&xwattr);
72 level=(signal)*(xwattr.width-1)/(0xffff);
73 XSetForeground(XD,xgc,getcolor("gray70"));
74 XFillRectangle(XD,Wicon,xgc,0,0,xwattr.width,xwattr.height);
75 if (status & FE_HAS_LOCK) XSetForeground(XD,xgc,getcolor("rgb:ff/00/00"));
76 else XSetForeground(XD,xgc,getcolor("wheat4"));
77 XFillRectangle(XD,Wicon,xgc,0,15,level,20);
78 level=(snr)*(xwattr.width-1)/(0xffff);
79 XFillRectangle(XD,Wicon,xgc,0,38,level,20);
81 XSetForeground(XD,xgc,getcolor("white"));
82 XDrawRectangle(XD,Wicon,xgc,0,14,59,21);
83 XDrawRectangle(XD,Wicon,xgc,0,37,59,21);
84 XSetForeground(XD,xgc,getcolor("black"));
85 XDrawLine(XD,Wicon,xgc,0,14,59,14); XDrawLine(XD,Wicon,xgc,0,14,0,35);
86 XDrawLine(XD,Wicon,xgc,0,37,59,37); XDrawLine(XD,Wicon,xgc,0,37,0,58);
88 level=(signal)*100/(0xffff);
89 sprintf(a,"sig %%%d",level);
90 XSetForeground(XD,xgc,getcolor("black"));
91 /* ascent font un dikey uzunlugu imis. */
92 xti.chars=a;
93 xti.nchars=strlen(a);
94 xti.delta=0;
95 xti.font=f2;
96 XDrawText(XD,Wicon,xgc,2,29,&xti,1);
97 level=(snr)*100/(0xffff);
98 sprintf(a,"snr %%%d",level);
99 xti.chars=a;
100 xti.nchars=strlen(a);
101 xti.delta=0;
102 xti.font=f2;
103 XDrawText(XD,Wicon,xgc,2,52,&xti,1);
104 //sprintf(a,"stat %02x|sig %04x|snr %04x|ber %08x|unc %08x|if %08d|sym %d|f %d", status, signal, snr, ber, uncorrected_blocks, dfp.frequency,dfp.u.qpsk.symbol_rate,freq);
105 sprintf(a,"f %d", freq);
106 xti.chars=a;
107 xti.nchars=strlen(a);
108 xti.delta=0;
109 xti.font=f1;
110 XDrawText(XD,Wicon,xgc,1,11,&xti,1);
111 XFlush(XD); /*Flush etmeden ekrana bok gibi yaziyor.*/
114 void xmon_W2(int fefd)
116 fe_status_t status;
117 uint16_t snr, signal, level;
118 uint32_t ber, uncorrected_blocks;
119 struct dvb_frontend_parameters dfp;
120 XWindowAttributes xwattr;
121 char a[250];
122 XTextItem xti;
123 int freq;
125 ioctl(fefd, FE_READ_STATUS, &status);
126 ioctl(fefd, FE_READ_SIGNAL_STRENGTH, &signal);
127 ioctl(fefd, FE_READ_SNR, &snr);
128 ioctl(fefd, FE_READ_BER, &ber);
129 ioctl(fefd, FE_READ_UNCORRECTED_BLOCKS, &uncorrected_blocks);
130 ioctl(fefd, FE_GET_FRONTEND, &dfp);
131 if (dfp.frequency+10600000 >= 11700000) freq=dfp.frequency+10600000;
132 else freq=dfp.frequency+ 9750000;
133 XGetWindowAttributes(XD,W2,&xwattr);
135 level=(signal)*(xwattr.width-1)/(0xffff);
136 XSetForeground(XD,xgc,getcolor("gray70"));
137 XFillRectangle(XD,W2,xgc,0,0,xwattr.width,xwattr.height);
138 if (status & FE_HAS_LOCK) XSetForeground(XD,xgc,getcolor("rgb:ff/00/00"));
139 else XSetForeground(XD,xgc,getcolor("wheat4"));
140 XFillRectangle(XD,W2,xgc,0,15,level,20);
141 level=(snr)*(xwattr.width-1)/(0xffff);
142 XFillRectangle(XD,W2,xgc,0,38,level,20);
144 XSetForeground(XD,xgc,getcolor("white"));
145 XDrawRectangle(XD,W2,xgc,0,14,xwattr.width-1,21);
146 XDrawRectangle(XD,W2,xgc,0,37,xwattr.width-1,21);
147 XSetForeground(XD,xgc,getcolor("black"));
148 XDrawLine(XD,W2,xgc,0,14,xwattr.width-1,14); XDrawLine(XD,Wicon,xgc,0,14,0,35);
149 XDrawLine(XD,W2,xgc,0,37,xwattr.width-1,37); XDrawLine(XD,Wicon,xgc,0,37,0,58);
151 level=(signal)*100/(0xffff);
152 sprintf(a,"sig %%%d",level);
153 XSetForeground(XD,xgc,getcolor("black"));
154 /* ascent font un dikey uzunlugu imis. */
155 xti.chars=a;
156 xti.nchars=strlen(a);
157 xti.delta=0;
158 xti.font=f2;
159 XDrawText(XD,W2,xgc,2,29,&xti,1);
160 level=(snr)*100/(0xffff);
161 sprintf(a,"snr %%%d",level);
162 xti.chars=a;
163 xti.nchars=strlen(a);
164 xti.delta=0;
165 xti.font=f2;
166 XDrawText(XD,W2,xgc,2,52,&xti,1);
167 sprintf(a,"stat %02x|sig %04x|snr %04x|ber %08x|unc %08x|if %08d|sym %d|f %d", status, signal, snr, ber, uncorrected_blocks, dfp.frequency,dfp.u.qpsk.symbol_rate,freq);
168 //sprintf(a,"f %d", freq);
169 xti.chars=a;
170 xti.nchars=strlen(a);
171 xti.delta=0;
172 xti.font=f1;
173 XDrawText(XD,W2,xgc,1,11,&xti,1);
174 XFlush(XD); /*Flush etmeden ekrana bok gibi yaziyor.*/
178 int main(int argc, char **argv)
180 XClassHint classhint;
181 XWMHints wmhints;
182 XGCValues gcval;
183 unsigned int adapter = 0, frontend = 0;
184 char fedev[128];
185 int fefd;
186 int z;
187 struct dvb_frontend_info fe_info;
188 int bekle=0, bekle2=0;
189 /* Komut satiri */
191 fprintf(stderr,"Option Sayisi=%d\n",argc);
192 for (sayac2=2;sayac2<=argc;sayac2++)
194 fprintf(stderr,"Option %d=%s\n",sayac2,argv[sayac2-1]);
198 while ((opt=getopt(argc, argv, "h:d:"))!=-1) {
199 switch (opt) {
200 case 'h':
201 fprintf(stderr, "Help: [-h] [-d device] [-t aralik]\n");
202 break;
203 case 'd':
204 dev=strdup(optarg);
205 break;
206 default:
207 fprintf(stderr, "\nUsage: %s %s", argv[0], USAGE);
208 exit(1);
212 XD = XOpenDisplay("");
213 if (!XD) { fprintf(stderr, "Default Display acilamadi.\n"); return 1; }
214 W1 = XCreateSimpleWindow(XD, DefaultRootWindow(XD),0, 0, 64, 64, 1, 50, getcolor("yellow"));
215 Wicon = XCreateSimpleWindow(XD, W1, 0, 0, 60, 60, 1, 50, getcolor("darkgreen"));
217 wmhints.initial_state = WithdrawnState;
218 wmhints.icon_window = Wicon;
219 wmhints.icon_x = 64;
220 wmhints.icon_y = 64;
221 wmhints.window_group = W1;
222 wmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
223 XSetWMHints(XD, W1, &wmhints);
225 XMapWindow(XD, W1);
227 classhint.res_name = "wmfemon";
228 classhint.res_class = "wmfemon";
229 XSetClassHint(XD, W1, &classhint);
230 XSelectInput(XD, W1, ExposureMask | ButtonPressMask | ButtonReleaseMask | StructureNotifyMask | PointerMotionMask);
231 XSelectInput(XD, Wicon, ExposureMask | ButtonPressMask | ButtonReleaseMask | StructureNotifyMask | PointerMotionMask);
232 XStoreName(XD, W1, "wmfemon");
233 XSetIconName(XD, W1, "wmfemon");
235 gcval.foreground = 50;
236 gcval.background = 150;
237 gcval.graphics_exposures = False;
238 gcval.font = XLoadFont(XD,"-*-fixed-medium-r-normal-*-10-*-*-*-*-*-*-1");
239 xgc = XCreateGC(XD, W1, GCForeground | GCBackground | GCGraphicsExposures | GCFont, &gcval);
241 //f1=XLoadFont(XD,"-*-fixed-medium-r-normal-*-10-*-*-*-*-*-*-1");
242 f1=XLoadFont(XD,"-*-clean-medium-r-*-*-12-*-*-*-*-*-*-*");
243 f2=XLoadFont(XD,"-*-helvetica-*-r-*-*-12-*-*-*-*-*-*-*");
244 snprintf(fedev, sizeof(fedev), FRONTENDDEVICE, adapter, frontend);
245 printf("Device: '%s'\n", fedev);
246 if ((fefd = open(fedev, O_RDONLY | O_NONBLOCK)) < 0) { perror("opening frontend failed"); return 0;}
247 z = ioctl(fefd, FE_GET_INFO, &fe_info);
248 if (z < 0) { perror("ERR ioctl FE_GET_INFO:"); close(fefd); return 0;}
250 /* main loop */
251 while (1)
253 XEvent event;
254 while (XPending(XD))
256 XNextEvent(XD, &event);
257 switch (event.type)
259 case Expose:
260 /* update */
261 fprintf(stderr,"Expose (Update) event geldi.\n");
262 fprintf(stderr,"xexpose.window=%ld\n",event.xexpose.window);
263 xmon(fefd);
264 break;
265 case MotionNotify:
266 #ifdef DEBUG
267 fprintf(stderr,"xmotion x=%d,y=%d\n",event.xmotion.x,event.xmotion.y);
268 fprintf(stderr,"xmotion window=%ld\n",event.xmotion.window);
269 fprintf(stderr,"W1 window=%ld\n",W1);
270 fprintf(stderr,"Wicon window=%ld\n",Wicon);
271 fprintf(stderr,"-----------------------\n");
272 #endif
273 break;
274 case UnmapNotify:
275 fprintf(stderr,"UnmapNotify geldi.\n");
276 break;
277 case DestroyNotify:
278 fprintf(stderr,"DestroyNotify geldi.\n");
279 XCloseDisplay(XD);
280 exit(0);
281 break;
282 case ClientMessage:
283 //Kapat tusu icin amma yarak isler lazim ulan. wmclockmon dockapp.c'den alinti.
284 //printf("ClientMessage\n");fflush(stdout);
285 if (event.xclient.data.l[0] == delete_win) {
286 if (event.xclient.window==W2)
287 { XDestroyWindow(XD,W2); W2=0;
290 break;
291 case ButtonPress:
292 //fprintf(stderr,"x=%d,y=%d\n",event.xbutton.x,event.xbutton.y);
293 break;
294 case ButtonRelease:
295 fprintf(stderr,"x=%d,y=%d\n",event.xbutton.x,event.xbutton.y);
296 if (W2==0)
297 { W2 = XCreateSimpleWindow(XD, DefaultRootWindow(XD),100,100,600,60,1,50,getcolor("blue"));
298 if (!W2) { perror("Error: W2"); }
299 delete_win = XInternAtom(XD, "WM_DELETE_WINDOW", False); //DIKKAT: Kapatma tusunun cikmasi icin bu yarak gerekiyor.
300 XSetWMProtocols (XD, W2, &delete_win, 1);
301 XStoreName(XD, W2, "DVB Frontend Monitor for WindowMaker");
302 XSetIconName(XD, W2, "DVB Signal");
303 XMapWindow(XD, W2);
304 } else
305 { XDestroyWindow(XD,W2); W2=0;
307 break;
310 if (bekle == 0) xmon(fefd);
311 bekle++;
312 bekle %= 10; // 10 modulu. W1 1000 ms.
313 if (W2)
314 { if (bekle2 == 0) xmon_W2(fefd);
315 bekle2++;
316 bekle2 %= 3; // 3 modulu. W2 biraz daha hizli gostersin. 300 ms.
319 usleep(100000L);
320 } //while
321 return 0;