]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/main_gtk.c
Update languages.json (#1088)
[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 <linux/limits.h>
13 #include <ventoy_define.h>
14 #include <ventoy_util.h>
15 #include "ventoy_gtk.h"
16
17 char g_log_file[PATH_MAX];
18 char g_ini_file[PATH_MAX];
19
20 static int set_image_from_pixbuf(GtkBuilder *pBuilder, const char *id, const void *pData, int len)
21 {
22 GtkImage *pImage = NULL;
23 GdkPixbuf *pPixbuf = NULL;
24 GInputStream *pStream = NULL;
25
26 pImage = (GtkImage *)gtk_builder_get_object(pBuilder, id);
27 pStream = g_memory_input_stream_new_from_data(pData, len, NULL);
28 pPixbuf = gdk_pixbuf_new_from_stream(pStream, NULL, NULL);
29 gtk_image_set_from_pixbuf(pImage, pPixbuf);
30
31 return 0;
32 }
33
34 static int set_window_icon_from_pixbuf(GtkWindow *window, const void *pData, int len)
35 {
36 GdkPixbuf *pPixbuf = NULL;
37 GInputStream *pStream = NULL;
38
39 pStream = g_memory_input_stream_new_from_data(pData, len, NULL);
40 pPixbuf = gdk_pixbuf_new_from_stream(pStream, NULL, NULL);
41
42 gtk_window_set_icon(window, pPixbuf);
43 return 0;
44 }
45
46 int early_msgbox(GtkMessageType type, GtkButtonsType buttons, const char *str)
47 {
48 int ret;
49 GtkWidget *pMsgBox = NULL;
50
51 pMsgBox= gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, type, buttons, str);
52
53 ret = gtk_dialog_run(GTK_DIALOG(pMsgBox));
54 gtk_widget_destroy(pMsgBox);
55
56 return ret;
57 }
58
59 int main(int argc, char *argv[])
60 {
61 int i;
62 int len;
63 const void *pData = NULL;
64 GtkWidget *pWidget = NULL;
65 GtkBuilder *pBuilder = NULL;
66 GError *error = NULL;
67
68 gtk_init(&argc, &argv);
69
70 if (geteuid() != 0)
71 {
72 early_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
73 "Ventoy2Disk permission denied!\r\nPlease run with root privileges.");
74 return EACCES;
75 }
76
77 if (access("./boot/boot.img", F_OK) == -1)
78 {
79 early_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Please run under the correct directory.");
80 return 1;
81 }
82
83 snprintf(g_log_file, sizeof(g_log_file), "log.txt");
84 snprintf(g_ini_file, sizeof(g_ini_file), "./Ventoy2Disk.ini");
85 for (i = 0; i < argc; i++)
86 {
87 if (argv[i] && argv[i + 1] && strcmp(argv[i], "-l") == 0)
88 {
89 snprintf(g_log_file, sizeof(g_log_file), "%s", argv[i + 1]);
90 }
91 else if (argv[i] && argv[i + 1] && strcmp(argv[i], "-i") == 0)
92 {
93 snprintf(g_ini_file, sizeof(g_ini_file), "%s", argv[i + 1]);
94 }
95 }
96
97 ventoy_log_init();
98
99 vlog("================================================\n");
100 vlog("===== Ventoy2Disk %s powered by GTK%d.x =====\n", ventoy_get_local_version(), GTK_MAJOR_VERSION);
101 vlog("================================================\n");
102
103 ventoy_disk_init();
104
105 ventoy_http_init();
106
107 pBuilder = gtk_builder_new();
108 if (!pBuilder)
109 {
110 vlog("failed to create builder\n");
111 return 1;
112 }
113
114 if (!gtk_builder_add_from_file(pBuilder, "./tool/VentoyGTK.glade", &error))
115 {
116 vlog("gtk_builder_add_from_file failed:%s\n", error->message);
117 g_clear_error(&error);
118 return 1;
119 }
120
121 pData = get_refresh_icon_raw_data(&len);
122 set_image_from_pixbuf(pBuilder, "image_refresh", pData, len);
123
124 pData = get_secure_icon_raw_data(&len);
125 set_image_from_pixbuf(pBuilder, "image_secure_local", pData, len);
126 set_image_from_pixbuf(pBuilder, "image_secure_dev", pData, len);
127
128 pWidget = GTK_WIDGET(gtk_builder_get_object(pBuilder, "window"));
129 gtk_widget_show_all(pWidget);
130
131 pData = get_window_icon_raw_data(&len);
132 set_window_icon_from_pixbuf(GTK_WINDOW(pWidget), pData, len);
133
134 on_init_window(pBuilder);
135 g_signal_connect(G_OBJECT(pWidget), "delete_event", G_CALLBACK(on_exit_window), NULL);
136 g_signal_connect(G_OBJECT(pWidget), "destroy", G_CALLBACK(gtk_main_quit), NULL);
137
138 gtk_main();
139
140 ventoy_disk_exit();
141 ventoy_http_exit();
142
143 g_object_unref (G_OBJECT(pBuilder));
144
145 vlog("######## Ventoy2Disk GTK %s exit ########\n", ventoy_get_local_version());
146
147 /* log exit must at the end */
148 ventoy_log_exit();
149 return 0;
150 }
151