Search
lxdream.org :: lxdream/src/drivers/video_gtk.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gtk.c
changeset 180:e6dcf9b65658
prev160:17c4c0e4f9ce
next335:fb890e1814c0
author nkeynes
date Sun Jul 02 04:59:00 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Merge changes made on the MILESTONE1 branch back into head (mostly
release touchups)
file annotate diff log raw
nkeynes@94
     1
/**
nkeynes@180
     2
 * $Id: video_gtk.c,v 1.8 2006-07-02 04:59:00 nkeynes Exp $
nkeynes@94
     3
 *
nkeynes@94
     4
 * The PC side of the video support (responsible for actually displaying / 
nkeynes@94
     5
 * rendering frames)
nkeynes@94
     6
 *
nkeynes@94
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@94
     8
 *
nkeynes@94
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@94
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@94
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@94
    12
 * (at your option) any later version.
nkeynes@94
    13
 *
nkeynes@94
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@94
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@94
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@94
    17
 * GNU General Public License for more details.
nkeynes@94
    18
 */
nkeynes@94
    19
nkeynes@94
    20
#include <gnome.h>
nkeynes@94
    21
#include <gdk/gdkx.h>
nkeynes@94
    22
#include <stdint.h>
nkeynes@180
    23
#include "dream.h"
nkeynes@144
    24
#include "display.h"
nkeynes@94
    25
#include "drivers/video_x11.h"
nkeynes@94
    26
nkeynes@94
    27
GdkImage *video_img = NULL;
nkeynes@94
    28
GtkWindow *video_win = NULL;
nkeynes@94
    29
GtkWidget *video_area = NULL;
nkeynes@94
    30
uint32_t video_width = 640;
nkeynes@94
    31
uint32_t video_height = 480;
nkeynes@94
    32
uint32_t video_frame_count = 0;
nkeynes@94
    33
nkeynes@144
    34
uint16_t video_gtk_resolve_keysym( const gchar *keysym );
nkeynes@94
    35
gboolean video_gtk_set_output_format( uint32_t width, uint32_t height,  
nkeynes@94
    36
				      int colour_format );
nkeynes@108
    37
gboolean video_gtk_set_render_format( uint32_t width, uint32_t height,  
nkeynes@144
    38
				      int colour_format, gboolean texture );
nkeynes@94
    39
gboolean video_gtk_display_frame( video_buffer_t frame );
nkeynes@94
    40
gboolean video_gtk_blank( uint32_t rgb );
nkeynes@94
    41
nkeynes@144
    42
struct display_driver display_gtk_driver = { "gtk", 
nkeynes@144
    43
					   NULL,
nkeynes@144
    44
					   NULL,
nkeynes@144
    45
					   video_gtk_resolve_keysym,
nkeynes@144
    46
					   video_gtk_set_output_format,
nkeynes@144
    47
					   video_gtk_set_render_format,
nkeynes@144
    48
					   video_gtk_display_frame,
nkeynes@144
    49
					   video_gtk_blank,
nkeynes@144
    50
					   video_glx_swap_buffers };
nkeynes@144
    51
nkeynes@144
    52
gboolean video_gtk_keydown_callback(GtkWidget       *widget,
nkeynes@144
    53
				     GdkEventKey     *event,
nkeynes@144
    54
				     gpointer         user_data)
nkeynes@144
    55
{
nkeynes@144
    56
    input_event_keydown( event->keyval );
nkeynes@144
    57
}
nkeynes@144
    58
nkeynes@144
    59
uint16_t video_gtk_resolve_keysym( const gchar *keysym )
nkeynes@144
    60
{
nkeynes@144
    61
    int val = gdk_keyval_from_name( keysym );
nkeynes@144
    62
    if( val == GDK_VoidSymbol )
nkeynes@144
    63
	return 0;
nkeynes@144
    64
    return (uint16_t)val;
nkeynes@144
    65
}
nkeynes@144
    66
nkeynes@144
    67
gboolean video_gtk_keyup_callback(GtkWidget       *widget,
nkeynes@144
    68
				  GdkEventKey     *event,
nkeynes@144
    69
				  gpointer         user_data)
nkeynes@144
    70
{
nkeynes@144
    71
    input_event_keyup( event->keyval );
nkeynes@144
    72
}
nkeynes@94
    73
nkeynes@94
    74
gboolean video_gtk_set_output_format( uint32_t width, uint32_t height,  
nkeynes@94
    75
				      int colour_format )
nkeynes@94
    76
{
nkeynes@94
    77
    video_width = width;
nkeynes@94
    78
    video_height = height;
nkeynes@94
    79
    if( video_win == NULL ) {
nkeynes@94
    80
	video_win = GTK_WINDOW(gtk_window_new( GTK_WINDOW_TOPLEVEL ));
nkeynes@180
    81
	gtk_window_set_title( video_win, APP_NAME " - Emulation Window" );
nkeynes@94
    82
	gtk_window_set_policy( video_win, FALSE, FALSE, FALSE );
nkeynes@94
    83
	gtk_window_set_default_size( video_win, width, height );
nkeynes@94
    84
    
nkeynes@144
    85
	g_signal_connect( video_win, "key_press_event", 
nkeynes@144
    86
			  G_CALLBACK(video_gtk_keydown_callback), NULL );
nkeynes@144
    87
	g_signal_connect( video_win, "key_release_event", 
nkeynes@144
    88
			  G_CALLBACK(video_gtk_keyup_callback), NULL );
nkeynes@144
    89
	gtk_widget_add_events( GTK_WIDGET(video_win), 
nkeynes@144
    90
			       GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
nkeynes@144
    91
			       GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK );
nkeynes@94
    92
	video_area = gtk_image_new();
nkeynes@94
    93
	gtk_widget_show( GTK_WIDGET(video_area) );
nkeynes@94
    94
	gtk_container_add( GTK_CONTAINER(video_win), GTK_WIDGET(video_area) );
nkeynes@94
    95
	gtk_widget_show( GTK_WIDGET(video_win) );
nkeynes@94
    96
	video_x11_set_display( gdk_x11_display_get_xdisplay( gtk_widget_get_display(video_area)),
nkeynes@94
    97
			       gdk_x11_screen_get_xscreen( gtk_widget_get_screen(video_area)),
nkeynes@94
    98
			       GDK_WINDOW_XWINDOW( GTK_WIDGET(video_win)->window ) );
nkeynes@94
    99
			       
nkeynes@94
   100
    }
nkeynes@94
   101
    gtk_window_set_default_size( video_win, width, height );
nkeynes@94
   102
    video_img = gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(),
nkeynes@94
   103
			       width, height );
nkeynes@94
   104
    gtk_image_set_from_image( GTK_IMAGE(video_area), video_img, NULL );
nkeynes@94
   105
    /* Note old image is auto de-refed */
nkeynes@94
   106
    return TRUE;
nkeynes@94
   107
}
nkeynes@94
   108
nkeynes@94
   109
nkeynes@94
   110
/**
nkeynes@94
   111
 * Fill the entire frame with the specified colour (00RRGGBB)
nkeynes@94
   112
 */
nkeynes@94
   113
gboolean video_gtk_blank( uint32_t colour ) 
nkeynes@94
   114
{
nkeynes@160
   115
    video_glx_blank( video_width, video_height, colour );
nkeynes@94
   116
}
nkeynes@94
   117
nkeynes@94
   118
gboolean video_gtk_display_frame( video_buffer_t frame ) 
nkeynes@94
   119
{
nkeynes@94
   120
    uint32_t bytes_per_line, x, y;
nkeynes@94
   121
    char *src = frame->data;
nkeynes@94
   122
    char *dest = video_img->mem;
nkeynes@94
   123
nkeynes@112
   124
    return video_glx_display_frame( frame );
nkeynes@112
   125
nkeynes@94
   126
    switch( frame->colour_format ) {
nkeynes@103
   127
    case COLFMT_ARGB1555:
nkeynes@94
   128
	for( y=0; y < frame->vres; y++ ) {
nkeynes@94
   129
	    uint16_t *p = (uint16_t *)src;
nkeynes@103
   130
	    for( x=0; x < frame->hres; x++ ) {
nkeynes@94
   131
		uint16_t pixel = *p++;
nkeynes@94
   132
		*dest++ = (pixel & 0x1F) << 3;
nkeynes@94
   133
		*dest++ = (pixel & 0x3E0) >> 2;
nkeynes@94
   134
		*dest++ = (pixel & 0x7C00) >> 7;
nkeynes@94
   135
		*dest++ = 0;
nkeynes@94
   136
	    }
nkeynes@94
   137
	    src += frame->rowstride;
nkeynes@94
   138
	}
nkeynes@94
   139
	break;
nkeynes@103
   140
    case COLFMT_RGB565:
nkeynes@94
   141
	for( y=0; y < frame->vres; y++ ) {
nkeynes@94
   142
	    uint16_t *p = (uint16_t *)src;
nkeynes@103
   143
	    for( x=0; x < frame->hres; x++ ) {
nkeynes@94
   144
		uint16_t pixel = *p++;
nkeynes@94
   145
		*dest++ = (pixel & 0x1F) << 3;
nkeynes@94
   146
		*dest++ = (pixel & 0x7E0) >> 3;
nkeynes@94
   147
		*dest++ = (pixel & 0xF800) >> 8;
nkeynes@94
   148
		*dest++ = 0;
nkeynes@94
   149
	    }
nkeynes@94
   150
	    src += frame->rowstride;
nkeynes@94
   151
	}
nkeynes@94
   152
	break;
nkeynes@103
   153
    case COLFMT_RGB888:
nkeynes@103
   154
	for( y=0; y< frame->vres; y++ ) {
nkeynes@103
   155
	    char *p = src;
nkeynes@103
   156
	    for( x=0; x < frame->hres; x++ ) {
nkeynes@103
   157
		*dest++ = *p++;
nkeynes@103
   158
		*dest++ = *p++;
nkeynes@103
   159
		*dest++ = *p++;
nkeynes@103
   160
		*dest++ = 0;
nkeynes@103
   161
	    }
nkeynes@103
   162
	    src += frame->rowstride;
nkeynes@103
   163
	}
nkeynes@103
   164
	break;
nkeynes@103
   165
    case COLFMT_ARGB8888:
nkeynes@94
   166
	bytes_per_line = frame->hres << 2;
nkeynes@94
   167
	if( bytes_per_line == frame->rowstride ) {
nkeynes@94
   168
	    /* A little bit faster */
nkeynes@94
   169
	    memcpy( dest, src, bytes_per_line * frame->vres );
nkeynes@94
   170
	} else {
nkeynes@94
   171
	    for( y=0; y< frame->vres; y++ ) {
nkeynes@94
   172
		memcpy( dest, src, bytes_per_line );
nkeynes@94
   173
		src += frame->rowstride;
nkeynes@94
   174
		dest += bytes_per_line;
nkeynes@94
   175
	    }
nkeynes@94
   176
	}
nkeynes@94
   177
	break;
nkeynes@94
   178
    }
nkeynes@94
   179
    gtk_widget_queue_draw( video_area );
nkeynes@94
   180
    return TRUE;
nkeynes@94
   181
}
nkeynes@94
   182
nkeynes@108
   183
gboolean video_gtk_set_render_format( uint32_t width, uint32_t height,  
nkeynes@144
   184
				      int colour_format, gboolean texture )
nkeynes@108
   185
{
nkeynes@108
   186
    return video_glx_set_render_format( 0, 0, width, height );
nkeynes@108
   187
}
.