1 /*
2  * nanox.c
3  *
4  * Replacements for some comomon Xlib functions
5  * Licence: The wxWindows licence
6  */
7 
8 #include "wx/setup.h"
9 
10 #if wxUSE_NANOX
11 
12 #include <ctype.h>
13 #include <malloc.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include "wx/defs.h"
17 #include "wx/x11/nanox/X11/Xlib.h"
18 
19 /* Expands to some compatibility functions (see XtoNX.h) */
20 
21 STATIC_FUNCTIONS
22 
23 static GR_PALETTE* s_globalColormap = 0;
24 
DefaultColormapOfScreen(Screen * screen)25 Colormap DefaultColormapOfScreen(Screen* screen)
26 {
27     if (!s_globalColormap)
28     {
29         s_globalColormap = (GR_PALETTE*) malloc(sizeof(GR_PALETTE));
30 
31         GrGetSystemPalette(s_globalColormap);
32     }
33 
34     return s_globalColormap;
35 }
36 
XSetGraphicsExposures(Display * display,GC gc,Bool graphics_exposures)37 int XSetGraphicsExposures( Display* display, GC gc, Bool graphics_exposures)
38 {
39     return Success ;
40 }
41 
XWarpPointer(Display * display,Window srcW,Window destW,int srcX,int srcY,unsigned int srcWidth,unsigned int srcHeight,int destX,int destY)42 int XWarpPointer( Display* display, Window srcW, Window destW,
43                  int srcX, int srcY,
44                  unsigned int srcWidth,
45                  unsigned int srcHeight,
46                  int destX, int destY)
47 {
48     GrMoveCursor(destX, destY);
49     return Success;
50 }
51 
XSetInputFocus(Display * display,Window focus,int revert_to,Time time)52 int XSetInputFocus(Display* display, Window focus, int revert_to, Time time)
53 {
54     GrSetFocus(focus);
55     return Success;
56 }
57 
XGetInputFocus(Display * display,Window * focus_return,int * revert_to_return)58 int XGetInputFocus(Display* display, Window* focus_return, int* revert_to_return)
59 {
60     * focus_return = GrGetFocus();
61     * revert_to_return = 0;
62     return Success;
63 }
64 
XGrabPointer(Display * display,Window grab_window,Bool owner_events,unsigned int event_mask,int pointer_mode,int keyboard_mode,Window confine_to,Cursor cursor,Time time)65 int XGrabPointer(Display* display, Window grab_window,
66                  Bool owner_events, unsigned int event_mask,
67                  int pointer_mode, int keyboard_mode,
68                  Window confine_to, Cursor cursor, Time time)
69 {
70     /* According to comments in srvevent.c in Nano-X, the pointer
71      * is implicitly grabbed when a mouse button is down.
72      * We may be able to simulate this further in the event loop.
73      */
74     return Success;
75 }
76 
XUngrabPointer(Display * display,Time time)77 int XUngrabPointer(Display* display, Time time)
78 {
79     return Success;
80 }
81 
XCopyArea(Display * display,Drawable src,Drawable dest,GC gc,int src_x,int src_y,unsigned int width,unsigned int height,int dest_x,int dest_y)82 int XCopyArea(Display* display, Drawable src, Drawable dest, GC gc,
83               int src_x, int src_y, unsigned int width, unsigned int height,
84               int dest_x, int dest_y)
85 {
86     GrCopyArea(dest, gc, dest_x, dest_y,
87                width, height, src,
88                src_x, src_y, 0);
89     return Success;
90 }
91 
XCopyPlane(Display * display,Drawable src,Drawable dest,GC gc,int src_x,int src_y,unsigned int width,unsigned int height,int dest_x,int dest_y,unsigned long plane)92 int XCopyPlane(Display* display, Drawable src, Drawable dest, GC gc,
93               int src_x, int src_y, unsigned int width, unsigned int height,
94               int dest_x, int dest_y, unsigned long plane)
95 {
96     GrCopyArea(dest, gc, dest_x, dest_y,
97                width, height, src,
98                src_x, src_y, 0);
99     return Success;
100 }
101 
102 #if 0
103 typedef struct {
104   GR_WINDOW_ID wid;           /* window id (or 0 if no such window) */
105   GR_WINDOW_ID parent;        /* parent window id */
106   GR_WINDOW_ID child;         /* first child window id (or 0) */
107   GR_WINDOW_ID sibling;       /* next sibling window id (or 0) */
108   GR_BOOL inputonly;          /* TRUE if window is input only */
109   GR_BOOL mapped;             /* TRUE if window is mapped */
110   GR_COUNT unmapcount;        /* reasons why window is unmapped */
111   GR_COORD x;                 /* absolute x position of window */
112   GR_COORD y;                 /* absolute y position of window */
113   GR_SIZE width;              /* width of window */
114   GR_SIZE height;             /* height of window */
115   GR_SIZE bordersize;         /* size of border */
116   GR_COLOR bordercolor;       /* color of border */
117   GR_COLOR background;        /* background color */
118   GR_EVENT_MASK eventmask;    /* current event mask for this client */
119   GR_WM_PROPS props;          /* window properties */
120   GR_CURSOR_ID cursor;        /* cursor id*/
121   unsigned long processid;    /* process id of owner*/
122 } GR_WINDOW_INFO;
123 
124 typedef struct {
125      int x, y;                /* location of window */
126      int width, height;       /* width and height of window */
127      int border_width;        /* border width of window */
128      int depth;               /* depth of window */
129      Visual *visual;          /* the associated visual structure */
130      Window root;             /* root of screen containing window */
131      int class;               /* InputOutput, InputOnly*/
132      int bit_gravity;         /* one of the bit gravity values */
133      int win_gravity;         /* one of the window gravity values */
134      int backing_store;       /* NotUseful, WhenMapped, Always */
135      unsigned long backing_planes;/* planes to be preserved if possible */
136      unsigned long backing_pixel;/* value to be used when restoring planes */
137      Bool save_under;         /* boolean, should bits under be saved? */
138      Colormap colormap;       /* color map to be associated with window */
139      Bool map_installed;      /* boolean, is color map currently installed*/
140      int map_state;           /* IsUnmapped, IsUnviewable, IsViewable */
141      long all_event_masks;    /* set of events all people have interest in*/
142      long your_event_mask;    /* my event mask */
143      long do_not_propagate_mask;/* set of events that should not propagate */
144      Bool override_redirect;  /* boolean value for override-redirect */
145      Screen *screen;          /* back pointer to correct screen */
146 } XWindowAttributes;
147 
148 #endif
149 
150 
XGetWindowAttributes(Display * display,Window w,XWindowAttributes * window_attributes)151 Status XGetWindowAttributes(Display* display, Window w,
152                             XWindowAttributes* window_attributes)
153 {
154     GR_WINDOW_INFO info;
155     Window parent = 0;
156     GrFlush();
157     GrGetWindowInfo(w, & info);
158 
159     window_attributes->x = info.x;
160     window_attributes->y = info.y;
161     window_attributes->width = info.width;
162     window_attributes->height = info.height;
163     window_attributes->border_width = info.bordersize;
164     window_attributes->depth = 0;
165     window_attributes->visual = NULL;
166     window_attributes->root = 0;
167     window_attributes->_class = info.inputonly ? InputOnly : InputOutput ;
168     window_attributes->bit_gravity = 0;
169     window_attributes->win_gravity = 0;
170     window_attributes->backing_store = 0;
171     window_attributes->backing_planes = 0;
172     window_attributes->backing_pixel = 0;
173     window_attributes->save_under = FALSE;
174     window_attributes->colormap = DefaultColormapOfScreen(0);
175     window_attributes->map_installed = FALSE;
176     window_attributes->map_state = info.mapped ? IsViewable : IsUnmapped ;
177     window_attributes->all_event_masks = 0;
178     window_attributes->do_not_propagate_mask = 0;
179     window_attributes->override_redirect = FALSE;
180     window_attributes->screen = NULL;
181 
182     /* We need to check if any parents are unmapped,
183      * or we will report a window as mapped when it is not.
184      */
185     parent = info.parent;
186     while (parent)
187     {
188         GrGetWindowInfo(parent, & info);
189         if (info.mapped == 0)
190             window_attributes->map_state = IsUnmapped;
191 
192         parent = info.parent;
193     }
194 
195     return 1;
196 }
197 
198 static XErrorHandler* g_ErrorHandler = NULL;
199 
DefaultNanoXErrorHandler(GR_EVENT_ERROR * ep)200 void DefaultNanoXErrorHandler(GR_EVENT_ERROR* ep)
201 {
202     if (g_ErrorHandler)
203     {
204         XErrorEvent errEvent;
205         errEvent.type = ep->type;
206         errEvent.display = NULL;
207         errEvent.resourceid = ep->id;
208         errEvent.serial = 0;
209         errEvent.error_code = ep->code;
210         errEvent.request_code = 0;
211         errEvent.minor_code = 0;
212         (*g_ErrorHandler)(NULL, & errEvent);
213     }
214 }
215 
XSetErrorHandler(XErrorHandler handler)216 XErrorHandler XSetErrorHandler (XErrorHandler handler)
217 {
218     XErrorHandler oldHandler = g_ErrorHandler;
219     g_ErrorHandler = handler;
220     GrSetErrorHandler(DefaultNanoXErrorHandler);
221     return oldHandler;
222 }
223 
224 static Screen s_screen;
XScreenOfDisplay(Display * display,int screen_number)225 Screen *XScreenOfDisplay(Display* display,
226                          int screen_number)
227 {
228     /* TODO: fill in the members. See Xlib.h */
229     return & s_screen;
230 }
231 
DisplayWidth(Display * display,int screen)232 int DisplayWidth(Display* display, int screen)
233 {
234     return _display.display_width;
235 }
236 
DisplayHeight(Display * display,int screen)237 int DisplayHeight(Display* display, int screen)
238 {
239     return _display.display_height;
240 }
241 
DefaultDepth(Display * display,int screen)242 int DefaultDepth(Display* display, int screen)
243 {
244     return _display.display_bpp;
245 }
246 
XAllocColor(Display * display,Colormap cmap,XColor * color)247 int XAllocColor(Display* display, Colormap cmap,
248                 XColor* color)
249 {
250     GR_PIXELVAL pixel;
251     GrFindColor(GR_RGB(color->red, color->green, color->blue), & pixel);
252     color->pixel = pixel;
253     return 1;
254 }
255 
256 typedef struct {
257     const char* name;
258     unsigned int red;
259     unsigned int green;
260     unsigned int blue;
261 } _wxColourEntry;
262 
263 static _wxColourEntry _wxColourDatabase[] =
264 {
265     { "WHITE", 255, 255, 255 },
266     { "BLACK", 0, 0, 0 },
267     { "RED", 255, 0, 0 },
268     { "GREEN", 0, 255, 0 },
269     { "BLUE", 0, 255, 255 },
270     { "GREY", 128, 128, 128 },
271     { "GRAY", 128, 128, 128 },
272     { "LIGHT GREY", 192, 192, 192 },
273     { "LIGHT GRAY", 192, 192, 192 },
274     { "DARK GREY", 32, 32, 32 },
275     { "DARK GRAY", 32, 32, 32 },
276     { "CYAN", 0, 255, 255 },
277     { "MAGENTA", 255, 255, 0 },
278 
279     /* TODO: the rest */
280     { NULL, 0, 0, 0 }
281 };
282 
XParseColor(Display * display,Colormap cmap,const char * cname,XColor * color)283 int XParseColor(Display* display, Colormap cmap,
284                 const char* cname, XColor* color)
285 {
286     int i = 0;
287     for (;;)
288     {
289         if (!_wxColourDatabase[i].name)
290             break;
291         else
292         {
293             if (strcmp(cname, _wxColourDatabase[i].name) == 0)
294             {
295                 color->red = _wxColourDatabase[i].red;
296                 color->green = _wxColourDatabase[i].green;
297                 color->blue = _wxColourDatabase[i].blue;
298 
299                 return 1;
300             }
301             i ++;
302         }
303     }
304 
305     /* Not found: use black */
306     color->red = 0;
307     color->green = 0;
308     color->blue = 0;
309 
310     return 0;
311 }
312 
XDrawLine(Display * display,Window win,GC gc,int x1,int y1,int x2,int y2)313 int XDrawLine(Display* display, Window win, GC gc,
314               int x1, int y1, int x2, int y2)
315 {
316     GR_POINT points[2];
317     points[0].x = x1;
318     points[0].y = y1;
319     points[1].x = x2;
320     points[1].y = y2;
321 
322     GrDrawLines(win, gc, points, 2);
323     return 1;
324 }
325 
XTextExtents(XFontStruct * font,char * s,int len,int * direction,int * ascent,int * descent2,XCharStruct * overall)326 int XTextExtents( XFontStruct* font, char* s, int len, int* direction,
327                   int* ascent, int* descent2, XCharStruct* overall)
328 {
329     GR_SIZE retwidth, retheight, retbase;
330     GR_GC_ID gc = GrNewGC();
331 
332     *ascent = font->info.baseline;
333     *direction = 1; /* ? */
334     *descent2 = 0; /* ? */
335 
336     GrSetGCFont(gc, font->fid);
337 
338     /* TODO need to set the relevant flags for the character set.
339      * A good trick might be to pass a wxString instead of char*
340      * to this function.
341      */
342 
343     GrGetGCTextSize(gc, s, len, GR_TFASCII, & retwidth,
344                     & retheight, & retbase);
345     if (overall)
346     {
347         overall->width = retwidth;
348         overall->lbearing = 0;
349         overall->rbearing = 0;
350         overall->ascent = *ascent;
351         overall->descent = 0;
352         overall->attributes = 0;
353     }
354 
355     GrDestroyGC(gc);
356 
357     return 1;
358 }
359 
XLoadQueryFont(Display * display,const char * fontSpec)360 XFontStruct* XLoadQueryFont(Display* display, const char* fontSpec)
361 {
362     /* TODO: map fontSpec to something sensible for Nano-X */
363     char *fontName = NULL;
364     XFontStruct* fontInfo = malloc(sizeof(XFontStruct));
365     fontInfo->fid = GrCreateFont(fontName, 0, 0);
366     GrGetFontInfo(fontInfo->fid, & fontInfo->info);
367     printf("Warning: should not call XLoadQueryFont since font spec is not used in Nano-X.\n");
368     return fontInfo;
369 }
370 
XFreeFont(Display * display,XFontStruct * fontStruct)371 int XFreeFont(Display* display, XFontStruct* fontStruct)
372 {
373     GrDestroyFont(fontStruct->fid);
374     free(fontStruct);
375     return 1;
376 }
377 
XQueryColor(Display * display,Colormap cmap,XColor * color)378 int XQueryColor(Display* display, Colormap cmap, XColor* color)
379 {
380     /* cmap is a GR_PALETTE */
381     if (color->pixel < cmap->count)
382     {
383         color->red = cmap->palette[color->pixel].r;
384         color->green = cmap->palette[color->pixel].g;
385         color->blue = cmap->palette[color->pixel].b;
386         return 1;
387     }
388     else
389         return 0;
390 }
391 
XConfigureWindow(Display * display,Window w,int mask,XWindowChanges * changes)392 int XConfigureWindow(Display* display, Window w, int mask, XWindowChanges* changes)
393 {
394     if ((mask & CWX) && (mask & CWY))
395         GrMoveWindow(w, changes->x, changes->y);
396     if ((mask & CWWidth) && (mask & CWHeight))
397         GrResizeWindow(w, changes->width, changes->height);
398     if (mask & CWBorderWidth)
399     {
400         /* TODO */
401     }
402     if (mask & CWSibling)
403     {
404         /* TODO */
405     }
406     if (mask & CWStackMode)
407     {
408         /* TODO */
409     }
410     return 1;
411 }
412 
XTranslateCoordinates(Display * display,Window srcWindow,Window destWindow,int srcX,int srcY,int * destX,int * destY,Window * childReturn)413 int XTranslateCoordinates(Display* display, Window srcWindow, Window destWindow, int srcX, int srcY, int* destX, int* destY, Window* childReturn)
414 {
415     int offx = 0;
416     int offy = 0;
417 
418     Window w = srcWindow;
419     while (w != GR_ROOT_WINDOW_ID)
420     {
421         GR_WINDOW_INFO info;
422         GrGetWindowInfo(w, & info);
423         w = info.parent;
424 
425         offx += info.x ;
426         offy += info.y ;
427     }
428 
429     w = destWindow;
430 
431     while (w != GR_ROOT_WINDOW_ID)
432     {
433         GR_WINDOW_INFO info;
434         GrGetWindowInfo(w, & info);
435         w = info.parent;
436 
437         offx -= info.x ;
438         offy -= info.y ;
439     }
440 
441     *destX = srcX + offx;
442     *destY = srcY + offy;
443 
444     if (childReturn)
445         *childReturn = 0;
446 
447     return 1;
448 }
449 
450 /* Should not really be necessary but in no-optimize mode
451  * gcc complains that wxNoop is not found if wxNoop is inline.
452  */
453 
wxNoop()454 void wxNoop()
455 {
456 }
457 
458 #endif
459   /* wxUSE_NANOX */
460