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