Codebase list xdemineur / HEAD xdemineur.c
HEAD

Tree @HEAD (Download .tar.gz)

xdemineur.c @HEADraw · history · blame

  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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
 * Copyright © 1993-1999 Marc Baudoin <babafou@babafou.eu.org>
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation.  The author makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 */

static char *const cvsid = "$Id: xdemineur.c,v 1.3.2.2 1999/07/29 21:25:33 babafou Exp $" ;

#include <sys/types.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>

#include <sys/time.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

#include <X11/xpm.h>

#include "demineur.h"
#include "xdemineur.h"

#include "xdemineur.xbm"

#include "pixmaps/face_normal.xpm"
#include "pixmaps/face_click.xpm"
#include "pixmaps/face_play.xpm"
#include "pixmaps/face_happy.xpm"
#include "pixmaps/face_sad.xpm"

#include "pixmaps/digit_0.xpm"
#include "pixmaps/digit_1.xpm"
#include "pixmaps/digit_2.xpm"
#include "pixmaps/digit_3.xpm"
#include "pixmaps/digit_4.xpm"
#include "pixmaps/digit_5.xpm"
#include "pixmaps/digit_6.xpm"
#include "pixmaps/digit_7.xpm"
#include "pixmaps/digit_8.xpm"
#include "pixmaps/digit_9.xpm"

#include "pixmaps/square_0.xpm"
#include "pixmaps/square_1.xpm"
#include "pixmaps/square_2.xpm"
#include "pixmaps/square_3.xpm"
#include "pixmaps/square_4.xpm"
#include "pixmaps/square_5.xpm"
#include "pixmaps/square_6.xpm"
#include "pixmaps/square_7.xpm"
#include "pixmaps/square_8.xpm"

#include "pixmaps/relief.xpm"
#include "pixmaps/flag.xpm"
#include "pixmaps/question.xpm"
#include "pixmaps/mine.xpm"
#include "pixmaps/mine_lost.xpm"
#include "pixmaps/mine_false.xpm"

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

typedef enum
{
   ITEM_NOTHING , ITEM_SQUARE , ITEM_FACE
}
item_t ;

typedef struct
{
   int row , column ;
   enum
   {
      STATE_NONE , STATE_UNCOVER , STATE_CLEAR , STATE_FLAG_QUESTION ,
      STATE_FACE
   }
   state ;
}
previous_t ;

typedef enum
{
   RAISED , SUNKEN
}
relief_t ;

typedef enum
{
   INSIDE , OUTSIDE
}
inout_t ;

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

void xdemineur_colors ( ) ;

void xdemineur_pixmaps ( ) ;

void xdemineur_xpm ( char **data , Pixmap *pixmap_return ) ;

item_t mouse ( int x , int y , int *row , int *column ) ;


void xdemineur_frames ( ) ;

void xdemineur_frame ( int x1 , int y1 , int x2 , int y2 , int width ,
                       relief_t relief , inout_t inoutside ) ;

void xdemineur_face_click ( ) ;

void xdemineur_face_play ( ) ;

void xdemineur_face_display ( Pixmap face ) ;

void xdemineur_mines ( ) ;

void xdemineur_digits ( int number , int digits , int x , int y ) ;

void xdemineur_grid ( ) ;

void xdemineur_square_play ( int row , int column ) ;

void xdemineur_squares_clear ( int row , int column ) ;

void xdemineur_squares ( int row , int column ) ;

void xdemineur_end ( ) ;

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

extern board_t           board ;
extern int               mines ;
extern volatile time_t   elapsed ;
extern state_t           state ;

static Display   *display ;
static int       screen ;
static Window    window ;
static Pixmap    icon_bitmap ;
static Atom      protocol[1] ;
static GC        gc ;

static unsigned long black , white , gray , light_gray ;

static Pixmap face_normal , face_click , face_play , face_happy , face_sad ,
              digit[10] , square[9] ,
              relief , flag , question , mine , mine_lost , mine_false ;

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

void xdemineur_initialize ( int argc , char **argv ,
                            char *display_name , char *geometry )
{
   XSizeHints      size_hints ;
   int             x_pos = 0 , y_pos = 0 ,
                   width = MIN_WIDTH , height = MIN_HEIGHT ;
   char            *window_title = "xdémineur" , *icon_title = "xdémineur" ;
   XTextProperty   window_name , icon_name ;
   XWMHints        wm_hints ;
   XClassHint      class_hints ;
   XGCValues       values ;

   display = XOpenDisplay ( display_name ) ;
   if ( display == NULL )
   {
      fprintf ( stderr , "Error: Can't open display: %s\n" ,
                XDisplayName ( display_name ) ) ;
      exit ( EX_OSERR ) ;
   }

   screen = DefaultScreen ( display ) ;

   board.columns = COLUMNS_MIN ;
   board.rows    = ROWS_MIN ;

   size_hints.flags = 0 ;
   if ( geometry != NULL )
   {
      int            flags , x , y ;
      unsigned int   w , h ;

      flags = XParseGeometry ( geometry , &x , &y , &w , &h ) ;

      if ( WidthValue & flags )
      {
         if ( w < COLUMNS_MIN )
         {
            board.columns = COLUMNS_MIN ;
            fprintf ( stderr ,
                      "%d columns is too small!  Using %d columns instead.\n" ,
                      w , COLUMNS_MIN ) ;
         }
         else
         {
            board.columns = w ;
            width  = BASE_WIDTH + board.columns * WIDTH_INC ;
         }
      }

      if ( HeightValue & flags )
      {
         if ( h < ROWS_MIN )
         {
            board.rows = ROWS_MIN ;
            fprintf ( stderr ,
                      "%d rows is too small!  Using %d rows instead.\n" ,
                      h , ROWS_MIN ) ;
         }
         else
         {
            board.rows = h ;
            height = BASE_HEIGHT + board.rows * HEIGHT_INC ;
         }
      }

      if ( XValue & flags )
      {
         size_hints.flags = USPosition ;
         if ( XNegative & flags )
         {
            x_pos = DisplayWidth ( display , screen ) - width + x ;
         }
         else
         {
            x_pos = x ;
         }
      }

      if ( YValue & flags )
      {
         size_hints.flags = USPosition ;
         if ( YNegative & flags )
         {
            y_pos = DisplayHeight ( display , screen ) - height + y ;
         }
         else
         {
            y_pos = y ;
         }
      }
   }

   window = XCreateSimpleWindow ( display , RootWindow ( display , screen ) ,
                                  x_pos , y_pos , width , height , 1 ,
                                  BlackPixel ( display , screen ) ,
                                  WhitePixel ( display , screen ) ) ;

   XStringListToTextProperty ( &window_title , 1 , &window_name ) ;
   XStringListToTextProperty ( &icon_title   , 1 , &icon_name   ) ;

   size_hints.flags      |= USSize | PMinSize | PMaxSize |
                            PResizeInc | PBaseSize ;
   size_hints.min_width   = size_hints.max_width  = width ;
   size_hints.min_height  = size_hints.max_height = height ;
   size_hints.width_inc   = WIDTH_INC ;
   size_hints.height_inc  = HEIGHT_INC ;
   size_hints.base_width  = BASE_WIDTH ;
   size_hints.base_height = BASE_HEIGHT ;

   icon_bitmap = XCreateBitmapFromData ( display ,
                                         RootWindow ( display , screen ) ,
                                         xdemineur_bits ,
                                         xdemineur_width , xdemineur_height ) ;

   wm_hints.flags         = InputHint | StateHint | IconPixmapHint | WindowGroupHint;
   wm_hints.input         = True ;
   wm_hints.initial_state = NormalState ;
   wm_hints.icon_pixmap   = icon_bitmap ;
   wm_hints.window_group  = window ;

   class_hints.res_name  = argv[0] ;
   class_hints.res_class = "XDémineur" ;

   XSetWMProperties ( display , window ,
                      &window_name , &icon_name ,
                      argv , argc ,
                      &size_hints , &wm_hints , &class_hints ) ;

   XSetCommand ( display , window , argv , argc ) ;

   protocol[0] = XInternAtom ( display , "WM_DELETE_WINDOW" , False ) ;
   XSetWMProtocols ( display , window , protocol , 1 ) ;

   values.foreground         = BlackPixel ( display , screen ) ;
   values.background         = WhitePixel ( display , screen ) ;
   values.graphics_exposures = False ;
   gc = XCreateGC ( display , window ,
                    GCForeground | GCBackground | GCGraphicsExposures ,
                    &values ) ;

   XSelectInput ( display , window ,
                  KeyPressMask         |
                  ButtonPressMask      |
                  ButtonReleaseMask    |
                  ExposureMask         |
                  VisibilityChangeMask |
                  StructureNotifyMask ) ;

   xdemineur_colors ( ) ;
   xdemineur_pixmaps ( ) ;

   XMapWindow ( display , window ) ;
}

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

void xdemineur_colors ( )
{
   Colormap   default_colormap ;
   XColor     color ;

   default_colormap = DefaultColormap ( display , screen ) ;
   XParseColor ( display , default_colormap , "black" , &color ) ;
   XAllocColor ( display , default_colormap , &color ) ;
   black = color.pixel ;
   XParseColor ( display , default_colormap , "white" , &color ) ;
   XAllocColor ( display , default_colormap , &color ) ;
   white = color.pixel ;
   XParseColor ( display , default_colormap , "gray50" , &color ) ;
   XAllocColor ( display , default_colormap , &color ) ;
   gray = color.pixel ;
   XParseColor ( display , default_colormap , "gray70" , &color ) ;
   XAllocColor ( display , default_colormap , &color ) ;
   light_gray = color.pixel ;

   XSetWindowBackground ( display , window , light_gray ) ;
}

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

void xdemineur_pixmaps ( )
{
   xdemineur_xpm ( xpm_face_normal , &face_normal ) ;
   xdemineur_xpm ( xpm_face_click  , &face_click  ) ;
   xdemineur_xpm ( xpm_face_play   , &face_play   ) ;
   xdemineur_xpm ( xpm_face_happy  , &face_happy  ) ;
   xdemineur_xpm ( xpm_face_sad    , &face_sad    ) ;

   xdemineur_xpm ( xpm_digit_0 , &digit[0] ) ;
   xdemineur_xpm ( xpm_digit_1 , &digit[1] ) ;
   xdemineur_xpm ( xpm_digit_2 , &digit[2] ) ;
   xdemineur_xpm ( xpm_digit_3 , &digit[3] ) ;
   xdemineur_xpm ( xpm_digit_4 , &digit[4] ) ;
   xdemineur_xpm ( xpm_digit_5 , &digit[5] ) ;
   xdemineur_xpm ( xpm_digit_6 , &digit[6] ) ;
   xdemineur_xpm ( xpm_digit_7 , &digit[7] ) ;
   xdemineur_xpm ( xpm_digit_8 , &digit[8] ) ;
   xdemineur_xpm ( xpm_digit_9 , &digit[9] ) ;

   xdemineur_xpm ( xpm_square_0 , &square[0] ) ;
   xdemineur_xpm ( xpm_square_1 , &square[1] ) ;
   xdemineur_xpm ( xpm_square_2 , &square[2] ) ;
   xdemineur_xpm ( xpm_square_3 , &square[3] ) ;
   xdemineur_xpm ( xpm_square_4 , &square[4] ) ;
   xdemineur_xpm ( xpm_square_5 , &square[5] ) ;
   xdemineur_xpm ( xpm_square_6 , &square[6] ) ;
   xdemineur_xpm ( xpm_square_7 , &square[7] ) ;
   xdemineur_xpm ( xpm_square_8 , &square[8] ) ;

   xdemineur_xpm ( xpm_relief     , &relief ) ;
   xdemineur_xpm ( xpm_flag       , &flag ) ;
   xdemineur_xpm ( xpm_question   , &question ) ;
   xdemineur_xpm ( xpm_mine       , &mine ) ;
   xdemineur_xpm ( xpm_mine_lost  , &mine_lost ) ;
   xdemineur_xpm ( xpm_mine_false , &mine_false ) ;
}

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

void xdemineur_xpm ( char **data , Pixmap *pixmap_return )
{
   int xpm_status ;

   xpm_status = XpmCreatePixmapFromData ( display , window ,
                                          data , pixmap_return ,
                                          NULL , NULL ) ;
   if ( xpm_status != XpmSuccess )
   {
      fprintf ( stderr ,
                "XpmError: %s\n" , XpmGetErrorString ( xpm_status ) ) ;
      exit ( EX_OSERR ) ;
   }
}

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

void xdemineur_event_loop ( )
{
   fd_set       readfds ;
   XEvent       event ;
   Region       region ;
   XRectangle   rectangle ;
   item_t       item ;
   previous_t   previous ;

   FD_ZERO ( &readfds ) ;
   region = XCreateRegion ( ) ;
   for ( ; ; )
   {
      int row , column ;

      if ( XPending ( display ) == 0 )   /* no more events to handle */
      {
         FD_SET ( ConnectionNumber ( display ) , &readfds ) ;
         if ( select ( ConnectionNumber ( display ) + 1 , &readfds , NULL ,
                       NULL , NULL ) == -1 )   /* wait for events or signal */
         {
            if ( errno == EINTR )   /* interrupted by signal */
            {
               xdemineur_timer ( ) ;
               XFlush ( display ) ;
               continue ;
            }
         }
         /* an event occurred, proceed to XNextEvent() */
      }

      XNextEvent ( display , &event ) ;
      switch ( event.type )
      {
      case KeyPress :
         switch ( XLookupKeysym ( &event.xkey , event.xkey.state ) )
         {
         case XK_Escape :
         case XK_Q :
         case XK_q :
            XDestroyRegion ( region ) ;
            xdemineur_end ( ) ;
            return ;
         }
         break ;
      case ButtonPress :
         switch ( mouse ( event.xbutton.x , event.xbutton.y ,
                          &row , &column ) )
         {
         case ITEM_SQUARE :
            if ( state != PLAYING )
            {
               previous.state = STATE_NONE ;
               break ;
            }

            switch ( event.xbutton.button )
            {
            case Button1 :   /* uncover square */
               if ( board.board[row][column].state == HIDDEN )
               {
                  xdemineur_face_play ( ) ;
                  xdemineur_square_play ( row , column ) ;
                  previous.row    = row ;
                  previous.column = column ;
                  previous.state  = STATE_UNCOVER ;
               }
               else
               {
                  previous.state = STATE_NONE ;
               }
               break ;
            case Button2 :   /* uncover squares around */
               if ( board.board[row][column].state == UNCOVERED )
               {
                  if ( demineur_hidden ( row , column ) != 0 &&
                       board.board[row][column].around
                       == demineur_flags ( row , column ) )
                  {
                     xdemineur_face_play ( ) ;
                     xdemineur_squares_clear ( row , column ) ;
                     previous.row    = row ;
                     previous.column = column ;
                     previous.state  = STATE_CLEAR ;
                  }
                  else
                  {
                     previous.state = STATE_NONE ;
                  }
               }
               break ;
            case Button3 :   /* put flag or question mark */
               if ( board.board[row][column].state == UNCOVERED )
               {
                  previous.state = STATE_NONE;
                  break ;
               }

               xdemineur_face_play ( ) ;
               demineur_flag_question ( row , column ) ;
               xdemineur_mines ( ) ;
               xdemineur_square ( row , column ) ;
               previous.state = STATE_FLAG_QUESTION ;
               break ;
            }
            break ;
         case ITEM_FACE :
            xdemineur_face_click ( ) ;
            previous.state = STATE_FACE ;
            break ;
         case ITEM_NOTHING :
            previous.state = STATE_NONE ;
            break ;
         }
         break ;
      case ButtonRelease :
         item = mouse ( event.xbutton.x , event.xbutton.y , &row , &column ) ;
         switch ( previous.state )
         {
         case STATE_NONE :
            break ;
         case STATE_UNCOVER :
            if ( row == previous.row && column == previous.column )
            {
               demineur_play ( row , column ) ;
            }
            else
            {
               xdemineur_square ( previous.row , previous.column ) ;
            }
            xdemineur_face ( ) ;
            break ;
         case STATE_CLEAR :
            if ( row == previous.row && column == previous.column )
            {
               demineur_clear ( row , column ) ;
            }
            else
            {
               xdemineur_squares ( previous.row , previous.column ) ;
            }
            xdemineur_face ( ) ;
            break ;
         case STATE_FLAG_QUESTION :
            xdemineur_face ( ) ;
            break ;
         case STATE_FACE :
            if ( item == ITEM_FACE )   /* new game */
            {
               demineur_end ( ) ;
               demineur_initialize ( 0 ) ;
               xdemineur_display ( ) ;
               demineur_start_timer ( ) ;
            }
            else
            {
               xdemineur_face ( ) ;
            }
            break ;
         }
         previous.state = STATE_NONE ;
         break ;
      case Expose :
         rectangle.x      = ( short )          event.xexpose.x ;
         rectangle.y      = ( short )          event.xexpose.y ;
         rectangle.width  = ( unsigned short ) event.xexpose.width ;
         rectangle.height = ( unsigned short ) event.xexpose.height ;
         XUnionRectWithRegion ( &rectangle , region , region ) ;
         if ( event.xexpose.count == 0 )
         {
            XSetRegion ( display , gc , region ) ;
            xdemineur_display ( ) ;
            XSetClipMask ( display , gc , None ) ;
            XDestroyRegion ( region ) ;
            region = XCreateRegion ( ) ;
         }
         break ;
      case VisibilityNotify :
         switch ( event.xvisibility.state )
         {
         case VisibilityUnobscured :
            if ( state == PLAYING )
            {
               demineur_start_timer ( ) ;
            }
            break ;
         case VisibilityPartiallyObscured :
            break ;
         case VisibilityFullyObscured :
            if ( state == PLAYING )
            {
               demineur_stop_timer ( ) ;
            }
            break ;
         }
         break ;
      case UnmapNotify :   /* the window has been iconified */
         if ( state == PLAYING )
         {
            demineur_stop_timer ( ) ;
         }
         break ;
      case MapNotify :   /* the window has been deiconified */
         if ( state == PLAYING )
         {
            demineur_start_timer ( ) ;
         }
         break ;
      case ClientMessage :
         if ( event.xclient.data.l[0] == protocol[0] )
         {
            XDestroyRegion ( region ) ;
            xdemineur_end ( ) ;
            return ;
         }
         break ;
      }
   }
}

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

item_t mouse ( int x , int y , int *row , int *column )
{
   int board_width  = board.columns * WIDTH_INC  ,
       board_height = board.rows    * HEIGHT_INC ,
       x_face = ( BASE_WIDTH + board.columns * WIDTH_INC - FACE_WIDTH ) / 2 ;

   *row = *column = 0 ;

   if ( x > X_BOARD               &&
        x < X_BOARD + board_width &&
        y > Y_BOARD               &&
        y < Y_BOARD + board_height )
   {
      if ( ( x - X_BOARD ) % WIDTH_INC  == 0 ||
           ( y - Y_BOARD ) % HEIGHT_INC == 0 )
      {
         return ITEM_NOTHING ;
      }
      else
      {
         *column = 1 + ( x - X_BOARD ) / WIDTH_INC ;
         *row    = 1 + ( y - Y_BOARD ) / HEIGHT_INC ;
         return ITEM_SQUARE ;
      }
   }
   else if ( x >= x_face              &&
             x <= x_face + FACE_WIDTH &&
             y >= Y_FACE              &&
             y <= Y_FACE + FACE_HEIGHT )
   {
      return ITEM_FACE ;
   }
   else
   {
      return ITEM_NOTHING ;
   }
}

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

void xdemineur_display ( )
{
   int row , column ;

   xdemineur_frames ( ) ;
   xdemineur_face ( ) ;
   xdemineur_mines ( ) ;
   xdemineur_timer ( ) ;
   xdemineur_grid ( ) ;
   for ( row = 1 ; row <= board.rows ; row ++ )
   {
      for ( column = 1 ; column <= board.columns ; column ++ )
      {
         xdemineur_square ( row , column ) ;
      }
   }
}

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

void xdemineur_frames ( )
{
   int board_width   = board.columns * WIDTH_INC ;
   int board_height  = board.rows    * HEIGHT_INC ;
   int window_width  = BASE_WIDTH  + board_width ;
   int window_height = BASE_HEIGHT + board_height ;

   xdemineur_frame ( 0 , 0 , window_width - 1 , window_height - 1 ,
                     RELIEF_WIDTH , RAISED , INSIDE) ;
   xdemineur_frame ( X_BOARD , Y_BOARD ,
                     X_BOARD + board_width , Y_BOARD + board_height ,
                     RELIEF_WIDTH , SUNKEN , OUTSIDE) ;
   xdemineur_frame ( EDGE , EDGE , window_width - 1 - EDGE , Y_BOARD - EDGE ,
                     RELIEF_WIDTH , SUNKEN , OUTSIDE ) ;
}

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

void xdemineur_frame ( int x1 , int y1 , int x2 , int y2 , int width ,
                       relief_t relief , inout_t inoutside )
{
   int coord ;

   if ( inoutside == OUTSIDE )
   {
      x1 -= width ; x2 += width ;
      y1 -= width ; y2 += width ;
   }

   XSetForeground ( display , gc , ( relief == RAISED ) ? white : gray ) ;
   for ( coord = 0 ; coord < width ; coord ++ )
   {
      XDrawLine ( display , window , gc ,
                  x1 , y1 + coord , x2 - coord , y1 + coord ) ;
      XDrawLine ( display , window , gc ,
                  x1 + coord , y1 , x1 + coord , y2 - coord ) ;
   }
   XSetForeground ( display , gc , ( relief == RAISED ) ? gray : white ) ;
   for ( coord = 0 ; coord < width ; coord ++ )
   {
      XDrawLine ( display , window , gc ,
                  x1 + 1 + coord , y2 - coord , x2 , y2 - coord ) ;
      XDrawLine ( display , window , gc ,
                  x2 - coord , y1 + 1 + coord , x2 - coord , y2 ) ;
   }
}

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

void xdemineur_face ( )
{
   switch ( state )
   {
   case PLAYING :
      xdemineur_face_display ( face_normal ) ;
      break ;
   case WON :
      xdemineur_face_display ( face_happy ) ;
      break ;
   case LOST :
      xdemineur_face_display ( face_sad ) ;
      break ;
   }
}

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

void xdemineur_face_click ( )
{
   xdemineur_face_display ( face_click ) ;
}

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

void xdemineur_face_play ( )
{
   xdemineur_face_display ( face_play ) ;
}

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

void xdemineur_face_display ( Pixmap face )
{
   XCopyArea ( display , face , window , gc ,
               0 , 0 , FACE_WIDTH , FACE_HEIGHT ,
               ( BASE_WIDTH + board.columns * WIDTH_INC - FACE_WIDTH ) / 2 ,
               Y_FACE ) ;
}

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

void xdemineur_mines ( )
{
   xdemineur_digits ( mines , 4 , X_DIGITS , Y_DIGITS ) ;
}

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

void xdemineur_timer ( )
{
   int x = BASE_WIDTH + board.columns * WIDTH_INC
           - X_DIGITS - 4 * DIGIT_WIDTH ;

   xdemineur_digits ( elapsed , 4 , x , Y_DIGITS ) ;
}

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

void xdemineur_digits ( int number , int digits , int x , int y )
{
   int i , remainder = number ;

   for ( i = digits - 1 ; i >= 0 ; i -- , remainder /= 10 )
   {
      XCopyArea ( display , digit[remainder % 10] , window , gc ,
                  0 , 0 , DIGIT_WIDTH , DIGIT_HEIGHT ,
                  x + i * DIGIT_WIDTH , y ) ;
   }

   xdemineur_frame ( x , y ,
                     x + digits * DIGIT_WIDTH - 1 , y + DIGIT_HEIGHT - 1 ,
                     1 , SUNKEN , OUTSIDE ) ;
}

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

void xdemineur_grid ( )
{
   int coord ,
       board_width  = board.columns * WIDTH_INC  + 1 ,
       board_height = board.rows    * HEIGHT_INC + 1 ;

   XSetForeground ( display , gc , black ) ;

   for ( coord = X_BOARD ;
         coord < X_BOARD + board_width ;
         coord += WIDTH_INC )
   {
      XDrawLine ( display , window , gc ,
                  coord , Y_BOARD , coord , Y_BOARD + board_height - 1 ) ;
   }

   for ( coord = Y_BOARD ;
         coord < Y_BOARD + board_height ;
         coord += HEIGHT_INC )
   {
      XDrawLine ( display , window , gc ,
                  X_BOARD , coord , X_BOARD + board_width - 1 , coord ) ;
   }
}

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

void xdemineur_square ( int row , int column )
{
   int x = X_BOARD + 1 + ( column - 1 ) * ( SQUARE_WIDTH  + 1 ) ,
       y = Y_BOARD + 1 + ( row    - 1 ) * ( SQUARE_HEIGHT + 1 ) ;

   if ( row < 1 || row > board.rows || column < 1 || column > board.columns )
   {
      return ;
   }

   switch ( board.board[row][column].state )
   {
      case HIDDEN :
         if ( state == LOST && board.board[row][column].mine )
         {
            XCopyArea ( display , mine , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         else
         {
            XCopyArea ( display , relief , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         break ;
      case FLAGGED :
         if ( state == LOST && ! board.board[row][column].mine )
         {
            XCopyArea ( display , mine_false , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         else
         {
            XCopyArea ( display , flag , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         break ;
      case QUESTION :
         if ( state == LOST && board.board[row][column].mine )
         {
            XCopyArea ( display , mine , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         else
         {
            XCopyArea ( display , question , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         break ;
      case UNCOVERED :
         if ( ! board.board[row][column].mine )
         {
            XCopyArea ( display , square[board.board[row][column].around] ,
                        window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         else
         {
            XCopyArea ( display , mine_lost , window , gc ,
                        0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
         }
         break ;
   }
}

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

void xdemineur_square_play ( int row , int column )
{
   int x = X_BOARD + 1 + ( column - 1 ) * ( SQUARE_WIDTH  + 1 ) ,
       y = Y_BOARD + 1 + ( row    - 1 ) * ( SQUARE_HEIGHT + 1 ) ;

   if ( row < 1 || row > board.rows || column < 1 || column > board.columns )
   {
      return ;
   }

   XCopyArea ( display , square[0] , window , gc ,
               0 , 0 , SQUARE_WIDTH , SQUARE_HEIGHT , x , y ) ;
}

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

void xdemineur_squares_clear ( int row , int column )
{
   int r , c ;

   for ( r = row - 1 ; r <= row + 1 ; r ++ )
   {
      for ( c = column - 1 ; c <= column + 1 ; c ++ )
      {
         if ( board.board[r][c].state == HIDDEN )
         {
            xdemineur_square_play ( r , c ) ;
         }
      }
   }
}

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

void xdemineur_squares ( int row , int column )
{
   int r , c ;

   for ( r = row - 1 ; r <= row + 1 ; r ++ )
   {
      for ( c = column - 1 ; c <= column + 1 ; c ++ )
      {
         xdemineur_square ( r , c ) ;
      }
   }
}

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

void xdemineur_end ( )
{
   int i ;

   XFreePixmap ( display , face_normal ) ;
   XFreePixmap ( display , face_click  ) ;
   XFreePixmap ( display , face_play   ) ;
   XFreePixmap ( display , face_happy  ) ;
   XFreePixmap ( display , face_sad    ) ;
   for ( i = 0 ; i <= 9 ; i ++ )
   {
      XFreePixmap ( display , digit[i] ) ;
   }
   for ( i = 0 ; i <= 8 ; i ++ )
   {
      XFreePixmap ( display , square[i] ) ;
   }
   XFreePixmap ( display , relief     ) ;
   XFreePixmap ( display , flag       ) ;
   XFreePixmap ( display , question   ) ;
   XFreePixmap ( display , mine       ) ;
   XFreePixmap ( display , mine_lost  ) ;
   XFreePixmap ( display , mine_false ) ;

   XFreeGC ( display , gc ) ;
   XFreePixmap ( display , icon_bitmap ) ;
   XDestroyWindow ( display , window ) ;
   XCloseDisplay ( display ) ;
}