~foxtrotgps-team/foxtrotgps/trunk

« back to all changes in this revision

Viewing changes to src/gps_functions.c

  • Committer: Joshua Judson Rosen
  • Date: 2009-09-26 02:35:15 UTC
  • Revision ID: rozzin@geekspace.com-20090926023515-yxl5sg1a45gupuc8
Imported from tangogps-0.9.7 tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
#ifdef HAVE_CONFIG_H
 
4
#  include <config.h>
 
5
#endif
 
6
 
 
7
#include <glib.h>
 
8
#include <glib/gprintf.h>
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include <unistd.h>
 
13
#include <time.h>
 
14
#include <string.h>
 
15
#include <math.h>
 
16
 
 
17
#include <sys/types.h>
 
18
#include <sys/socket.h>
 
19
#include <netinet/in.h>
 
20
#include <arpa/inet.h>
 
21
 
 
22
#include "gps_functions.h"
 
23
#include "globals.h"
 
24
#include "support.h"
 
25
#include "tile_management.h"
 
26
#include "converter.h"
 
27
 
 
28
char * distance2scale(float distance, float *factor);
 
29
 
 
30
 
 
31
static GIOChannel *gpsd_io_channel =NULL;
 
32
 
 
33
static guint sid1,  sid3; 
 
34
guint watchdog;
 
35
 
 
36
 
 
37
 
 
38
gboolean
 
39
reset_gpsd_io()
 
40
{
 
41
        printf("*** %s(): \n",__PRETTY_FUNCTION__);
 
42
        
 
43
        global_reconnect_gpsd = TRUE;
 
44
        g_source_remove(watchdog);
 
45
 
 
46
        g_source_remove(sid1); 
 
47
        g_source_remove(sid3); 
 
48
        
 
49
        return FALSE;   
 
50
}
 
51
 
 
52
 
 
53
void
 
54
osd_speed(gboolean force_redraw)
 
55
{
 
56
        PangoContext            *context = NULL;
 
57
        PangoLayout             *layout  = NULL;
 
58
        PangoFontDescription    *desc    = NULL;
 
59
        
 
60
        GdkColor color;
 
61
        GdkGC *gc;
 
62
        
 
63
        gchar *buffer;
 
64
        static int x = 10, y = 10;
 
65
        static int width = 0, height = 0;
 
66
 
 
67
        static double speed_tmp = 0;
 
68
        
 
69
        double unit_conv = 1;
 
70
                
 
71
 
 
72
 
 
73
        if(gpsdata && mouse_dx == 0 && mouse_dy == 0) 
 
74
        {
 
75
                switch (global_speed_unit)
 
76
                {
 
77
                        case 0:
 
78
                                unit_conv = 1.0;
 
79
                                break;
 
80
                        case 1 :
 
81
                                unit_conv = 1.0/1.609344;
 
82
                                break;
 
83
                        case 2 :
 
84
                                unit_conv = 1.0 / 1.852;
 
85
                                break;          
 
86
                }
 
87
                
 
88
 
 
89
        
 
90
                buffer = g_strdup_printf("%.0f", gpsdata->fix.speed*3.6*unit_conv);
 
91
                
 
92
                
 
93
                context = gtk_widget_get_pango_context (map_drawable);
 
94
                layout  = pango_layout_new (context);
 
95
                desc    = pango_font_description_new();
 
96
                
 
97
                pango_font_description_set_size (desc, 50 * PANGO_SCALE);
 
98
                pango_layout_set_font_description (layout, desc);
 
99
                pango_layout_set_text (layout, buffer, strlen(buffer));
 
100
        
 
101
        
 
102
                gc = gdk_gc_new (map_drawable->window);
 
103
        
 
104
                color.red = (gpsdata->fix.speed*3.6*unit_conv > 50) ? 0xffff : 0;
 
105
                color.green = 0;
 
106
                color.blue = 0;
 
107
                
 
108
                gdk_gc_set_rgb_fg_color (gc, &color);
 
109
        
 
110
        
 
111
                
 
112
                
 
113
                
 
114
                
 
115
                if(speed_tmp != floor(gpsdata->fix.speed*3.6*unit_conv) || force_redraw)
 
116
                {
 
117
                        
 
118
                        gdk_draw_drawable (
 
119
                                map_drawable->window,
 
120
                                map_drawable->style->fg_gc[GTK_WIDGET_STATE (map_drawable)],
 
121
                                pixmap,
 
122
                                0,0,
 
123
                                0, 0,
 
124
                                width+10,height+10);
 
125
                
 
126
                        
 
127
                        if(gpsdata->fix.speed>0.01 && gpsdata->valid) 
 
128
                                gdk_draw_layout(map_drawable->window,
 
129
                                                gc,
 
130
                                                x, y,
 
131
                                                layout);
 
132
                        
 
133
                        
 
134
                        pango_layout_get_pixel_size(layout, &width, &height);
 
135
                }
 
136
                
 
137
                speed_tmp = floor(gpsdata->fix.speed*3.6*unit_conv);
 
138
                
 
139
                g_free(buffer);
 
140
                pango_font_description_free (desc);
 
141
                g_object_unref (layout);
 
142
                g_object_unref (gc);
 
143
        }
 
144
}
 
145
void
 
146
set_label_nogps()
 
147
{
 
148
        static GtkLabel *label=NULL;
 
149
        static gchar buffer[256];
 
150
        int num_dl_threads = 0;
 
151
 
 
152
        if(label == NULL)
 
153
                label   = GTK_LABEL(lookup_widget(window1, "label4"));
 
154
        
 
155
        num_dl_threads = update_thread_number(0);
 
156
        if(num_dl_threads && !global_tiles_in_dl_queue)
 
157
        {       
 
158
                g_sprintf(buffer, 
 
159
                        "<b>no GPS found</b> - <span foreground='#0000ff'><b>D%d</b></span>",
 
160
                        num_dl_threads);
 
161
        }
 
162
        else if (num_dl_threads && global_tiles_in_dl_queue)
 
163
                g_sprintf(buffer, 
 
164
                        "<b>no GPSD found</b> - <span foreground='#0000ff'><b>D%d</b></span> - <b>[%d]</b>",
 
165
                        num_dl_threads, global_tiles_in_dl_queue);
 
166
        else
 
167
                g_sprintf(buffer,"<b>no GPS found</b>");
 
168
        
 
169
        
 
170
        gtk_label_set_label(label, buffer);
 
171
 
 
172
}
 
173
void
 
174
set_label()
 
175
{
 
176
        static GtkLabel *label=NULL,   *label31, *label38, *label39;
 
177
        static GtkLabel *label41, *label42, *label43, *label45;
 
178
        static GtkLabel *label66, *label68, *label70;
 
179
        static gchar buffer[256];
 
180
        static gchar numdl_buf[64], dl_buf[64], ff_buf[64], tr_buf[64];
 
181
        static gchar speedunit[5], distunit[3], altunit[3];
 
182
        int trip_hours, trip_minutes, trip_seconds;
 
183
        int num_dl_threads = 0;
 
184
        time_t time_sec;
 
185
        struct tm  *ts;
 
186
        double unit_conv = 1, unit_conv_alt = 1;
 
187
        
 
188
        osd_speed(FALSE);
 
189
        
 
190
        if(global_speed_unit==1)
 
191
        {
 
192
                unit_conv = 1.0/1.609344;
 
193
                g_sprintf(speedunit, "%s","mph");
 
194
                g_sprintf(distunit, "%s", "m");
 
195
        }
 
196
        else if(global_speed_unit==2)
 
197
        {
 
198
                unit_conv = 1.0/1.852;
 
199
                g_sprintf(speedunit, "%s","kn");
 
200
                g_sprintf(distunit, "%s", "NM");
 
201
        }
 
202
        else
 
203
        {
 
204
                g_sprintf(speedunit, "%s","km/h");
 
205
                g_sprintf(distunit, "%s", "km");
 
206
        }
 
207
        
 
208
        
 
209
        if(global_alt_unit==1)
 
210
        {
 
211
                unit_conv_alt = 1.0/0.3048;
 
212
                g_sprintf(altunit, "%s", "ft");
 
213
        }
 
214
        else
 
215
                g_sprintf(altunit, "%s", "m");
 
216
        
 
217
        if(global_auto_download)
 
218
                g_sprintf(dl_buf, "%s", "");
 
219
        else
 
220
                g_sprintf(dl_buf, "%s", "<span foreground='#ff0000'><b>!</b></span>");
 
221
        
 
222
        if (global_fftimer_running)
 
223
                g_sprintf(ff_buf, "%s", "<span foreground='#00e000'><b>f</b></span>");
 
224
        else
 
225
                g_sprintf(ff_buf, "%s", "");
 
226
        
 
227
        if (trip_logger_on)
 
228
                g_sprintf(tr_buf, "%s", "<span foreground='#00e000'><b>t</b></span>");
 
229
        else
 
230
                g_sprintf(tr_buf, "%s", "");
 
231
        
 
232
        if(label == NULL)
 
233
        {
 
234
                label   = GTK_LABEL(lookup_widget(window1, "label4"));
 
235
                label45 = GTK_LABEL(lookup_widget(window1, "label45"));
 
236
                label41 = GTK_LABEL(lookup_widget(window1, "label41"));
 
237
                label31 = GTK_LABEL(lookup_widget(window1, "label31"));
 
238
                label38 = GTK_LABEL(lookup_widget(window1, "label38"));
 
239
                label39 = GTK_LABEL(lookup_widget(window1, "label39"));
 
240
                label42 = GTK_LABEL(lookup_widget(window1, "label42"));
 
241
                label43 = GTK_LABEL(lookup_widget(window1, "label43"));
 
242
                label66 = GTK_LABEL(lookup_widget(window1, "label66"));
 
243
                label68 = GTK_LABEL(lookup_widget(window1, "label68"));
 
244
                label70 = GTK_LABEL(lookup_widget(window1, "label70"));
 
245
        }
 
246
 
 
247
 
 
248
        
 
249
        
 
250
        
 
251
        num_dl_threads = update_thread_number(0);
 
252
        
 
253
        if(num_dl_threads && !global_tiles_in_dl_queue)
 
254
                g_sprintf(numdl_buf, "<span foreground='#0000ff'><b>D%d</b></span> ", 
 
255
                          num_dl_threads);
 
256
        else if (num_dl_threads && global_tiles_in_dl_queue)
 
257
                g_sprintf(numdl_buf, "<span foreground='#0000ff'><b>D%d</b></span>-%d ", 
 
258
                          num_dl_threads, global_tiles_in_dl_queue);
 
259
        else
 
260
                g_sprintf(numdl_buf, "%s", "");
 
261
        
 
262
 
 
263
        g_sprintf(buffer, 
 
264
                "%s%s%s%s<b>%4.1f</b> %s  %.0f° a:%.0f%s  t:%.3f%s  %d/%d/%.1f",
 
265
                numdl_buf,
 
266
                dl_buf,
 
267
                tr_buf,
 
268
                ff_buf,
 
269
                gpsdata->fix.speed * 3.6 * unit_conv,   speedunit,
 
270
                gpsdata->fix.track * unit_conv,
 
271
                gpsdata->fix.altitude * unit_conv_alt,  altunit,
 
272
                trip_distance * unit_conv,              distunit,
 
273
                gpsdata->satellites_inview,
 
274
                gpsdata->satellites_used,
 
275
                gpsdata->hdop);
 
276
 
 
277
 
 
278
        gtk_label_set_label(label, buffer);
 
279
 
 
280
        
 
281
        
 
282
        
 
283
        
 
284
        
 
285
        time_sec = (time_t)gpsdata->fix.time;
 
286
        ts = localtime(&time_sec);
 
287
        
 
288
        
 
289
        strftime(buffer, sizeof(buffer), "%a %Y-%m-%d %H:%M:%S ", ts); 
 
290
        gtk_label_set_label(label41,buffer);
 
291
 
 
292
        
 
293
        switch (global_latlon_unit)
 
294
        {
 
295
                case 0:
 
296
                        g_sprintf(buffer, "%f - %f", gpsdata->fix.latitude, gpsdata->fix.longitude);
 
297
                        break;
 
298
                case 1:
 
299
                        g_sprintf(buffer, "%s   %s", 
 
300
                                  latdeg2latmin(gpsdata->fix.latitude),
 
301
                                  londeg2lonmin(gpsdata->fix.longitude));
 
302
                        break;
 
303
                case 2:
 
304
                        g_sprintf(buffer, "%s   %s", 
 
305
                                  latdeg2latsec(gpsdata->fix.latitude),
 
306
                                  londeg2lonsec(gpsdata->fix.longitude));
 
307
        }
 
308
        gtk_label_set_label(label31,buffer);
 
309
        
 
310
        
 
311
        g_sprintf(buffer, 
 
312
                "<b><span foreground='#0000ff'><span font_desc='40'>%.1f</span></span></b> %s", 
 
313
                gpsdata->fix.speed*3.6*unit_conv, speedunit);
 
314
        gtk_label_set_label(label38,buffer);
 
315
 
 
316
        
 
317
        g_sprintf(buffer, "%.1f %s", gpsdata->fix.altitude * unit_conv_alt, altunit);
 
318
        gtk_label_set_label(label39,buffer);
 
319
        
 
320
        
 
321
        g_sprintf(buffer, "%.1f° ", gpsdata->fix.track);
 
322
        gtk_label_set_label(label42,buffer);
 
323
        
 
324
        
 
325
        g_sprintf(buffer, "%d/%d    HDOP: %.1f ", 
 
326
                        gpsdata->satellites_inview, gpsdata->satellites_used, gpsdata->hdop);
 
327
        gtk_label_set_label(label43,buffer);
 
328
 
 
329
        
 
330
        
 
331
        
 
332
 
 
333
        
 
334
        g_sprintf(buffer, "%.3f %s", trip_distance*unit_conv,distunit);
 
335
        gtk_label_set_label(label45,buffer);
 
336
 
 
337
 
 
338
        
 
339
        trip_hours   = trip_time / 3600;
 
340
        trip_minutes = ((int)trip_time%3600)/60;
 
341
        trip_seconds = (int)trip_time % 60;
 
342
        
 
343
        if (trip_seconds < 10 && trip_minutes < 10)
 
344
        {
 
345
                g_sprintf(buffer, "  %d:0%d:0%d",trip_hours,trip_minutes,trip_seconds);
 
346
        }
 
347
        else if (trip_seconds < 10)
 
348
                g_sprintf(buffer, "  %d:%d:0%d",trip_hours,trip_minutes,trip_seconds);
 
349
        else if (trip_minutes < 10)
 
350
                g_sprintf(buffer, "  %d:0%d:%d",trip_hours,trip_minutes,trip_seconds);
 
351
        else
 
352
                g_sprintf(buffer, "  %d:%d:%d",trip_hours,trip_minutes,trip_seconds);
 
353
 
 
354
        gtk_label_set_label(label66,buffer);
 
355
 
 
356
        
 
357
        g_sprintf(buffer, " %.1f %s", trip_distance*3600*unit_conv/(trip_time+2.0), speedunit);
 
358
        gtk_label_set_label(label68,buffer);
 
359
 
 
360
        
 
361
        g_sprintf(buffer, " %.1f %s", trip_maxspeed*3.6*unit_conv, speedunit);
 
362
        gtk_label_set_label(label70,buffer);
 
363
 
 
364
        
 
365
        
 
366
 
 
367
}
 
368
 
 
369
 
 
370
 
 
371
void
 
372
parse_nmea_rmc(char *nmea)
 
373
{
 
374
        gchar **array;
 
375
        gchar lat_dec[3], lon_dec[4];
 
376
        double  lat_min, lat=0, lon_min,lon=0;
 
377
        gchar hour[3],min[3],sec[3],year[3],month[3],day[3];
 
378
        int i=0;
 
379
        
 
380
        typedef struct tm tm_t;
 
381
        
 
382
        static tm_t *tm = NULL;
 
383
 
 
384
        if(!tm) tm = g_new0(tm_t,1);
 
385
                
 
386
        array = g_strsplit(nmea,",",0);
 
387
 
 
388
        while (array[i]) i++;
 
389
 
 
390
        g_source_remove(watchdog);
 
391
        watchdog = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,10000,reset_gpsd_io,NULL,NULL);
 
392
 
 
393
 
 
394
        
 
395
        if(i>=9)
 
396
        {
 
397
                
 
398
                if (strlen(array[1]) >= 6)
 
399
                {
 
400
                        strncpy(hour, array[1],2);
 
401
                        hour[2]='\0';
 
402
                        strncpy(min, array[1]+2,2);
 
403
                        min[2]='\0';
 
404
                        strncpy(sec, array[1]+4,2);
 
405
                        sec[2]='\0';
 
406
                }
 
407
                
 
408
                
 
409
                if (strlen(array[9]) == 6)
 
410
                {
 
411
                        strncpy(day, array[9],2);
 
412
                        day[2]='\0';
 
413
                        strncpy(month, array[9]+2,2);
 
414
                        month[2]='\0';
 
415
                        strncpy(year, array[9]+4,2);
 
416
                        year[2]='\0';
 
417
 
 
418
                        tm->tm_sec = atoi(sec);
 
419
                        tm->tm_min = atoi(min);
 
420
                        tm->tm_hour= atoi(hour);
 
421
                        tm->tm_mday= atoi(day);
 
422
                        tm->tm_mon = atoi(month)-1;
 
423
                        tm->tm_year= atoi(year)+100;
 
424
        
 
425
                        
 
426
                        gpsdata->fix.time = (double) mktime(tm);
 
427
                        
 
428
                        
 
429
                        
 
430
                }
 
431
                
 
432
                
 
433
                if (strlen(array[3]) >= 3)
 
434
                {
 
435
                        strncpy(lat_dec, array[3], 2);
 
436
                        lat_dec[2]='\0';
 
437
                        lat_min = atof(array[3]+2);
 
438
                        lat = atof(lat_dec) + (lat_min/60);
 
439
                        if (strcmp(array[4],"S")==0)
 
440
                        {
 
441
                                lat = -lat;
 
442
                        }
 
443
                }
 
444
                
 
445
                
 
446
                if (strlen(array[5]) >= 4)
 
447
                {
 
448
                        strncpy(lon_dec, array[5], 3);
 
449
                        lon_dec[3]='\0';
 
450
                        lon_min = atof(array[5]+3);
 
451
                        lon = atof(lon_dec) + (lon_min/60);
 
452
                        if (strcmp(array[6],"W")==0)
 
453
                        {
 
454
                                lon = -lon;
 
455
                        }
 
456
 
 
457
                }
 
458
                
 
459
                gpsdata->valid = (strcmp(array[2],"A")==0) ? TRUE : FALSE;
 
460
                
 
461
                if(gpsdata->valid)
 
462
                {
 
463
                        gpsdata->seen_vaild = TRUE;
 
464
                        gpsdata->fix.latitude = lat;
 
465
                        gpsdata->fix.longitude = lon;
 
466
                        gpsdata->fix.speed = atof(array[7])*0.514444; 
 
467
                        gpsdata->fix.track = atof(array[8]);
 
468
                }
 
469
        }
 
470
        else
 
471
                printf("%s(): YIKES. not enough fields. GPS receiver broken?\n",__PRETTY_FUNCTION__);
 
472
        
 
473
        g_strfreev(array);
 
474
 
 
475
}
 
476
 
 
477
 
 
478
 
 
479
void
 
480
parse_nmea_gga(char *nmea)
 
481
{               
 
482
        gchar **array;
 
483
        int i=0;
 
484
 
 
485
 
 
486
        array = g_strsplit(nmea,",",0);
 
487
 
 
488
        
 
489
        while (array[i]) {
 
490
                
 
491
                i++;
 
492
        }
 
493
 
 
494
        
 
495
 
 
496
        if(i>=9)
 
497
        {
 
498
                gpsdata->satellites_used = atoi(array[7]);
 
499
                
 
500
                if(atoi(array[6])>0)  
 
501
                {
 
502
                        gpsdata->hdop = atof(array[8]);
 
503
                        gpsdata->fix.altitude = atof(array[9]);
 
504
                }
 
505
        }
 
506
        g_strfreev(array);
 
507
 
 
508
}
 
509
 
 
510
void
 
511
parse_nmea_gsv(char *nmea)
 
512
{               
 
513
        gchar **array;
 
514
        int i=0;
 
515
        
 
516
 
 
517
        
 
518
        array = g_strsplit(nmea,",",0);
 
519
 
 
520
        while (array[i]) i++;
 
521
                
 
522
 
 
523
        
 
524
        
 
525
        if (i>=3)
 
526
                gpsdata->satellites_inview = atoi(array[3]);
 
527
 
 
528
        g_strfreev(array);
 
529
 
 
530
}
 
531
 
 
532
 
 
533
static gboolean
 
534
cb_gpsd_io_error(GIOChannel *src, GIOCondition condition, gpointer data)
 
535
{
 
536
        printf("*** %s(): \n",__PRETTY_FUNCTION__);
 
537
        g_free(gpsdata);
 
538
        gpsdata = NULL;
 
539
        g_source_remove(sid1); 
 
540
        g_source_remove(sid3); 
 
541
        
 
542
        
 
543
        return FALSE; 
 
544
}
 
545
 
 
546
 
 
547
 
 
548
static gboolean
 
549
cb_gpsd_data(GIOChannel *src, GIOCondition condition, gpointer data)
 
550
{
 
551
 
 
552
        gsize length;
 
553
        GError *error = NULL;
 
554
        gchar *str_return;
 
555
        GIOStatus status;
 
556
 
 
557
        
 
558
 
 
559
        status =  g_io_channel_read_line(
 
560
                                gpsd_io_channel,
 
561
                                &str_return,
 
562
                                &length,
 
563
                                NULL,
 
564
                                &error);
 
565
 
 
566
        if(status == G_IO_STATUS_NORMAL)
 
567
        {               
 
568
        
 
569
                if (strncmp(str_return,"$GPGSV",6)==0)
 
570
                {
 
571
                        parse_nmea_gsv(str_return);
 
572
                }
 
573
                else if(strncmp(str_return,"$GPGGA",6)==0)
 
574
                {
 
575
                        parse_nmea_gga(str_return);
 
576
                }
 
577
                else if (strncmp(str_return,"$GPRMC",6)==0)
 
578
                {
 
579
                        parse_nmea_rmc(str_return);
 
580
                }
 
581
 
 
582
                
 
583
                
 
584
        }
 
585
        else
 
586
        {
 
587
                if (error)
 
588
                        printf("%s \n", error->message);
 
589
                
 
590
                
 
591
        }
 
592
        
 
593
        g_free(str_return);
 
594
 
 
595
        return TRUE;
 
596
}
 
597
 
 
598
void
 
599
get_gps()
 
600
{
 
601
        int res;
 
602
 
 
603
        
 
604
        static int sock = 0;
 
605
        int conn, len;
 
606
        char buffer[501]; 
 
607
        struct sockaddr_in server;
 
608
        char buffer_send[] = "r";
 
609
 
 
610
 
 
611
        if(!gpsdata  || global_reconnect_gpsd)
 
612
        {
 
613
                printf("reconnecting to gpsd\n");
 
614
                                
 
615
                if (sock) sock = close(sock);
 
616
                        
 
617
                
 
618
                server.sin_family       = AF_INET;
 
619
                server.sin_addr.s_addr  = inet_addr(global_server);
 
620
                server.sin_port         = htons (atoi(global_port));
 
621
                memset(&(server.sin_zero), '\0', 8);            
 
622
                
 
623
                
 
624
                sock = socket(AF_INET, SOCK_STREAM, 0);
 
625
                conn = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
 
626
                if (conn < 0)
 
627
                {
 
628
                        fprintf(stderr, "connection to gpsd FAILED \n");
 
629
                }
 
630
                else
 
631
                {
 
632
                        fprintf(stderr, "connection to gpsd SUCCEEDED \n");
 
633
                        
 
634
                        global_reconnect_gpsd = FALSE;
 
635
                        
 
636
                        if(!gpsdata)
 
637
                        {
 
638
                                gpsdata = g_new0(gps_data_t,1);
 
639
                        }
 
640
                        
 
641
                        len = write(sock, buffer_send, strlen(buffer_send));
 
642
                        if (len < 0)
 
643
                                perror("ERROR writing to socket");
 
644
                        
 
645
                        
 
646
                        len = recv(sock, buffer, 500, 0);
 
647
                        
 
648
                        buffer[len]='\0'; 
 
649
                        
 
650
                        fprintf(stderr, "Rcvd: %s",buffer);
 
651
                
 
652
                        
 
653
                        
 
654
                        
 
655
                        watchdog = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,10000,reset_gpsd_io,NULL,NULL);
 
656
                        
 
657
                        
 
658
                        gpsd_io_channel = g_io_channel_unix_new(sock);
 
659
                        g_io_channel_set_flags(gpsd_io_channel, G_IO_FLAG_NONBLOCK, NULL);
 
660
                        
 
661
                        
 
662
                        sid1 = g_io_add_watch_full(gpsd_io_channel, G_PRIORITY_HIGH_IDLE+200, G_IO_ERR | G_IO_HUP, cb_gpsd_io_error, NULL, NULL);
 
663
                        
 
664
                        
 
665
                        sid3 = g_io_add_watch_full(gpsd_io_channel, G_PRIORITY_HIGH_IDLE+200, G_IO_IN | G_IO_PRI, cb_gpsd_data, NULL, NULL);
 
666
                
 
667
                        
 
668
                }
 
669
                        
 
670
        }
 
671
        
 
672
        
 
673
        if(gpsdata)
 
674
        {
 
675
                
 
676
                
 
677
 
 
678
                res = 5;
 
679
 
 
680
                if (res==-1){
 
681
                        printf("POLL ERROR \n");
 
682
                        gpsdata=NULL;
 
683
                }
 
684
                
 
685
        
 
686
        }
 
687
        else printf("NOGPS");
 
688
 
 
689
        
 
690
 
 
691
}
 
692
 
 
693
void
 
694
map_scale_indicator()
 
695
{
 
696
        printf("%s():\n",__PRETTY_FUNCTION__);
 
697
        
 
698
        
 
699
        
 
700
        
 
701
        
 
702
 
 
703
        int y, width, height, bar_length=200;
 
704
        float distance, factor, lat, lon1, lon2;
 
705
        char *buffer=NULL;
 
706
        GdkColor color;
 
707
        static GdkGC *gc=NULL, *gc1=NULL;
 
708
        
 
709
        PangoContext            *context = NULL;
 
710
        PangoLayout             *layout  = NULL;
 
711
        PangoFontDescription    *desc    = NULL;
 
712
        
 
713
        y = map_drawable->allocation.height - 8;
 
714
        
 
715
        if(gc == NULL)
 
716
        {
 
717
                gc   = gdk_gc_new(pixmap);
 
718
                gc1  = gdk_gc_new(pixmap);
 
719
        }
 
720
        color.red   = 0;
 
721
        color.green = 0;
 
722
        color.blue  = 0;
 
723
        
 
724
        gdk_gc_set_rgb_fg_color(gc, &color);
 
725
        gdk_gc_set_line_attributes(gc,
 
726
                        5, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
 
727
 
 
728
        color.red   = 65000;
 
729
        color.green = 65000;
 
730
        color.blue  = 65000;
 
731
        
 
732
        gdk_gc_set_rgb_fg_color(gc1, &color);
 
733
        gdk_gc_set_line_attributes(gc1,
 
734
                        3, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
 
735
        
 
736
 
 
737
 
 
738
        lat   = pixel2lat(global_zoom, global_y+y);
 
739
        lon1  = pixel2lon(global_zoom, 0);
 
740
        lon2  = pixel2lon(global_zoom, 200);
 
741
 
 
742
        distance = get_distance(lat, lon1, lat, lon2);
 
743
        
 
744
        buffer = distance2scale(distance, &factor);
 
745
        bar_length *= factor;
 
746
        
 
747
        gdk_draw_line(map_drawable->window, gc, 4, y, bar_length+6, y);
 
748
        gdk_draw_line(map_drawable->window, gc1,5, y, bar_length+5, y); 
 
749
 
 
750
        
 
751
        context = gtk_widget_get_pango_context (map_drawable);
 
752
        layout  = pango_layout_new (context);
 
753
        desc    = pango_font_description_new();
 
754
        
 
755
        pango_font_description_set_size (desc, 9 * PANGO_SCALE);
 
756
        pango_layout_set_font_description (layout, desc);
 
757
        pango_layout_set_text (layout, buffer, strlen(buffer));
 
758
 
 
759
 
 
760
        
 
761
        pango_layout_get_pixel_size(layout, &width, &height);
 
762
 
 
763
        
 
764
        
 
765
        gdk_gc_set_line_attributes(gc,
 
766
                        height+2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);        
 
767
 
 
768
        gdk_gc_set_line_attributes(gc1,
 
769
                        height, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);  
 
770
 
 
771
        gdk_draw_line(map_drawable->window, gc1, 22, y-height/2+6, 20+width+6, y-height/2+6);
 
772
 
 
773
     
 
774
 
 
775
                gdk_draw_layout(map_drawable->window,
 
776
                                gc,
 
777
                                25, y-height+7,
 
778
                                layout);
 
779
        
 
780
 
 
781
 
 
782
        g_free(buffer);
 
783
        pango_font_description_free (desc);
 
784
        g_object_unref (layout);
 
785
        
 
786
        
 
787
        
 
788
}
 
789
 
 
790
char *
 
791
distance2scale(float distance, float *factor)
 
792
{
 
793
        float unit_conv=1;
 
794
        char *buf = NULL;
 
795
        char unit[5];
 
796
        
 
797
        switch (global_speed_unit)
 
798
        {
 
799
                case METRICAL:
 
800
                        unit_conv = 1.0;
 
801
                        strncpy(unit, "km", 3);
 
802
                        break;
 
803
                case IMPERIAL :
 
804
                        unit_conv = 1.0/1.609344;
 
805
                        strncpy(unit, "m", 2);
 
806
                        break;
 
807
                case NAUTICAL :
 
808
                        unit_conv = 1.0 / 1.852;
 
809
                        strncpy(unit, "NM", 3);
 
810
                        break;          
 
811
        }
 
812
        
 
813
        distance *= unit_conv;
 
814
        if (distance >= 5000) {
 
815
                buf = g_strconcat("5000", unit, NULL);
 
816
                *factor = 5000/distance;
 
817
        }
 
818
        else if (distance < 5000 && distance >= 2000) {
 
819
                buf = g_strconcat("2000", unit, NULL);
 
820
                *factor = 2000/distance;
 
821
        }
 
822
        else if (distance < 2000 && distance >= 1000) {
 
823
                buf = g_strconcat("1000", unit, NULL);
 
824
                *factor = 1000/distance;
 
825
        }
 
826
        else if (distance < 1000 && distance >= 500) {
 
827
                buf = g_strconcat("500", unit, NULL);
 
828
                *factor = 500/distance;
 
829
        }
 
830
        else if (distance < 500 && distance >= 200) {
 
831
                buf = g_strconcat("200", unit, NULL);
 
832
                *factor = 200/distance;
 
833
        }
 
834
        else if (distance < 200 && distance >= 100) {
 
835
                buf = g_strconcat("100", unit, NULL);
 
836
                *factor = 100/distance;
 
837
        }
 
838
        else if (distance < 100 && distance >= 50) {
 
839
                buf = g_strconcat("50", unit, NULL);
 
840
                *factor = 50/distance;
 
841
        }
 
842
        else if (distance < 50 && distance >= 20) {
 
843
                buf = g_strconcat("20", unit, NULL);
 
844
                *factor = 20/distance;
 
845
        }
 
846
        else if (distance < 20 && distance >= 10) {
 
847
                buf = g_strconcat("10", unit, NULL);
 
848
                *factor = 10/distance;
 
849
        }
 
850
        else if (distance < 10 && distance >= 5) {
 
851
                buf = g_strconcat("5", unit, NULL);
 
852
                *factor = 5/distance;
 
853
        }
 
854
        else if (distance < 5 && distance >= 2) {
 
855
                buf = g_strconcat("2", unit, NULL);
 
856
                *factor = 2/distance;
 
857
        }
 
858
        else if (distance < 2 && distance >= 1) {
 
859
                buf = g_strconcat("1", unit, NULL);
 
860
                *factor = 1/distance;
 
861
        }
 
862
        else if(global_speed_unit == METRICAL)
 
863
        {
 
864
                if (distance < 1 && distance >= 0.5) {
 
865
                        buf = g_strdup("500m");
 
866
                        *factor = 0.5/distance;
 
867
                }
 
868
                else if (distance < 0.5 && distance >= 0.2) {
 
869
                        buf = g_strdup("200m");
 
870
                        *factor = 0.2/distance;
 
871
                }
 
872
                else if (distance < 0.2 && distance >= 0.1) {
 
873
                        buf = g_strdup("100m");
 
874
                        *factor = 0.1/distance;
 
875
                }
 
876
                else if (distance < 0.1 && distance >= 0.05) {
 
877
                        buf = g_strdup("50m");
 
878
                        *factor = 0.05/distance;
 
879
                }
 
880
                else if (distance < 0.05 && distance >= 0.02) {
 
881
                        buf = g_strdup("20m");
 
882
                        *factor = 0.02/distance;
 
883
                }
 
884
                else {
 
885
                        buf = g_strdup("10m");
 
886
                        *factor = 0.01/distance;
 
887
                }
 
888
        }
 
889
        else if(global_speed_unit == IMPERIAL)
 
890
        {
 
891
                distance *= 5280;
 
892
                if (distance >= 5000) {
 
893
                        buf = g_strdup("5000ft");
 
894
                        *factor = 5000/distance;
 
895
                }
 
896
                else if (distance < 5000 && distance >= 2000) {
 
897
                        buf = g_strdup("2000ft");
 
898
                        *factor = 2000/distance;
 
899
                }
 
900
                else if (distance < 2000 && distance >= 1000) {
 
901
                        buf = g_strdup("1000ft");
 
902
                        *factor = 1000/distance;
 
903
                }
 
904
                else if (distance < 1000 && distance >= 500) {
 
905
                        buf = g_strdup("500ft");
 
906
                        *factor = 500/distance;
 
907
                }
 
908
                else if (distance < 500 && distance >= 200) {
 
909
                        buf = g_strdup("200ft");
 
910
                        *factor = 200/distance;
 
911
                }
 
912
                else {
 
913
                        buf = g_strdup("100ft");
 
914
                        *factor = 100/distance;
 
915
                }
 
916
        }
 
917
        else if(global_speed_unit == NAUTICAL)
 
918
        {
 
919
                if (distance < 1 && distance >= 0.5) {
 
920
                        buf = g_strdup("0.5NM");
 
921
                        *factor = 0.5/distance;
 
922
                }
 
923
                else if (distance < 0.5 && distance >= 0.2) {
 
924
                        buf = g_strdup("0.2NM");
 
925
                        *factor = 0.2/distance;
 
926
                }
 
927
                else if (distance < 0.2 && distance >= 0.1) {
 
928
                        buf = g_strdup("0.1NM");
 
929
                        *factor = 0.1/distance;
 
930
                }
 
931
                else if (distance < 0.1 && distance >= 0.05) {
 
932
                        buf = g_strdup("0.05NM");
 
933
                        *factor = 0.05/distance;
 
934
                }
 
935
                else if (distance < 0.05 && distance >= 0.02) {
 
936
                        buf = g_strdup("0.02NM");
 
937
                        *factor = 0.02/distance;
 
938
                }
 
939
                else {
 
940
                        buf = g_strdup("0.01NM");
 
941
                        *factor = 0.01/distance;
 
942
                }
 
943
        }
 
944
 
 
945
        if(!buf)
 
946
                buf=g_strdup("bingo");
 
947
 
 
948
        return buf;
 
949
}