File: xdisplay.c

package info (click to toggle)
fuse-emulator 1.0.0.1a%2Bdfsg1-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,568 kB
  • sloc: ansic: 67,895; sh: 10,265; perl: 3,386; makefile: 787; yacc: 227; lex: 139
file content (1002 lines) | stat: -rw-r--r-- 28,122 bytes parent folder | download
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
/* xdisplay.c: Routines for dealing with drawing the Speccy's screen via Xlib
   Copyright (c) 2000-2005 Philip Kendall, Darren Salt, Gergely Szsz

   $Id: xdisplay.c 4109 2009-12-27 06:15:10Z fredm $

   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 2 of the License, 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.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#ifdef HAVE_SIGINFO_H
#include <siginfo.h>		/* Needed for psignal on Solaris */
#endif				/* #ifdef HAVE_SIGINFO_H */

#ifdef HAVE_X11_EXTENSIONS_XSHM_H
#define X_USE_SHM
#endif				/* #ifdef HAVE_X11_EXTENSIONS_XSHM_H */

#ifdef X_USE_SHM
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#endif				/* #ifdef X_USE_SHM */

#include <libspectrum.h>

#include "display.h"
#include "fuse.h"
#include "keyboard.h"
#include "machine.h"
#include "screenshot.h"
#include "settings.h"
#include "xdisplay.h"
#include "xui.h"
#include "ui/scaler/scaler.h"
#include "ui/ui.h"
#include "ui/uidisplay.h"
#include "scld.h"

typedef enum {
  MSB_RED = 0,			/* 0RGB */
  LSB_RED,			/* 0BGR */
				/* ARGB/ABGR RGBA/BGRA ????*/
} redmask_t;

static XImage *image = NULL;	/* The image structure to draw the
				   Speccy's screen on */
static GC gc;			/* A graphics context to draw with */

Colormap currentMap;		/* The used colormap */

/* The size of a 1x1 image in units of
   DISPLAY_ASPECT WIDTH x DISPLAY_SCREEN_HEIGHT
   scale * 4, so 2 => 0.5, 6 => 1.5, 4 => 1.0 */
static int image_scale = 1;

/* The height and width of a 1x1 image in pixels */
static int image_width, image_height, scaled_image_w, scaled_image_h;

/* An RGB image of the Spectrum screen; slightly bigger than the real
   screen to handle the smoothing filters which read around each pixel 16bpp */
static libspectrum_word
  rgb_image[2 * ( DISPLAY_SCREEN_HEIGHT + 4 )][2 * ( DISPLAY_SCREEN_WIDTH  + 3 )];
static const int rgb_pitch = 2 * ( DISPLAY_SCREEN_WIDTH + 3 );

/* A scaled copy of the image displayed on the Spectrum's screen */
static libspectrum_word
  scaled_image[3 * DISPLAY_SCREEN_HEIGHT][3 * DISPLAY_SCREEN_WIDTH];
static const ptrdiff_t scaled_pitch =
  3 * DISPLAY_SCREEN_WIDTH * 2;

/* A scaled copy of the image displayed on the Spectrum's screen */
static libspectrum_word
  rgb_image_backup[2 * ( DISPLAY_SCREEN_HEIGHT + 4 )][2 * ( DISPLAY_SCREEN_WIDTH  + 3 )];
static const int rgb_backup_pitch = 2 * ( DISPLAY_SCREEN_WIDTH + 3 );

static unsigned long colours[128];
static int colours_allocated = 0;

/* The current size of the window (in units of DISPLAY_SCREEN_*) */
static int xdisplay_current_size = 1;
static int xdisplay_depth = -1;
static Visual *xdisplay_visual = NULL;
static int xdisplay_bw = -1;
static redmask_t xdisplay_redpos = MSB_RED;	/* red_mask 0xff000 ...*/
static int rShift = 16, gShift = 8, bShift = 0;

/* This is a rule of thumb for the maximum number of rects that can be updated
   each frame. If more are generated we just update the whole screen */
typedef struct {
  int x; int y; int w; int h;
} X_Rect;
#define MAX_UPDATE_RECT 300
static X_Rect updated_rects[MAX_UPDATE_RECT];
static int num_rects = 0;
static libspectrum_byte xdisplay_force_full_refresh = 1;

#ifdef X_USE_SHM
static XShmSegmentInfo shm_info;
int shm_eventtype;

static int try_shm( void );
static int get_shm_id( const int size );
#endif				/* #ifdef X_USE_SHM */

static int shm_used = 0;

typedef void xdisplay_update_rect_t( int x, int y, int w, int h );

static xdisplay_update_rect_t *xdisplay_update_rect;
static xdisplay_update_rect_t xdisplay_update_rect_noscale;
static xdisplay_update_rect_t xdisplay_update_rect_scale;

static int xdisplay_find_visual( void );
static int xdisplay_allocate_colours4( void );
static int xdisplay_allocate_colours8( void );
static int xdisplay_allocate_gc( Window window, GC *new_gc );

static int xdisplay_allocate_image( void );
static void register_scalers( void );
static void xdisplay_destroy_image( void );
static void xdisplay_catch_signal( int sig );

typedef void xdisplay_putpixel_t( int x, int y, libspectrum_word *color );

static xdisplay_putpixel_t *xdisplay_putpixel;

static xdisplay_putpixel_t xdisplay_putpixel_4;
static xdisplay_putpixel_t xdisplay_putpixel_8;
static xdisplay_putpixel_t xdisplay_putpixel_15;
static xdisplay_putpixel_t xdisplay_putpixel_16;
static xdisplay_putpixel_t xdisplay_putpixel_24;

static libspectrum_word pal_colour[16] = {
  0x0000, 0x0017, 0xb800, 0xb817, 0x05e0, 0x05f7, 0xbde0, 0xbdf7,
  0x0000, 0x001f, 0xf800, 0xf81f, 0x07e0, 0x07ff, 0xffe0, 0xffff,
};

static libspectrum_word pal_grey[16] = {
  0x0000, 0x39c7, 0x18a3, 0x526a, 0x738e, 0xad55, 0x8430, 0xbdf7,
  0x0000, 0x4a69, 0x20e4, 0x6b4d, 0x94b2, 0xdf1b, 0xb596, 0xffff,
};

static  int rgb_for_4[] = {
  0x00, 0x00, 0x00,
  0x00, 0x00, 0xBF,
  0xBF, 0x00, 0x00,
  0xBF, 0x00, 0xBF,
  0x00, 0xBF, 0x00,
  0x00, 0xBF, 0xBF,
  0xBF, 0xBF, 0x00,
  0xBF, 0xBF, 0xBF,
  0x00, 0x00, 0x00,
  0x00, 0x00, 0xFF,
  0xFF, 0x00, 0x00,
  0xFF, 0x00, 0xFF,
  0x00, 0xFF, 0x00,
  0x00, 0xFF, 0xFF,
  0xFF, 0xFF, 0x00,
  0xFF, 0xFF, 0xFF
};

int
xdisplay_init( void )
{
  if( xdisplay_find_visual() ) return 1;
  if( xdisplay_depth == 4 && xdisplay_allocate_colours4() ) return 1;
  if( xdisplay_depth == 8 && xdisplay_allocate_colours8() ) return 1;
  if( xdisplay_allocate_gc( xui_mainWindow,&gc ) ) return 1;
  if( xdisplay_allocate_image() ) return 1;

  return 0;
}

static int
xdisplay_find_visual( void )
{
  XVisualInfo visual_tmpl;
  XVisualInfo *vis;
  int nvis, i;
  int sel_v = -1;
  int sel_v_depth = -1;
  int sel_v_class = -1;
    
  visual_tmpl.screen = xui_screenNum;
  vis = XGetVisualInfo( display,
                             VisualScreenMask,
                             &visual_tmpl, &nvis );
  if( vis != NULL ) {
    for( i = 0; i < nvis; i++ ) {
            /*
             * Save the visual index and its depth, if this is the first
             * truecolor visual, or a visual that is 'preferred' over the
             * previous 'best' visual.
             */
      if( ( sel_v_depth == -1 && vis[i].depth >= 4 ) || /* depth >= 4  */
    	  ( vis[i].depth > sel_v_depth &&
		vis[i].depth <= 16 ) ||			/* depth up to 16 */
    	  ( vis[i].depth <= 8 && vis[i].depth == sel_v_depth &&
		vis[i].class != sel_v_class && 
		    vis[i].class == PseudoColor ) || /* indexed changable palette */
    	  ( vis[i].depth > 8 && vis[i].depth == sel_v_depth &&
		vis[i].class != sel_v_class && 
		    vis[i].class == TrueColor ) /* decomposed constatant colors */
      ) {
        sel_v = i;
        sel_v_depth = vis[i].depth;
	sel_v_class = vis[i].class;
      }
    }

    if ( sel_v != -1 ) {
      xdisplay_visual = vis[sel_v].visual;
      xdisplay_depth = vis[sel_v].depth;
    }
    XFree(vis);
  }
  return sel_v == -1 ? 1 : 0;
}

static void
xdisplay_putpixel_4( int x, int y, libspectrum_word *colour)
{
  XPutPixel( image, x, y, *colour );
}

static void
xdisplay_putpixel_8( int x, int y, libspectrum_word *colour)
{
  unsigned long c = *colour;

  c = (       c         & 0x1f ) * 3 / 31 +
      ( ( ( ( c >>  5 ) & 0x3f ) * 7 / 63 ) << 2 ) +
        ( ( ( c >> 11 )          * 3 / 31 ) << 5 );
  XPutPixel( image, x, y, colours[ c ] );
}

static void
xdisplay_putpixel_15( int x, int y, libspectrum_word *colour)
{
  unsigned long c = *colour;

  c = ( c & 0x1f ) + ( ( c >>  1 ) & 0xffe0 );
  XPutPixel( image, x, y, c );
}

static void
xdisplay_putpixel_16( int x, int y, libspectrum_word *colour)
{
  XPutPixel( image, x, y, *colour );
}

static void
xdisplay_putpixel_24( int x, int y, libspectrum_word *colour)
{
  unsigned long c = *colour;

  c =   ( ( ( c         & 0x1f ) * 255 / 31 ) << bShift ) +
      ( ( ( ( c >>  5 ) & 0x3f ) * 255 / 63 ) << gShift ) +
        ( ( ( c >> 11 )          * 255 / 31 ) << rShift );
  XPutPixel( image, x, y, c );
}

static int
xdisplay_alloc_colour( Colormap *map, XColor *colour )
{
  for(;;) {
    if( XAllocColor( display, *map, colour ) )
      return 0;

    fprintf(stderr,"%s: XAllocColor failed (%04x %04x %04x)\n", fuse_progname,
				colour->red, colour->green, colour->blue );
    if( *map == DefaultColormap( display, xui_screenNum ) ) {
      fprintf( stderr,"%s: switching to private colour map\n", fuse_progname );
      *map = XCopyColormapAndFree( display, *map );
      XSetWindowColormap( display, xui_mainWindow, *map );
      /* Need to repeat the failed allocation */
    } else {
      return 1;
    }
  }
  return 0;
}

static int
xdisplay_allocate_colours4( void )
{
  XColor c;
  int i;

  currentMap = DefaultColormap( display, xui_screenNum );

  if( colours_allocated ) {	/* free it */
    XFreeColors( display, currentMap, colours, 16, 0x00 );
    colours_allocated = 0;
  }
  if( settings_current.bw_tv ) {
    for( i=0; i<16; i++ ) {	/* grey */
      c.red = c.green = c.blue = 
	    rgb_for_4[i * 3    ] * 19595 / 255 +
	    rgb_for_4[i * 3 + 1] * 38469 / 255 +
	    rgb_for_4[i * 3 + 2] *  7471 / 255;
      if( xdisplay_alloc_colour( &currentMap, &c ) )
    	    return 1;
      colours[i] = pal_grey[i] = c.pixel;
    }
  } else {
    for( i=0; i<16; i++ ) {	/* rgb */
      c.red   = rgb_for_4[i * 3    ] * 65535 / 255;
      c.green = rgb_for_4[i * 3 + 1] * 65535 / 255;
      c.blue  = rgb_for_4[i * 3 + 2] * 65535 / 255;
      if( xdisplay_alloc_colour( &currentMap, &c ) )
        return 1;
      colours[i] = pal_colour[i] = c.pixel;
    }
  }

  colours_allocated = 1;
  return 0;
}

static int
xdisplay_allocate_colours8( void )
{
  XColor c;
  int i, r, g, b;

  currentMap = DefaultColormap( display, xui_screenNum );
  
  if( colours_allocated ) {	/* free it */
    XFreeColors( display, currentMap, colours, 128, 0x00 );
    colours_allocated = 0;
  }
  i = 0;
  for( r=0; r<4; r++ )		/* rgb232 => 128 */
    for( g=0; g<8; g++ )
      for( b=0; b<4; b++ ) {
        if( settings_current.bw_tv ) {
          c.red = c.green = c.blue = r * 19595 / 3 +
				     g * 38469 / 7 +
				     b *  7471 / 3;
	} else {
          c.red = r * 65535 / 3;
	  c.green = g * 65535 / 7;
          c.blue = b * 65535 / 3;
	}
	if( xdisplay_alloc_colour( &currentMap, &c ) )
    	    return 1;
	colours[i++] = c.pixel;
      }
  colours_allocated = 1;

  return 0;
}
  
static int
xdisplay_allocate_gc( Window window, GC *new_gc )
{
  unsigned valuemask=0;
  XGCValues values;

  *new_gc = XCreateGC( display, window, valuemask, &values );

  return 0;
}

static int
xdisplay_allocate_image( void )
{
  struct sigaction handler;

  handler.sa_handler = xdisplay_catch_signal;
  sigemptyset( &handler.sa_mask );
  handler.sa_flags = 0;
  sigaction( SIGINT, &handler, NULL );

#ifdef X_USE_SHM
  shm_used = try_shm();
#endif				/* #ifdef X_USE_SHM */

  /* If SHM isn't available, or we're not using it for some reason,
     just get a normal image */
  if( !shm_used ) {
    image = XCreateImage( display, xdisplay_visual,
		       xdisplay_depth, ZPixmap, 0, NULL,
		       3 * DISPLAY_ASPECT_WIDTH,
		       3 * DISPLAY_SCREEN_HEIGHT, 8, 0 );
    if(!image) {
      fprintf(stderr,"%s: couldn't create image\n",fuse_progname);
      return 1;
    }

    if( ( image->data = malloc( image->bytes_per_line *
						 image->height ) ) == NULL ) {
      fprintf(stderr, "%s: out of memory for image data\n", fuse_progname);
      return 1;
    }
  }
  if( image ) {
    switch( image->red_mask ) {
    case 0xff0000:		/* 24bit/32bit 0RGB */
      rShift = 16, gShift = 8, bShift = 0;
      break;
    case 0x0000ff:		/* 24bit/32bit 0BGR */
      rShift = 0, gShift = 8, bShift = 16;
      break;
    case 0xff000000:		/* 24bit/32bit RGB0 */
      rShift = 24, gShift = 16, bShift = 8;
      break;
    case 0x0000ff00:		/* 24bit/32bit BGR0 */
      rShift = 8, gShift = 16, bShift = 24;
      break;
    case 0xf800:		/* 16 RGB */
    case 0x7c00:		/* 15 RGB */
      xdisplay_redpos = MSB_RED;
      break;
    case 0x001f:		/* 16/15 BGR */
      xdisplay_redpos = LSB_RED;
      break;
    }
  }

  return 0;
}

#ifdef X_USE_SHM
static int
try_shm( void )
{
  int id;
  int error;

  if( !XShmQueryExtension( display ) ) return 0;

  shm_eventtype = XShmGetEventBase( display ) + ShmCompletion;
  image = XShmCreateImage( display, xdisplay_visual,
			   xdisplay_depth, ZPixmap,
			   NULL, &shm_info,
			   3 * DISPLAY_ASPECT_WIDTH,
			   3 * DISPLAY_SCREEN_HEIGHT );
  if( !image ) return 0;

  /* Get an SHM to work with */
  id = get_shm_id( image->bytes_per_line * image->height );
  if( id == -1 ) return 0;

  /* Attempt to attach to the shared memory */
  shm_info.shmid = id;
  image->data = shm_info.shmaddr = shmat( id, 0, 0 );

  /* If we couldn't attach, remove the chunk and give up */
  if( image->data == (void*)-1 ) {
    shmctl( id, IPC_RMID, NULL );
    image->data = NULL;
    return 0;
  }

  /* This may generate an X error */
  xerror_error = 0; xerror_expecting = 1;
  error = !XShmAttach( display, &shm_info );

  /* Force any X errors to occur before we disable traps */
  XSync( display, False );
  xerror_expecting = 0;

  /* If we caught an error, don't use SHM */
  if( error || xerror_error ) {
    shmctl( id, IPC_RMID, NULL );
    shmdt( image->data ); image->data = NULL;
    return 0;
  }

  /* Now flag the chunk for deletion; this will take effect when
     everything has detached from it */
  shmctl( id, IPC_RMID, NULL );

  return 1;
}  

/* Get an SHM ID; also attempt to reclaim any stale chunks we find */
static int
get_shm_id( const int size )
{
  key_t key = 'F' << 24 | 'u' << 16 | 's' << 8 | 'e';
  struct shmid_ds shm;

  int id;

  int pollution = 5;
  
  do {
    /* See if a chunk already exists with this key */
    id = shmget( key, size, 0777 );

    /* If the chunk didn't already exist, try and create one for our
       use */
    if( id == -1 ) {
      id = shmget( key, size, IPC_CREAT | 0777 );
      continue;			/* And then jump to the end of the loop */
    }

    /* If the chunk already exists, try and get information about it */
    if( shmctl( id, IPC_STAT, &shm ) != -1 ) {

      /* If something's actively using this chunk, try another key */
      if( shm.shm_nattch ) {
	key++;
      } else {		/* Otherwise, attempt to remove the chunk */
	
	/* If we couldn't remove that chunk, try another key. If we
	   could, just try again */
	if( shmctl( id, IPC_RMID, NULL ) != 0 ) key++;
      }
    } else {		/* Couldn't get info on the chunk, so try next key */
      key++;
    }
    
    id = -1;		/* To prevent early exit from loop */

  } while( id == -1 && --pollution );

  return id;
}
#endif			/* #ifdef X_USE_SHM */

int
uidisplay_init( int width, int height )
{
  image_width  = width;
  image_height = height;
  if( !scaler_is_supported( current_scaler ) ) {
    if( machine_current->timex )
      scaler_select_scaler( SCALER_HALFSKIP );
    else
      scaler_select_scaler( SCALER_NORMAL );
  }

  register_scalers();
  display_ui_initialised = 1;

  display_refresh_all();
  return 0;
}

static void
resize_window( int w, int h )
{
    if( xdisplay_current_size != w / DISPLAY_ASPECT_WIDTH ) {
      XResizeWindow( display, xui_mainWindow, w, h );
      xdisplay_current_size = w / DISPLAY_ASPECT_WIDTH;
    }
}

static void
register_scalers( void )
{
  int f = -1;
  
  scaler_register_clear();
  scaler_select_bitformat( 565 );		/* 16bit always */

    if( xdisplay_depth == 4 ) {
      scaler_register( SCALER_NORMAL );
      if( machine_current->timex ) {
        scaler_register( SCALER_HALFSKIP );
        scaler_register( SCALER_TIMEX1_5X );
      } else {
        scaler_register( SCALER_DOUBLESIZE );
        scaler_register( SCALER_ADVMAME2X );

        scaler_register( SCALER_TRIPLESIZE );
        scaler_register( SCALER_ADVMAME3X );
      }
    } else {
      scaler_register( SCALER_NORMAL );
      scaler_register( SCALER_PALTV );
      if( machine_current->timex ) {
        scaler_register( SCALER_HALF ); 
        scaler_register( SCALER_HALFSKIP );
        scaler_register( SCALER_TIMEXTV );
        scaler_register( SCALER_TIMEX1_5X );
      } else {
        scaler_register( SCALER_DOUBLESIZE );
        scaler_register( SCALER_2XSAI );
        scaler_register( SCALER_SUPER2XSAI );
        scaler_register( SCALER_SUPEREAGLE );
        scaler_register( SCALER_ADVMAME2X );
        scaler_register( SCALER_TV2X );
        scaler_register( SCALER_DOTMATRIX );
        scaler_register( SCALER_PALTV2X );
        scaler_register( SCALER_HQ2X );

        scaler_register( SCALER_TRIPLESIZE );
        scaler_register( SCALER_ADVMAME3X );
        scaler_register( SCALER_TV3X );
        scaler_register( SCALER_PALTV3X );
        scaler_register( SCALER_HQ3X );
      }
    }
  if( current_scaler != SCALER_NUM )
    f = 4.0 * scaler_get_scaling_factor( current_scaler ) * 
	    ( machine_current->timex ? 2 : 1 );
  if( scaler_is_supported( current_scaler ) &&
	( xdisplay_current_size * 4 == f ) ) {
    uidisplay_hotswap_gfx_mode();
  } else {
    switch( xdisplay_current_size ) {
    case 1:
      scaler_select_scaler( machine_current->timex ? SCALER_HALF : SCALER_NORMAL );
      break;
    case 2:
      scaler_select_scaler( machine_current->timex ? SCALER_NORMAL : SCALER_DOUBLESIZE );
      break;
    case 3:
      scaler_select_scaler( machine_current->timex ? SCALER_TIMEX1_5X : SCALER_TRIPLESIZE );
      break;
    }
  }
}

int
xdisplay_configure_notify( int width, int height )
{
  int size;

  /* If we're the same size as before, nothing special needed */
  size = width / DISPLAY_ASPECT_WIDTH;
  if( size != height / DISPLAY_SCREEN_HEIGHT ) {	/* out of aspect */
    if( size > height / DISPLAY_SCREEN_HEIGHT ) {
      size = height / DISPLAY_SCREEN_HEIGHT;
      width = size * DISPLAY_ASPECT_WIDTH;
    } else {
      height = size * DISPLAY_SCREEN_HEIGHT;
    }
    xdisplay_current_size = 0;				/* force resize */
    resize_window( width, height );
  } else if( size == xdisplay_current_size ) {
    return 0;
  }

  /* Else set ourselves to the new height */
  xdisplay_current_size = size;
  
  /* Get a new scaler */
  register_scalers();

  return 0;
}

static void
xdisplay_update_rect_noscale( int x, int y, int w, int h )
{
 int yy, xx;

  /* Call putpixel multiple times */
  for( yy = y; yy < y + h; yy++ )
    for( xx = x; xx < x + w; xx++ )
      xdisplay_putpixel( xx, yy, &rgb_image[yy + 2][xx + 1] );
  /* Blit to the real screen at the frame end end */
  xdisplay_area( x, y, w, h );
}

static void
xdisplay_update_rect_scale( int x, int y, int w, int h )
{
  int yy = y, xx = x;

  y = y * image_scale >> 2;
  x = x * image_scale >> 2;
  scaler_proc16(
        (libspectrum_byte *)&(rgb_image[yy + 2][xx + 1]),
        rgb_pitch * sizeof(rgb_image[0][0]),
        (libspectrum_byte *)&(scaled_image[y][x]),
        scaled_pitch,
        w, h
      );

  w = w * image_scale >> 2;
  h = h * image_scale >> 2;

  /* Call putpixel multiple times */
  for( yy = y; yy < y + h; yy++ )
    for( xx = x; xx < x + w; xx++ )
      xdisplay_putpixel( xx, yy, &scaled_image[yy][xx] );
  /* Blit to the real screen */
  xdisplay_area( x, y, w, h );
}

void
uidisplay_frame_end( void ) 
{
  X_Rect *r, *last_rect;

  /* Force a full redraw if requested */
  if ( xdisplay_force_full_refresh ) {
    num_rects = 1;

    updated_rects[0].x = 0;
    updated_rects[0].y = 0;
    updated_rects[0].w = image_width;
    updated_rects[0].h = image_height;
  }

  if ( !( ui_widget_level >= 0 ) && num_rects == 0 ) return;

  last_rect = updated_rects + num_rects;

  for( r = updated_rects; r != last_rect; r++ )
    xdisplay_update_rect( r->x, r->y, r->w, r->h );
  num_rects = 0;
  xdisplay_force_full_refresh = 0;
}

void
uidisplay_frame_save( void )
{
  memcpy( rgb_image_backup, rgb_image, sizeof( rgb_image ) );
}

void
uidisplay_frame_restore( void )
{
  memcpy( rgb_image, rgb_image_backup, sizeof( rgb_image ) );
  xdisplay_update_rect( 0, 0, image_width, image_height );
}

void
uidisplay_area( int x, int y, int w, int h )
{
  if ( xdisplay_force_full_refresh )
    return;

  if( num_rects == MAX_UPDATE_RECT ) {
    xdisplay_force_full_refresh = 1;
    return;
  }

  /* Extend the dirty region by 1 pixel for scalers
     that "smear" the screen, e.g. 2xSAI */
  if( scaler_flags & SCALER_FLAGS_EXPAND )
    scaler_expander( &x, &y, &w, &h, image_width, image_height );

  updated_rects[num_rects].x = x;
  updated_rects[num_rects].y = y;
  updated_rects[num_rects].w = w;
  updated_rects[num_rects].h = h;
  num_rects++;
}

void
xdisplay_area( int x, int y, int w, int h )
{
/* e.g. dwm first expose with too big w and h */
  if( x + w > 3 * DISPLAY_ASPECT_WIDTH )
    w = 3 * DISPLAY_ASPECT_WIDTH - x;

  if( y + h > 3 * DISPLAY_SCREEN_HEIGHT )
    h = 3 * DISPLAY_SCREEN_HEIGHT - y;

  if( shm_used ) {
#ifdef X_USE_SHM
    XShmPutImage( display, xui_mainWindow, gc, image, x, y, x, y, w, h, True );
    /* FIXME: should wait for an ShmCompletion event here */
#endif				/* #ifdef X_USE_SHM */
  } else {
    XPutImage( display, xui_mainWindow, gc, image, x, y, x, y, w, h );
  }
}

static void
xdisplay_destroy_image(void)
{
  /* Free the XImage used to store screen data; also frees the malloc'd
     data */
#ifdef X_USE_SHM
  if( shm_used ) {
    XShmDetach( display, &shm_info );
    shmdt( shm_info.shmaddr );
    image->data = NULL;
    shm_used = 0;
  }
#endif
  if( image ) XDestroyImage( image ); image = NULL;
}

static void
xdisplay_setup_rgb_putpixel( void )
{
  switch( xdisplay_depth ) {
  case 4:
    xdisplay_putpixel = xdisplay_putpixel_4;
    break;
  case 8:
    xdisplay_putpixel = xdisplay_putpixel_8;
    break;
  case 15:
    xdisplay_putpixel = xdisplay_putpixel_15;
    break;
  case 16:
    xdisplay_putpixel = xdisplay_putpixel_16;
    break;
  case 24:
  case 32:
    xdisplay_putpixel = xdisplay_putpixel_24;
    break;
  }
  resize_window( scaled_image_w, scaled_image_h );
}

int
uidisplay_hotswap_gfx_mode( void )
{
  image_scale = 4.0 * scaler_get_scaling_factor( current_scaler );
  scaled_image_w = image_width  * image_scale >> 2;
  scaled_image_h = image_height * image_scale >> 2;

  if( current_scaler == SCALER_NORMAL )
    xdisplay_update_rect = xdisplay_update_rect_noscale;
  else
    xdisplay_update_rect = xdisplay_update_rect_scale;

  xdisplay_force_full_refresh = 1;

  if( settings_current.bw_tv != xdisplay_bw ) {
    xdisplay_bw = settings_current.bw_tv;
    if( xdisplay_depth == 4 )
      xdisplay_allocate_colours4();
    else if( xdisplay_depth == 8 )
      xdisplay_allocate_colours8();
  }
  xdisplay_setup_rgb_putpixel();
  display_refresh_all();
  return 0;
}

int
uidisplay_end( void )
{
  display_ui_initialised = 0;
  return 0;
}

static void
xdisplay_catch_signal( int sig )
{
  xdisplay_end();
  psignal( sig, fuse_progname );
  exit( 1 );
}

int
xdisplay_end( void )
{
  xdisplay_destroy_image();
  /* Free the allocated GC */
  if( gc ) XFreeGC( display, gc ); gc = 0;

  return 0;
}

/* Set one pixel in the display */
void
uidisplay_putpixel( int x, int y, int colour )
{
  libspectrum_word pc = settings_current.bw_tv ? pal_grey[ colour ] :
                        	pal_colour[ colour ];

  if( machine_current->timex ) {
    x <<= 1; y <<= 1;
    rgb_image[y + 2][x + 1] = pc;
    rgb_image[y + 2][x + 2] = pc;
    rgb_image[y + 3][x + 1] = pc;
    rgb_image[y + 3][x + 2] = pc;
  } else {
    rgb_image[y + 2][x + 1] = pc;
  }
}

/* Print the 8 pixels in `data' using ink colour `ink' and paper
   colour `paper' to the screen at ( (8*x) , y ) */
void
uidisplay_plot8( int x, int y, libspectrum_byte data,
	         libspectrum_byte ink, libspectrum_byte paper )
{
  libspectrum_word *dest;
  libspectrum_word pi = settings_current.bw_tv ? pal_grey[ ink ] :
                        	pal_colour[ ink ];
  libspectrum_word pp = settings_current.bw_tv ? pal_grey[ paper ] :
                        	pal_colour[ paper ];

  if( machine_current->timex ) {

    x <<= 4; y <<= 1;

    dest = &(rgb_image[y + 2][x + 1]);

    *(dest + rgb_pitch) = *dest = ( data & 0x80 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x80 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x40 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x40 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x20 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x20 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x10 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x10 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x08 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x08 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x04 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x04 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x02 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x02 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x01 ) ? pi : pp; dest++;
    *(dest + rgb_pitch) = *dest = ( data & 0x01 ) ? pi : pp;
  } else {
    x <<= 3;

    dest = &(rgb_image[y + 2][x + 1]);

    *(dest++) = ( data & 0x80 ) ? pi : pp;
    *(dest++) = ( data & 0x40 ) ? pi : pp;
    *(dest++) = ( data & 0x20 ) ? pi : pp;
    *(dest++) = ( data & 0x10 ) ? pi : pp;
    *(dest++) = ( data & 0x08 ) ? pi : pp;
    *(dest++) = ( data & 0x04 ) ? pi : pp;
    *(dest++) = ( data & 0x02 ) ? pi : pp;
    *dest     = ( data & 0x01 ) ? pi : pp;
  }
}

/* Print the 16 pixels in `data' using ink colour `ink' and paper
   colour `paper' to the screen at ( (16*x) , y ) */
void
uidisplay_plot16( int x, int y, libspectrum_word data,
                 libspectrum_byte ink, libspectrum_byte paper )
{
  libspectrum_word *dest;
  libspectrum_word pi = settings_current.bw_tv ? pal_grey[ ink ] :
                        	pal_colour[ ink ];
  libspectrum_word pp = settings_current.bw_tv ? pal_grey[ paper ] :
                        	pal_colour[ paper ];
  x <<= 4; y <<= 1;

  dest = &(rgb_image[y + 2][x + 1]);
  *(dest + rgb_pitch) = *dest = ( data & 0x8000 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x4000 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x2000 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x1000 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0800 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0400 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0200 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0100 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0080 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0040 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0020 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0010 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0008 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0004 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0002 ) ? pi : pp; dest++;
  *(dest + rgb_pitch) = *dest = ( data & 0x0001 ) ? pi : pp;
}

int
ui_statusbar_update_speed( float speed )
{
  char *list[2];
  char buffer[16];
  XTextProperty text;

  list[0] = buffer;
  list[1] = 0;
  snprintf( buffer, 16, "Fuse - %4.0f%%", speed );

  XStringListToTextProperty( list, 1, &text);
  XSetWMName( display, xui_mainWindow, &text );

  return 0;
}