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