#
# Description: Get default keyboard layout from hal
# Ubuntu: https://bugs.launchpad.net/bugs/395103
# Fedora: http://cvs.fedoraproject.org/viewvc//devel/gdm/gdm-system-keyboard.patch?view=markup
#
diff -Nur -x '*.orig' -x '*~' gdm-2.28.0/configure.ac gdm-2.28.0.new/configure.ac
--- gdm-2.28.0/configure.ac	2009-09-21 22:06:40.000000000 +0200
+++ gdm-2.28.0.new/configure.ac	2009-10-01 12:35:50.345762314 +0200
@@ -70,6 +70,7 @@
         dbus-glib-1 >= $DBUS_GLIB_REQUIRED_VERSION
         gobject-2.0 >= $GLIB_REQUIRED_VERSION
         gio-2.0 >= $GLIB_REQUIRED_VERSION
+        hal
 )
 AC_SUBST(DAEMON_CFLAGS)
 AC_SUBST(DAEMON_LIBS)
diff -Nur -x '*.orig' -x '*~' gdm-2.28.0/daemon/gdm-session-direct.c gdm-2.28.0.new/daemon/gdm-session-direct.c
--- gdm-2.28.0/daemon/gdm-session-direct.c	2009-09-21 22:05:27.000000000 +0200
+++ gdm-2.28.0.new/daemon/gdm-session-direct.c	2009-10-01 12:34:43.000000000 +0200
@@ -45,6 +45,8 @@
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
 
+#include <libhal.h>
+
 #include "gdm-session-direct.h"
 #include "gdm-session.h"
 #include "gdm-session-private.h"
@@ -598,14 +600,87 @@
     return setlocale (LC_MESSAGES, NULL);
 }
 
+static char *
+get_system_default_layout (GdmSessionDirect *session)
+{
+    DBusConnection *connection;
+    LibHalContext *ctx;
+    char **devices;
+    int n_devices;
+    char *layout;
+    char *variant;
+    char *result;
+
+    result = NULL;
+
+    connection = dbus_g_connection_get_connection (session->priv->connection);
+    ctx = libhal_ctx_new ();
+    libhal_ctx_set_dbus_connection (ctx, connection);
+
+    if (!libhal_ctx_init (ctx, NULL)) {
+        goto out;
+    }
+
+    devices = libhal_find_device_by_capability (ctx,
+                                                "input.keyboard",
+                                                &n_devices,
+                                                NULL);
+    if (n_devices > 0) {
+        layout = libhal_device_get_property_string (ctx,
+                                                    devices[0],
+                                                    "input.x11_options.XkbLayout",
+                                                    NULL);
+        if (!layout) {
+            layout = libhal_device_get_property_string (ctx,
+                                                        devices[0],
+                                                        "input.xkb.layout",
+                                                        NULL);
+        }
+	if (!layout)
+	    goto out;
+
+        variant = libhal_device_get_property_string (ctx,
+                                                    devices[0],
+                                                    "input.x11_options.XkbVariant",
+                                                    NULL);
+        if (!variant) {
+            variant = libhal_device_get_property_string (ctx,
+                                                        devices[0],
+                                                        "input.xkb.variant",
+                                                        NULL);
+        }
+
+	if (variant) {
+	    result = g_strdup_printf("%s\t%s", layout, variant);
+	    libhal_free_string (variant);
+	} else {
+	    result = g_strdup (layout);
+	}
+	libhal_free_string (layout);
+    }
+
+    libhal_free_string_array (devices);
+
+    libhal_ctx_shutdown (ctx, NULL);
+    libhal_ctx_free (ctx);
+
+out:
+    if (!result) {
+        result = g_strdup ("us");
+    }
+
+    g_debug ("GdmSessionDirect: System default keyboard layout: '%s'", result);
+    return result;
+}
+
 static const char *
 get_default_layout_name (GdmSessionDirect *session)
 {
-    if (session->priv->saved_layout != NULL) {
-                return session->priv->saved_layout;
+    if (!session->priv->saved_layout) {
+        session->priv->saved_layout = get_system_default_layout (session);
     }
 
-    return "us";
+    return session->priv->saved_layout;
 }
 
 static char *
