~sbeattie/ubuntu/lucid/vnc4/lp556147

« back to all changes in this revision

Viewing changes to unix/xc/extras/FreeType/test/arch/win32/gw_win32.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
 *
 
3
 *  gw_win32.c  graphics driver for Win32 platform.              0.1
 
4
 *
 
5
 *  This is the driver for displaying inside a window under Win32,
 
6
 *  used by the graphics utility of the FreeType test suite.
 
7
 *
 
8
 *  Written by Antoine Leca.
 
9
 *  Copyright 1999 by Antoine Leca,
 
10
 *  David Turner, Robert Wilhelm, and Werner Lemberg.
 
11
 *
 
12
 *  Borrowing liberally from the other FreeType drivers.
 
13
 *
 
14
 *  This file is part of the FreeType project, and may only be used
 
15
 *  modified and distributed under the terms of the FreeType project
 
16
 *  license, LICENSE.TXT. By continuing to use, modify or distribute
 
17
 *  this file you indicate that you have read the license and
 
18
 *  understand and accept it fully.
 
19
 *
 
20
 ******************************************************************/
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
 
 
26
#define WIN32_LEAN_AND_MEAN
 
27
#include <windows.h>
 
28
 
 
29
#include "gdriver.h"
 
30
#include "gevents.h"
 
31
#include "gmain.h"
 
32
 
 
33
 
 
34
/*  Size of the window. */
 
35
#define WIN_WIDTH   640u
 
36
#define WIN_HEIGHT  450u
 
37
 
 
38
/* These values can be changed, but WIN_WIDTH should remain for now a  */
 
39
/* multiple of 32 to avoid padding issues.                             */
 
40
 
 
41
 
 
42
  typedef struct  _Translator
 
43
  {
 
44
    char    key;
 
45
    GEvent  event_class;
 
46
    int     event_info;
 
47
  } Translator;
 
48
 
 
49
#define NUM_Translators  20
 
50
 
 
51
  static const Translator  trans[NUM_Translators] =
 
52
  {
 
53
    { (char)27, event_Quit,              0 },
 
54
    { 'q',      event_Quit,              0 },
 
55
 
 
56
    { 'x',      event_Rotate_Glyph,     -1 },
 
57
    { 'c',      event_Rotate_Glyph,      1 },
 
58
    { 'v',      event_Rotate_Glyph,    -16 },
 
59
    { 'b',      event_Rotate_Glyph,     16 },
 
60
 
 
61
    { '{',      event_Change_Glyph, -10000 },
 
62
    { '}',      event_Change_Glyph,  10000 },
 
63
    { '(',      event_Change_Glyph,  -1000 },
 
64
    { ')',      event_Change_Glyph,   1000 },
 
65
    { '9',      event_Change_Glyph,   -100 },
 
66
    { '0',      event_Change_Glyph,    100 },
 
67
    { 'i',      event_Change_Glyph,    -10 },
 
68
    { 'o',      event_Change_Glyph,     10 },
 
69
    { 'k',      event_Change_Glyph,     -1 },
 
70
    { 'l',      event_Change_Glyph,      1 },
 
71
 
 
72
    { '+',      event_Scale_Glyph,      10 },
 
73
    { '-',      event_Scale_Glyph,     -10 },
 
74
    { 'u',      event_Scale_Glyph,       1 },
 
75
    { 'j',      event_Scale_Glyph,      -1 }
 
76
  };
 
77
 
 
78
  /* handle of the window. */
 
79
  static HWND   hwndGraphic;
 
80
 
 
81
  /* bitmap information */
 
82
  static LPBITMAPINFO pbmi;
 
83
  static HBITMAP      hbm;
 
84
 
 
85
  /* local event to pass on */
 
86
  static TEvent ourevent = { event_Quit, 0 };
 
87
  static int    eventToProcess = 0;
 
88
 
 
89
  /* array defined in the test programs */
 
90
  extern char   Header[];
 
91
 
 
92
 
 
93
/* restores screen to its original state */
 
94
 
 
95
int  Driver_Restore_Mode()
 
96
  {
 
97
    /* The graphical window has perhaps already destroyed itself */
 
98
    if ( hwndGraphic ) {
 
99
      DestroyWindow ( hwndGraphic );
 
100
      PostMessage( hwndGraphic, WM_QUIT, 0, 0 );
 
101
    }
 
102
 
 
103
    if ( pbmi )    free ( pbmi );
 
104
 
 
105
    return 1;
 
106
  }
 
107
 
 
108
 
 
109
/*
 
110
 * set graphics mode
 
111
 * and create the window class and the message handling.
 
112
 */
 
113
 
 
114
/* Declarations of the Windows-specific functions that are below. */
 
115
static BOOL RegisterTheClass ( void );
 
116
static BOOL CreateTheWindow ( int width, int height );
 
117
 
 
118
 
 
119
int Driver_Set_Graphics ( int mode )
 
120
  {
 
121
  int  i;
 
122
  static RGBQUAD gray_scale[5] = {
 
123
      { 0xFF, 0xFF, 0xFF, 0 },     /* white */
 
124
      { 0xC0, 0xC0, 0xC0, 0 },
 
125
      { 0x80, 0x80, 0x80, 0 },
 
126
      { 0x40, 0x40, 0x40, 0 },
 
127
      {    0,    0,    0, 0 } };   /* black */
 
128
 
 
129
    if( ! RegisterTheClass() ) return 0;  /* if already running, fails. */
 
130
 
 
131
    /* find some memory for the bitmap header */
 
132
    if ( (pbmi = malloc ( sizeof ( BITMAPINFO ) + sizeof ( RGBQUAD ) * 256 ) )
 
133
                                /* 256 should really be 2 if not grayscale */
 
134
               == NULL )
 
135
      /* lack of memory; fails the process */
 
136
      return 0;
 
137
 
 
138
    /* initialize the header to appropriate values */
 
139
    memset( pbmi, 0, sizeof ( BITMAPINFO ) + sizeof ( RGBQUAD ) * 256 );
 
140
 
 
141
    switch ( mode )
 
142
    {
 
143
    case Graphics_Mode_Mono:
 
144
      pbmi->bmiHeader.biBitCount = 1;
 
145
      pbmi->bmiColors[0] = gray_scale[0];
 
146
      pbmi->bmiColors[1] = gray_scale[4];
 
147
 
 
148
      vio_ScanLineWidth = WIN_WIDTH / 8;
 
149
      vio_Width         = WIN_WIDTH;
 
150
      vio_Height        = WIN_HEIGHT;
 
151
 
 
152
      break;
 
153
 
 
154
    case Graphics_Mode_Gray:
 
155
      pbmi->bmiHeader.biBitCount = 8;
 
156
      pbmi->bmiHeader.biClrUsed  = 5;
 
157
 
 
158
      memcpy ( &pbmi->bmiColors[0], gray_scale, sizeof gray_scale );
 
159
 
 
160
      vio_ScanLineWidth = WIN_WIDTH;
 
161
      vio_Width         = WIN_WIDTH;
 
162
      vio_Height        = WIN_HEIGHT;
 
163
 
 
164
      for ( i = 0; i < 5; ++i )
 
165
        gray_palette[i] = i;
 
166
 
 
167
      break;
 
168
 
 
169
    default:
 
170
      free ( pbmi );
 
171
      return 0;         /* Unknown mode */
 
172
    }
 
173
 
 
174
    pbmi->bmiHeader.biSize   = sizeof ( BITMAPINFOHEADER );
 
175
    pbmi->bmiHeader.biWidth  = vio_Width;
 
176
    pbmi->bmiHeader.biHeight = vio_Height;
 
177
    pbmi->bmiHeader.biPlanes = 1;
 
178
 
 
179
    if( ! CreateTheWindow(vio_Width, vio_Height) )
 
180
    {
 
181
      free ( pbmi );
 
182
      return 0;
 
183
    }
 
184
 
 
185
    return 1;     /* success even if the window was not built. */
 
186
  }
 
187
 
 
188
 
 
189
int Driver_Display_Bitmap ( char* buffer, int lines, int cols )
 
190
  {
 
191
  HDC     hDC;
 
192
 
 
193
    if ( cols * 8 != pbmi->bmiHeader.biWidth * pbmi->bmiHeader.biBitCount )
 
194
      pbmi->bmiHeader.biWidth  = cols * 8 / pbmi->bmiHeader.biBitCount;
 
195
 
 
196
    hDC = GetDC ( hwndGraphic );
 
197
    SetDIBits ( hDC, hbm, 0, lines, buffer, pbmi, DIB_RGB_COLORS );
 
198
    ReleaseDC ( hwndGraphic, hDC );
 
199
 
 
200
    ShowWindow( hwndGraphic, SW_SHOW );
 
201
    InvalidateRect ( hwndGraphic, NULL, FALSE );
 
202
    UpdateWindow ( hwndGraphic );
 
203
 
 
204
    return 1;       /* success */
 
205
  }
 
206
 
 
207
 
 
208
void Get_Event( TEvent* event )
 
209
  {
 
210
  MSG msg;
 
211
 
 
212
    if ( hwndGraphic )
 
213
    {
 
214
      SetWindowText ( hwndGraphic, Header );
 
215
    }
 
216
 
 
217
    do {
 
218
      while ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) {
 
219
        TranslateMessage ( &msg );
 
220
        DispatchMessage  ( &msg );
 
221
      }
 
222
      if ( ! eventToProcess )
 
223
        WaitMessage();
 
224
    } while ( ! eventToProcess );
 
225
 
 
226
    event->what = ourevent.what;
 
227
    event->info = ourevent.info;
 
228
    eventToProcess = 0;
 
229
    return;
 
230
  }
 
231
 
 
232
 
 
233
/* ---- Windows-specific stuff ------------------------------------------- */
 
234
 
 
235
LRESULT CALLBACK Message_Process( HWND, UINT, WPARAM, LPARAM );
 
236
 
 
237
static
 
238
BOOL RegisterTheClass ( void )
 
239
  {
 
240
  WNDCLASS ourClass = {
 
241
      /* UINT    style        */ 0,
 
242
      /* WNDPROC lpfnWndProc  */ Message_Process,
 
243
      /* int     cbClsExtra   */ 0,
 
244
      /* int     cbWndExtra   */ 0,
 
245
      /* HANDLE  hInstance    */ 0,
 
246
      /* HICON   hIcon        */ 0,
 
247
      /* HCURSOR hCursor      */ 0,
 
248
      /* HBRUSH  hbrBackground*/ 0,
 
249
      /* LPCTSTR lpszMenuName */ NULL,
 
250
      /* LPCTSTR lpszClassName*/ "FreeTypeTestGraphicDriver"
 
251
  };
 
252
 
 
253
    ourClass.hInstance    = GetModuleHandle( NULL );
 
254
    ourClass.hIcon        = LoadIcon(0, IDI_APPLICATION);
 
255
    ourClass.hCursor      = LoadCursor(0, IDC_ARROW);
 
256
    ourClass.hbrBackground= GetStockObject(BLACK_BRUSH);
 
257
 
 
258
    return RegisterClass(&ourClass) != 0;  /* return False if it fails. */
 
259
  }
 
260
 
 
261
static
 
262
BOOL CreateTheWindow ( int width, int height )
 
263
  {
 
264
 
 
265
    if ( ! (hwndGraphic = CreateWindow(
 
266
        /* LPCSTR lpszClassName;    */ "FreeTypeTestGraphicDriver",
 
267
        /* LPCSTR lpszWindowName;   */ "FreeType Test Graphic Driver",
 
268
        /* DWORD dwStyle;           */  WS_OVERLAPPED | WS_SYSMENU,
 
269
        /* int x;                   */  CW_USEDEFAULT,
 
270
        /* int y;                   */  CW_USEDEFAULT,
 
271
        /* int nWidth;              */  width + 2*GetSystemMetrics(SM_CXBORDER),
 
272
        /* int nHeight;             */  height+ GetSystemMetrics(SM_CYBORDER)
 
273
                                              + GetSystemMetrics(SM_CYCAPTION),
 
274
        /* HWND hwndParent;         */  HWND_DESKTOP,
 
275
        /* HMENU hmenu;             */  0,
 
276
        /* HINSTANCE hinst;         */  GetModuleHandle( NULL ),
 
277
        /* void FAR* lpvParam;      */  NULL))
 
278
       )
 
279
         /*  creation failed... */
 
280
         return 0;
 
281
 
 
282
    return 1;
 
283
  }
 
284
 
 
285
  /* Message processing for our Windows class */
 
286
LRESULT CALLBACK Message_Process( HWND handle, UINT mess,
 
287
                                  WPARAM wParam, LPARAM lParam )
 
288
  {
 
289
 
 
290
    switch( mess )
 
291
    {
 
292
    case WM_DESTROY:
 
293
        /* warn the main thread to quit if it didn't know */
 
294
      ourevent.what = event_Quit;
 
295
      ourevent.info = 0;
 
296
      eventToProcess = 1;
 
297
      hwndGraphic = 0;
 
298
      PostQuitMessage ( 0 );
 
299
      DeleteObject ( hbm );
 
300
      break;
 
301
 
 
302
    case WM_CREATE:
 
303
      {
 
304
      HDC     hDC;
 
305
 
 
306
        hDC = GetDC ( handle );
 
307
        hbm = CreateDIBitmap (
 
308
          /* HDC hdc;     handle of device context        */ hDC,
 
309
          /* BITMAPINFOHEADER FAR* lpbmih;  addr.of header*/ &pbmi->bmiHeader,
 
310
          /* DWORD dwInit;  CBM_INIT to initialize bitmap */ 0,
 
311
          /* const void FAR* lpvBits;   address of values */ NULL,
 
312
          /* BITMAPINFO FAR* lpbmi;   addr.of bitmap data */ pbmi,
 
313
          /* UINT fnColorUse;      RGB or palette indices */ DIB_RGB_COLORS);
 
314
        ReleaseDC ( handle, hDC );
 
315
        break;
 
316
      }
 
317
 
 
318
    case WM_PAINT:
 
319
      {
 
320
      HDC     hDC, memDC;
 
321
      HANDLE  oldbm;
 
322
      PAINTSTRUCT ps;
 
323
 
 
324
        hDC = BeginPaint ( handle, &ps );
 
325
        memDC = CreateCompatibleDC(hDC);
 
326
        oldbm = SelectObject(memDC, hbm);
 
327
        BitBlt ( hDC, 0, 0, vio_Width, vio_Height, memDC, 0, 0, SRCCOPY);
 
328
        ReleaseDC ( handle, hDC );
 
329
        SelectObject ( memDC, oldbm );
 
330
        DeleteObject ( memDC );
 
331
        EndPaint ( handle, &ps );
 
332
      }
 
333
 
 
334
    case WM_KEYDOWN:
 
335
      switch ( wParam )
 
336
      {
 
337
      case VK_ESCAPE:
 
338
        ourevent.what = event_Quit;
 
339
        ourevent.info = 0;
 
340
        eventToProcess = 1;
 
341
        break;
 
342
 
 
343
      case VK_F1:           /*  bring up help and about dialog window */
 
344
        break;
 
345
      }
 
346
      break;
 
347
 
 
348
    case WM_CHAR:
 
349
      {
 
350
      char  c = wParam ;
 
351
      int   i;
 
352
 
 
353
        for ( i = 0; i < NUM_Translators; i++ )
 
354
        {
 
355
          if ( c == trans[i].key )
 
356
          {
 
357
            ourevent.what = trans[i].event_class;
 
358
            ourevent.info = trans[i].event_info;
 
359
            eventToProcess = 1;
 
360
            return 0;
 
361
          }
 
362
        }
 
363
 
 
364
        /* unrecognized keystroke */
 
365
        ourevent.what = event_Keyboard;
 
366
        ourevent.info = (int)c;
 
367
        eventToProcess = 1;
 
368
      }
 
369
      break;
 
370
 
 
371
    default:
 
372
       return DefWindowProc( handle, mess, wParam, lParam );
 
373
    }
 
374
 
 
375
    return 0;
 
376
  }
 
377
 
 
378
/* End */