rnd-20070120-1-src
[rocksndiamonds.git] / src / libgame / x11.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * x11.c                                                    *
12 ***********************************************************/
13
14 #include "system.h"
15 #include "pcx.h"
16 #include "misc.h"
17 #include "setup.h"
18
19
20 #if defined(TARGET_X11)
21
22 static void X11InitDisplay();
23 static DrawWindow *X11InitWindow();
24
25 void X11InitVideoDisplay(void)
26 {
27   /* initialize X11 video */
28   X11InitDisplay();
29
30   /* set default X11 depth */
31   video.default_depth = XDefaultDepth(display, screen);
32 }
33
34 void X11InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window)
35 {
36   *window = X11InitWindow();
37
38   XMapWindow(display, (*window)->drawable);
39   FlushDisplay();
40
41   /* create additional (off-screen) buffer for double-buffering */
42   *backbuffer = CreateBitmap(video.width, video.height, video.depth);
43 }
44
45 static void X11InitDisplay()
46 {
47 #if !defined(PLATFORM_MSDOS)
48   XVisualInfo vinfo_template, *vinfo;
49   int num_visuals;
50 #endif
51   unsigned int depth;
52
53   /* connect to X server */
54   if (!(display = XOpenDisplay(options.display_name)))
55     Error(ERR_EXIT, "cannot connect to X server %s",
56           XDisplayName(options.display_name));
57
58   screen = DefaultScreen(display);
59   visual = DefaultVisual(display, screen);
60   depth  = DefaultDepth(display, screen);
61   cmap   = DefaultColormap(display, screen);
62
63 #if !defined(PLATFORM_MSDOS)
64   /* look for good enough visual */
65   vinfo_template.screen = screen;
66   vinfo_template.class = (depth == 8 ? PseudoColor : TrueColor);
67   vinfo_template.depth = depth;
68   if ((vinfo = XGetVisualInfo(display, VisualScreenMask | VisualClassMask |
69                               VisualDepthMask, &vinfo_template, &num_visuals)))
70   {
71     visual = vinfo->visual;
72     XFree((void *)vinfo);
73   }
74
75   /* got appropriate visual? */
76   if (depth < 8)
77     Error(ERR_EXIT, "X11 display not supported (less than 8 bits per pixel)");
78   else if ((depth ==8 && visual->class != PseudoColor) ||
79            (depth > 8 && visual->class != TrueColor &&
80             visual->class != DirectColor))
81     Error(ERR_EXIT, "X11 display not supported (inappropriate visual)");
82 #endif /* !PLATFORM_MSDOS */
83 }
84
85 static DrawWindow *X11InitWindow()
86 {
87   DrawWindow *new_window = CreateBitmapStruct();
88   unsigned int border_width = 4;
89   XGCValues gc_values;
90   unsigned long gc_valuemask;
91 #if !defined(PLATFORM_MSDOS)
92   XTextProperty windowName, iconName;
93   Pixmap icon_pixmap, iconmask_pixmap;
94   unsigned int icon_width, icon_height;
95   int icon_hot_x, icon_hot_y;
96   XSizeHints size_hints;
97   XWMHints wm_hints;
98   XClassHint class_hints;
99   char *window_name = program.window_title;
100   char *icon_name = program.window_title;
101   long window_event_mask;
102   Atom proto_atom = None, delete_atom = None;
103 #endif
104   int screen_width, screen_height;
105   int win_xpos, win_ypos;
106   unsigned long pen_fg = WhitePixel(display, screen);
107   unsigned long pen_bg = BlackPixel(display, screen);
108   const int width = video.width, height = video.height;
109   int i;
110
111   screen_width = XDisplayWidth(display, screen);
112   screen_height = XDisplayHeight(display, screen);
113
114   win_xpos = (screen_width - width) / 2;
115   win_ypos = (screen_height - height) / 2;
116
117   new_window->drawable = XCreateSimpleWindow(display,
118                                              RootWindow(display, screen),
119                                              win_xpos, win_ypos,
120                                              width, height, border_width,
121                                              pen_fg, pen_bg);
122
123 #if !defined(PLATFORM_MSDOS)
124   proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
125   delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
126   if ((proto_atom != None) && (delete_atom != None))
127     XChangeProperty(display, new_window->drawable, proto_atom, XA_ATOM, 32,
128                     PropModePrepend, (unsigned char *) &delete_atom, 1);
129
130   if (XReadBitmapFile(display, new_window->drawable,
131                       getCustomImageFilename(program.x11_icon_filename),
132                       &icon_width, &icon_height, &icon_pixmap,
133                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
134     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
135           program.x11_icon_filename);
136
137   if (XReadBitmapFile(display, new_window->drawable,
138                       getCustomImageFilename(program.x11_iconmask_filename),
139                       &icon_width, &icon_height, &iconmask_pixmap,
140                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
141     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
142           program.x11_iconmask_filename);
143
144   size_hints.width  = size_hints.min_width  = size_hints.max_width  = width;
145   size_hints.height = size_hints.min_height = size_hints.max_height = height;
146   size_hints.flags = PSize | PMinSize | PMaxSize;
147
148   if (win_xpos || win_ypos)
149   {
150     size_hints.x = win_xpos;
151     size_hints.y = win_ypos;
152     size_hints.flags |= PPosition;
153   }
154
155   if (!XStringListToTextProperty(&window_name, 1, &windowName))
156     Error(ERR_EXIT, "structure allocation for windowName failed");
157
158   if (!XStringListToTextProperty(&icon_name, 1, &iconName))
159     Error(ERR_EXIT, "structure allocation for iconName failed");
160
161   wm_hints.initial_state = NormalState;
162   wm_hints.input = True;
163   wm_hints.icon_pixmap = icon_pixmap;
164   wm_hints.icon_mask = iconmask_pixmap;
165   wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
166
167   class_hints.res_name = program.command_basename;
168   class_hints.res_class = program.program_title;
169
170   XSetWMProperties(display, new_window->drawable, &windowName, &iconName, 
171                    NULL, 0, &size_hints, &wm_hints, 
172                    &class_hints);
173
174   XFree(windowName.value);
175   XFree(iconName.value);
176
177   /* Select event types wanted */
178   window_event_mask =
179     ExposureMask | StructureNotifyMask | FocusChangeMask |
180     ButtonPressMask | ButtonReleaseMask |
181     PointerMotionMask | PointerMotionHintMask |
182     KeyPressMask | KeyReleaseMask;
183
184   XSelectInput(display, new_window->drawable, window_event_mask);
185 #endif
186
187   /* create GC for drawing with window depth and background color (black) */
188   gc_values.graphics_exposures = False;
189   gc_values.foreground = pen_bg;
190   gc_values.background = pen_bg;
191   gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
192   new_window->gc =
193     XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
194
195   /* create GCs for line drawing (black and white) */
196   for (i = 0; i < 2; i++)
197   {
198     gc_values.graphics_exposures = False;
199     gc_values.foreground = (i ? pen_fg : pen_bg);
200     gc_values.background = pen_bg;
201     gc_values.line_width = 4;
202     gc_values.line_style = LineSolid;
203     gc_values.cap_style = CapRound;
204     gc_values.join_style = JoinRound;
205
206     gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground |
207                    GCLineWidth | GCLineStyle | GCCapStyle | GCJoinStyle;
208     new_window->line_gc[i] =
209       XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
210   }
211
212   return new_window;
213 }
214
215 void X11ZoomBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap)
216 {
217 #if defined(TARGET_ALLEGRO)
218   AllegroZoomBitmap(src_bitmap->drawable, dst_bitmap->drawable,
219                     src_bitmap->width, src_bitmap->height,
220                     dst_bitmap->width, dst_bitmap->height);
221 #else
222   ZoomPixmap(display, src_bitmap->gc,
223              src_bitmap->drawable, dst_bitmap->drawable,
224              src_bitmap->width, src_bitmap->height,
225              dst_bitmap->width, dst_bitmap->height);
226 #endif
227 }
228
229 static void SetImageDimensions(Bitmap *bitmap)
230 {
231 #if defined(TARGET_ALLEGRO)
232   BITMAP *allegro_bitmap = (BITMAP *)(bitmap->drawable);
233
234   bitmap->width  = allegro_bitmap->w;
235   bitmap->height = allegro_bitmap->h;
236 #else
237   Window root;
238   int x, y;
239   unsigned int border_width, depth;
240
241   XGetGeometry(display, bitmap->drawable, &root, &x, &y,
242                &bitmap->width, &bitmap->height, &border_width, &depth);
243 #endif
244 }
245
246 Bitmap *X11LoadImage(char *filename)
247 {
248   Bitmap *new_bitmap = CreateBitmapStruct();
249   char *error = "Read_PCX_to_Pixmap(): %s '%s'";
250   int pcx_err;
251   XGCValues clip_gc_values;
252   unsigned long clip_gc_valuemask;
253
254   pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
255                                &new_bitmap->drawable, &new_bitmap->clip_mask);
256   switch(pcx_err)
257   {
258     case PCX_Success:
259       break;
260     case PCX_OpenFailed:
261       SetError(error, "cannot open PCX file", filename);
262       return NULL;
263     case PCX_ReadFailed:
264       SetError(error, "cannot read PCX file", filename);
265       return NULL;
266     case PCX_FileInvalid:
267       SetError(error, "invalid PCX file", filename);
268       return NULL;
269     case PCX_NoMemory:
270       SetError(error, "not enough memory for PCX file", filename);
271       return NULL;
272     case PCX_ColorFailed:
273       SetError(error, "cannot get colors for PCX file", filename);
274       return NULL;
275     case PCX_OtherError:
276       /* this should already have called SetError() */
277       return NULL;
278     default:
279       SetError(error, "unknown error reading PCX file", filename);
280       return NULL;
281   }
282
283   if (!new_bitmap->drawable)
284   {
285     SetError("X11LoadImage(): cannot get graphics for '%s'", filename);
286     return NULL;
287   }
288
289   if (!new_bitmap->clip_mask)
290   {
291     SetError("X11LoadImage(): cannot get clipmask for '%s'", filename);
292     return NULL;
293   }
294
295   clip_gc_values.graphics_exposures = False;
296   clip_gc_values.clip_mask = new_bitmap->clip_mask;
297   clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
298   new_bitmap->stored_clip_gc = XCreateGC(display, window->drawable,
299                                          clip_gc_valuemask, &clip_gc_values);
300
301   /* set GraphicContext inheritated from Window */
302   new_bitmap->gc = window->gc;
303
304   /* set image width and height */
305   SetImageDimensions(new_bitmap);
306
307   return new_bitmap;
308 }
309
310 void X11CreateBitmapContent(Bitmap *new_bitmap,
311                             int width, int height, int depth)
312 {
313   Pixmap pixmap;
314
315   if ((pixmap = XCreatePixmap(display, window->drawable, width, height, depth))
316       == None)
317     Error(ERR_EXIT, "cannot create pixmap");
318
319   new_bitmap->drawable = pixmap;
320
321   if (window == NULL)
322     Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
323
324   new_bitmap->gc = window->gc;
325
326   new_bitmap->line_gc[0] = window->line_gc[0];
327   new_bitmap->line_gc[1] = window->line_gc[1];
328 }
329
330 void X11FreeBitmapPointers(Bitmap *bitmap)
331 {
332   /* The X11 version seems to have a memory leak here -- although
333      "XFreePixmap()" is called, the corresponding memory seems not
334      to be freed (according to "ps"). The SDL version apparently
335      does not have this problem. */
336
337   if (bitmap->drawable)
338     XFreePixmap(display, bitmap->drawable);
339   if (bitmap->clip_mask)
340     XFreePixmap(display, bitmap->clip_mask);
341   if (bitmap->stored_clip_gc)
342     XFreeGC(display, bitmap->stored_clip_gc);
343   /* the other GCs are only pointers to GCs used elsewhere */
344   bitmap->drawable = None;
345   bitmap->clip_mask = None;
346   bitmap->stored_clip_gc = None;
347 }
348
349 void X11CopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
350                  int src_x, int src_y, int width, int height,
351                  int dst_x, int dst_y, int mask_mode)
352 {
353   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
354             (mask_mode == BLIT_MASKED ? src_bitmap->clip_gc : dst_bitmap->gc),
355             src_x, src_y, width, height, dst_x, dst_y);
356 }
357
358 void X11FillRectangle(Bitmap *bitmap, int x, int y,
359                       int width, int height, Pixel color)
360 {
361   XSetForeground(display, bitmap->gc, color);
362   XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
363 }
364
365 void X11FadeRectangle(Bitmap *bitmap_cross, int x, int y, int width, int height,
366                       int fade_mode, int fade_delay, int post_delay,
367                       void (*draw_border_function)(void))
368 {
369   /* fading currently not supported -- simply copy backbuffer to screen */
370
371   if (fade_mode == FADE_MODE_FADE_OUT)
372     X11FillRectangle(backbuffer, x, y, width, height, BLACK_PIXEL);
373
374   if (draw_border_function != NULL)
375     draw_border_function();
376
377   X11CopyArea(backbuffer, window, x, y, width, height, x, y, BLIT_OPAQUE);
378
379   /* as we currently cannot use the fade delay, also do not use post delay */
380 }
381
382 void X11DrawSimpleLine(Bitmap *bitmap, int from_x, int from_y,
383                        int to_x, int to_y, Pixel color)
384 {
385   XSetForeground(display, bitmap->gc, color);
386   XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
387 }
388
389 Pixel X11GetPixel(Bitmap *bitmap, int x, int y)
390 {
391   XImage *pixel_image;
392   Pixel pixel_value;
393
394   pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
395                           AllPlanes, ZPixmap);
396   pixel_value = XGetPixel(pixel_image, 0, 0);
397
398   X11DestroyImage(pixel_image);
399
400   return pixel_value;
401 }
402
403 #if defined(TARGET_X11_NATIVE)
404 Pixel X11GetPixelFromRGB(unsigned int color_r, unsigned int color_g,
405                          unsigned int color_b)
406 {
407   XColor xcolor;
408   Pixel pixel;
409
410   xcolor.flags = DoRed | DoGreen | DoBlue;
411   xcolor.red = (color_r << 8);
412   xcolor.green = (color_g << 8);
413   xcolor.blue = (color_b << 8);
414
415   XAllocColor(display, cmap, &xcolor);
416   pixel = xcolor.pixel;
417
418   return pixel;
419 }
420 #endif  /* TARGET_X11_NATIVE */
421
422 void X11DestroyImage(XImage *ximage)
423 {
424 #if defined(TARGET_X11_NATIVE)
425   /* this seems to be needed for OS/2, but does not hurt on other platforms */
426   if (ximage->data != NULL)
427   {
428     free(ximage->data);
429     ximage->data = NULL;
430   }
431 #endif  /* TARGET_X11_NATIVE */
432
433   XDestroyImage(ximage);
434 }
435
436
437 /* ------------------------------------------------------------------------- */
438 /* mouse pointer functions                                                   */
439 /* ------------------------------------------------------------------------- */
440
441 #if defined(TARGET_X11_NATIVE)
442
443 static Cursor create_cursor(struct MouseCursorInfo *cursor_info)
444 {
445   Pixmap pixmap_data, pixmap_mask;
446   XColor color_fg, color_bg;
447   Cursor cursor;
448
449   /* shape and mask are single plane pixmaps */
450   pixmap_data =
451     XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->data,
452                                 cursor_info->width, cursor_info->height,
453                                 1, 0, 1);
454   pixmap_mask =
455     XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->mask,
456                                 cursor_info->width, cursor_info->height,
457                                 1, 0, 1);
458
459   XParseColor(display, cmap, "black", &color_fg);
460   XParseColor(display, cmap, "white", &color_bg);
461
462   cursor = XCreatePixmapCursor(display, pixmap_data, pixmap_mask,
463                                &color_fg, &color_bg,
464                                cursor_info->hot_x, cursor_info->hot_y);
465
466   return cursor;
467 }
468
469 void X11SetMouseCursor(struct MouseCursorInfo *cursor_info)
470 {
471   static struct MouseCursorInfo *last_cursor_info = NULL;
472   static Cursor cursor_default = None;
473   static Cursor cursor_current = None;
474
475   if (cursor_info != NULL && cursor_info != last_cursor_info)
476   {
477     cursor_current = create_cursor(cursor_info);
478     last_cursor_info = cursor_info;
479   }
480
481   XDefineCursor(display, window->drawable,
482                 cursor_info ? cursor_current : cursor_default);
483 }
484 #endif  /* TARGET_X11_NATIVE */
485
486 #endif /* TARGET_X11 */