Subversion Repositories Kolibri OS

Rev

Rev 8553 | Rev 8555 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8553 Rev 8554
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.0b"
18
#define VERSION  "Weather 1.2b"
19
 
19
 
20
enum BUTTONS{
20
enum BUTTONS{
21
    BTN_QUIT = 1,
21
    BTN_QUIT = 1,
Line 25... Line 25...
25
#define JSON_OBJ(X) value->u.object.values[X]
25
#define JSON_OBJ(X) value->u.object.values[X]
26
#define OK 200
26
#define OK 200
27
#define API       "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric"
27
#define API       "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric"
28
#define IMAGE_URL "http://openweathermap.org/img/w/%s.png"
28
#define IMAGE_URL "http://openweathermap.org/img/w/%s.png"
29
#define START_YPOS 34 
29
#define START_YPOS 34 
-
 
30
#define UTF8_W 8
-
 
31
#define CP866_W 6
-
 
32
 
-
 
33
#define WINDOW_W 200
Line 30... Line 34...
30
 
34
 
-
 
35
Image *image;
-
 
36
Image *blend;
31
Image *image;
37
 
32
char full_url[256];
38
char full_url[512];
33
char full_url_image[256];
39
char full_url_image[256];
Line -... Line 40...
-
 
40
struct kolibri_system_colors sys_color_table;
-
 
41
 
34
struct kolibri_system_colors sys_color_table;
42
pos_t win_pos; 
35
 
43
 
36
#pragma pack(push,1)
44
#pragma pack(push,1)
37
typedef struct {
45
typedef struct {
38
    char *City;
46
    char City[256];
39
    float wind_speed;
47
    int wind_speed;
40
    //int wind_deg;
48
    //int wind_deg;
41
    int pressure;
49
    int pressure;
42
    int humidity;
50
    int humidity;
43
    char *weath_main;
51
    //char weath_main[256];
44
    char *weath_desc;
52
    char weath_desc[256];
45
    int visibility;
53
    int visibility;
46
    int timezone;
54
    int timezone;
47
    char* image_code;
55
    char image_code[4];
48
    double temp;
56
    int temp;
Line 49... Line 57...
49
}open_weather_data;
57
}open_weather_data;
Line 65... Line 73...
65
    }else{
73
    }else{
66
        return p;
74
        return p;
67
    }
75
    }
68
}
76
}
Line -... Line 77...
-
 
77
 
-
 
78
 char tmp_buff[100];
69
 
79
 
70
static void draw_format_text_sys(int x, int y, color_t color, const char *format_str, ... )
80
static void draw_format_text_sys(int x, int y, color_t color, const char *format_str, ... )
71
{
-
 
72
    char tmp_buff[100];
81
{
73
    va_list ap;
82
    va_list ap;
74
    va_start (ap, format_str);
83
    va_start (ap, format_str);
75
    vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap);
84
    vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap);
76
    va_end(ap);
85
    va_end(ap);
Line 79... Line 88...
79
 
88
 
80
void find_and_set(json_value *value, open_weather_data* weather)
89
void find_and_set(json_value *value, open_weather_data* weather)
81
{
90
{
82
    for(int i=0; iu.object.length; i++){
91
    for(int i=0; iu.object.length; i++){
-
 
92
        if(!strcmp(JSON_OBJ(i).name, "main")){
-
 
93
            if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double)
83
        if(!strcmp(JSON_OBJ(i).name, "main")){
94
            {
-
 
95
                weather->temp = (int)JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
-
 
96
            }else{
-
 
97
                weather->temp = JSON_OBJ(i).value->u.object.values[0].value->u.integer;
84
            weather->temp = JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
98
            }
85
            weather->pressure = JSON_OBJ(i).value->u.object.values[4].value->u.integer;
99
            weather->pressure = JSON_OBJ(i).value->u.object.values[4].value->u.integer;
86
            weather->humidity = JSON_OBJ(i).value->u.object.values[5].value->u.integer;
100
            weather->humidity = JSON_OBJ(i).value->u.object.values[5].value->u.integer;
87
        }
101
        }
88
        if(!strcmp(JSON_OBJ(i).name, "name")){
102
        if(!strcmp(JSON_OBJ(i).name, "name")){
89
            weather->City = JSON_OBJ(i).value->u.string.ptr;
103
            strcpy(weather->City,JSON_OBJ(i).value->u.string.ptr);
90
        }
104
        }
91
        if(!strcmp(JSON_OBJ(i).name, "weather")){
105
        if(!strcmp(JSON_OBJ(i).name, "weather")){
92
           weather->weath_desc = JSON_OBJ(i).value->u.array.values[0]->u.object.values[2].value->u.string.ptr;
106
           strcpy(weather->weath_desc, JSON_OBJ(i).value->u.array.values[0]->u.object.values[2].value->u.string.ptr);
93
           weather->image_code = JSON_OBJ(i).value->u.array.values[0]->u.object.values[3].value->u.string.ptr;
107
           strcpy(weather->image_code, JSON_OBJ(i).value->u.array.values[0]->u.object.values[3].value->u.string.ptr);
94
        }
108
        }
-
 
109
        if(!strcmp(JSON_OBJ(i).name, "wind")){
-
 
110
            if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double)
95
        if(!strcmp(JSON_OBJ(i).name, "wind")){
111
            {
-
 
112
                weather->wind_speed = (int)JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
-
 
113
            }else{
-
 
114
                weather->wind_speed = JSON_OBJ(i).value->u.object.values[0].value->u.integer;
96
            weather->wind_speed = JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
115
            }  
97
        }
116
        }
98
        if(!strcmp(JSON_OBJ(i).name, "visibility")){
117
        if(!strcmp(JSON_OBJ(i).name, "visibility")){
99
            weather->visibility = JSON_OBJ(i).value->u.integer;
118
            weather->visibility = JSON_OBJ(i).value->u.integer;
100
        }
119
        }
Line 103... Line 122...
103
        }
122
        }
104
        if(!strcmp(JSON_OBJ(i).name, "message")){
123
        if(!strcmp(JSON_OBJ(i).name, "message")){
105
            char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6);
124
            char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6);
106
            sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr);
125
            sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr);
107
            notify_show(errmsg);
126
            notify_show(errmsg);
-
 
127
            free(errmsg);
108
        }
128
        }
109
    }
129
    }
110
}
130
}
Line 111... Line 131...
111
 
131
 
Line 133... Line 153...
133
                if (!image) {
153
                if (!image) {
134
                notify_show("'Convetring image error!' -E");  
154
                notify_show("'Convetring image error!' -E");  
135
                exit(0);
155
                exit(0);
136
            }
156
            }
137
        }
157
        }
-
 
158
        user_free(h->content_ptr);
-
 
159
        user_free(h);
138
        //blend = img_create(64, 64, IMAGE_BPP32);  // Create an empty layer
160
        blend = img_create(64, 64, IMAGE_BPP32);  // Create an empty layer
139
        //img_fill_color(blend, 64, 64, sys_color_table.work_area); // Fill the layer with one color
161
        img_fill_color(blend, 64, 64, sys_color_table.work_area); // Fill the layer with one color
140
        image = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64);
162
        Image* image2 = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64);
141
        //img_blend(blend, image, 0, 0, 0, 0, 64, 64);  // Blending images to display the alpha channel. 
163
        img_blend(blend, image2, 0, 0, 0, 0, 64, 64);  // Blending images to display the alpha channel. 
-
 
164
        img_destroy(image);
-
 
165
        img_destroy(image2);
142
    }else{
166
    }else{
143
       notify_show("'Image not loaded!!' -W"); 
167
       notify_show("'Image not loaded!!' -W"); 
144
    }  
168
    }  
145
}
169
}
Line 146... Line 170...
146
 
170
 
147
void RedrawGUI() //Рисуем окно
171
void RedrawGUI() //Рисуем окно
148
{
-
 
149
    char buff[1000];
-
 
150
    pos_t win_pos = get_mouse_pos(0); // Получаем координаты курсора
172
{
151
    begin_draw(); //Начинаем рисование интерфейса )
-
 
Line -... Line 173...
-
 
173
    begin_draw(); //Начинаем рисование интерфейса )
-
 
174
    
-
 
175
    int new_win_w = (strlen(myw.City)+11)*UTF8_W;
-
 
176
    if(new_win_w
-
 
177
        new_win_w=WINDOW_W;
-
 
178
    }
-
 
179
    
152
    sys_create_window(win_pos.x, win_pos.y, 220, START_YPOS+200, VERSION, 0xffffff, 0x14); // Создаём окно.
180
    sys_create_window(win_pos.x, win_pos.y, new_win_w, START_YPOS+200, VERSION, sys_color_table.work_area, 0x14); // Создаём окно.
153
 
181
 
Line 154... Line 182...
154
    draw_format_text_sys(10, START_YPOS, 0xB0000000 /*| sys_color_table.work_text*/,  "%s (UTC%+d)\0", myw.City, myw.timezone);
182
    draw_format_text_sys(20, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone);
Line 155... Line 183...
155
    draw_format_text_sys(11, START_YPOS, 0xB0000000 /*| sys_color_table.work_text */, "%s (UTC%+d)\0", myw.City, myw.timezone);
183
    draw_format_text_sys(21, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone);
156
 
184
 
Line 157... Line 185...
157
    img_draw(image, 10, START_YPOS+30, 64,64,0,0);
185
    img_draw(blend, 10, START_YPOS+30, 64,64,0,0);
158
 
186
 
Line 159... Line 187...
159
    draw_format_text_sys(10, START_YPOS+20, 0xb0000000 /* | sys_color_table.work_text */, myw.weath_desc);
187
    draw_format_text_sys(20, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc);
160
    draw_format_text_sys(11, 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);
161
 
189
 
162
    draw_format_text_sys(90, START_YPOS+45, 0xB1000000 /*| sys_color_table.work_text*/, "%.1f °C", myw.temp);  
190
    draw_format_text_sys(100, START_YPOS+45, 0xB1000000 | sys_color_table.work_text, "%+d°C", myw.temp);  
Line 163... Line 191...
163
    draw_format_text_sys(91, START_YPOS+46, 0xB1000000 /*| sys_color_table.work_text*/, "%.1f °C", myw.temp);
191
    draw_format_text_sys(101, START_YPOS+46, 0xB1000000 | sys_color_table.work_text, "%+d°C", myw.temp);
164
 
192
 
165
    draw_format_text_sys(10, START_YPOS+80, 0x90000000 /*| sys_color_table.work_text*/,  "Pressure:   %d hPa",myw.pressure);
193
    draw_format_text_sys(20, START_YPOS+80, 0x90000000 | sys_color_table.work_text,  "Pressure:   %d hPa",myw.pressure);
166
    draw_format_text_sys(10, START_YPOS+100, 0x90000000 /* | sys_color_table.work_text*/,"Humidity:   %d %s", myw.humidity, "%");
194
    draw_format_text_sys(20, START_YPOS+100, 0x90000000 | sys_color_table.work_text, "Humidity:   %d %s", myw.humidity, "%");
Line 167... Line 195...
167
    draw_format_text_sys(10, START_YPOS+120, 0x90000000 /*| sys_color_table.work_text*/, "Wind speed: %.1f m/s", myw.wind_speed);
195
    draw_format_text_sys(20, START_YPOS+120, 0x90000000 | sys_color_table.work_text, "Wind speed: %d m/s", myw.wind_speed);
168
    draw_format_text_sys(10, START_YPOS+140, 0x90000000 /*| sys_color_table.work_text*/, "Visibility: %d m", myw.visibility);
196
    draw_format_text_sys(20, START_YPOS+140, 0x90000000 | sys_color_table.work_text, "Visibility: %d m", myw.visibility);
Line 197... Line 225...
197
    if(*City==NULL || *Token ==NULL){
225
    if(*City==NULL || *Token ==NULL){
198
         notify_show("'Invalid config!!' -E");
226
         notify_show("'Invalid config!!' -E");
199
         exit(0);
227
         exit(0);
200
    }
228
    }
201
    free(config_buff);
229
    free(config_buff);
-
 
230
    fclose(config_j);
202
}
231
}
Line 203... Line 232...
203
 
232
 
204
void Update(char* city, char* token)
233
void Update(char* city, char* token)
-
 
234
{
-
 
235
    if(blend!=NULL){
-
 
236
        img_destroy(blend);
205
{
237
    }
206
    memset(&myw, 0, sizeof myw);
238
    memset(&myw, 0, sizeof myw);
207
    myw.City="None";
239
    strcpy(myw.City,"None");
208
    myw.weath_desc="unknown";
240
    strcpy(myw.weath_desc,"unknown");
209
    http_msg *json_file = get_json(city, token);
241
    http_msg *json_file = get_json(city, token);
210
    if(json_file != NULL){
242
    if(json_file != NULL){
211
        json_value* value =json_parse (json_file->content_ptr, json_file->content_length);
243
        json_value* value=json_parse (json_file->content_ptr, json_file->content_length);
212
        find_and_set(value, &myw);
244
        find_and_set(value, &myw);
-
 
245
        get_image();
-
 
246
        json_value_free(value);
-
 
247
        user_free(json_file->content_ptr);
-
 
248
        user_free(json_file);
213
        get_image();
249
 
214
    }else{
250
    }else{
215
        notify_show("'Connection error!' -E");
251
       notify_show("'Connection error!' -E");
216
    }
252
    }
Line 217... Line 253...
217
}
253
}
-
 
254
 
218
 
255
int main(){
-
 
256
    win_pos = get_mouse_pos(0);
-
 
257
    if(!kolibri_libimg_init()){
-
 
258
        notify_show("Libimg.obj not loaded!' -E");
219
int main(){
259
        exit(0);
220
    kolibri_libimg_init();
-
 
221
    get_system_colors(&sys_color_table);
260
    }
222
    
261
    get_system_colors(&sys_color_table);
223
    char *City = NULL;
262
    char *City = NULL;
224
    char *Token = NULL;
263
    char *Token = NULL;