main_gtk.c 5.37 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
#include <sys/types.h>
longpanda's avatar
longpanda committed
12
#include <sys/stat.h>
longpanda's avatar
longpanda committed
13
#include <linux/limits.h>
14
15
16
17
#include <ventoy_define.h>
#include <ventoy_util.h>
#include "ventoy_gtk.h"

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

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
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;
}

longpanda's avatar
longpanda committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
static int adjust_cur_dir(char *argv0)
{
    int ret = 2;
    char c;
    char *pos = NULL;
    char *end = NULL;

    if (argv0[0] == '.')
    {
        return 1;
    }

    for (pos = argv0; pos && *pos; pos++)
    {
        if (*pos == '/')
        {
            end = pos;
        }
    }

    if (end)
    {
        c = *end;
        *end = 0;

        pos = strstr(argv0, "/tool/");
        if (pos)
        {
            *pos = 0;
        }
        
        ret = chdir(argv0);
        
        *end = c;
        if (pos)
        {
            *pos = '/';
        }
    }

    return ret;
}

103
104
int main(int argc, char *argv[])
{
longpanda's avatar
longpanda committed
105
    int i;
106
107
108
109
110
    int len;
    const void *pData = NULL;
    GtkWidget *pWidget = NULL;
    GtkBuilder *pBuilder = NULL;
    GError *error = NULL;
longpanda's avatar
longpanda committed
111
    struct stat logstat;
112
113
114
115
116
117
118
119
120
121

    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;
    }

longpanda's avatar
longpanda committed
122
123
124
125
126
    if (access("./boot/boot.img", F_OK) == -1)
    {
        adjust_cur_dir(argv[0]);        
    }

127
128
129
130
131
132
    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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    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]);
        }
    }

longpanda's avatar
longpanda committed
147
148
149
150
151
152
153
154
155
    memset(&logstat, 0, sizeof(logstat));
    if (0 == stat(g_log_file, &logstat))
    {
        if (logstat.st_size >= 4 * SIZE_1MB)
        {
            remove(g_log_file);
        }
    }

156
157
158
159
160
    ventoy_log_init();

    vlog("================================================\n");
    vlog("===== Ventoy2Disk %s powered by GTK%d.x =====\n", ventoy_get_local_version(), GTK_MAJOR_VERSION);
    vlog("================================================\n");
longpanda's avatar
longpanda committed
161
162
    vlog("log file is <%s> lastsize:%lld\n", g_log_file, (long long)logstat.st_size);
    vlog("ini file is <%s>\n", g_ini_file);
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

    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
197
    g_signal_connect(G_OBJECT(pWidget), "destroy", G_CALLBACK(gtk_main_quit), NULL);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

    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;
}