Remove ancient trunk folder from svn repository
[synfig.git] / synfig-osx / launcher / appledri.c
1 /* $XFree86: xc/programs/Xserver/GL/dri/xf86dri.c,v 1.10 2000/12/07 20:26:14 dawes Exp $ */
2 /**************************************************************************
3
4 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
5 Copyright 2000 VA Linux Systems, Inc.
6 Copyright (c) 2002 Apple Computer, Inc.
7 All Rights Reserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
19 of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32  * Authors:
33  *   Kevin E. Martin <martin@valinux.com>
34  *   Jens Owen <jens@valinux.com>
35  *   Rickard E. (Rik) Faith <faith@valinux.com>
36  *
37  */
38
39 #define NEED_REPLIES
40 #define NEED_EVENTS
41 #include "X.h"
42 #include "Xproto.h"
43 #include "misc.h"
44 #include "dixstruct.h"
45 #include "extnsionst.h"
46 #include "colormapst.h"
47 #include "cursorstr.h"
48 #include "scrnintstr.h"
49 #include "servermd.h"
50 #define _APPLEDRI_SERVER_
51 #include "appledristr.h"
52 #include "swaprep.h"
53 #include "dri.h"
54 #include "dri-surface.h"
55 #include "dristruct.h"
56 #include "rootless-common.h"
57 #include "X11Application.h"
58
59 static int DRIErrorBase;
60
61 static DISPATCH_PROC(ProcAppleDRIDispatch);
62 static DISPATCH_PROC(SProcAppleDRIDispatch);
63
64 static void AppleDRIResetProc(ExtensionEntry* extEntry);
65
66 static unsigned char DRIReqCode = 0;
67 static int DRIEventBase = 0;
68
69 extern void AppleDRIExtensionInit(void);
70
71 static void SNotifyEvent(xAppleDRINotifyEvent *from, xAppleDRINotifyEvent *to);
72
73 typedef struct _DRIEvent *DRIEventPtr;
74 typedef struct _DRIEvent {
75     DRIEventPtr     next;
76     ClientPtr       client;
77     XID             clientResource;
78     unsigned int    mask;
79 } DRIEventRec;
80
81 void
82 AppleDRIExtensionInit(void)
83 {
84     ExtensionEntry* extEntry;
85
86     if (DRIExtensionInit() &&
87         (extEntry = AddExtension(APPLEDRINAME,
88                                  AppleDRINumberEvents,
89                                  AppleDRINumberErrors,
90                                  ProcAppleDRIDispatch,
91                                  SProcAppleDRIDispatch,
92                                  AppleDRIResetProc,
93                                  StandardMinorOpcode))) {
94         DRIReqCode = (unsigned char)extEntry->base;
95         DRIErrorBase = extEntry->errorBase;
96         DRIEventBase = extEntry->eventBase;
97         EventSwapVector[DRIEventBase] = (EventSwapPtr) SNotifyEvent;
98     }
99 }
100
101 /*ARGSUSED*/
102 static void
103 AppleDRIResetProc (
104     ExtensionEntry* extEntry
105 )
106 {
107     DRIReset();
108 }
109
110 static int
111 ProcAppleDRIQueryVersion(
112     register ClientPtr client
113 )
114 {
115     xAppleDRIQueryVersionReply rep;
116     register int n;
117
118     REQUEST_SIZE_MATCH(xAppleDRIQueryVersionReq);
119     rep.type = X_Reply;
120     rep.length = 0;
121     rep.sequenceNumber = client->sequence;
122     rep.majorVersion = APPLE_DRI_MAJOR_VERSION;
123     rep.minorVersion = APPLE_DRI_MINOR_VERSION;
124     rep.patchVersion = APPLE_DRI_PATCH_VERSION;
125     if (client->swapped) {
126         swaps(&rep.sequenceNumber, n);
127         swapl(&rep.length, n);
128     }
129     WriteToClient(client, sizeof(xAppleDRIQueryVersionReply), (char *)&rep);
130     return (client->noClientException);
131 }
132
133 \f
134 /* surfaces */
135
136 static int
137 ProcAppleDRIQueryDirectRenderingCapable(
138     register ClientPtr client
139 )
140 {
141     xAppleDRIQueryDirectRenderingCapableReply   rep;
142     Bool isCapable;
143
144     REQUEST(xAppleDRIQueryDirectRenderingCapableReq);
145     REQUEST_SIZE_MATCH(xAppleDRIQueryDirectRenderingCapableReq);
146     rep.type = X_Reply;
147     rep.length = 0;
148     rep.sequenceNumber = client->sequence;
149
150     if (!DRIQueryDirectRenderingCapable( screenInfo.screens[stuff->screen], 
151                                          &isCapable)) {
152         return BadValue;
153     }
154     rep.isCapable = isCapable;
155
156     if (!LocalClient(client))
157         rep.isCapable = 0;
158
159     WriteToClient(client, 
160         sizeof(xAppleDRIQueryDirectRenderingCapableReply), (char *)&rep);
161     return (client->noClientException);
162 }
163
164 static int
165 ProcAppleDRIAuthConnection(
166     register ClientPtr client
167 )
168 {
169     xAppleDRIAuthConnectionReply rep;
170     
171     REQUEST(xAppleDRIAuthConnectionReq);
172     REQUEST_SIZE_MATCH(xAppleDRIAuthConnectionReq);
173
174     rep.type = X_Reply;
175     rep.length = 0;
176     rep.sequenceNumber = client->sequence;
177     rep.authenticated = 1;
178
179     if (!DRIAuthConnection( screenInfo.screens[stuff->screen], stuff->magic)) {
180         ErrorF("Failed to authenticate %u\n", stuff->magic);
181         rep.authenticated = 0;
182     }
183     WriteToClient(client, sizeof(xAppleDRIAuthConnectionReply), (char *)&rep);
184     return (client->noClientException);
185 }
186
187 static void surface_notify (void *_arg, void *data)
188 {
189     DRISurfaceNotifyArg *arg = _arg;
190     int client_index = (int) data;
191     ClientPtr client;
192     xAppleDRINotifyEvent se;
193
194     if (client_index < 0 || client_index >= currentMaxClients)
195         return;
196
197     client = clients[client_index];
198     if (client == NULL || client == serverClient || client->clientGone)
199         return;
200
201     se.type = DRIEventBase + AppleDRISurfaceNotify;
202     se.kind = arg->kind;
203     se.arg = arg->id;
204     se.sequenceNumber = client->sequence;
205     se.time = currentTime.milliseconds;
206     WriteEventsToClient (client, 1, (xEvent *) &se);
207 }
208
209 static int
210 ProcAppleDRICreateSurface(
211     ClientPtr client
212 )
213 {
214     xAppleDRICreateSurfaceReply rep;
215     DrawablePtr pDrawable;
216     xp_surface_id sid;
217     unsigned int key[2];
218
219     REQUEST(xAppleDRICreateSurfaceReq);
220     REQUEST_SIZE_MATCH(xAppleDRICreateSurfaceReq);
221     rep.type = X_Reply;
222     rep.length = 0;
223     rep.sequenceNumber = client->sequence;
224
225     if (!(pDrawable = (DrawablePtr)SecurityLookupDrawable(
226                                                 (Drawable)stuff->drawable,
227                                                 client, 
228                                                 SecurityReadAccess))) {
229         return BadValue;
230     }
231
232     rep.key_0 = rep.key_1 = rep.uid = 0;
233
234     if (!DRICreateSurface( screenInfo.screens[stuff->screen],
235                            (Drawable)stuff->drawable, pDrawable,
236                            stuff->client_id, &sid, key,
237                            surface_notify, (void *) client->index)) {
238         return BadValue;
239     }
240
241     rep.key_0 = key[0];
242     rep.key_1 = key[1];
243     rep.uid = sid;
244
245     WriteToClient(client, sizeof(xAppleDRICreateSurfaceReply), (char *)&rep);
246     return (client->noClientException);
247 }
248
249 static int
250 ProcAppleDRIDestroySurface(
251     register ClientPtr client
252 )
253 {
254     REQUEST(xAppleDRIDestroySurfaceReq);
255     DrawablePtr pDrawable;
256     REQUEST_SIZE_MATCH(xAppleDRIDestroySurfaceReq);
257
258     if (!(pDrawable = (DrawablePtr)SecurityLookupDrawable(
259                                                 (Drawable)stuff->drawable,
260                                                 client, 
261                                                 SecurityReadAccess))) {
262         return BadValue;
263     }
264
265     if (!DRIDestroySurface( screenInfo.screens[stuff->screen], 
266                             (Drawable)stuff->drawable,
267                             pDrawable, NULL, NULL)) {
268         return BadValue;
269     }
270
271     return (client->noClientException);
272 }
273
274 \f
275 /* dispatch */
276
277 static int
278 ProcAppleDRIDispatch (
279     register ClientPtr  client
280 )
281 {
282     REQUEST(xReq);
283
284     switch (stuff->data)
285     {
286     case X_AppleDRIQueryVersion:
287         return ProcAppleDRIQueryVersion(client);
288     case X_AppleDRIQueryDirectRenderingCapable:
289         return ProcAppleDRIQueryDirectRenderingCapable(client);
290     }
291
292     if (!LocalClient(client))
293         return DRIErrorBase + AppleDRIClientNotLocal;
294
295     switch (stuff->data)
296     {
297     case X_AppleDRIAuthConnection:
298         return ProcAppleDRIAuthConnection(client);
299     case X_AppleDRICreateSurface:
300         return ProcAppleDRICreateSurface(client);
301     case X_AppleDRIDestroySurface:
302         return ProcAppleDRIDestroySurface(client);
303     default:
304         return BadRequest;
305     }
306 }
307
308 static void
309 SNotifyEvent(from, to)
310     xAppleDRINotifyEvent *from, *to;
311 {
312     to->type = from->type;
313     to->kind = from->kind;
314     cpswaps (from->sequenceNumber, to->sequenceNumber);
315     cpswapl (from->time, to->time);
316     cpswapl (from->arg, to->arg);
317 }
318
319 static int
320 SProcAppleDRIQueryVersion(
321     register ClientPtr  client
322 )
323 {
324     register int n;
325     REQUEST(xAppleDRIQueryVersionReq);
326     swaps(&stuff->length, n);
327     return ProcAppleDRIQueryVersion(client);
328 }
329
330 static int
331 SProcAppleDRIDispatch (
332     register ClientPtr  client
333 )
334 {
335     REQUEST(xReq);
336
337     /* It is bound to be non-local when there is byte swapping */
338     if (!LocalClient(client))
339         return DRIErrorBase + AppleDRIClientNotLocal;
340
341     /* only local clients are allowed DRI access */
342     switch (stuff->data)
343     {
344     case X_AppleDRIQueryVersion:
345         return SProcAppleDRIQueryVersion(client);
346     default:
347         return BadRequest;
348     }
349 }