1054b8fc645167bc54739748c5fd353206c1fdff
[expresskeys.git] / src-server / config_read.c
1 /*
2  * config_read.c -- Support ExpressKeys & Touch Strips on a Wacom Intuos3 tablet.
3  *
4  * Copyright (C) 2005 - Mats Johannesson
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
19  *
20  */
21
22 #include "globals.h"
23
24 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
25 /* Function reads a configuration file containing program names and */
26 /* definitions of which keys that should be mapped to pad buttons and */
27 /* touch strips. It takes a file pointer and a pointer to a global */
28 /* structure as input. Returns nothing unless an error occured.*/
29 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
30
31 int read_file_config(int *ip, FILE *fp)
32 {
33
34         struct program *p;
35         p = (void *)*ip;
36
37         int i = 0;
38         int j = 0;
39         char c;
40         
41         int num_record = 0;
42         int num_field = 0;
43
44         char buffer [MAXBUFFER];
45
46 /* Previously allocated memory for the program names must be released */
47 /* on subsequent reads of the config file. At program start the flag is 0 */
48
49         if (reread_config) {
50                 for (i = 0; i < num_list; i++, p++) {
51                         free(p->class_name);
52                 }
53                 p = (void *)*ip;
54         }
55         reread_config = 1;
56
57 /* Read the config file in one go from top to bottom. Parse out the info */
58 /* between record start "{" and record end "}". Recognize field ends by a */
59 /* comma ",".Each full record is written to a global memory structure, */
60 /* while partial records are discarded and excessive fields are truncated. */
61 /* No sanity check on program names or keycode functionality is performed */
62 /* A global counter variable of full record instances is updated before */
63 /* the function exits to reflect the new state. */
64
65         while ((c = fgetc(fp)) != EOF) {
66                 if (num_record < MAXRECORDS) {
67                         if (c == '{') {
68                                 while ((c = fgetc(fp)) != EOF && (c != ',')) {
69                                                 if ((j < MAXBUFFER - 1) && (c != '"') && (isprint(c))) {
70                                                         buffer [j] = c;
71                                                         j++;
72                                                 }
73                                 }
74                                 buffer [j] = '\0';
75                                 i = 0;
76                                 j = 0;
77                                 if ((p->class_name = (char *)malloc(strlen(buffer)+1)) == NULL) {
78                                         return 2;
79                                 }
80                                 sprintf(p->class_name, "%s", buffer);
81                                 while ((c = fgetc(fp)) != EOF && (c != '}')) {
82                                         if (num_field < MAXFIELDS) {
83                                                 if ((c != ',') && (isdigit(c))) {
84                                                         if (i < MAXDIGITS) {
85                                                                 buffer [i] = c;
86                                                                 i++;
87                                                         }
88                                                 }
89                                                 if (c == ',') {
90                                                         buffer [i] = '\0';
91                                                         i=0;
92
93                                                         switch (num_field) {
94                                                                 case 0:
95                                                                 p->handle_touch = atoi(buffer);
96                                                                 break;
97                                                                 case 1:
98                                                                 p->l_touch_up = atoi(buffer);
99                                                                 break;
100                                                                 case 2:
101                                                                 p->l_touch_up_plus = atoi(buffer);
102                                                                 break;
103                                                                 case 3:
104                                                                 p->l_touch_down = atoi(buffer);
105                                                                 break;
106                                                                 case 4:
107                                                                 p->l_touch_down_plus = atoi(buffer);
108                                                                 break;
109                                                                 case 5:
110                                                                 p->r_touch_up = atoi(buffer);
111                                                                 break;
112                                                                 case 6:
113                                                                 p->r_touch_up_plus = atoi(buffer);
114                                                                 break;
115                                                                 case 7:
116                                                                 p->r_touch_down = atoi(buffer);
117                                                                 break;
118                                                                 case 8:
119                                                                 p->r_touch_down_plus = atoi(buffer);
120                                                                 break;
121                                                                 case 9:
122                                                                 p->key_9 = atoi(buffer);
123                                                                 break;
124                                                                 case 10:
125                                                                 p->key_9_plus = atoi(buffer);
126                                                                 break;
127                                                                 case 11:
128                                                                 p->key_10 = atoi(buffer);
129                                                                 break;
130                                                                 case 12:
131                                                                 p->key_10_plus = atoi(buffer);
132                                                                 break;
133                                                                 case 13:
134                                                                 p->key_11 = atoi(buffer);
135                                                                 break;
136                                                                 case 14:
137                                                                 p->key_11_plus = atoi(buffer);
138                                                                 break;
139                                                                 case 15:
140                                                                 p->key_12 = atoi(buffer);
141                                                                 break;
142                                                                 case 16:
143                                                                 p->key_12_plus = atoi(buffer);
144                                                                 break;
145                                                                 case 17:
146                                                                 p->key_13 = atoi(buffer);
147                                                                 break;
148                                                                 case 18:
149                                                                 p->key_13_plus = atoi(buffer);
150                                                                 break;
151                                                                 case 19:
152                                                                 p->key_14 = atoi(buffer);
153                                                                 break;
154                                                                 case 20:
155                                                                 p->key_14_plus = atoi(buffer);
156                                                                 break;
157                                                                 case 21:
158                                                                 p->key_15 = atoi(buffer);
159                                                                 break;
160                                                                 case 22:
161                                                                 p->key_15_plus = atoi(buffer);
162                                                                 break;
163                                                                 case 23:
164                                                                 p->key_16 = atoi(buffer);
165                                                                 break;
166                                                                 case 24:
167                                                                 p->key_16_plus = atoi(buffer);
168                                                                 break;
169                                                                 default:
170                                                                 break;
171                                                         }
172                                                         num_field++;
173                                                 }
174                                         }
175                                 }
176                                 if (num_field == MAXFIELDS) {
177                                         num_record++;
178                                         p++;
179                                 } else {
180                                         free(p->class_name);
181                                 }
182                                 num_field = 0;
183                         }
184                 }
185         }
186         if (!num_record) {
187                 num_list = 0;
188                 return 1;
189         }
190         num_list = num_record;
191         return 0;
192 }
193
194 /* End Code */
195