1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2001 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
19 #if defined(TARGET_X11)
21 static void X11InitDisplay();
22 static DrawWindow *X11InitWindow();
24 inline void X11InitVideoDisplay(void)
26 /* initialize X11 video */
29 /* set default X11 depth */
30 video.default_depth = XDefaultDepth(display, screen);
33 inline void X11InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window)
35 *window = X11InitWindow();
37 XMapWindow(display, (*window)->drawable);
40 /* create additional (off-screen) buffer for double-buffering */
41 *backbuffer = CreateBitmap(video.width, video.height, video.depth);
44 static void X11InitDisplay()
46 #if !defined(PLATFORM_MSDOS)
47 XVisualInfo vinfo_template, *vinfo;
52 /* connect to X server */
53 if (!(display = XOpenDisplay(options.display_name)))
54 Error(ERR_EXIT, "cannot connect to X server %s",
55 XDisplayName(options.display_name));
57 screen = DefaultScreen(display);
58 visual = DefaultVisual(display, screen);
59 depth = DefaultDepth(display, screen);
60 cmap = DefaultColormap(display, screen);
62 #if !defined(PLATFORM_MSDOS)
63 /* look for good enough visual */
64 vinfo_template.screen = screen;
65 vinfo_template.class = (depth == 8 ? PseudoColor : TrueColor);
66 vinfo_template.depth = depth;
67 if ((vinfo = XGetVisualInfo(display, VisualScreenMask | VisualClassMask |
68 VisualDepthMask, &vinfo_template, &num_visuals)))
70 visual = vinfo->visual;
74 /* got appropriate visual? */
76 Error(ERR_EXIT, "X11 display not supported (less than 8 bits per pixel)");
77 else if ((depth ==8 && visual->class != PseudoColor) ||
78 (depth > 8 && visual->class != TrueColor &&
79 visual->class != DirectColor))
80 Error(ERR_EXIT, "X11 display not supported (inappropriate visual)");
81 #endif /* !PLATFORM_MSDOS */
84 static DrawWindow *X11InitWindow()
86 DrawWindow *new_window = CreateBitmapStruct();
87 unsigned int border_width = 4;
89 unsigned long gc_valuemask;
90 #if !defined(PLATFORM_MSDOS)
91 XTextProperty windowName, iconName;
92 Pixmap icon_pixmap, iconmask_pixmap;
93 unsigned int icon_width, icon_height;
94 int icon_hot_x, icon_hot_y;
96 char icon_filename[256];
98 XSizeHints size_hints;
100 XClassHint class_hints;
101 char *window_name = program.window_title;
102 char *icon_name = program.window_title;
103 long window_event_mask;
104 Atom proto_atom = None, delete_atom = None;
106 int screen_width, screen_height;
107 int win_xpos, win_ypos;
108 unsigned long pen_fg = WhitePixel(display,screen);
109 unsigned long pen_bg = BlackPixel(display,screen);
110 const int width = video.width, height = video.height;
114 #if !defined(PLATFORM_MSDOS)
115 static struct IconFileInfo icon_pic =
123 screen_width = XDisplayWidth(display, screen);
124 screen_height = XDisplayHeight(display, screen);
126 win_xpos = (screen_width - width) / 2;
127 win_ypos = (screen_height - height) / 2;
129 new_window->drawable = XCreateSimpleWindow(display,
130 RootWindow(display, screen),
132 width, height, border_width,
135 #if !defined(PLATFORM_MSDOS)
136 proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
137 delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
138 if ((proto_atom != None) && (delete_atom != None))
139 XChangeProperty(display, new_window->drawable, proto_atom, XA_ATOM, 32,
140 PropModePrepend, (unsigned char *) &delete_atom, 1);
143 sprintf(icon_filename, "%s/%s", options.graphics_directory,
144 icon_pic.picture_filename);
146 if (XReadBitmapFile(display, new_window->drawable,
147 program.x11_icon_filename,
148 &icon_width, &icon_height, &icon_pixmap,
149 &icon_hot_x, &icon_hot_y) != BitmapSuccess)
150 Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
151 program.x11_icon_filename);
154 sprintf(icon_filename, "%s/%s", options.graphics_directory,
155 icon_pic.picturemask_filename);
157 if (XReadBitmapFile(display, new_window->drawable,
158 program.x11_iconmask_filename,
159 &icon_width, &icon_height, &iconmask_pixmap,
160 &icon_hot_x, &icon_hot_y) != BitmapSuccess)
161 Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
162 program.x11_iconmask_filename);
164 size_hints.width = size_hints.min_width = size_hints.max_width = width;
165 size_hints.height = size_hints.min_height = size_hints.max_height = height;
166 size_hints.flags = PSize | PMinSize | PMaxSize;
168 if (win_xpos || win_ypos)
170 size_hints.x = win_xpos;
171 size_hints.y = win_ypos;
172 size_hints.flags |= PPosition;
175 if (!XStringListToTextProperty(&window_name, 1, &windowName))
176 Error(ERR_EXIT, "structure allocation for windowName failed");
178 if (!XStringListToTextProperty(&icon_name, 1, &iconName))
179 Error(ERR_EXIT, "structure allocation for iconName failed");
181 wm_hints.initial_state = NormalState;
182 wm_hints.input = True;
183 wm_hints.icon_pixmap = icon_pixmap;
184 wm_hints.icon_mask = iconmask_pixmap;
185 wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
187 class_hints.res_name = program.command_basename;
188 class_hints.res_class = program.program_title;
190 XSetWMProperties(display, new_window->drawable, &windowName, &iconName,
191 NULL, 0, &size_hints, &wm_hints,
194 XFree(windowName.value);
195 XFree(iconName.value);
197 /* Select event types wanted */
199 ExposureMask | StructureNotifyMask | FocusChangeMask |
200 ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
201 PointerMotionHintMask | KeyPressMask | KeyReleaseMask;
203 XSelectInput(display, new_window->drawable, window_event_mask);
206 /* create GC for drawing with window depth and background color (black) */
207 gc_values.graphics_exposures = False;
208 gc_values.foreground = pen_bg;
209 gc_values.background = pen_bg;
210 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
212 XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
214 /* create GCs for line drawing (black and white) */
217 gc_values.graphics_exposures = False;
218 gc_values.foreground = (i ? pen_fg : pen_bg);
219 gc_values.background = pen_bg;
220 gc_values.line_width = 4;
221 gc_values.line_style = LineSolid;
222 gc_values.cap_style = CapRound;
223 gc_values.join_style = JoinRound;
225 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground |
226 GCLineWidth | GCLineStyle | GCCapStyle | GCJoinStyle;
227 new_window->line_gc[i] =
228 XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
234 Bitmap *X11LoadImage(char *filename)
236 Bitmap *new_bitmap = CreateBitmapStruct();
239 #if defined(PLATFORM_MSDOS)
243 pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
244 &new_bitmap->drawable, &new_bitmap->clip_mask);
250 Error(ERR_EXIT, "cannot open PCX file '%s'", filename);
252 Error(ERR_EXIT, "cannot read PCX file '%s'", filename);
253 case PCX_FileInvalid:
254 Error(ERR_EXIT, "invalid PCX file '%s'", filename);
256 Error(ERR_EXIT, "not enough memory for PCX file '%s'", filename);
257 case PCX_ColorFailed:
258 Error(ERR_EXIT, "cannot get colors for PCX file '%s'", filename);
263 if (!new_bitmap->drawable)
264 Error(ERR_EXIT, "cannot get graphics for '%s'", filename);
266 if (!new_bitmap->clip_mask)
267 Error(ERR_EXIT, "cannot get clipmask for '%s'", filename);
269 /* set GraphicContext inheritated from Window */
270 new_bitmap->gc = window->gc;
275 #endif /* TARGET_X11 */