]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/main_gtk.c
Optimization for VentoyGUI
[Ventoy.git] / LinuxGUI / Ventoy2Disk / main_gtk.c
1
2 #include <gtk/gtk.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <time.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <linux/limits.h>
14 #include <ventoy_define.h>
15 #include <ventoy_util.h>
16 #include "ventoy_gtk.h"
17
18 char g_log_file[PATH_MAX];
19 char g_ini_file[PATH_MAX];
20
21 static int set_image_from_pixbuf(GtkBuilder *pBuilder, const char *id, const void *pData, int len)
22 {
23 GtkImage *pImage = NULL;
24 GdkPixbuf *pPixbuf = NULL;
25 GInputStream *pStream = NULL;
26
27 pImage = (GtkImage *)gtk_builder_get_object(pBuilder, id);
28 pStream = g_memory_input_stream_new_from_data(pData, len, NULL);
29 pPixbuf = gdk_pixbuf_new_from_stream(pStream, NULL, NULL);
30 gtk_image_set_from_pixbuf(pImage, pPixbuf);
31
32 return 0;
33 }
34
35 static int set_window_icon_from_pixbuf(GtkWindow *window, const void *pData, int len)
36 {
37 GdkPixbuf *pPixbuf = NULL;
38 GInputStream *pStream = NULL;
39
40 pStream = g_memory_input_stream_new_from_data(pData, len, NULL);
41 pPixbuf = gdk_pixbuf_new_from_stream(pStream, NULL, NULL);
42
43 gtk_window_set_icon(window, pPixbuf);
44 return 0;
45 }
46
47 int early_msgbox(GtkMessageType type, GtkButtonsType buttons, const char *str)
48 {
49 int ret;
50 GtkWidget *pMsgBox = NULL;
51
52 pMsgBox= gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, type, buttons, str);
53
54 ret = gtk_dialog_run(GTK_DIALOG(pMsgBox));
55 gtk_widget_destroy(pMsgBox);
56
57 return ret;
58 }
59
60 static int adjust_cur_dir(char *argv0)
61 {
62 int ret = 2;
63 char c;
64 char *pos = NULL;
65 char *end = NULL;
66
67 if (argv0[0] == '.')
68 {
69 return 1;
70 }
71
72 for (pos = argv0; pos && *pos; pos++)
73 {
74 if (*pos == '/')
75 {
76 end = pos;
77 }
78 }
79
80 if (end)
81 {
82 c = *end;
83 *end = 0;
84
85 pos = strstr(argv0, "/tool/");
86 if (pos)
87 {
88 *pos = 0;
89 }
90
91 ret = chdir(argv0);
92
93 *end = c;
94 if (pos)
95 {
96 *pos = '/';
97 }
98 }
99
100 return ret;
101 }
102
103 int main(int argc, char *argv[])
104 {
105 int i;
106 int len;
107 const void *pData = NULL;
108 GtkWidget *pWidget = NULL;
109 GtkBuilder *pBuilder = NULL;
110 GError *error = NULL;
111 struct stat logstat;
112
113 gtk_init(&argc, &argv);
114
115 if (geteuid() != 0)
116 {
117 early_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
118 "Ventoy2Disk permission denied!\r\nPlease run with root privileges.");
119 return EACCES;
120 }
121
122 if (access("./boot/boot.img", F_OK) == -1)
123 {
124 adjust_cur_dir(argv[0]);
125 }
126
127 if (access("./boot/boot.img", F_OK) == -1)
128 {
129 early_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Please run under the correct directory.");
130 return 1;
131 }
132
133 snprintf(g_log_file, sizeof(g_log_file), "log.txt");
134 snprintf(g_ini_file, sizeof(g_ini_file), "./Ventoy2Disk.ini");
135 for (i = 0; i < argc; i++)
136 {
137 if (argv[i] && argv[i + 1] && strcmp(argv[i], "-l") == 0)
138 {
139 snprintf(g_log_file, sizeof(g_log_file), "%s", argv[i + 1]);
140 }
141 else if (argv[i] && argv[i + 1] && strcmp(argv[i], "-i") == 0)
142 {
143 snprintf(g_ini_file, sizeof(g_ini_file), "%s", argv[i + 1]);
144 }
145 }
146
147 memset(&logstat, 0, sizeof(logstat));
148 if (0 == stat(g_log_file, &logstat))
149 {
150 if (logstat.st_size >= 4 * SIZE_1MB)
151 {
152 remove(g_log_file);
153 }
154 }
155
156 ventoy_log_init();
157
158 vlog("================================================\n");
159 vlog("===== Ventoy2Disk %s powered by GTK%d.x =====\n", ventoy_get_local_version(), GTK_MAJOR_VERSION);
160 vlog("================================================\n");
161 vlog("log file is <%s> lastsize:%lld\n", g_log_file, (long long)logstat.st_size);
162 vlog("ini file is <%s>\n", g_ini_file);
163
164 ventoy_disk_init();
165
166 ventoy_http_init();
167
168 pBuilder = gtk_builder_new();
169 if (!pBuilder)
170 {
171 vlog("failed to create builder\n");
172 return 1;
173 }
174
175 if (!gtk_builder_add_from_file(pBuilder, "./tool/VentoyGTK.glade", &error))
176 {
177 vlog("gtk_builder_add_from_file failed:%s\n", error->message);
178 g_clear_error(&error);
179 return 1;
180 }
181
182 pData = get_refresh_icon_raw_data(&len);
183 set_image_from_pixbuf(pBuilder, "image_refresh", pData, len);
184
185 pData = get_secure_icon_raw_data(&len);
186 set_image_from_pixbuf(pBuilder, "image_secure_local", pData, len);
187 set_image_from_pixbuf(pBuilder, "image_secure_dev", pData, len);
188
189 pWidget = GTK_WIDGET(gtk_builder_get_object(pBuilder, "window"));
190 gtk_widget_show_all(pWidget);
191
192 pData = get_window_icon_raw_data(&len);
193 set_window_icon_from_pixbuf(GTK_WINDOW(pWidget), pData, len);
194
195 on_init_window(pBuilder);
196 g_signal_connect(G_OBJECT(pWidget), "delete_event", G_CALLBACK(on_exit_window), NULL);
197 g_signal_connect(G_OBJECT(pWidget), "destroy", G_CALLBACK(gtk_main_quit), NULL);
198
199 gtk_main();
200
201 ventoy_disk_exit();
202 ventoy_http_exit();
203
204 g_object_unref (G_OBJECT(pBuilder));
205
206 vlog("######## Ventoy2Disk GTK %s exit ########\n", ventoy_get_local_version());
207
208 /* log exit must at the end */
209 ventoy_log_exit();
210 return 0;
211 }
212