File: options.c

package info (click to toggle)
scantool 1.21+dfsg-5
  • links: PTS
  • area: main
  • in suites: sid
  • size: 2,956 kB
  • ctags: 2,392
  • sloc: ansic: 12,279; sh: 829; makefile: 344; asm: 86; perl: 29
file content (378 lines) | stat: -rw-r--r-- 14,046 bytes parent folder | download | duplicates (5)
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
#include <string.h>

#include "globals.h"
#include "custom_gui.h"
#include "serial.h"
#include "options.h"
#include "error_handlers.h"
#ifdef ALLEGRO_WINDOWS
   #include "get_port_names.h"
#else
   #define PORT_NAME_BUF_SIZE    5
#endif

#define MSG_SAVE_OPTIONS   MSG_USER
#define MSG_REFRESH        MSG_USER + 1

// Define defaults
#ifdef ALLEGRO_WINDOWS
   #define DEFAULT_DISPLAY_MODE         WINDOWED_MODE
#else
   #define DEFAULT_DISPLAY_MODE         WINDOWED_MODE
#endif
#define DEFAULT_SYSTEM_OF_MEASURMENTS   IMPERIAL
#define DEFAULT_COMPORT_NUMBER          -1
#define DEFAULT_BAUD_RATE               BAUD_RATE_115200

typedef struct
{
   int option_value;
} OPTION_ELEMENT;


static OPTION_ELEMENT option_metric = {METRIC};
static OPTION_ELEMENT option_imperial = {IMPERIAL};
static OPTION_ELEMENT option_baud_rate_9600 = {BAUD_RATE_9600};
static OPTION_ELEMENT option_baud_rate_38400 = {BAUD_RATE_38400};
static OPTION_ELEMENT option_baud_rate_115200 = {BAUD_RATE_115200};
#ifdef ALLEGRO_WINDOWS
   static OPTION_ELEMENT option_baud_rate_230400 = {BAUD_RATE_230400};
#endif
static OPTION_ELEMENT option_windowed_mode = {WINDOWED_MODE};
static OPTION_ELEMENT option_full_screen_mode = {FULL_SCREEN_MODE};


static int option_element_proc(int msg, DIALOG *d, int c);
static int save_options_proc(int msg, DIALOG *d, int c);
static int comport_list_proc(int msg, DIALOG *d, int c);
static char *listbox_getter(int index, int *list_size);
static void fill_comport_list();
static void clear_comport_list();

static DIALOG options_dialog[] =
{
   /* (proc)              (x)  (y)  (w)  (h)  (fg)     (bg)           (key) (flags) (d1) (d2) (dp)                       (dp2) (dp3)                     */
   { d_shadow_box_proc,   0,   0,   231, 466, 0,       C_LIGHT_GRAY,  0,    0,      0,   0,   NULL,                      NULL, NULL                     },
   { d_shadow_box_proc,   0,   0,   231, 24,  0,       C_DARK_GRAY,   0,    0,      0,   0,   NULL,                      NULL, NULL                     },
   { caption_proc,        115, 2,   113, 19,  C_WHITE, C_TRANSP,      0,    0,      0,   0,   "Program Options",         NULL, NULL                     },
   { d_text_proc,         16,  32,  200, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "System Of Measurements:", NULL, NULL                     },
   { option_element_proc, 48,  56,  80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      0,   0,   "Metric",                  NULL, &option_metric           },
   { option_element_proc, 48,  72,  88,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      0,   0,   "US",                      NULL, &option_imperial         },
   { d_text_proc,         16,  96,  152, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "COM Port:",               NULL, NULL                     },
   { comport_list_proc,   48,  120, 94,  148, C_BLACK, C_LIGHT_GRAY,  0,    0,      0,   0,   listbox_getter,            NULL, NULL                     },
   { d_text_proc,         16,  282, 200, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "Baud Rate:",              NULL, NULL                     },
   { option_element_proc, 20,  306, 80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      1,   0,   "9600",                    NULL, &option_baud_rate_9600   },
   { option_element_proc, 110, 306, 80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      1,   0,   "38400",                   NULL, &option_baud_rate_38400  },
   { option_element_proc, 20,  322, 80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      1,   0,   "115200",                  NULL, &option_baud_rate_115200 },
#ifdef ALLEGRO_WINDOWS
   { option_element_proc, 110, 322, 80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      1,   0,   "230400",                  NULL, &option_baud_rate_230400 },
#endif
   { d_text_proc,         16,  346, 200, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "Display Mode:",           NULL, NULL                     },
   { option_element_proc, 48,  370, 80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      2,   0,   "Windowed",                NULL, &option_windowed_mode    },
   { option_element_proc, 48,  386, 80,  10,  C_BLACK, C_LIGHT_GRAY,  0,    0,      2,   0,   "Full Screen",             NULL, &option_full_screen_mode },
   { save_options_proc,   16,  410, 92,  40,  C_BLACK, C_GREEN,       's',  D_EXIT, 0,   0,   "&Save",                   NULL, NULL                     },
   { d_button_proc,       123, 410, 92,  40,  C_BLACK, C_DARK_YELLOW, 'c',  D_EXIT, 0,   0,   "&Cancel",                 NULL, NULL                     },
   { d_yield_proc,        0,   0,   0,   0,   0,       0,             0,    0,      0,   0,   NULL,                      NULL, NULL                     },
   { NULL,                0,   0,   0,   0,   0,       0,             0,    0,      0,   0,   NULL,                      NULL, NULL                     }
};

static char* comport_list_strings = NULL;
static int* comport_list_numbers = NULL;
static int comport_list_size = 0;



int display_options()
{
   if (display_mode & WINDOWED_MODE_SET)
      display_mode |= WINDOWED_MODE;
   else
      display_mode &= ~WINDOWED_MODE;
   centre_dialog(options_dialog);
   return popup_dialog(options_dialog, -1);
}


char* listbox_getter(int index, int *list_size)
{
   if (index < 0)
   {
      *list_size = comport_list_size;
      return NULL;
   }
   else
      return comport_list_strings + index * PORT_NAME_BUF_SIZE;
}


int comport_list_proc(int msg, DIALOG *d, int c)
{
   int i;
   
   switch (msg)
   {
      case MSG_START:
      case MSG_REFRESH:
         fill_comport_list();
         d->d1 = 0;
         
         if (comport.number >= 0)
         {
            for (i = 0; i < comport_list_size; i++)
               if (comport_list_numbers[i] == comport.number)
                  d->d1 = i;
         }
         
         if (msg == MSG_REFRESH)
            msg = MSG_DRAW;
         break;
         
      case MSG_SAVE_OPTIONS:
         if (d->d1 >= 0 && d->d1 < comport_list_size)
            comport.number = comport_list_numbers[d->d1];
         else
            comport.number = -1;
         break;
         
      case MSG_END:
         clear_comport_list();
   }
   
   return d_list_proc(msg, d, c);
}


int option_element_proc(int msg, DIALOG *d, int c)
{
   OPTION_ELEMENT *option_element = (OPTION_ELEMENT *)d->dp3;

   switch (msg)
   {
      case MSG_START:
      case MSG_REFRESH:
         switch (d->d1)
         {
            case 0:
               if (option_element->option_value == system_of_measurements) // if the element should be selected
                  d->flags |= D_SELECTED;
               else
                  d->flags &= ~D_SELECTED;
               break;
               
            case 1:
               if (option_element->option_value == comport.baud_rate)
                  d->flags |= D_SELECTED;
               else
                  d->flags &= ~D_SELECTED;
               break;
               
            case 2:
               if (option_element->option_value == (display_mode & WINDOWED_MODE))
                  d->flags |= D_SELECTED; // make it selected
               else
                  d->flags &= ~D_SELECTED;
               if (((option_element->option_value == WINDOWED_MODE) && !(display_mode & WINDOWED_MODE_SUPPORTED)) || ((option_element->option_value == FULL_SCREEN_MODE) && !(display_mode & FULLSCREEN_MODE_SUPPORTED)))
                  d->flags |= D_DISABLED;
               else
                  d->flags &= ~D_DISABLED;
               break;
         }
         if (msg == MSG_REFRESH)
            msg = MSG_DRAW;
         break;

      case MSG_SAVE_OPTIONS:
         if (d->flags & D_SELECTED)  // if the element is selected,
            switch (d->d1)
            {
               case 0:
                  system_of_measurements = option_element->option_value;
                  break;
                  
               case 1:
                  comport.baud_rate = option_element->option_value;
                  break;
                  
               case 2:
                  display_mode &= ~WINDOWED_MODE;
                  display_mode |= option_element->option_value;
                  break;
            }
         break;
   
      case MSG_END:
         d->flags &= ~D_SELECTED;  // deselect
         d->flags &= ~D_DISABLED;  // and enable the element
         break;
   }

   return d_radio_proc(msg, d, c);
}


int save_options_proc(int msg, DIALOG *d, int c)
{
   int ret;
   int old_baud_rate;
   BITMAP *bmp;
   FILE *file;

   ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE)
   {
      old_baud_rate = comport.baud_rate;
      broadcast_dialog_message(MSG_SAVE_OPTIONS, 0);
      
      if (comport.baud_rate != old_baud_rate)
      {
         if (alert("WARNING!", "This operation may cause scan tool to stop responding.", "Are you sure you want to change the baud rate?", "Yes", "No", 0, 0) != 1)
         {
            comport.baud_rate = old_baud_rate;
            broadcast_dialog_message(MSG_REFRESH, 0);
            return D_O_K;
         }
      }

      close_comport(); // close current comport
      open_comport(); // try reinitializing comport (comport.status will be set)
      
      if ((!(display_mode & WINDOWED_MODE) && (display_mode & WINDOWED_MODE_SET)) || ((display_mode & WINDOWED_MODE) && !(display_mode & WINDOWED_MODE_SET)))
      {
         bmp = create_bitmap(SCREEN_W, SCREEN_H);
         if (bmp)
         {
            scare_mouse();
            blit(screen, bmp, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            unscare_mouse();

            if (display_mode & WINDOWED_MODE)
            {
               if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) == 0)
                  display_mode |= WINDOWED_MODE_SET;
               else
                  display_mode &= ~WINDOWED_MODE_SUPPORTED;
            }
            else
            {
               if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0) == 0)
                  display_mode &= ~WINDOWED_MODE_SET;
               else
                  display_mode &= ~FULLSCREEN_MODE_SUPPORTED;
            }

            set_pallete(datafile[MAIN_PALETTE].dat);
            gui_fg_color = C_BLACK;  // set the foreground color
            gui_bg_color = C_WHITE;  // set the background color
            gui_mg_color = C_GRAY;   // set the disabled color
            set_mouse_sprite(NULL); // make mouse use current palette
            blit(bmp, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            show_mouse(screen);
            destroy_bitmap(bmp);
         }
         else
            alert("Error switching display modes.", "Not enough memory to save screen.", NULL, "OK", NULL, 0, 0);
      }

      file = fopen(options_file_name, "a");

      if (file == NULL)
         alert("Options could not be saved, because file", options_file_name, "could not be open for writing", "OK", NULL, 0, 0);
      else
      {
         fclose(file);
         save_program_options();
      }
   }

   return ret;
}

// DO NOT TRANSLATE BELOW THIS LINE
void load_program_options()
{
   comport.number = get_config_int("comm", "comport_number", DEFAULT_COMPORT_NUMBER);
   comport.baud_rate = get_config_int("comm", "baud_rate", DEFAULT_BAUD_RATE);
   system_of_measurements = get_config_int("general", "system_of_measurements", DEFAULT_SYSTEM_OF_MEASURMENTS);
   if (get_config_int("general", "display_mode", DEFAULT_DISPLAY_MODE))
      display_mode |= WINDOWED_MODE_SET;
   else
      display_mode &= ~WINDOWED_MODE_SET;
}


void save_program_options()
{
   set_config_int("general", "system_of_measurements", system_of_measurements);
   if (display_mode & WINDOWED_MODE_SET)
      set_config_int("general", "display_mode", WINDOWED_MODE);
   else
      set_config_int("general", "display_mode", FULL_SCREEN_MODE);
   set_config_int("comm", "baud_rate", comport.baud_rate);
   set_config_int("comm", "comport_number", comport.number);
   flush_config_file();
}

void fill_comport_list()
{
   int i;
   
   clear_comport_list();
   
#ifdef ALLEGRO_WINDOWS
   
   if (get_port_names(&comport_list_strings, &comport_list_size) != 0)
      fatal_error("Could not allocate memory for comport_list_strings.");
   
   if (comport_list_size > 0)
   {
      comport_list_numbers = (int*)malloc(comport_list_size * sizeof(int));
      if (comport_list_numbers == NULL)
         fatal_error("Could not allocate memory for comport_list_numbers.");
      
      for (i = 0; i < comport_list_size; i++)
         comport_list_numbers[i] = atoi((comport_list_strings + i * PORT_NAME_BUF_SIZE) + 3) - 1;
   }
   
#else
   
   comport_list_strings = malloc(8 * (sizeof(char) * PORT_NAME_BUF_SIZE));
   if (comport_list_strings == NULL)
      fatal_error("Could not allocate memory for comport_list_strings.");
   
   comport_list_numbers = malloc(8 * sizeof(int));
   if (comport_list_numbers == NULL)
      fatal_error("Could not allocate memory for comport_list_numbers.");
   
   for (i = 0; i < 8; i++)
      sprintf(comport_list_strings + i * PORT_NAME_BUF_SIZE, "COM%i", i + 1);
   
   comport_list_numbers[0] = _com1;
   comport_list_numbers[1] = _com2;
   comport_list_numbers[2] = _com3;
   comport_list_numbers[3] = _com4;
   comport_list_numbers[4] = _com5;
   comport_list_numbers[5] = _com6;
   comport_list_numbers[6] = _com7;
   comport_list_numbers[7] = _com8;
   
   comport_list_size = 8;
   
#endif
}


void clear_comport_list()
{
   if (comport_list_strings != NULL)
   {
      free(comport_list_strings);
      comport_list_strings = NULL;
   }
   if (comport_list_numbers != NULL)
   {
      free(comport_list_numbers);
      comport_list_numbers = NULL;
   }
   comport_list_size = 0;
}