forked from mkoloberdin/waze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roadmap_alert.c
452 lines (353 loc) · 10.9 KB
/
roadmap_alert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* roadmap_alert.c - Manage the alert points in DB.
*
* LICENSE:
*
* Copyright 2008 Avi B.S
* Copyright 2008 Ehud Shabtai
*
* This file is part of RoadMap.
*
* RoadMap is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* RoadMap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RoadMap; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* SYNOPSYS:
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "roadmap.h"
#include "roadmap_dbread.h"
#include "roadmap_tile_model.h"
#include "roadmap_db_alert.h"
#include "roadmap_layer.h"
#include "roadmap_config.h"
#include "roadmap_sound.h"
#include "editor/static/add_alert.h"
#include "roadmap_screen.h"
#include "roadmap_alert.h"
#include "roadmap_alerter.h"
#include "tts_apptext.h"
#define ALERT_AUDIO_SPEED_CAM "ApproachSpeedCam"
#define ALERT_AUDIO_RED_LIGHT "ApproachRedLightCam"
#define ALERT_ICON_SPEED_CAM "speed_cam_alert"
#define WARN_ICON_SPEED_CAM "speed_cam_warn"
#define ALERT_ICON_RED_LIGHT "redlightcam_alert"
#define MINIMUM_DISTANCE_TO_CHECK 80
static void *roadmap_alert_map (const roadmap_db_data_file *file);
static void roadmap_alert_activate (void *context);
static void roadmap_alert_unmap (void *context);
static int roadmap_alert_is_cancelable(int alertId);
static BOOL roadmap_alert_can_send_thumbs_up(int alertId);
static int roadmap_alert_thumbs_up(int alertId);
static int roadmap_alert_cancel(int alertId);
static int roadmap_alert_check_same_street(int record);
static int roadmap_alert_handle_alert(int alertId);
static BOOL roadmap_alert_is_square_dependent(void);
static BOOL roadmap_alert_distance_check(RoadMapPosition);
static roadmap_alerter_location_info * roadmap_alert_get_location_info(int alertId){
return NULL; // information (line_id, square_id) is not cached in these types of alerts
}
static RoadMapConfigDescriptor AlertDistanceCfg =
ROADMAP_CONFIG_ITEM("Alerts", "Alert Distance");
static char *RoadMapAlertType = "RoadMapAlertContext";
typedef struct {
char *type;
RoadMapAlert *Alert;
int AlertCount;
} RoadMapAlertContext;
static RoadMapAlertContext *RoadMapAlertActive = NULL;
roadmap_db_handler RoadMapAlertHandler = {
"alert",
roadmap_alert_map,
roadmap_alert_activate,
roadmap_alert_unmap
};
roadmap_alert_provider RoadmapAlertProvider = {
"alertDb",
roadmap_alert_count,
roadmap_alert_get_id,
roadmap_alert_get_position,
roadmap_alert_get_speed,
roadmap_alert_get_map_icon,
roadmap_alert_get_alert_icon,
roadmap_alert_get_warning_icon,
roadmap_alert_get_distance,
roadmap_alert_get_alert_sound,
roadmap_alert_alertable,
roadmap_alert_get_string,
roadmap_alert_is_cancelable,
roadmap_alert_cancel,
roadmap_alert_check_same_street,
roadmap_alert_handle_alert,
roadmap_alert_is_square_dependent,
roadmap_alert_get_location_info,
roadmap_alert_distance_check,
roadmap_alert_get_priority,
NULL,
roadmap_alert_can_send_thumbs_up,
roadmap_alert_thumbs_up,
NULL,
NULL,
NULL,
roadmap_alert_start_handling,
roadmap_alert_stop_handling
};
RoadMapAlert * roadmap_alert_get_alert(int record){
return RoadMapAlertActive->Alert + record;
}
static BOOL roadmap_alert_is_square_dependent(void){
return TRUE;
}
BOOL roadmap_alert_distance_check(RoadMapPosition gps_pos){
static RoadMapPosition last_checked_position = {0,0};
int distance;
if(!last_checked_position.latitude){ // first time
last_checked_position = gps_pos;
return TRUE;
}
distance = roadmap_math_distance(&gps_pos, &last_checked_position);
if (distance < MINIMUM_DISTANCE_TO_CHECK)
return FALSE;
else{
last_checked_position = gps_pos;
return TRUE;
}
}
RoadMapAlert *roadmap_alert_get_alert_by_id( int id)
{
int i;
RoadMapAlert *alert;
if (RoadMapAlertActive == NULL)
return NULL;
// Find alert:
for( i=0; i< RoadMapAlertActive->AlertCount; i++){
alert = roadmap_alert_get_alert(i);
if( alert->id == id)
return alert;
}
return NULL;
}
int roadmap_alert_is_cancelable(int alertId){
return TRUE;
}
static BOOL roadmap_alert_can_send_thumbs_up(int alertId){
return FALSE;
}
static int roadmap_alert_thumbs_up(int alertId){
return TRUE;
}
int roadmap_alert_cancel(int alertId){
RoadMapAlert *pAlert;
request_speed_cam_delete();
pAlert = roadmap_alert_get_alert_by_id( alertId);
if (pAlert)
pAlert->category = 0;
return TRUE;
}
static int roadmap_alert_check_same_street(int record){
return TRUE;
}
static int roadmap_alert_handle_alert(int alertId){
return FALSE;
}
static void *roadmap_alert_map (const roadmap_db_data_file *file) {
RoadMapAlertContext *context;
context = malloc(sizeof(RoadMapAlertContext));
roadmap_check_allocated(context);
context->type = RoadMapAlertType;
if (!roadmap_db_get_data (file,
model__tile_alert_data,
sizeof(RoadMapAlert),
(void **)&(context->Alert),
&(context->AlertCount))) {
roadmap_log (ROADMAP_FATAL, "invalid alert/data structure");
}
//Distance to alert
roadmap_config_declare
("preferences", &AlertDistanceCfg, "400", NULL);
return context;
}
static void roadmap_alert_activate (void *context) {
RoadMapAlertContext *alert_context = (RoadMapAlertContext *) context;
if (alert_context != NULL) {
if (alert_context->type != RoadMapAlertType) {
roadmap_log (ROADMAP_FATAL, "cannot activate alert (bad type)");
}
}
RoadMapAlertActive = alert_context;
}
static void roadmap_alert_unmap (void *context) {
RoadMapAlertContext *alert_context = (RoadMapAlertContext *) context;
if (alert_context->type != RoadMapAlertType) {
roadmap_log (ROADMAP_FATAL, "cannot activate alert (bad type)");
}
if (RoadMapAlertActive == alert_context) {
RoadMapAlertActive = NULL;
}
free(alert_context);
}
int roadmap_alert_count (void) {
if (RoadMapAlertActive == NULL) {
return 0;
}
return RoadMapAlertActive->AlertCount;
}
void roadmap_alert_get_position(int alert, RoadMapPosition *position, int *steering) {
RoadMapAlert *alert_st = roadmap_alert_get_alert (alert);
assert(alert_st != NULL);
position->longitude = alert_st->pos.longitude;
position->latitude = alert_st->pos.latitude;
*steering = alert_st->steering;
}
unsigned int roadmap_alert_get_speed(int alert){
RoadMapAlert *alert_st = roadmap_alert_get_alert (alert);
assert(alert_st != NULL);
return (int) alert_st->speed;
}
int roadmap_alert_get_category(int alert) {
RoadMapAlert *alert_st = roadmap_alert_get_alert (alert);
assert(alert_st != NULL);
return (int) alert_st->category;
}
int roadmap_alert_get_id(int alert){
RoadMapAlert *alert_st = roadmap_alert_get_alert (alert);
assert(alert_st != NULL);
return (int) alert_st->id;
}
// check if an alert should be generated for a specific category
int roadmap_alert_alertable(int record){
int alert_category = roadmap_alert_get_category(record);
switch (alert_category) {
case ALERT_CATEGORY_SPEED_CAM:
return 1;
case ALERT_CATEGORY_RED_LIGHT_CAM:
return 1;
default:
return 0;
}
}
const char * roadmap_alert_get_string(int id){
RoadMapAlert *alert_st = roadmap_alert_get_alert_by_id(id);
if (alert_st == NULL)
return NULL;
switch (alert_st->category) {
case ALERT_CATEGORY_SPEED_CAM:
return "Speed trap ahead" ;
case ALERT_CATEGORY_RED_LIGHT_CAM:
return "Red light cam ahead";
default:
return NULL;
}
}
const char * roadmap_alert_get_map_icon(int id){
RoadMapAlert *alert_st = roadmap_alert_get_alert_by_id(id);
if (alert_st == NULL)
return NULL;
switch (alert_st->category) {
case ALERT_CATEGORY_SPEED_CAM:
return "rm_speed_cam" ;
case ALERT_CATEGORY_DUMMY_SPEED_CAM:
return "rm_dummy_speed_cam";
case ALERT_CATEGORY_RED_LIGHT_CAM:
return "rm_red_light_cam";
default:
return NULL;
}
}
int roadmap_alert_get_priority(void){
return ALERTER_PRIORITY_MEDIUM;
}
int roadmap_alert_get_distance(int record){
return roadmap_config_get_integer(&AlertDistanceCfg);
}
const char *roadmap_alert_get_alert_icon(int Id){
RoadMapAlert *alert_st = roadmap_alert_get_alert_by_id(Id);
if (alert_st == NULL)
return NULL;
switch (alert_st->category) {
case ALERT_CATEGORY_SPEED_CAM:
return ALERT_ICON_SPEED_CAM;
case ALERT_CATEGORY_RED_LIGHT_CAM:
return ALERT_ICON_RED_LIGHT;
default:
return NULL;
}
}
const char *roadmap_alert_get_warning_icon(int Id){
RoadMapAlert *alert_st = roadmap_alert_get_alert_by_id(Id);
if (alert_st == NULL)
return NULL;
switch (alert_st->category) {
case ALERT_CATEGORY_SPEED_CAM:
return WARN_ICON_SPEED_CAM;
case ALERT_CATEGORY_RED_LIGHT_CAM:
return ALERT_ICON_RED_LIGHT;
default:
return NULL;
}
}
RoadMapSoundList roadmap_alert_get_alert_sound (int Id) {
RoadMapSoundList sound_list = NULL;
RoadMapAlert *alert_st = roadmap_alert_get_alert_by_id (Id);
if (alert_st == NULL)
return NULL;
switch (alert_st->category) {
case ALERT_CATEGORY_SPEED_CAM:
{
if ( tts_apptext_available( TTS_APPTEXT_APPROACH_SPEED_CAM ) ) {
sound_list = tts_apptext_get_sound( TTS_APPTEXT_APPROACH_SPEED_CAM );
}
else {
sound_list = roadmap_sound_list_create (0);
roadmap_sound_list_add (sound_list, ALERT_AUDIO_SPEED_CAM);
}
break;
}
case ALERT_CATEGORY_RED_LIGHT_CAM:
{
if ( tts_apptext_available( TTS_APPTEXT_APPROACH_REDLIGHT_CAM ) ) {
sound_list = tts_apptext_get_sound( TTS_APPTEXT_APPROACH_REDLIGHT_CAM );
}
else {
sound_list = roadmap_sound_list_create (0);
roadmap_sound_list_add (sound_list, ALERT_AUDIO_RED_LIGHT);
}
break;
}
default:
break;
}
return sound_list;
}
int roadmap_alert_start_handling(int id){
#ifdef OPENGL
RoadMapGuiPoint offset = {0,0};
RoadMapAlert *pAlert = roadmap_alert_get_alert_by_id (id);
if (pAlert == NULL)
return FALSE;
roadmap_screen_start_glow(&pAlert->pos, 120, &offset);
return TRUE;
#endif
return FALSE;
}
int roadmap_alert_stop_handling(int alert){
#ifdef OPENGL
roadmap_screen_stop_glow();
return TRUE;
#endif
return FALSE;
}