wmpager: drop tooltip suport
[dockapps.git] / wmweather+ / moon.c
blob295addad83c8dfd051082c72eed43f3c08818039
1 #include "config.h"
3 /* Copyright (C) 2002 Brad Jorsch <anomie@users.sourceforge.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /* One-line algorithm from http://www.moonstick.com/moon_phase_emergency.htm
21 * It's a bit rough, but it works well enough */
23 #if TM_IN_SYS_TIME
24 # if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 # else
28 # if HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 # else
31 # include <time.h>
32 # endif
33 # endif
34 #else
35 #include <time.h>
36 #endif
37 #include <math.h>
39 #include <X11/Xlib.h>
40 #include <X11/xpm.h>
42 #include "convert.h"
44 #include "wmgeneral/wmgeneral-x11.h"
46 static double fpart(double t){
47 return t-trunc(t);
50 double calc_moon(int month, int day, int year, int hm){
51 time_t t=time(NULL);
52 struct tm *tm;
53 double p;
55 tm=gmtime(&t);
56 tm->tm_hour=hm/100;
57 tm->tm_min=hm%100;
58 tm->tm_sec=0;
59 tm->tm_mon=month-1;
60 tm->tm_mday=day;
61 tm->tm_year=year;
62 t=mkgmtime(tm);
64 /* This next line is the algorithm. */
65 p=fpart(((t/86400.0-11323.0)*850.0+5130.5769)/25101.0);
66 if(p>.5) return -.5+cos(2*PI*p)/2;
67 return .5-cos(2*PI*p)/2;
71 #define darkside 0.19921875
72 #define lightside (1-0.19921875)
73 #define maxwidth 17
74 static int widths[]={ 7, 11, 13, 15, 15, 17, 17, 17, 17, 17, 17, 17, 15, 15, 13, 11, 7, -1 };
75 extern int screen;
76 extern XpmIcon wmgen;
77 extern GC NormalGC;
79 /* Duplicates quite a bit of code from combineWithOpacity for speed */
80 void copySunMoon(int x, int y, double percent){
81 XImage *pix;
82 unsigned int w, h, bar;
83 int foo;
84 Window baz;
85 int rmask, gmask, bmask;
86 unsigned long spixel;
87 int xx, terminator, oflag;
88 int flag;
89 double frac;
91 if(isnan(percent)){
92 copyPixmapArea(164, 64, 26, 25, x, y);
93 return;
96 XGetGeometry(display, wmgen.pixmap, &baz, &foo, &foo, &w, &h, &bar, &bar);
97 pix=XGetImage(display, wmgen.pixmap, 0, 0, w, h, AllPlanes, ZPixmap);
99 if (pix->depth == DefaultDepth(display, screen)) {{
100 Visual *visual=DefaultVisual(display, screen);
101 rmask = visual->red_mask;
102 gmask = visual->green_mask;
103 bmask = visual->blue_mask;
104 }} else {
105 rmask = pix->red_mask;
106 gmask = pix->green_mask;
107 bmask = pix->blue_mask;
110 x+=4; y+=4;
111 flag=(percent<0);
112 if(flag) percent=-percent;
113 for(h=0; widths[h]>0; h++){
114 xx=(maxwidth-widths[h])>>1;
115 if(flag){
116 oflag=1;
117 terminator=widths[h]*percent;
118 } else {
119 oflag=0;
120 terminator=widths[h]-widths[h]*percent;
122 frac=lightside*fpart(widths[h]*percent)+darkside;
123 for(w=0; w<widths[h]; w++){
124 spixel=XGetPixel(pix, 168+xx+w, 93+h);
125 if(w==terminator){
126 oflag=!oflag;
127 XPutPixel(pix, x+xx+w, y+h,
128 (((unsigned long)((spixel&rmask)*frac))&rmask) |
129 (((unsigned long)((spixel&gmask)*frac))&gmask) |
130 (((unsigned long)((spixel&bmask)*frac))&bmask));
131 } else if(oflag){
132 XPutPixel(pix, x+xx+w, y+h, spixel);
133 } else {
134 XPutPixel(pix, x+xx+w, y+h,
135 (((unsigned long)((spixel&rmask)*darkside))&rmask) |
136 (((unsigned long)((spixel&gmask)*darkside))&gmask) |
137 (((unsigned long)((spixel&bmask)*darkside))&bmask));
142 XPutImage(display, wmgen.pixmap, NormalGC, pix, 0, 0, 0, 0, pix->width, pix->height);
144 XDestroyImage(pix);
147 #if 0
148 void copySunMoon(int x, int y, double percent){
149 int w, h;
150 int xx;
151 int frac;
152 int flag;
154 if(isnan(percent)){
155 copyPixmapArea(164, 64, 26, 25, x, y);
156 return;
159 combineWithOpacity(164, 89, 26, 25, x, y, 51);
160 x+=4; y+=4;
161 flag=(percent<0);
162 if(flag) percent=-percent;
163 for(h=0; h<=8; h++){
164 w=widths[h]*percent;
165 frac=(widths[h]*percent-w)*100;
166 if(flag) xx=(17-widths[h])/2;
167 else xx=(17+widths[h])/2-w;
168 copyPixmapArea(141+xx, 93+h, w, 1, x+xx, y+h);
169 copyPixmapArea(141+xx, 109-h, w, 1, x+xx, y+16-h);
170 if(flag){
171 combineWithOpacity(141+xx+w, 93+h, 1, 1, x+xx+w, y+h, frac);
172 combineWithOpacity(141+xx+w, 109-h, 1, 1, x+xx+w, y+16-h, frac);
173 } else {
174 combineWithOpacity(141+xx-1, 93+h, 1, 1, x+xx-1, y+h, frac);
175 combineWithOpacity(141+xx-1, 109-h, 1, 1, x+xx-1, y+16-h, frac);
179 #endif