rnd-20010101-1-src
[rocksndiamonds.git] / src / libgame / x11.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2000 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
18
19 #if defined(TARGET_X11)
20
21 static void X11InitDisplay();
22 static DrawWindow *X11InitWindow();
23
24 inline void X11InitVideoDisplay(void)
25 {
26   /* initialize X11 video */
27   X11InitDisplay();
28
29   /* set default X11 depth */
30   video.default_depth = XDefaultDepth(display, screen);
31 }
32
33 inline void X11InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window)
34 {
35   *window = X11InitWindow();
36
37   XMapWindow(display, (*window)->drawable);
38   FlushDisplay();
39
40   /* create additional (off-screen) buffer for double-buffering */
41   *backbuffer = CreateBitmap(video.width, video.height, video.depth);
42 }
43
44 static void X11InitDisplay()
45 {
46 #if !defined(PLATFORM_MSDOS)
47   XVisualInfo vinfo_template, *vinfo;
48   int num_visuals;
49 #endif
50   unsigned int depth;
51
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));
56
57   screen = DefaultScreen(display);
58   visual = DefaultVisual(display, screen);
59   depth  = DefaultDepth(display, screen);
60   cmap   = DefaultColormap(display, screen);
61
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)))
69   {
70     visual = vinfo->visual;
71     XFree((void *)vinfo);
72   }
73
74   /* got appropriate visual? */
75   if (depth < 8)
76   {
77     printf("Sorry, displays with less than 8 bits per pixel not supported.\n");
78     exit(-1);
79   }
80   else if ((depth ==8 && visual->class != PseudoColor) ||
81            (depth > 8 && visual->class != TrueColor &&
82             visual->class != DirectColor))
83   {
84     printf("Sorry, cannot get appropriate visual.\n");
85     exit(-1);
86   }
87 #endif /* !PLATFORM_MSDOS */
88 }
89
90 static DrawWindow *X11InitWindow()
91 {
92   DrawWindow *new_window = CreateBitmapStruct();
93   unsigned int border_width = 4;
94   XGCValues gc_values;
95   unsigned long gc_valuemask;
96 #if !defined(PLATFORM_MSDOS)
97   XTextProperty windowName, iconName;
98   Pixmap icon_pixmap, iconmask_pixmap;
99   unsigned int icon_width, icon_height;
100   int icon_hot_x, icon_hot_y;
101 #if 0
102   char icon_filename[256];
103 #endif
104   XSizeHints size_hints;
105   XWMHints wm_hints;
106   XClassHint class_hints;
107   char *window_name = program.window_title;
108   char *icon_name = program.window_title;
109   long window_event_mask;
110   Atom proto_atom = None, delete_atom = None;
111 #endif
112   int screen_width, screen_height;
113   int win_xpos, win_ypos;
114   unsigned long pen_fg = WhitePixel(display,screen);
115   unsigned long pen_bg = BlackPixel(display,screen);
116   const int width = video.width, height = video.height;
117   int i;
118
119 #if 0
120 #if !defined(PLATFORM_MSDOS)
121   static struct IconFileInfo icon_pic =
122   {
123     "rocks_icon.xbm",
124     "rocks_iconmask.xbm"
125   };
126 #endif
127 #endif
128
129   screen_width = XDisplayWidth(display, screen);
130   screen_height = XDisplayHeight(display, screen);
131
132   win_xpos = (screen_width - width) / 2;
133   win_ypos = (screen_height - height) / 2;
134
135   new_window->drawable = XCreateSimpleWindow(display,
136                                              RootWindow(display, screen),
137                                              win_xpos, win_ypos,
138                                              width, height, border_width,
139                                              pen_fg, pen_bg);
140
141 #if !defined(PLATFORM_MSDOS)
142   proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
143   delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
144   if ((proto_atom != None) && (delete_atom != None))
145     XChangeProperty(display, new_window->drawable, proto_atom, XA_ATOM, 32,
146                     PropModePrepend, (unsigned char *) &delete_atom, 1);
147
148 #if 0
149   sprintf(icon_filename, "%s/%s/%s",
150           options.ro_base_directory, GRAPHICS_DIRECTORY,
151           icon_pic.picture_filename);
152 #endif
153   if (XReadBitmapFile(display, new_window->drawable,
154                       program.x11_icon_filename,
155                       &icon_width, &icon_height, &icon_pixmap,
156                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
157     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
158           program.x11_icon_filename);
159
160 #if 0
161   sprintf(icon_filename, "%s/%s/%s",
162           options.ro_base_directory, GRAPHICS_DIRECTORY,
163           icon_pic.picturemask_filename);
164 #endif
165   if (XReadBitmapFile(display, new_window->drawable,
166                       program.x11_iconmask_filename,
167                       &icon_width, &icon_height, &iconmask_pixmap,
168                       &icon_hot_x, &icon_hot_y) != BitmapSuccess)
169     Error(ERR_EXIT, "cannot read icon bitmap file '%s'",
170           program.x11_iconmask_filename);
171
172   size_hints.width  = size_hints.min_width  = size_hints.max_width  = width;
173   size_hints.height = size_hints.min_height = size_hints.max_height = height;
174   size_hints.flags = PSize | PMinSize | PMaxSize;
175
176   if (win_xpos || win_ypos)
177   {
178     size_hints.x = win_xpos;
179     size_hints.y = win_ypos;
180     size_hints.flags |= PPosition;
181   }
182
183   if (!XStringListToTextProperty(&window_name, 1, &windowName))
184     Error(ERR_EXIT, "structure allocation for windowName failed");
185
186   if (!XStringListToTextProperty(&icon_name, 1, &iconName))
187     Error(ERR_EXIT, "structure allocation for iconName failed");
188
189   wm_hints.initial_state = NormalState;
190   wm_hints.input = True;
191   wm_hints.icon_pixmap = icon_pixmap;
192   wm_hints.icon_mask = iconmask_pixmap;
193   wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
194
195   class_hints.res_name = program.command_basename;
196   class_hints.res_class = program.program_title;
197
198   XSetWMProperties(display, new_window->drawable, &windowName, &iconName, 
199                    NULL, 0, &size_hints, &wm_hints, 
200                    &class_hints);
201
202   XFree(windowName.value);
203   XFree(iconName.value);
204
205   /* Select event types wanted */
206   window_event_mask =
207     ExposureMask | StructureNotifyMask | FocusChangeMask |
208     ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
209     PointerMotionHintMask | KeyPressMask | KeyReleaseMask;
210
211   XSelectInput(display, new_window->drawable, window_event_mask);
212 #endif
213
214   /* create GC for drawing with window depth and background color (black) */
215   gc_values.graphics_exposures = False;
216   gc_values.foreground = pen_bg;
217   gc_values.background = pen_bg;
218   gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
219   new_window->gc =
220     XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
221
222   /* create GCs for line drawing (black and white) */
223   for(i=0; i<2; i++)
224   {
225     gc_values.graphics_exposures = False;
226     gc_values.foreground = (i ? pen_fg : pen_bg);
227     gc_values.background = pen_bg;
228     gc_values.line_width = 4;
229     gc_values.line_style = LineSolid;
230     gc_values.cap_style = CapRound;
231     gc_values.join_style = JoinRound;
232
233     gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground |
234                    GCLineWidth | GCLineStyle | GCCapStyle | GCJoinStyle;
235     new_window->line_gc[i] =
236       XCreateGC(display, new_window->drawable, gc_valuemask, &gc_values);
237   }
238
239   return new_window;
240 }
241
242 Bitmap *X11LoadImage(char *filename)
243 {
244   Bitmap *new_bitmap = CreateBitmapStruct();
245   int pcx_err;
246
247 #if defined(PLATFORM_MSDOS)
248   rest(100);
249 #endif
250
251   pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
252                                &new_bitmap->drawable, &new_bitmap->clip_mask);
253   switch(pcx_err)
254   {
255     case PCX_Success:
256       break;
257     case PCX_OpenFailed:
258       Error(ERR_EXIT, "cannot open PCX file '%s'", filename);
259     case PCX_ReadFailed:
260       Error(ERR_EXIT, "cannot read PCX file '%s'", filename);
261     case PCX_FileInvalid:
262       Error(ERR_EXIT, "invalid PCX file '%s'", filename);
263     case PCX_NoMemory:
264       Error(ERR_EXIT, "not enough memory for PCX file '%s'", filename);
265     case PCX_ColorFailed:
266       Error(ERR_EXIT, "cannot get colors for PCX file '%s'", filename);
267     default:
268       break;
269   }
270
271   if (!new_bitmap->drawable)
272     Error(ERR_EXIT, "cannot get graphics for '%s'", filename);
273
274   if (!new_bitmap->clip_mask)
275     Error(ERR_EXIT, "cannot get clipmask for '%s'", filename);
276
277   /* set GraphicContext inheritated from Window */
278   new_bitmap->gc = window->gc;
279
280   return new_bitmap;
281 }
282
283 #endif /* TARGET_X11 */