1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
20 #if defined(TARGET_X11)
22 static void X11InitDisplay();
23 static DrawWindow *X11InitWindow();
25 void X11InitVideoDisplay(void)
27 /* initialize X11 video */
30 /* set default X11 depth */
31 video.default_depth = XDefaultDepth(display, screen);
34 void X11InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window)
36 *window = X11InitWindow();
38 XMapWindow(display, (*window)->drawable);
41 /* create additional (off-screen) buffer for double-buffering */
42 *backbuffer = CreateBitmap(video.width, video.height, video.depth);
45 static void X11InitDisplay()
47 #if !defined(PLATFORM_MSDOS)
48 XVisualInfo vinfo_template, *vinfo;
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));
58 screen = DefaultScreen(display);
59 visual = DefaultVisual(display, screen);
60 depth = DefaultDepth(display, screen);
61 cmap = DefaultColormap(display, screen);
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)))
71 visual = vinfo->visual;
75 /* got appropriate visual? */
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 */
85 static DrawWindow *X11InitWindow()
87 DrawWindow *new_window = CreateBitmapStruct();
88 unsigned int border_width = 4;
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;
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;
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;
111 screen_width = XDisplayWidth(display, screen);
112 screen_height = XDisplayHeight(display, screen);
114 win_xpos = (screen_width - width) / 2;
115 win_ypos = (screen_height - height) / 2;
117 new_window->drawable = XCreateSimpleWindow(display,
118 RootWindow(display, screen),
120 width, height, border_width,
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);
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);
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);
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;
148 if (win_xpos || win_ypos)
150 size_hints.x = win_xpos;
151 size_hints.y = win_ypos;
152 size_hints.flags |= PPosition;
155 if (!XStringListToTextProperty(&window_name, 1, &windowName))
156 Error(ERR_EXIT, "structure allocation for windowName failed");
158 if (!XStringListToTextProperty(&icon_name, 1, &iconName))
159 Error(ERR_EXIT, "structure allocation for iconName failed");
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;
167 class_hints.res_name = program.command_basename;
168 class_hints.res_class = program.program_title;
170 XSetWMProperties(display, new_window->drawable, &windowName, &iconName,
171 NULL, 0, &size_hints, &wm_hints,
174 XFree(windowName.value);
175 XFree(iconName.value);
177 /* Select event types wanted */
179 ExposureMask | StructureNotifyMask | FocusChangeMask |
180 ButtonPressMask | ButtonReleaseMask |
181 PointerMotionMask | PointerMotionHintMask |
182 KeyPressMask | KeyReleaseMask;
184 XSelectInput(display, new_window->drawable, window_event_mask);
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;
193 XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
195 /* create GCs for line drawing (black and white) */
196 for (i = 0; i < 2; i++)
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;
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);
215 void X11ZoomBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap)
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);
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);
229 static void SetImageDimensions(Bitmap *bitmap)
231 #if defined(TARGET_ALLEGRO)
232 BITMAP *allegro_bitmap = (BITMAP *)(bitmap->drawable);
234 bitmap->width = allegro_bitmap->w;
235 bitmap->height = allegro_bitmap->h;
239 unsigned int border_width, depth;
241 XGetGeometry(display, bitmap->drawable, &root, &x, &y,
242 &bitmap->width, &bitmap->height, &border_width, &depth);
246 Bitmap *X11LoadImage(char *filename)
248 Bitmap *new_bitmap = CreateBitmapStruct();
249 char *error = "Read_PCX_to_Pixmap(): %s '%s'";
251 XGCValues clip_gc_values;
252 unsigned long clip_gc_valuemask;
254 pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
255 &new_bitmap->drawable, &new_bitmap->clip_mask);
261 SetError(error, "cannot open PCX file", filename);
264 SetError(error, "cannot read PCX file", filename);
266 case PCX_FileInvalid:
267 SetError(error, "invalid PCX file", filename);
270 SetError(error, "not enough memory for PCX file", filename);
272 case PCX_ColorFailed:
273 SetError(error, "cannot get colors for PCX file", filename);
276 /* this should already have called SetError() */
279 SetError(error, "unknown error reading PCX file", filename);
283 if (!new_bitmap->drawable)
285 SetError("X11LoadImage(): cannot get graphics for '%s'", filename);
289 if (!new_bitmap->clip_mask)
291 SetError("X11LoadImage(): cannot get clipmask for '%s'", filename);
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);
301 /* set GraphicContext inheritated from Window */
302 new_bitmap->gc = window->gc;
304 /* set image width and height */
305 SetImageDimensions(new_bitmap);
310 void X11CreateBitmapContent(Bitmap *new_bitmap,
311 int width, int height, int depth)
315 if ((pixmap = XCreatePixmap(display, window->drawable, width, height, depth))
317 Error(ERR_EXIT, "cannot create pixmap");
319 new_bitmap->drawable = pixmap;
322 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
324 new_bitmap->gc = window->gc;
326 new_bitmap->line_gc[0] = window->line_gc[0];
327 new_bitmap->line_gc[1] = window->line_gc[1];
330 void X11FreeBitmapPointers(Bitmap *bitmap)
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. */
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;
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)
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);
358 void X11FillRectangle(Bitmap *bitmap, int x, int y,
359 int width, int height, Pixel color)
361 XSetForeground(display, bitmap->gc, color);
362 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
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))
369 /* fading currently not supported -- simply copy backbuffer to screen */
371 if (fade_mode == FADE_MODE_FADE_OUT)
372 X11FillRectangle(backbuffer, x, y, width, height, BLACK_PIXEL);
374 if (draw_border_function != NULL)
375 draw_border_function();
377 X11CopyArea(backbuffer, window, x, y, width, height, x, y, BLIT_OPAQUE);
379 /* as we currently cannot use the fade delay, also do not use post delay */
382 void X11DrawSimpleLine(Bitmap *bitmap, int from_x, int from_y,
383 int to_x, int to_y, Pixel color)
385 XSetForeground(display, bitmap->gc, color);
386 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
389 Pixel X11GetPixel(Bitmap *bitmap, int x, int y)
394 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
396 pixel_value = XGetPixel(pixel_image, 0, 0);
398 X11DestroyImage(pixel_image);
403 #if defined(TARGET_X11_NATIVE)
404 Pixel X11GetPixelFromRGB(unsigned int color_r, unsigned int color_g,
405 unsigned int color_b)
410 xcolor.flags = DoRed | DoGreen | DoBlue;
411 xcolor.red = (color_r << 8);
412 xcolor.green = (color_g << 8);
413 xcolor.blue = (color_b << 8);
415 XAllocColor(display, cmap, &xcolor);
416 pixel = xcolor.pixel;
420 #endif /* TARGET_X11_NATIVE */
422 void X11DestroyImage(XImage *ximage)
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)
431 #endif /* TARGET_X11_NATIVE */
433 XDestroyImage(ximage);
437 /* ------------------------------------------------------------------------- */
438 /* mouse pointer functions */
439 /* ------------------------------------------------------------------------- */
441 #if defined(TARGET_X11_NATIVE)
443 static Cursor create_cursor(struct MouseCursorInfo *cursor_info)
445 Pixmap pixmap_data, pixmap_mask;
446 XColor color_fg, color_bg;
449 /* shape and mask are single plane pixmaps */
451 XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->data,
452 cursor_info->width, cursor_info->height,
455 XCreatePixmapFromBitmapData(display, window->drawable, cursor_info->mask,
456 cursor_info->width, cursor_info->height,
459 XParseColor(display, cmap, "black", &color_fg);
460 XParseColor(display, cmap, "white", &color_bg);
462 cursor = XCreatePixmapCursor(display, pixmap_data, pixmap_mask,
463 &color_fg, &color_bg,
464 cursor_info->hot_x, cursor_info->hot_y);
469 void X11SetMouseCursor(struct MouseCursorInfo *cursor_info)
471 static struct MouseCursorInfo *last_cursor_info = NULL;
472 static Cursor cursor_default = None;
473 static Cursor cursor_current = None;
475 if (cursor_info != NULL && cursor_info != last_cursor_info)
477 cursor_current = create_cursor(cursor_info);
478 last_cursor_info = cursor_info;
481 XDefineCursor(display, window->drawable,
482 cursor_info ? cursor_current : cursor_default);
484 #endif /* TARGET_X11_NATIVE */
486 #endif /* TARGET_X11 */