]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/main_gtk.c
11 #include <sys/types.h>
13 #include <linux/limits.h>
14 #include <ventoy_define.h>
15 #include <ventoy_util.h>
16 #include "ventoy_gtk.h"
18 char g_log_file
[PATH_MAX
];
19 char g_ini_file
[PATH_MAX
];
21 static int set_image_from_pixbuf(GtkBuilder
*pBuilder
, const char *id
, const void *pData
, int len
)
23 GtkImage
*pImage
= NULL
;
24 GdkPixbuf
*pPixbuf
= NULL
;
25 GInputStream
*pStream
= NULL
;
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
);
35 static int set_window_icon_from_pixbuf(GtkWindow
*window
, const void *pData
, int len
)
37 GdkPixbuf
*pPixbuf
= NULL
;
38 GInputStream
*pStream
= NULL
;
40 pStream
= g_memory_input_stream_new_from_data(pData
, len
, NULL
);
41 pPixbuf
= gdk_pixbuf_new_from_stream(pStream
, NULL
, NULL
);
43 gtk_window_set_icon(window
, pPixbuf
);
47 int early_msgbox(GtkMessageType type
, GtkButtonsType buttons
, const char *str
)
50 GtkWidget
*pMsgBox
= NULL
;
52 pMsgBox
= gtk_message_dialog_new(NULL
, GTK_DIALOG_MODAL
, type
, buttons
, str
);
54 ret
= gtk_dialog_run(GTK_DIALOG(pMsgBox
));
55 gtk_widget_destroy(pMsgBox
);
60 static int adjust_cur_dir(char *argv0
)
72 for (pos
= argv0
; pos
&& *pos
; pos
++)
85 pos
= strstr(argv0
, "/tool/");
103 int main(int argc
, char *argv
[])
107 const void *pData
= NULL
;
108 GtkWidget
*pWidget
= NULL
;
109 GtkBuilder
*pBuilder
= NULL
;
110 GError
*error
= NULL
;
113 gtk_init(&argc
, &argv
);
117 early_msgbox(GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
,
118 "Ventoy2Disk permission denied!\r\nPlease run with root privileges.");
122 if (access("./boot/boot.img", F_OK
) == -1)
124 adjust_cur_dir(argv
[0]);
127 if (access("./boot/boot.img", F_OK
) == -1)
129 early_msgbox(GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
, "Please run under the correct directory.");
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
++)
137 if (argv
[i
] && argv
[i
+ 1] && strcmp(argv
[i
], "-l") == 0)
139 snprintf(g_log_file
, sizeof(g_log_file
), "%s", argv
[i
+ 1]);
141 else if (argv
[i
] && argv
[i
+ 1] && strcmp(argv
[i
], "-i") == 0)
143 snprintf(g_ini_file
, sizeof(g_ini_file
), "%s", argv
[i
+ 1]);
147 memset(&logstat
, 0, sizeof(logstat
));
148 if (0 == stat(g_log_file
, &logstat
))
150 if (logstat
.st_size
>= 4 * SIZE_1MB
)
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
);
168 pBuilder
= gtk_builder_new();
171 vlog("failed to create builder\n");
175 if (!gtk_builder_add_from_file(pBuilder
, "./tool/VentoyGTK.glade", &error
))
177 vlog("gtk_builder_add_from_file failed:%s\n", error
->message
);
178 g_clear_error(&error
);
182 pData
= get_refresh_icon_raw_data(&len
);
183 set_image_from_pixbuf(pBuilder
, "image_refresh", pData
, len
);
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
);
189 pWidget
= GTK_WIDGET(gtk_builder_get_object(pBuilder
, "window"));
190 gtk_widget_show_all(pWidget
);
192 pData
= get_window_icon_raw_data(&len
);
193 set_window_icon_from_pixbuf(GTK_WINDOW(pWidget
), pData
, len
);
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
);
204 g_object_unref (G_OBJECT(pBuilder
));
206 vlog("######## Ventoy2Disk GTK %s exit ########\n", ventoy_get_local_version());
208 /* log exit must at the end */