File: picture.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 (194 lines) | stat: -rw-r--r-- 5,217 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
/* picture.c: GTK+ routines to draw the keyboard picture
   Copyright (c) 2002-2005 Philip Kendall

   $Id: picture.c 4090 2009-09-03 12:06:32Z 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <libspectrum.h>

#include "display.h"
#include "fuse.h"
#include "gtkinternals.h"
#include "ui/ui.h"
#include "utils.h"

/* An RGB image of the keyboard picture */
static guchar picture[ DISPLAY_SCREEN_HEIGHT * DISPLAY_ASPECT_WIDTH * 4 ];
static const gint picture_pitch = DISPLAY_ASPECT_WIDTH * 4;

static int dialog_created = 0;

static int read_screen( const char *filename, utils_file *screen );
static void draw_screen( libspectrum_byte *screen, int border );
static gint
picture_expose( GtkWidget *widget, GdkEvent *event, gpointer data );

static GtkWidget *dialog;

int
gtkui_picture( const char *filename, int border )
{
  utils_file screen;

  GtkWidget *drawing_area;

  if( !dialog_created ) {

    if( read_screen( filename, &screen ) ) {
      return 1;
    }

    draw_screen( screen.buffer, border );

    if( utils_close_file( &screen ) ) {
      return 1;
    }

    dialog = gtkstock_dialog_new( "Fuse - Keyboard",
				  GTK_SIGNAL_FUNC( gtk_widget_hide ) );
  
    drawing_area = gtk_drawing_area_new();
    gtk_drawing_area_size( GTK_DRAWING_AREA( drawing_area ),
			   DISPLAY_ASPECT_WIDTH, DISPLAY_SCREEN_HEIGHT );
    gtk_signal_connect( GTK_OBJECT( drawing_area ),
			"expose_event", GTK_SIGNAL_FUNC( picture_expose ),
			NULL );
    gtk_container_add( GTK_CONTAINER( GTK_DIALOG( dialog )->vbox ),
		       drawing_area );

    gtkstock_create_close( dialog, NULL, GTK_SIGNAL_FUNC( gtk_widget_hide ),
			   FALSE );

    /* Stop users resizing this window */
    gtk_window_set_policy( GTK_WINDOW( dialog ), FALSE, FALSE, TRUE );

    dialog_created = 1;
  }

  gtk_widget_show_all( dialog );

  return 0;
}

static int
read_screen( const char *filename, utils_file *screen )
{
  int error;
  compat_fd fd;

  fd = utils_find_auxiliary_file( filename, UTILS_AUXILIARY_LIB );
  if( fd == COMPAT_FILE_OPEN_FAILED ) {
    ui_error( UI_ERROR_ERROR, "couldn't find keyboard picture ('%s')",
	      filename );
    return 1;
  }
  
  error = utils_read_fd( fd, filename, screen );
  if( error ) return error;

  if( screen->length != 6912 ) {
    utils_close_file( screen );
    ui_error( UI_ERROR_ERROR, "keyboard picture ('%s') is not 6912 bytes long",
	      filename );
    return 1;
  }

  return 0;
}

static void
draw_screen( libspectrum_byte *screen, int border )
{
  int i, x, y, ink, paper;
  libspectrum_byte attr, data; 

  for( y=0; y < DISPLAY_BORDER_HEIGHT; y++ ) {
    for( x=0; x < DISPLAY_ASPECT_WIDTH; x++ ) {
      *(libspectrum_dword*)( picture + y * picture_pitch + 4 * x ) =
	gtkdisplay_colours[border];
      *(libspectrum_dword*)(
          picture +
	  ( y + DISPLAY_BORDER_HEIGHT + DISPLAY_HEIGHT ) * picture_pitch +
	  4 * x
	) = gtkdisplay_colours[ border ];
    }
  }

  for( y=0; y<DISPLAY_HEIGHT; y++ ) {

    for( x=0; x < DISPLAY_BORDER_ASPECT_WIDTH; x++ ) {
      *(libspectrum_dword*)
	(picture + ( y + DISPLAY_BORDER_HEIGHT) * picture_pitch + 4 * x) =
	gtkdisplay_colours[ border ];
      *(libspectrum_dword*)(
          picture +
	  ( y + DISPLAY_BORDER_HEIGHT ) * picture_pitch +
	  4 * ( x+DISPLAY_ASPECT_WIDTH-DISPLAY_BORDER_ASPECT_WIDTH )
	) = gtkdisplay_colours[ border ];
    }

    for( x=0; x < DISPLAY_WIDTH_COLS; x++ ) {

      attr = screen[ display_attr_start[y] + x ];

      ink = ( attr & 0x07 ) + ( ( attr & 0x40 ) >> 3 );
      paper = ( attr & ( 0x0f << 3 ) ) >> 3;

      data = screen[ display_line_start[y]+x ];

      for( i=0; i<8; i++ ) {
	*(libspectrum_dword*)(
	    picture +
	    ( y + DISPLAY_BORDER_HEIGHT ) * picture_pitch +
	    4 * ( 8 * x + DISPLAY_BORDER_ASPECT_WIDTH + i )
	  ) = ( data & 0x80 ) ? gtkdisplay_colours[ ink ]
	                      : gtkdisplay_colours[ paper ];
	data <<= 1;
      }
    }

  }
}

static gint
picture_expose( GtkWidget *widget, GdkEvent *event, gpointer data GCC_UNUSED )
{
  int x = event->expose.area.x, y = event->expose.area.y;

  gdk_draw_rgb_32_image( widget->window,
			 widget->style->fg_gc[ GTK_STATE_NORMAL ],
			 x, y,
			 event->expose.area.width, event->expose.area.height,
			 GDK_RGB_DITHER_NONE,
			 picture + y * picture_pitch + 4 * x, picture_pitch );

  return TRUE;
}