-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftview.py
265 lines (227 loc) · 7.66 KB
/
ftview.py
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
from __future__ import print_function
# -----------------------------------------------------------------------
# This is an example illustrating how to use customview in Python
# (c) Hex-Rays
#
import ida_kernwin
import ida_lines
from PyQt5.QtCore import QTimer
# -----------------------------------------------------------------------
from exectools import execfile, _import, make_refresh
refresh_ftview = make_refresh(os.path.abspath(__file__))
refresh = make_refresh(os.path.abspath(__file__))
class ft_char:
"""colored character"""
def __init__(self, char=' ', color=default_color, attributes=None, dirty=True):
self.char = char
self.color = default_color
self.attributes = {}
if attributes is not None:
_.extend(self.attributes, attributes)
self.dirty = not not dirty
ft_screen = collections.defaultdict(dict)
class say_something_handler_t(ida_kernwin.action_handler_t):
def __init__(self, thing):
ida_kernwin.action_handler_t.__init__(self)
self.thing = thing
def activate(self, ctx):
print(self.thing)
def update(self, ctx):
return ida_kernwin.AST_ENABLE_ALWAYS
@staticmethod
def compose_action_name(v):
return "custview:say_%s" % v
# create actions
actions_variants = ["Hello", "World"]
for av in actions_variants:
actname = say_something_handler_t.compose_action_name(av)
if ida_kernwin.unregister_action(actname):
print("Unregistered previously-registered action \"%s\"" % actname)
desc = ida_kernwin.action_desc_t(actname, "Say %s" % av, say_something_handler_t(av))
if ida_kernwin.register_action(desc):
print("Registered action \"%s\"" % actname)
# -----------------------------------------------------------------------
class mycv_t(ida_kernwin.simplecustviewer_t):
def __init__(self):
self.q = []
self.buffer = []
def draw_request(self):
self.debounce.start()
def debounced(self):
if self.q:
mycv.ClearLines()
mycv.Draw()
mycv.draw_request()
def Draw(self):
self.last_ea = idc.here()
self.buffer.extend(self.q)
self.q.clear()
if len(self.buffer) > 10:
self.buffer = self.buffer[:-10]
# func_tails(idc.here(), output=output)
# output.extend(["a", "b", "c"])
for i in range(0, len(self.buffer)):
prefix, bg = ida_lines.COLOR_DEFAULT, None
# make every 10th line a bit special
if i % 10 == 0:
prefix = ida_lines.COLOR_DNAME # i.e., dark yellow...
# bg = 0xFFFF00 # ...on cyan
pfx = ida_lines.COLSTR("%3d" % i, ida_lines.SCOLOR_PREFIX)
if self.use_colors:
self.AddLine("%s: %s" % (pfx, self.buffer[i]), fgcolor=prefix) # , bgcolor=bg)
else:
self.AddLine("%s: %s" % (pfx, self.buffer[i]))
def Create(self, sn=None, use_colors=True):
# Form the title
title = "Simple custom view test"
if sn:
title += " %d" % sn
self.use_colors = use_colors
# Create the customviewer
if not ida_kernwin.simplecustviewer_t.Create(self, title):
print("failed to create")
return False
# self.Draw()
self.debounce = QTimer()
self.debounce.setInterval(500)
self.debounce.setSingleShot(True)
self.debounce.timeout.connect(self.debounced)
self.debounce.start()
return True
def OnClick(self, shift):
"""
User clicked in the view
@param shift: Shift flag
@return: Boolean. True if you handled the event
"""
print("OnClick, shift=%d" % shift)
return True
def OnDblClick(self, shift):
"""
User dbl-clicked in the view
@param shift: Shift flag
@return: Boolean. True if you handled the event
"""
word = self.GetCurrentWord()
if not word: word = "<None>"
print("OnDblClick, shift=%d, current word=%s" % (shift, word))
return True
def OnCursorPosChanged(self):
"""
Cursor position changed.
@return: Nothing
"""
print("OnCurposChanged")
def OnClose(self):
"""
The view is closing. Use this event to cleanup.
@return: Nothing
"""
print("OnClose " + self.title)
def OnKeydown(self, vkey, shift):
"""
User pressed a key
@param vkey: Virtual key code
@param shift: Shift flag
@return: Boolean. True if you handled the event
"""
print("OnKeydown, vk=%d shift=%d" % (vkey, shift))
# ESCAPE?
if vkey == 27:
self.Close()
# VK_DELETE
elif vkey == 46:
n = self.GetLineNo()
if n is not None:
self.DelLine(n)
self.Refresh()
print("Deleted line %d" % n)
# Goto?
elif vkey == ord('G'):
n = self.GetLineNo()
if n is not None:
v = ida_kernwin.ask_long(self.GetLineNo(), "Where to go?")
if v:
self.Jump(v, 0, 5)
elif vkey == ord('R'):
print("refreshing....")
self.Refresh()
elif vkey == ord('C'):
print("refreshing current line...")
self.RefreshCurrent()
elif vkey == ord('A'):
s = ida_kernwin.ask_str("NewLine%d" % self.Count(), 0, "Append new line")
self.AddLine(s)
self.Refresh()
elif vkey == ord('X'):
print("Clearing all lines")
self.ClearLines()
self.Refresh()
elif vkey == ord('I'):
n = self.GetLineNo()
s = ida_kernwin.ask_str("InsertedLine%d" % n, 0, "Insert new line")
self.InsertLine(n, s)
self.Refresh()
elif vkey == ord('E'):
l = self.GetCurrentLine(notags=1)
if not l:
return False
n = self.GetLineNo()
print("curline=<%s>" % l)
l = l + ida_lines.COLSTR("*", ida_lines.SCOLOR_VOIDOP)
self.EditLine(n, l)
self.RefreshCurrent()
print("Edited line %d" % n)
else:
return False
return True
def OnHint(self, lineno):
"""
Hint requested for the given line number.
@param lineno: The line number (zero based)
@return:
- tuple(number of important lines, hint string)
- None: if no hint available
"""
return (1, "OnHint, line=%d" % lineno)
def Show(self, *args):
ok = ida_kernwin.simplecustviewer_t.Show(self, *args)
if ok:
# permanently attach actions to this viewer's popup menu
for av in actions_variants:
actname = say_something_handler_t.compose_action_name(av)
ida_kernwin.attach_action_to_popup(self.GetWidget(), None, actname)
return ok
def OnRefresh(self):
"""
"""
print("OnRefresh ")
# -----------------------------------------------------------------------
try:
# created already?
mycv
print("Already created, will close it...")
mycv.Close()
del mycv
except:
pass
def show_win():
x = mycv_t()
if not x.Create():
print("Failed to create!")
return None
x.Show()
# globals()['tcc'] = x.GetWidget()
return x
mycv = show_win()
if not mycv:
del mycv
def make_many(n):
L = []
for i in range(1, n+1):
v = mycv_t()
if not v.Create(i):
break
v.Show()
L.append(v)
return L