[caribou] libcaribou: refactor some code



commit aac59640e793752d1ae815ffea4d9113f77679e0
Author: Daiki Ueno <ueno unixuser org>
Date:   Sat Nov 17 10:27:12 2012 +0900

    libcaribou: refactor some code
    
    Use Gee lists and hashtables extensively instead of GLib's;
    specify proper cname to libxklavier signals to avoid using
    a static signal handler; remove unnecessary "using".
    
    https://bugzilla.gnome.org/show_bug.cgi?id=688517

 libcaribou/group-model.vala      |    2 -
 libcaribou/key-model.vala        |    2 -
 libcaribou/keyboard-model.vala   |    2 -
 libcaribou/level-model.vala      |    2 -
 libcaribou/libxklavier.vapi      |    3 ++
 libcaribou/xadapter.vala         |   52 +++++++++++++++----------------------
 libcaribou/xml-deserializer.vala |   34 +++++++++++-------------
 7 files changed, 40 insertions(+), 57 deletions(-)
---
diff --git a/libcaribou/group-model.vala b/libcaribou/group-model.vala
index 0721189..0107822 100644
--- a/libcaribou/group-model.vala
+++ b/libcaribou/group-model.vala
@@ -1,5 +1,3 @@
-using GLib;
-
 namespace Caribou {
     public class GroupModel : Object, IKeyboardObject {
         public string active_level { get; private set; }
diff --git a/libcaribou/key-model.vala b/libcaribou/key-model.vala
index af7b623..017e046 100644
--- a/libcaribou/key-model.vala
+++ b/libcaribou/key-model.vala
@@ -1,5 +1,3 @@
-using GLib;
-
 namespace Caribou {
     public class KeyModel : GLib.Object, IScannableItem, IKeyboardObject {
         public string align { get; set; default = "center"; }
diff --git a/libcaribou/keyboard-model.vala b/libcaribou/keyboard-model.vala
index e7370ac..b40a557 100644
--- a/libcaribou/keyboard-model.vala
+++ b/libcaribou/keyboard-model.vala
@@ -1,5 +1,3 @@
-using Bus;
-
 namespace Caribou {
     public class KeyboardModel : Object, IKeyboardObject {
         public string active_group { get; private set; default = ""; }
diff --git a/libcaribou/level-model.vala b/libcaribou/level-model.vala
index 6c526b9..e55526c 100644
--- a/libcaribou/level-model.vala
+++ b/libcaribou/level-model.vala
@@ -1,5 +1,3 @@
-using GLib;
-
 namespace Caribou {
     public class LevelModel : ScannableGroup, IKeyboardObject {
         public signal void level_toggled (string new_level);
diff --git a/libcaribou/libxklavier.vapi b/libcaribou/libxklavier.vapi
index 4fa1058..6420d01 100644
--- a/libcaribou/libxklavier.vapi
+++ b/libcaribou/libxklavier.vapi
@@ -114,8 +114,11 @@ namespace Xkl {
 		public uint max_num_groups { get; }
 		public uint num_groups { get; }
 		public uint secondary_groups_mask { get; }
+		[CCode (cname = "X_config_changed")]
 		public virtual signal void X_config_changed ();
+		[CCode (cname = "X_new_device")]
 		public virtual signal void X_new_device ();
+		[CCode (cname = "X_state_changed")]
 		public virtual signal void X_state_changed (int p0, int p1, bool p2);
 		public virtual signal int new_toplevel_window (long p0, long p1);
 	}
diff --git a/libcaribou/xadapter.vala b/libcaribou/xadapter.vala
index 38e9c8e..012c42d 100644
--- a/libcaribou/xadapter.vala
+++ b/libcaribou/xadapter.vala
@@ -1,9 +1,3 @@
-using Xkl;
-using Gdk;
-using Xkb;
-using XTest;
-using X;
-
 namespace Caribou {
     public class XAdapter : Object {
 
@@ -32,8 +26,8 @@ namespace Caribou {
             }
         }
 
-        HashTable<uint, KeyButtonHandler> button_funcs;
-        HashTable<uint, KeyButtonHandler> key_funcs;
+        Gee.HashMap<uint, KeyButtonHandler> button_funcs;
+        Gee.HashMap<uint, KeyButtonHandler> key_funcs;
 
         construct {
             Xkb.State xkb_state;
@@ -49,9 +43,7 @@ namespace Caribou {
             xkl_engine.start_listen (Xkl.EngineListenModes.TRACK_KEYBOARD_STATE);
             xkl_state = this.xkl_engine.get_current_state ();
             this.group = (uchar) xkl_state.group;
-            Signal.connect_object (xkl_engine, "X-state-changed",
-                                   (Callback) xkl_state_changed,
-                                   this, ConnectFlags.AFTER);
+            xkl_engine.X_state_changed.connect_after (xkl_state_changed);
 
             Xkb.get_state (this.xdisplay, Xkb.UseCoreKbd, out xkb_state);
             this.modifiers = xkb_state.mods;
@@ -68,11 +60,9 @@ namespace Caribou {
                 level_switch_modifiers += Gdk.ModifierType.SHIFT_MASK | lv3_mod;
             }
 
-            button_funcs = new HashTable<uint, KeyButtonHandler> (direct_hash,
-                                                                  direct_equal);
+            button_funcs = new Gee.HashMap<uint, KeyButtonHandler> ();
 
-            key_funcs = new HashTable<uint, KeyButtonHandler> (direct_hash,
-                                                               direct_equal);
+            key_funcs = new Gee.HashMap<uint, KeyButtonHandler> ();
 
             Xkb.select_events (
                 this.xdisplay, Xkb.UseCoreKbd,
@@ -102,7 +92,7 @@ namespace Caribou {
             if (xev.type == X.EventType.ButtonPress ||
                 xev.type == X.EventType.ButtonRelease) {
                 KeyButtonHandler handler =
-                    (KeyButtonHandler) button_funcs.lookup (xev.xbutton.button);
+                    (KeyButtonHandler) button_funcs.get (xev.xbutton.button);
                 if (handler != null)
                     handler.cb (xev.xbutton.button,
                                 xev.type == X.EventType.ButtonPress);
@@ -110,7 +100,7 @@ namespace Caribou {
                        xev.type == X.EventType.KeyRelease) {
 
                 KeyButtonHandler handler =
-                    (KeyButtonHandler) key_funcs.lookup (xev.xkey.keycode);
+                    (KeyButtonHandler) key_funcs.get (xev.xkey.keycode);
                 if (handler != null)
                     handler.cb (xev.xkey.keycode,
                                 xev.type == X.EventType.KeyPress);
@@ -124,14 +114,14 @@ namespace Caribou {
             return Gdk.FilterReturn.CONTINUE;
         }
 
-		private static void xkl_state_changed (Xkl.Engine xklengine, int type, int group, bool restore, XAdapter self) {
-			string group_name;
-			string variant_name;
+        private void xkl_state_changed (int type, int group, bool restore) {
+            string group_name;
+            string variant_name;
 
-			self.group = (uchar) group;
-			self.get_current_group (out group_name, out variant_name);
-			self.group_changed (self.group, group_name, variant_name);
-		}
+            this.group = (uchar) group;
+            get_current_group (out group_name, out variant_name);
+            group_changed (this.group, group_name, variant_name);
+        }
 
         private uchar keysym_to_modifier (uint keyval) {
             for (var i = xkbdesc.min_key_code; i <= xkbdesc.max_key_code; i++) {
@@ -218,7 +208,7 @@ namespace Caribou {
 
             Gdk.KeymapKey? best_match = null;
 
-            foreach (KeymapKey km in kmk)
+            foreach (Gdk.KeymapKey km in kmk)
                if (km.group == this.group &&
                    km.level < this.level_switch_modifiers.length)
                    best_match = km;
@@ -329,12 +319,12 @@ namespace Caribou {
 
             if (func != null) {
                 var handler = new KeyButtonHandler (func);
-                key_funcs.insert (keycode, handler);
+                key_funcs.set (keycode, handler);
                 xdisplay.grab_key ((int)keycode, 0, xid,
-                                   true, GrabMode.Async, GrabMode.Async);
+                                   true, X.GrabMode.Async, X.GrabMode.Async);
 
             } else {
-                key_funcs.remove (keycode);
+                key_funcs.unset (keycode);
                 xdisplay.ungrab_key ((int)keycode, 0, xid);
             }
         }
@@ -342,14 +332,14 @@ namespace Caribou {
         public void register_button_func (uint button, KeyButtonCallback? func) {
             if (func != null) {
                 var handler = new KeyButtonHandler (func);
-                button_funcs.insert (button, handler);
+                button_funcs.set (button, handler);
                 xdisplay.grab_button (button, 0, xid, true,
                                       X.EventMask.ButtonPressMask |
                                       X.EventMask.ButtonReleaseMask,
-                                      GrabMode.Async, GrabMode.Async, 0, 0);
+                                      X.GrabMode.Async, X.GrabMode.Async, 0, 0);
 
             } else {
-                button_funcs.remove (button);
+                button_funcs.unset (button);
                 xdisplay.ungrab_button (button, 0, xid);
             }
         }
diff --git a/libcaribou/xml-deserializer.vala b/libcaribou/xml-deserializer.vala
index 4bfc088..8058c0f 100644
--- a/libcaribou/xml-deserializer.vala
+++ b/libcaribou/xml-deserializer.vala
@@ -1,5 +1,3 @@
-using Xml;
-
 namespace Caribou {
 
     private class XmlDeserializer : Object {
@@ -22,19 +20,19 @@ namespace Caribou {
         public static string get_layout_file (string keyboard_type, string group,
                                               string variant) throws IOError {
 
-            List<string> dirs = new List<string> ();
+            Gee.ArrayList<string> dirs = new Gee.ArrayList<string> ();
             string custom_dir = Environment.get_variable("CARIBOU_LAYOUTS_DIR");
 
             if (custom_dir != null)
-                dirs.append (Path.build_filename (custom_dir, "layouts",
-                                                  keyboard_type));
+                dirs.add (Path.build_filename (custom_dir, "layouts",
+                                               keyboard_type));
 
-            dirs.append (Path.build_filename (Environment.get_user_data_dir (),
-                                              "caribou", "layouts", keyboard_type));
+            dirs.add (Path.build_filename (Environment.get_user_data_dir (),
+                                           "caribou", "layouts", keyboard_type));
 
             foreach (string dir in Environment.get_system_data_dirs ()) {
-                dirs.append (Path.build_filename (
-                                 dir, "caribou", "layouts", keyboard_type));
+                dirs.add (Path.build_filename (
+                              dir, "caribou", "layouts", keyboard_type));
             }
 
             foreach (string data_dir in dirs) {
@@ -57,11 +55,11 @@ namespace Caribou {
 
         public static GroupModel? load_group (string keyboard_type,
                                               string group, string variant) {
-            Doc* doc;
+            Xml.Doc* doc;
 
             try {
                 string fn = get_layout_file (keyboard_type, group, variant);
-                doc = Parser.parse_file (fn);
+                doc = Xml.Parser.parse_file (fn);
                 if (doc == null)
                     throw new IOError.FAILED (
                         "Cannot load XML text reader for %s", fn);
@@ -76,7 +74,7 @@ namespace Caribou {
             create_levels_from_xml (grp, node);
 
             delete doc;
-            Parser.cleanup ();
+            Xml.Parser.cleanup ();
 
             return grp;
         }
@@ -85,7 +83,7 @@ namespace Caribou {
                                                    Xml.Node* node) {
             assert (node->name == "layout");
             for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
-                if (iter->type != ElementType.ELEMENT_NODE)
+                if (iter->type != Xml.ElementType.ELEMENT_NODE)
                     continue;
 
                 string levelname = iter->get_prop ("name");
@@ -101,14 +99,14 @@ namespace Caribou {
         public static void load_rows (LevelModel level, Xml.Node* node) {
             assert (node->name == "level");
             for (Xml.Node* i = node->children; i != null; i = i->next) {
-                if (i->type != ElementType.ELEMENT_NODE)
+                if (i->type != Xml.ElementType.ELEMENT_NODE)
                     continue;
 
                 RowModel row = new RowModel ();
                 level.add_row (row);
 
                 for (Xml.Node* i2 = i->children; i2 != null; i2 = i2->next) {
-                    if (i2->type != ElementType.ELEMENT_NODE)
+                    if (i2->type != Xml.ElementType.ELEMENT_NODE)
                         continue;
 
                     if (i2->name == "key") {
@@ -126,7 +124,7 @@ namespace Caribou {
             row.add_column (column);
 
             for (Xml.Node* i = node->children; i != null; i = i->next) {
-                if (i->type != ElementType.ELEMENT_NODE)
+                if (i->type != Xml.ElementType.ELEMENT_NODE)
                     continue;
 
                 column.add_key (load_key (i, align));
@@ -146,7 +144,7 @@ namespace Caribou {
             if (align != null)
                 key.align = align;
 
-            for (Attr* prop = node->properties; prop != null; prop = prop->next) {
+            for (Xml.Attr* prop = node->properties; prop != null; prop = prop->next) {
                 if (prop->name == "toggle")
                     key.toggle = prop->children->content;
                 else if (prop->name == "align")
@@ -156,7 +154,7 @@ namespace Caribou {
             }
 
             for (Xml.Node* i = node->children; i != null; i = i->next) {
-                if (i->type != ElementType.ELEMENT_NODE)
+                if (i->type != Xml.ElementType.ELEMENT_NODE)
                     continue;
 
                 key.add_subkey (load_key (i, null));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]