allow empty lines in config
[sdl_omap.git] / src / video / omapdss / config.c
CommitLineData
b78828bf 1/*
83b5751b 2 * (C) GraÅžvydas "notaz" Ignotas, 2010
b78828bf 3 *
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
0aab059f 9#include <stdlib.h>
b78828bf 10#include <string.h>
11#include <strings.h>
12#include <ctype.h>
13
14#include "omapsdl.h"
15
0aab059f 16int gcfg_force_vsync;
17
b78828bf 18static char *sskip(char *p)
19{
20 while (*p && isspace(*p))
21 p++;
22 return p;
23}
24
25static char *nsskip(char *p)
26{
27 while (*p && !isspace(*p))
28 p++;
29 return p;
30}
31
0aab059f 32static int check_token(char **p_, const char *token)
b78828bf 33{
0aab059f 34 char *p = *p_;
b78828bf 35 int tlen = strlen(token);
0aab059f 36 int ret = strncasecmp(p, token, tlen) == 0 && isspace(p[tlen]);
37 if (ret)
38 *p_ = sskip(p + tlen + 1);
39
40 return ret;
41}
42
43static int check_token_eq(char **p_, const char *token)
44{
45 char *p = *p_;
46 int ret = check_token(&p, token);
47 ret = ret && *p == '=';
48 if (ret)
49 *p_ = sskip(p + 1);
50
51 return ret;
b78828bf 52}
53
54void omapsdl_config(void)
55{
56 char buff[256];
57 FILE *f;
58
59 f = fopen("omapsdl.cfg", "r");
60 if (f == NULL)
61 return;
62
63 while (!feof(f)) {
64 char *p, *line = fgets(buff, sizeof(buff), f);
65 if (line == NULL)
66 break;
67 p = line = sskip(line);
b3747abb 68 if (*p == 0 || *p == '#')
b78828bf 69 continue;
70
0aab059f 71 if (check_token(&p, "bind")) {
b78828bf 72 char *key, *key_end, *sdlkey, *sdlkey_end;
0aab059f 73 key = p;
b78828bf 74 key_end = nsskip(key);
75 p = sskip(key_end);
76 if (*p != '=')
77 goto bad;
78 sdlkey = sskip(p + 1);
79 sdlkey_end = nsskip(sdlkey);
80 p = sskip(sdlkey_end);
81 if (*key == 0 || *sdlkey == 0 || *p != 0)
82 goto bad;
83 *key_end = *sdlkey_end = 0;
84
85 omapsdl_input_bind(key, sdlkey);
86 continue;
87 }
0aab059f 88 else if (check_token_eq(&p, "force_vsync")) {
89 gcfg_force_vsync = strtol(p, NULL, 0);
90 continue;
91 }
b78828bf 92
93bad:
94 err("config: failed to parse: %s", line);
95 }
96 fclose(f);
97}
98
f641fccb 99void omapsdl_config_from_env(void)
100{
101 const char *tmp;
102
103 tmp = getenv("SDL_OMAP_VSYNC");
104 if (tmp != NULL)
105 gcfg_force_vsync = atoi(tmp);
106}
b78828bf 107