Rev 8554 | Rev 8556 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8554 | Rev 8555 | ||
---|---|---|---|
Line 13... | Line 13... | ||
13 | #include |
13 | #include |
14 | #include |
14 | #include |
15 | #include |
15 | #include |
16 | #include |
16 | #include |
Line 17... | Line 17... | ||
17 | 17 | ||
Line 18... | Line 18... | ||
18 | #define VERSION "Weather 1.2b" |
18 | #define VERSION "Weather 1.3" |
19 | 19 | ||
20 | enum BUTTONS{ |
20 | enum BUTTONS{ |
21 | BTN_QUIT = 1, |
21 | BTN_QUIT = 1, |
Line 22... | Line -... | ||
22 | BTN_UPDATE = 2 |
- | |
23 | }; |
- | |
24 | - | ||
25 | #define JSON_OBJ(X) value->u.object.values[X] |
- | |
26 | #define OK 200 |
22 | BTN_UPDATE = 2 |
27 | #define API "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric" |
23 | }; |
28 | #define IMAGE_URL "http://openweathermap.org/img/w/%s.png" |
24 | |
- | 25 | #define START_YPOS 34 |
|
- | 26 | #define UTF8_W 8 |
|
Line 29... | Line 27... | ||
29 | #define START_YPOS 34 |
27 | #define CP866_W 6 |
- | 28 | #define JSON_OBJ(X) value->u.object.values[X] |
|
- | 29 | #define OK 200 |
|
- | 30 | ||
Line 30... | Line 31... | ||
30 | #define UTF8_W 8 |
31 | unsigned WINDOW_W = 200; |
31 | #define CP866_W 6 |
32 | |
- | 33 | #define API "api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s&lang=%s" |
|
- | 34 | #define IMAGE_URL "openweathermap.org/img/w/%s.png" |
|
- | 35 | ||
- | 36 | Image *image; |
|
Line -... | Line 37... | ||
- | 37 | Image *blend; |
|
- | 38 | char *units; |
|
32 | 39 | unsigned char char_size=1; |
|
33 | #define WINDOW_W 200 |
40 | |
- | 41 | char *wind_speed_str, *pressure_str, *visibility_str, *humidity_str, *update_str; |
|
- | 42 | ||
- | 43 | char lang[3]="en"; |
|
34 | 44 | char format_temp_str[6]; |
|
Line 35... | Line 45... | ||
35 | Image *image; |
45 | char full_url[512]; |
Line 36... | Line 46... | ||
36 | Image *blend; |
46 | char full_url_image[256]; |
37 | 47 | ||
38 | char full_url[512]; |
48 | char temp_char='K'; |
39 | char full_url_image[256]; |
49 | |
40 | struct kolibri_system_colors sys_color_table; |
50 | struct kolibri_system_colors sys_color_table; |
41 | 51 | ||
42 | pos_t win_pos; |
52 | pos_t win_pos; |
43 | - | ||
44 | #pragma pack(push,1) |
53 | |
45 | typedef struct { |
54 | #pragma pack(push,1) |
46 | char City[256]; |
55 | struct open_weather_data{ |
47 | int wind_speed; |
56 | char City[100]; |
48 | //int wind_deg; |
57 | int wind_speed; |
49 | int pressure; |
58 | int wind_deg; |
50 | int humidity; |
59 | int pressure; |
Line 51... | Line -... | ||
51 | //char weath_main[256]; |
- | |
52 | char weath_desc[256]; |
- | |
53 | int visibility; |
60 | int humidity; |
54 | int timezone; |
61 | char weath_desc[100]; |
55 | char image_code[4]; |
62 | int visibility; |
56 | int temp; |
63 | int timezone; |
Line 57... | Line 64... | ||
57 | }open_weather_data; |
64 | char image_code[4]; |
58 | #pragma pack(pop) |
65 | int temp; |
59 | 66 | }myw; |
|
60 | open_weather_data myw; |
67 | #pragma pack(pop) |
61 | 68 | ||
62 | void notify_show(char *text) |
69 | void notify_show(char *text) |
63 | { |
70 | { |
64 | start_app("/sys/@notify", text); |
71 | start_app("/sys/@notify", text); |
Line 84... | Line 91... | ||
84 | vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap); |
91 | vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap); |
85 | va_end(ap); |
92 | va_end(ap); |
86 | draw_text_sys(tmp_buff, x, y , 0, color); |
93 | draw_text_sys(tmp_buff, x, y , 0, color); |
87 | } |
94 | } |
Line 88... | Line 95... | ||
88 | 95 | ||
89 | void find_and_set(json_value *value, open_weather_data* weather) |
96 | void find_and_set(json_value *value, struct open_weather_data* weather) |
90 | { |
97 | { |
91 | for(int i=0; i |
98 | for(int i=0; i |
92 | if(!strcmp(JSON_OBJ(i).name, "main")){ |
99 | if(!strcmp(JSON_OBJ(i).name, "main")){ |
93 | if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double) |
100 | if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double) |
Line 122... | Line 129... | ||
122 | } |
129 | } |
123 | if(!strcmp(JSON_OBJ(i).name, "message")){ |
130 | if(!strcmp(JSON_OBJ(i).name, "message")){ |
124 | char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6); |
131 | char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6); |
125 | sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr); |
132 | sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr); |
126 | notify_show(errmsg); |
133 | notify_show(errmsg); |
127 | free(errmsg); |
134 | user_free(errmsg); |
128 | } |
135 | } |
129 | } |
136 | } |
130 | } |
137 | } |
Line 131... | Line 138... | ||
131 | 138 | ||
132 | http_msg* get_json(char *City, char *Token) |
139 | http_msg* get_json(char *City, char *Token, char* Units) |
133 | { |
140 | { |
134 | sprintf(full_url, API, City, Token); |
141 | sprintf(full_url, API, City, Token, Units, lang); |
135 | http_msg *h = http_get(full_url, 0, HTTP_FLAG_BLOCK, ""); |
142 | http_msg *h = http_get(full_url, 0, HTTP_FLAG_BLOCK, ""); |
136 | http_long_receive(h); |
143 | http_long_receive(h); |
137 | if (h->status == OK || h->status == 404) { |
144 | if (h->status == OK || h->status == 404) { |
138 | return h; |
145 | return h; |
- | 146 | } else { |
|
- | 147 | user_free(h->content_ptr); |
|
139 | } else { |
148 | user_free(h); |
140 | return NULL; |
149 | return NULL; |
141 | } |
150 | } |
Line 142... | Line 151... | ||
142 | } |
151 | } |
Line 153... | Line 162... | ||
153 | if (!image) { |
162 | if (!image) { |
154 | notify_show("'Convetring image error!' -E"); |
163 | notify_show("'Convetring image error!' -E"); |
155 | exit(0); |
164 | exit(0); |
156 | } |
165 | } |
157 | } |
166 | } |
158 | user_free(h->content_ptr); |
- | |
159 | user_free(h); |
- | |
160 | blend = img_create(64, 64, IMAGE_BPP32); // Create an empty layer |
167 | blend = img_create(64, 64, IMAGE_BPP32); // Create an empty layer |
161 | img_fill_color(blend, 64, 64, sys_color_table.work_area); // Fill the layer with one color |
168 | img_fill_color(blend, 64, 64, sys_color_table.work_area); // Fill the layer with one color |
162 | Image* image2 = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64); |
169 | Image* image2 = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64); |
163 | img_blend(blend, image2, 0, 0, 0, 0, 64, 64); // Blending images to display the alpha channel. |
170 | img_blend(blend, image2, 0, 0, 0, 0, 64, 64); // Blending images to display the alpha channel. |
164 | img_destroy(image); |
171 | img_destroy(image); |
165 | img_destroy(image2); |
172 | img_destroy(image2); |
166 | }else{ |
173 | }else{ |
167 | notify_show("'Image not loaded!!' -W"); |
174 | notify_show("'Image not loaded!!' -W"); |
168 | } |
175 | } |
- | 176 | user_free(h->content_ptr); |
|
- | 177 | user_free(h); |
|
169 | } |
178 | } |
Line 170... | Line 179... | ||
170 | 179 | ||
171 | void RedrawGUI() //Рисуем окно |
180 | void RedrawGUI() |
172 | { |
181 | { |
Line 173... | Line 182... | ||
173 | begin_draw(); //Начинаем рисование интерфейса ) |
182 | begin_draw(); |
174 | 183 | ||
175 | int new_win_w = (strlen(myw.City)+11)*UTF8_W; |
184 | int new_win_w = (strlen(myw.City)/char_size+10)*(UTF8_W+char_size-1); |
176 | if(new_win_w |
185 | if(new_win_w |
Line 177... | Line 186... | ||
177 | new_win_w=WINDOW_W; |
186 | new_win_w=WINDOW_W; |
Line 178... | Line 187... | ||
178 | } |
187 | } |
179 | 188 | ||
Line 180... | Line 189... | ||
180 | sys_create_window(win_pos.x, win_pos.y, new_win_w, START_YPOS+200, VERSION, sys_color_table.work_area, 0x14); // Создаём окно. |
189 | sys_create_window(win_pos.x, win_pos.y, new_win_w, START_YPOS+200, VERSION, sys_color_table.work_area, 0x14); |
Line 181... | Line 190... | ||
181 | 190 | ||
182 | draw_format_text_sys(20, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone); |
191 | draw_format_text_sys(20, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone); |
Line 183... | Line 192... | ||
183 | draw_format_text_sys(21, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone); |
192 | draw_format_text_sys(21, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone); |
184 | 193 | ||
Line 185... | Line 194... | ||
185 | img_draw(blend, 10, START_YPOS+30, 64,64,0,0); |
194 | img_draw(blend, 10, START_YPOS+30, 64,64,0,0); |
186 | 195 | ||
187 | draw_format_text_sys(20, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc); |
196 | draw_format_text_sys(20, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc); |
188 | draw_format_text_sys(21, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc); |
197 | draw_format_text_sys(21, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc); |
Line 189... | Line 198... | ||
189 | 198 | ||
190 | draw_format_text_sys(100, START_YPOS+45, 0xB1000000 | sys_color_table.work_text, "%+d°C", myw.temp); |
199 | draw_format_text_sys(100, START_YPOS+45, 0xb1000000 | sys_color_table.work_text, format_temp_str, myw.temp); |
191 | draw_format_text_sys(101, START_YPOS+46, 0xB1000000 | sys_color_table.work_text, "%+d°C", myw.temp); |
200 | draw_format_text_sys(101, START_YPOS+46, 0xb1000000 | sys_color_table.work_text, format_temp_str, myw.temp); |
192 | 201 | ||
Line 193... | Line 202... | ||
193 | draw_format_text_sys(20, START_YPOS+80, 0x90000000 | sys_color_table.work_text, "Pressure: %d hPa",myw.pressure); |
202 | draw_format_text_sys(20, START_YPOS+80, 0xb0000000 | sys_color_table.work_text, pressure_str,myw.pressure); |
194 | draw_format_text_sys(20, START_YPOS+100, 0x90000000 | sys_color_table.work_text, "Humidity: %d %s", myw.humidity, "%"); |
203 | draw_format_text_sys(20, START_YPOS+100, 0xb0000000 | sys_color_table.work_text, humidity_str, myw.humidity, "%"); |
Line 219... | Line 228... | ||
219 | *City = JSON_OBJ(i).value->u.string.ptr; |
228 | *City = JSON_OBJ(i).value->u.string.ptr; |
220 | } |
229 | } |
221 | if(!strcmp(JSON_OBJ(i).name, "Token")){ |
230 | if(!strcmp(JSON_OBJ(i).name, "Token")){ |
222 | *Token = JSON_OBJ(i).value->u.string.ptr; |
231 | *Token = JSON_OBJ(i).value->u.string.ptr; |
223 | } |
232 | } |
- | 233 | if(!strcmp(JSON_OBJ(i).name, "Celsius")){ |
|
- | 234 | if(JSON_OBJ(i).value->u.boolean){ |
|
- | 235 | units = "metric"; |
|
- | 236 | temp_char = 'C'; |
|
- | 237 | }else{ |
|
- | 238 | units = "imperial"; |
|
- | 239 | temp_char = 'F'; |
|
- | 240 | } |
|
- | 241 | } |
|
- | 242 | if(!strcmp(JSON_OBJ(i).name, "Lang")){ |
|
- | 243 | strncpy(lang, JSON_OBJ(i).value->u.string.ptr,2); |
|
- | 244 | } |
|
224 | } |
245 | } |
225 | if(*City==NULL || *Token ==NULL){ |
246 | if(*City==NULL || *Token ==NULL){ |
226 | notify_show("'Invalid config!!' -E"); |
247 | notify_show("'Invalid config!' -E"); |
227 | exit(0); |
248 | exit(0); |
228 | } |
249 | } |
229 | free(config_buff); |
250 | user_free(config_buff); |
230 | fclose(config_j); |
251 | fclose(config_j); |
231 | } |
252 | } |
Line 232... | Line 253... | ||
232 | 253 | ||
233 | void Update(char* city, char* token) |
254 | void Update(char* city, char* token) |
234 | { |
255 | { |
235 | if(blend!=NULL){ |
256 | if(blend!=NULL){ |
- | 257 | img_destroy(blend); |
|
236 | img_destroy(blend); |
258 | blend = NULL; |
237 | } |
259 | } |
238 | memset(&myw, 0, sizeof myw); |
260 | memset(&myw, 0, sizeof myw); |
239 | strcpy(myw.City,"None"); |
261 | strcpy(myw.City,"None"); |
240 | strcpy(myw.weath_desc,"unknown"); |
262 | strcpy(myw.weath_desc,"unknown"); |
241 | http_msg *json_file = get_json(city, token); |
263 | http_msg *json_file = get_json(city, token, units); |
242 | if(json_file != NULL){ |
264 | if(json_file != NULL){ |
243 | json_value* value=json_parse (json_file->content_ptr, json_file->content_length); |
265 | json_value* value=json_parse (json_file->content_ptr, json_file->content_length); |
- | 266 | find_and_set(value, &myw); |
|
244 | find_and_set(value, &myw); |
267 | sprintf(format_temp_str, "%s°%c","%d",temp_char); |
245 | get_image(); |
268 | get_image(); |
246 | json_value_free(value); |
269 | json_value_free(value); |
247 | user_free(json_file->content_ptr); |
270 | user_free(json_file->content_ptr); |
248 | user_free(json_file); |
- | |
249 | 271 | user_free(json_file); |
|
250 | }else{ |
272 | }else{ |
251 | notify_show("'Connection error!' -E"); |
273 | notify_show("'Connection error!' -E"); |
252 | } |
274 | } |
Line -... | Line 275... | ||
- | 275 | } |
|
- | 276 | ||
- | 277 | void set_lang() |
|
- | 278 | { |
|
- | 279 | if(!strcmp(lang, "ru")){ |
|
- | 280 | wind_speed_str = "Скорость ветра: %d м/с"; |
|
- | 281 | pressure_str = "Давление: %d гПa"; |
|
- | 282 | visibility_str = "Видимость: %d м"; |
|
- | 283 | humidity_str = "Влажность: %d%s"; |
|
- | 284 | update_str = "Обновить"; |
|
- | 285 | char_size = 2; |
|
- | 286 | WINDOW_W = 230; |
|
- | 287 | }else if(!strcmp(lang, "de")){ |
|
- | 288 | wind_speed_str = "Windgeschwindigkeit: %d m/s"; |
|
- | 289 | pressure_str = "Druck: %d hPa"; |
|
- | 290 | visibility_str = "Sichtbarkeit: %d m"; |
|
- | 291 | humidity_str = "Luftfeuchtigkeit: %d%s"; |
|
- | 292 | WINDOW_W = 270; |
|
- | 293 | update_str = "Aktualisieren"; |
|
- | 294 | }else{ |
|
- | 295 | pressure_str = "Pressure: %d hPa"; |
|
- | 296 | humidity_str = "Humidity: %d%s"; |
|
- | 297 | visibility_str = "Visibility: %d m"; |
|
- | 298 | wind_speed_str = "Wind speed: %d m/s"; |
|
- | 299 | update_str = "Refresh"; |
|
- | 300 | } |
|
253 | } |
301 | } |
254 | 302 | ||
255 | int main(){ |
303 | int main(){ |
256 | win_pos = get_mouse_pos(0); |
304 | win_pos = get_mouse_pos(0); |
257 | if(!kolibri_libimg_init()){ |
305 | if(!kolibri_libimg_init()){ |
258 | notify_show("Libimg.obj not loaded!' -E"); |
306 | notify_show("Libimg.obj not loaded!' -E"); |
259 | exit(0); |
307 | exit(0); |
260 | } |
308 | } |
261 | get_system_colors(&sys_color_table); |
309 | get_system_colors(&sys_color_table); |
- | 310 | char *City = NULL; |
|
262 | char *City = NULL; |
311 | char *Token = NULL; |
- | 312 | ||
263 | char *Token = NULL; |
313 | get_config(&City, &Token); |
Line 264... | Line 314... | ||
264 | get_config(&City, &Token); |
314 | set_lang(); |
265 | Update(City,Token); |
315 | Update(City,Token); |
266 | 316 | ||
267 | while(1){ |
317 | while(1){ |
268 | switch(get_os_event()){ |
318 | switch(get_os_event()){ |
269 | case KOLIBRI_EVENT_NONE: |
319 | case KOLIBRI_EVENT_NONE: |
270 | break; |
320 | break; |
271 | case KOLIBRI_EVENT_REDRAW: |
321 | case KOLIBRI_EVENT_REDRAW: |
272 | RedrawGUI(); |
322 | RedrawGUI(); |
273 | break; |
323 | break; |
274 | case KOLIBRI_EVENT_BUTTON: // Событие обработки кнопок |
324 | case KOLIBRI_EVENT_BUTTON: |
275 | switch (get_os_button()){ |
325 | switch (get_os_button()){ |
276 | case BTN_UPDATE: |
326 | case BTN_UPDATE: |