Added missing properties.
[AROS.git] / arch / all-hosted / hidd / x11 / xshm.c
blobb234729ff5011bdfb8e9ca4b49acf6d8c2f04687
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 #if USE_XSHM
39 /****************************************************************************************/
41 #include <sys/shm.h>
42 #include <sys/ipc.h>
43 #include <X11/extensions/XShm.h>
45 #include "x11_hostlib.h"
47 #include <proto/hostlib.h>
49 static void *xext_handle = NULL;
51 static struct {
52 Status (*XShmDetach) ( Display* , XShmSegmentInfo* );
53 Status (*XShmPutImage) ( Display* , Drawable , GC , XImage* , int , int , int , int , unsigned int , unsigned int , Bool );
54 Status (*XShmGetImage) ( Display* , Drawable , XImage* , int , int , unsigned long );
55 XImage * (*XShmCreateImage) ( Display* , Visual* , unsigned int , int , char* , XShmSegmentInfo* , unsigned int , unsigned int );
56 Bool (*XShmQueryVersion) ( Display* , int* , int* , Bool* );
57 Status (*XShmAttach) ( Display* , XShmSegmentInfo* );
58 } xext_func;
60 static const char *xext_func_names[] = {
61 "XShmDetach",
62 "XShmPutImage",
63 "XShmGetImage",
64 "XShmCreateImage",
65 "XShmQueryVersion",
66 "XShmAttach"
69 #ifdef HOST_OS_linux
70 #define XEXT_SOFILE "libXext.so.6"
71 #endif
73 #ifdef HOST_OS_darwin
74 #define XEXT_SOFILE "/usr/X11/lib/libXext.6.dylib"
75 #endif
77 #ifndef XEXT_SOFILE
78 #define XEXT_SOFILE "libXext.so"
79 #endif
81 #define XEXTCALL(func,...) (xext_func.func(__VA_ARGS__))
83 extern void *x11_hostlib_load_so(const char *, const char **, int, void **);
85 static int xext_hostlib_init(void *libbase) {
86 D(bug("[x11] xext hostlib init\n"));
88 if ((xext_handle = x11_hostlib_load_so(XEXT_SOFILE, xext_func_names, 6, (void **) &xext_func)) == NULL)
89 return FALSE;
91 return TRUE;
94 static int xext_hostlib_expunge(void *libbase) {
95 D(bug("[x11] xext hostlib expunge\n"));
97 if (xext_handle != NULL)
98 HostLib_Close(xext_handle, NULL);
100 return TRUE;
103 ADD2INITLIB(xext_hostlib_init, 1)
104 ADD2EXPUNGELIB(xext_hostlib_expunge, 1)
107 /****************************************************************************************/
109 void *init_shared_mem(Display *display)
111 /* TODO: Also check if this is a local display */
113 XShmSegmentInfo *shminfo;
114 int xshm_major, xshm_minor;
115 Bool xshm_pixmaps;
117 if (XEXTCALL(XShmQueryVersion, display, &xshm_major, &xshm_minor, &xshm_pixmaps))
119 #if NO_MALLOC
120 shminfo = (XShmSegmentInfo *)AllocVec(sizeof(*shminfo), MEMF_PUBLIC);
121 #else
122 shminfo = (XShmSegmentInfo *)malloc(sizeof(*shminfo));
123 #endif
125 if (NULL != shminfo)
127 key_t key;
130 * Try and get a key for us to use. The idea is to use a
131 * filename that isn't likely to change all that often. This
132 * is made somewhat easier since we must be run from the AROS
133 * root directory (atm). So, I shall choose the path "C",
134 * since the inode number isn't likely to change all that
135 * often.
137 #if 1
138 key = IPC_PRIVATE;
139 #else
140 key = CCALL(ftok, "./C", 'A');
141 if(key == -1)
143 kprintf("Hmm, path \"./C\" doesn't seem to exist?\n");
144 key = IPC_PRIVATE;
146 else
148 kprintf("Using shared memory key %d\n", key);
150 #endif
151 memset(shminfo, 0, sizeof (*shminfo));
153 /* Allocate shared memory */
154 shminfo->shmid = CCALL(shmget, key, XSHM_MEMSIZE, IPC_CREAT|0777);
156 if (shminfo->shmid >= 0)
158 /* Attach the mem to our process */
159 shminfo->shmaddr = CCALL(shmat, shminfo->shmid, NULL, 0);
160 if (NULL != shminfo->shmaddr)
162 shminfo->readOnly = False;
163 if (XEXTCALL(XShmAttach, display, shminfo))
165 return shminfo;
168 CCALL(shmdt, shminfo->shmaddr);
171 CCALL(shmctl, shminfo->shmid, IPC_RMID, NULL);
173 #if NO_MALLOC
174 FreeVec(shminfo);
175 #else
176 free(shminfo);
177 #endif
181 } /* If has XShm extension */
183 return NULL;
187 /****************************************************************************************/
189 void cleanup_shared_mem(Display *display, void *meminfo)
191 XShmSegmentInfo *shminfo = (XShmSegmentInfo *)meminfo;
193 if (NULL == meminfo)
194 return;
196 XEXTCALL(XShmDetach, display, shminfo);
197 CCALL(shmdt, shminfo->shmaddr);
198 CCALL(shmctl, shminfo->shmid, IPC_RMID, 0);
200 #if NO_MALLOC
201 FreeVec(shminfo);
202 #else
203 free(shminfo);
204 #endif
208 /****************************************************************************************/
210 XImage *create_xshm_ximage(Display *display, Visual *visual, int depth, int format,
211 int width, int height, void *xshminfo)
213 XShmSegmentInfo *shminfo;
214 XImage *image;
216 shminfo = (XShmSegmentInfo *)xshminfo;
218 image = XEXTCALL(XShmCreateImage, display, visual, depth, format, shminfo->shmaddr,
219 shminfo, width, height);
221 return image;
224 /****************************************************************************************/
226 void put_xshm_ximage(Display *display, Drawable d, GC gc, XImage *image,
227 int xsrc, int ysrc, int xdest, int ydest,
228 int width, int height, Bool send_event)
230 XEXTCALL(XShmPutImage, display, d, gc, image, xsrc, ysrc, xdest, ydest,
231 width, height, send_event);
232 XCALL(XSync, display, False);
235 /****************************************************************************************/
237 int get_xshm_ximage(Display *display, Drawable d, XImage *image, int x, int y)
239 XCALL(XSync, display, False);
240 XEXTCALL(XShmGetImage, display, d, image, x, y, AllPlanes);
242 return (int)XEXTCALL(XShmGetImage, display, d, image, x, y, AllPlanes);
245 /****************************************************************************************/
247 void destroy_xshm_ximage(XImage *image)
249 XDestroyImage(image);
252 /****************************************************************************************/
254 #endif /* USE_XSHM */
256 /****************************************************************************************/