]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
gtk/gtkmain.c g_module_symbol takes a gpointer *, not just a gpointer.
[~andy/gtk] / gtk / queryimmodules.c
1 /* GTK+
2  * querymodules.c:
3  *
4  * Copyright (C) 2000 Red Hat Software
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <gmodule.h>
26
27 #include <errno.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <stdio.h>
33
34 #ifdef G_OS_WIN32
35 #define SOEXT ".dll"
36 #else
37 #define SOEXT ".so"
38 #endif
39
40 #include <pango/pango-utils.h>
41 #include "gtk/gtkrc.h"
42 #include "gtk/gtkimmodule.h"
43
44 void
45 print_escaped (const char *str)
46 {
47   char *tmp = g_strescape (str, NULL);
48   printf ("\"%s\" ", tmp);
49   g_free (tmp);
50 }
51
52 gboolean
53 query_module (const char *dir, const char *name)
54 {
55   void          (*list)   (const GtkIMContextInfo ***contexts,
56                            guint                    *n_contexts);
57   void          (*init)   (GTypeModule              *type_module);
58   void          (*exit)   (void);
59   GtkIMContext *(*create) (const gchar             *context_id);
60
61   GModule *module;
62   gchar *path;
63   gboolean error = FALSE;
64
65   if (g_path_is_absolute (name))
66     path = g_strdup (name);
67   else
68     path = g_build_filename (dir, name, NULL);
69   
70   module = g_module_open (path, 0);
71
72   if (!module)
73     {
74       fprintf(stderr, "Cannot load module %s: %s\n", path, g_module_error());
75       error = TRUE;
76     }
77           
78   if (module &&
79       g_module_symbol (module, "im_module_list", (gpointer *) &list) &&
80       g_module_symbol (module, "im_module_init", (gpointer *) &init) &&
81       g_module_symbol (module, "im_module_exit", (gpointer *) &exit) &&
82       g_module_symbol (module, "im_module_create", (gpointer *) &create))
83     {
84       const GtkIMContextInfo **contexts;
85       guint n_contexts;
86       int i;
87
88       print_escaped (path);
89       fputs ("\n", stdout);
90
91       (*list) (&contexts, &n_contexts);
92
93       for (i=0; i<n_contexts; i++)
94         {
95           print_escaped (contexts[i]->context_id);
96           print_escaped (contexts[i]->context_name);
97           print_escaped (contexts[i]->domain);
98           print_escaped (contexts[i]->domain_dirname);
99           print_escaped (contexts[i]->default_locales);
100           fputs ("\n", stdout);
101         }
102       fputs ("\n", stdout);
103     }
104   else
105     {
106       fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
107                g_module_error());
108       error = TRUE;
109     }
110
111   g_free (path);
112   if (module)
113     g_module_close (module);
114
115   return error;
116 }                      
117
118 int main (int argc, char **argv)
119 {
120   char *cwd;
121   int i;
122   char *path;
123   gboolean error = FALSE;
124
125   printf ("# GTK+ Input Method Modules file\n"
126           "# Automatically generated file, do not edit\n"
127           "#\n");
128
129   if (argc == 1)                /* No arguments given */
130     {
131       char **dirs;
132       int i;
133
134       path = gtk_rc_get_im_module_path ();
135
136       printf ("# ModulesPath = %s\n#\n", path);
137
138       dirs = pango_split_file_list (path);
139
140       for (i=0; dirs[i]; i++)
141         {
142           GDir *dir = g_dir_open (dirs[i], 0, NULL);
143           if (dir)
144             {
145               const char *dent;
146
147               while ((dent = g_dir_read_name (dir)))
148                 {
149                   int len = strlen (dent);
150                   if (len > 3 && strcmp (dent + len - strlen (SOEXT), SOEXT) == 0)
151                     error |= query_module (dirs[i], dent);
152                 }
153               
154               g_dir_close (dir);
155             }
156         }
157     }
158   else
159     {
160       cwd = g_get_current_dir ();
161       
162       for (i=1; i<argc; i++)
163         error |= query_module (cwd, argv[i]);
164
165       g_free (cwd);
166     }
167   
168   return error ? 1 : 0;
169 }