[uim-commit] r276 - in trunk: scm test uim

yamaken@freedesktop.org yamaken@freedesktop.org
Thu Jan 13 06:21:59 PST 2005


Author: yamaken
Date: 2005-01-13 06:21:56 -0800 (Thu, 13 Jan 2005)
New Revision: 276

Modified:
   trunk/scm/custom.scm
   trunk/test/test-custom.scm
   trunk/uim/uim-custom.c
   trunk/uim/uim-custom.h
Log:
* This commit adds "a group is updated" callback and "group list is
  updated" callback feature to uim-custom API. The features are
  validated by test-custom.scm in Scheme level. Calling via
  uim-custom.h is not yet tested

* uim/uim-custom.h
  - (uim_custom_group_cb_add, uim_custom_group_cb_remove,
    uim_custom_global_cb_add, uim_custom_global_cb_remove): New
    function
* uim/uim-custom.c
  - (uim_custom_group_cb_add, uim_custom_group_cb_remove,
    uim_custom_global_cb_add, uim_custom_global_cb_remove): New
    function
  - (uim_custom_global_cb_update_cb_t): New type
  - (uim_custom_global_cb_update_cb_gate, uim_bool custom_cb_add,
    uim_bool custom_cb_remove): New static function
  - (uim_custom_init): Add initialization of custom-global-update-cb-gate
  - (uim_custom_cb_add): Simplify with custom_cb_add()
  - (uim_custom_cb_remove): Simplify with custom_cb_remove()
* scm/custom.scm
  - (custom-group-update-hooks, custom-group-list-update-hooks): New
    variable
  - (define-custom-group): Add custom-group-list-update-hooks handling
  - (define-custom): Add custom-group-update-hooks handling
  - (custom-register-update-cb): Remove
  - (custom-register-cb): New procedure
* test/test-custom.scm
  - (testcase custom hooks): Modify setup proc
  - (test custom-register-update-cb, test custom-register-update-cb (2
    callbaks)): Remove
  - (test custom-register-cb (custom update hook), test
    custom-register-cb (custom update hook, 2 callbaks), test
    custom-register-cb (custom-group update hook), test
    custom-register-cb (custom-group update hook, 2 callbaks), test
    custom-register-cb (group-list update hook)): New test


Modified: trunk/scm/custom.scm
===================================================================
--- trunk/scm/custom.scm	2005-01-13 10:29:50 UTC (rev 275)
+++ trunk/scm/custom.scm	2005-01-13 14:21:56 UTC (rev 276)
@@ -43,10 +43,12 @@
 (define custom-subgroup-alist ())
 
 (define custom-activity-hooks ())
-(define custom-update-hooks ())
 (define custom-get-hooks ())
 (define custom-set-hooks ())
 (define custom-literalize-hooks ())
+(define custom-update-hooks ())
+(define custom-group-update-hooks ())
+(define custom-group-list-update-hooks ())
 
 (define custom-validator-alist
   '((boolean      . custom-boolean?)
@@ -152,8 +154,10 @@
   (lambda (gsym label desc)
     (let ((grec (custom-group-rec-new gsym label desc)))
       (if (not (custom-group-rec gsym))
-	  (set! custom-group-rec-alist (cons grec
-					     custom-group-rec-alist))))))
+	  (begin
+	    (set! custom-group-rec-alist (cons grec custom-group-rec-alist))
+	    (custom-call-hook-procs 'global
+				    custom-group-list-update-hooks))))))
 
 (define custom-group-rec
   (lambda (gsym)
@@ -262,8 +266,9 @@
 	  (primary-grp (car groups))
 	  (subgrps (cons 'main (cdr groups))))
       (if (not (custom-rec sym))
-	  (set! custom-rec-alist (cons crec
-				       custom-rec-alist)))
+	  (begin
+	    (set! custom-rec-alist (cons crec custom-rec-alist))
+	    (custom-call-hook-procs primary-grp custom-group-update-hooks)))
       (if (not (symbol-bound? sym))
 	  (let ((quoted-default (if (or (symbol? default)
 					(list? default))
@@ -464,8 +469,8 @@
   (lambda (context custom-sym val)
     (custom-set-value! custom-sym val)))
 
-(define custom-register-update-cb
-  (lambda (custom-sym ptr gate-func func)
-    (and (custom-rec custom-sym)
+(define custom-register-cb
+  (lambda (hook valid? custom-sym ptr gate-func func)
+    (and (valid? custom-sym)
 	 (let ((cb (lambda () (gate-func func ptr custom-sym))))
-	   (custom-add-hook custom-sym 'custom-update-hooks cb)))))
+	   (custom-add-hook custom-sym hook cb)))))

Modified: trunk/test/test-custom.scm
===================================================================
--- trunk/test/test-custom.scm	2005-01-13 10:29:50 UTC (rev 275)
+++ trunk/test/test-custom.scm	2005-01-13 14:21:56 UTC (rev 276)
@@ -764,11 +764,15 @@
    (lambda ()
      (uim '(require "custom.scm"))
      (uim '(define test-hook ()))
+     (uim '(define test-group1-trace ()))
      (uim '(define test-custom1-trace ()))
      (uim '(define test-custom2-trace ()))
      (uim '(define test-custom3-trace ()))
+     (uim '(define-custom-group 'test-group1
+	                        (_ "Test group 1")
+				(_ "long description will be here.")))
      (uim '(define-custom 'test-custom1 'test-custom1-ddskk
-	     '(global)
+	     '(test-group1)
 	     '(choice
 	       (test-custom1-uim "uim" "uim native")
 	       (test-custom1-ddskk "ddskk like" "Similar to ddskk")
@@ -1158,32 +1162,38 @@
 		 (uim 'test-custom1-trace))
    (assert-true  (uim-bool '(custom-active? 'test-custom1))))
 
-  ("test custom-register-update-cb"
+  ("test custom-register-cb (custom update hook)"
    (uim '(define test-update-gate (lambda (func ptr custom-sym)
 				    (set! test-custom1-trace
 					  (list func ptr custom-sym)))))
-   (uim '(custom-register-update-cb 'test-custom1
-				    'custom1-ptr
-				    test-update-gate 'custom1-func))
+   (uim '(custom-register-cb 'custom-update-hooks
+			     custom-rec
+	                     'test-custom1
+			     'custom1-ptr
+			     test-update-gate 'custom1-func))
    (assert-equal ()
 		 (uim 'test-custom1-trace))
    (assert-equal 'test-custom1-ddskk
 		 (uim 'test-custom1))
-   ;; update hook
+   ;; custom update hook
    (assert-true  (uim-bool '(custom-set-value! 'test-custom1 'test-custom1-uim)))
    (assert-equal '(custom1-func custom1-ptr test-custom1)
 		 (uim 'test-custom1-trace)))
-  ("test custom-register-update-cb (2 callbaks)"
+  ("test custom-register-cb (custom update hook, 2 callbaks)"
    (uim '(define test-update-gate (lambda (func ptr custom-sym)
 				    (set! test-custom1-trace
 					  (cons (list func ptr custom-sym)
 						test-custom1-trace)))))
-   (uim '(custom-register-update-cb 'test-custom1
-				    'custom1-ptr
-				    test-update-gate 'custom1-func))
-   (uim '(custom-register-update-cb 'test-custom1
-				    'custom1-ptr2
-				    test-update-gate 'custom1-func2))
+   (uim '(custom-register-cb 'custom-update-hooks
+			     custom-rec
+			     'test-custom1
+			     'custom1-ptr
+			     test-update-gate 'custom1-func))
+   (uim '(custom-register-cb 'custom-update-hooks
+			     custom-rec
+			     'test-custom1
+			     'custom1-ptr2
+			     test-update-gate 'custom1-func2))
    (assert-equal ()
 		 (uim 'test-custom1-trace))
    (assert-equal 'test-custom1-ddskk
@@ -1192,8 +1202,109 @@
    (assert-true  (uim-bool '(custom-set-value! 'test-custom1 'test-custom1-uim)))
    (assert-equal '((custom1-func custom1-ptr test-custom1)
 		   (custom1-func2 custom1-ptr2 test-custom1))
-		 (uim 'test-custom1-trace))))
+		 (uim 'test-custom1-trace)))
 
+  ("test custom-register-cb (custom-group update hook)"
+   (uim '(define test-update-gate (lambda (func ptr custom-sym)
+				    (set! test-group1-trace
+					  (list func ptr custom-sym)))))
+   (uim '(custom-register-cb 'custom-group-update-hooks
+			     custom-group-rec
+	                     'test-group1
+			     'group1-ptr
+			     test-update-gate 'group1-func))
+   (assert-equal ()
+		 (uim 'test-group1-trace))
+   (assert-equal 'test-custom1-ddskk
+		 (uim 'test-custom1))
+   ;; custom update hook is not set
+   (assert-true  (uim-bool '(custom-set-value! 'test-custom1 'test-custom1-uim)))
+   ;; custom-group update hook
+   (uim '(define-custom 'test-custom4 #f
+	   '(global)
+	   '(boolean)
+	   "Test custom4"
+	   "long description will be here."))
+   (assert-equal ()
+		 (uim 'test-group1-trace))
+   (uim '(define-custom 'test-custom5 #f
+	   '(test-group1)
+	   '(boolean)
+	   "Test custom5"
+	   "long description will be here."))
+   (assert-equal '(group1-func group1-ptr test-group1)
+		 (uim 'test-group1-trace)))
+
+  ("test custom-register-cb (custom-group update hook, 2 callbaks)"
+   (uim '(define test-update-gate (lambda (func ptr custom-sym)
+				    (set! test-group1-trace
+					  (cons (list func ptr custom-sym)
+						test-group1-trace)))))
+   (uim '(custom-register-cb 'custom-group-update-hooks
+			     custom-group-rec
+	                     'test-group1
+			     'group1-ptr
+			     test-update-gate 'group1-func))
+   (uim '(custom-register-cb 'custom-group-update-hooks
+			     custom-group-rec
+	                     'test-group1
+			     'group1-ptr
+			     test-update-gate 'group1-func2))
+   (assert-equal ()
+		 (uim 'test-group1-trace))
+   (assert-equal 'test-custom1-ddskk
+		 (uim 'test-custom1))
+   ;; custom update hook is not set
+   (assert-true  (uim-bool '(custom-set-value! 'test-custom1 'test-custom1-uim)))
+   (assert-equal '()
+		 (uim 'test-custom1-trace))
+   ;; custom-group update hook
+   (uim '(define-custom 'test-custom4 #f
+	   '(global)
+	   '(boolean)
+	   "Test custom4"
+	   "long description will be here."))
+   (assert-equal ()
+		 (uim 'test-group1-trace))
+   (uim '(define-custom 'test-custom5 #f
+	   '(test-group1)
+	   '(boolean)
+	   "Test custom5"
+	   "long description will be here."))
+   (assert-equal '((group1-func group1-ptr test-group1)
+		   (group1-func2 group1-ptr test-group1))
+		 (uim 'test-group1-trace)))
+
+  ("test custom-register-cb (group-list update hook)"
+   (uim '(define test-update-gate (lambda (func ptr custom-sym)
+				    (set! test-group1-trace
+					  (list func ptr custom-sym)))))
+   (uim '(custom-register-cb 'custom-group-list-update-hooks
+			     (lambda (dummy) #t)
+	                     'global
+			     'group1-ptr
+			     test-update-gate 'group1-func))
+   (assert-equal ()
+		 (uim 'test-group1-trace))
+   (assert-equal 'test-custom1-ddskk
+		 (uim 'test-custom1))
+   ;; custom update hook is not set
+   (assert-true  (uim-bool '(custom-set-value! 'test-custom1 'test-custom1-uim)))
+   ;; custom-group update hook is not set
+   (uim '(define-custom 'test-custom5 #f
+	   '(test-group1)
+	   '(boolean)
+	   "Test custom5"
+	   "long description will be here."))
+   (assert-equal ()
+		 (uim 'test-group1-trace))
+   ;; new group causes group-list-update-hook invocation
+   (uim '(define-custom-group 'test-group2
+	                      (_ "Test group 2")
+			      (_ "long description will be here.")))
+   (assert-equal '(group1-func group1-ptr global)
+		 (uim 'test-group1-trace))))
+
 (define-uim-test-case "testcase custom get and set hooks"
   (setup
    (lambda ()

Modified: trunk/uim/uim-custom.c
===================================================================
--- trunk/uim/uim-custom.c	2005-01-13 10:29:50 UTC (rev 275)
+++ trunk/uim/uim-custom.c	2005-01-13 14:21:56 UTC (rev 276)
@@ -56,6 +56,7 @@
 
 
 typedef void (*uim_custom_cb_update_cb_t)(void *ptr, const char *custom_sym);
+typedef void (*uim_custom_global_cb_update_cb_t)(void *ptr);
 
 static char *c_list_to_str(const void *const *list, char *(*mapper)(const void *elem), const char *sep);
 
@@ -90,6 +91,11 @@
 static union uim_custom_range *uim_custom_range_get(const char *custom_sym);
 static void uim_custom_range_free(int custom_type, union uim_custom_range *custom_range);
 static uim_lisp uim_custom_cb_update_cb_gate(uim_lisp cb, uim_lisp ptr, uim_lisp custom_sym);
+static uim_lisp uim_custom_global_cb_update_cb_gate(uim_lisp cb, uim_lisp ptr);
+static uim_bool custom_cb_add(const char *hook, const char *validator,
+			      const char *custom_sym, void *ptr,
+			      const char *gate_func, void (*cb)(void));
+static uim_bool custom_cb_remove(const char *key_sym, const char *hook);
 
 static void helper_disconnect_cb(void);
 static char *uim_conf_path(const char *subpath);
@@ -637,6 +643,8 @@
   uim_scm_gc_protect(&return_val);
 
   uim_scm_init_subr_3("custom-update-cb-gate", uim_custom_cb_update_cb_gate);
+  uim_scm_init_subr_2("custom-global-update-cb-gate",
+		      uim_custom_global_cb_update_cb_gate);
 
   uim_scm_require_file("custom.scm");
 
@@ -1186,6 +1194,54 @@
   return uim_scm_f();
 }
 
+static uim_lisp
+uim_custom_global_cb_update_cb_gate(uim_lisp cb, uim_lisp ptr)
+{
+  uim_custom_global_cb_update_cb_t update_cb;
+  void *c_ptr;
+
+  update_cb = (uim_custom_global_cb_update_cb_t)uim_scm_c_func_ptr(cb);
+  c_ptr = uim_scm_c_ptr(ptr);
+  (*update_cb)(c_ptr);
+
+  return uim_scm_f();
+}
+
+static uim_bool
+custom_cb_add(const char *hook, const char *validator,
+	      const char *custom_sym, void *ptr,
+	      const char *gate_func, void (*cb)(void))
+{
+  uim_bool succeeded;
+  uim_lisp stack_start;
+  uim_lisp form;
+
+  uim_scm_gc_protect_stack(&stack_start);
+  form = uim_scm_list5(uim_scm_quote(uim_scm_make_symbol(hook)),
+		       uim_scm_make_symbol(custom_sym),
+		       uim_scm_make_ptr(ptr),
+		       uim_scm_make_symbol(gate_func),
+		       uim_scm_make_func_ptr(cb));
+  form = uim_scm_cons(uim_scm_make_symbol(validator), form);
+  form = uim_scm_cons(uim_scm_make_symbol("custom-register-cb"), form);
+  succeeded = uim_scm_c_bool(uim_scm_eval(form));
+  uim_scm_gc_unprotect_stack(&stack_start);
+
+  return succeeded;
+}
+
+static uim_bool
+custom_cb_remove(const char *key_sym, const char *hook)
+{
+  uim_bool removed;
+
+  UIM_EVAL_FSTRING2(NULL, "(custom-remove-hook '%s '%s)",
+		    (key_sym) ? key_sym : "#f", hook);
+  removed = uim_scm_c_bool(uim_scm_return_value());
+
+  return removed;
+}
+
 /**
  * Set a callback function in a custom variable. The @a update_cb is called
  * back when the custom variable specified by @a custom_sym is
@@ -1202,20 +1258,9 @@
 uim_custom_cb_add(const char *custom_sym, void *ptr,
 		  void (*update_cb)(void *ptr, const char *custom_sym))
 {
-  uim_bool succeeded;
-  uim_lisp stack_start;
-  uim_lisp form;
-
-  uim_scm_gc_protect_stack(&stack_start);
-  form = uim_scm_list5(uim_scm_make_symbol("custom-register-update-cb"),
-		       uim_scm_make_symbol(custom_sym),
-		       uim_scm_make_ptr(ptr),
-		       uim_scm_make_symbol("custom-update-cb-gate"),
-		       uim_scm_make_func_ptr((void (*)(void))update_cb));
-  succeeded = uim_scm_c_bool(uim_scm_eval(form));
-  uim_scm_gc_unprotect_stack(&stack_start);
-
-  return succeeded;
+  return custom_cb_add("custom-update-hooks", "custom-rec",
+		       custom_sym, ptr,
+		       "custom-update-cb-gate", (void (*)(void))update_cb);
 }
 
 /**
@@ -1229,11 +1274,74 @@
 uim_bool
 uim_custom_cb_remove(const char *custom_sym)
 {
-  uim_bool removed;
+  return custom_cb_remove(custom_sym, "custom-update-hooks");
+}
 
-  UIM_EVAL_FSTRING1(NULL, "(custom-remove-hook '%s 'custom-update-hooks)",
-		    (custom_sym) ? custom_sym : "#f");
-  removed = uim_scm_c_bool(uim_scm_return_value());
+/**
+ * Set a callback function in a custom group. The @a update_cb is
+ * called back when the custom group specified by @a group_sym is
+ * updated (i.e. new custom variable has been defined in the group or
+ * a custom variable is removed from the group). Multiple callbacks
+ * for one custom variable is allowed.
+ *
+ * @retval UIM_TRUE succeeded
+ * @retval UIM_FALSE failed
+ * @param group_sym custom group name
+ * @param ptr an opaque value passed back to client at callback
+ * @param update_cb function pointer called back when the custom group is
+ *        updated
+ */
+uim_bool
+uim_custom_group_cb_add(const char *group_sym, void *ptr,
+			void (*update_cb)(void *ptr, const char *group_sym))
+{
+  return custom_cb_add("custom-group-update-hooks", "custom-group-rec",
+		       group_sym, ptr,
+		       "custom-update-cb-gate", (void (*)(void))update_cb);
+}
 
-  return removed;
+/**
+ * Remove the callback functions in a custom group. All functions set for @a
+ * group_sym will be removed.
+ *
+ * @retval UIM_TRUE some functions are removed
+ * @retval UIM_FALSE no functions are removed
+ * @param group_sym custom group name. NULL instructs 'all callbacks'
+ */
+uim_bool
+uim_custom_group_cb_remove(const char *group_sym)
+{
+  return custom_cb_remove(group_sym, "custom-group-update-hooks");
 }
+
+/**
+ * Set a callback function for global events. The @a
+ * group_list_update_cb is called back when group list is updated
+ * (i.e. new custom group has been defined or a custom group is
+ * undefined). Multiple callbacks is allowed.
+ *
+ * @retval UIM_TRUE succeeded
+ * @retval UIM_FALSE failed
+ * @param ptr an opaque value passed back to client at callback
+ * @param group_list_update_cb function pointer called back when
+ *        custom group list is updated
+ */
+uim_bool
+uim_custom_global_cb_add(void *ptr, void (*group_list_update_cb)(void *ptr))
+{
+  return custom_cb_add("custom-group-update-hooks", "(lambda (dummy) #t)",
+		       "global", ptr,
+		       "custom-global-update-cb-gate",
+		       (void (*)(void))group_list_update_cb);
+}
+
+/**
+ * Remove all callback functions for global events.
+ *
+ * @retval UIM_TRUE some functions are removed
+ * @retval UIM_FALSE no functions are removed
+ */
+uim_bool uim_custom_global_cb_remove(void)
+{
+  return custom_cb_remove("global", "custom-group-list-update-hooks");
+}

Modified: trunk/uim/uim-custom.h
===================================================================
--- trunk/uim/uim-custom.h	2005-01-13 10:29:50 UTC (rev 275)
+++ trunk/uim/uim-custom.h	2005-01-13 14:21:56 UTC (rev 276)
@@ -138,6 +138,12 @@
 uim_bool uim_custom_cb_add(const char *custom_sym, void *ptr,
 			   void (*update_cb)(void *ptr, const char *custom_sym));
 uim_bool uim_custom_cb_remove(const char *custom_sym);
+uim_bool uim_custom_group_cb_add(const char *group_sym, void *ptr,
+				 void (*update_cb)(void *ptr, const char *group_sym));
+uim_bool uim_custom_group_cb_remove(const char *group_sym);
+uim_bool uim_custom_global_cb_add(void *ptr,
+				  void (*group_list_update_cb)(void *ptr));
+uim_bool uim_custom_global_cb_remove(void);
 
 /* literalization */
 char *uim_custom_value_as_literal(const char *custom_sym);



More information about the Uim-commit mailing list