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