Plugin-loader handles .la files
[projects/chimara/chimara.git] / tests / plugin-loader.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * main.c
4  * Copyright (C) Philip en Marijn 2008 <>
5  * 
6  * main.c is free software copyrighted by Philip en Marijn.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name ``Philip en Marijn'' nor the name of any other
17  *    contributor may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  * 
20  * plugin-loader.c IS PROVIDED BY Philip en Marijn ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Philip en Marijn OR ANY OTHER CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include <glib.h>
40 #include <glib/gi18n.h>
41 #include <gtk/gtk.h>
42
43 #include <config.h>
44 #include <libchimara/chimara-glk.h>
45
46 /* Global pointers to widgets */
47 GtkWidget *window = NULL;
48 GtkWidget *glk = NULL;
49
50 static gboolean
51 quit()
52 {
53     gtk_main_quit();
54     return TRUE;
55 }
56
57 static void
58 create_window(void)
59 {
60     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
61     gtk_widget_set_size_request(window, 400, 400);
62     g_signal_connect(window, "delete-event", G_CALLBACK(quit), NULL);
63         glk = chimara_glk_new();
64         g_object_ref(glk);
65         gtk_container_add(GTK_CONTAINER(window), glk);
66 }
67
68 static gchar *
69 resource_load(ChimaraResourceType usage, guint32 resnum)
70 {
71         char *resstr;
72         if(usage == CHIMARA_RESOURCE_IMAGE)
73                 resstr = "PIC";
74         else if(usage == CHIMARA_RESOURCE_SOUND)
75                 resstr = "SND";
76         else
77                 resstr = "FCK";
78         return g_strdup_printf("%s%d", resstr, resnum);
79 }
80
81 static GFile *
82 libname_from_la_file(char *la_filename)
83 {
84         GFile *la_file = g_file_new_for_commandline_arg(la_filename);
85         GFile *parentdir = g_file_get_parent(la_file);
86         GFile *objdir = g_file_get_child(parentdir, LT_OBJDIR);
87         g_object_unref(parentdir);
88
89         GFileInputStream *istream = g_file_read(la_file, NULL, NULL);
90         if(istream == NULL)
91                 return NULL;
92         GDataInputStream *stream = g_data_input_stream_new( G_INPUT_STREAM(istream) );
93
94         char *line;
95         char *dlname = NULL;
96         while( (line = g_data_input_stream_read_line(stream, NULL, NULL, NULL)) ) {
97                 if( g_str_has_prefix(line, "dlname=") ) {
98                         dlname = g_strdup( line + strlen("dlname='") );
99                         *(strrchr(dlname, '\'')) = '\0';
100                         g_free(line);
101                         break;
102                 }
103                 g_free(line);
104         }
105         if(dlname == NULL)
106                 return NULL;
107
108         g_input_stream_close(G_INPUT_STREAM (stream), NULL, NULL);
109
110         GFile *libfile = g_file_get_child(objdir, dlname);
111         g_free(dlname);
112
113         g_object_unref(la_file);
114         g_object_unref(objdir);
115         return libfile;
116 }
117
118 int
119 main(int argc, char *argv[])
120 {
121         GError *error = NULL;
122
123 #ifdef ENABLE_NLS
124         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
125         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
126         textdomain(GETTEXT_PACKAGE);
127 #endif
128
129         gdk_threads_init();
130         gtk_init(&argc, &argv);
131
132         create_window();
133         gtk_widget_show_all(window);
134
135         if(argc < 2)
136                 g_error("Must provide a plugin\n");
137
138     GFile *plugin_file;
139     if( g_str_has_suffix(argv[1], ".la") )
140         plugin_file = libname_from_la_file(argv[1]);
141     else
142         plugin_file = g_file_new_for_commandline_arg(argv[1]);
143
144         chimara_glk_set_resource_load_callback(CHIMARA_GLK(glk), (ChimaraResourceLoadFunc)resource_load, NULL, NULL);
145
146     if( !chimara_glk_run_file(CHIMARA_GLK(glk), plugin_file,
147         argc - 1, argv + 1, &error) )
148                 g_error("Error starting Glk library: %s\n", error->message);
149     g_object_unref(plugin_file);
150
151     gdk_threads_enter();
152         gtk_main();
153         gdk_threads_leave();
154
155         chimara_glk_stop(CHIMARA_GLK(glk));
156         chimara_glk_wait(CHIMARA_GLK(glk));
157         g_object_unref(glk);
158
159         return 0;
160 }