rnd-20001203-5-src
[rocksndiamonds.git] / src / libgame / x11.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  ©1995 Artsoft Development                               *
5 *        Holger Schemel                                    *
6 *        33659 Bielefeld-Senne                             *
7 *        Telefon: (0521) 493245                            *
8 *        eMail: aeglos@valinor.owl.de                      *
9 *               aeglos@uni-paderborn.de                    *
10 *               q99492@pbhrzx.uni-paderborn.de             *
11 *----------------------------------------------------------*
12 *  x11.c                                                   *
13 ***********************************************************/
14
15 #include "system.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   char icon_filename[256];
102   XSizeHints size_hints;
103   XWMHints wm_hints;
104   XClassHint class_hints;
105   char *window_name = program.window_title;
106   char *icon_name = program.window_title;
107   long window_event_mask;
108   Atom proto_atom = None, delete_atom = None;
109 #endif
110   int screen_width, screen_height;
111   int win_xpos, win_ypos;
112   unsigned long pen_fg = WhitePixel(display,screen);
113   unsigned long pen_bg = BlackPixel(display,screen);
114   const int width = video.width, height = video.height;
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   return new_window;
218 }
219
220 #endif /* TARGET_X11 */