~foxtrotgps-team/foxtrotgps/trunk

« back to all changes in this revision

Viewing changes to src/gps_functions.c

  • Committer: Joshua Judson Rosen
  • Author(s): Marcus Bauer
  • Date: 2010-08-08 22:28:45 UTC
  • Revision ID: rozzin@geekspace.com-20100808222845-86w25l3qq5nlf5ib
Perl scripts to convert track-logs from tangoGPS format to GPX and OSM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "converter.h"
25
25
#include "wp.h"
26
26
#include "tracks.h"
27
 
#include "hrm_functions.h"
28
 
 
29
 
#if GPSD_API_MAJOR_VERSION < 5
30
 
#define gps_read gps_poll
31
 
#define gps_open gps_open_r
32
 
#endif
33
27
 
34
28
#define BUFSIZE 512
35
29
char * distance2scale(float distance, float *factor);
36
 
static void * get_gps_thread(void *ptr);
 
30
void * get_gps_thread(void *ptr);
37
31
 
38
32
 
39
33
static GIOChannel *gpsd_io_channel =NULL;
40
 
static struct gps_data_t libgps_gpsdata;
41
 
static gboolean libgps_initialized = FALSE;
42
 
 
43
 
gboolean reconnect_gpsd = TRUE;
44
 
 
45
 
static guint sid1 = 0;
46
 
static guint sid3 = 0;
47
 
static guint watchdog = 0;
48
 
static guint gps_timer = 0;
 
34
static struct gps_data_t *libgps_gpsdata = NULL;
 
35
 
 
36
static guint sid1,  sid3; 
 
37
guint watchdog;
49
38
 
50
39
gboolean
51
40
cb_gps_timer()
52
41
{
53
 
 
54
 
 
 
42
        
 
43
        
55
44
        int pixel_x, pixel_y, x, y, last_x, last_y;
56
45
        static float lat, lon, lat_tmp=0, lon_tmp=0;
57
46
        float trip_delta=0;
61
50
 
62
51
        GdkColor color;
63
52
        static GdkGC *gc=NULL, *gc_2=NULL, *gc_3=NULL, *gc_4=NULL, *gc_5=NULL;
64
 
 
 
53
        
65
54
        if(gc == NULL)
66
55
        {
67
56
                gc   = gdk_gc_new(pixmap);
69
58
                gc_3 = gdk_gc_new(pixmap);
70
59
                gc_4 = gdk_gc_new(pixmap);
71
60
                gc_5 = gdk_gc_new(pixmap);
72
 
 
73
 
 
 
61
                
 
62
                
74
63
                color.red = 60000;
75
64
                color.green = 0;
76
65
                color.blue = 0;
77
66
                gdk_gc_set_rgb_fg_color(gc, &color);
78
67
                gdk_gc_set_line_attributes(gc,
79
68
                                5, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
80
 
 
81
 
 
 
69
                
 
70
                
82
71
                color.red = 5000;
83
72
                color.green = 5000;
84
73
                color.blue = 55000;
85
74
                gdk_gc_set_rgb_fg_color(gc_2, &color);
86
75
                gdk_gc_set_line_attributes(gc_2,
87
76
                                6, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
88
 
 
89
 
 
90
 
                color.red = 25500;
 
77
        
 
78
                
 
79
                color.red = 25500; 
91
80
                color.green = 35000;
92
81
                color.blue = 65500;
93
82
                gdk_gc_set_rgb_fg_color(gc_3, &color);
94
83
                gdk_gc_set_line_attributes(gc_3,
95
84
                                7, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
96
 
 
97
 
 
98
 
                color.red = 35500;
 
85
                
 
86
                
 
87
                color.red = 35500; 
99
88
                color.green = 5000;
100
89
                color.blue = 500;
101
90
                gdk_gc_set_rgb_fg_color(gc_4, &color);
102
91
                gdk_gc_set_line_attributes(gc_4,
103
92
                                7, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
104
 
 
105
 
 
106
 
                color.red = 65500;
 
93
                                
 
94
                
 
95
                color.red = 65500; 
107
96
                color.green = 65500;
108
97
                color.blue = 65500;
109
98
                gdk_gc_set_rgb_fg_color(gc_5, &color);
110
99
                gdk_gc_set_line_attributes(gc_5,
111
100
                                11, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
112
 
 
 
101
                                
113
102
        }
114
 
 
115
 
 
116
 
        if(!gpsdata  || reconnect_gpsd)
 
103
        
 
104
        
 
105
        if(!gpsdata  || global_reconnect_gpsd)
117
106
                get_gps();
118
 
 
 
107
        
119
108
 
120
109
        if(gpsdata)
121
110
        {
122
111
                trackpoint_t *tp = g_new0(trackpoint_t,1);
123
 
 
 
112
                
124
113
                lat = deg2rad(gpsdata->fix.latitude);
125
 
                lon = deg2rad(gpsdata->fix.longitude);
126
 
 
127
 
 
 
114
                lon = deg2rad(gpsdata->fix.longitude);          
 
115
                
 
116
                
128
117
                pixel_x = lon2pixel(global_zoom, lon);
129
118
                pixel_y = lat2pixel(global_zoom, lat);
130
 
 
 
119
                
131
120
                x = pixel_x - global_x;
132
121
                y = pixel_y - global_y;
133
 
 
134
 
 
 
122
                
 
123
                
135
124
                pixel_x = lon2pixel(global_zoom, lon_tmp);
136
125
                pixel_y = lat2pixel(global_zoom, lat_tmp);
137
 
 
 
126
                
138
127
                last_x = pixel_x - global_x;
139
128
                last_y = pixel_y - global_y;
140
129
 
141
130
 
142
 
 
143
 
                if(gpsdata->seen_valid)
 
131
                
 
132
                if(gpsdata->seen_vaild)
144
133
                {
145
134
                        int hand_x, hand_y, hand_wp_x, hand_wp_y;
146
135
                        double heading_rad, bearing;
147
 
 
 
136
                        
148
137
                        heading_rad = (gpsdata->fix.heading * (1.0 / 180.0)) * M_PI;
149
138
 
150
 
                        if(gpsdata->fix.speed>0.3)
 
139
                        if(gpsdata->fix.speed>0.3) 
151
140
                        {
152
141
                                hand_x =  25 * sinf(heading_rad);
153
142
                                hand_y = -25 * cosf(heading_rad);
159
148
                        }
160
149
 
161
150
 
162
 
 
 
151
                        
163
152
                        gdk_draw_drawable (
164
153
                                map_drawable->window,
165
154
                                map_drawable->style->fg_gc[GTK_WIDGET_STATE (map_drawable)],
168
157
                                last_x-29 + mouse_dx, last_y-29 + mouse_dy,
169
158
                                58,58);
170
159
 
171
 
 
 
160
                
172
161
                        if (lat_tmp && lon_tmp)
173
162
                                gdk_draw_line(pixmap, gc, last_x, last_y, x, y);
174
 
 
175
 
 
 
163
                        
 
164
                        
176
165
                        gdk_window_process_all_updates();
177
166
 
178
 
 
 
167
                        
179
168
                        if(mouse_dx == 0 && mouse_dy == 0)
180
169
                        {
181
 
 
 
170
                                
182
171
                                gdk_draw_arc (
183
172
                                        map_drawable->window,
184
173
                                        gc_2,
185
 
                                        FALSE,
 
174
                                        FALSE,          
186
175
                                        x-15 + mouse_dx,
187
176
                                        y-15 + mouse_dy,
188
 
                                        30,30,
189
 
                                        0, 360*64);
190
 
 
191
 
 
 
177
                                        30,30,          
 
178
                                        0, 360*64);     
 
179
                                
 
180
                                
192
181
                                if(global_wp_on && gpsdata->valid)
193
182
                                {
194
 
 
 
183
                                        
195
184
                                        bearing = get_bearing(lat, lon, global_wp.lat, global_wp.lon);
196
185
                                        gpsdata->fix.bearing = bearing;
197
186
 
198
187
                                        hand_wp_x =  25 * sinf(bearing);
199
188
                                        hand_wp_y = -25 * cosf(bearing);
200
 
 
 
189
                                        
201
190
                                        gdk_draw_line(map_drawable->window,
202
191
                                                        gc_5,
203
192
                                                        x + mouse_dx,
211
200
                                                        y + mouse_dy,
212
201
                                                        x + mouse_dx + hand_wp_x,
213
202
                                                        y + mouse_dy + hand_wp_y);
214
 
 
 
203
                                        
215
204
                                        osd_wp();
216
 
 
 
205
                                        
217
206
                                }
218
 
 
219
 
 
 
207
                                
 
208
                                
220
209
                                gdk_draw_line(map_drawable->window,
221
210
                                                gc_5,
222
211
                                                x + mouse_dx,
223
212
                                                y + mouse_dy,
224
213
                                                x + mouse_dx + hand_x,
225
214
                                                y + mouse_dy + hand_y);
226
 
 
 
215
                                
227
216
                                gdk_draw_line(map_drawable->window,
228
217
                                                gc_3,
229
218
                                                x + mouse_dx,
232
221
                                                y + mouse_dy + hand_y);
233
222
                        }
234
223
                }
235
 
 
236
 
 
 
224
                
 
225
                
237
226
                if(global_autocenter)
238
227
                {
239
228
                        if(    (x < (global_drawingarea_width /2 - global_drawingarea_width /8) ||
240
229
                                x > (global_drawingarea_width /2 + global_drawingarea_width /8) ||
241
230
                                y < (global_drawingarea_height /2 - global_drawingarea_height /8) ||
242
231
                                y > (global_drawingarea_height /2 + global_drawingarea_height /8) ) &&
243
 
 
244
 
 
 
232
                        
 
233
                                
245
234
                                isnan(gpsdata->fix.latitude) ==0 &&
246
235
                                isnan(gpsdata->fix.longitude)==0 &&
247
236
                                gpsdata->fix.latitude  !=0 &&
251
240
                                set_mapcenter(gpsdata->fix.latitude, gpsdata->fix.longitude, global_zoom);
252
241
                        }
253
242
                }
254
 
 
255
 
 
256
 
 
257
 
 
258
 
 
 
243
                
 
244
 
 
245
                
 
246
 
 
247
                
259
248
                if( gpsdata->valid && lat_tmp!=0 && lon_tmp!=0)
260
249
                {
261
 
                        trip_delta = 6371.0 *  acos(sin(lat) * sin(lat_tmp) +
 
250
                        trip_delta = 6371.0 *  acos(sin(lat) * sin(lat_tmp) + 
262
251
                                     cos(lat) * cos(lat_tmp) * cos(lon_tmp-lon) );
263
 
 
 
252
                        
264
253
                        if(isnan(trip_delta))
265
254
                        {
266
 
 
 
255
                                
267
256
                                trip_delta = 0;
268
257
                        }
269
 
 
 
258
                        
270
259
                        if(trip_delta > TRIP_DELTA_MIN)
271
260
                        {
272
261
                                        tp->time    = gpsdata->fix.time;
273
262
                                        tp->lat     = lat;
274
263
                                        tp->lon     = lon;
275
264
                                        tp->lat_deg = gpsdata->fix.latitude;
276
 
                                        tp->lon_deg = gpsdata->fix.longitude;
 
265
                                        tp->lat_deg = gpsdata->fix.longitude;
277
266
                                        tp->alt     = gpsdata->fix.altitude;
278
267
                                        tp->speed   = gpsdata->fix.speed;
279
268
                                        tp->head    = gpsdata->fix.heading;
280
269
                                        tp->hdop    = gpsdata->hdop;
281
270
                                        tp->heart   = 0;
282
 
 
 
271
                                        
283
272
                                        if (trip_delta > SEGMENT_DISTANCE)
284
273
                                                tp->hdop = 999;
285
274
 
286
 
 
 
275
                                        
287
276
                                        if (trackpoint_list->length > TRACKPOINT_LIST_MAX_LENGTH)
288
277
                                                g_free(g_queue_pop_head(trackpoint_list));
289
 
 
 
278
                                                
290
279
                                        g_queue_push_tail(trackpoint_list, tp);
291
280
                        }
292
281
                }
293
 
 
 
282
                        
294
283
                if(trip_counter_on)
295
284
                {
296
285
                        trip_distance += trip_delta;
297
286
 
298
 
 
 
287
                        
299
288
                        if(gpsdata->valid && gpsdata->fix.speed > trip_maxspeed)
300
289
                                trip_maxspeed = gpsdata->fix.speed;
301
 
 
302
 
 
303
 
 
304
 
 
305
 
                        if(trip_time == 0)
 
290
                        
 
291
                        
 
292
                        
 
293
                        
 
294
                        if(trip_time == 0) 
306
295
                                trip_time_accumulated = 0;
307
 
 
 
296
                        
308
297
                        if(trip_counter_got_stopped)
309
298
                        {
 
299
                                printf("counter had been stopped \n");
310
300
                                trip_counter_got_stopped = FALSE;
311
301
                                trip_time_accumulated = trip_time;
312
302
                                trip_starttime = 0;
313
303
                        }
314
 
 
315
 
 
316
 
                        if(trip_starttime == 0 && gpsdata->seen_valid)
 
304
                        
 
305
                        
 
306
                        if(trip_starttime == 0 && gpsdata->seen_vaild)
317
307
                        {
318
308
                                trip_starttime = gpsdata->fix.time;
319
309
                        }
320
 
 
321
 
 
322
 
                        if(trip_starttime > 0 && gpsdata->seen_valid)
 
310
                        
 
311
                        
 
312
                        if(trip_starttime > 0 && gpsdata->seen_vaild)
323
313
                        {
324
 
                                trip_time = gpsdata->fix.time - trip_starttime + trip_time_accumulated;
 
314
                                trip_time = gpsdata->fix.time - trip_starttime + trip_time_accumulated;                         
325
315
                        }
326
 
 
 
316
                        
327
317
                        if(trip_time < 0)
328
318
                        {
329
319
                                trip_time = 0;
336
326
 
337
327
                else
338
328
                {
 
329
                        printf("trip counter halted\n");
339
330
                        trip_counter_got_stopped = TRUE;
340
331
                        lat_tmp = lon_tmp = 0;
341
332
                }
342
 
 
343
 
 
344
 
                if (global_new_msg)
345
 
                        set_label_newmsg ();
346
 
                else
347
 
                        set_label ();
348
 
 
 
333
                
 
334
                
 
335
                
 
336
                set_label();
 
337
                
 
338
                
349
339
                if(trip_logger_on && gpsdata->valid)
350
340
                        track_log();
351
 
 
 
341
                
352
342
                if(gpsdata->valid)
353
 
                {
 
343
                {       
354
344
                        lat_tmp = lat;
355
345
                        lon_tmp = lon;
356
346
                }
357
347
        }
358
 
        else
 
348
        else 
359
349
        {
360
 
                if (global_new_msg)
361
 
                        set_label_newmsg ();
362
 
                else
363
 
                        set_label_nogps();
364
 
        }
365
 
 
366
 
        if(hrm_on && (!hrmdata  || global_reconnect_hrm))
367
 
                get_hrm_data();
368
 
        else if(hrm_on && hrmdata) {
369
 
                osd_hrm(FALSE);
370
 
                if(global_infopane_visible)
371
 
                        set_hrm_labels();
372
 
        }
373
 
 
374
 
        return TRUE;
 
350
                set_label_nogps();
 
351
        }
 
352
        
 
353
        return TRUE; 
375
354
}
376
355
 
377
356
gboolean
378
357
reset_gpsd_io()
379
358
{
380
 
        reconnect_gpsd = TRUE;
 
359
        printf("*** %s(): \n",__PRETTY_FUNCTION__);
 
360
        
 
361
        global_reconnect_gpsd = TRUE;
 
362
        g_source_remove(watchdog);
381
363
 
382
 
        return FALSE;
 
364
        g_source_remove(sid1); 
 
365
        g_source_remove(sid3); 
 
366
        
 
367
        return FALSE;   
383
368
}
384
369
 
385
370
 
386
371
void
387
372
osd_speed(gboolean force_redraw)
388
373
{
389
 
 
 
374
        
390
375
        PangoContext            *context = NULL;
391
376
        PangoLayout             *layout  = NULL;
392
377
        PangoFontDescription    *desc    = NULL;
393
 
 
 
378
        
394
379
        GdkColor color;
395
380
        GdkGC *gc;
396
 
 
 
381
        
397
382
        gchar *buffer;
398
383
        static int x = 10, y = 10;
399
384
        static int width = 0, height = 0;
400
385
 
401
386
        static double speed_tmp = 0;
402
 
 
 
387
        
403
388
        double unit_conv = 1;
404
 
 
405
 
        if(gpsdata && mouse_dx == 0 && mouse_dy == 0)
 
389
                
 
390
        if(gpsdata && mouse_dx == 0 && mouse_dy == 0) 
406
391
        {
407
392
                switch (global_speed_unit)
408
393
                {
414
399
                                break;
415
400
                        case 2 :
416
401
                                unit_conv = 1.0 / 1.852;
417
 
                                break;
 
402
                                break;          
418
403
                }
419
 
 
420
 
 
421
 
 
 
404
                
 
405
 
 
406
        
422
407
                buffer = g_strdup_printf("%.0f", gpsdata->fix.speed*3.6*unit_conv);
423
 
 
424
 
 
 
408
                
 
409
                
425
410
                context = gtk_widget_get_pango_context (map_drawable);
426
411
                layout  = pango_layout_new (context);
427
412
                desc    = pango_font_description_new();
428
 
 
 
413
                
429
414
                pango_font_description_set_absolute_size (desc, 80 * PANGO_SCALE);
430
415
                pango_layout_set_font_description (layout, desc);
431
416
                pango_layout_set_text (layout, buffer, strlen(buffer));
432
 
 
433
 
 
 
417
        
 
418
        
434
419
                gc = gdk_gc_new (map_drawable->window);
435
 
 
 
420
        
436
421
                color.red = (gpsdata->fix.speed*3.6*unit_conv > 50) ? 0xffff : 0;
437
422
                color.green = 0;
438
423
                color.blue = 0;
439
 
 
 
424
                
440
425
                gdk_gc_set_rgb_fg_color (gc, &color);
441
426
 
442
 
 
443
 
 
 
427
                
 
428
                
444
429
                if(speed_tmp != floor(gpsdata->fix.speed*3.6*unit_conv) || force_redraw)
445
430
                {
446
 
 
 
431
                        
447
432
                        gdk_draw_drawable (
448
433
                                map_drawable->window,
449
434
                                map_drawable->style->fg_gc[GTK_WIDGET_STATE (map_drawable)],
451
436
                                0,0,
452
437
                                0, 0,
453
438
                                width+10,height+10);
454
 
 
455
 
 
456
 
                        if(gpsdata->fix.speed>0.01 && gpsdata->valid)
 
439
                
 
440
                        
 
441
                        if(gpsdata->fix.speed>0.01 && gpsdata->valid) 
457
442
                                gdk_draw_layout(map_drawable->window,
458
443
                                                gc,
459
444
                                                x, y,
460
445
                                                layout);
461
 
 
462
 
 
 
446
                        
 
447
                        
463
448
                        pango_layout_get_pixel_size(layout, &width, &height);
464
449
                }
465
 
 
 
450
                
466
451
                speed_tmp = floor(gpsdata->fix.speed*3.6*unit_conv);
467
 
 
 
452
                
468
453
                g_free(buffer);
469
454
                pango_font_description_free (desc);
470
455
                g_object_unref (layout);
471
456
                g_object_unref (gc);
472
457
        }
473
458
}
474
 
 
475
 
void
476
 
set_label_newmsg()
477
 
{
478
 
        static GtkLabel *label = NULL;
479
 
 
480
 
        if (label == NULL)
481
 
        {
482
 
                label = GTK_LABEL(lookup_widget(window1, "label4"));
483
 
        }
484
 
 
485
 
        gtk_label_set_label (label,
486
 
                             _("<span foreground='#ff0000'><b>"
487
 
                               "New Message arrived. Click here."
488
 
                               "</b></span>"));
489
 
}
490
 
 
491
459
void
492
460
set_label_nogps()
493
461
{
497
465
 
498
466
        if(label == NULL)
499
467
                label   = GTK_LABEL(lookup_widget(window1, "label4"));
500
 
 
 
468
        
501
469
        num_dl_threads = update_thread_number(0);
502
470
        if(num_dl_threads && !global_tiles_in_dl_queue)
503
 
        {
504
 
                g_snprintf (buffer, BUFSIZE,
505
 
                            _("<b>no GPSD found</b>"
506
 
                              " - "
507
 
                              "<span foreground='#0000ff'><b>D%d</b></span>"),
 
471
        {       
 
472
                g_snprintf(buffer, BUFSIZE,
 
473
                        "<b>no GPSD found</b> - <span foreground='#0000ff'><b>D%d</b></span>",
508
474
                        num_dl_threads);
509
475
        }
510
476
        else if (num_dl_threads && global_tiles_in_dl_queue)
511
 
                g_snprintf (buffer, BUFSIZE,
512
 
                            _("<b>no GPSD found</b>"
513
 
                              " - "
514
 
                              "<span foreground='#0000ff'><b>D%d</b></span>"
515
 
                              " - "
516
 
                              "<b>[%d]</b>"),
 
477
                g_snprintf(buffer, BUFSIZE,
 
478
                        "<b>no GPSD found</b> - <span foreground='#0000ff'><b>D%d</b></span> - <b>[%d]</b>",
517
479
                        num_dl_threads, global_tiles_in_dl_queue);
518
480
        else
519
 
                g_snprintf (buffer, BUFSIZE, _("<b>no GPSD found</b>"));
520
 
 
 
481
                g_snprintf(buffer, BUFSIZE, "<b>no GPSD found</b>");
 
482
        
 
483
        if(global_new_msg)
 
484
                g_snprintf(buffer, BUFSIZE, "<span foreground='#ff0000'><b>New Message arrived. Click here.</b></span>");
 
485
        
521
486
        gtk_label_set_label(label, buffer);
522
487
 
523
488
}
535
500
        time_t time_sec;
536
501
        struct tm  *ts;
537
502
        double unit_conv = 1, unit_conv_alt = 1;
538
 
 
 
503
        
539
504
        osd_speed(FALSE);
540
 
 
 
505
        
541
506
        if(global_speed_unit==1)
542
507
        {
543
508
                unit_conv = 1.0/1.609344;
555
520
                g_sprintf(speedunit, "%s","km/h");
556
521
                g_sprintf(distunit, "%s", "km");
557
522
        }
558
 
 
559
 
 
 
523
        
 
524
        
560
525
        if(global_alt_unit==1)
561
526
        {
562
527
                unit_conv_alt = 1.0/0.3048;
564
529
        }
565
530
        else
566
531
                g_sprintf(altunit, "%s", "m");
567
 
 
 
532
        
568
533
        if(global_auto_download)
569
534
                g_sprintf(dl_buf, "%s", "");
570
535
        else
571
536
                g_sprintf(dl_buf, "%s", "<span foreground='#ff0000'><b>!</b></span>");
572
 
 
 
537
        
573
538
        if (global_fftimer_running)
574
539
                g_sprintf(ff_buf, "%s", "<span foreground='#00e000'><b>f</b></span>");
575
540
        else
576
541
                g_sprintf(ff_buf, "%s", "");
577
 
 
 
542
        
578
543
        if (trip_logger_on)
579
544
                g_sprintf(tr_buf, "%s", "<span foreground='#00e000'><b>t</b></span>");
580
545
        else
581
546
                g_sprintf(tr_buf, "%s", "");
582
 
 
 
547
        
583
548
        if(label == NULL)
584
549
        {
585
550
                label   = GTK_LABEL(lookup_widget(window1, "label4"));
596
561
        }
597
562
 
598
563
 
599
 
 
600
 
 
601
 
 
 
564
        
 
565
        
 
566
        
602
567
        num_dl_threads = update_thread_number(0);
603
 
 
 
568
        
604
569
        if(num_dl_threads && !global_tiles_in_dl_queue)
605
 
                g_sprintf(numdl_buf, "<span foreground='#0000ff'><b>D%d</b></span> ",
 
570
                g_sprintf(numdl_buf, "<span foreground='#0000ff'><b>D%d</b></span> ", 
606
571
                          num_dl_threads);
607
572
        else if (num_dl_threads && global_tiles_in_dl_queue)
608
 
                g_sprintf(numdl_buf, "<span foreground='#0000ff'><b>D%d</b></span>-%d ",
 
573
                g_sprintf(numdl_buf, "<span foreground='#0000ff'><b>D%d</b></span>-%d ", 
609
574
                          num_dl_threads, global_tiles_in_dl_queue);
610
575
        else
611
576
                g_sprintf(numdl_buf, "%s", "");
612
 
 
 
577
        
613
578
 
614
579
        g_snprintf(buffer, BUFSIZE,
615
 
                   _("%s%s%s%s<b>%4.1f</b>%s "
616
 
                     "<small>trp </small><b>%.2f</b>%s "
617
 
                     "<small>alt </small><b>%.0f</b>%s "
618
 
                     "<small>hdg </small><b>%.0f</b>° "
619
 
                     "<small></small>%d/%.1f"),
620
 
                     numdl_buf, dl_buf, tr_buf, ff_buf,
621
 
                     gpsdata->fix.speed * 3.6 * unit_conv, speedunit,
622
 
                     trip_distance * unit_conv, distunit,
623
 
                     gpsdata->fix.altitude * unit_conv_alt, altunit,
624
 
                     gpsdata->fix.heading,
625
 
                     gpsdata->satellites_used,
626
 
                     gpsdata->hdop);
 
580
                "%s%s%s%s<b>%4.1f</b>%s "
 
581
                "<small>trp </small><b>%.2f</b>%s "
 
582
                "<small>alt </small><b>%.0f</b>%s "
 
583
                "<small>hdg </small><b>%.0f</b>° "
 
584
                "<small></small>%d/%.1f",
 
585
                numdl_buf,
 
586
                dl_buf,
 
587
                tr_buf,
 
588
                ff_buf,
 
589
                gpsdata->fix.speed * 3.6 * unit_conv,   speedunit,
 
590
                trip_distance * unit_conv,              distunit,
 
591
                gpsdata->fix.altitude * unit_conv_alt,  altunit,
 
592
                gpsdata->fix.heading * unit_conv,
 
593
                gpsdata->satellites_used,
 
594
                gpsdata->hdop);
 
595
 
 
596
        if(global_new_msg)
 
597
                g_snprintf(buffer, BUFSIZE, "<span foreground='#ff0000'><b>New Message arrived. Click here.</b></span>");
627
598
 
628
599
        gtk_label_set_label(label, buffer);
629
600
 
630
601
 
631
602
        if(global_infopane_visible)
632
603
        {
633
 
 
634
 
 
635
 
 
636
 
 
637
 
 
 
604
                
 
605
                
 
606
                
 
607
                
 
608
                
638
609
                time_sec = (time_t)gpsdata->fix.time;
639
610
                ts = localtime(&time_sec);
640
 
 
641
 
 
642
 
                strftime(buffer, sizeof(buffer), "<big><b>%a %Y-%m-%d %H:%M:%S</b></big>", ts);
 
611
                
 
612
                
 
613
                strftime(buffer, sizeof(buffer), "<big><b>%a %Y-%m-%d %H:%M:%S</b></big>", ts); 
643
614
                gtk_label_set_label(label41,buffer);
644
 
 
645
 
 
 
615
        
 
616
                
646
617
                switch (global_latlon_unit)
647
618
                {
648
619
                        case 0:
649
620
                                g_snprintf(buffer, BUFSIZE, "<big><b>%f - %f</b></big>", gpsdata->fix.latitude, gpsdata->fix.longitude);
650
621
                                break;
651
622
                        case 1:
652
 
                                g_snprintf(buffer, BUFSIZE, "<big><b>%s   %s</b></big>",
 
623
                                g_snprintf(buffer, BUFSIZE, "<big><b>%s   %s</b></big>", 
653
624
                                          latdeg2latmin(gpsdata->fix.latitude),
654
625
                                          londeg2lonmin(gpsdata->fix.longitude));
655
626
                                break;
656
627
                        case 2:
657
 
                                g_snprintf(buffer, BUFSIZE, "<big><b>%s   %s</b></big>",
 
628
                                g_snprintf(buffer, BUFSIZE, "<big><b>%s   %s</b></big>", 
658
629
                                          latdeg2latsec(gpsdata->fix.latitude),
659
630
                                          londeg2lonsec(gpsdata->fix.longitude));
660
631
                }
661
632
                gtk_label_set_label(label31,buffer);
662
 
 
663
 
 
664
 
                g_snprintf(buffer, BUFSIZE,
665
 
                        "<b><span foreground='#0000ff'><span font_desc='50'>%.1f</span></span></b> %s",
 
633
                
 
634
                
 
635
                g_snprintf(buffer, BUFSIZE, 
 
636
                        "<b><span foreground='#0000ff'><span font_desc='50'>%.1f</span></span></b> %s", 
666
637
                        gpsdata->fix.speed*3.6*unit_conv, speedunit);
667
638
                gtk_label_set_label(label38,buffer);
668
 
 
669
 
 
 
639
        
 
640
                
670
641
                g_snprintf(buffer, BUFSIZE, "<big><b>%.1f %s</b></big>", gpsdata->fix.altitude * unit_conv_alt, altunit);
671
642
                gtk_label_set_label(label39,buffer);
672
 
 
673
 
 
 
643
                
 
644
                
674
645
                g_snprintf(buffer, BUFSIZE, "<big><b>%.1f°</b></big> ", gpsdata->fix.heading);
675
646
                gtk_label_set_label(label42,buffer);
676
 
 
677
 
 
678
 
                g_snprintf (buffer, BUFSIZE, _("<big><b>%d</b>  <small>HDOP</small><b> %.1f</b></big>"),
 
647
                
 
648
                
 
649
                g_snprintf(buffer, BUFSIZE, "<big><b>%d</b>  <small>HDOP</small><b> %.1f</b></big>", 
679
650
                                gpsdata->satellites_used, gpsdata->hdop);
680
651
                gtk_label_set_label(label43,buffer);
681
 
 
682
 
 
683
 
 
684
 
 
685
 
 
686
 
 
 
652
        
 
653
                
 
654
                
 
655
                
 
656
        
 
657
                
687
658
                g_snprintf(buffer, BUFSIZE, "<big><b>%.3f</b></big> <small>%s</small>", trip_distance*unit_conv,distunit);
688
659
                gtk_label_set_label(label45,buffer);
689
 
 
690
 
 
691
 
 
 
660
        
 
661
        
 
662
                
692
663
                trip_hours   = trip_time / 3600;
693
664
                trip_minutes = ((int)trip_time%3600)/60;
694
665
                trip_seconds = (int)trip_time % 60;
695
 
 
 
666
                
696
667
                if (trip_seconds < 10 && trip_minutes < 10)
697
668
                {
698
669
                        g_sprintf(buffer, "<big><b>%d:0%d:0%d</b></big>",trip_hours,trip_minutes,trip_seconds);
703
674
                        g_sprintf(buffer, "<big><b>%d:0%d:%d</b></big>",trip_hours,trip_minutes,trip_seconds);
704
675
                else
705
676
                        g_sprintf(buffer, "<big><b>%d:%d:%d</b></big>",trip_hours,trip_minutes,trip_seconds);
706
 
 
 
677
        
707
678
                gtk_label_set_label(label66,buffer);
708
 
 
709
 
 
 
679
        
 
680
                
710
681
                g_sprintf(buffer, "<big><b>%.1f</b></big><small> %s</small>", trip_distance*3600*unit_conv/(trip_time+2.0), speedunit);
711
682
                gtk_label_set_label(label68,buffer);
712
 
 
713
 
 
 
683
        
 
684
                
714
685
                g_sprintf(buffer, "<big><b>%.1f</b></big><small> %s</small>", trip_maxspeed*3.6*unit_conv, speedunit);
715
686
                gtk_label_set_label(label70,buffer);
716
687
        }
723
694
static gboolean
724
695
cb_gpsd_io_error(GIOChannel *src, GIOCondition condition, gpointer data)
725
696
{
726
 
        reset_gpsd_io();
727
 
 
728
 
        return FALSE;
 
697
        printf("*** %s(): \n",__PRETTY_FUNCTION__);
 
698
        g_free(gpsdata);
 
699
        gpsdata = NULL;
 
700
        g_source_remove(sid1); 
 
701
        g_source_remove(sid3); 
 
702
        gps_close(libgps_gpsdata);
 
703
        libgps_gpsdata = NULL;
 
704
        
 
705
        
 
706
        return FALSE; 
729
707
}
730
708
 
731
709
 
735
713
{
736
714
        int ret;
737
715
 
738
 
        if (!libgps_initialized)
739
 
                return FALSE;
740
 
 
741
 
#if GPSD_API_MAJOR_VERSION >= 7 /* API change. gpsd version 3.18 and subsequent. */
742
 
        ret = gps_read(&libgps_gpsdata, NULL, 0);
743
 
#else
744
 
        ret = gps_read(&libgps_gpsdata);
745
 
#endif
746
 
        /* Note that gps_read() will never actually return 0
747
 
           (zero-length reads are converted internally to a -1 return,
748
 
            since they mean that the connection to the daemon has closed),
749
 
           and gps_poll() will never return >0, but both will return
750
 
           <0 to indicate failure; hence the following ">= 0" check:
751
 
        */
752
 
        if (ret >= 0)
 
716
        ret = gps_poll(libgps_gpsdata);
 
717
        if (ret == 0)
753
718
        {
754
 
                gpsdata->satellites_used = libgps_gpsdata.satellites_used;
755
 
                gpsdata->hdop = libgps_gpsdata.dop.hdop;
756
 
                gpsdata->fix.time = libgps_gpsdata.fix.time;
 
719
                gpsdata->satellites_used = libgps_gpsdata->satellites_used;
 
720
                gpsdata->hdop = libgps_gpsdata->dop.hdop;
 
721
                gpsdata->fix.time = libgps_gpsdata->fix.time;
757
722
                if (isnan(gpsdata->fix.time))
758
723
                {
759
724
                        gpsdata->fix.time = (time_t) 0;
760
725
                }
761
 
                gpsdata->valid = (libgps_gpsdata.status != STATUS_NO_FIX);
 
726
                gpsdata->valid = (libgps_gpsdata->status != STATUS_NO_FIX);
762
727
                if (gpsdata->valid)
763
728
                {
764
 
                        gpsdata->seen_valid = TRUE;
765
 
                        gpsdata->fix.mode = libgps_gpsdata.fix.mode;
766
 
                        gpsdata->fix.latitude = libgps_gpsdata.fix.latitude;
767
 
                        gpsdata->fix.longitude = libgps_gpsdata.fix.longitude;
768
 
                        gpsdata->fix.speed = libgps_gpsdata.fix.speed;
769
 
                        gpsdata->fix.heading = libgps_gpsdata.fix.track;
770
 
                        gpsdata->fix.altitude = libgps_gpsdata.fix.altitude;
 
729
                        gpsdata->seen_vaild = TRUE;
 
730
                        gpsdata->fix.latitude = libgps_gpsdata->fix.latitude;
 
731
                        gpsdata->fix.longitude = libgps_gpsdata->fix.longitude;
 
732
                        gpsdata->fix.speed = libgps_gpsdata->fix.speed;
 
733
                        gpsdata->fix.heading = libgps_gpsdata->fix.track;
 
734
                        gpsdata->fix.altitude = libgps_gpsdata->fix.altitude;
771
735
                }
772
 
 
 
736
                
773
737
                g_source_remove(watchdog);
774
738
                watchdog = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,60,reset_gpsd_io,NULL,NULL);
775
739
        }
776
740
        else
777
741
        {
778
 
                fprintf (stderr, _("connection to gpsd LOST\n"));
779
 
                return cb_gpsd_io_error(src, condition, data);
 
742
                printf("gps_poll returned %d\n", ret);
780
743
        }
781
744
        return TRUE;
782
745
}
783
746
 
784
747
 
785
 
void
 
748
void 
786
749
get_gps()
787
750
{
788
 
        /* Set this flag to FALSE *immediately* so that a slow gps_open()
789
 
           doesn't cause successive cb_gps_timer() callbacks to call
790
 
           us *again* before we've even finished *this* invocation--
791
 
           which would create multiple, simultaneous get_gps_thread()
792
 
           threads and all sorts of associated problems:
793
 
        */
794
 
        reconnect_gpsd = FALSE;
795
 
 
796
 
        if (gps_timer) {
797
 
                /* Disable the regularly scheduled callback to cb_gps_timer()
798
 
                   until after get_gps_thread() has returned, to guard against
799
 
                   the situation described above when reconnect_gpsd
800
 
                   is re-set by something else (e.g.: the user bouncing
801
 
                   on the `Change GPSD' button):
802
 
                */
803
 
                g_source_remove (gps_timer);
804
 
                gps_timer = 0;
805
 
        }
806
 
 
807
 
        if (watchdog) {
808
 
                g_source_remove(watchdog);
809
 
        }
810
 
 
811
 
        if (sid1) {
812
 
                g_source_remove(sid1);
813
 
                sid1 = 0;
814
 
        }
815
 
 
816
 
        if (sid3) {
817
 
                g_source_remove(sid3);
818
 
                sid3 = 0;
819
 
        }
820
 
 
821
 
        if (gpsd_io_channel) {
822
 
                g_io_channel_unref (gpsd_io_channel);
823
 
                gpsd_io_channel = NULL;
824
 
        }
825
 
 
826
 
        /* Note that we're *not* free'ing the internal gpsdata structure,
827
 
           ever. We'd only immediately reallocate it, so it's not a net win
828
 
           in terms of memory-footprint; and it doesn't contain references
829
 
           to any file-descriptors or other ephemeral resources, so
830
 
           the only thing we'd be doing by re-initialising it would be
831
 
           throwing away our last known fix, etc.
832
 
        */
833
 
 
834
 
        if (libgps_initialized) {
835
 
                gps_close(&libgps_gpsdata);
836
 
                libgps_initialized = FALSE;
837
 
        }
838
 
 
839
 
#if GLIB_CHECK_VERSION(2,34,0)
840
 
        g_thread_new("gps thread", &get_gps_thread, (gpointer)NULL);
841
 
#else
842
751
        g_thread_create(&get_gps_thread, NULL, FALSE, NULL);
843
 
#endif
844
752
}
845
753
 
846
 
static void *
 
754
void *
847
755
get_gps_thread(void *ptr)
848
756
{
849
 
        if (gps_open(global_server, global_port, &libgps_gpsdata) == 0)
 
757
        libgps_gpsdata = gps_open(global_server, global_port);
 
758
        if (libgps_gpsdata)
850
759
        {
851
 
                fprintf (stderr, _("connection to gpsd SUCCEEDED \n"));
852
 
 
853
 
                libgps_initialized = TRUE;
854
 
 
855
 
                /* Unless someone has re-set reconnect_gpsd
856
 
                   between when get_gps() set it and now,
857
 
                   then it should still be FALSE; if someone else
858
 
                   *did* re-set it, then they did it for a reason.
859
 
 
860
 
                   Either way, we're better off leaving it alone,
861
 
                   here.
862
 
                */
863
 
 
 
760
                fprintf(stderr, "connection to gpsd SUCCEEDED \n");
 
761
                
 
762
                global_reconnect_gpsd = FALSE;
 
763
                
864
764
                if(!gpsdata)
865
765
                {
866
766
                        gpsdata = g_new0(tangogps_gps_data_t,1);
867
767
                }
868
 
 
869
 
 
870
 
                gps_stream(&libgps_gpsdata, WATCH_ENABLE, NULL);
871
 
 
 
768
                
 
769
        
 
770
                gps_stream(libgps_gpsdata, WATCH_ENABLE | POLL_NONBLOCK, NULL);
 
771
                
872
772
                watchdog = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,60,reset_gpsd_io,NULL,NULL);
873
 
 
874
 
 
875
 
                gpsd_io_channel = g_io_channel_unix_new(libgps_gpsdata.gps_fd);
 
773
                
 
774
                
 
775
                gpsd_io_channel = g_io_channel_unix_new(libgps_gpsdata->gps_fd);
876
776
                g_io_channel_set_flags(gpsd_io_channel, G_IO_FLAG_NONBLOCK, NULL);
877
 
 
878
 
 
 
777
                
 
778
                
879
779
                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);
880
 
 
881
 
 
 
780
                
 
781
                
882
782
                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);
883
 
 
884
 
        } else {
885
 
                /* Failure. Maybe it's transient--try again
886
 
                   on the next cycle: */
887
 
                reconnect_gpsd = TRUE;
 
783
        
888
784
        }
889
 
 
890
 
        gps_timer = g_timeout_add (1000, cb_gps_timer, NULL);
891
 
 
 
785
        
892
786
        return NULL;
893
787
}
894
788
 
900
794
        char *buffer=NULL;
901
795
        GdkColor color;
902
796
        static GdkGC *gc=NULL, *gc1=NULL;
903
 
 
 
797
        
904
798
        PangoContext            *context = NULL;
905
799
        PangoLayout             *layout  = NULL;
906
800
        PangoFontDescription    *desc    = NULL;
907
 
 
 
801
        
908
802
        y = map_drawable->allocation.height - 8;
909
 
 
 
803
        
910
804
        if(gc == NULL)
911
805
        {
912
806
                gc   = gdk_gc_new(pixmap);
915
809
        color.red   = 0;
916
810
        color.green = 0;
917
811
        color.blue  = 0;
918
 
 
 
812
        
919
813
        gdk_gc_set_rgb_fg_color(gc, &color);
920
 
        gdk_gc_set_line_attributes(gc,
921
 
                                   5, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
 
814
        gdk_gc_set_line_attributes(gc,
 
815
                        5, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
922
816
 
923
817
        color.red   = 65000;
924
818
        color.green = 65000;
925
819
        color.blue  = 65000;
926
 
 
 
820
        
927
821
        gdk_gc_set_rgb_fg_color(gc1, &color);
928
 
        gdk_gc_set_line_attributes(gc1,
929
 
                                   3, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
930
 
 
 
822
        gdk_gc_set_line_attributes(gc1,
 
823
                        3, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
 
824
        
931
825
 
932
826
 
933
827
        lat   = pixel2lat(global_zoom, global_y+y);
935
829
        lon2  = pixel2lon(global_zoom, max_bar_length);
936
830
 
937
831
        distance = get_distance(lat, lon1, lat, lon2);
938
 
 
 
832
        
939
833
        buffer = distance2scale(distance, &factor);
940
834
        max_bar_length *= factor;
941
 
 
 
835
        
942
836
        gdk_draw_line(map_drawable->window, gc, 4, y, max_bar_length+6, y);
943
 
        gdk_draw_line(map_drawable->window, gc1,5, y, max_bar_length+5, y);
944
 
 
945
 
 
 
837
        gdk_draw_line(map_drawable->window, gc1,5, y, max_bar_length+5, y);     
 
838
 
 
839
        
946
840
        context = gtk_widget_get_pango_context (map_drawable);
947
841
        layout  = pango_layout_new (context);
948
842
        desc    = pango_font_description_new();
949
 
 
 
843
        
950
844
        pango_font_description_set_absolute_size (desc, 12 * PANGO_SCALE);
951
845
        pango_layout_set_font_description (layout, desc);
952
846
        pango_layout_set_text (layout, buffer, strlen(buffer));
953
847
 
954
848
 
955
 
 
 
849
        
956
850
        pango_layout_get_pixel_size(layout, &width, &height);
957
851
 
958
 
 
959
 
 
 
852
        
 
853
        
960
854
        gdk_gc_set_line_attributes(gc,
961
 
                                   height+2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
 
855
                        height+2, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);        
962
856
 
963
 
        gdk_gc_set_line_attributes(gc1,
964
 
                                   height, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);
 
857
        gdk_gc_set_line_attributes(gc1,
 
858
                        height, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_ROUND);  
965
859
 
966
860
        gdk_draw_line(map_drawable->window, gc1, 22, y-height/2+6, 20+width+6, y-height/2+6);
967
861
 
968
 
 
969
 
 
970
 
        gdk_draw_layout(map_drawable->window,
971
 
                        gc,
972
 
                        25, y-height+7,
973
 
                        layout);
974
 
 
 
862
     
 
863
 
 
864
                gdk_draw_layout(map_drawable->window,
 
865
                                gc,
 
866
                                25, y-height+7,
 
867
                                layout);
 
868
        
975
869
 
976
870
 
977
871
        g_free(buffer);
978
872
        pango_font_description_free (desc);
979
873
        g_object_unref (layout);
980
 
 
981
 
 
982
 
 
 
874
        
 
875
        
 
876
        
983
877
}
984
878
 
985
879
char *
988
882
        float unit_conv=1;
989
883
        char *buf = NULL;
990
884
        char unit[5];
991
 
 
 
885
        
992
886
        switch (global_speed_unit)
993
887
        {
994
888
                case METRICAL:
1002
896
                case NAUTICAL :
1003
897
                        unit_conv = 1.0 / 1.852;
1004
898
                        strncpy(unit, "NM", 3);
1005
 
                        break;
 
899
                        break;          
1006
900
        }
1007
 
 
 
901
        
1008
902
        distance *= unit_conv;
1009
903
        if (distance >= 5000) {
1010
904
                buf = g_strconcat("5000", unit, NULL);