rocksndiamonds-3.1.0
[rocksndiamonds.git] / src / libgame / x11.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 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 inline 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 inline 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 #if 0
97   char icon_filename[256];
98 #endif
99   XSizeHints size_hints;
100   XWMHints wm_hints;
101   XClassHint class_hints;
102   char *window_name = program.window_title;
103   char *icon_name = program.window_title;
104   long window_event_mask;
105   Atom proto_atom = None, delete_atom = None;
106 #endif
107   int screen_width, screen_height;
108   int win_xpos, win_ypos;
109   unsigned long pen_fg = WhitePixel(display,screen);
110   unsigned long pen_bg = BlackPixel(display,screen);
111   const int width = video.width, height = video.height;
112   int i;
113
114 #if 0
115 #if !defined(PLATFORM_MSDOS)
116   static struct IconFileInfo icon_pic =
117   {
118     "rocks_icon.xbm",
119     "rocks_iconmask.xbm"
120   };
121 #endif
122 #endif
123
124   screen_width = XDisplayWidth(display, screen);
125   screen_height = XDisplayHeight(display, screen);
126
127   win_xpos = (screen_width - width) / 2;
128   win_ypos = (screen_height - height) / 2;
129
130   new_window->drawable = XCreateSimpleWindow(display,
131                                              RootWindow(display, screen),
132                                              win_xpos, win_ypos,
133                                              width, height, border_width,
134                                              pen_fg, pen_bg);
135
136 #if !defined(PLATFORM_MSDOS)
137   proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
138   delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
139   if ((proto_atom != None) && (delete_atom != None))
140     XChangeProperty(display, new_window->drawable, proto_atom, XA_ATOM, 32,
141                     PropModePrepend, (unsigned char *) &delete_atom, 1);
142
143 #if 0
144   sprintf(icon_filename, "%s/%s", options.graphics_directory,
145           icon_pic.picture_filename);
146 #endif
147   if (XReadBitmapFile(display, new_window->drawable,
148                       getCustomImageFilename(program.x11_icon_filename),
149                       &icon_width, &icon_height, &icon_pixmap,
150                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
151     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
152           program.x11_icon_filename);
153
154 #if 0
155   sprintf(icon_filename, "%s/%s", options.graphics_directory,
156           icon_pic.picturemask_filename);
157 #endif
158   if (XReadBitmapFile(display, new_window->drawable,
159                       getCustomImageFilename(program.x11_iconmask_filename),
160                       &icon_width, &icon_height, &iconmask_pixmap,
161                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
162     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
163           program.x11_iconmask_filename);
164
165   size_hints.width  = size_hints.min_width  = size_hints.max_width  = width;
166   size_hints.height = size_hints.min_height = size_hints.max_height = height;
167   size_hints.flags = PSize | PMinSize | PMaxSize;
168
169   if (win_xpos || win_ypos)
170   {
171     size_hints.x = win_xpos;
172     size_hints.y = win_ypos;
173     size_hints.flags |= PPosition;
174   }
175
176   if (!XStringListToTextProperty(&window_name, 1, &windowName))
177     Error(ERR_EXIT, "structure allocation for windowName failed");
178
179   if (!XStringListToTextProperty(&icon_name, 1, &iconName))
180     Error(ERR_EXIT, "structure allocation for iconName failed");
181
182   wm_hints.initial_state = NormalState;
183   wm_hints.input = True;
184   wm_hints.icon_pixmap = icon_pixmap;
185   wm_hints.icon_mask = iconmask_pixmap;
186   wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
187
188   class_hints.res_name = program.command_basename;
189   class_hints.res_class = program.program_title;
190
191   XSetWMProperties(display, new_window->drawable, &windowName, &iconName, 
192                    NULL, 0, &size_hints, &wm_hints, 
193                    &class_hints);
194
195   XFree(windowName.value);
196   XFree(iconName.value);
197
198   /* Select event types wanted */
199   window_event_mask =
200     ExposureMask | StructureNotifyMask | FocusChangeMask |
201     ButtonPressMask | ButtonReleaseMask |
202     PointerMotionMask | PointerMotionHintMask |
203     KeyPressMask | KeyReleaseMask;
204
205   XSelectInput(display, new_window->drawable, window_event_mask);
206 #endif
207
208   /* create GC for drawing with window depth and background color (black) */
209   gc_values.graphics_exposures = False;
210   gc_values.foreground = pen_bg;
211   gc_values.background = pen_bg;
212   gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
213   new_window->gc =
214     XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
215
216   /* create GCs for line drawing (black and white) */
217   for (i = 0; i < 2; i++)
218   {
219     gc_values.graphics_exposures = False;
220     gc_values.foreground = (i ? pen_fg : pen_bg);
221     gc_values.background = pen_bg;
222     gc_values.line_width = 4;
223     gc_values.line_style = LineSolid;
224     gc_values.cap_style = CapRound;
225     gc_values.join_style = JoinRound;
226
227     gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground |
228                    GCLineWidth | GCLineStyle | GCCapStyle | GCJoinStyle;
229     new_window->line_gc[i] =
230       XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
231   }
232
233   return new_window;
234 }
235
236 void X11ZoomBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap)
237 {
238 #if defined(TARGET_ALLEGRO)
239   AllegroZoomBitmap(src_bitmap->drawable, dst_bitmap->drawable,
240                     src_bitmap->width, src_bitmap->height,
241                     dst_bitmap->width, dst_bitmap->height);
242 #else
243   ZoomPixmap(display, src_bitmap->gc,
244              src_bitmap->drawable, dst_bitmap->drawable,
245              src_bitmap->width, src_bitmap->height,
246              dst_bitmap->width, dst_bitmap->height);
247 #endif
248 }
249
250 static void SetImageDimensions(Bitmap *bitmap)
251 {
252 #if defined(TARGET_ALLEGRO)
253   BITMAP *allegro_bitmap = (BITMAP *)(bitmap->drawable);
254
255   bitmap->width  = allegro_bitmap->w;
256   bitmap->height = allegro_bitmap->h;
257 #else
258   Window root;
259   int x, y;
260   unsigned int border_width, depth;
261
262   XGetGeometry(display, bitmap->drawable, &root, &x, &y,
263                &bitmap->width, &bitmap->height, &border_width, &depth);
264 #endif
265 }
266
267 Bitmap *X11LoadImage(char *filename)
268 {
269   Bitmap *new_bitmap = CreateBitmapStruct();
270   char *error = "Read_PCX_to_Pixmap(): %s '%s'";
271   int pcx_err;
272   XGCValues clip_gc_values;
273   unsigned long clip_gc_valuemask;
274
275   pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
276                                &new_bitmap->drawable, &new_bitmap->clip_mask);
277   switch(pcx_err)
278   {
279     case PCX_Success:
280       break;
281     case PCX_OpenFailed:
282       SetError(error, "cannot open PCX file", filename);
283       return NULL;
284     case PCX_ReadFailed:
285       SetError(error, "cannot read PCX file", filename);
286       return NULL;
287     case PCX_FileInvalid:
288       SetError(error, "invalid PCX file", filename);
289       return NULL;
290     case PCX_NoMemory:
291       SetError(error, "not enough memory for PCX file", filename);
292       return NULL;
293     case PCX_ColorFailed:
294       SetError(error, "cannot get colors for PCX file", filename);
295       return NULL;
296     case PCX_OtherError:
297       /* this should already have called SetError() */
298       return NULL;
299     default:
300       SetError(error, "unknown error reading PCX file", filename);
301       return NULL;
302   }
303
304   if (!new_bitmap->drawable)
305   {
306     SetError("X11LoadImage(): cannot get graphics for '%s'", filename);
307     return NULL;
308   }
309
310   if (!new_bitmap->clip_mask)
311   {
312     SetError("X11LoadImage(): cannot get clipmask for '%s'", filename);
313     return NULL;
314   }
315
316   clip_gc_values.graphics_exposures = False;
317   clip_gc_values.clip_mask = new_bitmap->clip_mask;
318   clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
319   new_bitmap->stored_clip_gc = XCreateGC(display, window->drawable,
320                                          clip_gc_valuemask, &clip_gc_values);
321
322   /* set GraphicContext inheritated from Window */
323   new_bitmap->gc = window->gc;
324
325   /* set image width and height */
326   SetImageDimensions(new_bitmap);
327
328   return new_bitmap;
329 }
330
331 inline void X11CreateBitmapContent(Bitmap *new_bitmap,
332                                    int width, int height, int depth)
333 {
334   Pixmap pixmap;
335
336   if ((pixmap = XCreatePixmap(display, window->drawable, width, height, depth))
337       == None)
338     Error(ERR_EXIT, "cannot create pixmap");
339
340   new_bitmap->drawable = pixmap;
341
342   if (window == NULL)
343     Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
344
345   new_bitmap->gc = window->gc;
346
347   new_bitmap->line_gc[0] = window->line_gc[0];
348   new_bitmap->line_gc[1] = window->line_gc[1];
349 }
350
351 inline void X11FreeBitmapPointers(Bitmap *bitmap)
352 {
353   /* The X11 version seems to have a memory leak here -- although
354      "XFreePixmap()" is called, the corresponding memory seems not
355      to be freed (according to "ps"). The SDL version apparently
356      does not have this problem. */
357
358   if (bitmap->drawable)
359     XFreePixmap(display, bitmap->drawable);
360   if (bitmap->clip_mask)
361     XFreePixmap(display, bitmap->clip_mask);
362   if (bitmap->stored_clip_gc)
363     XFreeGC(display, bitmap->stored_clip_gc);
364   /* the other GCs are only pointers to GCs used elsewhere */
365   bitmap->drawable = None;
366   bitmap->clip_mask = None;
367   bitmap->stored_clip_gc = None;
368 }
369
370 inline void X11CopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
371                         int src_x, int src_y, int width, int height,
372                         int dst_x, int dst_y, int mask_mode)
373 {
374   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
375             (mask_mode == BLIT_MASKED ? src_bitmap->clip_gc : dst_bitmap->gc),
376             src_x, src_y, width, height, dst_x, dst_y);
377 }
378
379 inline void X11FillRectangle(Bitmap *bitmap, int x, int y,
380                              int width, int height, Pixel color)
381 {
382   XSetForeground(display, bitmap->gc, color);
383   XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
384 }
385
386 inline void X11DrawSimpleLine(Bitmap *bitmap, int from_x, int from_y,
387                               int to_x, int to_y, Pixel color)
388 {
389   XSetForeground(display, bitmap->gc, color);
390   XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
391 }
392
393 inline Pixel X11GetPixel(Bitmap *bitmap, int x, int y)
394 {
395   XImage *pixel_image;
396   Pixel pixel_value;
397
398   pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
399                           AllPlanes, ZPixmap);
400   pixel_value = XGetPixel(pixel_image, 0, 0);
401
402   X11DestroyImage(pixel_image);
403
404   return pixel_value;
405 }
406
407 #if defined(TARGET_X11_NATIVE)
408 inline Pixel X11GetPixelFromRGB(unsigned int color_r, unsigned int color_g,
409                                 unsigned int color_b)
410 {
411   XColor xcolor;
412   Pixel pixel;
413
414   xcolor.flags = DoRed | DoGreen | DoBlue;
415   xcolor.red = (color_r << 8);
416   xcolor.green = (color_g << 8);
417   xcolor.blue = (color_b << 8);
418
419   XAllocColor(display, cmap, &xcolor);
420   pixel = xcolor.pixel;
421
422   return pixel;
423 }
424 #endif  /* TARGET_X11_NATIVE */
425
426 inline void X11DestroyImage(XImage *ximage)
427 {
428 #if defined(TARGET_X11_NATIVE)
429   /* this seems to be needed for OS/2, but does not hurt on other platforms */
430   if (ximage->data != NULL)
431   {
432     free(ximage->data);
433     ximage->data = NULL;
434   }
435 #endif  /* TARGET_X11_NATIVE */
436
437   XDestroyImage(ximage);
438 }
439
440
441 /* ------------------------------------------------------------------------- */
442 /* mouse pointer functions                                                   */
443 /* ------------------------------------------------------------------------- */
444
445 #if defined(TARGET_X11_NATIVE)
446
447 static Cursor create_cursor(struct MouseCursorInfo *cursor_info)
448 {
449   Pixmap pixmap_data, pixmap_mask;
450   XColor color_fg, color_bg;
451   Cursor cursor;
452
453   /* shape and mask are single plane pixmaps */
454   pixmap_data =
455     XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->data,
456                                 cursor_info->width, cursor_info->height,
457                                 1, 0, 1);
458   pixmap_mask =
459     XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->mask,
460                                 cursor_info->width, cursor_info->height,
461                                 1, 0, 1);
462
463   XParseColor(display, cmap, "black", &color_fg);
464   XParseColor(display, cmap, "white", &color_bg);
465
466   cursor = XCreatePixmapCursor(display, pixmap_data, pixmap_mask,
467                                &color_fg, &color_bg,
468                                cursor_info->hot_x, cursor_info->hot_y);
469
470   return cursor;
471 }
472
473 void X11SetMouseCursor(struct MouseCursorInfo *cursor_info)
474 {
475   static struct MouseCursorInfo *last_cursor_info = NULL;
476   static Cursor cursor_default = None;
477   static Cursor cursor_current = None;
478
479   if (cursor_info != NULL && cursor_info != last_cursor_info)
480   {
481     cursor_current = create_cursor(cursor_info);
482     last_cursor_info = cursor_info;
483   }
484
485   XDefineCursor(display, window->drawable,
486                 cursor_info ? cursor_current : cursor_default);
487 }
488 #endif  /* TARGET_X11_NATIVE */
489
490 #endif /* TARGET_X11 */