Search
lxdream.org :: lxdream/src/gtkui/dump_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/dump_win.c
changeset 561:533f6b478071
prev537:d924be49e192
next669:ab344e42bca9
author nkeynes
date Fri Mar 28 12:32:25 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Merge lxdream-render branch (643:670) to trunk
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Implements the memory dump window.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <ctype.h>
    20 #include <assert.h>
    21 #include <string.h>
    22 #include <glib/gi18n.h>
    23 #include "mem.h"
    24 #include "gtkui/gtkui.h"
    26 #define MAX_DUMP_SIZE 4096
    28 #define DUMP_WINDOW_TAG 0xD4B9DA7A
    30 struct dump_window_info {
    31     uint32_t _tag;
    32     uint32_t start;
    33     uint32_t end;
    34     int flags;
    35     unsigned char *data;
    37     GtkWidget *window;
    38     GtkWidget *fromInput, *toInput;
    39     GtkWidget *textArea;
    40     GtkTextTag *changedTag;
    41     GtkTextBuffer *textBuffer;
    42     struct dump_window_info *next;
    43 };
    45 static dump_window_t dump_list_head = NULL;
    47 gboolean on_dump_window_delete_event( GtkWidget *widget, GdkEvent *event,
    48                                    gpointer user_data );
    49 void on_dump_window_button_view_clicked( GtkWidget *widget, gpointer user_data );
    50 void dump_window_set_text( dump_window_t data, unsigned char *old_data, unsigned char *new_data );
    53 dump_window_t dump_window_new( const gchar *title )
    54 {
    55     GtkWidget *vbox3;
    56     GtkWidget *hbox2;
    57     GtkWidget *dump_view_button;
    58     GtkWidget *scrolledwindow9;
    60     dump_window_t dump = g_malloc0( sizeof( struct dump_window_info ) );
    62     dump->_tag = DUMP_WINDOW_TAG;
    63     dump->next = dump_list_head;
    64     dump_list_head = dump;
    65     dump->data = NULL;
    66     dump->start = 0;
    67     dump->end = 0;
    68     dump->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    69     gtk_window_set_title (GTK_WINDOW (dump->window), _("Memory dump"));
    71     vbox3 = gtk_vbox_new (FALSE, 0);
    72     gtk_container_add (GTK_CONTAINER (dump->window), vbox3);
    74     hbox2 = gtk_hbox_new (FALSE, 0);
    75     dump->fromInput = gtk_entry_new ();
    76     gtk_entry_set_text( GTK_ENTRY(dump->fromInput), "" );
    77     dump->toInput = gtk_entry_new ();
    78     gtk_entry_set_text( GTK_ENTRY(dump->toInput), "" );
    79     dump_view_button = gtk_button_new_with_mnemonic (_("View"));
    81     gtk_box_pack_start (GTK_BOX (hbox2), gtk_label_new(_(" From ")), FALSE, FALSE, 0);
    82     gtk_box_pack_start (GTK_BOX (hbox2), dump->fromInput, FALSE, TRUE, 0);
    83     gtk_box_pack_start (GTK_BOX (hbox2), gtk_label_new(_(" To ")), FALSE, FALSE, 0);
    84     gtk_box_pack_start (GTK_BOX (hbox2), dump->toInput, FALSE, TRUE, 0);
    85     gtk_box_pack_start (GTK_BOX (hbox2), dump_view_button, FALSE, FALSE, 0);
    86     gtk_box_pack_start (GTK_BOX (hbox2), gtk_label_new (_("   ")), TRUE, TRUE, 0);
    87     gtk_box_pack_start (GTK_BOX (vbox3), hbox2, FALSE, TRUE, 3);
    89     dump->textArea = gtk_text_view_new ();
    90     dump->textBuffer = gtk_text_buffer_new(NULL);
    91     dump->changedTag = gtk_text_buffer_create_tag(dump->textBuffer, "changed",
    92 						  "foreground", "blue", NULL);
    93     gtk_widget_modify_font(GTK_WIDGET(dump->textArea),gui_fixed_font);
    94     gtk_text_view_set_editable(GTK_TEXT_VIEW(dump->textArea), FALSE);
    95     gtk_text_view_set_buffer(GTK_TEXT_VIEW(dump->textArea), dump->textBuffer);
    96     scrolledwindow9 = gtk_scrolled_window_new (NULL, NULL);
    97     gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow9), GTK_SHADOW_IN);
    98     gtk_container_add (GTK_CONTAINER (scrolledwindow9), dump->textArea);
    99     gtk_box_pack_start (GTK_BOX (vbox3), scrolledwindow9, TRUE, TRUE, 0);
   101     g_signal_connect (dump->window, "delete_event",
   102                       G_CALLBACK (on_dump_window_delete_event),
   103                       dump);
   104     g_signal_connect (dump_view_button, "clicked",
   105                       G_CALLBACK (on_dump_window_button_view_clicked),
   106                       dump);
   107     gtk_widget_show_all( dump->window );
   109     return dump;
   110 }
   112 void gtk_entry_set_hex_value( GtkEntry *entry, uint32_t value )
   113 {
   114     char buf[10];
   115     sprintf( buf, "%08X", value );
   116     gtk_entry_set_text( entry, buf );
   117 }
   119 uint32_t gtk_entry_get_hex_value( GtkEntry *entry, uint32_t defaultValue )
   120 {
   121     const gchar *text = gtk_entry_get_text(entry);
   122     if( text == NULL )
   123         return defaultValue;
   124     gchar *endptr;
   125     uint32_t value = strtoul( text, &endptr, 16 );
   126     if( text == endptr ) { /* invalid input */
   127         value = defaultValue;
   128         gtk_entry_set_hex_value( entry, value );
   129     }
   130     return value;
   131 }
   133 gboolean on_dump_window_delete_event( GtkWidget *widget, GdkEvent *event,
   134                                    gpointer user_data )
   135 {
   136     dump_window_t data = (dump_window_t)user_data;
   137     if( data->data != NULL )
   138         free( data->data );
   139     dump_window_t node = dump_list_head;
   140     if( node == data )
   141         dump_list_head = data->next;
   142     else {
   143         while( node->next != data ) {
   144             node = node->next;
   145             assert( node != NULL );
   146         }
   147         node->next = data->next;
   148     }
   149     free( data );
   150     return FALSE;
   151 }
   153 void on_dump_window_button_view_clicked( GtkWidget *widget, gpointer user_data )
   154 {
   155     dump_window_t data = (dump_window_t)user_data;
   156     uint32_t startVal, endVal;
   158     assert( data != NULL );
   159     assert( data->_tag == DUMP_WINDOW_TAG );
   161     startVal = gtk_entry_get_hex_value(GTK_ENTRY(data->fromInput), data->start);
   162     endVal = gtk_entry_get_hex_value(GTK_ENTRY(data->toInput), data->end);
   163     if( startVal != data->start || endVal != data->end ) {
   164         if( startVal > endVal ) {
   165             int tmp = endVal;
   166             endVal = startVal;
   167             startVal = tmp;
   168         }
   169         if( endVal > startVal + MAX_DUMP_SIZE )
   170             endVal = startVal + MAX_DUMP_SIZE;
   172         gtk_entry_set_hex_value(GTK_ENTRY(data->fromInput),startVal);
   173 	gtk_entry_set_hex_value(GTK_ENTRY(data->toInput),endVal);
   174         data->start = startVal;
   175         data->end = endVal;
   177         if( data->data != NULL ) {
   178             free( data->data );
   179             data->data = NULL;
   180         }
   181         if( startVal != endVal ) {
   182             data->data = malloc( endVal - startVal );
   183             mem_copy_from_sh4( data->data, startVal, endVal-startVal );
   184             dump_window_set_text( data, data->data, data->data );
   185         }
   186     }
   187 }
   189 void dump_window_update( dump_window_t data )
   190 {
   191     if( data->data == NULL )
   192         return;
   193     unsigned char tmp[data->end-data->start];
   194     int length = data->end-data->start;
   195     memcpy( tmp, data->data, length );
   196     mem_copy_from_sh4( data->data, data->start, length );
   197     dump_window_set_text( data, tmp, data->data );
   198 }
   200 void dump_window_update_all( )
   201 {
   202     dump_window_t node = dump_list_head;
   203     while( node != NULL ) {
   204         dump_window_update(node);
   205         node = node->next;
   206     }
   207 }
   209 void dump_window_set_text( dump_window_t data, unsigned char *old_data, unsigned char *new_data )
   210 {
   211     GtkTextBuffer *buf = data->textBuffer;
   212     GtkTextTag *changedTag = data->changedTag;
   213     GtkTextIter iter, endIter;
   214     int i, j, offset;
   215     /* Clear out the buffer */
   216     gtk_text_buffer_get_start_iter(buf,&iter);
   217     gtk_text_buffer_get_end_iter(buf,&endIter);
   218     gtk_text_buffer_delete(buf,&iter,&endIter);
   219     gtk_text_buffer_get_start_iter(buf,&iter);
   221     for( offset = 0, i=data->start; i<data->end; i+=16, offset+=16 ) {
   222         char text[80];
   223         sprintf(text, "%08X:", i );
   224         gtk_text_buffer_insert( buf, &iter, text, 9 );
   225         for( j=0; j<16; j++ ) {
   226             if( j%4 == 0 )
   227                 gtk_text_buffer_insert( buf, &iter, " ", 1 );
   228             if( i+j < data->end ) {
   229                 int oldVal = ((int)old_data[offset+j])&0xFF;
   230                 int newVal = ((int)new_data[offset+j])&0xFF;
   231                 sprintf(text, "%02X ", newVal);
   232                 if( oldVal == newVal )
   233                     gtk_text_buffer_insert( buf, &iter, text, 3 );
   234                 else
   235                     gtk_text_buffer_insert_with_tags( buf, &iter, text, 3,
   236                                                       changedTag, NULL );
   237             } else {
   238                 gtk_text_buffer_insert( buf, &iter, "   ", 3 );
   239             }
   240         }
   241         gtk_text_buffer_insert( buf, &iter, "  ", 2 );
   242         for( j=0; j<16 && i+j < data->end; j++ ) {
   243             int oldVal = ((int)old_data[offset+j])&0xFF;
   244             int newVal = ((int)new_data[offset+j])&0xFF;
   245             if( isprint(newVal) )
   246                 sprintf( text, "%c", newVal );
   247             else strcpy( text, "." );
   248             if( oldVal == newVal )
   249                 gtk_text_buffer_insert( buf, &iter, text, 1 );
   250             else
   251                 gtk_text_buffer_insert_with_tags( buf, &iter, text, 1,
   252                                                   changedTag, NULL );
   253         }
   254         gtk_text_buffer_insert( buf, &iter, "\n", 1 );
   255     }
   256 }
.