Codebase list gpsbabel / debian/1.8.0+ds-2 garmin.cc
debian/1.8.0+ds-2

Tree @debian/1.8.0+ds-2 (Download .tar.gz)

garmin.cc @debian/1.8.0+ds-2raw · history · blame

   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
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
/*
    Jeeps wrapper for Garmin serial protocol.

    Copyright (C) 2002, 2003, 2004, 2005, 2006  Robert Lipe, robertlipe+source@gpsbabel.org

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 */

#include <cctype>               // for isalpha, toupper
#include <climits>              // for INT_MAX
#include <cmath>                // for atan2, floor, sqrt
#include <csetjmp>              // for setjmp
#include <cstdio>               // for fprintf, fflush, snprintf, sprintf
#include <cstdlib>              // for atoi, strtol
#include <cstring>              // for memcpy, strlen, strncpy, strchr
#include <ctime>                // for time_t

#include <QByteArray>           // for QByteArray
#include <QChar>                // for QChar
#include <QString>              // for QString
#include <Qt>                   // for CaseInsensitive
#include <QtGlobal>             // for qPrintable, foreach

#include "defs.h"
#include "cet_util.h"           // for cet_convert_init, cet_cs_vec_utf8
#include "format.h"             // for Format
#include "garmin_device_xml.h"  // for gdx_get_info, gdx_info, gdx_file, gdx_jmp_buf
#include "garmin_fs.h"          // for garmin_fs_garmin_after_read, garmin_fs_garmin_before_write
#include "garmin_tables.h"      // for gt_find_icon_number_from_desc, PCX, gt_find_desc_from_icon_number
#include "grtcirc.h"            // for DEG
#include "jeeps/gps.h"
#include "jeeps/gpsserial.h"
#include "src/core/datetime.h"  // for DateTime
#include "vecs.h"               // for Vecs

#define MYNAME "GARMIN"
static const char* portname;
static short_handle mkshort_handle;
static GPS_PWay* tx_waylist;
static GPS_PWay* tx_routelist;
static GPS_PWay* cur_tx_routelist_entry;
static GPS_PTrack* tx_tracklist;
static GPS_PTrack* cur_tx_tracklist_entry;
static int my_track_count = 0;
static char* getposn = nullptr;
static char* poweroff = nullptr;
static char* eraset = nullptr;
static char* resettime = nullptr;
static char* snlen = nullptr;
static char* snwhiteopt = nullptr;
static char* deficon = nullptr;
static char* category = nullptr;
static char* categorybitsopt = nullptr;
static char* baudopt = nullptr;
static int baud = 0;
static int categorybits;
static int receiver_must_upper = 1;

static Format* gpx_vec;

#define MILITANT_VALID_WAYPT_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

/* Technically, even this is a little loose as spaces arent allowed */
static const char* valid_waypt_chars = MILITANT_VALID_WAYPT_CHARS " ";

static
QVector<arglist_t> garmin_args = {
  {
    "snlen", &snlen, "Length of generated shortnames", nullptr,
    ARGTYPE_INT, "1", nullptr, nullptr
  },
  {
    "snwhite", &snwhiteopt, "Allow whitespace synth. shortnames",
    nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
  },
  { "deficon", &deficon, "Default icon name", nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr },
  {
    "get_posn", &getposn, "Return current position as a waypoint",
    nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
  },
  {
    "power_off", &poweroff, "Command unit to power itself down",
    nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
  },
  {
    "erase_t", &eraset, "Erase existing courses when writing new ones",
    nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
  },
  {
    "resettime", &resettime, "Sync GPS time to computer time",
    nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
  },
  {
    "category", &category, "Category number to use for written waypoints",
    nullptr, ARGTYPE_INT, "1", "16", nullptr
  },
  {
    "bitscategory", &categorybitsopt, "Bitmap of categories",
    nullptr, ARGTYPE_INT, "1", "65535", nullptr
  },
  {
    "baud", &baudopt, "Speed in bits per second of serial port (baud=9600)",
    nullptr, ARGTYPE_INT, ARG_NOMINMAX, nullptr },

};

static const char* d103_symbol_from_icon_number(unsigned int n);
static int d103_icon_number_from_symbol(const QString& s);


static void
rw_init(const QString& fname)
{
  receiver_must_upper = 1;
  const char* receiver_charset = nullptr;

  if (!mkshort_handle) {
    mkshort_handle = mkshort_new_handle();
  }

  if (global_opts.debug_level > 0)  {
    GPS_Enable_Warning();
    GPS_Enable_User();
  }
  if (global_opts.debug_level > 1)  {
    GPS_Enable_Diagnose();
  }
  GPS_Enable_Error();

  if (poweroff) {
    GPS_Command_Off(qPrintable(fname));
    return;
  }

  if (categorybitsopt) {
    categorybits = strtol(categorybitsopt, nullptr, 0);
  }

  if (baudopt) {
    baud = strtol(baudopt, nullptr, 0);
    switch (baud) {
      case 9600:
      case 19200:
      case 38400:
      case 57600:
      case 115200:
        break;
      default:
        fatal("Baud rate %d is not supported\n", baud);
    }
  }

  if (GPS_Init(qPrintable(fname)) < 0) {
    fatal(MYNAME ":Can't init %s\n", qPrintable(fname));
  }

  /*
   * THis is Gross. The B&W Vista sometimes sets its time decades into
   * the future with no way to reset it.  This apparently can "cure"
   * an affected unit.
   */
  if (resettime) {
    GPS_User("Issuing Time Reset...\n");
    GPS_Command_Send_Time(qPrintable(fname), current_time().toTime_t());
    GPS_User("done.\n");
  }

  portname = xstrdup(qPrintable(fname));

  if (baud && baud != DEFAULT_BAUD) {
    if (0 == GPS_Set_Baud_Rate(portname, baud)) {
      gps_baud_rate = baud;
    }
  }

  /*
   * Grope the unit we're talking to to set setshort_length to
   * 	20 for  the V,
   * 	10 for Street Pilot, (old) Rhino, 76
   * 	6 for the III, 12, emap, and etrex
   * Fortunately, getting this "wrong" only results in ugly names
   * when we're using the synthesize_shortname path.
   */
  int receiver_short_length = 10;

  switch (gps_waypt_type) {	/* waypoint type as defined by jeeps */
  case 0:
    fatal("Garmin unit %d does not support waypoint xfer.",
          gps_save_id);

    break;
  case 100:	/* The GARMIN GPS Interface Specification, */
  case 101:	/* says these waypoint types use an ident */
  case 102:	/* length of 6.  Waypoint types 106, 108 */
  case 103:	/* and 109 are all variable  length    */
  case 104:
  case 105:
  case 107:
  case 150:
  case 151:
  case 152:
  case 154:
  case 155:
    receiver_short_length = 6;
    break;
  case 106:	/* Waypoint types with variable ident length */
  case 108: 	/* Need GPSr id to know the actual length */
  case 109:
  case 110:
    switch (gps_save_id) {
    case 130:	/* Garmin Etrex (yellow) */
      receiver_short_length = 6;
      break;
    case 295: 	/* eTrex (yellow, fw v. 3.30) */
    case 696: 	/* eTrex HC */
    case 574: 	/* Geko 201 */
      receiver_short_length = 6;
      valid_waypt_chars =
        MILITANT_VALID_WAYPT_CHARS " +-";
      setshort_badchars(mkshort_handle, "\"$.,'!");
      break;

    case 155:	/* Garmin V */
    case 404:	/* SP2720 */
    case 520:	/* SP2820 */
      receiver_short_length = 20;
      break;
    case 382: 	/* C320 */
      receiver_short_length = 30;
      receiver_must_upper = 0;
      break;
    case 292: /* (60|76)C[S]x series */
    case 421: /* Vista|Legend Cx */
    case 694: /* Legend HCx */
    case 695: /* Vista HC */
    case 786: /* HC model */
    case 957: /* Legend HC */
      receiver_short_length = 14;
      snwhiteopt = xstrdup("1");
      receiver_must_upper = 0;
      /* This might be 8859-1 */
      receiver_charset = CET_CHARSET_MS_ANSI;
      break;
    case 291: /* GPSMAP 60CS, probably others */
    case 1095: /* GPS 72H */
      receiver_short_length = 10;
      valid_waypt_chars = MILITANT_VALID_WAYPT_CHARS " +-";
      setshort_badchars(mkshort_handle, "\"$.,'!");
      break;
    case 231: /* Quest */
    case 463: /* Quest 2 */
      receiver_must_upper = 0;
      receiver_short_length = 30;
      receiver_charset = CET_CHARSET_MS_ANSI;
      break;
    case 577: // Rino 530HCx Version 2.50
      receiver_must_upper = 0;
      receiver_short_length = 14;
      break;
    case 429: // Streetpilot i3
      receiver_must_upper = 0;
      receiver_charset = CET_CHARSET_MS_ANSI;
      receiver_short_length = 30;
      break;
    case 484: // Forerunner 305
      receiver_short_length = 8;
      break;
    case 260: /* GPSMap 296 */
    default:
      break;
    }
    break;
  default:
    break;

  }

  if (global_opts.debug_level > 0)  {
    fprintf(stderr, "Waypoint type: %d\n"
            "Chosen waypoint length %d\n",
            gps_waypt_type, receiver_short_length);
    if (gps_category_type) {
      fprintf(stderr, "Waypoint category type: %d\n",
              gps_category_type);
    }
  }

  /*
   * If the user provided a short_length, override the calculated value.
   */
  if (snlen) {
    setshort_length(mkshort_handle, atoi(snlen));
  } else {
    setshort_length(mkshort_handle, receiver_short_length);
  }

  if (snwhiteopt) {
    setshort_whitespace_ok(mkshort_handle, atoi(snwhiteopt));
  }

  /*
   * Until Garmins documents how to determine valid character space
   * for the new models, we just release this safety check manually.
   */
  if (receiver_must_upper) {
    setshort_goodchars(mkshort_handle, valid_waypt_chars);
  } else {
    setshort_badchars(mkshort_handle, "");
  }

  setshort_mustupper(mkshort_handle, receiver_must_upper);

  /*
   * This used to mean something when we used cet, but these days this
   * format either use implicit QString conversions (utf8) which is
   * likely a bug, or we have hard coded QString::fromLatin1 or CSTRc.
   * So all the above detection of receiver_charset is for naught.
   * But perhaps we will use an appropriate codec based on receiver_charset
   * someday.
   */
  if (receiver_charset) {
    cet_convert_init(receiver_charset, 1);
  }
}

static void
rd_init(const QString& fname)
{
  if (setjmp(gdx_jmp_buf)) {
    const gdx_info* gi = gdx_get_info();
    gpx_vec = Vecs::Instance().find_vec("gpx");
    gpx_vec->rd_init(gi->from_device.canon);
  } else {
    gpx_vec = nullptr;
    rw_init(fname);
  }
}

static void
rw_deinit()
{
  if (gps_baud_rate != DEFAULT_BAUD) {
    if (0 == GPS_Set_Baud_Rate(portname, DEFAULT_BAUD)) {
      gps_baud_rate = baud;
    }
  }

  if (mkshort_handle) {
    mkshort_del_handle(&mkshort_handle);
  }

  xfree(portname);
  portname = nullptr;
}

static int
waypt_read_cb(int total_ct, GPS_PWay* )
{
  if (global_opts.verbose_status) {
    static int i;
    i++;
    waypt_status_disp(total_ct, i);
  }
  return 0;
}

static void
waypt_read()
{
  int n;
  GPS_PWay* way = nullptr;

  if (getposn) {
    auto* wpt = new Waypoint;
    wpt->latitude = gps_save_lat;
    wpt->longitude = gps_save_lon;
    wpt->shortname = "Position";
    if (gps_save_time) {
      wpt->SetCreationTime(gps_save_time);
    }
    waypt_add(wpt);
    return;
  }

  if ((n = GPS_Command_Get_Waypoint(portname, &way, waypt_read_cb)) < 0) {
    fatal(MYNAME  ":Can't get waypoint from %s\n", portname);
  }

  for (int i = 0; i < n; i++) {
    auto* wpt_tmp = new Waypoint;

    wpt_tmp->shortname = QString::fromLatin1(way[i]->ident);
    wpt_tmp->description = QString::fromLatin1(way[i]->cmnt);
    wpt_tmp->shortname = wpt_tmp->shortname.simplified();
    wpt_tmp->description = wpt_tmp->description.simplified();
    wpt_tmp->longitude = way[i]->lon;
    wpt_tmp->latitude = way[i]->lat;
    if (gps_waypt_type == 103) {
      wpt_tmp->icon_descr = d103_symbol_from_icon_number(
                              way[i]->smbl);
    } else {
      wpt_tmp->icon_descr = gt_find_desc_from_icon_number(
                              way[i]->smbl, PCX);
    }
    /*
     * If a unit doesn't store altitude info (i.e. a D103)
     * gpsmem will default the alt to INT_MAX.   Other units
     * (I can't recall if it was the V (D109) hor the Vista (D108)
     * return INT_MAX+1, contrary to the Garmin protocol doc which
     * says they should report 1.0e25.   So we'll try to trap
     * all the cases here.     Yes, libjeeps should probably
     * do this and not us...
     */
    if ((way[i]->alt == (float)(1U<<31)) ||
        (way[i]->alt == INT_MAX) ||
        (way[i]->alt >= (float) 1.0e20)
       ) {
      wpt_tmp->altitude = unknown_alt;
    } else {
      wpt_tmp->altitude = way[i]->alt;
    }
    if (way[i]->time_populated) {
      wpt_tmp->SetCreationTime(way[i]->time);
    }
    garmin_fs_garmin_after_read(way[i], wpt_tmp, gps_waypt_type);
    waypt_add(wpt_tmp);
    GPS_Way_Del(&way[i]);
  }
  if (way) {
    xfree(way);
  }
}

static int lap_read_nop_cb(int, struct GPS_SWay**)
{
  return 0;
}

// returns 1 if the waypoint's start_time can be found
// in the laps array, 0 otherwise
static unsigned int checkWayPointIsAtSplit(Waypoint* wpt, GPS_PLap* laps, int nlaps)
{
  int result = 0;

  if ((laps != nullptr) && (nlaps > 0)) {
    for (int i = (nlaps-1); i >= 0; i--) {
      GPS_PLap lap = laps[i];
      time_t delta = lap->start_time - wpt->GetCreationTime().toTime_t();
      if ((delta >= -1) && (delta <= 1)) {
        result = 1;
        break;

        // as an optimization this will stop going through
        // the lap array when the negative delta gets too
        // big. It assumes that laps is sorted by time in
        // ascending order (which appears to be the case for
        // Forerunners. Don't know about other devices.
      } else if (delta < -1) {
        break;
      }
    }
  }

  return result;
}

static
void
track_read()
{
  GPS_PTrack* array;
  route_head* trk_head = nullptr;
  int trk_num = 0;
  const char* trk_name = "";
  GPS_PLap* laps = nullptr;
  int nlaps = 0;
  int next_is_new_trkseg = 0;

  if (gps_lap_type != -1) {
    nlaps = GPS_Command_Get_Lap(portname, &laps, &lap_read_nop_cb);
  }


  int32 ntracks = GPS_Command_Get_Track(portname, &array, waypt_read_cb);

  if (ntracks <= 0) {
    return;
  }

  for (int i = 0; i < ntracks; i++) {
    /*
     * This is probably always in slot zero, but the Garmin
     * serial spec says these can appear anywhere.  Toss them
     * out so we don't treat it as an extraneous trackpoint.
     */
    if (array[i]->ishdr) {
      trk_name = array[i]->trk_ident;
      if (!trk_name) {
        trk_name = "";
      }
    }

    if (trk_head == nullptr || array[i]->ishdr) {
      trk_head = new route_head;
      trk_head->rte_num = trk_num;
      trk_head->rte_name = QString::fromLatin1(trk_name);
      trk_num++;
      track_add_head(trk_head);
    }

    /* Need to do this here because fitness devices set tnew
     * on a trackpoint without lat/lon.
     */
    if (array[i]->tnew) {
      next_is_new_trkseg = 1;
    }

    if (array[i]->no_latlon || array[i]->ishdr) {
      continue;
    }
    auto* wpt = new Waypoint;

    wpt->longitude = array[i]->lon;
    wpt->latitude = array[i]->lat;
    wpt->altitude = array[i]->alt;
    wpt->heartrate = array[i]->heartrate;
    wpt->cadence = array[i]->cadence;
    wpt->shortname = array[i]->trk_ident;
    wpt->SetCreationTime(array[i]->Time);
    wpt->wpt_flags.is_split = checkWayPointIsAtSplit(wpt, laps,
                              nlaps);
    wpt->wpt_flags.new_trkseg = next_is_new_trkseg;
    next_is_new_trkseg = 0;

    if (array[i]->dpth < 1.0e25f) {
      WAYPT_SET(wpt, depth, array[i]->dpth);
    }
    if (array[i]->temperature_populated) {
      WAYPT_SET(wpt, temperature, array[i]->temperature);
    }

    track_add_wpt(trk_head, wpt);
  }

  while (ntracks) {
    GPS_Track_Del(&array[--ntracks]);
  }
  xfree(array);
}

static
void
route_read()
{
  GPS_PWay* array;
  /* TODO: Fixes warning but is it right?
   * RJL:  No, the warning isn't right; GCC's flow analysis is broken.
   * still, it's good taste...
   */
  route_head* rte_head = nullptr;

  int32 nroutepts = GPS_Command_Get_Route(portname, &array);

//	fprintf(stderr, "Routes %d\n", (int) nroutepts);
#if 1
  for (int i = 0; i < nroutepts; i++) {
    if (array[i]->isrte) {
      char* csrc = nullptr;
      /* What a horrible API has libjeeps for making this
       * my problem.
       */
      switch (array[i]->rte_prot) {
      case 201:
        csrc = array[i]->rte_cmnt;
        break;
      case 202:
        csrc = array[i]->rte_ident;
        break;
      default:
        break;
      }
      rte_head = new route_head;
      route_add_head(rte_head);
      if (csrc) {
        rte_head->rte_name = QString::fromLatin1(csrc);
      }
    } else {
      if (array[i]->islink)  {
        continue;
      } else {
        auto* wpt_tmp = new Waypoint;
        wpt_tmp->latitude = array[i]->lat;
        wpt_tmp->longitude = array[i]->lon;
        wpt_tmp->shortname = array[i]->ident;
        route_add_wpt(rte_head, wpt_tmp);
      }
    }
  }
#else
  GPS_Fmt_Print_Route(array, nroutepts, stderr);
#endif

}

#if 0
static
void
lap_read_as_track(void)
{
  int32 ntracks;
  GPS_PLap* array;
  route_head* trk_head = NULL;
  int trk_num = 0;
  int index;
  int i;
  char tbuf[128];

  ntracks = GPS_Command_Get_Lap(portname, &array, waypt_read_cb);
  if (ntracks <= 0) {
    return;
  }
  for (i = 0; i < ntracks; i++) {
    Waypoint* wpt;
    if (array[i]->index == -1) {
      index=i;
    } else {
      index=array[i]->index;
      index=i;
    }

    if ((trk_head == NULL) || (i == 0) ||
        /* D906 - last track:index is the track index */
        (array[i]->index == -1 && array[i]->track_index != 255) ||
        /* D10xx - no real separator, use begin/end time to guess */
        (abs(array[i-1]->start_time + array[i]->total_time/100-array[i]->start_time) > 2)
       ) {
      static struct tm* stmp;
      stmp = gmtime(&array[i]->start_time);
      trk_head = new route_head;
      /*For D906, we would like to use the track_index in the last packet instead...*/
      trk_head->rte_num = ++trk_num;
      strftime(tbuf, 32, "%Y-%m-%dT%H:%M:%SZ", stmp);
      trk_head->rte_name = tbuf;
      track_add_head(trk_head);

      wpt = new Waypoint;

      wpt->longitude = array[i]->begin_lon;
      wpt->latitude = array[i]->begin_lat;
      wpt->heartrate = array[i]->avg_heart_rate;
      wpt->cadence = array[i]->avg_cadence;
      wpt->speed = array[i]->max_speed;
      wpt->creation_time = array[i]->start_time;
      wpt->microseconds = 0;

      sprintf(tbuf, "#%d-0", index);
      wpt->shortname = tbuf;
      sprintf(tbuf, "D:%f Cal:%d MS:%f AH:%d MH:%d AC:%d I:%d T:%d",
              array[i]->total_distance, array[i]->calories, array[i]->max_speed, array[i]->avg_heart_rate,
              array[i]->max_heart_rate, array[i]->avg_cadence, array[i]->intensity, array[i]->trigger_method);
      wpt->description = tbuf;
      track_add_wpt(trk_head, wpt);
    }
    /*Allow even if no correct location, no skip if invalid */
    /*		if (array[i]->no_latlon) {
    *			continue;
    *		}
    */
    wpt = new Waypoint;

    wpt->longitude = array[i]->end_lon;
    wpt->latitude = array[i]->end_lat;
    wpt->heartrate = array[i]->avg_heart_rate;
    wpt->cadence = array[i]->avg_cadence;
    wpt->speed = array[i]->max_speed;
    wpt->creation_time = array[i]->start_time + array[i]->total_time/100;
    wpt->microseconds = 10000*(array[i]->total_time % 100);
    /*Add fields with no mapping in the description */
    sprintf(tbuf, "#%d", index);
    wpt->shortname = tbuf;
    sprintf(tbuf, "D:%f Cal:%d MS:%f AH:%d MH:%d AC:%d I:%d T:%d (%f,%f)",
            array[i]->total_distance, array[i]->calories, array[i]->max_speed, array[i]->avg_heart_rate,
            array[i]->max_heart_rate, array[i]->avg_cadence, array[i]->intensity, array[i]->trigger_method,
            array[i]->begin_lon, array[i]->begin_lat);
    wpt->description = tbuf;

    track_add_wpt(trk_head, wpt);
  }
  while (ntracks) {
    GPS_Lap_Del(&array[--ntracks]);
  }
  xfree(array);
}
#endif

/*
 * Rather than propagate Garmin-specific data types outside of the Garmin
 * code, we convert the PVT (position/velocity/time) data from the receiver
 * to the data type we use throughout.   Yes, we do lose some data that way.
 */
static void
pvt2wpt(GPS_PPvt_Data pvt, Waypoint* wpt)
{
  wpt->altitude = pvt->alt;
  wpt->latitude = pvt->lat;
  wpt->longitude = pvt->lon;
  WAYPT_SET(wpt,course,1);
  WAYPT_SET(wpt,speed,1);

  wpt->course = 180 + DEG(atan2(-pvt->east, -pvt->north));

  /* velocity in m/s */
  WAYPT_SET(wpt,speed, sqrt(pvt->north*pvt->north + pvt->east*pvt->east));
  // wpt->vs = pvt->up;

  /*
   * The unit reports time in three fields:
   * 1) The # of days to most recent Sun. since  1989-12-31 midnight UTC.
   * 2) The number of seconds (fractions allowed) since that Sunday.
   * 3) The number of leap seconds that offset the current UTC and GPS
   *    reference clocks.
   */
  double wptime = 631065600.0 + pvt->wn_days * 86400.0  +
    pvt->tow
    - pvt->leap_scnds;
  double wptimes = floor(wptime);
  wpt->SetCreationTime(wptimes, 1000000.0 * (wptime - wptimes));

  /*
   * The Garmin spec fifteen different models that use a different
   * table for 'fix' without a really good way to tell if the model
   * we're talking to happens to be one of those...By inspection,
   * it looks like even though the models (Summit, Legend, etc.) may
   * be popular, it's older (2001 and earlier or so) versions that
   * are affected and I think there are relatively few readers of
   * the fix field anyway.   Time will tell if this is a good plan.
   */
  switch (pvt->fix) {
  case 0:
    wpt->fix = fix_unknown;
    break;
  case 1:
    wpt->fix = fix_none;
    break;
  case 2:
    wpt->fix = fix_2d;
    break;
  case 3:
    wpt->fix = fix_3d;
    break;
  case 4:
    wpt->fix = fix_dgps;
    break; /* 2D_diff */
  case 5:
    wpt->fix = fix_dgps;
    break; /* 3D_diff */
  default:
    /* undocumented type. */
    break;
  }
}

static gpsdevh* pvt_fd;

static void
pvt_init(const QString& fname)
{
  rw_init(fname);
  GPS_Command_Pvt_On(qPrintable(fname), &pvt_fd);
}

static Waypoint*
pvt_read(posn_status* posn_status)
{
  auto* wpt = new Waypoint;
  GPS_PPvt_Data pvt = GPS_Pvt_New();

  if (GPS_Command_Pvt_Get(&pvt_fd, &pvt)) {
    pvt2wpt(pvt, wpt);
    GPS_Pvt_Del(&pvt);

    wpt->shortname = "Position";

    if (gps_errno && posn_status) {
      posn_status->request_terminate = 1;
    }

    return wpt;
  }

  /*
   * If the caller has not given us a better way to return the
   * error, do it now.
   */
  if (gps_errno) {
    fatal(MYNAME ": Fatal error reading position.\n");
  }

  delete wpt;
  GPS_Pvt_Del(&pvt);

  return nullptr;
}

static void
data_read()
{
  if (gpx_vec) {
    gpx_vec->read();
    return;
  }

  if (poweroff) {
    return;
  }

  if (global_opts.masked_objective & WPTDATAMASK) {
    waypt_read();
  }
  if (global_opts.masked_objective & TRKDATAMASK) {
    track_read();
  }
  if (global_opts.masked_objective & RTEDATAMASK) {
    route_read();
  }
  if (!(global_opts.masked_objective &
        (WPTDATAMASK | TRKDATAMASK | RTEDATAMASK | POSNDATAMASK))) {
    fatal(MYNAME ": Nothing to do.\n");
  }
}

static GPS_PWay
sane_GPS_Way_New()
{
  GPS_PWay way = GPS_Way_New();
  if (!way) {
    fatal(MYNAME ":not enough memory\n");
  }

  /*
   *  Undo less than helpful defaults from Way_New.
   */
  way->rte_ident[0] = 0;
  way->rte_cmnt[0] = 0;
  way->rte_link_subclass[0] = 0;
  way->rte_link_ident[0] = 0;
  way->city[0] = 0;
  way->state[0] = 0;
  way->facility[0] = 0;
  way->addr[0] = 0;
  way->cross_road[0] = 0;
  way->dpth = 1.0e25f;
  way->wpt_class = 0;  // user waypoint by default.

  return way;
}

static int
waypt_write_cb(GPS_PWay*)
{
  int n = waypt_count();

  if (global_opts.verbose_status) {
    static int i;
    i++;
    waypt_status_disp(n, i);
  }
  return 0;
}

/*
 * If we're using smart names, try to put the cache info in the
 * description.
 */
static const char*
get_gc_info(const Waypoint* wpt)
{
  if (global_opts.smart_names) {
    if (wpt->gc_data->type == gt_virtual) {
      return  "V ";
    }
    if (wpt->gc_data->type == gt_unknown) {
      return  "? ";
    }
    if (wpt->gc_data->type == gt_multi) {
      return  "Mlt ";
    }
    if (wpt->gc_data->type == gt_earth) {
      return  "EC ";
    }
    if (wpt->gc_data->type == gt_event) {
      return  "Ev ";
    }
    if (wpt->gc_data->container == gc_micro) {
      return  "M ";
    }
    if (wpt->gc_data->container == gc_small) {
      return  "S ";
    }
  }
  return "";
}

static int
waypoint_prepare()
{
  int i;
  int n = waypt_count();
  extern WaypointList* global_waypoint_list;
  int icon;

  tx_waylist = (struct GPS_SWay**) xcalloc(n,sizeof(*tx_waylist));

  for (i = 0; i < n; i++) {
    tx_waylist[i] = sane_GPS_Way_New();
  }

  i = 0;

  // Iterate with waypt_disp_all?
  for (const Waypoint* wpt : qAsConst(*global_waypoint_list)) {
    char obuf[256];

    QString src;
    if (!wpt->description.isEmpty()) {
      src = wpt->description;
    }
    if (!wpt->notes.isEmpty()) {
      src = wpt->notes;
    }

    /*
     * mkshort will do collision detection and namespace
     * cleaning
     */
    char* ident = mkshort(mkshort_handle,
                           global_opts.synthesize_shortnames ? CSTRc(src) :
                             CSTRc(wpt->shortname), false);
    /* Should not be a strcpy as 'ident' isn't really a C string,
     * but rather a garmin "fixed length" buffer that's padded
     * to the end with spaces.  So this is NOT (strlen+1).
     */
    memcpy(tx_waylist[i]->ident, ident, strlen(ident));

    if (global_opts.synthesize_shortnames) {
      xfree(ident);
    }
    tx_waylist[i]->ident[sizeof(tx_waylist[i]->ident)-1] = 0;

    // If we were explicitly given a comment from GPX, use that.
    //  This logic really is horrible and needs to be untangled.
    if (!wpt->description.isEmpty() &&
        global_opts.smart_names && !wpt->gc_data->diff) {
      memcpy(tx_waylist[i]->cmnt, CSTRc(wpt->description), strlen(CSTRc(wpt->description)));
    } else {
      if (global_opts.smart_names &&
          wpt->gc_data->diff && wpt->gc_data->terr) {
        snprintf(obuf, sizeof(obuf), "%s%u/%u %s",
                 get_gc_info(wpt),
                 wpt->gc_data->diff, wpt->gc_data->terr,
                 CSTRc(src));
        memcpy(tx_waylist[i]->cmnt, obuf, strlen(obuf));
      } else  {
        memcpy(tx_waylist[i]->cmnt, CSTRc(src), strlen(CSTRc(src)));
      }
    }



    tx_waylist[i]->lon = wpt->longitude;
    tx_waylist[i]->lat = wpt->latitude;

    if (deficon) {
      icon = gt_find_icon_number_from_desc(deficon, PCX);
    } else {
      if (get_cache_icon(wpt)) {
        icon = gt_find_icon_number_from_desc(get_cache_icon(wpt), PCX);
      } else {
        icon = gt_find_icon_number_from_desc(wpt->icon_descr, PCX);
      }
    }

    /* For units that support tiny numbers of waypoints, just
     * overwrite that and go very literal.
     */
    if (gps_waypt_type == 103) {
      icon = d103_icon_number_from_symbol(wpt->icon_descr);
    }
    tx_waylist[i]->smbl = icon;
    if (wpt->altitude == unknown_alt) {
      tx_waylist[i]->alt_is_unknown = 1;
      tx_waylist[i]->alt = 0;
    } else {
      tx_waylist[i]->alt = wpt->altitude;
    }
    gpsbabel::DateTime t = wpt->GetCreationTime();
    if (t.isValid()) {
      tx_waylist[i]->time = t.toTime_t();
      tx_waylist[i]->time_populated = 1;
    }
    if (category) {
      tx_waylist[i]->category = 1 << (atoi(category) - 1);
    }
    if (categorybits) {
      tx_waylist[i]->category = categorybits;
    }
    garmin_fs_garmin_before_write(wpt, tx_waylist[i], gps_waypt_type);
    i++;
  }

  return n;
}

static void
waypoint_write()
{
  int n = waypoint_prepare();

  if (int32 ret = GPS_Command_Send_Waypoint(portname, tx_waylist, n, waypt_write_cb); ret < 0) {
    fatal(MYNAME ":communication error sending waypoints..\n");
  }

  for (int i = 0; i < n; ++i) {
    GPS_Way_Del(&tx_waylist[i]);
  }
  if (global_opts.verbose_status) {
    fprintf(stdout, "\r\n");
    fflush(stdout);
  }
  xfree(tx_waylist);
}

static void
route_hdr_pr(const route_head* rte)
{
  (*cur_tx_routelist_entry)->rte_num = rte->rte_num;
  (*cur_tx_routelist_entry)->isrte = 1;
  if (!rte->rte_name.isEmpty()) {
    strncpy((*cur_tx_routelist_entry)->rte_ident, CSTRc(rte->rte_name),
            sizeof((*cur_tx_routelist_entry)->rte_ident) - 1);
    (*cur_tx_routelist_entry)->rte_ident[sizeof((*cur_tx_routelist_entry)->rte_ident) - 1] = 0;
  }
}

static void
route_waypt_pr(const Waypoint* wpt)
{
  GPS_PWay rte = *cur_tx_routelist_entry;

  /*
   * As stupid as this is, libjeeps seems to want an empty
   * waypoint between every link in a route that has nothing
   * but the 'islink' member set.   Rather than "fixing" libjeeps,
   * we just double them up (sigh) and do that here.
   */
  rte->islink = 1;
  rte->lon = wpt->longitude;
  rte->lat = wpt->latitude;
  cur_tx_routelist_entry++;
  rte = *cur_tx_routelist_entry;

  rte->lon = wpt->longitude;
  rte->lat = wpt->latitude;
  rte->smbl = gt_find_icon_number_from_desc(wpt->icon_descr, PCX);

  // map class so unit doesn't duplicate routepoints as a waypoint.
  rte->wpt_class = 0x80;

  if (wpt->altitude != unknown_alt) {
    rte->alt = wpt->altitude;
  } else {
    rte->alt_is_unknown = 1;
    rte->alt = 0;
  }

  char* d = rte->ident;
  for (auto idx : wpt->shortname) {
    int c = idx.toLatin1();
    if (receiver_must_upper && isalpha(c)) {
      c = toupper(c);
    }
    if (strchr(valid_waypt_chars, c)) {
      *d++ = c;
    }
  }

  rte->ident[sizeof(rte->ident)-1] = 0;
  if (wpt->description.isEmpty()) {
    rte->cmnt[0] = 0;
  } else {
    strncpy(rte->cmnt, CSTR(wpt->description), sizeof(rte->cmnt) - 1);
    rte->cmnt[sizeof(rte->cmnt)-1] = 0;
  }
  cur_tx_routelist_entry++;
}

static void
route_write()
{
  int n = 2 * route_waypt_count(); /* Doubled for the islink crap. */

  tx_routelist = (struct GPS_SWay**) xcalloc(n,sizeof(GPS_PWay));
  cur_tx_routelist_entry = tx_routelist;

  for (int i = 0; i < n; i++) {
    tx_routelist[i] = sane_GPS_Way_New();
  }

  route_disp_all(route_hdr_pr, nullptr, route_waypt_pr);
  GPS_Command_Send_Route(portname, tx_routelist, n);
}

static void
track_hdr_pr(const route_head* trk_head)
{
  (*cur_tx_tracklist_entry)->ishdr = true;
  if (!trk_head->rte_name.isEmpty()) {
    strncpy((*cur_tx_tracklist_entry)->trk_ident, CSTRc(trk_head->rte_name), sizeof((*cur_tx_tracklist_entry)->trk_ident) - 1);
    (*cur_tx_tracklist_entry)->trk_ident[sizeof((*cur_tx_tracklist_entry)->trk_ident)-1] = 0;
  } else {
    sprintf((*cur_tx_tracklist_entry)->trk_ident, "TRACK%02d", my_track_count);
  }
  cur_tx_tracklist_entry++;
  my_track_count++;
}

static void
track_waypt_pr(const Waypoint* wpt)
{
  (*cur_tx_tracklist_entry)->lat = wpt->latitude;
  (*cur_tx_tracklist_entry)->lon = wpt->longitude;
  (*cur_tx_tracklist_entry)->alt = (wpt->altitude != unknown_alt) ? wpt->altitude : 1e25;
  (*cur_tx_tracklist_entry)->Time = wpt->GetCreationTime().toTime_t();
  if (!wpt->shortname.isEmpty()) {
    strncpy((*cur_tx_tracklist_entry)->trk_ident, CSTRc(wpt->shortname), sizeof((*cur_tx_tracklist_entry)->trk_ident) - 1);
    (*cur_tx_tracklist_entry)->trk_ident[sizeof((*cur_tx_tracklist_entry)->trk_ident)-1] = 0;
  }
  (*cur_tx_tracklist_entry)->tnew = wpt->wpt_flags.new_trkseg;
  cur_tx_tracklist_entry++;
}

static int
track_prepare()
{
  int32 n = track_waypt_count() + track_count();

  tx_tracklist = (struct GPS_STrack**) xcalloc(n, sizeof(GPS_PTrack));
  cur_tx_tracklist_entry = tx_tracklist;
  for (int i = 0; i < n; i++) {
    tx_tracklist[i] = GPS_Track_New();
  }
  my_track_count = 0;
  track_disp_all(track_hdr_pr, nullptr, track_waypt_pr);

  GPS_Prepare_Track_For_Device(&tx_tracklist, &n);

  return n;
}

static void
track_write()
{
  int n = track_prepare();
  GPS_Command_Send_Track(portname, tx_tracklist, n, (eraset)? 1 : 0);

  for (int i = 0; i < n; i++) {
    GPS_Track_Del(&tx_tracklist[i]);
  }
  xfree(tx_tracklist);
}

static void
course_write()
{
  int i;

  int n_wpt = waypoint_prepare();
  int n_trk = track_prepare();

  GPS_Command_Send_Track_As_Course(portname, tx_tracklist, n_trk,
                                   tx_waylist, n_wpt, (eraset)? 1 : 0);

  for (i = 0; i < n_wpt; ++i) {
    GPS_Way_Del(&tx_waylist[i]);
  }
  xfree(tx_waylist);

  for (i = 0; i < n_trk; i++) {
    GPS_Track_Del(&tx_tracklist[i]);
  }
  xfree(tx_tracklist);
}

static void
data_write()
{
  if (poweroff) {
    return;
  }

  /* If we have both trackpoints and waypoints and the device
   * supports courses, combine them to a course. Otherwise,
   * send tracks & waypoints separately.
   */
  if ((global_opts.masked_objective & WPTDATAMASK) &&
      (global_opts.masked_objective & TRKDATAMASK) &&
      gps_course_transfer != -1) {
    course_write();
  } else {
    if (global_opts.masked_objective & WPTDATAMASK) {
      waypoint_write();
    }
    if (global_opts.masked_objective & TRKDATAMASK) {
      track_write();
    }
  }
  if (global_opts.masked_objective & RTEDATAMASK) {
    route_write();
  }
}


ff_vecs_t garmin_vecs = {
  ff_type_serial,
  FF_CAP_RW_ALL,
  rd_init,
  rw_init,
  rw_deinit,
  rw_deinit,
  data_read,
  data_write,
  nullptr,
  &garmin_args,
  CET_CHARSET_ASCII, 0,
  { pvt_init, pvt_read, rw_deinit, nullptr, nullptr, nullptr },
  nullptr
};

static const char* d103_icons[16] = {
  "dot",
  "house",
  "gas",
  "car",
  "fish",
  "boat",
  "anchor",
  "wreck",
  "exit",
  "skull",
  "flag",
  "camp",
  "circle_x",
  "deer",
  "1st_aid",
  "back-track"
};

static const char*
d103_symbol_from_icon_number(unsigned int n)
{
  if (n  <= 15) {
    return d103_icons[n];
  } else {
    return "unknown";
  }
}

static int
d103_icon_number_from_symbol(const QString& s)
{
  if (s.isNull()) {
    return 0;
  }

  for (unsigned int i = 0; i < sizeof(d103_icons) / sizeof(d103_icons[0]); i++) {
    if (0 == (s.compare(d103_icons[i], Qt::CaseInsensitive))) {
      return i;
    }
  }
  return 0;
}