|
| 1 | +#include <math.h> |
| 2 | +#include <fcntl.h> |
1 | 3 | #include <stdio.h>
|
2 |
| -#include <stdlib.h> |
| 4 | +#include <stddef.h> |
| 5 | +#include <stdint.h> |
3 | 6 | #include <string.h>
|
| 7 | +#include <unistd.h> |
| 8 | +#include <assert.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <dirent.h> |
4 | 11 | #include <stdbool.h>
|
| 12 | + |
| 13 | +#include <linux/fb.h> |
| 14 | +#include <sys/time.h> |
| 15 | +#include <sys/wait.h> |
| 16 | +#include <sys/ioctl.h> |
| 17 | +#include <sys/types.h> |
| 18 | + |
5 | 19 | #include <curl/curl.h>
|
6 | 20 |
|
| 21 | +#include <cjson/cJSON.h> |
| 22 | + |
| 23 | +#include <kot-graphics/font.h> |
| 24 | +#include <kot-graphics/utils.h> |
| 25 | +#include <kot-graphics/image.h> |
| 26 | + |
7 | 27 | #include "../apps/apps.h"
|
8 | 28 | #include "../launch/launch.h"
|
9 | 29 | #include "../update/update.h"
|
10 | 30 | #include "../install/install.h"
|
11 | 31 | #include "../remove/remove.h"
|
12 | 32 |
|
13 |
| -int main(int argc, char *argv[]){ |
14 |
| - curl_global_init(CURL_GLOBAL_DEFAULT); |
15 |
| - CURL *curl = curl_easy_init(); |
| 33 | +int fb_fd = -1; |
| 34 | +kframebuffer_t fb; |
| 35 | +CURL* curl = NULL; |
| 36 | +kfont_t font = NULL; |
| 37 | +char* font_path = NULL; |
| 38 | +struct fb_fix_screeninfo fix_screeninfo; |
| 39 | +struct fb_var_screeninfo var_screeninfo; |
16 | 40 |
|
17 |
| - if(curl == NULL){ |
18 |
| - printf("Can't load curl"); |
19 |
| - return -1; |
| 41 | +size_t json_size = 0; |
| 42 | +void* json_buffer = NULL; |
| 43 | +FILE* json_file = NULL; |
| 44 | +cJSON* json_root = NULL; |
| 45 | +cJSON* font_path_json = NULL; |
| 46 | + |
| 47 | +uint32_t header_part_width = 0; |
| 48 | +char* header_text[] = {"Install <F1>", "Update <F2>", "Uninstall <F3>"}; |
| 49 | + |
| 50 | +int load_fb(){ |
| 51 | + fb_fd = open("/dev/fb0", O_RDWR); |
| 52 | + |
| 53 | + assert(fb_fd >= 0); |
| 54 | + |
| 55 | + |
| 56 | + assert(ioctl(fb_fd, FBIOGET_FSCREENINFO, &fix_screeninfo) == 0); |
| 57 | + assert(ioctl(fb_fd, FBIOGET_VSCREENINFO, &var_screeninfo) == 0); |
| 58 | + |
| 59 | + if( |
| 60 | + fix_screeninfo.visual == FB_VISUAL_TRUECOLOR && |
| 61 | + var_screeninfo.bits_per_pixel == 32 && |
| 62 | + var_screeninfo.red.length == 8 && var_screeninfo.red.msb_right == 0 && |
| 63 | + var_screeninfo.green.length == 8 && var_screeninfo.green.msb_right == 0 && |
| 64 | + var_screeninfo.blue.length == 8 && var_screeninfo.blue.msb_right == 0 |
| 65 | + ){ |
| 66 | + fb.bpp = var_screeninfo.bits_per_pixel; |
| 67 | + fb.btpp = fb.bpp / 8; |
| 68 | + fb.size = var_screeninfo.xres_virtual * var_screeninfo.yres_virtual * fb.btpp; |
| 69 | + fb.width = var_screeninfo.xres_virtual; |
| 70 | + fb.pitch = fb.width * fb.btpp; |
| 71 | + fb.height = var_screeninfo.yres_virtual; |
| 72 | + fb.buffer = malloc(fb.size); |
| 73 | + memset(fb.buffer, 0, fb.size); |
20 | 74 | }else{
|
21 |
| - if(!strcmp(argv[1], "--install") && argc == 2){ |
22 |
| - char search_mode[3]; |
23 |
| - printf("Search with > T(tag)/N(name)\n"); |
24 |
| - fgets(search_mode, sizeof(search_mode), stdin); |
25 |
| - search_mode[strcspn(search_mode, "\n")] = 0; |
26 |
| - if(!strcmp("T", search_mode)){ |
27 |
| - char tag[512]; |
28 |
| - printf("What is the tag of the application you want to install?\n"); |
29 |
| - fgets(tag, sizeof(tag), stdin); |
30 |
| - tag[strcspn(tag, "\n")] = 0; |
31 |
| - |
32 |
| - app_url_by_tag_t** apps_available = find_apps_url_by_tag(curl, tag); |
33 |
| - |
34 |
| - if(apps_available != NULL){ |
35 |
| - int i = 0; |
36 |
| - while(apps_available[i] != NULL){ |
37 |
| - printf("%d) %s\n", i, apps_available[i]->name); |
38 |
| - i++; |
39 |
| - } |
40 |
| - |
41 |
| - char link_index[10]; |
42 |
| - printf("Select the index you want to install :\n"); |
43 |
| - fgets(link_index, sizeof(link_index), stdin); |
44 |
| - link_index[strcspn(link_index, "\n")] = 0; |
45 |
| - |
46 |
| - int index_to_install = atoi(link_index); |
47 |
| - |
48 |
| - if(index_to_install >= 0 && index_to_install < i){ |
49 |
| - char allow_install[3]; |
50 |
| - printf("%s > Would you like to install it? (Y/N)\n", apps_available[index_to_install]->name); |
51 |
| - fgets(allow_install, sizeof(allow_install), stdin); |
52 |
| - allow_install[strcspn(allow_install, "\n")] = 0; |
53 |
| - if(!strcmp("Y", allow_install)){ |
54 |
| - install_app(curl, apps_available[index_to_install]->url, apps_available[index_to_install]->name, false); |
55 |
| - }else{ |
56 |
| - printf("Cancel the installation\n"); |
57 |
| - } |
58 |
| - }else{ |
59 |
| - printf("Unknow index !\n"); |
60 |
| - } |
61 |
| - |
62 |
| - free_app_url_by_tag(apps_available); |
63 |
| - }else{ |
64 |
| - printf("No application found with tag: %s. Please check the spelling or try a different tag.\n", tag); |
65 |
| - } |
66 |
| - }else if(!strcmp("N", search_mode)){ |
67 |
| - char name[512]; |
68 |
| - printf("What is the name of the application you want to install?\n"); |
69 |
| - fgets(name, sizeof(name), stdin); |
70 |
| - name[strcspn(name, "\n")] = 0; |
71 |
| - |
72 |
| - char* url = find_apps_url_by_name(curl, name); |
73 |
| - |
74 |
| - if(url != NULL){ |
75 |
| - char allow_install[3]; |
76 |
| - printf("%s found in the store. Would you like to install it? (Y/N)\n", name); |
77 |
| - fgets(allow_install, sizeof(allow_install), stdin); |
78 |
| - allow_install[strcspn(allow_install, "\n")] = 0; |
79 |
| - if(!strcmp("Y", allow_install)){ |
80 |
| - install_app(curl, url, name, false); |
81 |
| - }else{ |
82 |
| - printf("Cancel the installation\n"); |
83 |
| - } |
84 |
| - free(url); |
85 |
| - }else{ |
86 |
| - printf("Can't find %s in the store. Did you spell it correctly?\n", name); |
87 |
| - } |
88 |
| - }else{ |
89 |
| - printf("Unknow search method !\n"); |
90 |
| - } |
91 |
| - }else if(!strcmp(argv[1], "--install") && argc == 3){ |
92 |
| - char* name = argv[2]; |
93 |
| - |
94 |
| - char* url = find_apps_url_by_name(curl, name); |
| 75 | + perror("'/dev/fb0' : format not supported"); |
| 76 | + return EXIT_FAILURE; |
| 77 | + } |
| 78 | + |
| 79 | + return EXIT_SUCCESS; |
| 80 | +} |
| 81 | + |
| 82 | +void unload_fb(){ |
| 83 | + free(fb.buffer); |
| 84 | + close(fb_fd); |
| 85 | +} |
| 86 | + |
| 87 | +int load_json(){ |
| 88 | + json_file = fopen("/usr/bin/res/store-ui/store-ui.json", "r"); |
| 89 | + |
| 90 | + if(json_file == NULL){ |
| 91 | + perror("Error: Unable to open the file : /usr/bin/res/store-ui/store-ui.json\n"); |
| 92 | + return EXIT_FAILURE; |
| 93 | + } |
| 94 | + |
| 95 | + fseek(json_file, 0, SEEK_END); |
| 96 | + json_size = ftell(json_file); |
| 97 | + fseek(json_file, 0, SEEK_SET); |
| 98 | + |
| 99 | + json_buffer = malloc(json_size); |
| 100 | + fread(json_buffer, 1, json_size, json_file); |
| 101 | + |
| 102 | + json_root = cJSON_Parse(json_buffer); |
| 103 | + |
| 104 | + if(json_root != NULL){ |
| 105 | + font_path_json = cJSON_GetObjectItem(json_root, "font_path"); |
| 106 | + if(cJSON_IsString(font_path_json) && (font_path_json->valuestring != NULL)){ |
| 107 | + font_path = strdup(font_path_json->valuestring); |
| 108 | + }else{ |
| 109 | + free(json_buffer); |
| 110 | + |
| 111 | + fclose(json_file); |
95 | 112 |
|
96 |
| - if(url != NULL){ |
97 |
| - char allow_install[3]; |
98 |
| - printf("%s found in the store. Would you like to install it? (Y/N)\n", name); |
99 |
| - fgets(allow_install, sizeof(allow_install), stdin); |
100 |
| - allow_install[strcspn(allow_install, "\n")] = 0; |
101 |
| - if(!strcmp("Y", allow_install)){ |
102 |
| - install_app(curl, url, name, false); |
103 |
| - }else{ |
104 |
| - printf("Cancel the installation\n"); |
105 |
| - } |
106 |
| - free(url); |
107 |
| - }else{ |
108 |
| - printf("Can't find %s in the store. Did you spell it correctly?\n", name); |
109 |
| - } |
110 |
| - }else if(!strcmp(argv[1], "--update") && argc == 3){ |
111 |
| - char* name = argv[2]; |
112 |
| - update_app(curl, name); |
113 |
| - }else if(!strcmp(argv[1], "--remove") && argc == 3){ |
114 |
| - char* name = argv[2]; |
115 |
| - remove_app(name, true); |
116 |
| - }else if(!strcmp(argv[1], "--launch") && argc >= 3){ |
117 |
| - char* name = argv[2]; |
118 |
| - // Keep the app name |
119 |
| - launch_app(name, argc - 2, &argv[2]); |
| 113 | + printf("Error: Unable to parse the file : /usr/bin/res/store-ui/store-ui.json\n"); |
| 114 | + return EXIT_FAILURE; |
120 | 115 | }
|
| 116 | + }else{ |
| 117 | + free(json_buffer); |
| 118 | + |
| 119 | + fclose(json_file); |
| 120 | + |
| 121 | + printf("Error: Unable to parse the file : /usr/bin/res/image-reader/image-reader.json\n"); |
| 122 | + return EXIT_FAILURE; |
| 123 | + } |
| 124 | + |
| 125 | + return EXIT_SUCCESS; |
| 126 | +} |
| 127 | + |
| 128 | +void unload_json(){ |
| 129 | + free(font_path); |
| 130 | +} |
| 131 | + |
| 132 | +int load_font_data(){ |
| 133 | + FILE* font_file = fopen(font_path, "rb"); |
| 134 | + if(font_file == NULL){ |
| 135 | + perror("Failed to open font file"); |
| 136 | + return EXIT_FAILURE; |
| 137 | + } |
| 138 | + |
| 139 | + fseek(font_file, 0, SEEK_END); |
| 140 | + size_t font_file_size = ftell(font_file); |
| 141 | + fseek(font_file, 0, SEEK_SET); |
| 142 | + |
| 143 | + void* font_data = malloc(font_file_size); |
| 144 | + fread(font_data, font_file_size, 1, font_file); |
| 145 | + fclose(font_file); |
| 146 | + |
| 147 | + font = load_font(font_data, font_file_size); |
| 148 | + free(font_data); |
| 149 | + |
| 150 | + return EXIT_SUCCESS; |
| 151 | +} |
| 152 | + |
| 153 | +void unload_font_data(){ |
| 154 | + free_font(font); |
| 155 | +} |
| 156 | + |
| 157 | +int load_curl(){ |
| 158 | + curl_global_init(CURL_GLOBAL_DEFAULT); |
| 159 | + curl = curl_easy_init(); |
| 160 | + |
| 161 | + if(curl == NULL){ |
| 162 | + perror("Can't load curl"); |
| 163 | + return EXIT_FAILURE; |
121 | 164 | }
|
122 | 165 |
|
| 166 | + return EXIT_SUCCESS; |
| 167 | +} |
| 168 | + |
| 169 | +void unload_curl(){ |
123 | 170 | curl_easy_cleanup(curl);
|
124 | 171 |
|
125 | 172 | curl_global_cleanup();
|
| 173 | +} |
| 174 | + |
| 175 | +int main(int argc, char *argv[]){ |
| 176 | + if(load_fb() != EXIT_SUCCESS){ |
| 177 | + return EXIT_FAILURE; |
| 178 | + } |
| 179 | + |
| 180 | + if(load_json() != EXIT_SUCCESS){ |
| 181 | + unload_fb(); |
| 182 | + return EXIT_FAILURE; |
| 183 | + } |
| 184 | + |
| 185 | + if(load_font_data(font_path) != EXIT_SUCCESS){ |
| 186 | + unload_json(); |
| 187 | + unload_fb(); |
| 188 | + return EXIT_FAILURE; |
| 189 | + } |
| 190 | + |
| 191 | + if(load_curl() != EXIT_SUCCESS){ |
| 192 | + unload_fb(); |
| 193 | + unload_font_data(); |
| 194 | + return EXIT_FAILURE; |
| 195 | + } |
| 196 | + |
| 197 | + while(true){ |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + unload_fb(); |
| 202 | + unload_curl(); |
| 203 | + unload_json(); |
| 204 | + unload_font_data(); |
126 | 205 |
|
127 |
| - return 0; |
| 206 | + return EXIT_SUCCESS; |
128 | 207 | }
|
0 commit comments