]> err.no Git - mapper/blob - src/utils.c
Remove gps reset hack.
[mapper] / src / utils.c
1 #define _GNU_SOURCE
2
3 #include <config.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <strings.h>
8 #include <stddef.h>
9 #include <locale.h>
10 #include <math.h>
11 #include <errno.h>
12 #include <sys/wait.h>
13 #include <glib/gstdio.h>
14 #include <gtk/gtk.h>
15 #include <fcntl.h>
16 #include <gdk/gdkkeysyms.h>
17 #include <libgnomevfs/gnome-vfs.h>
18 #include <curl/multi.h>
19 #include <gconf/gconf-client.h>
20 #include <libxml/parser.h>
21
22 #include "utils.h"
23 #include "gps.h"
24 #include "mapper-types.h"
25 #include "bt.h"
26
27 void sound_noise(void)
28 {
29 #ifdef WITH_HILDON
30         hildon_play_system_sound("/usr/share/sounds/ui-information_note.wav");
31 #else
32
33 #endif
34 }
35
36 void sound_speak(gchar * phrase)
37 {
38 #define _voice_synth_path "/usr/bin/flite"
39
40 #if WITH_HILDON
41         if (!fork()) {
42                 /* We are the fork child.  Synthesize the voice. */
43                 sound_noise();
44                 sleep(1);
45                 printf("%s %s\n", _voice_synth_path, _last_spoken_phrase);
46                 execl(_voice_synth_path, _voice_synth_path,
47                       "-t", _last_spoken_phrase, (char *)NULL);
48                 exit(0);
49         }
50 #else
51
52 #endif
53 }
54
55 #if 0
56 void latlon2unit(gfloat lat, gfloat lon, gint *unitx_, gint *unity_)
57 {
58         gfloat tmp;
59
60         gint unitx = &unitx_;
61         gint unity = &unity_;
62
63         unitx = (lon + 180.f) * (WORLD_SIZE_UNITS / 360.f) + 0.5f;
64         tmp = sinf(lat * (PI / 180.f));
65         unity = 0.5f + (WORLD_SIZE_UNITS / MERCATOR_SPAN)
66             * (logf((1.f + tmp) / (1.f - tmp)) * 0.5f - MERCATOR_TOP);
67 }
68
69 void unit2latlon(gint unitx, gint unity, gfloat *lat, gfloat *lon)
70 {
71         (lon) = ((unitx) * (360.f / WORLD_SIZE_UNITS)) - 180.f;
72         (lat) = (360.f * (atanf(expf(((unity)
73                                       * (MERCATOR_SPAN / WORLD_SIZE_UNITS))
74                                      + MERCATOR_TOP)))) * (1.f / PI) - 90.f;
75 }
76 #endif
77
78 void
79 deg_format(DegFormat degformat, gfloat coor, gchar * scoor, gchar neg_char,
80            gchar pos_char)
81 {
82         gfloat min;
83         gfloat acoor = fabs(coor);
84         printf("%s()\n", __PRETTY_FUNCTION__);
85
86         switch (degformat) {
87         case DDPDDDDD:
88                 sprintf(scoor, "%.5f°", coor);
89                 break;
90         case DDPDDDDD_NSEW:
91                 sprintf(scoor, "%.5f° %c", acoor,
92                         coor < 0.f ? neg_char : pos_char);
93                 break;
94
95         case DD_MMPMMM:
96                 sprintf(scoor, "%d°%06.3f'",
97                         (int)coor, (acoor - (int)acoor) * 60.0);
98                 break;
99         case DD_MMPMMM_NSEW:
100                 sprintf(scoor, "%d°%06.3f' %c",
101                         (int)acoor, (acoor - (int)acoor) * 60.0,
102                         coor < 0.f ? neg_char : pos_char);
103                 break;
104
105         case DD_MM_SSPS:
106                 min = (acoor - (int)acoor) * 60.0;
107                 sprintf(scoor, "%d°%02d'%04.1f\"", (int)coor, (int)min,
108                         ((min - (int)min) * 60.0));
109                 break;
110         case DD_MM_SSPS_NSEW:
111                 min = (acoor - (int)acoor) * 60.0;
112                 sprintf(scoor, "%d°%02d'%04.1f\" %c", (int)acoor, (int)min,
113                         ((min - (int)min) * 60.0),
114                         coor < 0.f ? neg_char : pos_char);
115                 break;
116         default:;
117         }
118         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
119 }
120
121 /**
122  * Calculate the distance between two lat/lon pairs.  The distance is returned
123  * in kilometers and should be converted using UNITS_CONVERT[_units].
124  */
125 gfloat calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2)
126 {
127         gdouble dlat, dlon, slat, slon, a;
128
129         /* Convert to radians. */
130         lat1 *= (PI / 180.f);
131         lon1 *= (PI / 180.f);
132         lat2 *= (PI / 180.f);
133         lon2 *= (PI / 180.f);
134
135         dlat = lat2 - lat1;
136         dlon = lon2 - lon1;
137
138         slat = sinf(dlat / 2.f);
139         slon = sinf(dlon / 2.f);
140         a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon);
141
142         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
143         return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS);
144 }
145
146 /**
147  * Convert the float lat/lon/speed/heading data into integer units.
148  */
149 void integerize_data()
150 {
151         gfloat tmp;
152         printf("%s()\n", __PRETTY_FUNCTION__);
153
154         latlon2unit(_gps.lat, _gps.lon, _pos.unitx, _pos.unity);
155
156         tmp = (_gps.heading * (1.f / 180.f)) * PI;
157         _vel_offsetx = (gint) (floorf(_gps.speed * sinf(tmp) + 0.5f));
158         _vel_offsety = -(gint) (floorf(_gps.speed * cosf(tmp) + 0.5f));
159
160         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
161 }
162
163 gint
164 download_comparefunc(const ProgressUpdateInfo * a,
165                      const ProgressUpdateInfo * b, gpointer user_data)
166 {
167         gint diff = (a->priority - b->priority);
168         if (diff)
169                 return diff;
170         diff = (a->tilex - b->tilex);
171         if (diff)
172                 return diff;
173         diff = (a->tiley - b->tiley);
174         if (diff)
175                 return diff;
176         diff = (a->zoom - b->zoom);
177         if (diff)
178                 return diff;
179         diff = (a->repo - b->repo);
180         if (diff)
181                 return diff;
182         /* Otherwise, deletes are "greatest" (least priority). */
183         if (!a->retries)
184                 return (b->retries ? -1 : 0);
185         else if (!b->retries)
186                 return (a->retries ? 1 : 0);
187         /* Do updates after non-updates (because they'll both be done anyway). */
188         return (a->retries - b->retries);
189 }