wmmoonclock: Bump to version 1.29.
[dockapps.git] / wmglobe / src / wmglobe.c
blob13fc49f7794055b995cc3c572be031c5fb792f26
1 /* WMGlobe 0.5 - All the Earth on a WMaker Icon
2 * copyright (C) 1998,99 Jerome Dumonteil <jerome.dumonteil@capway.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 ***************************************************************************/
19 * I used many functions of wmgeneral.c ("openXwindow")
20 * for the main function of wmglobe.c
21 * wmgeneral.c was taken from wmaker applet wmtune-1.0 :
22 * Author: Martijn Pieterse (pieterse@xs4all.nl)
24 * wmglobe.c uses functions of : Xglobe, Xearth, wmgeneral, wmaker/wrlib
25 ***************************************************************************/
27 #include "wmglobe.h"
29 #include "cadre0.xbm"
30 #include "cadre1.xbm"
31 #include "cadre2.xbm"
33 #ifdef DEFMAPOK
34 #include "defmap.xpm"
35 #include "defnimap.xpm"
36 #endif
38 /***************************************************************************/
40 int main(int argc, char *argv[])
42 unsigned int borderwidth = 1;
43 XClassHint classHint;
44 char *wname = argv[0];
45 XTextProperty name;
47 XGCValues gcv;
48 unsigned long gcm;
49 XWindowAttributes attributes;
50 XColor color;
52 RContext *ctx;
54 XSizeHints mysizehints;
55 XWMHints mywmhints;
56 Pixel back_pix, fore_pix;
57 char Geometry[256];
59 char *rond_bits;
61 int dummy = 0;
62 int ok, redoaction, wait_release, move_lat_flag;
63 int xx, yy;
65 /** initialisation *********************/
67 xx = 0;
68 yy = 0;
69 ok = FALSE;
70 move_lat_flag = FALSE;
71 redoaction = 0;
72 wait_release = 0;
74 setlocale(LC_TIME, "");
76 #ifdef DEBUG
77 fprintf(stdout, "%s\n", setlocale(LC_TIME, ""));
78 #endif
80 set_defaults();
82 cmdline(argc, argv);
84 switch (typecadre) {
85 case 1:
86 rond_bits = cadre1_bits;
87 break;
88 case 2:
89 rond_bits = cadre2_bits;
90 break;
91 default:
92 rond_bits = cadre0_bits;
94 if (p_type == PTRANDOM) {
95 dlat = 0;
96 dlong = 0;
98 initmyconvert();
100 tdelay.tv_sec = (int) floor(delay);
101 tdelay.tv_usec = (int) ((delay - tdelay.tv_sec) * 1000000);
102 aml = (int) floor(ambient_light * 256);
104 /****************************************************************************/
106 if (!(dpy = XOpenDisplay(dpy_name))) {
107 fprintf(stderr, "%s: can't open display \"%s\"\n",
108 wname, XDisplayName(dpy_name));
109 exit(1);
111 ctx = myRCreateContext(dpy, DefaultScreen(dpy), NULL);
113 if (ctx->attribs->use_shared_memory) {
114 #ifdef DEBUG
115 fprintf(stdout, "enleve les flags use_shared_memory\n");
116 #endif
117 ctx->attribs->flags ^= RC_UseSharedMemory;
118 ctx->attribs->use_shared_memory = FALSE;
119 ctx->flags.use_shared_pixmap = 0;
121 #ifdef DEBUG
122 fprintf(stdout, "depth %d\n", ctx->depth);
123 fflush(stdout);
124 #endif
127 * loading maps .............
131 if (dayfile != NULL) {
132 map = RLoadImage(ctx, dayfile, 0);
133 if (!map) {
134 fprintf(stdout, "pb map ! file not found ?\n");
135 exit(1);
137 } else {
138 #ifdef DEFMAPOK
139 map = RGetImageFromXPMData(ctx, defmap_xpm);
140 if (!map) {
141 fprintf(stdout, "pb def map ! file not found ?\n");
142 exit(1);
144 nightfile = NULL;
145 use_nightmap = TRUE;
147 #else
148 fprintf(stdout, "need a map !\n");
149 exit(1);
151 #endif
153 if (use_nightmap) {
154 if (nightfile != NULL) {
155 mapnight = RLoadImage(ctx, nightfile, 0);
156 if (!mapnight) {
157 fprintf(stdout, "pb map night! file not found ?\n");
158 exit(1);
160 } else {
161 #ifdef DEFMAPOK
162 mapnight = RGetImageFromXPMData(ctx, defnimap_xpm);
163 if (!mapnight) {
164 fprintf(stdout, "pb def map night ! file not found ?\n");
165 exit(1);
168 #else
169 /* not very useful... */
170 use_nightmap = FALSE;
172 #endif
174 use_nmap_ini = use_nightmap;
175 if (!oknimap)
176 use_nightmap = FALSE;
177 /* some other init ..................................... */
178 ratiox = (double) map->width / (2 * PI);
179 ratioy = (double) map->height / PI;
180 mratiox = (int) floor(ratiox * 256);
181 mratioy = (int) floor(ratioy * 256);
182 loadxpm(ctx->drawable);
184 small = RCreateImage(DIAMETRE, DIAMETRE, 1);
186 calcDistance();
187 /*...................................................... */
190 * first rendering of the earth
192 recalc(0);
193 do_something = FALSE;
196 /*************************************************************************
197 * well, here the problems begin : this code is a merge from wmgeneral and
198 * some stuff of wmaker, should be rewritten ...
199 ************************************************************************/
201 /* wmg */
203 XGetWindowAttributes(dpy, ctx->drawable, &attributes);
205 if (!RConvertImage(ctx, small, &pix)) {
206 fprintf(stdout, "error small->&pix\n");
207 puts(RMessageForError(RErrorCode));
208 exit(1);
210 wmg.pixmap = pix;
211 wmg.mask = pix;
213 mysizehints.flags = USSize | USPosition;
214 mysizehints.x = 0;
215 mysizehints.y = 0;
217 color.pixel = 0;
218 if (!XParseColor(dpy, attributes.colormap, "white", &color)) {
219 fprintf(stdout, "wmglobe: can't parse white\n");
220 } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
221 fprintf(stdout, "wmglobe: can't allocate white\n");
223 back_pix = color.pixel;
225 XGetWindowAttributes(dpy, ctx->drawable, &attributes);
227 color.pixel = 0;
228 if (!XParseColor(dpy, attributes.colormap, "black", &color)) {
229 fprintf(stdout, "wmglobe: can't parse black\n");
230 } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
231 fprintf(stdout, "wmglobe: can't allocate black\n");
233 fore_pix = color.pixel;
236 XWMGeometry(dpy, ctx->screen_number, Geometry, NULL, borderwidth, &mysizehints,
237 &mysizehints.x, &mysizehints.y, &mysizehints.width, &mysizehints.height, &dummy);
238 mysizehints.width = DIAMETRE;
239 mysizehints.height = DIAMETRE;
241 win = XCreateSimpleWindow(dpy, ctx->drawable, mysizehints.x, mysizehints.y,
242 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
244 iconwin = XCreateSimpleWindow(dpy, win, mysizehints.x, mysizehints.y,
245 mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
247 /* Activate hints */
248 XSetWMNormalHints(dpy, win, &mysizehints);
249 classHint.res_name = wname;
250 classHint.res_class = wname;
251 XSetClassHint(dpy, win, &classHint);
253 XSelectInput(dpy, win, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
254 XSelectInput(dpy, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask);
256 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
257 fprintf(stdout, "%s: can't allocate window name\n", wname);
258 exit(1);
260 XSetWMName(dpy, win, &name);
262 /* Create GC for drawing */
264 gcm = GCForeground | GCBackground | GCGraphicsExposures;
265 gcv.foreground = fore_pix;
266 gcv.background = back_pix;
267 gcv.graphics_exposures = 0;
268 NormalGC = XCreateGC(dpy, ctx->drawable, gcm, &gcv);
270 /* ONLYSHAPE ON */
271 if (onlyshape) {
272 pixmask = XCreateBitmapFromData(dpy, win, rond_bits, DIAMETRE, DIAMETRE);
273 XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
274 XShapeCombineMask(dpy, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
276 /* ONLYSHAPE OFF */
278 mywmhints.initial_state = option_iw;
279 mywmhints.icon_window = iconwin;
280 mywmhints.icon_x = mysizehints.x;
281 mywmhints.icon_y = mysizehints.y;
282 mywmhints.window_group = win;
283 mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
285 XSetWMHints(dpy, win, &mywmhints);
287 XSetCommand(dpy, win, argv, argc);
288 XMapWindow(dpy, win);
290 /****************************************************************************/
293 XCopyArea(dpy, wmg.pixmap, win, NormalGC, 0, 0, DIAMETRE, DIAMETRE, 0, 0);
296 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
299 * ================= MAIN LOOP ==================
301 while (1) {
302 while (XPending(dpy)) {
303 XNextEvent(dpy, &Event);
304 switch (Event.type) {
305 case Expose:
306 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
307 break;
308 case DestroyNotify:
309 XCloseDisplay(dpy);
310 exit(0);
311 break;
312 case ButtonPress:
314 * earth rotate when clic left (1) , zooming when middle (2)
315 * change screen to longitude / latitude when (3)
317 switch (Event.xbutton.button) {
318 case 1:
319 #ifdef MOUSE_LAT_NO_SHIFT
320 move_lat_flag = TRUE;
321 #else
322 if (Event.xbutton.state & ShiftMask)
323 move_lat_flag = TRUE;
324 else
325 move_lat_flag = FALSE;
326 #endif
327 redoaction = 1;
328 wait_release = 1;
329 break;
330 case 2:
331 if (Event.xbutton.state & ShiftMask)
332 redoaction = 2;
333 else
334 redoaction = 3;
335 wait_release = 1;
336 break;
337 case 3:
338 wait_release = 0;
339 redoaction = 0;
340 screen_back();
341 ok = TRUE;
342 break;
343 default:
344 break;
346 break;
347 case ButtonRelease:
348 wait_release = 0;
349 redoaction = 0;
350 break;
351 default:
352 break;
355 if (wait_release) {
356 usleep(2 * VAL_USLEEP_SHORT);
357 if (redoaction == 1)
358 rotation_terre(Event.xbutton.x, Event.xbutton.y, move_lat_flag);
359 else
360 zooming(Event.xbutton.state & ShiftMask);
361 ok = TRUE;
363 if (diftimev(tnext, getimev()).tv_sec < 0 || ok) {
364 ok = FALSE;
365 recalc(redoaction == 1);
366 if (do_something) {
367 if (!myRConvertImage(ctx, small, &pix)) {
368 fprintf(stdout, "plante !?\n");
369 fprintf(stdout, RMessageForError(RErrorCode));
370 exit(1);
372 wmg.pixmap = pix;
373 wmg.mask = pix;
374 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
375 #ifdef DEBUG
376 fprintf(stdout, "draw\n");
377 #endif
378 do_something = FALSE;
381 usleep(VAL_USLEEP);
384 * Still wonder about freeing some memory for X
386 return 0;