1 /*
2  * Copyright (C) 2015 Canonical Ltd.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11 
12 #include "wayland-session.h"
13 #include "vt.h"
14 
15 typedef struct
16 {
17     /* VT to run on */
18     gint vt;
19     gboolean have_vt_ref;
20 } WaylandSessionPrivate;
21 
G_DEFINE_TYPE_WITH_PRIVATE(WaylandSession,wayland_session,DISPLAY_SERVER_TYPE)22 G_DEFINE_TYPE_WITH_PRIVATE (WaylandSession, wayland_session, DISPLAY_SERVER_TYPE)
23 
24 WaylandSession *
25 wayland_session_new (void)
26 {
27     return g_object_new (WAYLAND_SESSION_TYPE, NULL);
28 }
29 
30 void
wayland_session_set_vt(WaylandSession * session,gint vt)31 wayland_session_set_vt (WaylandSession *session, gint vt)
32 {
33     WaylandSessionPrivate *priv = wayland_session_get_instance_private (session);
34 
35     g_return_if_fail (session != NULL);
36 
37     if (priv->have_vt_ref)
38         vt_unref (priv->vt);
39     priv->have_vt_ref = FALSE;
40     priv->vt = vt;
41     if (vt > 0)
42     {
43         vt_ref (vt);
44         priv->have_vt_ref = TRUE;
45     }
46 }
47 
48 static gint
wayland_session_get_vt(DisplayServer * server)49 wayland_session_get_vt (DisplayServer *server)
50 {
51     WaylandSessionPrivate *priv = wayland_session_get_instance_private (WAYLAND_SESSION (server));
52     g_return_val_if_fail (server != NULL, 0);
53     return priv->vt;
54 }
55 
56 static void
wayland_session_connect_session(DisplayServer * display_server,Session * session)57 wayland_session_connect_session (DisplayServer *display_server, Session *session)
58 {
59     WaylandSession *wayland_session = WAYLAND_SESSION (display_server);
60     WaylandSessionPrivate *priv = wayland_session_get_instance_private (wayland_session);
61 
62     session_set_env (session, "XDG_SESSION_TYPE", "wayland");
63 
64     if (priv->vt >= 0)
65     {
66         g_autofree gchar *value = g_strdup_printf ("%d", priv->vt);
67         session_set_env (session, "XDG_VTNR", value);
68     }
69 }
70 
71 static void
wayland_session_disconnect_session(DisplayServer * display_server,Session * session)72 wayland_session_disconnect_session (DisplayServer *display_server, Session *session)
73 {
74     session_unset_env (session, "XDG_SESSION_TYPE");
75     session_unset_env (session, "XDG_VTNR");
76 }
77 
78 static void
wayland_session_init(WaylandSession * session)79 wayland_session_init (WaylandSession *session)
80 {
81 }
82 
83 static void
wayland_session_finalize(GObject * object)84 wayland_session_finalize (GObject *object)
85 {
86     WaylandSession *self = WAYLAND_SESSION (object);
87     WaylandSessionPrivate *priv = wayland_session_get_instance_private (self);
88 
89     if (priv->have_vt_ref)
90         vt_unref (priv->vt);
91 
92     G_OBJECT_CLASS (wayland_session_parent_class)->finalize (object);
93 }
94 
95 static void
wayland_session_class_init(WaylandSessionClass * klass)96 wayland_session_class_init (WaylandSessionClass *klass)
97 {
98     GObjectClass *object_class = G_OBJECT_CLASS (klass);
99     DisplayServerClass *display_server_class = DISPLAY_SERVER_CLASS (klass);
100 
101     display_server_class->get_vt = wayland_session_get_vt;
102     display_server_class->connect_session = wayland_session_connect_session;
103     display_server_class->disconnect_session = wayland_session_disconnect_session;
104     object_class->finalize = wayland_session_finalize;
105 }
106