unit mainunit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ctypes, dbus // sudo apt-get install libdbus-1-dev
;
type
{ TMainForm }
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
procedure BusSend(sigvalue: PChar);
public
{ public declarations }
end;
var
MainForm: TMainForm;
err: DBusError;
conn: PDBusConnection;
ret: cint;
implementation
{$R *.lfm}
{ TMainForm }
procedure TMainForm.Button1Click(Sender: TObject);
begin
// signal com.canonical.Unity.LauncherEntry.Update (in s app_uri, in a{sv} properties)
BusSend(PChar('"application://mainform.desktop", "{' + #39 + 'progress-visible' + #39 + ': <true>, ' + #39 + 'progress' + #39 + ': <0.5>}"'));
// "application://mainform.desktop" "{'progress-visible': <true>, 'progress': <0.5>}"
end;
procedure TMainForm.BusSend(sigvalue: PChar);
var
msg: PDBusMessage;
args: DBusMessageIter;
serial: dbus_uint32_t = 0;
begin
dbus_error_init(@err);
{ Connection }
conn := dbus_bus_get(DBUS_BUS_SESSION, @err);
if dbus_error_is_set(@err) <> 0 then
begin
ShowMessage('Connection Error: ' + err.message);
dbus_error_free(@err);
end;
if conn = nil then
begin
ShowMessage('conn = nil');
Exit;
end;
ShowMessage('Sending signal with value ' + string(sigvalue));
{ Request the name of the bus }
ret := dbus_bus_request_name(conn, 'com.canonical.Unity.LauncherEntry', DBUS_NAME_FLAG_REPLACE_EXISTING, @err);
if dbus_error_is_set(@err) <> 0 then
begin
ShowMessage('Name Error: ' + err.message);
dbus_error_free(@err);
end;
if ret <> DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER then
begin
ShowMessage('NOT DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER');
Exit;
end;
// create a signal & check for errors
msg := dbus_message_new_signal('/', // object name of the signal
'com.canonical.Unity.LauncherEntry', // interface name of the signal
'Update'); // name of the signal
if (msg = nil) then
begin
ShowMessage('Message Null');
Exit;
end;
// append arguments onto signal
dbus_message_iter_init_append(msg, @args);
if (dbus_message_iter_append_basic(@args, DBUS_TYPE_STRING, @sigvalue) = 0) then
begin
ShowMessage('Out Of Memory, iter append basic!');
Exit;
end;
// send the message and flush the connection
if (dbus_connection_send(conn, msg, @serial) = 0) then
begin
ShowMessage('Out Of Memory, connection send!');
Exit;
end;
dbus_connection_flush(conn);
ShowMessage('Signal Sent');
// free the message and close the connection
dbus_message_unref(msg);
end;
end.