]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blobdiff - liblightdm-gobject/layout.c
Revert r2392 - it seems to have broken ABI in liblightdm-gobject
[sojka/lightdm.git] / liblightdm-gobject / layout.c
index 6e5442f7a0d8292e23491918bf24ee173fb725d4..a780343a32d14aa40664443adb27977ae49cef8b 100644 (file)
@@ -1,12 +1,12 @@
-/*
+/* -*- Mode: C; indent-tabs-mode:nil; tab-width:4 -*-
+ *
  * Copyright (C) 2010 Robert Ancell.
  * Author: Robert Ancell <robert.ancell@canonical.com>
- * 
+ *
  * This library is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option) any
- * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
- * license.
+ * Software Foundation; either version 2 or version 3 of the License.
+ * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
  */
 
 #include <libxklavier/xklavier.h>
@@ -14,8 +14,7 @@
 #include "lightdm/layout.h"
 
 enum {
-    PROP_0,
-    PROP_NAME,
+    PROP_NAME = 1,
     PROP_SHORT_DESCRIPTION,
     PROP_DESCRIPTION
 };
@@ -36,6 +35,55 @@ static Display *display = NULL;
 static XklEngine *xkl_engine = NULL;
 static XklConfigRec *xkl_config = NULL;
 static GList *layouts = NULL;
+static LightDMLayout *default_layout = NULL;
+
+static gchar *
+make_layout_string (const gchar *layout, const gchar *variant)
+{
+    if (!layout || layout[0] == 0)
+        return NULL;
+    else if (!variant || variant[0] == 0)
+        return g_strdup (layout);
+    else
+        return g_strdup_printf ("%s\t%s", layout, variant);
+}
+
+static void
+parse_layout_string (const gchar *name, gchar **layout, gchar **variant)
+{
+    gchar **split;
+
+    *layout = NULL;
+    *variant = NULL;
+
+    if (!name)
+        return;
+
+    split = g_strsplit (name, "\t", 2);
+    if (split[0])
+    {
+        *layout = g_strdup (split[0]);
+        if (split[1])
+            *variant = g_strdup (split[1]);
+    }
+    g_strfreev (split);
+}
+
+static void
+variant_cb (XklConfigRegistry *config,
+           const XklConfigItem *item,
+           gpointer data)
+{
+    LightDMLayout *layout;
+    gchar *full_name;
+
+    full_name = make_layout_string (data, item->name);
+
+    layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", full_name, "short-description", item->short_description, "description", item->description, NULL);
+    layouts = g_list_append (layouts, layout);
+
+    g_free (full_name);
+}
 
 static void
 layout_cb (XklConfigRegistry *config,
@@ -46,6 +94,8 @@ layout_cb (XklConfigRegistry *config,
 
     layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", item->name, "short-description", item->short_description, "description", item->description, NULL);
     layouts = g_list_append (layouts, layout);
+
+    xkl_config_registry_foreach_layout_variant (config, item->name, variant_cb, (gpointer) item->name);
 }
 
 /**
@@ -64,6 +114,9 @@ lightdm_get_layouts (void)
         return layouts;
 
     display = XOpenDisplay (NULL);
+    if (display == NULL)
+        return NULL;
+
     xkl_engine = xkl_engine_get_instance (display);
     xkl_config = xkl_config_rec_new ();
     if (!xkl_config_rec_get_from_server (xkl_config, xkl_engine))
@@ -86,19 +139,25 @@ lightdm_get_layouts (void)
  * Set the layout for this session.
  **/
 void
-lightdm_set_layout (LightDMLayout *layout)
+lightdm_set_layout (LightDMLayout *dmlayout)
 {
     XklConfigRec *config;
+    gchar *layout, *variant;
+
+    g_return_if_fail (dmlayout != NULL);
 
-    g_return_if_fail (layout != NULL);
+    g_debug ("Setting keyboard layout to '%s'", lightdm_layout_get_name (dmlayout));
 
-    g_debug ("Setting keyboard layout to %s", lightdm_layout_get_name (layout));
+    parse_layout_string (lightdm_layout_get_name (dmlayout), &layout, &variant);
 
     config = xkl_config_rec_new ();
     config->layouts = g_malloc (sizeof (gchar *) * 2);
+    config->variants = g_malloc (sizeof (gchar *) * 2);
     config->model = g_strdup (xkl_config->model);
-    config->layouts[0] = g_strdup (lightdm_layout_get_name (layout));
+    config->layouts[0] = layout;
     config->layouts[1] = NULL;
+    config->variants[0] = variant;
+    config->variants[1] = NULL;
     if (!xkl_config_rec_activate (config, xkl_engine))
         g_warning ("Failed to activate XKL config");
     g_object_unref (config);
@@ -115,18 +174,37 @@ LightDMLayout *
 lightdm_get_layout (void)
 {
     lightdm_get_layouts ();
-    if (layouts)
-        return (LightDMLayout *) g_list_first (layouts)->data;
-    else
-        return NULL;
+
+    if (layouts && xkl_config && !default_layout)
+    {
+        gchar *full_name;
+        GList *item;
+
+        full_name = make_layout_string (xkl_config->layouts ? xkl_config->layouts[0] : NULL,
+                                        xkl_config->variants ? xkl_config->variants[0] : NULL);
+
+        for (item = layouts; item; item = item->next)
+        {
+            LightDMLayout *iter_layout = (LightDMLayout *) item->data;
+            if (g_strcmp0 (lightdm_layout_get_name (iter_layout), full_name) == 0)
+            {
+                default_layout = iter_layout;
+                break;
+            }
+        }
+
+        g_free (full_name);
+    }
+
+    return default_layout;
 }
 
 /**
  * lightdm_layout_get_name:
  * @layout: A #LightDMLayout
- * 
+ *
  * Get the name of a layout.
- * 
+ *
  * Return value: The name of the layout
  **/
 const gchar *
@@ -139,7 +217,7 @@ lightdm_layout_get_name (LightDMLayout *layout)
 /**
  * lightdm_layout_get_short_description:
  * @layout: A #LightDMLayout
- * 
+ *
  * Get the short description of a layout.
  *
  * Return value: A short description of the layout
@@ -154,9 +232,9 @@ lightdm_layout_get_short_description (LightDMLayout *layout)
 /**
  * lightdm_layout_get_description:
  * @layout: A #LightDMLayout
- * 
+ *
  * Get the long description of a layout.
- * 
+ *
  * Return value: A long description of the layout
  **/
 const gchar *
@@ -173,9 +251,9 @@ lightdm_layout_init (LightDMLayout *layout)
 
 static void
 lightdm_layout_set_property (GObject      *object,
-                         guint         prop_id,
-                         const GValue *value,
-                         GParamSpec   *pspec)
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
 {
     LightDMLayout *self = LIGHTDM_LAYOUT (object);
     LightDMLayoutPrivate *priv = GET_PRIVATE (self);
@@ -201,9 +279,9 @@ lightdm_layout_set_property (GObject      *object,
 
 static void
 lightdm_layout_get_property (GObject    *object,
-                         guint       prop_id,
-                         GValue     *value,
-                         GParamSpec *pspec)
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
 {
     LightDMLayout *self;
 
@@ -225,15 +303,27 @@ lightdm_layout_get_property (GObject    *object,
     }
 }
 
+static void
+lightdm_layout_finalize (GObject *object)
+{
+    LightDMLayout *self = LIGHTDM_LAYOUT (object);
+    LightDMLayoutPrivate *priv = GET_PRIVATE (self);
+
+    g_free (priv->name);
+    g_free (priv->short_description);  
+    g_free (priv->description);
+}
+
 static void
 lightdm_layout_class_init (LightDMLayoutClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  
+
     g_type_class_add_private (klass, sizeof (LightDMLayoutPrivate));
 
     object_class->set_property = lightdm_layout_set_property;
     object_class->get_property = lightdm_layout_get_property;
+    object_class->finalize = lightdm_layout_finalize;
 
     g_object_class_install_property (object_class,
                                      PROP_NAME,