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