summaryrefslogtreecommitdiff
path: root/Build/source/libs/freetype/freetype-1.5/test/arch/unix/gwin_x11.c
blob: 965c1f60784e349721d35bf5d4eaa80093d968ce (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*******************************************************************
 *
 *  gwin_x11.c  graphics utility X-Window driver.              1.0
 *
 *  This is the driver for windowed display under X11, used by the
 *  graphics utility of the FreeType test suite.
 *
 *  Copyright 1996-2000 by
 *  David Turner, Robert Wilhelm, and Werner Lemberg.
 *
 *  This file is part of the FreeType project, and may only be used
 *  modified and distributed under the terms of the FreeType project
 *  license, LICENSE.TXT. By continuing to use, modify or distribute 
 *  this file you indicate that you have read the license and
 *  understand and accept it fully.
 *
 ******************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>

#include "common.h"     /* for Panic() */
#include "freetype.h"

#include "gdriver.h"
#include "gevents.h"
#include "gmain.h"


  /* Translator added to ease changes to control keys */

  typedef struct  _Translator
  {
    char    key;
    GEvent  event_class;
    int     event_info;
  } Translator;

#define NUM_Translators  20

  static const Translator  trans[NUM_Translators] =
  {
    { (char)27, event_Quit,              0 },
    { 'q',      event_Quit,              0 },

    { 'x',      event_Rotate_Glyph,     -1 },
    { 'c',      event_Rotate_Glyph,      1 },
    { 'v',      event_Rotate_Glyph,    -16 },
    { 'b',      event_Rotate_Glyph,     16 },

    { '{',      event_Change_Glyph, -10000 },
    { '}',      event_Change_Glyph,  10000 },
    { '(',      event_Change_Glyph,  -1000 },
    { ')',      event_Change_Glyph,   1000 },
    { '9',      event_Change_Glyph,   -100 },
    { '0',      event_Change_Glyph,    100 },
    { 'i',      event_Change_Glyph,    -10 },
    { 'o',      event_Change_Glyph,     10 },
    { 'k',      event_Change_Glyph,     -1 },
    { 'l',      event_Change_Glyph,      1 },

    { '+',      event_Scale_Glyph,      10 },
    { '-',      event_Scale_Glyph,     -10 },
    { 'u',      event_Scale_Glyph,       1 },
    { 'j',      event_Scale_Glyph,      -1 }
  };

  /* End of translators addition. See Get_Event() below */

  extern char      Header[];    /* defined in the test programs */

  static Window    win;
  static GC        gcblack;
  static XColor    color[5];

  Display*         display;
  static char*     displayname = "";

  static XImage*   image;

  static Visual*   visual;
  static Colormap  colormap;
  static int       depth;
  static Bool      gray;

  static Cursor    idle;
  static Cursor    busy;

  static int       win_origin_x, win_origin_y,
                   win_width, win_height;
  static int       image_width, image_height;

  long             vioBufOfs;



  /* restore acreen to its original state */

  int  Driver_Restore_Mode( void )
  {
    XUnmapWindow( display, win );
    XCloseDisplay( display );

    return 1;       /* success */
  }


  /* set graphics mode */

  void  x11init( void )
  {
    int                   screen_num, i;
    XTextProperty         xtp;
    XSizeHints            xsh;
    XSetWindowAttributes  xswa;

#if 1
    unsigned short colors[5] = { 0, 16, 32, 48, 63 }; /* gamma = 1.0 */
#else
    unsigned short colors[5] = { 0, 34, 46, 55, 63 }; /* gamma = 2.2 */
#endif


    XrmInitialize();

    if( !( display = XOpenDisplay( displayname ) ) )
      Panic( "ERROR: cannot open display\n" );

    screen_num = DefaultScreen  ( display );
    colormap   = DefaultColormap( display, screen_num );
    depth      = DefaultDepth   ( display, screen_num );
    visual     = DefaultVisual  ( display, screen_num );

    idle       = XCreateFontCursor( display, XC_left_ptr );
    busy       = XCreateFontCursor( display, XC_watch );

    if ( gray )
    {
      int                   count;
      XPixmapFormatValues*  formats;


      formats           = XListPixmapFormats( display, &count );
      vio_ScanLineWidth = 0;

      while ( count > 0 )
      {
        --count;
        if ( formats[count].depth == depth )
        {
          int  bits;

          bits = win_width * formats[count].bits_per_pixel;
          if ( bits % formats[count].scanline_pad )
          {
            bits -= bits % formats[count].scanline_pad;
            bits += formats[count].scanline_pad;
          }

          vio_ScanLineWidth = bits / 8;
          break;
        }
      }
      if ( !vio_ScanLineWidth )
        Panic( "ERROR: the display doesn't offer a suitable pixmap format\n" );

      XFree( formats );
    }

    Vio = (char*)malloc( win_height * vio_ScanLineWidth );

    if ( !Vio )
      Panic( "ERROR: cannot malloc display memory\n" );

    xswa.border_pixel     = BlackPixel( display, screen_num );
    xswa.background_pixel = WhitePixel( display, screen_num );
    xswa.cursor           = busy;

    xswa.event_mask = KeyPressMask | ExposureMask;
    
    win = XCreateWindow( display,
                         RootWindow( display, screen_num ),
                         win_origin_x,
                         win_origin_y,
                         win_width,
                         win_height,
                         10,
                         depth,
                         InputOutput, 
                         visual,
                         CWBackPixel | CWBorderPixel | CWEventMask | CWCursor,
                         &xswa );

    XMapWindow( display, win );
 
    gcblack = XCreateGC( display, RootWindow( display, screen_num ),
                         0L, NULL );
    XSetForeground( display, gcblack, BlackPixel( display, screen_num ) );
    XSetBackground( display, gcblack, WhitePixel( display, screen_num ) );

    /* allocate colors */

    if ( gray )
      for ( i = 0; i < 5; i++ )
      {
        gray_palette[i] = i;
  
        color[i].red   =
        color[i].green =
        color[i].blue  = 65535 - ( colors[i] * 65535 ) / 63;
  
        if ( !XAllocColor( display, colormap, &color[i] ) )
          Panic( "ERROR: cannot allocate Color\n" );
      }

    image = XCreateImage( display, 
                          visual,
                          gray ? depth   : 1, 
                          gray ? ZPixmap : XYBitmap,
                          0, 
                          (char*)Vio, 
                          win_width, 
                          win_height, 
                          8, 
                          0 );
    if ( !image )
      Panic( "ERROR: cannot create image\n" );

    if ( !gray )
    {
      image->byte_order       = MSBFirst;
      image->bitmap_bit_order = MSBFirst;
    }

    /* make window manager happy :-) */
    xtp.value    = (unsigned char*)"FreeType";
    xtp.encoding = 31;
    xtp.format   = 8;
    xtp.nitems   = strlen( (char*)xtp.value );

    xsh.x = win_origin_x;
    xsh.y = win_origin_y;

    xsh.width  = win_width;
    xsh.height = win_height;
    xsh.flags  = (PPosition | PSize);
    xsh.flags  = 0;

    XSetWMProperties( display, win, &xtp, &xtp, NULL, 0, &xsh, NULL, NULL );
  }


  int  Driver_Set_Graphics( int  mode )
  {
    if ( mode == Graphics_Mode_Gray )
    {
      gray = 1;
      vio_ScanLineWidth = 320;

      win_origin_x = 0;
      win_origin_y = 0;
      win_width  = 320;
      win_height = 200;
    }
    else if ( mode == Graphics_Mode_Mono )
    {
      gray = 0;
      vio_ScanLineWidth = 80;

      win_origin_x = 0;
      win_origin_y = 0;
      win_width  = 640;
      win_height = 450;
    }
    else
      Panic( "ERROR: mode %d not supported\n", mode );

    vio_Width  = win_width;
    vio_Height = win_height;

    x11init();

    return 1;       /* success */
  }


  void  Put_Image( int  x, int  y, int  w, int  h )
  {
    XPutImage( display, win, gcblack, image, x, y, x, y, w, h );
  }


  int  Driver_Display_Bitmap( char*  buffer, int  line, int  col )
  {
    int    z, y, used_col;
    char*  target;


    XClearWindow( display, win );

    /* this displays the Header string in the window title */
    XStoreName( display, win, Header );

    if ( line > win_height )
      line = win_height;

    if ( !gray )
    {
      if ( col > vio_ScanLineWidth )
        used_col = vio_ScanLineWidth;
      else
        used_col = col;

      target = Vio + ( line - 1 ) * vio_ScanLineWidth;

      for ( y = 0; y < line; y++ )
      {
        memcpy( (char*)target, buffer, used_col );
        target -= vio_ScanLineWidth;
        buffer += col;
      }

      Put_Image( 0, 0, used_col * 8, line );
      image_width  = used_col * 8;
      image_height = line;
    }
    else
    {
      if ( col > win_width )
        used_col = win_width;
      else
        used_col = col;

      for ( y = line - 1; y >= 0; y-- )
      {
        char*  bufp;


        bufp = buffer;

        for ( z = 0; z < used_col; z++ )
        {
          int  c;


          c = *bufp++;

          if ( c < 0 || c >= 5 ) /* security check */
          {
            /* Message( "weird grayshade: %d\n", c ); */
            c = 0;
          }
          XPutPixel( image, z, y, color[c].pixel );
        }

        buffer += col;
      }

      Put_Image( 0, 0, used_col, line );
      image_width  = used_col;
      image_height = line;
    }

    return 1;
  }


  /* This function maps X keystrokes into GEvents.  Note that */
  /* currently only keystrokes events exit this function.     */

  void  Get_Event( TEvent*  event )
  {
    static char     key_buffer[10];
    static int      key_cursor = 0;
    static int      key_number = 0;
    static XEvent   x_event;
           KeySym   key;

    int             i, bool_exit;
    char            c;

    XComposeStatus  compose;


    bool_exit = key_cursor < key_number;

    XDefineCursor( display, win, idle );

    while ( !bool_exit )
    {
      XNextEvent( display, &x_event );

      switch ( x_event.type )
      {
      case KeyPress:
        key_number = XLookupString( &x_event.xkey,
                                    key_buffer,
                                    sizeof ( key_buffer ),
                                    &key,
                                    &compose );
        key_cursor = 0;

        if ( key_number > 0 )
          bool_exit = 1;
        break;

      case MappingNotify:
        XRefreshKeyboardMapping( &x_event.xmapping );
        break;
      
      case Expose:
#if 0
        Put_Image( x_event.xexpose.x, 
                   x_event.xexpose.y,
                   x_event.xexpose.width, 
                   x_event.xexpose.height );
#else
        /* we always redraw the whole image */
        Put_Image( 0, 0, image_width, image_height );
#endif
        break;

      /* You should add more cases to handle mouse events, etc. */
      }
    }

    XDefineCursor( display, win, busy );
    XFlush       ( display );

    c = key_buffer[key_cursor++];

    for ( i = 0; i < NUM_Translators; i++ )
    {
      if ( c == trans[i].key )
      {
        event->what = trans[i].event_class;
        event->info = trans[i].event_info;
        return;
      }
    }

    event->what = event_Keyboard;
    event->info = (int)c;
  }


/* End */