main_gtk.c 4.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
longpanda's avatar
longpanda committed
11
12
#include <sys/types.h>
#include <linux/limits.h>
13
14
15
16
#include <ventoy_define.h>
#include <ventoy_util.h>
#include "ventoy_gtk.h"

longpanda's avatar
longpanda committed
17
18
19
char g_log_file[PATH_MAX];
char g_ini_file[PATH_MAX];

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
static int set_image_from_pixbuf(GtkBuilder *pBuilder, const char *id, const void *pData, int len)
{
    GtkImage *pImage = NULL;
    GdkPixbuf *pPixbuf = NULL;
    GInputStream *pStream = NULL;

    pImage = (GtkImage *)gtk_builder_get_object(pBuilder, id);
    pStream = g_memory_input_stream_new_from_data(pData, len, NULL);
    pPixbuf = gdk_pixbuf_new_from_stream(pStream, NULL, NULL);
    gtk_image_set_from_pixbuf(pImage, pPixbuf);

    return 0;
}

static int set_window_icon_from_pixbuf(GtkWindow *window, const void *pData, int len)
{
    GdkPixbuf *pPixbuf = NULL;
    GInputStream *pStream = NULL;

    pStream = g_memory_input_stream_new_from_data(pData, len, NULL);
    pPixbuf = gdk_pixbuf_new_from_stream(pStream, NULL, NULL);

    gtk_window_set_icon(window, pPixbuf);
    return 0;
}

int early_msgbox(GtkMessageType type, GtkButtonsType buttons, const char *str)
{
    int ret;
    GtkWidget *pMsgBox = NULL;
    
    pMsgBox= gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, type, buttons, str);
    
    ret = gtk_dialog_run(GTK_DIALOG(pMsgBox));
    gtk_widget_destroy(pMsgBox);

    return ret;
}

int main(int argc, char *argv[])
{
longpanda's avatar
longpanda committed
61
    int i;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    int len;
    const void *pData = NULL;
    GtkWidget *pWidget = NULL;
    GtkBuilder *pBuilder = NULL;
    GError *error = NULL;

    gtk_init(&argc, &argv);
    
    if (geteuid() != 0)
    {
        early_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 
                     "Ventoy2Disk permission denied!\r\nPlease run with root privileges.");
        return EACCES;
    }

    if (access("./boot/boot.img", F_OK) == -1)
    {
        early_msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Please run under the correct directory.");
        return 1;
    }

longpanda's avatar
longpanda committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    snprintf(g_log_file, sizeof(g_log_file), "log.txt");
    snprintf(g_ini_file, sizeof(g_ini_file), "./Ventoy2Disk.ini");
    for (i = 0; i < argc; i++)
    {
        if (argv[i] && argv[i + 1] && strcmp(argv[i], "-l") == 0)
        {
            snprintf(g_log_file, sizeof(g_log_file), "%s", argv[i + 1]);
        }
        else if (argv[i] && argv[i + 1] &&  strcmp(argv[i], "-i") == 0)
        {
            snprintf(g_ini_file, sizeof(g_ini_file), "%s", argv[i + 1]);
        }
    }

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    ventoy_log_init();

    vlog("================================================\n");
    vlog("===== Ventoy2Disk %s powered by GTK%d.x =====\n", ventoy_get_local_version(), GTK_MAJOR_VERSION);
    vlog("================================================\n");

    ventoy_disk_init();

    ventoy_http_init();

    pBuilder = gtk_builder_new();
    if (!pBuilder)
    {
        vlog("failed to create builder\n");
        return 1;
    }

    if (!gtk_builder_add_from_file(pBuilder, "./tool/VentoyGTK.glade", &error))
    {
        vlog("gtk_builder_add_from_file failed:%s\n", error->message);
        g_clear_error(&error);
        return 1;
    }

    pData = get_refresh_icon_raw_data(&len);
    set_image_from_pixbuf(pBuilder, "image_refresh", pData, len);
    
    pData = get_secure_icon_raw_data(&len);
    set_image_from_pixbuf(pBuilder, "image_secure_local", pData, len);
    set_image_from_pixbuf(pBuilder, "image_secure_dev", pData, len);

    pWidget = GTK_WIDGET(gtk_builder_get_object(pBuilder, "window"));
    gtk_widget_show_all(pWidget);

    pData = get_window_icon_raw_data(&len);
    set_window_icon_from_pixbuf(GTK_WINDOW(pWidget), pData, len);

    on_init_window(pBuilder);
    g_signal_connect(G_OBJECT(pWidget), "delete_event", G_CALLBACK(on_exit_window), NULL);
longpanda's avatar
longpanda committed
136
    g_signal_connect(G_OBJECT(pWidget), "destroy", G_CALLBACK(gtk_main_quit), NULL);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

    gtk_main();

    ventoy_disk_exit();
    ventoy_http_exit();
    
    g_object_unref (G_OBJECT(pBuilder));
    
    vlog("######## Ventoy2Disk GTK %s exit ########\n", ventoy_get_local_version());

    /* log exit must at the end */
    ventoy_log_exit();
    return 0;
}