dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Other / XpmLib.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: XpmLib.cc /main/3 1996/06/11 16:30:30 cde-hal $
24 /*      Copyright (c) 1994,1995 FUJITSU LIMITED         */
25 /*      All Rights Reserved                             */
26
27 #include <unistd.h>
28
29 #define C_XpmLib
30 #define C_WindowSystem
31 #define L_Other
32
33 #include <assert.h>
34 #include <stdlib.h>
35
36 #include <Prelude.h>
37
38 static XpmLib g_xpmlib;
39
40 #define XPMLIB_PREFIX           "libXpm.so"
41 #define XPMLIB_DEFAULT_PATH     "libXpm.so.4.1"
42
43 static char* default_path = XPMLIB_DEFAULT_PATH;
44
45 XpmLib* XpmLib::f_xpm_lib;
46
47 XpmLib::XpmLib(const char* arg_path, int open_mode) : f_handle(0)
48 {
49         const char* path = 0;
50
51         f_xpm_lib = this;
52
53         if (arg_path)
54             path = arg_path;
55
56         if (! path || *path == '\0')
57             path = default_path;
58
59         if (! (f_handle = dlopen(path, open_mode)))
60             f_handle = dlopen(XPMLIB_PREFIX, open_mode);
61 }
62
63 XpmLib::~XpmLib()
64 {
65         if (f_handle)
66             dlclose(f_handle);
67 }
68
69 void* XpmLib::symbol(const char* name)
70 {
71         if (! f_handle || ! name || *name == '\0')
72             return 0;
73
74         return dlsym(f_handle, name);
75 }
76
77 int XpmLib::CreatePixmapFromData(Display *display, Drawable d, char **data,
78                                 Pixmap *pixmap_return, Pixmap *shapemask_return,
79                                 XpmAttributes *attributes)
80 {
81     int status;
82
83     int (*func)(Display*, Drawable, char**, Pixmap*, Pixmap*, XpmAttributes*);
84
85     func = (int (*)(Display*, Drawable, char**, Pixmap*, Pixmap*, XpmAttributes*)) symbol("XpmCreatePixmapFromData");
86
87     if (func) {
88         status = (*func)(display, d, data, pixmap_return,
89                                 shapemask_return, attributes);
90         return status;
91     }
92     else
93         return 1;
94 }
95
96 int XpmLib::CreatePixmapFromBuffer(Display *display, Drawable d, char *buffer,
97                         Pixmap *pixmap_return, Pixmap *shapemask_return,
98                         XpmAttributes *attributes)
99 {
100     int status;
101
102     int (*func)(Display*, Drawable, char*, Pixmap*, Pixmap*, XpmAttributes*);
103
104     func = (int (*)(Display*, Drawable, char*, Pixmap*, Pixmap*, XpmAttributes*)) symbol("XpmCreatePixmapFromBuffer");
105
106     if (func) {
107         status = (*func)(display, d, buffer, pixmap_return,
108                                 shapemask_return, attributes);
109         return status;
110     }
111     else
112         return 1;
113 }
114
115 int XpmLib::ReadFileToPixmap(Display *display, Drawable d, char *filename,
116                 Pixmap *pixmap_return, Pixmap *shapemask_return,
117                 XpmAttributes *attributes)
118 {
119     int status;
120
121     int (*func)(Display*, Drawable, char*, Pixmap*, Pixmap*, XpmAttributes*);
122
123     func = (int (*)(Display*, Drawable, char*, Pixmap*, Pixmap*, XpmAttributes*)) symbol("XpmReadFileToPixmap");
124
125     if (func) {
126         status = (*func)(display, d, filename, pixmap_return,
127                                 shapemask_return, attributes);
128         return status;
129     }
130     else
131         return 1;
132 }