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