a70cd3c8f588eba999373d755592a6082a8b3d19
[sdl_omap.git] / config.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2010
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>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include <ctype.h>
13
14 #include "omapsdl.h"
15
16 int gcfg_force_vsync;
17
18 static char *sskip(char *p)
19 {
20         while (*p && isspace(*p))
21                 p++;
22         return p;
23 }
24
25 static char *nsskip(char *p)
26 {
27         while (*p && !isspace(*p))
28                 p++;
29         return p;
30 }
31
32 static int check_token(char **p_, const char *token)
33 {
34         char *p = *p_;
35         int tlen = strlen(token);
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
43 static 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;
52 }
53
54 void 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);
68                 if (*p == 0 || *p == '#')
69                         continue;
70
71                 if (check_token(&p, "bind")) {
72                         char *key, *key_end, *sdlkey, *sdlkey_end;
73                         key = p;
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                 }
88                 else if (check_token_eq(&p, "force_vsync")) {
89                         gcfg_force_vsync = strtol(p, NULL, 0);
90                         continue;
91                 }
92
93 bad:
94                 err("config: failed to parse: %s", line);
95         }
96         fclose(f);
97 }
98
99 void 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 }
107