wmglobe: Update to version 1.3.
[dockapps.git] / wmglobe / src / wmglobe.c
blob2c463e482b4d2d0ffa5efb1aa904d42389e1e354
1 /* WMGlobe 1.3 - All the Earth on a WMaker Icon
2 * copyright (C) 1998,99,2000,01 Jerome Dumonteil <jerome.dumonteil@linuxfr.org>
3 *
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.
8 *
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 ***************************************************************************/
18 /*
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
42 int main(int argc, char *argv[])
44 unsigned int borderwidth = 1;
45 XClassHint classHint;
46 char *wname = argv[0];
47 XTextProperty name;
49 XGCValues gcv;
50 unsigned long gcm;
51 XWindowAttributes attributes;
52 XColor color;
54 RContext *ctx;
56 XSizeHints mysizehints;
57 XWMHints mywmhints;
58 Pixel back_pix, fore_pix;
59 char Geometry[256];
61 char *rond_bits;
63 int dummy = 0;
64 int ok, redoaction, wait_release, move_lat_flag;
65 int xx, yy;
67 /** initialisation *********************/
69 xx = 0;
70 yy = 0;
71 ok = FALSE;
72 move_lat_flag = FALSE;
73 redoaction = 0;
74 wait_release = 0;
76 setlocale(LC_TIME, "");
78 #ifdef DEBUG
79 fprintf(stdout, "%s\n", setlocale(LC_TIME, ""));
80 #endif
82 set_defaults();
84 cmdline(argc, argv);
86 #if WITH_MARKERS
87 if (nb_marker) {
88 int i;
89 for (i = 0; i < nb_marker; i++)
90 if (i != moon_marker && i != sun_marker) {
91 marker[i][0] = marker[i][0] * PI / 180.;
92 marker[i][1] = marker[i][1] * PI / 180.;
93 transform_marker(i);
96 #endif
98 switch (typecadre) {
99 case 1:
100 rond_bits = cadre1_bits;
101 break;
102 case 2:
103 rond_bits = cadre2_bits;
104 break;
105 default:
106 rond_bits = cadre0_bits;
108 if (p_type == PTRANDOM) {
109 dlat = 0;
110 dlong = 0;
113 /* setup long term mem allocation */
114 initmyconvert();
117 tdelay.tv_sec = (int) floor(delay);
118 tdelay.tv_usec = (int) ((delay - tdelay.tv_sec) * 1000000);
119 aml = (int) floor(ambient_light * 256);
122 /* ctx initialization */
124 if (!(dpy = XOpenDisplay(dpy_name))) {
125 fprintf(stderr, "%s: can't open display \"%s\"\n",
126 wname, XDisplayName(dpy_name));
127 exit(1);
129 ctx = myRCreateContext(dpy, DefaultScreen(dpy), NULL);
131 if (ctx->attribs->use_shared_memory) {
132 #ifdef DEBUG
133 fprintf(stdout, "remove flags use_shared_memory\n");
134 #endif
135 ctx->attribs->flags ^= RC_UseSharedMemory;
136 ctx->attribs->use_shared_memory = FALSE;
137 ctx->flags.use_shared_pixmap = 0;
139 #ifdef DEBUG
140 fprintf(stdout, "depth %d\n", ctx->depth);
141 fflush(stdout);
142 #endif
145 * loading maps .............
148 if (dayfile != NULL) {
149 map = RLoadImage(ctx, dayfile, 0);
150 if (!use_default_nightmap)
151 use_nightmap = FALSE;
152 ripalpha(map);
153 if (!map) {
154 fprintf(stdout, "pb map ! file not found ?\n");
155 exit(1);
157 } else {
158 #ifdef DEFMAPOK
159 map = RGetImageFromXPMData(ctx, defmap_xpm);
160 use_default_nightmap = TRUE;
161 ripalpha(map);
162 if (!map) {
163 fprintf(stdout, "pb def map ! file not found ?\n");
164 exit(1);
167 #else
168 fprintf(stdout, "need a map !\n");
169 exit(1);
171 #endif
172 if (!oknimap) {
173 use_nightmap = FALSE;
174 use_default_nightmap = FALSE;
176 if (use_nightmap) {
177 if (nightfile != NULL) {
178 mapnight = RLoadImage(ctx, nightfile, 0);
179 ripalpha(mapnight);
180 if (!mapnight) {
181 fprintf(stdout, "pb map night! file not found ?\n");
182 exit(1);
184 } else {
185 #ifdef DEFMAPOK
186 if (use_default_nightmap) {
187 mapnight = RGetImageFromXPMData(ctx, defnimap_xpm);
188 ripalpha(mapnight);
189 if (!mapnight) {
190 fprintf(stdout,
191 "pb def map night ! file not found ?\n");
192 exit(1);
196 #else
197 use_nightmap = FALSE;
199 #endif
202 use_nmap_ini = use_nightmap;
204 /* we need a night map of same size as day map */
205 if (mapnight) {
206 if ((mapnight->width != map->width)
207 || (mapnight->height != map->height)) {
208 RImage *tmp;
209 tmp = mapnight;
210 mapnight = RScaleImage(tmp, map->width, map->height);
211 RReleaseImage(tmp);
212 ripalpha(mapnight);
216 /* some other init ..................................... */
217 ratiox = (double) map->width / (2 * PI);
218 ratioy = (double) map->height / PI;
219 mratiox = (int) floor(ratiox * 256);
220 mratioy = (int) floor(ratioy * 256);
221 loadxpm(ctx->drawable);
223 small = RCreateImage(DIAMETRE, DIAMETRE, 0);
225 calcDistance();
228 * first rendering of the earth
230 rotation_terre(DIAMETRE / 2, DIAMETRE / 2, 0);
231 recalc(0);
232 do_something = FALSE;
234 /*************************************************************************
235 * well, here the problems begin : this code is a merge from wmgeneral and
236 * some stuff of wmaker, should be rewritten ...
237 ************************************************************************/
239 XGetWindowAttributes(dpy, ctx->drawable, &attributes);
241 if (!RConvertImage(ctx, small, &pix)) {
242 fprintf(stdout, "error small->&pix\n");
243 puts(RMessageForError(RErrorCode));
244 exit(1);
246 wmg.pixmap = pix;
247 wmg.mask = pix;
249 mysizehints.flags = USSize | USPosition;
250 mysizehints.x = 0;
251 mysizehints.y = 0;
253 color.pixel = 0;
254 if (!XParseColor(dpy, attributes.colormap, "white", &color)) {
255 fprintf(stdout, "wmglobe: can't parse white\n");
256 } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
257 fprintf(stdout, "wmglobe: can't allocate white\n");
259 back_pix = color.pixel;
261 XGetWindowAttributes(dpy, ctx->drawable, &attributes);
263 color.pixel = 0;
264 if (!XParseColor(dpy, attributes.colormap, "black", &color)) {
265 fprintf(stdout, "wmglobe: can't parse black\n");
266 } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
267 fprintf(stdout, "wmglobe: can't allocate black\n");
269 fore_pix = color.pixel;
272 XWMGeometry(dpy, ctx->screen_number, Geometry, NULL, borderwidth,
273 &mysizehints, &mysizehints.x, &mysizehints.y,
274 &mysizehints.width, &mysizehints.height, &dummy);
275 mysizehints.width = DIAMETRE;
276 mysizehints.height = DIAMETRE;
278 win =
279 XCreateSimpleWindow(dpy, ctx->drawable, mysizehints.x,
280 mysizehints.y, mysizehints.width,
281 mysizehints.height, borderwidth, fore_pix,
282 back_pix);
284 iconwin = XCreateSimpleWindow(dpy, win, mysizehints.x, mysizehints.y,
285 mysizehints.width, mysizehints.height,
286 borderwidth, fore_pix, back_pix);
288 /* Activate hints */
289 XSetWMNormalHints(dpy, win, &mysizehints);
290 classHint.res_name = wname;
291 classHint.res_class = wname;
292 XSetClassHint(dpy, win, &classHint);
294 XSelectInput(dpy, win,
295 ButtonPressMask | ExposureMask | ButtonReleaseMask |
296 PointerMotionMask | StructureNotifyMask);
297 XSelectInput(dpy, iconwin,
298 ButtonPressMask | ExposureMask | ButtonReleaseMask |
299 PointerMotionMask | StructureNotifyMask);
301 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
302 fprintf(stdout, "%s: can't allocate window name\n", wname);
303 exit(1);
305 XSetWMName(dpy, win, &name);
307 /* Create GC for drawing */
309 gcm = GCForeground | GCBackground | GCGraphicsExposures;
310 gcv.foreground = fore_pix;
311 gcv.background = back_pix;
312 gcv.graphics_exposures = 0;
313 NormalGC = XCreateGC(dpy, ctx->drawable, gcm, &gcv);
315 /* ONLYSHAPE ON */
316 if (onlyshape) {
317 pixmask =
318 XCreateBitmapFromData(dpy, win, rond_bits, DIAMETRE, DIAMETRE);
319 XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, pixmask,
320 ShapeSet);
321 XShapeCombineMask(dpy, iconwin, ShapeBounding, 0, 0, pixmask,
322 ShapeSet);
324 /* ONLYSHAPE OFF */
326 mywmhints.initial_state = option_iw;
327 mywmhints.icon_window = iconwin;
328 mywmhints.icon_x = mysizehints.x;
329 mywmhints.icon_y = mysizehints.y;
330 mywmhints.window_group = win;
331 mywmhints.flags =
332 StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
334 XSetWMHints(dpy, win, &mywmhints);
336 XSetCommand(dpy, win, argv, argc);
337 XMapWindow(dpy, win);
340 XCopyArea(dpy, wmg.pixmap, win, NormalGC, 0, 0, DIAMETRE, DIAMETRE, 0,
343 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
346 * ================= MAIN LOOP ==================
348 while (1) {
349 while (XPending(dpy)) {
350 XNextEvent(dpy, &Event);
351 switch (Event.type) {
352 case Expose:
353 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
354 break;
355 case DestroyNotify:
356 XCloseDisplay(dpy);
357 exit(0);
358 break;
359 case ButtonPress:
361 * earth rotate when clic left (1) , zooming when middle (2)
362 * change screen to longitude / latitude when (3)
364 switch (Event.xbutton.button) {
365 case 1:
366 #ifdef MOUSE_LAT_NO_SHIFT
367 move_lat_flag = TRUE;
368 #else
369 if (Event.xbutton.state & ShiftMask)
370 move_lat_flag = TRUE;
371 else
372 move_lat_flag = FALSE;
373 #endif
374 redoaction = 1;
375 wait_release = 1;
376 break;
377 case 2:
378 if (Event.xbutton.state & ShiftMask)
379 redoaction = 2;
380 else
381 redoaction = 3;
382 wait_release = 1;
383 break;
384 case 3:
385 wait_release = 0;
386 redoaction = 0;
387 screen_back();
388 ok = TRUE;
389 break;
390 default:
391 break;
393 break;
394 case ButtonRelease:
395 wait_release = 0;
396 redoaction = 0;
397 break;
398 default:
399 break;
402 if (wait_release) {
403 usleep(2 * VAL_USLEEP_SHORT);
404 if (redoaction == 1)
405 rotation_terre(Event.xbutton.x, Event.xbutton.y,
406 move_lat_flag);
407 else
408 zooming(Event.xbutton.state & ShiftMask);
409 ok = TRUE;
411 if (diftimev(tnext, getimev()).tv_sec < 0 || ok) {
412 ok = FALSE;
413 recalc(redoaction == 1);
414 if (do_something) {
415 if (!myRConvertImage(ctx, small, &pix)) {
416 fprintf(stderr, "crash !?\n");
417 fprintf(stderr, RMessageForError(RErrorCode));
418 exit(1);
420 wmg.pixmap = pix;
421 wmg.mask = pix;
422 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
423 #ifdef DEBUG
424 fprintf(stdout, "draw\n");
425 #endif
426 do_something = FALSE;
429 usleep(VAL_USLEEP);
431 return 0;