File: regwnd.cpp

package info (click to toggle)
kdbg 1.2.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,504 kB
  • ctags: 1,945
  • sloc: cpp: 15,961; sh: 8,183; makefile: 481; perl: 71; ansic: 16
file content (340 lines) | stat: -rw-r--r-- 8,656 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// $Id: regwnd.cpp,v 1.12 2000/11/11 15:00:00 jsixt Exp $

// Copyright by Judin Max, Johannes Sixt
// This file is under GPL, the GNU General Public Licence

#include <qheader.h>
#if QT_VERSION >= 200
#include <kglobalsettings.h>
#include <klocale.h>			/* i18n */
#include <kiconloader.h>
#include <qfontdialog.h>
#include <qmessagebox.h>
#else
#include <kapp.h>			/* i18n */
#endif
#include <qpopmenu.h>
#include <ctype.h>
#include <stdlib.h>			/* strtoul */
#include "regwnd.h"
#include "dbgdriver.h"


class RegisterViewItem : public QListViewItem
{
public:
    RegisterViewItem(RegisterView* parent, QListViewItem* insertAfter,
		     QString reg, QString raw, QString cooked);
    ~RegisterViewItem();

    void setValue(QString raw, QString cooked);
    RegisterInfo m_reg;
    bool m_changes;
    bool m_found;

protected:
    virtual void paintCell(QPainter*, const QColorGroup& cg,
			   int column, int width, int alignment);

};


RegisterViewItem::RegisterViewItem(RegisterView* parent, QListViewItem* insertAfter,
				   QString reg, QString raw, QString cooked) :
	QListViewItem(parent, insertAfter),
	m_changes(false),
	m_found(true)
{
    m_reg.regName = reg;
    setValue(raw, cooked);
    setText(0, reg);
}

RegisterViewItem::~RegisterViewItem()
{
}

/*
 * We must be careful when converting the hex value because
 * it may exceed this computer's long values.
 */
inline int hexCharToDigit(char h)
{
    if (h < '0')
	return -1;
    if (h <= '9')
	return h - '0';
    if (h < 'A')
	return -1;
    if (h <= 'F')
	return h - ('A' - 10);
    if (h < 'a')
	return -1;
    if (h <= 'f')
	return h - ('a' - 10);
    return -1;
}
#if QT_VERSION < 200
#define DIGIT(c) hexCharToDigit(c)
#else
#define DIGIT(c) hexCharToDigit(c.latin1())
#endif

static QString toBinary(QString hex)
{
    static const char digits[16][8] = {
	"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
	"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
    };
    QString result;
    
    for (unsigned i = 2; i < hex.length(); i++) {
	int idx = DIGIT(hex[i]);
	if (idx < 0) {
	    // not a hex digit; no conversion
	    return hex;
	}
	const char* bindigits = digits[idx];
	result += bindigits;
    }
    // remove leading zeros
    switch (DIGIT(hex[2])) {
    case 0: case 1: result.remove(0, 3); break;
    case 2: case 3: result.remove(0, 2); break;
    case 4: case 5:
    case 6: case 7: result.remove(0, 1); break;
    }
    return result;
}

static QString toOctal(QString hex)
{
    QString result;
    int shift = 0;
    unsigned v = 0;
    for (int i = hex.length()-1; i >= 2; i--) {
	int idx = DIGIT(hex[i]);
	if (idx < 0)
	    return hex;
	v += idx << shift;
	result.insert(0, (v & 7) + '0');
	v >>= 3;
	shift++;
	if (shift == 3) {
	    // an extra digit this round
	    result.insert(0, v + '0');
	    shift = v = 0;
	}
    }
    if (v != 0) {
	result.insert(0, v + '0');
    }
    return "0" + result;
}

static QString toDecimal(QString hex)
{
    /*
     * We convert only numbers that are small enough for this computer's
     * size of long integers.
     */
    if (hex.length() > sizeof(unsigned long)*2+2)	/*  count in leading "0x" */
	return hex;

#if QT_VERSION >= 200
    const char* start = hex.latin1();
#else
    const char* start = hex;
#endif
    char* end;
    unsigned long val = strtoul(start, &end, 0);
    if (start == end)
	return hex;
    else
	return QString().setNum(val);
}

#undef DIGIT

void RegisterViewItem::setValue(QString raw, QString cooked)
{
    m_reg.rawValue = raw;
    m_reg.cookedValue = cooked;

    /*
     * If the raw value is in hexadecimal notation, we allow to display it
     * in different modes (in the second column). The cooked value is
     * displayed in the third column.
     */
    if (raw.length() > 2 && raw[0] == '0' && raw[1] == 'x') {
	switch (static_cast<RegisterView*>(listView())->m_mode) {
	case  2: raw = toBinary(raw); break;
	case  8: raw = toOctal(raw); break;
	case 10: raw = toDecimal(raw); break;
	default: /* no change */ break;
	}
    }
	
    setText(1, raw);
    setText(2, cooked);
}

void RegisterViewItem::paintCell(QPainter* p, const QColorGroup& cg,
				 int column, int width, int alignment)
{
    if (m_changes) {
#if QT_VERSION >= 200
	QColorGroup newcg = cg;
	newcg.setColor(QColorGroup::Text, red);
#else
	QColorGroup newcg(cg.foreground(), cg.background(),
			  cg.light(), cg.dark(), cg.mid(),
			  red, cg.base());
#endif
	QListViewItem::paintCell(p, newcg, column, width, alignment);
    } else {
	QListViewItem::paintCell(p, cg, column, width, alignment);
    }
}


RegisterView::RegisterView(QWidget* parent, const char* name) :
	QListView(parent, name),
	m_lastItem(0),
	m_mode(16)
{
    setSorting(-1);

#if QT_VERSION < 200
    addColumn(i18n("Register"));
    addColumn(i18n("Value"));
    addColumn(i18n("Decoded value"));
#else
    QPixmap iconRegs = BarIcon("regs.xpm");
    QPixmap iconWatchcoded = BarIcon("watchcoded.xpm");
    QPixmap iconWatch = BarIcon("watch.xpm");

    addColumn(QIconSet(iconRegs), i18n("Register"));
    addColumn(QIconSet(iconWatchcoded), i18n("Value"));
    addColumn(QIconSet(iconWatch), i18n("Decoded value"));
#endif

    setColumnAlignment(0,AlignLeft);
    setColumnAlignment(1,AlignLeft);
    setColumnAlignment(2,AlignLeft);

    setAllColumnsShowFocus( true );
    header()->setClickEnabled(false);

    connect(this, SIGNAL(doubleClicked(QListViewItem*)),
	    SLOT(doubleClicked(QListViewItem*)));
    connect(this, SIGNAL(rightButtonClicked(QListViewItem*,const QPoint&,int)),
	    SLOT(rightButtonClicked(QListViewItem*,const QPoint&,int)));

    m_modemenu = new QPopupMenu;
    m_modemenu->insertItem(i18n("&Binary"),0);
    m_modemenu->insertItem(i18n("&Octal"),1);
    m_modemenu->insertItem(i18n("&Decimal"),2);
    m_modemenu->insertItem(i18n("He&xadecimal"),3);
    connect(m_modemenu,SIGNAL(activated(int)),SLOT(slotModeChange(int)));

    resize(200,300);
}

RegisterView::~RegisterView()
{
}

void RegisterView::updateRegisters(QList<RegisterInfo>& regs)
{
    setUpdatesEnabled(false);

    // mark all items as 'not found'
    for (QListViewItem* i = firstChild(); i; i = i->nextSibling()) {
	static_cast<RegisterViewItem*>(i)->m_found = false;
    }

    // parse register values
    for (RegisterInfo* reg = regs.first(); reg != 0; reg = regs.next())
    {
	// check if this is a new register
	bool found = false;
	for (QListViewItem* i = firstChild(); i; i = i->nextSibling()) {
	    RegisterViewItem* it = static_cast<RegisterViewItem*>(i);
	    if (it->m_reg.regName == reg->regName) {
		found = true;
		it->m_found = true;
		if (it->m_reg.rawValue != reg->rawValue ||
		    it->m_reg.cookedValue != reg->cookedValue)
		{
		    it->m_changes = true;
		    it->setValue(reg->rawValue, reg->cookedValue);
		    repaintItem(it);
		} else {
		    /*
		     * If there was a change last time, but not now, we
		     * must revert the color.
		     */
		    if (it->m_changes) {
			it->m_changes = false;
			repaintItem(it);
		    }
		}
	    }
	}
	if (!found)
	    m_lastItem = new RegisterViewItem(this, m_lastItem, reg->regName,
					      reg->rawValue, reg->cookedValue);
    }

    // remove all 'not found' items;
    QList<QListViewItem> deletedItem;
    deletedItem.setAutoDelete(true);
    m_lastItem = 0;
    for (QListViewItem* i = firstChild(); i; i = i->nextSibling() ){
	RegisterViewItem* it = static_cast<RegisterViewItem*>(i);
	if (!it->m_found) {
	    deletedItem.append(it);
	} else {
	    m_lastItem = it;
	}
    }
    deletedItem.clear();

    setUpdatesEnabled(true);
    triggerUpdate();
}

void RegisterView::doubleClicked( QListViewItem* item )
{
#if QT_VERSION >= 200
    RegisterViewItem* it = static_cast<RegisterViewItem*>(item);
    QMessageBox::information(this, "work in progress...",
			     QString("Prepare for change register value\nregister: %1    current value: %2").arg(it->m_reg.regName).arg(it->m_reg.rawValue));
#endif
}

void RegisterView::rightButtonClicked(QListViewItem* item, const QPoint& p, int c)
{
    m_modemenu->setItemChecked(0, m_mode ==  2);
    m_modemenu->setItemChecked(1, m_mode ==  8);
    m_modemenu->setItemChecked(2, m_mode == 10);
    m_modemenu->setItemChecked(3, m_mode == 16);
    m_modemenu->popup(p);
}

void RegisterView::slotModeChange(int code)
{
    static const int modes[] = { 2, 8, 10, 16 };
    if (code < 0 || code >= 4 || m_mode == modes[code])
	return;

    m_mode = modes[code];

    for (QListViewItem* i = firstChild(); i; i = i->nextSibling()) {
	RegisterViewItem* it = static_cast<RegisterViewItem*>(i);
	it->setValue(it->m_reg.rawValue, it->m_reg.cookedValue);
    }
}

#include "regwnd.moc"