File: initdisp.c

package info (click to toggle)
xbl 1.1.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 704 kB
  • ctags: 717
  • sloc: ansic: 7,286; makefile: 273; python: 21; sh: 8
file content (210 lines) | stat: -rw-r--r-- 5,457 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
  XBlockOut a 3D Tetris

  Copyright (C) 1992,1993,1994,1997,2001  Thierry EXCOFFIER

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 1, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  Contact: Thierry.EXCOFFIER@liris.univ-lyon1.fr
*/
#include "opt.h"
#include "x.h"
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <stdio.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif


void SymCode(Display *d, char *key)
{
  int i,j,min,max ;
  KeySym *map ;
  int nbmod,nb ;

  XDisplayKeycodes(d,&min,&max) ;
  nb = max-min+1 ;
  map = XGetKeyboardMapping(d,min,nb,&nbmod) ;
  for(i=0;i<256;i++) key[i] = 0 ;
  for(i=0;i<nb;i++)
    {
      key[i+min] = 0 ;
      for(j=0;j<nbmod;j++)
	{
	  if ( map[i*nbmod+j]>= XK_KP_0 && map[i*nbmod+j]<= XK_KP_9 )
	    key[i+min] = map[i*nbmod+j] - XK_KP_0 + '0' ;
	  else
	    switch ( map[i*nbmod+j] )
	      {
	      case XK_Left : key[i+min] = '4' ; break ;
	      case XK_Right: key[i+min] = '6' ; break ;
	      case XK_Up   : key[i+min] = '8' ; break ;
	      case XK_Down : key[i+min] = '2' ; break ;
	      }
	}
    }
}

void initdisp(struct opt *opt, struct x *x)
{
  int i ;
  char *name ;

  if ( opt->displayname[0]!='\0' )
    x->display = XOpenDisplay(opt->displayname) ;
  if ( x->display==0 )
    {
      name = getenv("DISPLAY") ;
      if ( name==0 )
	{
	  fprintf(stderr,"Xbl is an X11 game\n") ;
	  fprintf(stderr,"And your DISPLAY variable doesn't exist\n") ;
	  fprintf(stderr,"Goodbye\n") ;
	  exit(1) ;
	}
      STRCPY_SAFE(opt->displayname,name) ;
      x->display = XOpenDisplay(opt->displayname) ;
    }
  if ( x->display==0 )
    {
      fprintf(stderr,"Cannot open display\n");
      exit(1) ;
    }
  x->screen = DefaultScreen( x->display ) ;
  i = strlen(opt->displayname) ;
  if ( opt->displayname[i-2]=='.' )
    {
      x->screen = opt->displayname[i-1] - '0' ;
    }
  if ( x->screen>=ScreenCount(x->display) )
    {
      /* This case is impossible XOpenDisplay change the string */
      fprintf(stderr,"You give an invalide screen number\n") ;
      exit(1) ;
    }
  x->root        = RootWindow( x->display,x->screen ) ;
  x->depth       = DefaultDepth(x->display,x->screen);
}

void initdisp2(struct opt *opt, struct x *x)
{
  XVisualInfo viproto;                /* fill in for getting info */
  XVisualInfo *vip;                   /* retured info */	
  int nbv ;		
  int i ;

  x->visual      = DefaultVisual(x->display,x->screen) ;
  x->colormap    = DefaultColormap( x->display, x->screen) ;

  if ( opt->verbose )
    {
      fprintf(stderr,"DEFAULT Colormap %ld visual %p\n",x->colormap, x->visual) ;
    }
  /*-----------------------------------------*/
  /* Find the PseudoColor with maximum depth */
  /* It's obviously not a good method        */
  /*-----------------------------------------*/
  if ( opt->visual )
    {
      int new_visual ;

      viproto.screen = x->screen ;
      viproto.class  = PseudoColor ;
      vip = XGetVisualInfo (x->display, VisualScreenMask|VisualClassMask, &viproto, &nbv);


      if ( opt->verbose )
	fprintf(stderr,"Number of Visual --------------> %d\n",nbv) ;

      new_visual = 0 ;

      for(i=0;i<nbv;i++)
	{
	  if ( opt->verbose )
	    fprintf(stderr,"PSEUDOCOLOR %d entries\n",
		    vip[i].visual->map_entries) ;
	  if ( x->visual->class != PseudoColor ||
	       vip[i].visual->map_entries > x->visual->map_entries
	       )
	    {
	      if ( opt->verbose )
		{
		  fprintf(stderr,"I USE IT VisualID %ld!!!!!\n",
			  vip[i].visual->visualid) ;
		}
	      x->visual = vip[i].visual ;
	      new_visual = 1 ;
	      if ( x->visual->map_entries>=128 ) break ; /* No more useful */
	    }
	}

      if ( new_visual )
	{
	  x->colormap = XCreateColormap( x->display, x->root, x->visual, AllocNone) ;
	  if ( opt->verbose )
	    {
	      fprintf(stderr,"Colormap %ld created\n",x->colormap) ;
	    }
	}

    }


  /*-----------------------------------------*/
  /*-----------------------------------------*/

  x->map_entries = x->visual->map_entries ;
  if ( x->map_entries&1 ) x->map_entries = (x->map_entries+1)/2 ;

  x->white_pixel = XWhitePixel(x->display,x->screen) ;
  x->black_pixel = XBlackPixel(x->display,x->screen) ;

  if ( opt->buffering==-1)
    {
      if (x->visual->class==PseudoColor || x->visual->class==GrayScale)
	{
	  if ( opt->use_bw )
	    {
	      if ( x->map_entries>=4 )
		if ( x->map_entries>=8 )
		  opt->buffering=3 ;
		else opt->buffering=1 ;
	      else opt->buffering=2 ;
	    }
	  else {
	    if ( x->map_entries>=16 )
	      if ( x->map_entries>=32 )
		opt->buffering=3 ;
	      else opt->buffering=1 ;
	    else opt->buffering=2 ;
	  }
	}
      else opt->buffering = 2 ;
      if ( opt->verbose )
	fprintf(stderr,"I choose buffering=%d\n",opt->buffering) ;
    }
  SymCode(x->display,x->codes) ;
}