]> err.no Git - mapper/blob - src/utils.c
Fixes so it builds again under maemo scratchbox.
[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 #ifdef WITH_HILDON
23 #include <hildon-widgets/hildon-system-sound.h>
24 #endif
25
26 #include "utils.h"
27 #include "gps.h"
28 #include "mapper-types.h"
29 #include "bt.h"
30 #include "route.h"
31
32 void sound_noise(void)
33 {
34 #ifdef WITH_HILDON
35         hildon_play_system_sound("/usr/share/sounds/ui-information_note.wav");
36 #else
37
38 #endif
39 }
40
41 void sound_speak(gchar * phrase)
42 {
43 #define _voice_synth_path "/usr/bin/flite"
44
45 #if WITH_HILDON
46         if (!fork()) {
47                 /* We are the fork child.  Synthesize the voice. */
48                 sound_noise();
49                 sleep(1);
50                 printf("%s %s\n", _voice_synth_path, _last_spoken_phrase);
51                 execl(_voice_synth_path, _voice_synth_path,
52                       "-t", _last_spoken_phrase, (char *)NULL);
53                 exit(0);
54         }
55 #else
56
57 #endif
58 }
59
60 #if 0
61 void latlon2unit(gfloat lat, gfloat lon, gint *unitx_, gint *unity_)
62 {
63         gfloat tmp;
64
65         gint unitx = &unitx_;
66         gint unity = &unity_;
67
68         unitx = (lon + 180.f) * (WORLD_SIZE_UNITS / 360.f) + 0.5f;
69         tmp = sinf(lat * (PI / 180.f));
70         unity = 0.5f + (WORLD_SIZE_UNITS / MERCATOR_SPAN)
71             * (logf((1.f + tmp) / (1.f - tmp)) * 0.5f - MERCATOR_TOP);
72 }
73
74 void unit2latlon(gint unitx, gint unity, gfloat *lat, gfloat *lon)
75 {
76         (lon) = ((unitx) * (360.f / WORLD_SIZE_UNITS)) - 180.f;
77         (lat) = (360.f * (atanf(expf(((unity)
78                                       * (MERCATOR_SPAN / WORLD_SIZE_UNITS))
79                                      + MERCATOR_TOP)))) * (1.f / PI) - 90.f;
80 }
81 #endif
82
83 void
84 deg_format(DegFormat degformat, gfloat coor, gchar * scoor, gchar neg_char,
85            gchar pos_char)
86 {
87         gfloat min;
88         gfloat acoor = fabs(coor);
89         printf("%s()\n", __PRETTY_FUNCTION__);
90
91         switch (degformat) {
92         case DDPDDDDD:
93                 sprintf(scoor, "%.5f°", coor);
94                 break;
95         case DDPDDDDD_NSEW:
96                 sprintf(scoor, "%.5f° %c", acoor,
97                         coor < 0.f ? neg_char : pos_char);
98                 break;
99
100         case DD_MMPMMM:
101                 sprintf(scoor, "%d°%06.3f'",
102                         (int)coor, (acoor - (int)acoor) * 60.0);
103                 break;
104         case DD_MMPMMM_NSEW:
105                 sprintf(scoor, "%d°%06.3f' %c",
106                         (int)acoor, (acoor - (int)acoor) * 60.0,
107                         coor < 0.f ? neg_char : pos_char);
108                 break;
109
110         case DD_MM_SSPS:
111                 min = (acoor - (int)acoor) * 60.0;
112                 sprintf(scoor, "%d°%02d'%04.1f\"", (int)coor, (int)min,
113                         ((min - (int)min) * 60.0));
114                 break;
115         case DD_MM_SSPS_NSEW:
116                 min = (acoor - (int)acoor) * 60.0;
117                 sprintf(scoor, "%d°%02d'%04.1f\" %c", (int)acoor, (int)min,
118                         ((min - (int)min) * 60.0),
119                         coor < 0.f ? neg_char : pos_char);
120                 break;
121         default:;
122         }
123         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
124 }
125
126 /**
127  * Calculate the distance between two lat/lon pairs.  The distance is returned
128  * in kilometers and should be converted using UNITS_CONVERT[_units].
129  */
130 gfloat calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2)
131 {
132         gdouble dlat, dlon, slat, slon, a;
133
134         /* Convert to radians. */
135         lat1 *= (PI / 180.f);
136         lon1 *= (PI / 180.f);
137         lat2 *= (PI / 180.f);
138         lon2 *= (PI / 180.f);
139
140         dlat = lat2 - lat1;
141         dlon = lon2 - lon1;
142
143         slat = sinf(dlat / 2.f);
144         slon = sinf(dlon / 2.f);
145         a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon);
146
147         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
148         return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS);
149 }
150
151 /**
152  * Convert the float lat/lon/speed/heading data into integer units.
153  */
154 void integerize_data()
155 {
156         gfloat tmp;
157         printf("%s()\n", __PRETTY_FUNCTION__);
158
159         latlon2unit(_gps.lat, _gps.lon, _pos.unitx, _pos.unity);
160
161         tmp = (_gps.heading * (1.f / 180.f)) * PI;
162         _vel_offsetx = (gint) (floorf(_gps.speed * sinf(tmp) + 0.5f));
163         _vel_offsety = -(gint) (floorf(_gps.speed * cosf(tmp) + 0.5f));
164
165         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
166 }
167
168 gint
169 download_comparefunc(const ProgressUpdateInfo * a,
170                      const ProgressUpdateInfo * b, gpointer user_data)
171 {
172         gint diff = (a->priority - b->priority);
173         if (diff)
174                 return diff;
175         diff = (a->tilex - b->tilex);
176         if (diff)
177                 return diff;
178         diff = (a->tiley - b->tiley);
179         if (diff)
180                 return diff;
181         diff = (a->zoom - b->zoom);
182         if (diff)
183                 return diff;
184         diff = (a->repo - b->repo);
185         if (diff)
186                 return diff;
187         /* Otherwise, deletes are "greatest" (least priority). */
188         if (!a->retries)
189                 return (b->retries ? -1 : 0);
190         else if (!b->retries)
191                 return (a->retries ? 1 : 0);
192         /* Do updates after non-updates (because they'll both be done anyway). */
193         return (a->retries - b->retries);
194 }
195
196 #ifdef BT_HACK
197 void reset_bluetooth()
198 {
199         printf("%s()\n", __PRETTY_FUNCTION__);
200         if (system
201             ("/usr/bin/sudo -l | grep -q '/usr/sbin/hciconfig  *hci0  *reset'"
202              " && sudo /usr/sbin/hciconfig hci0 reset"))
203                 popup_error(_window,
204                             _
205                             ("An error occurred while trying to reset the bluetooth "
206                              "radio.\n\n"
207                              "Did you make sure to modify\nthe /etc/sudoers file?"));
208         else if (_conn_state > RCVR_OFF) {
209                 set_conn_state(RCVR_DOWN);
210                 rcvr_connect_later();
211         }
212         vprintf("%s(): return\n", __PRETTY_FUNCTION__);
213 }
214 #else
215 void reset_bluetooth()
216 {
217 }
218 #endif