Make AddMouseRegion's index unsigned
[dockapps.git] / wmglobe / src / wmglobe.c
blobf69fd11dba695b94879b3e42455bfe0f0dbe3a23
1 /* WMGlobe 1.3 - All the Earth on a WMaker Icon
2 * copyright (C) 1998,99,2000,01 Jerome Dumonteil <jerome.dumonteil@linuxfr.org>
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 #define MY_EXTERN
29 #include "wmglobe.h"
31 #include "cadre0.xbm"
32 #include "cadre1.xbm"
33 #include "cadre2.xbm"
35 #ifdef DEFMAPOK
36 #include "defmap.xpm"
37 #include "defnimap.xpm"
38 #endif
44 int main(int argc, char *argv[])
46 unsigned int borderwidth = 1;
47 XClassHint classHint;
48 char *wname = argv[0];
49 XTextProperty name;
51 XGCValues gcv;
52 unsigned long gcm;
53 XWindowAttributes attributes;
54 XColor color;
56 RContext *ctx;
58 XSizeHints mysizehints;
59 XWMHints mywmhints;
60 Pixel back_pix, fore_pix;
61 char Geometry[256];
63 char *rond_bits;
65 int dummy = 0;
66 int ok, redoaction, wait_release, move_lat_flag;
67 int xx, yy;
69 /** initialisation *********************/
71 xx = 0;
72 yy = 0;
73 ok = FALSE;
74 move_lat_flag = FALSE;
75 redoaction = 0;
76 wait_release = 0;
78 setlocale(LC_TIME, "");
80 #ifdef DEBUG
81 fprintf(stdout, "%s\n", setlocale(LC_TIME, ""));
82 #endif
84 set_defaults();
86 cmdline(argc, argv);
88 #if WITH_MARKERS
89 if (nb_marker) {
90 int i;
91 for (i = 0; i < nb_marker; i++)
92 if (i != moon_marker && i != sun_marker) {
93 marker[i][0] = marker[i][0] * PI / 180.;
94 marker[i][1] = marker[i][1] * PI / 180.;
95 transform_marker(i);
98 #endif
100 switch (typecadre) {
101 case 1:
102 rond_bits = cadre1_bits;
103 break;
104 case 2:
105 rond_bits = cadre2_bits;
106 break;
107 default:
108 rond_bits = cadre0_bits;
110 if (p_type == PTRANDOM) {
111 dlat = 0;
112 dlong = 0;
115 /* setup long term mem allocation */
116 initmyconvert();
119 tdelay.tv_sec = (int) floor(delay);
120 tdelay.tv_usec = (int) ((delay - tdelay.tv_sec) * 1000000);
121 aml = (int) floor(ambient_light * 256);
124 /* ctx initialization */
126 if (!(dpy = XOpenDisplay(dpy_name))) {
127 fprintf(stderr, "%s: can't open display \"%s\"\n",
128 wname, XDisplayName(dpy_name));
129 exit(1);
131 ctx = myRCreateContext(dpy, DefaultScreen(dpy), NULL);
133 if (ctx->attribs->use_shared_memory) {
134 #ifdef DEBUG
135 fprintf(stdout, "remove flags use_shared_memory\n");
136 #endif
137 ctx->attribs->flags ^= RC_UseSharedMemory;
138 ctx->attribs->use_shared_memory = FALSE;
139 ctx->flags.use_shared_pixmap = 0;
141 #ifdef DEBUG
142 fprintf(stdout, "depth %d\n", ctx->depth);
143 fflush(stdout);
144 #endif
147 * loading maps .............
150 if (dayfile != NULL) {
151 map = RLoadImage(ctx, dayfile, 0);
152 if (!use_default_nightmap)
153 use_nightmap = FALSE;
154 ripalpha(map);
155 if (!map) {
156 fprintf(stdout, "pb map ! file not found ?\n");
157 exit(1);
159 } else {
160 #ifdef DEFMAPOK
161 map = RGetImageFromXPMData(ctx, defmap_xpm);
162 use_default_nightmap = TRUE;
163 ripalpha(map);
164 if (!map) {
165 fprintf(stdout, "pb def map ! file not found ?\n");
166 exit(1);
169 #else
170 fprintf(stdout, "need a map !\n");
171 exit(1);
173 #endif
174 if (!oknimap) {
175 use_nightmap = FALSE;
176 use_default_nightmap = FALSE;
178 if (use_nightmap) {
179 if (nightfile != NULL) {
180 mapnight = RLoadImage(ctx, nightfile, 0);
181 ripalpha(mapnight);
182 if (!mapnight) {
183 fprintf(stdout, "pb map night! file not found ?\n");
184 exit(1);
186 } else {
187 #ifdef DEFMAPOK
188 if (use_default_nightmap) {
189 mapnight = RGetImageFromXPMData(ctx, defnimap_xpm);
190 ripalpha(mapnight);
191 if (!mapnight) {
192 fprintf(stdout,
193 "pb def map night ! file not found ?\n");
194 exit(1);
198 #else
199 use_nightmap = FALSE;
201 #endif
204 use_nmap_ini = use_nightmap;
206 /* we need a night map of same size as day map */
207 if (mapnight) {
208 if ((mapnight->width != map->width)
209 || (mapnight->height != map->height)) {
210 RImage *tmp;
211 tmp = mapnight;
212 mapnight = RScaleImage(tmp, map->width, map->height);
213 RReleaseImage(tmp);
214 ripalpha(mapnight);
218 /* some other init ..................................... */
219 ratiox = (double) map->width / (2 * PI);
220 ratioy = (double) map->height / PI;
221 mratiox = (int) floor(ratiox * 256);
222 mratioy = (int) floor(ratioy * 256);
223 loadxpm(ctx->drawable);
225 small = RCreateImage(DIAMETRE, DIAMETRE, 0);
227 calcDistance();
230 * first rendering of the earth
232 rotation_terre(DIAMETRE / 2, DIAMETRE / 2, 0);
233 recalc(0);
234 do_something = FALSE;
236 /*************************************************************************
237 * well, here the problems begin : this code is a merge from wmgeneral and
238 * some stuff of wmaker, should be rewritten ...
239 ************************************************************************/
241 XGetWindowAttributes(dpy, ctx->drawable, &attributes);
243 if (!RConvertImage(ctx, small, &pix)) {
244 fprintf(stdout, "error small->&pix\n");
245 puts(RMessageForError(RErrorCode));
246 exit(1);
248 wmg.pixmap = pix;
249 wmg.mask = pix;
251 mysizehints.flags = USSize | USPosition;
252 mysizehints.x = 0;
253 mysizehints.y = 0;
255 color.pixel = 0;
256 if (!XParseColor(dpy, attributes.colormap, "white", &color)) {
257 fprintf(stdout, "wmglobe: can't parse white\n");
258 } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
259 fprintf(stdout, "wmglobe: can't allocate white\n");
261 back_pix = color.pixel;
263 XGetWindowAttributes(dpy, ctx->drawable, &attributes);
265 color.pixel = 0;
266 if (!XParseColor(dpy, attributes.colormap, "black", &color)) {
267 fprintf(stdout, "wmglobe: can't parse black\n");
268 } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
269 fprintf(stdout, "wmglobe: can't allocate black\n");
271 fore_pix = color.pixel;
274 XWMGeometry(dpy, ctx->screen_number, Geometry, NULL, borderwidth,
275 &mysizehints, &mysizehints.x, &mysizehints.y,
276 &mysizehints.width, &mysizehints.height, &dummy);
277 mysizehints.width = DIAMETRE;
278 mysizehints.height = DIAMETRE;
280 win =
281 XCreateSimpleWindow(dpy, ctx->drawable, mysizehints.x,
282 mysizehints.y, mysizehints.width,
283 mysizehints.height, borderwidth, fore_pix,
284 back_pix);
286 iconwin = XCreateSimpleWindow(dpy, win, mysizehints.x, mysizehints.y,
287 mysizehints.width, mysizehints.height,
288 borderwidth, fore_pix, back_pix);
290 /* Activate hints */
291 XSetWMNormalHints(dpy, win, &mysizehints);
292 classHint.res_name = wname;
293 classHint.res_class = wname;
294 XSetClassHint(dpy, win, &classHint);
296 XSelectInput(dpy, win,
297 ButtonPressMask | ExposureMask | ButtonReleaseMask |
298 PointerMotionMask | StructureNotifyMask);
299 XSelectInput(dpy, iconwin,
300 ButtonPressMask | ExposureMask | ButtonReleaseMask |
301 PointerMotionMask | StructureNotifyMask);
303 if (XStringListToTextProperty(&wname, 1, &name) == 0) {
304 fprintf(stdout, "%s: can't allocate window name\n", wname);
305 exit(1);
307 XSetWMName(dpy, win, &name);
309 /* Create GC for drawing */
311 gcm = GCForeground | GCBackground | GCGraphicsExposures;
312 gcv.foreground = fore_pix;
313 gcv.background = back_pix;
314 gcv.graphics_exposures = 0;
315 NormalGC = XCreateGC(dpy, ctx->drawable, gcm, &gcv);
317 /* ONLYSHAPE ON */
318 if (onlyshape) {
319 pixmask =
320 XCreateBitmapFromData(dpy, win, rond_bits, DIAMETRE, DIAMETRE);
321 XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, pixmask,
322 ShapeSet);
323 XShapeCombineMask(dpy, iconwin, ShapeBounding, 0, 0, pixmask,
324 ShapeSet);
326 /* ONLYSHAPE OFF */
328 mywmhints.initial_state = option_iw;
329 mywmhints.icon_window = iconwin;
330 mywmhints.icon_x = mysizehints.x;
331 mywmhints.icon_y = mysizehints.y;
332 mywmhints.window_group = win;
333 mywmhints.flags =
334 StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
336 XSetWMHints(dpy, win, &mywmhints);
338 XSetCommand(dpy, win, argv, argc);
339 XMapWindow(dpy, win);
342 XCopyArea(dpy, wmg.pixmap, win, NormalGC, 0, 0, DIAMETRE, DIAMETRE, 0,
345 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
348 * ================= MAIN LOOP ==================
350 while (1) {
351 while (XPending(dpy)) {
352 XNextEvent(dpy, &Event);
353 switch (Event.type) {
354 case Expose:
355 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
356 break;
357 case DestroyNotify:
358 XCloseDisplay(dpy);
359 exit(0);
360 break;
361 case ButtonPress:
363 * earth rotate when clic left (1) , zooming when middle (2)
364 * change screen to longitude / latitude when (3)
366 switch (Event.xbutton.button) {
367 case 1:
368 #ifdef MOUSE_LAT_NO_SHIFT
369 move_lat_flag = TRUE;
370 #else
371 if (Event.xbutton.state & ShiftMask)
372 move_lat_flag = TRUE;
373 else
374 move_lat_flag = FALSE;
375 #endif
376 redoaction = 1;
377 wait_release = 1;
378 break;
379 case 2:
380 if (Event.xbutton.state & ShiftMask)
381 redoaction = 2;
382 else
383 redoaction = 3;
384 wait_release = 1;
385 break;
386 case 3:
387 wait_release = 0;
388 redoaction = 0;
389 screen_back();
390 ok = TRUE;
391 break;
392 default:
393 break;
395 break;
396 case ButtonRelease:
397 wait_release = 0;
398 redoaction = 0;
399 break;
400 default:
401 break;
404 if (wait_release) {
405 usleep(2 * VAL_USLEEP_SHORT);
406 if (redoaction == 1)
407 rotation_terre(Event.xbutton.x, Event.xbutton.y,
408 move_lat_flag);
409 else
410 zooming(Event.xbutton.state & ShiftMask);
411 ok = TRUE;
413 if (diftimev(tnext, getimev()).tv_sec < 0 || ok) {
414 ok = FALSE;
415 recalc(redoaction == 1);
416 if (do_something) {
417 if (!myRConvertImage(ctx, small, &pix)) {
418 fprintf(stderr, "crash !?\n");
419 fprintf(stderr, RMessageForError(RErrorCode));
420 exit(1);
422 wmg.pixmap = pix;
423 wmg.mask = pix;
424 RedrawWindowXYWH(0, 0, DIAMETRE, DIAMETRE);
425 #ifdef DEBUG
426 fprintf(stdout, "draw\n");
427 #endif
428 do_something = FALSE;
431 usleep(VAL_USLEEP);
433 return 0;