Back to index

lightdm  1.3.2
test-gobject-greeter.c
Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*- */
00002 
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <xcb/xcb.h>
00007 #include <lightdm.h>
00008 
00009 #include "status.h"
00010 
00011 static LightDMGreeter *greeter;
00012 static xcb_connection_t *connection = NULL;
00013 static GKeyFile *config;
00014 
00015 static void
00016 show_message_cb (LightDMGreeter *greeter, const gchar *text, LightDMMessageType type)
00017 {
00018     status_notify ("GREETER %s SHOW-MESSAGE TEXT=\"%s\"", getenv ("DISPLAY"), text);
00019 }
00020 
00021 static void
00022 show_prompt_cb (LightDMGreeter *greeter, const gchar *text, LightDMPromptType type)
00023 {
00024     status_notify ("GREETER %s SHOW-PROMPT TEXT=\"%s\"", getenv ("DISPLAY"), text);
00025 }
00026 
00027 static void
00028 authentication_complete_cb (LightDMGreeter *greeter)
00029 {
00030     if (lightdm_greeter_get_authentication_user (greeter))
00031         status_notify ("GREETER %s AUTHENTICATION-COMPLETE USERNAME=%s AUTHENTICATED=%s",
00032                        getenv ("DISPLAY"),
00033                        lightdm_greeter_get_authentication_user (greeter),
00034                        lightdm_greeter_get_is_authenticated (greeter) ? "TRUE" : "FALSE");
00035     else
00036         status_notify ("GREETER %s AUTHENTICATION-COMPLETE AUTHENTICATED=%s",
00037                        getenv ("DISPLAY"),
00038                        lightdm_greeter_get_is_authenticated (greeter) ? "TRUE" : "FALSE");
00039 }
00040 
00041 static void
00042 signal_cb (int signum)
00043 {
00044     status_notify ("GREETER %s TERMINATE SIGNAL=%d", getenv ("DISPLAY"), signum);
00045     exit (EXIT_SUCCESS);
00046 }
00047 
00048 static void
00049 request_cb (const gchar *request)
00050 {
00051     gchar *r;
00052   
00053     r = g_strdup_printf ("GREETER %s AUTHENTICATE", getenv ("DISPLAY"));
00054     if (strcmp (request, r) == 0)
00055         lightdm_greeter_authenticate (greeter, NULL);
00056     g_free (r);
00057 
00058     r = g_strdup_printf ("GREETER %s AUTHENTICATE USERNAME=", getenv ("DISPLAY"));
00059     if (g_str_has_prefix (request, r))
00060         lightdm_greeter_authenticate (greeter, request + strlen (r));
00061     g_free (r);
00062 
00063     r = g_strdup_printf ("GREETER %s AUTHENTICATE-GUEST", getenv ("DISPLAY"));
00064     if (strcmp (request, r) == 0)
00065         lightdm_greeter_authenticate_as_guest (greeter);
00066     g_free (r);
00067 
00068     r = g_strdup_printf ("GREETER %s RESPOND TEXT=\"", getenv ("DISPLAY"));
00069     if (g_str_has_prefix (request, r))
00070     {
00071         gchar *text = g_strdup (request + strlen (r));
00072         text[strlen (text) - 1] = '\0';
00073         lightdm_greeter_respond (greeter, text);
00074         g_free (text);
00075     }
00076     g_free (r);
00077 
00078     r = g_strdup_printf ("GREETER %s START-SESSION", getenv ("DISPLAY"));
00079     if (strcmp (request, r) == 0)
00080     {
00081         if (!lightdm_greeter_start_session_sync (greeter, NULL, NULL))
00082             status_notify ("GREETER %s SESSION-FAILED", getenv ("DISPLAY")); 
00083     }
00084     g_free (r);
00085 
00086     r = g_strdup_printf ("GREETER %s START-SESSION SESSION=", getenv ("DISPLAY"));
00087     if (g_str_has_prefix (request, r))
00088     {
00089         if (!lightdm_greeter_start_session_sync (greeter, request + strlen (r), NULL))
00090             status_notify ("GREETER %s SESSION-FAILED", getenv ("DISPLAY")); 
00091     }
00092     g_free (r);
00093 
00094     r = g_strdup_printf ("GREETER %s LOG-LAYOUT", getenv ("DISPLAY"));
00095     if (strcmp (request, r) == 0)
00096     {
00097         const gchar *layout;
00098         layout = lightdm_layout_get_name (lightdm_get_layout ());
00099         status_notify ("GREETER %s LOG-LAYOUT LAYOUT='%s'", getenv ("DISPLAY"), layout ? layout : "");
00100     }
00101 
00102     r = g_strdup_printf ("GREETER %s LOG-LAYOUT USERNAME=", getenv ("DISPLAY"));
00103     if (g_str_has_prefix (request, r))
00104     {
00105         LightDMUser *user;
00106         const gchar *username, *layout;
00107 
00108         username = request + strlen (r);
00109         user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username);
00110         layout = lightdm_user_get_layout (user);
00111 
00112         status_notify ("GREETER %s LOG-LAYOUT USERNAME=%s LAYOUT='%s'", getenv ("DISPLAY"), username, layout ? layout : "");
00113     }
00114     g_free (r);
00115 
00116     r = g_strdup_printf ("GREETER %s LOG-LAYOUTS USERNAME=", getenv ("DISPLAY"));
00117     if (g_str_has_prefix (request, r))
00118     {
00119         LightDMUser *user;
00120         const gchar *username;
00121         const gchar * const *layouts;
00122         int i;
00123 
00124         username = request + strlen (r);
00125         user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username);
00126         layouts = lightdm_user_get_layouts (user);
00127 
00128         for (i = 0; layouts[i]; i++)
00129             status_notify ("GREETER %s LOG-LAYOUTS USERNAME=%s LAYOUT='%s'", getenv ("DISPLAY"), username, layouts[i]);
00130     }
00131     g_free (r);
00132 
00133     r = g_strdup_printf ("GREETER %s LOG-VARIANTS LAYOUT=", getenv ("DISPLAY"));
00134     if (g_str_has_prefix (request, r))
00135     {
00136         GList *layouts, *iter;
00137         const gchar *layout_prefix;
00138 
00139         layout_prefix = request + strlen (r);
00140         layouts = lightdm_get_layouts ();
00141 
00142         for (iter = layouts; iter; iter = iter->next)
00143         {
00144             LightDMLayout *layout;
00145             const gchar *name;
00146 
00147             layout = (LightDMLayout *) iter->data;
00148             name = lightdm_layout_get_name (layout);
00149 
00150             if (g_str_has_prefix (name, layout_prefix))
00151                 status_notify ("GREETER %s LOG-VARIANTS LAYOUT='%s'", getenv ("DISPLAY"), name);
00152         }
00153     }
00154     g_free (r);
00155 
00156     r = g_strdup_printf ("GREETER %s LOG-LANGUAGE USERNAME=", getenv ("DISPLAY"));  
00157     if (g_str_has_prefix (request, r))
00158     {
00159         LightDMUser *user;
00160         const gchar *username, *language;
00161 
00162         username = request + strlen (r);
00163         user = lightdm_user_list_get_user_by_name (lightdm_user_list_get_instance (), username);
00164         language = lightdm_user_get_language (user);
00165 
00166         status_notify ("GREETER %s LOG-LANGUAGE USERNAME=%s LANGUAGE=%s", getenv ("DISPLAY"), username, language ? language : "");
00167     }
00168     g_free (r);
00169 }
00170 
00171 int
00172 main (int argc, char **argv)
00173 {
00174     GMainLoop *main_loop;
00175 
00176     signal (SIGINT, signal_cb);
00177     signal (SIGTERM, signal_cb);
00178 
00179     g_type_init ();
00180 
00181     main_loop = g_main_loop_new (NULL, FALSE);
00182 
00183     status_connect (request_cb);
00184 
00185     status_notify ("GREETER %s START", getenv ("DISPLAY"));
00186 
00187     config = g_key_file_new ();
00188     g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
00189 
00190     if (g_key_file_has_key (config, "test-greeter-config", "return-value", NULL))
00191     {
00192         int return_value = g_key_file_get_integer (config, "test-greeter-config", "return-value", NULL);
00193         status_notify ("GREETER %s EXIT CODE=%d", getenv ("DISPLAY"), return_value);
00194         return return_value;
00195     }
00196 
00197     connection = xcb_connect (NULL, NULL);
00198 
00199     if (xcb_connection_has_error (connection))
00200     {
00201         status_notify ("GREETER %s FAIL-CONNECT-XSERVER", getenv ("DISPLAY"));
00202         return EXIT_FAILURE;
00203     }
00204 
00205     status_notify ("GREETER %s CONNECT-XSERVER", getenv ("DISPLAY"));
00206 
00207     greeter = lightdm_greeter_new ();
00208     g_signal_connect (greeter, "show-message", G_CALLBACK (show_message_cb), NULL);
00209     g_signal_connect (greeter, "show-prompt", G_CALLBACK (show_prompt_cb), NULL);
00210     g_signal_connect (greeter, "authentication-complete", G_CALLBACK (authentication_complete_cb), NULL);
00211 
00212     status_notify ("GREETER %s CONNECT-TO-DAEMON", getenv ("DISPLAY"));
00213     if (!lightdm_greeter_connect_sync (greeter, NULL))
00214     {
00215         status_notify ("GREETER %s FAIL-CONNECT-DAEMON", getenv ("DISPLAY"));
00216         return EXIT_FAILURE;
00217     }
00218 
00219     status_notify ("GREETER %s CONNECTED-TO-DAEMON", getenv ("DISPLAY"));
00220 
00221     if (lightdm_greeter_get_select_user_hint (greeter))
00222         status_notify ("GREETER %s SELECT-USER-HINT USERNAME=%s", getenv ("DISPLAY"), lightdm_greeter_get_select_user_hint (greeter));
00223     if (lightdm_greeter_get_lock_hint (greeter))
00224         status_notify ("GREETER %s LOCK-HINT", getenv ("DISPLAY"));
00225 
00226     g_main_loop_run (main_loop);
00227 
00228     return EXIT_SUCCESS;
00229 }