summaryrefslogtreecommitdiff
path: root/src/util_wayland.c
blob: db02498dee6dca257644d339e25b5f7d43df232a (plain)
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
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

#include <dlog.h>

#include <sqlite3.h>
#include <unicode/uloc.h>

#include "widget_errno.h"
#include "util.h"
#include "widget_service.h"
#include "debug.h"

#define CONF_PATH_FORMAT "/usr/share/data-provider-master/%dx%d/resolution.ini"

int errno;

static struct {
	unsigned int w;
	unsigned int h;
	int res_resolved;
} s_info = {
	.w = 0,
	.h = 0,
	.res_resolved = 0,
};

static int update_info(struct supported_size_list *SIZE_LIST, int width_type, int height_type, int width, int height)
{
	int idx;

	if (width_type == 1 && height_type == 1) {
		idx = 0;
	} else if (width_type == 2 && height_type == 1) {
		idx = 1;
	} else if (width_type == 2 && height_type == 2) {
		idx = 2;
	} else if (width_type == 4 && height_type == 1) {
		idx = 3;
	} else if (width_type == 4 && height_type == 2) {
		idx = 4;
	} else if (width_type == 4 && height_type == 3) {
		idx = 5;
	} else if (width_type == 4 && height_type == 4) {
		idx = 6;
	} else if (width_type == 4 && height_type == 5) {
		idx = 7;
	} else if (width_type == 4 && height_type == 6) {
		idx = 8;
	} else if (width_type == 21 && height_type == 21) {
		idx = 9;
	} else if (width_type == 23 && height_type == 21) {
		idx = 10;
	} else if (width_type == 23 && height_type == 23) {
		idx = 11;
	} else if (width_type == 0 && height_type == 0) {
		idx = 12;
	} else {
		ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
		return 0;
	}

	SIZE_LIST[idx].w = width;
	SIZE_LIST[idx].h = height;
	return 1;
}

static inline int update_from_file(struct service_info *info, struct supported_size_list *SIZE_LIST)
{
	FILE *fp;
	int updated;
	int width_type;
	int height_type;
	int width;
	int height;
	char buffer[MAX_COLUMN];
	int ch;
	int idx;
	enum status {
		START = 0x0,
		TYPE = 0x01,
		SIZE = 0x02,
		COMMENT = 0x03,
		ERROR = 0x04,
		EOL = 0x05,
		TYPE_END = 0x06,
		SIZE_START = 0x07
	} status;

	fp = fopen(info->conf_file, "r");
	if (!fp) {
		ErrPrint("Open failed: %s\n", strerror(errno));
		return WIDGET_ERROR_IO_ERROR;
	}

	updated = 0;
	status = START;
	idx = 0;
	do {
		ch = fgetc(fp);

		if (idx == MAX_COLUMN) {
			ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
			status = ERROR;
		}

		switch (status) {
		case START:
			if (isspace(ch) || ch == EOF) {
				continue;
			}

			info->base_parse = 0;

			if (ch == '#') {
				status = COMMENT;
			} else {
				status = TYPE;
				idx = 0;
				ungetc(ch, fp);
			}
			break;
		case TYPE:
			if (isblank(ch)) {
				buffer[idx] = '\0';
				status = TYPE_END;
				if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
					if (!strcasecmp(buffer, "base")) {
						info->base_parse = 1;
					} else {
						ErrPrint("Invalid syntax: [%s]\n", buffer);
						status = ERROR;
					}
				}
				break;
			} else if (ch == '=') {
				buffer[idx] = '\0';
				status = SIZE_START;
				if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
					if (!strcasecmp(buffer, "base")) {
						info->base_parse = 1;
					} else {
						ErrPrint("Invalid syntax: [%s]\n", buffer);
						status = ERROR;
					}
				}
				break;
			} else if (ch == EOF) {
				ErrPrint("Invalid Syntax\n");
				status = ERROR;
				continue;
			}
			buffer[idx++] = ch;
			break;
		case TYPE_END:
			if (ch == '=') {
				status = SIZE_START;
			}
			break;
		case SIZE_START:
			if (isspace(ch) || ch == EOF) {
				continue;
			}

			status = SIZE;
			idx = 0;
			ungetc(ch, fp);
			break;
		case SIZE:
			if (isspace(ch) || ch == EOF) {
				buffer[idx] = '\0';
				status = EOL;

				if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
					ErrPrint("Invalid syntax: [%s]\n", buffer);
					status = ERROR;
				} else if (ch == EOF) {
					if (info->base_parse) {
						info->base_w = width;
						info->base_h = height;
					} else {
						updated += update_info(SIZE_LIST, width_type, height_type, width, height);
					}
				}
				break;
			}
			buffer[idx++] = ch;
			break;
		case EOL:
			if (!info->base_parse) {
				updated += update_info(SIZE_LIST, width_type, height_type, width, height);
			} else {
				info->base_w = width;
				info->base_h =  height;
			}
			status = START;
			ungetc(ch, fp);
			break;
		case ERROR:
			if (ch == '\n' || ch == '\r' || ch == '\f') {
				status = START;
			}
			break;
		case COMMENT:
			if (ch == '\n' || ch == '\r' || ch == '\f') {
				status = START;
			}
			break;
		default:
			ErrPrint("Unknown status. couldn't be reach to here\n");
			break;
		}
	} while (!feof(fp));

	if (fclose(fp) != 0) {
		ErrPrint("fclose: %s\n", strerror(errno));
	}

	return WIDGET_NR_OF_SIZE_LIST - updated;
}

/*
 * Find proper configuration and install(link) it to conf path.
 */
static char *conf_path(void)
{
	char *path;
	int length;

	length = strlen(CONF_PATH_FORMAT) + 12;    // 12 == RESERVED SPACE
	path = calloc(1, length);
	if (!path) {
		ErrPrint("calloc: %s\n", strerror(errno));
		return NULL;
	}

	if (s_info.w == 0 || s_info.h == 0) {
		/* Try to update resolution first if it is not initialized */
		util_screen_size_get(NULL, NULL);
	}

	snprintf(path, length, CONF_PATH_FORMAT, s_info.w, s_info.h);
	DbgPrint("Selected conf file: %s\n", path);
	if (access(path, F_OK) != 0) {
		ErrPrint("Fallback to default, access: %s\n", strerror(errno));
		strncpy(path, RESOLUTION_FILE, length);
		if (access(path, F_OK) != 0) {
			ErrPrint("Serious error - there is no conf file, use default setting: %s\n", strerror(errno));
			free(path);
			path = NULL;
		}
	}

	return path;
}

int util_screen_size_get(unsigned int *width, unsigned int *height)
{
	return WIDGET_ERROR_NONE;
}

int util_update_resolution(struct service_info *info, struct supported_size_list *SIZE_LIST)
{
	if (s_info.res_resolved) {
		return WIDGET_ERROR_NONE;
	}

	if (!info->conf_file) {
		info->conf_file = conf_path();
	}

	if (info->conf_file) {
		register int i;
		unsigned int width;
		unsigned int height;

		i = util_screen_size_get(&width, &height);
		if (i != WIDGET_ERROR_NONE) {
			return i;
		}

		if (update_from_file(info, SIZE_LIST) == 0) {
			DbgPrint("Resolution info is all updated by file\n");
		}

		if (width != info->base_w) {
			for (i = 0; i < WIDGET_NR_OF_SIZE_LIST; i++) {
				SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / (double)info->base_w);
				SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)width / (double)info->base_w);
			}
		}
	} else {
		DbgPrint("Conf file is not loaded\n");
	}

	s_infos_info..res_resolved = 1;
	return WIDGET_ERROR_NONE;
}

/* End of a file */