Author: omote.masahito
Date: Thu Apr 17 05:30:06 2008
New Revision: 5424

Modified:
  trunk/doc/COMPATIBILITY
  trunk/scm/plugin.scm
  trunk/uim/plugin.c
  trunk/uim/uim-custom.c

Log:
* New API for loding/unloading modules rewritten in C and Scheme. module-load
and module-unload are the same ones as load-plugin/unload-plugin written in
 Scheme. module-bind/module-unbind are written in C for the purpose of
 binding/unbinding library pointer bounded by dlopen and C function pointers.
 Using module-load/module-unbind are encouraged when you want to load/unload
 modules from Scheme code.
* uim/plugin.c: Most of the load/unload-plugin are delegated to Scheme.
 (module_bind): New function for the replacement for load-plugin.
 (module_unbind): New function for the replacement for unload-plugin.
* scm/plugin.scm
 (module-load): New function for the replacement for load-plugin.
 (module-unload): New function for the replacement for unload-plugin.
* doc/COMPATIBILITY
 - Describe about this commit.
* uim/uim-custom.c: use module-load instead of load-plugin.


Modified: trunk/doc/COMPATIBILITY
==============================================================================
--- trunk/doc/COMPATIBILITY     (original)
+++ trunk/doc/COMPATIBILITY     Thu Apr 17 05:30:06 2008
@@ -57,6 +57,29 @@

The changes are described below in most recently updated order.
------------------------------------------------------------------------------
+Summary: Plugin loding and unloading schemes are changed
+Affects: uim developers, IM developers
+Updates: libuim ABI, Scheme API
+Version: 1.6.0
+Revision: ac5424
+Date: 2008-04-17
+Modifier: Masahito Omote
+Related:
+URL:
+Changes:
+     (added) module-load
+     (added) module-unload
+     (added) module-bind
+     (added) module-unbind
+Description:
+  New API for loding/unloading modules rewritten in C and Scheme. module-load
+ and module-unload are the same ones as load-plugin/unload-plugin written in
+  Scheme. module-bind/module-unbind are written in C for the purpose of
+  binding/unbinding library pointer bounded by dlopen and C function pointers.
+  Using module-load/module-unbind are encouraged when you want to load/unload
+  modules from Scheme code.
+
+------------------------------------------------------------------------------
Summary: Feature provision of 'uim'
Affects: uim developers, IM developers
Updates:

Modified: trunk/scm/plugin.scm
==============================================================================
--- trunk/scm/plugin.scm        (original)
+++ trunk/scm/plugin.scm        Thu Apr 17 05:30:06 2008
@@ -120,7 +120,7 @@
(define require-module
  (lambda (module-name)
    (set! currently-loading-module-name module-name)
-    (let ((succeeded (or (load-plugin module-name)
+    (let ((succeeded (or (module-load module-name)
                         (try-require (string-append module-name ".scm")))))
      (set! currently-loading-module-name #f)
      succeeded)))
@@ -168,3 +168,57 @@
                    user-file
                    (try-load user-file))
               #t)))))
+
+(define find-module-lib-path
+  (lambda (paths module-name)
+    (let ((path ()))
+      (cond ((null? paths) #f)
+           ((not (string? (car paths))) #f)
+           ((file-readable? (string-append (car paths)
+                                            "/libuim-"
+                                            module-name
+                                            ".so"))
+            (string-append (car paths) "/libuim-" module-name ".so"))
+           (else
+            (find-module-lib-path (cdr paths) module-name))))))
+
+(define find-module-scm-path
+  (lambda (paths module-name)
+    (let ((path ()))
+      (cond ((null? paths) #f)
+           ((not (string? (car paths))) #f)
+           ((file-readable? (string-append (car paths)
+                                            "/"
+                                            module-name
+                                            ".scm"))
+            (string-append (car paths) "/" module-name ".scm"))
+           (else
+            (find-module-scm-path (cdr paths) module-name))))))
+
+(define module-load
+  (lambda (module-name)
+ (and-let* ((lib-path (find-module-lib-path uim-plugin-lib-load-path module-name))
+              (scm-path (find-module-scm-path uim-plugin-scm-load-path 
module-name))
+              (proc-ptrs (module-bind lib-path))
+              (library-ptr (car proc-ptrs))
+              (init-proc (car (cdr proc-ptrs)))
+              (quit-proc (car (cdr (cdr proc-ptrs)))))
+             (if (not (and (null? proc-ptrs)
+                           (null? init-proc)
+                           (null? quit-proc)))
+                 (begin
+                   (try-require scm-path)
+                   (plugin-list-append module-name
+                                       library-ptr
+                                       init-proc
+                                       quit-proc)
+                   #t)
+                 #f))))
+
+(define module-unload
+  (lambda (module-name)
+    (and-let* ((library-ptr (plugin-list-query-library module-name))
+              (init-proc (plugin-list-query-instance-init module-name))
+              (quit-proc (plugin-list-query-instance-quit module-name)))
+             (module-unbind library-ptr init-proc quit-proc)
+             (plugin-list-delete module-name) #t)))

Modified: trunk/uim/plugin.c
==============================================================================
--- trunk/uim/plugin.c  (original)
+++ trunk/uim/plugin.c  Thu Apr 17 05:30:06 2008
@@ -43,6 +43,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/param.h>
#include <fcntl.h>
#include <errno.h>
#include <pwd.h>
@@ -82,6 +83,8 @@
static void *plugin_unload_internal(void *uim_lisp_name);
static void *uim_quit_plugin_internal(void *dummy);

+static uim_lisp module_unbind(uim_lisp, uim_lisp, uim_lisp);
+static uim_lisp module_bind(uim_lisp);

static long
verbose_level(void)
@@ -245,6 +248,8 @@

  uim_scm_init_proc1("load-plugin", plugin_load);
  uim_scm_init_proc1("unload-plugin", plugin_unload);
+  uim_scm_init_proc1("module-bind", module_bind);
+  uim_scm_init_proc3("module-unbind", module_unbind);

  UIM_CATCH_ERROR_END();
}
@@ -275,4 +280,53 @@
  }

  return NULL;
+}
+
+static uim_lisp
+module_unbind(uim_lisp lib_ptr,
+             uim_lisp init_proc,
+             uim_lisp quit_proc)
+{
+  void *library;
+  void (*plugin_instance_quit)(void);
+
+  library = C_PTR(lib_ptr);
+  plugin_instance_quit = C_FPTR(quit_proc);
+
+  (plugin_instance_quit)();
+  dlclose(library);
+
+  return uim_scm_t();
+}
+
+static uim_lisp
+module_bind(uim_lisp name)
+{
+  void *library;
+  void (*plugin_instance_init)(void);
+  void (*plugin_instance_quit)(void);
+
+  DPRINTFN(UIM_VLEVEL_PLUGIN, (stderr, "Loading %s", REFER_C_STR(name)));
+  library = dlopen(REFER_C_STR(name), RTLD_NOW);
+
+  if (library == NULL) {
+    uim_notify_fatal(N_("module: %s: Load failed."), dlerror());
+    return uim_scm_f();
+  }
+
+  plugin_instance_init
+    = (void (*)(void))dlfunc(library, "uim_plugin_instance_init");
+  plugin_instance_quit
+    = (void (*)(void))dlfunc(library, "uim_plugin_instance_quit");
+  if (!plugin_instance_init) {
+    uim_notify_fatal(N_("module: %s: Initialize failed."), REFER_C_STR(name));
+    return uim_scm_f();
+  }
+       
+ DPRINTFN(UIM_VLEVEL_PLUGIN, (stderr, "Calling plugin_instance_init() for %s.\n", REFER_C_STR(name)));
+  (plugin_instance_init)();
+
+  return LIST3(MAKE_PTR(library),
+              MAKE_FPTR(plugin_instance_init),
+              MAKE_FPTR(plugin_instance_quit));
}

Modified: trunk/uim/uim-custom.c
==============================================================================
--- trunk/uim/uim-custom.c      (original)
+++ trunk/uim/uim-custom.c      Thu Apr 17 05:30:06 2008
@@ -967,7 +967,7 @@
uim_bool
uim_custom_enable(void)
{
-  UIM_EVAL_STRING(NULL, "(load-plugin \"custom-enabler\")");
+  UIM_EVAL_STRING(NULL, "(module-load \"custom-enabler\")");
  return uim_scm_c_bool(uim_scm_return_value());
}

Reply via email to