Separated several classes into header files from tool main.cpp
[synfig.git] / synfig-osx / launcher / applewm.c
1 /* $XFree86: xc/lib/GL/dri/XF86dri.c,v 1.12 2001/08/27 17:40:57 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 /* THIS IS NOT AN X CONSORTIUM STANDARD */
32
33 #define NEED_EVENTS
34 #define NEED_REPLIES
35 #include <X11/Xlibint.h>
36 #include "applewmstr.h"
37 #include <X11/extensions/Xext.h>
38 #include "extutil.h"
39 #include <stdio.h>
40
41 static XExtensionInfo _applewm_info_data;
42 static XExtensionInfo *applewm_info = &_applewm_info_data;
43 static char *applewm_extension_name = APPLEWMNAME;
44
45 #define AppleWMCheckExtension(dpy,i,val) \
46   XextCheckExtension (dpy, i, applewm_extension_name, val)
47
48 /*****************************************************************************
49  *                                                                           *
50  *                         private utility routines                          *
51  *                                                                           *
52  *****************************************************************************/
53
54 static int close_display(Display *dpy, XExtCodes *extCodes);
55 static Bool wire_to_event();
56 static Status event_to_wire();
57
58 static /* const */ XExtensionHooks applewm_extension_hooks = {
59     NULL,                               /* create_gc */
60     NULL,                               /* copy_gc */
61     NULL,                               /* flush_gc */
62     NULL,                               /* free_gc */
63     NULL,                               /* create_font */
64     NULL,                               /* free_font */
65     close_display,                      /* close_display */
66     wire_to_event,                      /* wire_to_event */
67     event_to_wire,                      /* event_to_wire */
68     NULL,                               /* error */
69     NULL,                               /* error_string */
70 };
71
72 static XEXT_GENERATE_FIND_DISPLAY (find_display, applewm_info, 
73                                    applewm_extension_name, 
74                                    &applewm_extension_hooks, 
75                                    AppleWMNumberEvents, NULL)
76
77 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, applewm_info)
78
79 static Bool wire_to_event (dpy, re, event)
80     Display *dpy;
81     XEvent  *re;
82     xEvent  *event;
83 {
84     XExtDisplayInfo *info = find_display (dpy);
85     XAppleWMNotifyEvent *se;
86     xAppleWMNotifyEvent *sevent;
87
88     AppleWMCheckExtension (dpy, info, False);
89
90     switch ((event->u.u.type & 0x7f) - info->codes->first_event) {
91     case AppleWMControllerNotify:
92     case AppleWMActivationNotify:
93     case AppleWMPasteboardNotify:
94         se = (XAppleWMNotifyEvent *) re;
95         sevent = (xAppleWMNotifyEvent *) event;
96         se->type = sevent->type & 0x7f;
97         se->serial = _XSetLastRequestRead(dpy,(xGenericReply *) event);
98         se->send_event = (sevent->type & 0x80) != 0;
99         se->display = dpy;
100         se->window = 0;
101         se->time = sevent->time;
102         se->kind = sevent->kind;
103         se->arg = sevent->arg;
104         return True;
105     }
106     return False;
107 }
108
109 static Status event_to_wire (dpy, re, event)
110     Display *dpy;
111     XEvent  *re;
112     xEvent  *event;
113 {
114     XExtDisplayInfo *info = find_display (dpy);
115     XAppleWMNotifyEvent *se;
116     xAppleWMNotifyEvent *sevent;
117
118     AppleWMCheckExtension (dpy, info, False);
119
120     switch ((re->type & 0x7f) - info->codes->first_event) {
121     case AppleWMControllerNotify:
122     case AppleWMActivationNotify:
123     case AppleWMPasteboardNotify:
124         se = (XAppleWMNotifyEvent *) re;
125         sevent = (xAppleWMNotifyEvent *) event;
126         sevent->type = se->type | (se->send_event ? 0x80 : 0);
127         sevent->sequenceNumber = se->serial & 0xffff;
128         sevent->kind = se->kind;
129         sevent->arg = se->arg;
130         sevent->time = se->time;
131         return 1;
132     }
133     return 0;
134 }
135
136 /*****************************************************************************
137  *                                                                           *
138  *                  public Apple-WM Extension routines                      *
139  *                                                                           *
140  *****************************************************************************/
141
142 #if 0
143 #include <stdio.h>
144 #define TRACE(msg)  fprintf(stderr, "AppleWM%s\n", msg);
145 #else
146 #define TRACE(msg)
147 #endif
148
149
150 Bool XAppleWMQueryExtension (dpy, event_basep, error_basep)
151     Display *dpy;
152     int *event_basep, *error_basep;
153 {
154     XExtDisplayInfo *info = find_display (dpy);
155
156     TRACE("QueryExtension...");
157     if (XextHasExtension(info)) {
158         *event_basep = info->codes->first_event;
159         *error_basep = info->codes->first_error;
160         TRACE("QueryExtension... return True");
161         return True;
162     } else {
163         TRACE("QueryExtension... return False");
164         return False;
165     }
166 }
167
168 Bool XAppleWMQueryVersion(dpy, majorVersion, minorVersion, patchVersion)
169     Display* dpy;
170     int* majorVersion; 
171     int* minorVersion;
172     int* patchVersion;
173 {
174     XExtDisplayInfo *info = find_display (dpy);
175     xAppleWMQueryVersionReply rep;
176     xAppleWMQueryVersionReq *req;
177
178     TRACE("QueryVersion...");
179     AppleWMCheckExtension (dpy, info, False);
180
181     LockDisplay(dpy);
182     GetReq(AppleWMQueryVersion, req);
183     req->reqType = info->codes->major_opcode;
184     req->wmReqType = X_AppleWMQueryVersion;
185     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
186         UnlockDisplay(dpy);
187         SyncHandle();
188         TRACE("QueryVersion... return False");
189         return False;
190     }
191     *majorVersion = rep.majorVersion;
192     *minorVersion = rep.minorVersion;
193     *patchVersion = rep.patchVersion;
194     UnlockDisplay(dpy);
195     SyncHandle();
196     TRACE("QueryVersion... return True");
197     return True;
198 }
199
200 Bool XAppleWMDisableUpdate(dpy, screen)
201     Display* dpy;
202     int screen;
203 {
204     XExtDisplayInfo *info = find_display (dpy);
205     xAppleWMDisableUpdateReq *req;
206
207     TRACE("DisableUpdate...");
208     AppleWMCheckExtension (dpy, info, False);
209
210     LockDisplay(dpy);
211     GetReq(AppleWMDisableUpdate, req);
212     req->reqType = info->codes->major_opcode;
213     req->wmReqType = X_AppleWMDisableUpdate;
214     req->screen = screen;
215     UnlockDisplay(dpy);
216     SyncHandle();
217     TRACE("DisableUpdate... return True");
218     return True;
219 }
220
221 Bool XAppleWMReenableUpdate(dpy, screen)
222     Display* dpy;
223     int screen;
224 {
225     XExtDisplayInfo *info = find_display (dpy);
226     xAppleWMReenableUpdateReq *req;
227
228     TRACE("ReenableUpdate...");
229     AppleWMCheckExtension (dpy, info, False);
230
231     LockDisplay(dpy);
232     GetReq(AppleWMReenableUpdate, req);
233     req->reqType = info->codes->major_opcode;
234     req->wmReqType = X_AppleWMReenableUpdate;
235     req->screen = screen;
236     UnlockDisplay(dpy);
237     SyncHandle();
238     TRACE("ReenableUpdate... return True");
239     return True;
240 }
241
242 Bool XAppleWMSelectInput(dpy, mask)
243     Display* dpy;
244     unsigned long mask;
245 {
246     XExtDisplayInfo *info = find_display (dpy);
247     xAppleWMSelectInputReq *req;
248
249     TRACE("SelectInput...");
250     AppleWMCheckExtension (dpy, info, False);
251
252     LockDisplay(dpy);
253     GetReq(AppleWMSelectInput, req);
254     req->reqType = info->codes->major_opcode;
255     req->wmReqType = X_AppleWMSelectInput;
256     req->mask = mask;
257     UnlockDisplay(dpy);
258     SyncHandle();
259     TRACE("SetlectInput... return True");
260     return True;
261 }
262
263 Bool XAppleWMSetWindowMenuWithShortcuts(dpy, nitems, items, shortcuts)
264     Display* dpy;
265     int nitems;
266     const char **items;
267     const char *shortcuts;
268 {
269     XExtDisplayInfo *info = find_display (dpy);
270     xAppleWMSetWindowMenuReq *req;
271     int i, total_length, len;
272     char *buf, *ptr;
273
274     TRACE("SetWindowMenu...");
275     AppleWMCheckExtension (dpy, info, False);
276
277     LockDisplay(dpy);
278     GetReq(AppleWMSetWindowMenu, req);
279     req->reqType = info->codes->major_opcode;
280     req->wmReqType = X_AppleWMSetWindowMenu;
281     req->nitems = nitems;
282
283     total_length = 0;
284     for (i = 0; i < nitems; i++)
285         total_length += strlen (items[i]) + 2;
286
287     ptr = buf = alloca (total_length);
288     for (i = 0; i < nitems; i++)
289     {
290         len = strlen (items[i]);
291         *ptr++ = shortcuts ? shortcuts[i] : 0;
292         memcpy (ptr, items[i], len);
293         ptr[len] = 0;
294         ptr += len + 1;
295     }
296
297     req->length += (total_length + 3) >> 2;
298     Data (dpy, buf, total_length);
299
300     UnlockDisplay(dpy);
301     SyncHandle();
302     TRACE("SetlectInput... return True");
303     return True;
304 }
305
306 Bool XAppleWMSetWindowMenu(dpy, nitems, items)
307     Display* dpy;
308     int nitems;
309     const char **items;
310 {
311     return XAppleWMSetWindowMenuWithShortcuts (dpy, nitems, items, NULL);
312 }
313
314 Bool XAppleWMSetWindowMenuCheck(dpy, idx)
315     Display* dpy;
316     int idx;
317 {
318     XExtDisplayInfo *info = find_display (dpy);
319     xAppleWMSetWindowMenuCheckReq *req;
320
321     TRACE("SetWindowMenuCheck...");
322     AppleWMCheckExtension (dpy, info, False);
323
324     LockDisplay(dpy);
325     GetReq(AppleWMSetWindowMenuCheck, req);
326     req->reqType = info->codes->major_opcode;
327     req->wmReqType = X_AppleWMSetWindowMenuCheck;
328     req->index = idx;
329     UnlockDisplay(dpy);
330     SyncHandle();
331     TRACE("SetWindowMenuCheck... return True");
332     return True;
333 }
334
335 Bool XAppleWMSetFrontProcess(dpy)
336     Display* dpy;
337 {
338     XExtDisplayInfo *info = find_display (dpy);
339     xAppleWMSetFrontProcessReq *req;
340
341     TRACE("SetFrontProcess...");
342     AppleWMCheckExtension (dpy, info, False);
343
344     LockDisplay(dpy);
345     GetReq(AppleWMSetFrontProcess, req);
346     req->reqType = info->codes->major_opcode;
347     req->wmReqType = X_AppleWMSetFrontProcess;
348     UnlockDisplay(dpy);
349     SyncHandle();
350     TRACE("SetFrontProcess... return True");
351     return True;
352 }
353
354 Bool XAppleWMSetWindowLevel(dpy, id, level)
355     Display* dpy;
356     Window id;
357     int level;
358 {
359     XExtDisplayInfo *info = find_display (dpy);
360     xAppleWMSetWindowLevelReq *req;
361
362     TRACE("SetWindowLevel...");
363     AppleWMCheckExtension (dpy, info, False);
364
365     LockDisplay(dpy);
366     GetReq(AppleWMSetWindowLevel, req);
367     req->reqType = info->codes->major_opcode;
368     req->wmReqType = X_AppleWMSetWindowLevel;
369     req->window = id;
370     req->level = level;
371     UnlockDisplay(dpy);
372     SyncHandle();
373     TRACE("SetWindowLevel... return True");
374     return True;
375 }
376
377 Bool XAppleWMSetCanQuit(dpy, state)
378     Display* dpy;
379     Bool state;
380 {
381     XExtDisplayInfo *info = find_display (dpy);
382     xAppleWMSetCanQuitReq *req;
383
384     TRACE("SetCanQuit...");
385     AppleWMCheckExtension (dpy, info, False);
386
387     LockDisplay(dpy);
388     GetReq(AppleWMSetCanQuit, req);
389     req->reqType = info->codes->major_opcode;
390     req->wmReqType = X_AppleWMSetCanQuit;
391     req->state = state;
392     UnlockDisplay(dpy);
393     SyncHandle();
394     TRACE("SetCanQuit... return True");
395     return True;
396 }
397
398 Bool XAppleWMFrameGetRect(dpy, frame_class, frame_rect,
399                            ix, iy, iw, ih, ox, oy, ow, oh, rx, ry, rw, rh)
400     Display* dpy;
401     unsigned int frame_class, frame_rect;
402     short ix, iy, iw, ih;
403     short ox, oy, ow, oh;
404     short *rx, *ry, *rw, *rh;
405 {
406     XExtDisplayInfo *info = find_display (dpy);
407     xAppleWMFrameGetRectReply rep;
408     xAppleWMFrameGetRectReq *req;
409
410     TRACE("FrameGetRect...");
411     AppleWMCheckExtension (dpy, info, False);
412
413     LockDisplay(dpy);
414     GetReq(AppleWMFrameGetRect, req);
415     req->reqType = info->codes->major_opcode;
416     req->wmReqType = X_AppleWMFrameGetRect;
417     req->frame_class = frame_class;
418     req->frame_rect = frame_rect;
419     req->ix = ix;
420     req->iy = iy;
421     req->iw = iw;
422     req->ih = ih;
423     req->ox = ox;
424     req->oy = oy;
425     req->ow = ow;
426     req->oh = oh;
427     rep.x = rep.y = rep.w = rep.h = 0;
428     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
429         UnlockDisplay(dpy);
430         SyncHandle();
431         TRACE("FrameGetRect... return False");
432         return False;
433     }
434     *rx = rep.x; *ry = rep.y;
435     *rw = rep.w; *rh = rep.h;
436     UnlockDisplay(dpy);
437     SyncHandle();
438     TRACE("FrameGetRect... return True");
439     return True;
440 }
441
442 unsigned int XAppleWMFrameHitTest(dpy, frame_class, px, py,
443                                    ix, iy, iw, ih, ox, oy, ow, oh)
444     Display* dpy;
445     unsigned int frame_class;
446     short px, py;
447     short ix, iy, iw, ih;
448     short ox, oy, ow, oh;
449 {
450     XExtDisplayInfo *info = find_display (dpy);
451     xAppleWMFrameHitTestReply rep;
452     xAppleWMFrameHitTestReq *req;
453
454     TRACE("FrameHitTest...");
455     AppleWMCheckExtension (dpy, info, False);
456
457     LockDisplay(dpy);
458     GetReq(AppleWMFrameHitTest, req);
459     req->reqType = info->codes->major_opcode;
460     req->wmReqType = X_AppleWMFrameHitTest;
461     req->frame_class = frame_class;
462     req->px = px;
463     req->py = py;
464     req->ix = ix;
465     req->iy = iy;
466     req->iw = iw;
467     req->ih = ih;
468     req->ox = ox;
469     req->oy = oy;
470     req->ow = ow;
471     req->oh = oh;
472     rep.ret = 0;
473     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
474         UnlockDisplay(dpy);
475         SyncHandle();
476         TRACE("FrameHitTest... return False");
477         return False;
478     }
479     UnlockDisplay(dpy);
480     SyncHandle();
481     TRACE("FrameHiTest... return True");
482     return rep.ret;
483 }
484
485 Bool XAppleWMFrameDraw(dpy, screen, window,
486                         frame_class, frame_attr,
487                         ix, iy, iw, ih, ox, oy, ow, oh,
488                         title_length, title_bytes)
489     Display* dpy;
490     int screen;
491     Window window;
492     unsigned int frame_class, frame_attr;
493     short ix, iy, iw, ih;
494     short ox, oy, ow, oh;
495     unsigned int title_length;
496     const unsigned char *title_bytes;
497 {
498     XExtDisplayInfo *info = find_display (dpy);
499     xAppleWMFrameDrawReq *req;
500
501     TRACE("FrameDraw...");
502     AppleWMCheckExtension (dpy, info, False);
503
504     LockDisplay(dpy);
505     GetReq(AppleWMFrameDraw, req);
506     req->reqType = info->codes->major_opcode;
507     req->wmReqType = X_AppleWMFrameDraw;
508     req->screen = screen;
509     req->window = window;
510     req->frame_class = frame_class;
511     req->frame_attr = frame_attr;
512     req->ix = ix;
513     req->iy = iy;
514     req->iw = iw;
515     req->ih = ih;
516     req->ox = ox;
517     req->oy = oy;
518     req->ow = ow;
519     req->oh = oh;
520     req->title_length = title_length;
521
522     req->length += (title_length + 3)>>2;
523     Data (dpy, title_bytes, title_length);
524
525     UnlockDisplay(dpy);
526     SyncHandle();
527     TRACE("FrameDraw... return True");
528     return True;
529 }