summaryrefslogtreecommitdiff
path: root/dalos-hexview.lua
blob: b59993a9fb0ca06bb0a79b21f6e0fd323aa6c3d1 (plain)
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
load "iupe-hexview.lua"
load "iupe-hexview-toolbox.lua"

dalosp.hexview = {
    activate = function (self)
        self.extra.hvdlg:show()
    end,
    
    input_change = function (self, ind)
        local extra = self.extra
        local hv = extra.hv
        local h = self:get_linked_input(ind)
        if not h then
            self.color = cd.YELLOW
            hv:updatehandle(nil)
            self.dcanvas:draw()
            return
        end
        if not h:canread() or not h:canseek() then
            self.color = cd.RED
            hv:updatehandle(nil)
            self.dcanvas:draw()
            return
        end
        self.color = cd.GREEN
        for i = 1, 12 do
            self:set_houtput(nil, i)
            self.oldcursors[i] = -1
        end
        hv:updatehandle(h)
        self.dcanvas:draw()
    end,
    
    configure = function (self)
        local s, newname, gsx, gsy, dgsx, dgsy, dl, ml =
            iup.GetParam(self.name .. " properties", nil, [[
Name: %s
Gridsize X: %i
Gridsize Y: %i
Default gridsize X: %i
Default gridsize Y: %i
Lines: %i
Marker lengths: %b
]],
            self.name, self.extra.hv.gridsize.x, self.extra.hv.gridsize.y, iupep.hexview.gridsize.x, iupep.hexview.gridsize.y,
            self.extra.hv.displaylines, self.extra.hv.showmarkerslengths and 1 or 0)
        if not s then return end
        self.name = newname
        self.extra.hv.gridsize.x = gsx
        self.extra.hv.gridsize.y = gsy
        iupep.hexview.gridsize.x = dgsx
        iupep.hexview.gridsize.y = dgsy
        self.extra.hv.displaylines = dl
        self.extra.hv.showmarkerslengths = ml == 1
    end,
    
    get_settings = function (self)
        local hv = self.extra.hv
        local r = {
            mcursor = hv.mcursor + 0,
            kcursor = hv.kcursor + 0,
            markers = {},
            filecursor = hv.filecursor + 0,
            displaylines = hv.displaylines + 0,
            gridsize = hv.gridsize,
            showmarkerslengths = hv.showmarkerslengths,
        }
        for i = 1, 10 do
            r.markers[i] = hv.markers[i] + 0
        end
        return r
    end,
    
    output_change = function (self, ind)
        self.extra.hv.markerslengths[ind] = 0
        self.watchees[ind] = self.outputs[ind] and true or false
        self:set_houtput(nil, ind)
        self.oldcursors[ind] = -1
        self:dalos_hv_cb(self.extra.hv)
    end,
    
    update_houtput = function (self, ind, cursor)
        local hv = self.extra.hv
        local h = self:get_linked_input(1)
        local maxsize = h and h:getsize() or -1
        cursor = cursor + 0
        if cursor >= 0 and h and self.watchees[ind] then
            if cursor >= maxsize then
                self:set_houtput(nil, ind)
                return
            end
            h:seek(cursor)
            local obj = {
                h = h,
                hvo = self,
                ind = ind,
                origin = cursor,
                size = maxsize - cursor,
                getname = function (self) return self.hvo.name .. ":" .. self.ind end,
                getmodif = function (self) return self.h:getmodif() end,
                do_seek = function (self)
                    self.h:seek(self.offset + self.origin)
                    self:post_read()
                end,
                do_read = function (self, count, userdata)
                    return self.h:read(count, userdata)
                end,
                post_read = function (self, count)
                    local ol = hv.markerslengths[ind]
                    local nl = self.offset
                    if nl > ol then
                        hv.markerslengths[ind] = nl
                        if hv.showmarkerslengths then
                            hv:draw()
                        end
                    end
                end,
            }
            self:set_houtput(dalos.luahandle(obj), ind)
        end
    end,
    
    dalos_hv_cb = function (self, hv, reset)
        local m
        for i = 1, 10 do
            m = hv.markers[i]
            if m and self.oldcursors[i] ~= m then
                self:update_houtput(i, m)
                self.oldcursors[i] = m
            end
        end
        
        m = hv.kcursor
        if m and self.oldcursors[11] ~= m then
            self:update_houtput(11, m)
            self.oldcursors[11] = m
        end
        
        m = hv.mcursor
        if m and self.oldcursors[12] ~= m then
            self:update_houtput(12, m)
            self.oldcursors[12] = m
        end
    end,
    
    create = function (d, tab, settings)
        tab.otype = dalos.objtype.LUA_FILTER
        tab.activate = dalosp.hexview.activate
        tab.input_change = dalosp.hexview.input_change
        tab.output_change = dalosp.hexview.output_change
        tab.configure = dalosp.hexview.configure
        tab.get_settings = dalosp.hexview.get_settings
        tab.ninputs = 1
        tab.noutputs = 12
        tab.default_name = "Hexview"
        tab.ntype = "Hexview"
        
        local extra = { }
        
        local obj = dalos.object(d, tab, extra)

        local hv = iupe.hexview { }
        local hvtb = iupe.hexview_toolbox { hexview = hv }
        local hvdlg = iup.dialog { iup.hbox { iup.frame { hv }, iup.sbox { direction = "WEST", hvtb } }, title = obj.name, size = "500x", shrink = "Yes" }
        
        extra.hv = hv
        extra.hvtb = hvtb
        extra.hvdlg = hvdlg
        
        obj.oldcursors = { }
        obj.watchees = { }
        obj.update_houtput = dalosp.hexview.update_houtput
        for i = 1, 12 do obj.oldcursors[i] = -1 end
        
        if settings then
            if settings.markers then
                for i = 1, 10 do
                    if settings.markers[i] then hv.markers[i] = settings.markers[i] end
                end
            end
            if settings.mcursor then hv.mcursor = settings.mcursor end
            if settings.kcursor then hv.kcursor = settings.kcursor end
            if settings.filecursor then hv.filecursor = settings.filecursor end
            if settings.displaylines then hv.displaylines = settings.displaylines end
            if settings.gridsize then hv.gridsize.x, hv.gridsize.y = settings.gridsize.x, settings.gridsize.y end
            if settings.showmarkerslengths then hv.showmarkerslengths = settings.showmarkerslengths end
        end
        
        hv:registercb(dalosp.hexview.dalos_hv_cb, obj)
        obj.dalos_hv_cb = dalosp.hexview.dalos_hv_cb
        
        return obj
    end,
}

dalos.hexview = dalosp.hexview.create
dalos:register_obj("Hexview", dalos.hexview, "Basic Viewers")