File: gtk-plugin-terminal.c

package info (click to toggle)
cdebconf-terminal 0.13
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 576 kB
  • ctags: 58
  • sloc: sh: 2,770; ansic: 540; makefile: 63
file content (409 lines) | stat: -rw-r--r-- 13,597 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * cdebconf gtk plugin to display a terminal
 *
 * Copyright © 2008 Jérémy Bobbio <lunar@debian.org>
 * See debian/copyright for license.
 *
 */

#include <cdebconf/frontend.h>
#include <cdebconf/cdebconf_gtk.h>

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <vte/vte.h>
#include <string.h>

extern char ** environ;

#define DEFAULT_COMMAND_LINE "/bin/sh"

/* Here's the plugin! */
int cdebconf_gtk_handler_terminal(struct frontend * fe,
                                  struct question * question,
                                  GtkWidget * question_box);

struct terminal {
    struct frontend * fe;
    GtkWidget * goback_button;
    VteTerminal * terminal;
    char * command;
    char ** argv;
    char ** environ;
};

static void destroy_terminal(struct terminal * terminal_data)
{
    if (NULL != terminal_data->command) {
        g_free(terminal_data->command);
    }
    if (NULL != terminal_data->argv) {
        g_strfreev(terminal_data->argv);
    }
    if (NULL != terminal_data->environ) {
        g_strfreev(terminal_data->environ);
    }
    if (NULL != terminal_data->terminal) {
        g_object_unref(G_OBJECT(terminal_data->terminal));
    }
    if (NULL != terminal_data->goback_button) {
        g_object_unref(G_OBJECT(terminal_data->goback_button));
    }
    g_free(terminal_data);
}

static void cleanup(GtkWidget * widget, struct terminal * terminal_data)
{
    destroy_terminal(terminal_data);
}

static void handle_child_exit(GtkWidget * button,
                              struct terminal * terminal_data)
{
    cdebconf_gtk_set_answer_ok(terminal_data->fe);
}

static struct terminal * init_terminal(struct frontend * fe,
                                       struct question * question)
{
    struct terminal * terminal_data;

    if (NULL == (terminal_data = g_malloc0(sizeof (struct terminal)))) {
        g_critical("g_malloc0 failed.");
        return NULL;
    }
    terminal_data->fe = fe;

    return terminal_data;
}

static gboolean init_command(struct terminal * terminal_data,
                             struct question * question)
{
    const char * command_line;

    command_line = question_get_variable(question, "COMMAND_LINE");
    if (NULL == command_line) {
        command_line = DEFAULT_COMMAND_LINE;
    }
    terminal_data->argv = g_strsplit_set(
        command_line, " \t\n" /* default IFS */,
        4096 /* max number of arguments */
        /* XXX: replace with the correct system macro */);
    if (NULL == terminal_data->argv || NULL == terminal_data->argv[0]) {
        g_critical("g_strsplit_set failed.");
        return FALSE;
    }
    terminal_data->command = g_strdup(terminal_data->argv[0]);
    if (NULL == terminal_data->command) {
        g_critical("g_strplit_set failed.");
        return FALSE;
    }
    return TRUE;
}

/* XXX: mostly copied from ui.c */
static GtkWidget * create_dialog_action_box(struct frontend * fe,
                                            GtkWidget * dialog)
{
    GtkWidget * action_box;
    GtkWidget * cancel_button;
    GtkWidget * continue_button;
    char * label;

    /* check NULL! */
    action_box = gtk_hbutton_box_new();
    gtk_button_box_set_layout(GTK_BUTTON_BOX(action_box), GTK_BUTTONBOX_END);

    label = cdebconf_gtk_get_text(fe, "debconf/button-cancel", "Cancel");
    /* check NULL! */
    cancel_button = gtk_button_new_with_label(label);
    g_free(label);

    GTK_WIDGET_SET_FLAGS(cancel_button, GTK_CAN_DEFAULT);
    g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
                             G_CALLBACK(gtk_widget_destroy), dialog);
    g_signal_connect(G_OBJECT(cancel_button), "realize",
                     G_CALLBACK(gtk_widget_grab_focus), NULL /* no data */);

    label = cdebconf_gtk_get_text(fe, "debconf/button-continue", "Continue");
    continue_button = gtk_button_new_with_label(label);
    g_free(label);

    g_signal_connect_swapped(G_OBJECT(continue_button), "clicked",
                             G_CALLBACK(cdebconf_gtk_set_answer_ok), fe);
    g_signal_connect_swapped(G_OBJECT(continue_button), "clicked",
                             G_CALLBACK(gtk_widget_destroy), dialog);

    gtk_box_pack_start(GTK_BOX(action_box), cancel_button,
                       TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);
    gtk_box_pack_start(GTK_BOX(action_box), continue_button,
                       TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);

    gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(action_box),
                                       cancel_button, TRUE /* secondary */);
    return action_box;
}

/* XXX: copied from ui.c */
static GtkWidget * create_dialog_title_label(const gchar * title)
{
    GtkWidget * label;
    gchar * markup;

    /* check NULL! */
    label = gtk_label_new(NULL /* no text */);
    gtk_misc_set_alignment(GTK_MISC(label), 0 /* left aligned */,
                           0 /* top aligned */);

    markup = g_strdup_printf("<b>%s</b>", title);
    gtk_label_set_markup(GTK_LABEL(label), markup);
    g_free(markup);

    return label;
}

/* XXX: mostly copied from cdebconf_gtk_run_message_dialog() */
static gboolean run_confirm_dialog(struct terminal * terminal_data)
{
    GtkWidget * parent;
    GtkWidget * dialog;
    GtkWidget * vbox;
    GtkWidget * label;
    GtkWidget * frame;
    gchar * text;

    parent = gtk_widget_get_ancestor(terminal_data->goback_button,
                                     GTK_TYPE_WINDOW);

    dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
    gtk_window_set_modal(GTK_WINDOW(dialog), TRUE /* modal */);
    gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE /* not resizale */);
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
    gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE /* no decoration */);
    gtk_container_set_border_width(GTK_CONTAINER(dialog), 0 /* no border */);

    vbox = gtk_vbox_new(FALSE /* don't make children equal */,
                        DEFAULT_PADDING);
    text = cdebconf_gtk_get_text(
        terminal_data->fe, "debconf/terminal/gtk/confirm-title",
        "Resume installation");
    gtk_box_pack_start(GTK_BOX(vbox), create_dialog_title_label(text),
                       FALSE /* don't expand */, FALSE /* don't fille */,
                       0 /* no padding */);
    g_free(text);
    text = cdebconf_gtk_get_text(
        terminal_data->fe, "debconf/terminal/gtk/confirm-message",
        "Choose \"Continue\" to really exit the shell and resume the "
        "installation; any processes still running in the shell will be "
        "aborted.");
    label = gtk_label_new(text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    g_free(text);
    gtk_box_pack_start(GTK_BOX(vbox), label,
                       FALSE /* don't expand */, FALSE /* don't fill */,
                       DEFAULT_PADDING);
    gtk_box_pack_start(GTK_BOX(vbox), gtk_hseparator_new(),
                       FALSE /* don't expand */, FALSE /* don't fill */,
                       0 /* no padding */);
    gtk_box_pack_start(GTK_BOX(vbox),
                       create_dialog_action_box(terminal_data->fe, dialog),
                       FALSE /* don't expand */, FALSE /* don't fill */,
                       0 /* no padding */);
    cdebconf_gtk_center_widget(&vbox, DEFAULT_PADDING, DEFAULT_PADDING);

    frame = gtk_frame_new(NULL /* no label */);
    gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
    gtk_container_add(GTK_CONTAINER(frame), vbox);
    gtk_container_add(GTK_CONTAINER(dialog), frame);

    gtk_widget_show_all(dialog);
    return TRUE;
}

static GtkWidget * create_goback_button(struct terminal * terminal_data)
{
    GtkWidget * button;
    char * label;

    label = cdebconf_gtk_get_text(terminal_data->fe, "debconf/button-goback", "Go Back");
    /* XXX: check NULL! */
    button = gtk_button_new_with_label(label);
    g_free(label);

    g_signal_connect_swapped(G_OBJECT(button), "clicked",
                             G_CALLBACK(run_confirm_dialog), terminal_data);

    cdebconf_gtk_add_button(terminal_data->fe, button);

    return button;
}

static gboolean handle_goback_key(GtkWidget * widget, GdkEventKey * key,
                                  struct terminal * terminal_data)
{
    if (GDK_Escape == key->keyval &&
        (GDK_SHIFT_MASK | GDK_CONTROL_MASK) ==
        (key->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
        run_confirm_dialog(terminal_data);
        return TRUE;
    }
    return FALSE;
}

static GtkWidget * create_widgets(struct terminal * terminal_data)
{
    GtkWidget * hbox;
    VteTerminal * terminal;
    GtkWidget * scrollbar;
    GtkWidget * goback_button;

    goback_button = create_goback_button(terminal_data);
    if (NULL == goback_button) {
        g_critical("create_goback_button failed.");
        return NULL;
    }
    g_object_ref(G_OBJECT(goback_button));
    terminal_data->goback_button = goback_button;

    g_setenv("VTE_BACKEND", "pango", TRUE /* overwrite */);
    if (NULL == (terminal = VTE_TERMINAL(vte_terminal_new()))) {
        g_critical("vte_terminal_new failed.");
        return NULL;
    }
    vte_terminal_set_font_from_string(terminal, "monospace");
    g_signal_connect(terminal, "destroy", G_CALLBACK(cleanup), terminal_data);
    g_signal_connect(terminal, "child-exited", G_CALLBACK(handle_child_exit),
                     terminal_data);
    g_signal_connect(terminal, "key_press_event", G_CALLBACK(handle_goback_key),
                     terminal_data);
    g_signal_connect(terminal, "realize", G_CALLBACK(gtk_widget_grab_focus),
                     NULL);
    g_object_ref(terminal);
    terminal_data->terminal = terminal;

    hbox = gtk_hbox_new(FALSE /* not homogeneous */, 0 /* no spacing */);
    if (NULL == hbox) {
        g_critical("gtk_hbox_new failed.");
        return NULL;
    }
    gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(terminal), TRUE /* expand */,
                       TRUE /* fill */, 0 /* no padding */);

    scrollbar = gtk_vscrollbar_new(terminal->adjustment);
    if (NULL == scrollbar) {
        g_critical("gtk_vscrollbar_new failed.");
        return NULL;
    }
    gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE /* don't expand */,
                       FALSE /* don't fill */, 0 /* no padding */);
    return hbox;
}

static void set_nothing(struct question * question, void * dummy)
{
    /* terminal questions do not put anything in the database */
    return;
}

static gboolean prepare_environ(struct terminal * terminal_data)
{
    guint orig_index;
    guint new_index;
    const char * src;

    terminal_data->environ = g_new0(char *, g_strv_length(environ) + 1);
    if (NULL == terminal_data->environ) {
        g_critical("g_malloc0 failed.");
        return FALSE;
    }
    new_index = 0;
    for (orig_index = 0; NULL != environ[orig_index]; orig_index++) {
        if (g_str_has_prefix(environ[orig_index], "DEBIAN_HAS_FRONTEND=")) {
            src = "DEBIAN_HAS_FRONTEND=";
        } else if (g_str_has_prefix(environ[orig_index], "DEBIAN_FRONTEND=")) {
            src = "DEBIAN_FRONTEND=newt";
        } else {
            src = environ[orig_index];
        }
        if (NULL == (terminal_data->environ[new_index] = g_strdup(src))) {
            g_critical("g_strdup failed.");
            return FALSE;
        }
        new_index++;
    }
    return TRUE;
}

static gboolean start_command(struct terminal * terminal_data)
{
    gboolean ret = TRUE;
    pid_t pid;

#if VTE_CHECK_VERSION(0,25,0)
    ret = vte_terminal_fork_command_full(
        terminal_data->terminal,
        VTE_PTY_NO_LASTLOG | VTE_PTY_NO_UTMP | VTE_PTY_NO_WTMP,
        "/", terminal_data->argv, terminal_data->environ,
        G_SPAWN_CHILD_INHERITS_STDIN | G_SPAWN_SEARCH_PATH,
        NULL, NULL, &pid, NULL);
#else
    pid = vte_terminal_fork_command(
        terminal_data->terminal, terminal_data->command,
        terminal_data->argv, terminal_data->environ, "/",
        FALSE /* no lastlog */, FALSE /* no utmp */, FALSE /* no wtmp */);
    if (0 == pid)
        ret = FALSE;
#endif
    if (!ret)
        g_critical("vte_terminal_fork_command failed.");
    return ret;
}

int cdebconf_gtk_handler_terminal(struct frontend * fe,
                                  struct question * question,
                                  GtkWidget * question_box)
{
    struct terminal * terminal_data;
    GtkWidget * widget;

    if (!IS_QUESTION_SINGLE(question)) {
        g_critical("entropy plugin does not work alongside other questions.");
        return DC_NOTOK;
    }
    if (NULL == (terminal_data = init_terminal(fe, question))) {
        g_critical("init_terminal failed.");
        return DC_NOTOK;
    }
    if (NULL == (widget = create_widgets(terminal_data))) {
        g_critical("create_widgets failed.");
        goto failed;
    }
    if (!init_command(terminal_data, question)) {
        g_critical("init_command failed.");
        goto failed;
    }
    if (!prepare_environ(terminal_data)) {
        g_critical("prepare_environ failed.");
        goto failed;
    }
    if (!start_command(terminal_data)) {
        g_critical("start_command failed.");
        goto failed;
    }

    cdebconf_gtk_add_common_layout(fe, question, question_box, widget);

    cdebconf_gtk_register_setter(fe, SETTER_FUNCTION(set_nothing), question,
                                 NULL);

    return DC_OK;

failed:
    destroy_terminal(terminal_data);
    return DC_NOTOK;
}

/* vim: et sw=4 si
 */