~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to libaqsistypes/plugins.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
Import upstream version 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright � 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU 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
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Implements CqPluginBase plugin loader/deloader for textures/riprocedural/shared objects for shaders
 
23
                \author Michel Joron (joron@sympatico.ca)
 
24
*/
 
25
 
 
26
 
 
27
#include <stdio.h>
 
28
#include <string.h>
 
29
 
 
30
 
 
31
 
 
32
#include "aqsis.h"
 
33
 
 
34
#ifdef AQSIS_SYSTEM_WIN32
 
35
#include <Windows.h>                /* LoadLibrary() */
 
36
#endif /* AQSIS_SYSTEM_WIN32 */
 
37
 
 
38
#ifdef AQSIS_SYSTEM_BEOS
 
39
#endif /* AQSIS_SYSTEM_BEOS */
 
40
 
 
41
#ifdef AQSIS_SYSTEM_MACOSX
 
42
extern "C"
 
43
{
 
44
    // For Mac OS X, define MACOSX_NO_LIBDL if libdl not installed
 
45
#ifndef MACOSX_NO_LIBDL
 
46
#include <dlfcn.h>                /* dlopen() */
 
47
#endif
 
48
}
 
49
#endif /* AQSIS_SYSTEM_MACOSX */
 
50
#ifdef AQSIS_SYSTEM_POSIX
 
51
extern "C"
 
52
{
 
53
    // For Mac OS X, define MACOSX_NO_LIBDL if libdl not installed
 
54
#ifndef MACOSX_NO_LIBDL
 
55
#include <dlfcn.h>                /* dlopen() */
 
56
#endif
 
57
}
 
58
#endif /* AQSIS_SYSTEM_POSIX */
 
59
 
 
60
 
 
61
#include "plugins.h"
 
62
 
 
63
 
 
64
START_NAMESPACE( Aqsis )
 
65
 
 
66
 
 
67
//---------------------------------------------------------------------
 
68
/** dlfunc wrappers
 
69
 * These function abstract dlopen,dlsym and dlclose for win32, OS-X, BeOS 
 
70
 * and POSIX etc.
 
71
 */
 
72
void *
 
73
CqPluginBase::DLOpen( CqString *library )
 
74
{
 
75
    void * handle = NULL;
 
76
 
 
77
#ifdef  PLUGINS
 
78
#ifdef AQSIS_SYSTEM_WIN32
 
79
    handle = ( void* ) LoadLibrary( library->c_str() );
 
80
#elif defined (AQSIS_SYSTEM_MACOSX)
 
81
#  ifndef MACOSX_NO_LIBDL
 
82
    handle = ( void * ) dlopen( library->c_str(), RTLD_NOW | RTLD_GLOBAL );
 
83
#  endif
 
84
#elif defined (AQSIS_SYSTEM_BEOS)
 
85
    // We probably need an interface for CFPlugins here
 
86
    // But for now, we will not implement plugins
 
87
#else
 
88
    handle = ( void * ) dlopen( library->c_str(), RTLD_LAZY );
 
89
#endif
 
90
#endif
 
91
    if ( handle ) m_activeHandles.push_back( handle );
 
92
    return handle;
 
93
}
 
94
 
 
95
void *
 
96
CqPluginBase::DLSym( void *handle, CqString *symbol )
 
97
{
 
98
    void * location = NULL;
 
99
 
 
100
#ifdef  PLUGINS
 
101
    if ( handle )
 
102
    {
 
103
 
 
104
#if   defined (AQSIS_SYSTEM_WIN32) //Win32 LoadProc support
 
105
        location = ( void * ) GetProcAddress( ( HINSTANCE ) handle, symbol->c_str() );
 
106
#elif defined (AQSIS_SYSTEM_MACOSX)
 
107
#  ifndef MACOSX_NO_LIBDL
 
108
        location = ( void * ) dlsym( handle, ( CqString( "_" ) + *symbol ).c_str() );
 
109
#  endif
 
110
#elif defined (AQSIS_SYSTEM_BEOS)
 
111
        // We probably need an interface for CFPlugins here
 
112
        // But for now, we will not implement plugins
 
113
#else
 
114
        //this is the same as MacOS-X but we might aswell keep the same seperation for the moment
 
115
        location = ( void * ) dlsym( handle, symbol->c_str() );
 
116
#endif
 
117
 
 
118
    };
 
119
 
 
120
#endif
 
121
    return location;
 
122
}
 
123
 
 
124
void
 
125
CqPluginBase::DLClose( void *handle )
 
126
{
 
127
#ifdef  PLUGINS
 
128
    if ( handle )
 
129
    {
 
130
 
 
131
#if   defined (AQSIS_SYSTEM_WIN32) //Win32 LoadProc support
 
132
        FreeLibrary( ( HINSTANCE ) handle );
 
133
#elif defined (AQSIS_SYSTEM_MACOSX)
 
134
#  ifndef MACOSX_NO_LIBDL
 
135
        dlclose( handle );
 
136
#  endif
 
137
#elif defined (AQSIS_SYSTEM_BEOS)
 
138
        // We probably need an interface for CFPlugins here
 
139
        // But for now, we will not implement plugins
 
140
#else
 
141
        //this is the same as MacOS-X but we might aswell keep the same seperation for the moment
 
142
        dlclose( handle );
 
143
#endif
 
144
 
 
145
    };
 
146
    m_activeHandles.remove( handle );
 
147
#endif
 
148
}
 
149
 
 
150
CqPluginBase::~CqPluginBase()
 
151
{
 
152
    while ( !m_activeHandles.empty() )
 
153
    {
 
154
        if ( m_activeHandles.front() != NULL )
 
155
            DLClose( m_activeHandles.front() );
 
156
    };
 
157
};
 
158
 
 
159
 
 
160
const CqString
 
161
CqPluginBase::DLError( void )
 
162
{
 
163
    CqString errorlog;
 
164
#ifdef  PLUGINS
 
165
#ifdef AQSIS_SYSTEM_WIN32
 
166
    LPVOID lpMsgBuf;
 
167
    FormatMessage(
 
168
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
 
169
        FORMAT_MESSAGE_FROM_SYSTEM |
 
170
        FORMAT_MESSAGE_IGNORE_INSERTS,
 
171
        NULL,
 
172
        GetLastError(),
 
173
        MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),   // Default language
 
174
        ( LPTSTR ) & lpMsgBuf,
 
175
        0,
 
176
        NULL
 
177
    );
 
178
    // Process any inserts in lpMsgBuf.
 
179
    // ...
 
180
    // Display the string.
 
181
    errorlog = ( CqString ) ( LPCTSTR ) lpMsgBuf ;
 
182
 
 
183
    // Free the buffer.
 
184
    LocalFree( lpMsgBuf );
 
185
#else
 
186
    errorlog = dlerror() ;
 
187
#endif
 
188
 
 
189
#else
 
190
    errorlog = "Aqsis was built without plugin support\n";
 
191
#endif
 
192
    return errorlog;
 
193
}
 
194
 
 
195
END_NAMESPACE( Aqsis )
 
196
//---------------------------------------------------------------------
 
197