revert between 56095 -> 55830 in arch
[AROS.git] / arch / all-hosted / hidd / x11 / x11_xshm.c
blob44d3b595034138ea9f8c5b9d48d1cf49e74b244c
1 /*
2 Copyright © 1995-2017, 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_debug.h"
16 #include <aros/config.h>
17 #include <aros/symbolsets.h>
19 #include "x11_types.h"
20 #include "x11_xshm.h"
22 /****************************************************************************************/
24 /* stegerg: maybe more safe, even if Unix malloc is used and not AROS malloc */
25 #define NO_MALLOC 1
27 /****************************************************************************************/
29 #if NO_MALLOC
30 #include <proto/exec.h>
31 #endif
33 /****************************************************************************************/
35 #if USE_XSHM
37 /****************************************************************************************/
39 #include <X11/extensions/XShm.h>
41 #include "x11_hostlib.h"
43 static void *xext_handle = NULL;
45 static struct {
46 Status (*XShmDetach) ( Display* , XShmSegmentInfo* );
47 Status (*XShmPutImage) ( Display* , Drawable , GC , XImage* , int , int , int , int , unsigned int , unsigned int , Bool );
48 Status (*XShmGetImage) ( Display* , Drawable , XImage* , int , int , unsigned long );
49 XImage * (*XShmCreateImage) ( Display* , Visual* , unsigned int , int , char* , XShmSegmentInfo* , unsigned int , unsigned int );
50 Bool (*XShmQueryVersion) ( Display* , int* , int* , Bool* );
51 Status (*XShmAttach) ( Display* , XShmSegmentInfo* );
52 } xext_func;
54 static const char *xext_func_names[] = {
55 "XShmDetach",
56 "XShmPutImage",
57 "XShmGetImage",
58 "XShmCreateImage",
59 "XShmQueryVersion",
60 "XShmAttach"
63 #ifdef HOST_OS_linux
64 #define XEXT_SOFILE "libXext.so.6"
65 #endif
67 #ifdef HOST_OS_darwin
68 #define XEXT_SOFILE "/usr/X11/lib/libXext.6.dylib"
69 #endif
71 #ifndef XEXT_SOFILE
72 #define XEXT_SOFILE "libXext.so"
73 #endif
75 #define XEXTCALL(func,...) (xext_func.func(__VA_ARGS__))
77 extern void *x11_hostlib_load_so(const char *, const char **, int, void **);
79 static int xext_hostlib_init(void *libbase) {
80 D(bug("[x11] xext hostlib init\n"));
82 if ((xext_handle = x11_hostlib_load_so(XEXT_SOFILE, xext_func_names, 6, (void **) &xext_func)) == NULL)
83 return FALSE;
85 return TRUE;
88 static int xext_hostlib_expunge(void *libbase) {
89 D(bug("[x11] xext hostlib expunge\n"));
91 if (xext_handle != NULL)
92 HostLib_Close(xext_handle, NULL);
94 return TRUE;
97 ADD2INITLIB(xext_hostlib_init, 1)
98 ADD2EXPUNGELIB(xext_hostlib_expunge, 1)
101 /****************************************************************************************/
103 void *init_shared_mem(Display *display)
105 /* TODO: Also check if this is a local display */
107 XShmSegmentInfo *shminfo;
108 int xshm_major, xshm_minor;
109 Bool xshm_pixmaps;
111 if (XEXTCALL(XShmQueryVersion, display, &xshm_major, &xshm_minor, &xshm_pixmaps))
113 #if NO_MALLOC
114 shminfo = (XShmSegmentInfo *)AllocVec(sizeof(*shminfo), MEMF_PUBLIC);
115 #else
116 shminfo = (XShmSegmentInfo *)malloc(sizeof(*shminfo));
117 #endif
119 if (NULL != shminfo)
121 key_t key;
124 * Try and get a key for us to use. The idea is to use a
125 * filename that isn't likely to change all that often. This
126 * is made somewhat easier since we must be run from the AROS
127 * root directory (atm). So, I shall choose the path "C",
128 * since the inode number isn't likely to change all that
129 * often.
131 #if 1
132 key = IPC_PRIVATE;
133 #else
134 key = CCALL(ftok, "./C", 'A');
135 if(key == -1)
137 kprintf("Hmm, path \"./C\" doesn't seem to exist?\n");
138 key = IPC_PRIVATE;
140 else
142 kprintf("Using shared memory key %d\n", key);
144 #endif
145 memset(shminfo, 0, sizeof (*shminfo));
147 /* Allocate shared memory */
148 shminfo->shmid = CCALL(shmget, key, XSHM_MEMSIZE, IPC_CREAT|0777);
150 if (shminfo->shmid >= 0)
152 /* Attach the mem to our process */
153 shminfo->shmaddr = CCALL(shmat, shminfo->shmid, NULL, 0);
154 if (NULL != shminfo->shmaddr)
156 shminfo->readOnly = False;
157 if (XEXTCALL(XShmAttach, display, shminfo))
159 return shminfo;
162 CCALL(shmdt, shminfo->shmaddr);
165 CCALL(shmctl, shminfo->shmid, IPC_RMID, NULL);
167 #if NO_MALLOC
168 FreeVec(shminfo);
169 #else
170 free(shminfo);
171 #endif
175 } /* If has XShm extension */
177 return NULL;
181 /****************************************************************************************/
183 void cleanup_shared_mem(Display *display, void *meminfo)
185 XShmSegmentInfo *shminfo = (XShmSegmentInfo *)meminfo;
187 if (NULL == meminfo)
188 return;
190 XEXTCALL(XShmDetach, display, shminfo);
191 CCALL(shmdt, shminfo->shmaddr);
192 CCALL(shmctl, shminfo->shmid, IPC_RMID, 0);
194 #if NO_MALLOC
195 FreeVec(shminfo);
196 #else
197 free(shminfo);
198 #endif
202 /****************************************************************************************/
204 XImage *create_xshm_ximage(Display *display, Visual *visual, int depth, int format,
205 int width, int height, void *xshminfo)
207 XShmSegmentInfo *shminfo;
208 XImage *image;
210 shminfo = (XShmSegmentInfo *)xshminfo;
212 image = XEXTCALL(XShmCreateImage, display, visual, depth, format, shminfo->shmaddr,
213 shminfo, width, height);
215 return image;
218 /****************************************************************************************/
220 void put_xshm_ximage(Display *display, Drawable d, GC gc, XImage *image,
221 int xsrc, int ysrc, int xdest, int ydest,
222 int width, int height, Bool send_event)
224 XEXTCALL(XShmPutImage, display, d, gc, image, xsrc, ysrc, xdest, ydest,
225 width, height, send_event);
226 XCALL(XSync, display, False);
229 /****************************************************************************************/
231 int get_xshm_ximage(Display *display, Drawable d, XImage *image, int x, int y)
233 XCALL(XSync, display, False);
234 XEXTCALL(XShmGetImage, display, d, image, x, y, AllPlanes);
236 return (int)XEXTCALL(XShmGetImage, display, d, image, x, y, AllPlanes);
239 /****************************************************************************************/
241 void destroy_xshm_ximage(XImage *image)
243 XDestroyImage(image);
246 /****************************************************************************************/
248 #endif /* USE_XSHM */
250 /****************************************************************************************/