Back to index

im-sdk  12.3.91
aux_manager_modules.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include "config.h"
00005 #endif
00006 
00007 #include "imbean.h"
00008 
00009 #include <stdio.h>
00010 #include <dlfcn.h>
00011 
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015 
00016 #include "imbean.h"
00017 #include "aux_module.h"
00018 
00019 #define MAX_NUM_AUX_MODULES  32
00020 #define MAX_LEN_CLASS_NAME   56
00021 
00022 #define AUX_MODULES_CONFIG_FILE "aux_modules.xml"
00023 
00024 AuxModuleRec aux_modules[MAX_NUM_AUX_MODULES] = {
00025        { NULL, NULL, NULL, NULL},
00026 };
00027 
00028 int aux_manager_print()
00029 {
00030 #if DEBUG
00031        AuxModule aux_module;
00032 
00033        printf("aux_manger_print ====\n");
00034        for (aux_module = aux_modules; aux_module->class_name != NULL; aux_module ++) { 
00035               printf("classname: %s, aux_module: %p\n", aux_module->class_name, aux_module);
00036        }
00037 
00038        return (AUX_OK);
00039 #endif
00040 }
00041 
00042 
00043 AuxModule aux_manager_get_module_by_classname(const char *classname)
00044 {
00045        AuxModule aux_module;
00046 
00047        DEBUG_printf("aux_manager_get_module_by_classname: classname: %s\n", classname);
00048 
00049        for (aux_module = aux_modules; aux_module->class_name != NULL; aux_module ++) { 
00050               if (!strcasecmp(aux_module->class_name, classname))
00051                      return (aux_module);
00052        }
00053 
00054        return NULL;
00055 }
00056 
00057 char *aux_manager_get_full_file_path(char *file_path)
00058 {
00059        int len;
00060        char *full_file_path = NULL;
00061 
00062        if (!file_path || !*file_path)
00063               return (NULL);
00064 
00065        if (file_path[0] == '/') {
00066               len = strlen (file_path);
00067 
00068               full_file_path = (char *)calloc(len + 1, sizeof(char));
00069               if (full_file_path == NULL) return (NULL);
00070 
00071               snprintf(full_file_path, len + 1, "%s", file_path);
00072        } else {
00073               len = strlen(LE_AUX_MODULES_DIR) + 1 + strlen (file_path);
00074 
00075               full_file_path = (char *)calloc(len + 1, sizeof(char));
00076               if (full_file_path == NULL) return (NULL);
00077 
00078               snprintf(full_file_path, len + 1, "%s/%s", LE_AUX_MODULES_DIR, file_path);
00079        }
00080 
00081        return (full_file_path);
00082 }
00083 
00084 int aux_manager_get_custom_aux_modules(char *aux_modules_config_file)
00085 {
00086 #define AUX_MODULE_CLASS_NAME  "class_name"
00087 #define AUX_MODULE_OBJECT_FILE "object_file"
00088 
00089        AuxModuleRec *p;
00090        int num_aux_modules;
00091        int num_aux_modules_custom;
00092 
00093        IbmlData *ibml_data;
00094        IbmlCategory    *ibml_category;
00095        IbmlElement     *ibml_element;
00096        IbmlProperty    *ibml_property;
00097        int i, j, k, n;
00098 
00099        char *id, *scope;
00100        char *name, *value;
00101 
00102        num_aux_modules = 0;
00103        for (p=aux_modules; p->class_name; p++) num_aux_modules++;
00104 
00105        ibml_data = (IbmlData *)imbean_config_new_from_file(aux_modules_config_file);
00106        if (ibml_data == NULL) return(AUX_FAIL);
00107 
00108        num_aux_modules_custom = 0;
00109        for (k=0; k<ibml_data->num_categories; k++) {
00110               ibml_category = ibml_data->categories[k];
00111 
00112               if (ibml_category->scope == NULL) continue;
00113               if (! *ibml_category->scope) continue;
00114               if (strcasecmp(ibml_category->scope, "gtk2")) continue;
00115 
00116               for (i=0; i<ibml_category->num_elements; i++) {
00117                      CARD16 *utfname_ptr;
00118                      char *aux_module_class_name = NULL;
00119                      char *aux_module_object_file = NULL;
00120                      char *object_file_full_path = NULL;
00121                      void *module_handler = NULL;
00122                      AuxModuleMethodsRec *module_methods = NULL;
00123 
00124                      ibml_element = ibml_category->elements[i];
00125                      if (!ibml_element) continue;
00126 
00127                      id = (char *)ibml_element->id;
00128                      scope = (char *)ibml_element->scope;
00129                      DEBUG_printf("id:%s, scope:%s\n", id?id:"NULL", scope?scope:"NULL");
00130                      if (!id || !*id) continue;
00131 
00132                      for(j=0; j<ibml_element->num_properties; j++) {
00133                             ibml_property = ibml_element->properties[j];
00134                             if (!ibml_property) continue;
00135 
00136                             name = (char *)ibml_property->name;
00137                             if (!name || !*name)
00138                                    continue;
00139 
00140                             value = (char *)ibml_property->value;
00141                             if (!value || !*value)
00142                                    continue;
00143 
00144                             if (!strcasecmp(name, AUX_MODULE_CLASS_NAME)) {
00145                                    aux_module_class_name = value;
00146                             } else if (!strcasecmp(name, AUX_MODULE_OBJECT_FILE)) {
00147                                    aux_module_object_file = value;
00148                             }
00149                      }
00150 
00151                      if (!aux_module_class_name || !*aux_module_class_name)
00152                             continue;
00153                      if (!aux_module_object_file || !*aux_module_object_file)
00154                             continue;
00155 
00156                      if (num_aux_modules + num_aux_modules_custom >= MAX_NUM_AUX_MODULES-1)
00157                             break;
00158 
00159                      object_file_full_path = aux_manager_get_full_file_path(aux_module_object_file);
00160                      if (!object_file_full_path || !*object_file_full_path)
00161                             continue;
00162 
00163                      module_handler = (void *)dlopen (object_file_full_path, RTLD_LAZY);
00164                      DEBUG_printf("object_file_full_path: %s, module_handler: 0x%x\n",
00165                             object_file_full_path, module_handler);
00166 
00167                      if (module_handler == NULL) {
00168                             fprintf(stderr, "Error: could not open module file: %s\n",
00169                                    object_file_full_path);
00170                             free ((char *)object_file_full_path);
00171                             continue;
00172                      }
00173 
00174                      module_methods = (AuxModuleMethodsRec *) dlsym(module_handler, "aux_module_methods");
00175                      if (module_methods == NULL) {
00176                             fprintf(stderr, "Error: could not dlsym AuxModuleMethodsRec: %s\n", object_file_full_path);
00177                             free ((char *)object_file_full_path);
00178                             dlclose(module_handler);
00179                             continue;
00180                      }
00181 
00182                      free ((char *)object_file_full_path);
00183 
00184                      p = &(aux_modules[num_aux_modules + num_aux_modules_custom]);
00185                      p->file_name = (char *)strdup(aux_module_object_file);
00186                      p->handler   = module_handler;
00187                      p->class_name = (char *)strdup(aux_module_class_name);
00188                      p->methods   = module_methods;
00189 
00190                      num_aux_modules_custom ++;
00191               }
00192        }
00193 
00194        p = &(aux_modules[num_aux_modules + num_aux_modules_custom]);
00195        p->file_name = NULL;
00196        p->handler   = NULL;
00197        p->class_name = NULL;
00198        p->methods   = NULL;
00199 
00200        ibml_data_free(ibml_data);
00201        return (AUX_OK);
00202 }
00203 
00204 void aux_manager_init_aux_modules()
00205 {
00206        char *aux_modules_config_file = NULL;
00207 
00208        aux_modules_config_file =  aux_manager_get_full_file_path(AUX_MODULES_CONFIG_FILE);
00209 
00210        if (aux_modules_config_file) {
00211               DEBUG_printf("aux_modules_config_file: %s\n", aux_modules_config_file);
00212               aux_manager_get_custom_aux_modules(aux_modules_config_file);
00213               free ((char *)aux_modules_config_file);
00214        }
00215 
00216        aux_manager_print();
00217 
00218        return;
00219 }
00220 
00221 void aux_manager_register_aux_modules(Display *display, Window window)
00222 {
00223        AuxModule aux_module;
00224        Atom atom_aux_module;
00225 
00226        DEBUG_printf("aux_manager_register_aux_modules \n");
00227        for (aux_module = aux_modules; aux_module->class_name != NULL; aux_module ++) { 
00228               DEBUG_printf("classname: %s\n", aux_module->class_name);
00229               atom_aux_module = XInternAtom(display, aux_module->class_name, False);
00230               if (atom_aux_module == (Atom)None)
00231                      continue;
00232               XSetSelectionOwner(display, atom_aux_module, window, CurrentTime);
00233        }
00234 
00235        return;
00236 }
00237 
00238 int aux_manager_destroy()
00239 {
00240        AuxModule aux_module;
00241 
00242        for (aux_module = aux_modules; aux_module->handler != NULL; aux_module ++) { 
00243               if (aux_module->file_name) free ((char *)aux_module->file_name);
00244               if (aux_module->class_name) free ((char *)aux_module->class_name);
00245               if (aux_module->handler) dlclose(aux_module->handler);
00246        }
00247 
00248        return (AUX_OK);
00249 }