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 736:a02d1475ccfd
prev669:ab344e42bca9
next755:ab873907b00e
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
file annotate diff log raw
nkeynes@2
     1
/**
nkeynes@561
     2
 * $Id$
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 <ctype.h>
nkeynes@2
    20
#include <assert.h>
nkeynes@484
    21
#include <string.h>
nkeynes@669
    22
#include <stdlib.h>
nkeynes@484
    23
#include <glib/gi18n.h>
nkeynes@2
    24
#include "mem.h"
nkeynes@537
    25
#include "gtkui/gtkui.h"
nkeynes@2
    26
nkeynes@2
    27
#define MAX_DUMP_SIZE 4096
nkeynes@2
    28
nkeynes@457
    29
#define DUMP_WINDOW_TAG 0xD4B9DA7A
nkeynes@2
    30
nkeynes@457
    31
struct dump_window_info {
nkeynes@2
    32
    uint32_t _tag;
nkeynes@2
    33
    uint32_t start;
nkeynes@2
    34
    uint32_t end;
nkeynes@2
    35
    int flags;
nkeynes@430
    36
    unsigned char *data;
nkeynes@2
    37
nkeynes@457
    38
    GtkWidget *window;
nkeynes@457
    39
    GtkWidget *fromInput, *toInput;
nkeynes@457
    40
    GtkWidget *textArea;
nkeynes@2
    41
    GtkTextTag *changedTag;
nkeynes@2
    42
    GtkTextBuffer *textBuffer;
nkeynes@457
    43
    struct dump_window_info *next;
nkeynes@457
    44
};
nkeynes@2
    45
nkeynes@457
    46
static dump_window_t dump_list_head = NULL;
nkeynes@2
    47
nkeynes@455
    48
gboolean on_dump_window_delete_event( GtkWidget *widget, GdkEvent *event,
nkeynes@736
    49
                                      gpointer user_data );
nkeynes@455
    50
void on_dump_window_button_view_clicked( GtkWidget *widget, gpointer user_data );
nkeynes@457
    51
void dump_window_set_text( dump_window_t data, unsigned char *old_data, unsigned char *new_data );
nkeynes@2
    52
nkeynes@2
    53
nkeynes@457
    54
dump_window_t dump_window_new( const gchar *title )
nkeynes@457
    55
{
nkeynes@457
    56
    GtkWidget *vbox3;
nkeynes@457
    57
    GtkWidget *hbox2;
nkeynes@457
    58
    GtkWidget *dump_view_button;
nkeynes@457
    59
    GtkWidget *scrolledwindow9;
nkeynes@457
    60
nkeynes@457
    61
    dump_window_t dump = g_malloc0( sizeof( struct dump_window_info ) );
nkeynes@457
    62
nkeynes@457
    63
    dump->_tag = DUMP_WINDOW_TAG;
nkeynes@457
    64
    dump->next = dump_list_head;
nkeynes@457
    65
    dump_list_head = dump;
nkeynes@457
    66
    dump->data = NULL;
nkeynes@457
    67
    dump->start = 0;
nkeynes@457
    68
    dump->end = 0;
nkeynes@457
    69
    dump->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
nkeynes@457
    70
    gtk_window_set_title (GTK_WINDOW (dump->window), _("Memory dump"));
nkeynes@457
    71
nkeynes@457
    72
    vbox3 = gtk_vbox_new (FALSE, 0);
nkeynes@457
    73
    gtk_container_add (GTK_CONTAINER (dump->window), vbox3);
nkeynes@457
    74
nkeynes@457
    75
    hbox2 = gtk_hbox_new (FALSE, 0);
nkeynes@457
    76
    dump->fromInput = gtk_entry_new ();
nkeynes@457
    77
    gtk_entry_set_text( GTK_ENTRY(dump->fromInput), "" );
nkeynes@457
    78
    dump->toInput = gtk_entry_new ();
nkeynes@457
    79
    gtk_entry_set_text( GTK_ENTRY(dump->toInput), "" );
nkeynes@457
    80
    dump_view_button = gtk_button_new_with_mnemonic (_("View"));
nkeynes@736
    81
nkeynes@457
    82
    gtk_box_pack_start (GTK_BOX (hbox2), gtk_label_new(_(" From ")), FALSE, FALSE, 0);
nkeynes@457
    83
    gtk_box_pack_start (GTK_BOX (hbox2), dump->fromInput, FALSE, TRUE, 0);
nkeynes@457
    84
    gtk_box_pack_start (GTK_BOX (hbox2), gtk_label_new(_(" To ")), FALSE, FALSE, 0);
nkeynes@457
    85
    gtk_box_pack_start (GTK_BOX (hbox2), dump->toInput, FALSE, TRUE, 0);
nkeynes@457
    86
    gtk_box_pack_start (GTK_BOX (hbox2), dump_view_button, FALSE, FALSE, 0);
nkeynes@457
    87
    gtk_box_pack_start (GTK_BOX (hbox2), gtk_label_new (_("   ")), TRUE, TRUE, 0);
nkeynes@457
    88
    gtk_box_pack_start (GTK_BOX (vbox3), hbox2, FALSE, TRUE, 3);
nkeynes@457
    89
nkeynes@457
    90
    dump->textArea = gtk_text_view_new ();
nkeynes@457
    91
    dump->textBuffer = gtk_text_buffer_new(NULL);
nkeynes@457
    92
    dump->changedTag = gtk_text_buffer_create_tag(dump->textBuffer, "changed",
nkeynes@736
    93
            "foreground", "blue", NULL);
nkeynes@457
    94
    gtk_widget_modify_font(GTK_WIDGET(dump->textArea),gui_fixed_font);
nkeynes@457
    95
    gtk_text_view_set_editable(GTK_TEXT_VIEW(dump->textArea), FALSE);
nkeynes@457
    96
    gtk_text_view_set_buffer(GTK_TEXT_VIEW(dump->textArea), dump->textBuffer);
nkeynes@457
    97
    scrolledwindow9 = gtk_scrolled_window_new (NULL, NULL);
nkeynes@457
    98
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow9), GTK_SHADOW_IN);
nkeynes@457
    99
    gtk_container_add (GTK_CONTAINER (scrolledwindow9), dump->textArea);
nkeynes@457
   100
    gtk_box_pack_start (GTK_BOX (vbox3), scrolledwindow9, TRUE, TRUE, 0);
nkeynes@736
   101
nkeynes@457
   102
    g_signal_connect (dump->window, "delete_event",
nkeynes@455
   103
                      G_CALLBACK (on_dump_window_delete_event),
nkeynes@457
   104
                      dump);
nkeynes@457
   105
    g_signal_connect (dump_view_button, "clicked",
nkeynes@455
   106
                      G_CALLBACK (on_dump_window_button_view_clicked),
nkeynes@457
   107
                      dump);
nkeynes@457
   108
    gtk_widget_show_all( dump->window );
nkeynes@457
   109
nkeynes@457
   110
    return dump;
nkeynes@2
   111
}
nkeynes@2
   112
nkeynes@457
   113
void gtk_entry_set_hex_value( GtkEntry *entry, uint32_t value )
nkeynes@457
   114
{
nkeynes@457
   115
    char buf[10];
nkeynes@457
   116
    sprintf( buf, "%08X", value );
nkeynes@457
   117
    gtk_entry_set_text( entry, buf );
nkeynes@457
   118
}
nkeynes@435
   119
nkeynes@435
   120
uint32_t gtk_entry_get_hex_value( GtkEntry *entry, uint32_t defaultValue )
nkeynes@435
   121
{
nkeynes@435
   122
    const gchar *text = gtk_entry_get_text(entry);
nkeynes@435
   123
    if( text == NULL )
nkeynes@435
   124
        return defaultValue;
nkeynes@435
   125
    gchar *endptr;
nkeynes@435
   126
    uint32_t value = strtoul( text, &endptr, 16 );
nkeynes@435
   127
    if( text == endptr ) { /* invalid input */
nkeynes@435
   128
        value = defaultValue;
nkeynes@435
   129
        gtk_entry_set_hex_value( entry, value );
nkeynes@435
   130
    }
nkeynes@435
   131
    return value;
nkeynes@435
   132
}
nkeynes@435
   133
nkeynes@455
   134
gboolean on_dump_window_delete_event( GtkWidget *widget, GdkEvent *event,
nkeynes@736
   135
                                      gpointer user_data )
nkeynes@2
   136
{
nkeynes@457
   137
    dump_window_t data = (dump_window_t)user_data;
nkeynes@2
   138
    if( data->data != NULL )
nkeynes@2
   139
        free( data->data );
nkeynes@457
   140
    dump_window_t node = dump_list_head;
nkeynes@2
   141
    if( node == data )
nkeynes@2
   142
        dump_list_head = data->next;
nkeynes@2
   143
    else {
nkeynes@2
   144
        while( node->next != data ) {
nkeynes@2
   145
            node = node->next;
nkeynes@2
   146
            assert( node != NULL );
nkeynes@2
   147
        }
nkeynes@2
   148
        node->next = data->next;
nkeynes@2
   149
    }
nkeynes@2
   150
    free( data );
nkeynes@2
   151
    return FALSE;
nkeynes@2
   152
}
nkeynes@2
   153
nkeynes@455
   154
void on_dump_window_button_view_clicked( GtkWidget *widget, gpointer user_data )
nkeynes@2
   155
{
nkeynes@457
   156
    dump_window_t data = (dump_window_t)user_data;
nkeynes@2
   157
    uint32_t startVal, endVal;
nkeynes@2
   158
nkeynes@2
   159
    assert( data != NULL );
nkeynes@457
   160
    assert( data->_tag == DUMP_WINDOW_TAG );
nkeynes@736
   161
nkeynes@457
   162
    startVal = gtk_entry_get_hex_value(GTK_ENTRY(data->fromInput), data->start);
nkeynes@457
   163
    endVal = gtk_entry_get_hex_value(GTK_ENTRY(data->toInput), data->end);
nkeynes@2
   164
    if( startVal != data->start || endVal != data->end ) {
nkeynes@2
   165
        if( startVal > endVal ) {
nkeynes@2
   166
            int tmp = endVal;
nkeynes@2
   167
            endVal = startVal;
nkeynes@2
   168
            startVal = tmp;
nkeynes@2
   169
        }
nkeynes@2
   170
        if( endVal > startVal + MAX_DUMP_SIZE )
nkeynes@2
   171
            endVal = startVal + MAX_DUMP_SIZE;
nkeynes@2
   172
nkeynes@457
   173
        gtk_entry_set_hex_value(GTK_ENTRY(data->fromInput),startVal);
nkeynes@736
   174
        gtk_entry_set_hex_value(GTK_ENTRY(data->toInput),endVal);
nkeynes@2
   175
        data->start = startVal;
nkeynes@2
   176
        data->end = endVal;
nkeynes@2
   177
nkeynes@2
   178
        if( data->data != NULL ) {
nkeynes@2
   179
            free( data->data );
nkeynes@2
   180
            data->data = NULL;
nkeynes@2
   181
        }
nkeynes@2
   182
        if( startVal != endVal ) {
nkeynes@2
   183
            data->data = malloc( endVal - startVal );
nkeynes@2
   184
            mem_copy_from_sh4( data->data, startVal, endVal-startVal );
nkeynes@455
   185
            dump_window_set_text( data, data->data, data->data );
nkeynes@2
   186
        }
nkeynes@2
   187
    }
nkeynes@2
   188
}
nkeynes@2
   189
nkeynes@457
   190
void dump_window_update( dump_window_t data )
nkeynes@2
   191
{
nkeynes@2
   192
    if( data->data == NULL )
nkeynes@2
   193
        return;
nkeynes@430
   194
    unsigned char tmp[data->end-data->start];
nkeynes@2
   195
    int length = data->end-data->start;
nkeynes@2
   196
    memcpy( tmp, data->data, length );
nkeynes@2
   197
    mem_copy_from_sh4( data->data, data->start, length );
nkeynes@455
   198
    dump_window_set_text( data, tmp, data->data );
nkeynes@2
   199
}
nkeynes@2
   200
nkeynes@455
   201
void dump_window_update_all( )
nkeynes@2
   202
{
nkeynes@457
   203
    dump_window_t node = dump_list_head;
nkeynes@2
   204
    while( node != NULL ) {
nkeynes@455
   205
        dump_window_update(node);
nkeynes@2
   206
        node = node->next;
nkeynes@2
   207
    }
nkeynes@2
   208
}
nkeynes@2
   209
nkeynes@457
   210
void dump_window_set_text( dump_window_t data, unsigned char *old_data, unsigned char *new_data )
nkeynes@2
   211
{
nkeynes@2
   212
    GtkTextBuffer *buf = data->textBuffer;
nkeynes@2
   213
    GtkTextTag *changedTag = data->changedTag;
nkeynes@2
   214
    GtkTextIter iter, endIter;
nkeynes@2
   215
    int i, j, offset;
nkeynes@2
   216
    /* Clear out the buffer */
nkeynes@2
   217
    gtk_text_buffer_get_start_iter(buf,&iter);
nkeynes@2
   218
    gtk_text_buffer_get_end_iter(buf,&endIter);
nkeynes@2
   219
    gtk_text_buffer_delete(buf,&iter,&endIter);
nkeynes@2
   220
    gtk_text_buffer_get_start_iter(buf,&iter);
nkeynes@736
   221
nkeynes@2
   222
    for( offset = 0, i=data->start; i<data->end; i+=16, offset+=16 ) {
nkeynes@2
   223
        char text[80];
nkeynes@2
   224
        sprintf(text, "%08X:", i );
nkeynes@2
   225
        gtk_text_buffer_insert( buf, &iter, text, 9 );
nkeynes@2
   226
        for( j=0; j<16; j++ ) {
nkeynes@2
   227
            if( j%4 == 0 )
nkeynes@2
   228
                gtk_text_buffer_insert( buf, &iter, " ", 1 );
nkeynes@2
   229
            if( i+j < data->end ) {
nkeynes@2
   230
                int oldVal = ((int)old_data[offset+j])&0xFF;
nkeynes@2
   231
                int newVal = ((int)new_data[offset+j])&0xFF;
nkeynes@2
   232
                sprintf(text, "%02X ", newVal);
nkeynes@2
   233
                if( oldVal == newVal )
nkeynes@2
   234
                    gtk_text_buffer_insert( buf, &iter, text, 3 );
nkeynes@2
   235
                else
nkeynes@2
   236
                    gtk_text_buffer_insert_with_tags( buf, &iter, text, 3,
nkeynes@736
   237
                            changedTag, NULL );
nkeynes@2
   238
            } else {
nkeynes@2
   239
                gtk_text_buffer_insert( buf, &iter, "   ", 3 );
nkeynes@2
   240
            }
nkeynes@2
   241
        }
nkeynes@2
   242
        gtk_text_buffer_insert( buf, &iter, "  ", 2 );
nkeynes@2
   243
        for( j=0; j<16 && i+j < data->end; j++ ) {
nkeynes@2
   244
            int oldVal = ((int)old_data[offset+j])&0xFF;
nkeynes@2
   245
            int newVal = ((int)new_data[offset+j])&0xFF;
nkeynes@2
   246
            if( isprint(newVal) )
nkeynes@2
   247
                sprintf( text, "%c", newVal );
nkeynes@2
   248
            else strcpy( text, "." );
nkeynes@2
   249
            if( oldVal == newVal )
nkeynes@2
   250
                gtk_text_buffer_insert( buf, &iter, text, 1 );
nkeynes@2
   251
            else
nkeynes@2
   252
                gtk_text_buffer_insert_with_tags( buf, &iter, text, 1,
nkeynes@736
   253
                        changedTag, NULL );
nkeynes@2
   254
        }
nkeynes@2
   255
        gtk_text_buffer_insert( buf, &iter, "\n", 1 );
nkeynes@2
   256
    }
nkeynes@2
   257
}
.