summaryrefslogtreecommitdiff
path: root/plugins/Actman/lowlevelc.pas
blob: fe6d9bde313d6f79569e37a7381b49426e6cd0dd (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
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
unit lowlevelc;

interface

uses
  windows,
  iac_global;

// Macro flags
const
  ACF_ASSIGNED = $80000000; // macro assigned
  ACF_FIRSTRUN = $40000000; // FirstRun flag
  ACF_USEDNOW  = $20000000; // macro in use (reserved)
  ACF_VOLATILE = $10000000; // don't save in DB

  ACF_TOSAVE  = ACF_ASSIGNED or ACF_FIRSTRUN;

type
  pActionList = ^tActionList;
  tActionList = array [0..1023] of tBaseAction;

const
  MacroNameLen = 64;
type
  pMacroRecord = ^tMacroRecord;
  tMacroRecord = record
    id         :dword;
    flags      :dword;     // ACF_* flags
    descr      :array [0..MacroNameLen-1] of WideChar;
    ActionList :pActionList;
    ActionCount:integer;
  end;

type // array dimension - just for indexing
  pMacroList  = ^taMacroList;
  taMacroList = array [0..1023] of tMacroRecord;

type
  tMacroList = class
  private
    fMacroList:pMacroList;
    fMacroCount:cardinal;

    procedure ReallocMacroList;
    function GetMacroElement(i:integer):pMacroRecord;
  public

    constructor Create(isize:cardinal);
    destructor Destroy; override;

    procedure Clear(filter:dword=0);
    function Clone:tMacroList;
    function NewMacro:cardinal;

    function GetMacro(id:uint_ptr   ):pMacroRecord; overload;
    function GetMacro(name:pWideChar):pMacroRecord; overload;
    function GetMacroNameById(id:dword):PWideChar;

    property List[i:integer]:pMacroRecord read GetMacroElement; default;
    property Count: cardinal read fMacroCount;
  end;

procedure FreeMacro(Macro:pMacroRecord;mask:dword=0);

var
  MacroList:tMacroList;


implementation

uses Common;

const
  MacroListPage = 8;


function tMacroList.GetMacroElement(i:integer):pMacroRecord;
begin
  result:=@fMacroList[i];
end;

function tMacroList.GetMacro(id:uint_ptr):pMacroRecord;
var
  i:integer;
begin
  for i:=0 to fMacroCount-1 do
  begin
    if ((fMacroList^[i].flags and ACF_ASSIGNED)<>0) and
       (id=fMacroList^[i].id) then
    begin
      result:=@(fMacroList^[i]);
      exit;
    end;
  end;
  result:=nil;
end;

function tMacroList.GetMacro(name:pWideChar):pMacroRecord;
var
  i:integer;
begin
  for i:=0 to fMacroCount-1 do
  begin
    if ((fMacroList^[i].flags and ACF_ASSIGNED)<>0) and
       (StrCmpW(name,fMacroList^[i].descr)=0) then
    begin
      result:=@(fMacroList^[i]);
      exit;
    end;
  end;
  result:=nil;
end;

function tMacroList.GetMacroNameById(id:dword):PWideChar;
var
  p:pMacroRecord;
begin
  p:=GetMacro(id);
  if p<>nil then
    result:=@(p^.descr)
  else
    result:=nil;
end;

procedure FreeActionList(var src:pActionList; count:integer; mask:dword);
var
  i:integer;
begin
  for i:=0 to count-1 do
  begin
    if (mask=0) or ((src^[i].flags and mask)<>0) then
      src^[i].Free;
  end;
  FreeMem(src);
  src:=nil;
end;

procedure FreeMacro(Macro:pMacroRecord;mask:dword=0);
begin
  with Macro^ do
  begin
    if (flags and ACF_ASSIGNED)<>0 then
    begin
      flags:=0; // make Unassigned
      FreeActionList(ActionList,ActionCount,mask);
      ActionCount:=0;
    end;
  end;
end;

procedure tMacroList.Clear(filter:dword=0);
var
  i:integer;
begin
  for i:=0 to fMacroCount-1 do
  begin
    FreeMacro(@(fMacroList[i]),filter);
  end;
  fMacroCount:=0;
  FreeMem(fMacroList);
  fMacroList:=nil;
end;

destructor tMacroList.Destroy;
begin
  fMacroCount:=0;
  FreeMem(fMacroList);
  fMacroList:=nil;

  inherited Destroy;
end;

function CloneActionList(src:pActionList;count:integer):pActionList;
begin
  if src=nil then
  begin
    result:=nil;
    exit;
  end;
  GetMem(result    ,count*SizeOf(tBaseAction));
  move(src^,result^,count*SizeOf(tBaseAction))
end;

procedure CloneMacro(var dst:pMacroRecord; src:pMacroRecord);
begin
  if (src^.flags and ACF_ASSIGNED)<>0 then
  begin
    move(src^,dst^,SizeOf(tMacroRecord));
    dst^.ActionList:=CloneActionList(src^.ActionList,src^.ActionCount);
  end;
end;

function tMacroList.Clone:tMacroList;
var
  src,dst:pMacroRecord;
  i:integer;
  cnt:integer;
begin
  result:=nil;

  if fMacroList<>nil then
  begin
    cnt:=0;
    for i:=0 to fMacroCount-1 do
      if (fMacroList^[i].flags and ACF_ASSIGNED)<>0 then
        inc(cnt);
    if cnt>0 then
    begin
      result:=tMacroList.Create(cnt);

      src:=pMacroRecord(self.fMacroList);
      dst:=pMacroRecord(result.fMacroList);

      while cnt>0 do
      begin
        if (src^.flags and ACF_ASSIGNED)<>0 then
        begin
          CloneMacro(dst,src);
          inc(dst);
          dec(cnt);
        end;
        inc(src);
      end;
    end;
  end;
  if result=nil then
    result:=tMacroList.Create(0);
end;

procedure tMacroList.ReallocMacroList;
var
  i:cardinal;
  tmp:pMacroList;
begin
  i:=(fMacroCount+MacroListPage)*SizeOf(tMacroRecord);
  GetMem(tmp,i);
  FillChar(tmp^,i,0);
  if fMacroCount>0 then
  begin
    move(fMacroList^,tmp^,fMacroCount*SizeOf(tMacroRecord));
    FreeMem(fMacroList);
  end;
  fMacroList:=tmp;
  inc(fMacroCount,MacroListPage);
end;

constructor tMacroList.Create(isize:cardinal);
begin
  inherited Create;

  if isize<MacroListPage then
    fMacroCount:=MacroListPage
  else
    fMacroCount:=isize;
  GetMem  (fMacroList ,fMacroCount*SizeOf(tMacroRecord));
  FillChar(fMacroList^,fMacroCount*SizeOf(tMacroRecord),0);
end;

procedure InitMacroValue(pMacro:pMacroRecord);
var
  tmp:int64;
begin
  with pMacro^ do
  begin
    StrCopyW(descr,NoDescription,MacroNameLen-1);
    QueryPerformanceCounter(tmp);
    id         :=tmp and $FFFFFFFF;
    flags      :=ACF_ASSIGNED;
  end;
end;

function tMacroList.NewMacro:cardinal;
var
  i:cardinal;
  pMacro:pMacroRecord;
begin
  i:=0;
  pMacro:=pMacroRecord(fMacroList);
  while i<fMacroCount do
  begin
    if (pMacro^.flags and ACF_ASSIGNED)=0 then
    begin
      result:=i;
      InitMacroValue(pMacro);
      exit;
    end;
    inc(i);
    inc(pMacro);
  end;
  // realloc
  result:=fMacroCount;
  ReallocMacroList;
  InitMacroValue(@(fMacroList^[result]));
end;

end.