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