~voldyman/pantheon-greeter/retain-back

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
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/***
    BEGIN LICENSE

    Copyright (C) 2011-2014 elementary Developers

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License version 3, as published
    by the Free Software Foundation.

    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranties of
    MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
    PURPOSE.  See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program.  If not, see <http://www.gnu.org/licenses/>

    END LICENSE
***/
public class PantheonGreeter : Gtk.Window {

    public static LoginGateway login_gateway { get; private set; }

    GtkClutter.Embed clutter;

    Clutter.Actor greeterbox;
    UserListActor userlist_actor;
    UserList userlist;

    TimeLabel time;
    Indicators indicators;
    Wallpaper wallpaper;

    public Settings settings { get; private set; }

    public static PantheonGreeter instance { get; private set; }

    //from this width on we use the shrinked down version
    const int NORMAL_WIDTH = 1200;
    //from this width on the clock wont fit anymore
    const int NO_CLOCK_WIDTH = 920;

    public static bool TEST_MODE { get; private set; }

    public PantheonGreeter () {
        //singleton
        assert (instance == null);
        instance = this;

        TEST_MODE = Environment.get_variable ("LIGHTDM_TO_SERVER_FD") == null;

        if (TEST_MODE) {
            message ("Using dummy LightDM because LIGHTDM_TO_SERVER_FD was not found.");
            login_gateway = new DummyGateway ();
        } else {
            login_gateway = new LightDMGateway ();
        }

        settings = new Settings ("org.pantheon.desktop.greeter");

        delete_event.connect (() => {
            message ("Window got closed. Exiting...");
            Posix.exit (Posix.EXIT_SUCCESS);
            return false;
        });

        message ("Loading default-avatar...");
        LoginOption.load_default_avatar ();

        message ("Building UI...");
        clutter = new GtkClutter.Embed ();
        greeterbox = new Clutter.Actor ();

        userlist = new UserList (LightDM.UserList.get_instance ());
        userlist_actor = new UserListActor (userlist);

        time = new TimeLabel ();
        if (!TEST_MODE) {
            indicators = new Indicators (settings);
        }
        wallpaper = new Wallpaper ();

        message ("Connecting signals...");
        get_screen ().monitors_changed.connect (monitors_changed);

        login_gateway.login_successful.connect (() => {
            fade_out_ui ();
        });

        configure_event.connect (() => {
            reposition ();
            return false;
        });

        monitors_changed ();

        userlist.current_user_changed.connect ((user) => {
            wallpaper.set_wallpaper (user.background);
            if (!TEST_MODE) {
                indicators.keyboard_menu.user_changed_cb (user);
            }
        });

        /*activate the numlock if needed*/
        var activate_numlock = settings.get_boolean ("activate-numlock");
        if (activate_numlock)
            Granite.Services.System.execute_command ("/usr/bin/numlockx on");

        /*build up UI*/
        clutter.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK);
        var stage = clutter.get_stage () as Clutter.Stage;
        stage.background_color = {0, 0, 0, 255};

        greeterbox.add_child (wallpaper);
        greeterbox.add_child (time);
        greeterbox.add_child (userlist_actor);
        if (!TEST_MODE) {
            greeterbox.add_child (indicators);
        }

        greeterbox.opacity = 0;

        stage.add_child (greeterbox);

        greeterbox.add_constraint (new Clutter.BindConstraint (stage, Clutter.BindCoordinate.WIDTH, 0));
        greeterbox.add_constraint (new Clutter.BindConstraint (stage, Clutter.BindCoordinate.HEIGHT, 0));

        if (!TEST_MODE)
            indicators.add_constraint (new Clutter.BindConstraint (greeterbox, Clutter.BindCoordinate.WIDTH, 0));

        clutter.key_press_event.connect (keyboard_navigation);

        add (clutter);
        show_all ();

        scroll_event.connect (scroll_navigation);

        greeterbox.animate (Clutter.AnimationMode.EASE_OUT_QUART, 250, opacity: 255);

        message ("Selecting last used user...");
        var last_user = settings.get_string ("last-user");
        for (var i = 0; i < userlist.size; i++) {
            if (userlist.get_user (i).name == last_user) {
                userlist.current_user = userlist.get_user (i);
                break;
            }
        }
        if (userlist.current_user == null)
            userlist.current_user = userlist.get_user (0);

        message ("Finished building UI...");
        this.get_window ().focus (Gdk.CURRENT_TIME);
    }

    /**
     * Fades out an actor and returns the used transition that we can
     * connect us to its completed-signal.
     */
    Clutter.PropertyTransition fade_out_actor (Clutter.Actor actor) {
        var transition = new Clutter.PropertyTransition ("opacity");
        transition.animatable = actor;
        transition.set_duration (300);
        transition.set_progress_mode (Clutter.AnimationMode.EASE_OUT_CIRC);
        transition.set_from_value (actor.opacity);
        transition.set_to_value (0);
        actor.add_transition ("fadeout", transition);
        return transition;
    }

    /**
     * Fades out the ui and then starts the session.
     * Only call this if the LoginGateway has signaled it is awaiting
     * start_session by firing login_successful!.
     */
    void fade_out_ui () {
        refresh_background ();

        // The animations are always the same. If they would have different
        // lengths we need to use a TransitionGroup to determine
        // the correct time everything is faded out.
        var anim = fade_out_actor (time);
        fade_out_actor (userlist_actor);
        if (!TEST_MODE)
            fade_out_actor (indicators);

        anim.completed.connect (() => {
            login_gateway.start_session ();
        });
    }

    void monitors_changed () {
        Gdk.Rectangle geometry;
        get_screen ().get_monitor_geometry (get_screen ().get_primary_monitor (), out geometry);
        resize (geometry.width, geometry.height);
        move (geometry.x, geometry.y);
        reposition ();
    }

    void reposition () {
        int width = 0;
        int height = 0;

        get_size (out width, out height);

        if (width > NORMAL_WIDTH) {
            userlist_actor.x = 243;
        } else {
            userlist_actor.x = 120 * ((float) (width) / NORMAL_WIDTH);
        }
        userlist_actor.y = Math.floorf (height / 2.0f);

        time.x = width - time.width - 100;
        time.y = height / 2 - time.height / 2;

        time.visible = width > NO_CLOCK_WIDTH;

        wallpaper.width = width;
        wallpaper.screen_width = width;
        wallpaper.height = height;
        wallpaper.screen_height = height;
        wallpaper.reposition ();
    }

    bool keyboard_navigation (Gdk.EventKey e) {
        switch (e.keyval) {
            case Gdk.Key.Num_Lock:
                settings.set_boolean ("activate-numlock", !settings.get_boolean ("activate-numlock"));
                break;
            case Gdk.Key.Up:
                userlist.select_prev_user ();
                break;
            case Gdk.Key.Down:
                userlist.select_next_user ();
                break;
            default:
                return false;
        }

        return true;
    }

    bool scroll_navigation (Gdk.EventScroll e) {
        switch (e.direction) {
        case Gdk.ScrollDirection.UP:
            userlist.select_prev_user ();
            break;
        case Gdk.ScrollDirection.DOWN:
            userlist.select_next_user ();
            break;
        }

        return false;
    }

    Cairo.XlibSurface? create_root_surface (Gdk.Screen screen) {
        var visual = screen.get_system_visual ();
        var xvisual =(visual as Gdk.X11.Visual).get_xvisual ();

        var gdk_display = (screen.get_display () as Gdk.X11.Display);
        unowned X.Display display = gdk_display.get_xdisplay ();

        var root_window = (screen.get_root_window () as Gdk.X11.Window);
        var pixmap = X.CreatePixmap (display,
                                     root_window.get_xid (),
                                     screen.get_width (),
                                     screen.get_height (),
                                     visual.get_depth ());

        /* Convert into a Cairo surface */
        var surface = new Cairo.XlibSurface (display, pixmap,
                                             xvisual,
                                             screen.get_width (),
                                             screen.get_height ());

        return surface;
    }

    void draw_wallpaper_on_surface (Cairo.Surface surface) {
        var ctx = new Cairo.Context (surface);
        var current_pixbuf = wallpaper.background_pixbuf;

        Cairo.Format format = Cairo.Format.RGB24;
        if (current_pixbuf.get_has_alpha ()) {
            format = Cairo.Format.ARGB32;
        }
        var img_surface = new Cairo.ImageSurface (format,
                                                  current_pixbuf.get_width (),
                                                  current_pixbuf.get_height ());
        var img_ctx = new Cairo.Context (img_surface);
        
        Gdk.cairo_set_source_pixbuf (img_ctx, current_pixbuf,
                                     0, 0);

        img_ctx.paint ();

        ctx.set_source_surface (img_surface, 0, 0);
    }

    void refresh_background () {
        var root_window = (get_screen ().get_root_window () as Gdk.X11.Window);
        var root_surface = create_root_surface (get_screen ());

        draw_wallpaper_on_surface (root_surface);

        Gdk.flush ();

        var x_display = (get_screen ().get_display () as Gdk.X11.Display);
        unowned X.Display display = x_display.get_xdisplay ();

        /* Ensure Cairo has actually finished it's drawing */
        root_surface.flush ();
        
        /* Use this pixmap for the background */
        X.SetWindowBackgroundPixmap (display,
                                     root_window.get_xid (),
                                     root_surface.get_drawable ());

        X.ClearWindow (display, root_window.get_xid ());

        var prop = display.intern_atom ("_XSETROOT_ID", false);
        X.ChangeProperty (display, root_window.get_xid (), prop,
                          X.XA_PIXMAP, 32, X.PropMode.Replace,
                          root_surface.get_drawable (), 1);

        display.set_close_down_mode (X.RetainPermanent);
    }
}

public static int main (string [] args) {
    message ("Starting pantheon-greeter...");
    /* Protect memory from being paged to disk, as we deal with passwords */
    PosixMLock.mlockall (PosixMLock.MCL_CURRENT | PosixMLock.MCL_FUTURE);

    var init = GtkClutter.init (ref args);
    if (init != Clutter.InitError.SUCCESS)
        error ("Clutter could not be intiailized");


    message ("Applying settings...");
    /*some settings*/
    Intl.setlocale (LocaleCategory.ALL, "");
    Intl.bind_textdomain_codeset ("pantheon-greeter", "UTF-8");
    Intl.textdomain ("pantheon-greeter");

    Gdk.get_default_root_window ().set_cursor (new Gdk.Cursor (Gdk.CursorType.LEFT_PTR));

    var settings = Gtk.Settings.get_default ();
    settings.gtk_theme_name = "elementary";
    settings.gtk_icon_theme_name = "elementary";
    settings.gtk_font_name = "Droid Sans";
    settings.gtk_xft_dpi= (int) (1024 * 96);
    settings.gtk_xft_antialias = 1;
    settings.gtk_xft_hintstyle = "hintslight";
    settings.gtk_xft_rgba = "rgb";
    settings.gtk_cursor_blink = true;

    new PantheonGreeter ();
    message ("Entering main-loop...");
    Gtk.main ();
    message ("Gtk.main exited - shutting down.");
    return Posix.EXIT_SUCCESS;
}