program msepatch;

{$MODE OBJFPC}
{$H+}
{$RANGECHECKS ON}
{$SMARTLINK ON}

uses sysutils, unix;

var
m : utf8string = './mseide-msegui/lib/common/kernel/linux/mseguiintf.pas';
p : utf8string = './mseguiintf.pas';
fpm, fptm : Text;
s, trim_s : utf8string;
f : Int64;
fixes:boolean=false;
usescomplete:boolean=false;
inuses:boolean=false;

begin
Assign(fpm, m); Assign(fptm, p);
FileMode:=0;
ReSet(fpm);
FileMode:=1;
ReWrite(fptm);

while not eof(fpm) do begin // Ok
ReadLn(fpm, s);

if s = 'implementation' then begin
WriteLn(fptm, 'var');
WriteLn(fptm, 'clipboardcopymonitor : procedure = nil;');
WriteLn(fptm, 'event_base, error_base : LongInt;');
WriteLn(fptm, 'checkforglobalhotkey : function(k : TXKeyEvent) : boolean of object = nil;');
WriteLn(fptm, 'r : LongInt;');
end;

if s = ' if xev.xany.xwindow = appid then begin' then begin
WriteLn(fptm, 'if (xev.xtype = event_base + XFixesSelectionNotify) and (clipboardcopymonitor <> nil) and (DWord(PXFixesSelectionNotifyEvent(@xev)^.owner) >= 15000000) then clipboardcopymonitor;');
end;

if s = '  cardinalatom:= xinternatom(appdisp,''CARDINAL'',' then begin
WriteLn(fptm, 'XFixesSelectSelectionInput(Pointer(appdisp), gui_getrootwindow, clipboardbuffers[low(clipboardbuffers)].name, XFixesSetSelectionOwnerNotifyMask);');
WriteLn(fptm, 'XFixesQueryExtension(Pointer(appdisp), @event_base, @error_base);');
end;

if s = 'function gui_initcolormap: guierrorty;' then begin
WriteLn(fptm, 'function globalhotkeyserrorhandler(Display: PDisplay; ErrorEvent: PXErrorEvent): Longint; cdecl;');
WriteLn(fptm, 'const');
WriteLn(fptm, ' buflen = 256;');
WriteLn(fptm, 'var');
WriteLn(fptm, ' buffer: array[0..buflen] of char;');
WriteLn(fptm, 'begin');
WriteLn(fptm, 'ghklastf := false;');
WriteLn(fptm, ' if xlockerror = 0 then begin');
WriteLn(fptm, '  xgeterrortext(display,errorevent^.error_code,@buffer,buflen);');
WriteLn(fptm, '   sys_errorout(''Global HotKeys: X Error: ''+ pchar(@buffer) + ''  ''+inttostr(errorevent^.error_code)+');
WriteLn(fptm, '        lineend +''  Major opcode:  ''+inttostr(errorevent^.request_code)+lineend);');
WriteLn(fptm, ' end;');
WriteLn(fptm, ' result:= 0;');
WriteLn(fptm, 'end;');
end;

if s = '{$include ../mseguiintf.inc}' then begin
WriteLn(fptm, '{$include ./mseide-msegui/lib/common/kernel/mseguiintf.inc}');
continue;
end;

if not usescomplete then begin
trim_s := Trim(s);
if (Pos('uses', trim_s) = 1) and (not inuses) then begin
inuses := true;
end;
if inuses then begin
 if trim_s[Length(trim_s)] = ';' then begin
  trim_s[Length(trim_s)] := ',';
  WriteLn(fptm, trim_s);
  WriteLn(fptm, 'xfixes;');
  WriteLn(fptm);
  WriteLn(fptm, 'var');
  WriteLn(fptm, '  ghklastf : bytebool = true;');
  WriteLn(fptm);
  WriteLn(fptm, 'function globalhotkeyserrorhandler(Display: PDisplay; ErrorEvent: PXErrorEvent): Longint; cdecl;');
  inuses := false;
  usescomplete := true;
  continue;
 end;
end;
end;

WriteLn(fptm, s);

if s = '  keypress: begin'
 then WriteLn(fptm, 'if checkforglobalhotkey <> nil then if checkforglobalhotkey(xev.xkey) then Exit(nil);');
end; {wend}
Close(fpm);
Close(fptm);
end.