CrGlCur.c revision 61b2299d
1/* $Xorg: CrGlCur.c,v 1.4 2001/02/09 02:03:32 xorgcvs Exp $ */
2/*
3
4Copyright 1986, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/lib/X11/CrGlCur.c,v 1.7 2003/04/13 19:22:15 dawes Exp $ */
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include "Xlibint.h"
33
34#ifdef USE_DYNAMIC_XCURSOR
35
36#ifdef __UNIXOS2__
37#define RTLD_LAZY 1
38#define LIBXCURSOR "Xcursor.dll"
39#endif
40#include <stdio.h>
41#include <string.h>
42#if defined(hpux)
43#include <dl.h>
44#else
45#include <dlfcn.h>
46#endif
47#include "Cr.h"
48
49#if defined(hpux)
50typedef shl_dt	XModuleType;
51#else
52typedef void *XModuleType;
53#endif
54
55#ifndef LIBXCURSOR
56#define LIBXCURSOR "libXcursor.so.1"
57#endif
58
59static char libraryName[] = LIBXCURSOR;
60
61static XModuleType
62open_library (void)
63{
64    char	*library = libraryName;
65    char	*dot;
66    XModuleType	module;
67    for (;;)
68    {
69#if defined(hpux)
70	module = shl_load(library, BIND_DEFERRED, 0L);
71#else
72	module =  dlopen(library, RTLD_LAZY);
73#endif
74	if (module)
75	    return module;
76	dot = strrchr (library, '.');
77	if (!dot)
78	    break;
79	*dot = '\0';
80    }
81    return NULL;
82}
83
84static void *
85fetch_symbol (XModuleType module, const char *under_symbol)
86{
87    void *result = NULL;
88    const char *symbol = under_symbol + 1;
89#if defined(hpux)
90    int getsyms_cnt, i;
91    struct shl_symbol *symbols;
92
93    getsyms_cnt = shl_getsymbols(module, TYPE_PROCEDURE,
94				 EXPORT_SYMBOLS, malloc, &symbols);
95
96    for(i=0; i<getsyms_cnt; i++) {
97        if(!strcmp(symbols[i].name, symbol)) {
98	    result = symbols[i].value;
99	    break;
100         }
101    }
102
103    if(getsyms_cnt > 0) {
104        free(symbols);
105    }
106#else
107    result = dlsym (module, symbol);
108    if (!result)
109	result = dlsym (module, under_symbol);
110#endif
111    return result;
112}
113
114typedef void	(*NoticeCreateBitmapFunc) (Display	    *dpy,
115					   Pixmap	    pid,
116					   unsigned int width,
117					   unsigned int height);
118
119typedef void	(*NoticePutBitmapFunc) (Display	    *dpy,
120					Drawable    draw,
121					XImage	    *image);
122
123typedef Cursor	(*TryShapeBitmapCursorFunc) (Display	    *dpy,
124					     Pixmap	    source,
125					     Pixmap	    mask,
126					     XColor	    *foreground,
127					     XColor	    *background,
128					     unsigned int   x,
129					     unsigned int   y);
130
131typedef Cursor	(*TryShapeCursorFunc) (Display	    *dpy,
132				       Font	    source_font,
133				       Font	    mask_font,
134				       unsigned int source_char,
135				       unsigned int mask_char,
136				       XColor _Xconst *foreground,
137				       XColor _Xconst *background);
138
139static XModuleType  _XcursorModule;
140static Bool	    _XcursorModuleTried;
141
142#define GetFunc(type,name,ret) {\
143    static Bool	    been_here; \
144    static type	    staticFunc; \
145     \
146    _XLockMutex (_Xglobal_lock); \
147    if (!been_here) \
148    { \
149	been_here = True; \
150	if (!_XcursorModuleTried) \
151	{ \
152	    _XcursorModuleTried = True; \
153	    _XcursorModule = open_library (); \
154	} \
155	if (_XcursorModule) \
156	    staticFunc = (type) fetch_symbol (_XcursorModule, "_" name); \
157    } \
158    ret = staticFunc; \
159    _XUnlockMutex (_Xglobal_lock); \
160}
161
162static Cursor
163_XTryShapeCursor (Display	    *dpy,
164		  Font		    source_font,
165		  Font		    mask_font,
166		  unsigned int	    source_char,
167		  unsigned int	    mask_char,
168		  XColor _Xconst    *foreground,
169		  XColor _Xconst    *background)
170{
171    TryShapeCursorFunc		func;
172
173    GetFunc (TryShapeCursorFunc, "XcursorTryShapeCursor", func);
174    if (func)
175	return (*func) (dpy, source_font, mask_font, source_char, mask_char,
176			foreground, background);
177    return None;
178}
179
180void
181_XNoticeCreateBitmap (Display	    *dpy,
182		      Pixmap	    pid,
183		      unsigned int  width,
184		      unsigned int  height)
185{
186    NoticeCreateBitmapFunc  func;
187
188    GetFunc (NoticeCreateBitmapFunc, "XcursorNoticeCreateBitmap", func);
189    if (func)
190	(*func) (dpy, pid, width, height);
191}
192
193void
194_XNoticePutBitmap (Display	*dpy,
195		   Drawable	draw,
196		   XImage	*image)
197{
198    NoticePutBitmapFunc	func;
199
200    GetFunc (NoticePutBitmapFunc, "XcursorNoticePutBitmap", func);
201    if (func)
202	(*func) (dpy, draw, image);
203}
204
205Cursor
206_XTryShapeBitmapCursor (Display		*dpy,
207			Pixmap		source,
208			Pixmap		mask,
209			XColor		*foreground,
210			XColor		*background,
211			unsigned int	x,
212			unsigned int	y)
213{
214    TryShapeBitmapCursorFunc	func;
215
216    GetFunc (TryShapeBitmapCursorFunc, "XcursorTryShapeBitmapCursor", func);
217    if (func)
218	return (*func) (dpy, source, mask, foreground, background, x, y);
219    return None;
220}
221#endif
222
223Cursor XCreateGlyphCursor(
224     register Display *dpy,
225     Font source_font,
226     Font mask_font,
227     unsigned int source_char,
228     unsigned int mask_char,
229     XColor _Xconst *foreground,
230     XColor _Xconst *background)
231{
232    Cursor cid;
233    register xCreateGlyphCursorReq *req;
234
235#ifdef USE_DYNAMIC_XCURSOR
236    cid = _XTryShapeCursor (dpy, source_font, mask_font,
237			    source_char, mask_char, foreground, background);
238    if (cid)
239	return cid;
240#endif
241    LockDisplay(dpy);
242    GetReq(CreateGlyphCursor, req);
243    cid = req->cid = XAllocID(dpy);
244    req->source = source_font;
245    req->mask = mask_font;
246    req->sourceChar = source_char;
247    req->maskChar = mask_char;
248    req->foreRed = foreground->red;
249    req->foreGreen = foreground->green;
250    req->foreBlue = foreground->blue;
251    req->backRed = background->red;
252    req->backGreen = background->green;
253    req->backBlue = background->blue;
254    UnlockDisplay(dpy);
255    SyncHandle();
256    return (cid);
257}
258
259