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

#include <dlog.h>

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

#include "dynamicbox_errno.h"
#include "util.h"
#include "dynamicbox_service.h"
#include "debug.h"

int errno;

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 DBOX_STATUS_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 DBOX_NR_OF_SIZE_LIST - updated;
}

int util_update_resolution(struct service_info *info, struct supported_size_list *SIZE_LIST)
{
	unsigned int width;
	unsigned int height;
	unsigned int border;
	unsigned int depth;
	register int i;
	static int res_resolved = 0;

	if (res_resolved) {
		return DBOX_STATUS_ERROR_NONE;
	}

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

	res_resolved = 1;
	return DBOX_STATUS_ERROR_NONE;
}

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

/* End of a file */