camera-control-webcam-switc.../main.pas

232 lines
5.9 KiB
ObjectPascal
Raw Normal View History

2020-01-23 11:35:57 +01:00
unit Main;
{$mode objfpc}{$H+}
2020-01-23 18:44:44 +01:00
{$warnings off}
{$hints off}
2020-01-23 11:35:57 +01:00
2020-01-28 22:29:30 +01:00
// Created at 23th of January 2020 by Linuxer (https://gitlab.com/psposito), from scratch with Free Pascal
2020-01-23 11:38:42 +01:00
2020-01-23 11:35:57 +01:00
interface
uses
2020-01-28 22:29:30 +01:00
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Menus, Unix,
unitAbout, PopupNotifier;
2020-01-23 11:35:57 +01:00
type
{ TForm1 }
TForm1 = class(TForm)
2020-01-28 22:29:30 +01:00
ImageList2: TImageList;
MenuItem1 : TMenuItem;
MenuItem2 : TMenuItem;
MenuItem3 : TMenuItem;
MenuItem4 : TMenuItem;
MenuItem5 : TMenuItem;
MenuItem6 : TMenuItem;
MenuItem7 : TMenuItem;
MenuItem8 : TMenuItem;
MenuItem9 : TMenuItem;
PopupMenu1 : TPopupMenu;
TrayIcon1 : TTrayIcon;
ImageList1 : TImageList;
2020-01-23 11:35:57 +01:00
procedure FormCreate(Sender: TObject);
2020-01-28 22:29:30 +01:00
procedure MenuItem1Click(Sender: TObject);
2020-01-23 11:35:57 +01:00
procedure MenuItem2Click(Sender: TObject);
procedure MenuItem3Click(Sender: TObject);
procedure MenuItem4Click(Sender: TObject);
2020-01-28 22:29:30 +01:00
procedure MenuItem5Click(Sender: TObject);
2020-01-28 19:46:42 +01:00
procedure MenuItem6Click(Sender: TObject);
procedure MenuItem7Click(Sender: TObject);
procedure TrayIcon1Click(Sender: TObject);
2020-01-23 11:35:57 +01:00
private
public
end;
var
2020-01-28 22:29:30 +01:00
Form1 : TForm1;
S : LongInt;
n : Integer;
HasPassword : Boolean;
Password : String;
Bitmap : TBitMap;
2020-01-23 11:35:57 +01:00
implementation
2020-01-28 22:29:30 +01:00
{$R *.frm}
2020-01-23 11:35:57 +01:00
{ TForm1 }
2020-01-28 22:29:30 +01:00
procedure ScaleImageList(ImgList: TImageList; NewWidth, NewHeight: Integer);
2020-01-28 19:46:42 +01:00
var
2020-01-28 22:29:30 +01:00
TempImgList: TImageList;
TempBmp1: TBitmap;
TempBmp2: TBitmap;
I: Integer;
begin
TempImgList := TImageList.Create(nil);
TempBmp1 := TBitmap.Create;
TempBmp1.PixelFormat := pf32bit;
TempBmp2 := TBitmap.Create;
TempBmp2.PixelFormat := pf32bit;
TempBmp2.SetSize(NewWidth, NewHeight);
try
TempImgList.Width := NewWidth;
TempImgList.Height := NewHeight;
for I := 0 to ImgList.Count - 1 do begin
// Load image for given index to temporary bitmap
ImgList.GetBitmap(I, TempBmp1);
// Clear transparent image background
TempBmp2.Canvas.Brush.Style := bsSolid;
TempBmp2.Canvas.Brush.Color := TempBmp2.TransparentColor;
TempBmp2.Canvas.FillRect(0, 0, TempBmp2.Width, TempBmp2.Height);
// Stretch image to new size
TempBmp2.Canvas.StretchDraw(Rect(0, 0, TempBmp2.Width, TempBmp2.Height), TempBmp1);
TempImgList.Add(TempBmp2, nil);
end;
ImgList.Assign(TempImgList);
finally
TempImgList.Free;
TempBmp1.Free;
TempBmp2.Free;
end;
2020-01-28 19:46:42 +01:00
end;
2020-01-23 11:35:57 +01:00
procedure CameraOn;
begin
2020-01-26 11:56:51 +01:00
S := fpsystem(concat('echo ', Password, ' | sudo -S modprobe uvcvideo'));
if (S <> 0) then
begin
ShowMessage('Incorrect password');
HasPassword := false;
end;
2020-01-23 11:35:57 +01:00
end;
procedure CameraOff;
begin
2020-01-26 11:56:51 +01:00
S := fpsystem(concat('echo ', Password, ' | sudo -S modprobe -r uvcvideo'));
if (S <> 0) then
begin
ShowMessage('Incorrect password');
HasPassword := false;
end;
2020-01-23 11:35:57 +01:00
end;
procedure AskPassword;
begin
Password := PasswordBox('Authorization Needed / User Input','Please Enter Password');
2020-01-23 11:35:57 +01:00
HasPassword := True;
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.FormCreate(Sender: TObject);
begin
S := fpsystem('echo off');
ImageList1.Width := 64;
ImageList1.Height := 64;
ImageList1.Scaled := true;
//ImageList2.Width := 64;
//ImageList2.Height := 64;
//ImageList2.Scaled := true;
HasPassword := false;
application.showmainform := false;
TrayIcon1.Hint := 'Camera Switcher';
if DirectoryExists('/dev/v4l/') then
begin
ImageList1.GetIcon(0, TrayIcon1.Icon);
ShowMessage('Camera is On');
end
else
begin
ImageList1.GetIcon(1, TrayIcon1.Icon);
ShowMessage('Camera is Off');
end;
TrayIcon1.ShowIcon := True;
TrayIcon1.Show;
end;
procedure TForm1.MenuItem1Click(Sender: TObject);
2020-01-23 11:35:57 +01:00
begin
2020-01-28 22:29:30 +01:00
if HasPassword then
2020-01-23 11:35:57 +01:00
begin
CameraOn;
2020-01-28 22:29:30 +01:00
ImageList1.GetIcon(0, TrayIcon1.Icon);
2020-01-23 11:35:57 +01:00
end
else
begin
AskPassword;
CameraOn;
2020-01-28 19:46:42 +01:00
if HasPassword then
2020-01-28 22:29:30 +01:00
ImageList1.GetIcon(0, TrayIcon1.Icon);
2020-01-23 11:35:57 +01:00
end;
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.MenuItem2Click(Sender: TObject);
2020-01-23 11:35:57 +01:00
begin
2020-01-28 22:29:30 +01:00
if HasPassword then
2020-01-23 11:35:57 +01:00
begin
CameraOff;
2020-01-28 22:29:30 +01:00
ImageList1.GetIcon(1, TrayIcon1.Icon);
2020-01-23 11:35:57 +01:00
end
else
begin
AskPassword;
CameraOff;
2020-01-28 19:46:42 +01:00
if HasPassword then
2020-01-28 22:29:30 +01:00
ImageList1.GetIcon(1, TrayIcon1.Icon);
2020-01-23 11:35:57 +01:00
end;
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.MenuItem3Click(Sender: TObject);
2020-01-23 11:35:57 +01:00
begin
2020-01-28 22:29:30 +01:00
TrayIcon1.Hide;
PopupMenu1.Free;
TrayIcon1.Free;
Halt (0);
2020-01-23 11:35:57 +01:00
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.MenuItem4Click(Sender: TObject);
begin
2020-01-28 22:29:30 +01:00
HasPassword := false;
ShowMessage('Password has been forgotten');
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.MenuItem5Click(Sender: TObject);
2020-01-26 11:56:51 +01:00
begin
2020-01-28 22:29:30 +01:00
FormAbout.Label1.Caption:='Project';
FormAbout.Label2.Caption:='Developer';
FormAbout.Label3.Caption:='Licence';
FormAbout.ListBox1.Items.Clear;
FormAbout.ListBox1.Items.Add('Camera Control On/Off Switch for Linux is an');
FormAbout.ListBox1.Items.Add('LGPL2 Free Pascal Project with LCL components,');
FormAbout.ListBox1.Items.Add('completely developed from scratch, at 23th, of');
FormAbout.ListBox1.Items.Add('January 2020 by Linuxer パスカリス スポシト');
FormAbout.Show;
2020-01-26 11:56:51 +01:00
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.MenuItem6Click(Sender: TObject);
begin
2020-01-28 22:29:30 +01:00
S := fpsystem('amixer set Capture cap');
ShowMessage('Microphone has been unmutted');
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.MenuItem7Click(Sender: TObject);
2020-01-28 19:46:42 +01:00
begin
2020-01-28 22:29:30 +01:00
S := fpsystem('amixer set Capture nocap');
ShowMessage('Microphone has been mutted');
2020-01-28 19:46:42 +01:00
end;
2020-01-28 22:29:30 +01:00
procedure TForm1.TrayIcon1Click(Sender: TObject);
begin
PopupMenu1.PopUp;
end;
2020-01-28 19:46:42 +01:00
2020-01-28 22:29:30 +01:00
end.