]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
hack on this some
[~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 #ifdef HAVE_DIRENT_H
26 #include <dirent.h>
27 #endif
28 #include <gmodule.h>
29
30 #include <errno.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <stdio.h>
36
37 #ifdef G_OS_WIN32
38 #define SOEXT ".dll"
39 #else
40 #define SOEXT ".so"
41 #endif
42
43 #include <pango/pango-utils.h>
44 #include "gtk/gtkrc.h"
45 #include "gtk/gtkimmodule.h"
46
47 void
48 print_escaped (const char *str)
49 {
50   char *tmp = g_strescape (str, NULL);
51   printf ("\"%s\" ", tmp);
52   g_free (tmp);
53 }
54
55 gboolean
56 query_module (const char *dir, const char *name)
57 {
58   void          (*list)   (const GtkIMContextInfo ***contexts,
59                            guint                    *n_contexts);
60   void          (*init)   (GTypeModule              *type_module);
61   void          (*exit)   (void);
62   GtkIMContext *(*create) (const gchar             *context_id);
63
64   GModule *module;
65   gchar *path;
66   gboolean error = FALSE;
67
68   if (name[0] == G_DIR_SEPARATOR)
69     path = g_strdup (name);
70   else
71     path = g_strconcat (dir, G_DIR_SEPARATOR_S, name, NULL);
72   
73   module = g_module_open (path, 0);
74
75   if (!module)
76     {
77       fprintf(stderr, "Cannot load module %s: %s\n", path, g_module_error());
78       error = TRUE;
79     }
80           
81   if (module &&
82       g_module_symbol (module, "im_module_list", (gpointer)&list) &&
83       g_module_symbol (module, "im_module_init", (gpointer)&init) &&
84       g_module_symbol (module, "im_module_exit", (gpointer)&exit) &&
85       g_module_symbol (module, "im_module_create", (gpointer)&create))
86     {
87       const GtkIMContextInfo **contexts;
88       guint n_contexts;
89       int i;
90
91       print_escaped (path);
92       fputs ("\n", stdout);
93
94       (*list) (&contexts, &n_contexts);
95
96       for (i=0; i<n_contexts; i++)
97         {
98           print_escaped (contexts[i]->context_id);
99           print_escaped (contexts[i]->context_name);
100           print_escaped (contexts[i]->domain);
101           print_escaped (contexts[i]->domain_dirname);
102           print_escaped (contexts[i]->default_locales);
103           fputs ("\n", stdout);
104         }
105       fputs ("\n", stdout);
106     }
107   else
108     {
109       fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
110                g_module_error());
111       error = TRUE;
112     }
113
114   g_free (path);
115   if (module)
116     g_module_close (module);
117
118   return error;
119 }                      
120
121 int main (int argc, char **argv)
122 {
123   char cwd[PATH_MAX];
124   int i;
125   char *path;
126   gboolean error = FALSE;
127
128   printf ("# GTK+ Input Method Modules file\n"
129           "# Automatically generated file, do not edit\n"
130           "#\n");
131
132   if (argc == 1)                /* No arguments given */
133     {
134       char **dirs;
135       int i;
136
137       path = gtk_rc_get_im_module_path ();
138
139       printf ("# ModulesPath = %s\n#\n", path);
140
141       dirs = pango_split_file_list (path);
142
143       for (i=0; dirs[i]; i++)
144         {
145           DIR *dir = opendir (dirs[i]);
146           if (dir)
147             {
148               struct dirent *dent;
149
150               while ((dent = readdir (dir)))
151                 {
152                   int len = strlen (dent->d_name);
153                   if (len > 3 && strcmp (dent->d_name + len - strlen (SOEXT), SOEXT) == 0)
154                     error |= query_module (dirs[i], dent->d_name);
155                 }
156               
157               closedir (dir);
158             }
159         }
160     }
161   else
162     {
163       getcwd (cwd, PATH_MAX);
164       
165       for (i=1; i<argc; i++)
166         error |= query_module (cwd, argv[i]);
167     }
168   
169   return error ? 1 : 0;
170 }