File: greeter.c

package info (click to toggle)
ldm 2%3A2.0.6-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,012 kB
  • ctags: 144
  • sloc: sh: 3,813; ansic: 1,648; makefile: 105; sed: 16
file content (453 lines) | stat: -rw-r--r-- 14,418 bytes parent folder | download
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * LTSP Graphical GTK Greeter
 * Copyright (2007) Oliver Grawert <ogra@ubuntu.com>, Canonical Ltd.
 * Code Licensed under GPL v2
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>

#include <greeter.h>


#include <config.h>

#include <libintl.h>
#include <locale.h>
#define _(text) gettext(text)


GtkWidget *UserPrompt;          /* prompt area before the entry */
GtkWidget *StatusMessages;      /* Status msg area below entry */
GtkWidget *entry;               /* entry box */
GtkWidget *guestbox;

GHashTable *ldminfo_hash = NULL;
GList *host_list = NULL;
GIOChannel *g_stdout;           /* stdout io channel */
char ldm_theme_dir[512];

int allowguest;

char *
ldm_theme_file(char * file)
{
    static char path[512];
    strcpy(path, ldm_theme_dir);
    strcat(path, file);
    return path;
}

static void
destroy(GtkWidget * widget, gpointer data)
{
    gtk_main_quit();
}

static void
halt(GtkWidget * widget, gpointer data)
{
    GError **error = NULL;
    g_spawn_command_line_async("/sbin/poweroff -fp", error);
}

static void
reboot(GtkWidget * widget, gpointer data)
{
    GError **error = NULL;
    g_spawn_command_line_async("/sbin/reboot -fp", error);
}

gboolean
update_time(GtkWidget * label)
{
    gchar *timestring = 0;
    time_t timet;
    struct tm *timePtr;

    timet = time(NULL);
    timePtr = localtime(&timet);

    // use 12 hour clock format if LDM_12HOURCLOCK is set to true
    if (ldm_getenv_bool("LDM_12HOURCLOCK")) {
        timestring = g_strdup_printf("%.2d:%.2d ", 
                                   (timePtr->tm_hour % 12) ? (timePtr->tm_hour % 12) : 12, 
                                   timePtr->tm_min);
    }
    else {
        timestring = g_strdup_printf("%.2d:%.2d ", 
                                   timePtr->tm_hour, timePtr->tm_min);
    }

    gtk_label_set_markup((GtkLabel *) label, timestring);

    g_free(timestring);

    return TRUE;
}

void
destroy_popup(GtkWidget * widget, GtkWidget * popup)
{
    gtk_widget_destroy(popup);
    return;
}

gboolean
handle_command(GIOChannel * io_input)
{
    GString *buf;

    buf = g_string_new("");

    g_io_channel_read_line_string(io_input, buf, NULL, NULL);

    if (!g_strncasecmp(buf->str, "msg", 3)) {
        gchar **split_buf;
        split_buf = g_strsplit(buf->str, " ", 2);
        gtk_label_set_markup((GtkLabel *) StatusMessages, split_buf[1]);
        g_strfreev(split_buf);
        gtk_entry_set_editable(GTK_ENTRY(entry), FALSE);
    } else if (!g_strncasecmp(buf->str, "quit", 4)) {
        GdkCursor *cursor;
        cursor = gdk_cursor_new(GDK_WATCH);
        gdk_window_set_cursor(gdk_get_default_root_window(), cursor);
        gtk_main_quit();
    } else if (!g_strncasecmp(buf->str, "prompt", 6)) {
        gchar **split_buf;
        split_buf = g_strsplit(buf->str, " ", 2);
        gtk_label_set_markup((GtkLabel *) UserPrompt, split_buf[1]);
        g_strfreev(split_buf);
    } else if (!g_strncasecmp(buf->str, "userid", 6)) {
        gtk_widget_show(guestbox);
        gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
        gtk_entry_set_editable(GTK_ENTRY(entry), TRUE);
    } else if (!g_strncasecmp(buf->str, "passwd", 6)) {
        gtk_widget_hide(guestbox);
        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
        gtk_entry_set_editable(GTK_ENTRY(entry), TRUE);
    } else if (!g_strncasecmp(buf->str, "hostname", 8)) {
        gchar *hoststr;
        update_selected_host();
        hoststr = g_strdup_printf("%s\n", host);
        g_io_channel_write_chars(g_stdout, hoststr, -1, NULL, NULL);
        g_io_channel_flush(g_stdout, NULL);
        g_free(hoststr);
    } else if (!g_strncasecmp(buf->str, "language", 8)) {
        gchar *langstr;
        update_selected_lang();
        langstr = g_strdup_printf("%s\n", language);
        g_io_channel_write_chars(g_stdout, langstr, -1, NULL, NULL);
        g_io_channel_flush(g_stdout, NULL);
        g_free(langstr);
    } else if (!g_strncasecmp(buf->str, "session", 7)) {
        gchar *sessstr;
        update_selected_sess();
        sessstr = g_strdup_printf("%s\n", session);
        g_io_channel_write_chars(g_stdout, sessstr, -1, NULL, NULL);
        g_io_channel_flush(g_stdout, NULL);
        g_free(sessstr);
    }

    g_string_free(buf, TRUE);
    return TRUE;
}

char *
get_sysname(void)
{
    struct utsname name;
    char *node;

    if (uname(&name) == 0) {
        node = strdup(name.nodename);
        return node;
    }
    return NULL;
}

static void
handle_guestbutton(GtkButton * entry, GdkWindow * window)
{
    g_io_channel_write_chars(g_stdout, g_strdup_printf("@GUEST@\n"), -1, NULL, NULL);
    g_io_channel_flush(g_stdout, NULL);
}

static void
handle_entry(GtkEntry * entry, GdkWindow * window)
{
    gchar *entrystr;

    if(gtk_entry_get_visibility(GTK_ENTRY(entry))) 
        g_io_channel_write_chars(g_stdout, g_strdup_printf("@NOTGUEST@\n"), -1, NULL, NULL);
    entrystr = g_strdup_printf("%s\n", gtk_entry_get_text(entry));
    g_io_channel_write_chars(g_stdout, entrystr, -1, NULL, NULL);
    g_io_channel_flush(g_stdout, NULL);
    g_free(entrystr);
    gtk_entry_set_text(entry, "");
}

static void
popup_menu(GtkWidget * widget, GtkWindow * window)
{
    GtkWidget *menu, *lang_item, *sess_item, *host_item, *quit_item,
        *reboot_item;
    GtkWidget *sep, *langico, *sessico, *hostico, *rebootico, *haltico;

    menu = gtk_menu_new();

    lang_item =
        gtk_image_menu_item_new_with_mnemonic(_("Select _Language ..."));
    langico = gtk_image_new_from_file(ldm_theme_file("/language.png"));
    gtk_image_menu_item_set_image((GtkImageMenuItem *) lang_item, langico);

    sess_item =
        gtk_image_menu_item_new_with_mnemonic(_("Select _Session ..."));
    sessico = gtk_image_new_from_file(ldm_theme_file("/session.png"));
    gtk_image_menu_item_set_image((GtkImageMenuItem *) sess_item, sessico);

    host_item =
        gtk_image_menu_item_new_with_mnemonic(_("Select _Host ..."));
    hostico = gtk_image_new_from_file(ldm_theme_file("/host.png"));
    gtk_image_menu_item_set_image((GtkImageMenuItem *) host_item, hostico);

    sep = gtk_separator_menu_item_new();
    reboot_item = gtk_image_menu_item_new_with_mnemonic(_("_Reboot"));
    rebootico = gtk_image_new_from_file(ldm_theme_file("/reboot.png"));
    gtk_image_menu_item_set_image((GtkImageMenuItem *) reboot_item,
                                  rebootico);
    quit_item = gtk_image_menu_item_new_with_mnemonic(_("Shut_down"));
    haltico = gtk_image_new_from_file(ldm_theme_file("/shutdown.png"));
    gtk_image_menu_item_set_image((GtkImageMenuItem *) quit_item, haltico);

    g_signal_connect_swapped(G_OBJECT(quit_item), "activate",
                             G_CALLBACK(halt), NULL);
    g_signal_connect_swapped(G_OBJECT(reboot_item), "activate",
                             G_CALLBACK(reboot), NULL);
    g_signal_connect_swapped(G_OBJECT(lang_item), "activate",
                             G_CALLBACK(langwin), window);
    g_signal_connect_swapped(G_OBJECT(sess_item), "activate",
                             G_CALLBACK(sesswin), window);
    g_signal_connect_swapped(G_OBJECT(host_item), "activate",
                             G_CALLBACK(hostwin), window);


    gtk_menu_shell_append(GTK_MENU_SHELL(menu), lang_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), sess_item);
    if (g_hash_table_size(ldminfo_hash) > 1)
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), host_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), sep);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), reboot_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), quit_item);

    gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
                   0, gtk_get_current_event_time());

    gtk_widget_show_all(menu);
    gtk_menu_reposition(GTK_MENU(menu));

    return;
}

int
main(int argc, char *argv[])
{
    int pw, ph, spheight, w, h;
    const char *hoststring = 0;

    gint lw, lh;

    GdkCursor *normcursor, *busycursor;
    GtkWidget *window, *syslabel, *logo, *EntryBox, *timelabel;
    GtkWidget *GuestButton, *StatusBarBox, *spacer, *guestspacer1, *guestspacer2, *vbox, *vbox2, *hbox;
    GtkButton *optionbutton, *cancelbutton;
    GdkWindow *root;
    GdkPixbuf *rawpic, *pix;
    GdkPixmap *pic;
    GdkBitmap *mask;
    gint width, height;
    GIOChannel *g_stdin;

    char * ldm_theme;

#ifdef ENABLE_NLS
     setlocale (LC_ALL, "");
     bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
     bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
     textdomain (GETTEXT_PACKAGE);
#endif

    gtk_init(&argc, &argv);

    *ldm_theme_dir='\0';
    ldm_theme = getenv("LDM_THEME"); 
    
    if (ldm_theme)
    {
        char temp[512];
        strcpy(temp, ldm_theme);
        if(*temp != '/')
            strcpy(ldm_theme_dir, LDM_THEME_DIR);
        strcat(ldm_theme_dir, temp);
    }
    else
    {
        strcpy(ldm_theme_dir, LDM_THEME_DIR);
        strcat(ldm_theme_dir, "default");
    }

    allowguest=ldm_getenv_bool("LDM_GUESTLOGIN");
    gtk_rc_add_default_file(ldm_theme_file("/greeter-gtkrc"));

    /* Initialize information about hosts */
    ldminfo_init(&ldminfo_hash, &host_list, getenv("LDM_SERVER"));

    normcursor = gdk_cursor_new(GDK_LEFT_PTR);
    busycursor = gdk_cursor_new(GDK_WATCH);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy),
                     NULL);

    root = gdk_get_default_root_window();

    gdk_window_set_cursor(root, busycursor);
    gdk_drawable_get_size(root, &width, &height);

    pw = width;
    ph = height;
    w = width;
    h = height;

    logo = gtk_image_new_from_file(ldm_theme_file("/logo.png"));

    pix = gtk_image_get_pixbuf((GtkImage *) logo);
    lw = gdk_pixbuf_get_width(pix);
    lh = gdk_pixbuf_get_height(pix);

    spheight = (height / 4) - (lh / 2);

    rawpic = gdk_pixbuf_new_from_file_at_scale(ldm_theme_file("/bg.png"),
                                               pw, ph, FALSE, NULL);
    gdk_pixbuf_render_pixmap_and_mask(rawpic, &pic, &mask, 0);
    gdk_pixbuf_unref(rawpic);

    gtk_widget_set_app_paintable(window, TRUE);
    gtk_widget_set_size_request(window, w, h);
    gtk_widget_realize(window);
    gdk_window_set_back_pixmap((GdkWindow *) window->window, pic, 0);
    gtk_window_set_decorated((GtkWindow *) window, FALSE);

    vbox = gtk_vbox_new(FALSE, 5);
    vbox2 = gtk_vbox_new(FALSE, 0);
    EntryBox = gtk_hbox_new(FALSE, 5);
    hbox = gtk_hbox_new(FALSE, 0);

    StatusBarBox = gtk_hbox_new(FALSE, 0);

    optionbutton =
        (GtkButton *) gtk_button_new_from_stock("gtk-preferences");
    gtk_button_set_relief(optionbutton, GTK_RELIEF_NONE);
    gtk_button_set_focus_on_click((GtkButton *) optionbutton, FALSE);

    g_signal_connect(G_OBJECT(optionbutton), "clicked",
                     G_CALLBACK(popup_menu), window);

    /*
     * Cancel button 
     */

    cancelbutton =
        (GtkButton *) gtk_button_new_from_stock(GTK_STOCK_CANCEL);
    gtk_button_set_relief(cancelbutton, GTK_RELIEF_NONE);
    gtk_button_set_focus_on_click((GtkButton *) cancelbutton, FALSE);

    g_signal_connect(G_OBJECT(cancelbutton), "clicked",
                     G_CALLBACK(destroy), window);

    syslabel = gtk_label_new("");
    timelabel = gtk_label_new("");
    hoststring =
        g_strdup_printf("<b>%s (%s) //</b>", get_sysname(), getenv("LDMINFO_IPADDR"));
    gtk_label_set_markup((GtkLabel *) syslabel, hoststring);
    update_time(timelabel);

    g_timeout_add(30000, (GSourceFunc) update_time, timelabel);

    gtk_box_pack_start(GTK_BOX(StatusBarBox),
                       GTK_WIDGET(optionbutton), FALSE, FALSE, 5);
    /*gtk_box_pack_start(GTK_BOX(StatusBarBox),
                       GTK_WIDGET(cancelbutton), FALSE, FALSE, 5);*/
    gtk_box_pack_end(GTK_BOX(StatusBarBox),
                     GTK_WIDGET(timelabel), FALSE, FALSE, 5);
    gtk_box_pack_end(GTK_BOX(StatusBarBox),
                     GTK_WIDGET(syslabel), FALSE, FALSE, 0);

    UserPrompt = gtk_label_new("");
    spacer = gtk_label_new("");

    if (lw < 180)
        lw = 180;

    gtk_misc_set_alignment((GtkMisc *) UserPrompt, 1, 0.5);
    gtk_widget_set_size_request(UserPrompt, (lw / 2), 0);

    StatusMessages = gtk_label_new("");
    entry = gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(entry), 12);
    g_signal_connect(G_OBJECT(entry), "activate",
                     G_CALLBACK(handle_entry), root);

    guestspacer1 = gtk_label_new("");
    guestspacer2 = gtk_label_new("");
    GuestButton = gtk_button_new_with_label (_("Login as Guest")); 
    g_signal_connect(G_OBJECT(GuestButton), "clicked", 
        G_CALLBACK(handle_guestbutton), root); 
    gtk_button_set_relief((GtkButton *)GuestButton, GTK_RELIEF_NONE);
    gtk_button_set_focus_on_click((GtkButton *)GuestButton, FALSE);
    guestbox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(guestbox), guestspacer1, TRUE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(guestbox), GuestButton, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(guestbox), guestspacer2, TRUE, FALSE, 0);

    gtk_box_pack_start(GTK_BOX(EntryBox), UserPrompt, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(EntryBox), entry, FALSE, FALSE, 0);

    gtk_box_pack_start(GTK_BOX(vbox), spacer, FALSE, FALSE, spheight);
    gtk_box_pack_start(GTK_BOX(vbox), logo, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(vbox), EntryBox, TRUE, FALSE, 0);
    if(allowguest) gtk_box_pack_start(GTK_BOX(vbox), guestbox, FALSE, FALSE, 0); 
    gtk_box_pack_start(GTK_BOX(vbox), StatusMessages, TRUE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox2), hbox, FALSE, FALSE, 0);
    gtk_box_pack_end(GTK_BOX(vbox2), StatusBarBox, FALSE, FALSE, 5);

    gtk_container_add(GTK_CONTAINER(window), vbox2);

    gtk_widget_show_all(window);

    gdk_window_set_cursor(root, normcursor);

    /*
     * Start listening to stdin
     */

    g_stdin = g_io_channel_unix_new(STDIN_FILENO);      /* listen to stdin */
    g_stdout = g_io_channel_unix_new(STDOUT_FILENO);
    g_io_add_watch(g_stdin, G_IO_IN, (GIOFunc) handle_command, g_stdin);

    gtk_main();

    return 0;
}