The program when compiled displays the percentage charge and a button which shows a dialog with more details.
The Form's Code:
Code: Select all
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, Process;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Panel1: TPanel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Label1DblClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
data: string;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
s1, s2, tmpS: string;
List: TStringList;
begin
List := TStringList.Create;
Try
RunCommand('/usr/bin/upower', ['-e'], s1);
List.Text := s1;
for i := 0 to List.Count - 1 do
begin
if pos('BAT', List.Strings[i]) = 0
then continue;
tmpS := List.Strings[i];
break;
end;
RunCommand('/usr/bin/upower', ['-i', tmpS], s2);
List.Text := s2;
data := s2;
for i := 0 to List.Count - 1 do
begin
if pos('percentage', List.Strings[i]) = 0
then continue;
tmpS := List.Strings[i];
break;
end;
s1 := '';
for i := length(tmpS) downto 1 do
if (tmpS[i] = ':') or (tmpS[i] = ' ')
then break
else s1 := tmpS[i] + s1;
Label1.caption := ' Battery: ' + s1 + ' ';
if s1 <> ''
then
begin
i := pos(': ', s1);
if i > 0
then
begin
i := i + 2;
s1 := copy(s1, i, length(s1));
end;
end;
Caption := s1;
finally
List.Free;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Timer1Timer(nil);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MessageDlg(data, mtInformation, [mbOK], 0);
end;
procedure TForm1.Label1DblClick(Sender: TObject);
begin
MessageDlg(data, mtInformation, [mbOK], 0);
end;
end.
Code: Select all
object Form1: TForm1
Left = 295
Height = 260
Top = 222
Width = 887
AutoSize = True
BorderStyle = bsDialog
BorderWidth = 15
Caption = 'Form1'
ClientHeight = 260
ClientWidth = 887
OnShow = FormShow
LCLVersion = '1.6.2.0'
object Panel1: TPanel
Left = 15
Height = 230
Top = 15
Width = 857
Align = alClient
BevelInner = bvLowered
BevelOuter = bvLowered
ClientHeight = 230
ClientWidth = 857
TabOrder = 0
object Label1: TLabel
Left = 2
Height = 226
Top = 2
Width = 679
Align = alClient
Caption = 'Label1'
Font.Height = -53
Font.Name = 'Monospace'
ParentColor = False
ParentFont = False
OnDblClick = Label1DblClick
end
object Button1: TButton
Left = 681
Height = 226
Top = 2
Width = 174
Align = alRight
Caption = 'Details...'
OnClick = Button1Click
TabOrder = 0
end
end
object Timer1: TTimer
Interval = 10000
OnTimer = Timer1Timer
left = 31
top = 117
end
end