Squashed commit of 'x11-hostlib' branch
[tangerine.git] / arch / all-x11 / hidd / xshm.c
blob865c9af2ae34df0b508cb5f4fd87889a1f6b1169
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /* I have to put his in its own file because of include file
7 conflicts between AROS includes and system includes */
10 /* NOTE !!! All these functions need to be
11 singlethreded by the LOCK_X11/UNLOCK_X11 macros form the outside
14 #include <X11/Xlib.h>
15 #include <X11/Xutil.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "xshm.h"
19 #include <aros/debug.h>
21 #include <aros/symbolsets.h>
23 /****************************************************************************************/
25 /* stegerg: maybe more safe, even if Unix malloc is used and not AROS malloc */
26 #define NO_MALLOC 1
28 /****************************************************************************************/
30 #if NO_MALLOC
31 #include <exec/memory.h>
32 #include <proto/exec.h>
33 #endif
35 /****************************************************************************************/
37 static void dummy_func()
39 return;
42 /****************************************************************************************/
44 #if USE_XSHM
46 /****************************************************************************************/
48 #include <sys/shm.h>
49 #include <sys/ipc.h>
50 #include <X11/extensions/XShm.h>
52 #include "x11_hostlib.h"
54 #include <proto/hostlib.h>
56 static void *xext_handle = NULL;
57 static void *shm_handle = NULL;
59 static struct {
60 Status (*XShmDetach) ( Display* , XShmSegmentInfo* );
61 Status (*XShmPutImage) ( Display* , Drawable , GC , XImage* , int , int , int , int , unsigned int , unsigned int , Bool );
62 Status (*XShmGetImage) ( Display* , Drawable , XImage* , int , int , unsigned long );
63 XImage * (*XShmCreateImage) ( Display* , Visual* , unsigned int , int , char* , XShmSegmentInfo* , unsigned int , unsigned int );
64 Bool (*XShmQueryVersion) ( Display* , int* , int* , Bool* );
65 Status (*XShmAttach) ( Display* , XShmSegmentInfo* );
66 } xext_func;
68 static const char *xext_func_names[] = {
69 "XShmDetach",
70 "XShmPutImage",
71 "XShmGetImage",
72 "XShmCreateImage",
73 "XShmQueryVersion",
74 "XShmAttach"
77 #define XEXT_SOFILE "libXext.so"
79 #define XEXTCALL(func,...) (xext_func.func(__VA_ARGS__))
81 extern void *x11_hostlib_load_so(const char *, const char **, int, void **);
83 static int xext_hostlib_init(void *libbase) {
84 D(bug("[x11] xext hostlib init\n"));
86 if ((xext_handle = x11_hostlib_load_so(XEXT_SOFILE, xext_func_names, 6, (void **) &xext_func)) == NULL)
87 return FALSE;
89 return TRUE;
92 static int xext_hostlib_expunge(void *libbase) {
93 D(bug("[x11] xext hostlib expunge\n"));
95 if (xext_handle != NULL)
96 HostLib_Close(xext_handle, NULL);
98 return TRUE;
101 ADD2INITLIB(xext_hostlib_init, 1)
102 ADD2EXPUNGELIB(xext_hostlib_expunge, 1)
105 /****************************************************************************************/
107 void *init_shared_mem(Display *display)
109 #warning "Also check if this is a local display"
111 XShmSegmentInfo *shminfo;
112 int xshm_major, xshm_minor;
113 Bool xshm_pixmaps;
115 if (XEXTCALL(XShmQueryVersion, display, &xshm_major, &xshm_minor, &xshm_pixmaps))
117 #if NO_MALLOC
118 shminfo = (XShmSegmentInfo *)AllocVec(sizeof(*shminfo), MEMF_PUBLIC);
119 #else
120 shminfo = (XShmSegmentInfo *)malloc(sizeof(*shminfo));
121 #endif
123 if (NULL != shminfo)
125 key_t key;
128 * Try and get a key for us to use. The idea is to use a
129 * filename that isn't likely to change all that often. This
130 * is made somewhat easier since we must be run from the AROS
131 * root directory (atm). So, I shall choose the path "C",
132 * since the inode number isn't likely to change all that
133 * often.
135 key = CCALL(ftok, "./C", 'A');
136 if(key == -1)
138 kprintf("Hmm, path \"./C\" doesn't seem to exist?\n");
139 key = IPC_PRIVATE;
141 else
143 kprintf("Using shared memory key %d\n", key);
146 memset(shminfo, 0, sizeof (*shminfo));
148 /* Allocate shared memory */
149 shminfo->shmid = CCALL(shmget, key, XSHM_MEMSIZE, IPC_CREAT|0777);
151 if (shminfo->shmid >= 0)
153 /* Attach the mem to our process */
154 shminfo->shmaddr = CCALL(shmat, shminfo->shmid, NULL, 0);
155 if (NULL != shminfo->shmaddr)
157 shminfo->readOnly = False;
158 if (XEXTCALL(XShmAttach, display, shminfo))
160 return shminfo;
163 CCALL(shmdt, shminfo->shmaddr);
166 CCALL(shmctl, shminfo->shmid, IPC_RMID, NULL);
168 #if NO_MALLOC
169 FreeVec(shminfo);
170 #else
171 free(shminfo);
172 #endif
176 } /* If has XShm extension */
178 return NULL;
182 /****************************************************************************************/
184 void cleanup_shared_mem(Display *display, void *meminfo)
186 XShmSegmentInfo *shminfo = (XShmSegmentInfo *)meminfo;
188 if (NULL == meminfo)
189 return;
191 XEXTCALL(XShmDetach, display, shminfo);
192 CCALL(shmdt, shminfo->shmaddr);
193 CCALL(shmctl, shminfo->shmid, IPC_RMID, 0);
195 #if NO_MALLOC
196 FreeVec(shminfo);
197 #else
198 free(shminfo);
199 #endif
203 /****************************************************************************************/
205 XImage *create_xshm_ximage(Display *display, Visual *visual, int depth, int format,
206 int width, int height, void *xshminfo)
208 XShmSegmentInfo *shminfo;
209 XImage *image;
211 shminfo = (XShmSegmentInfo *)xshminfo;
213 image = XEXTCALL(XShmCreateImage, display, visual, depth, format, shminfo->shmaddr,
214 shminfo, width, height);
216 return image;
219 /****************************************************************************************/
221 void put_xshm_ximage(Display *display, Drawable d, GC gc, XImage *image,
222 int xsrc, int ysrc, int xdest, int ydest,
223 int width, int height, Bool send_event)
225 XEXTCALL(XShmPutImage, display, d, gc, image, xsrc, ysrc, xdest, ydest,
226 width, height, send_event);
227 XCALL(XSync, display, False);
230 /****************************************************************************************/
232 void get_xshm_ximage(Display *display, Drawable d, XImage *image, int x, int y)
234 XCALL(XSync, display, False);
235 XEXTCALL(XShmGetImage, display, d, image, x, y, AllPlanes);
238 /****************************************************************************************/
240 void destroy_xshm_ximage(XImage *image)
242 XDestroyImage(image);
245 /****************************************************************************************/
247 #endif /* USE_XSHM */
249 /****************************************************************************************/