From 15d6336dd0a0bc57c7705538c90837febedc11d6 Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 16 Feb 2021 18:12:13 +0100 Subject: [PATCH] Make same app run on Win, Linux and Mac / Folder Lazarus_any_OS --- .../ExternalPumpBrowser/README.txt | 24 -------- .../ExternalPumpBrowser/00-Delete.bat | 0 .../ExternalPumpBrowser.ico | Bin .../ExternalPumpBrowser.lpi | 16 +++-- .../ExternalPumpBrowser.lpr | 7 ++- .../ExternalPumpBrowser.res | Bin .../ExternalPumpBrowser/README.txt | 40 +++++++++++++ .../ExternalPumpBrowser/cef.inc | 0 .../ExternalPumpBrowser/create_mac_helper.sh | 0 .../globalcefapplication.pas | 47 +++++++++++++++ .../ExternalPumpBrowser/initsubprocess.pas | 55 ++++++++++++++++++ .../uExternalPumpBrowser.lfm | 0 .../uExternalPumpBrowser.pas | 41 ++++--------- 13 files changed, 171 insertions(+), 59 deletions(-) delete mode 100644 demos/Lazarus_Mac/ExternalPumpBrowser/README.txt rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/00-Delete.bat (100%) rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/ExternalPumpBrowser.ico (100%) rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/ExternalPumpBrowser.lpi (84%) rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/ExternalPumpBrowser.lpr (93%) rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/ExternalPumpBrowser.res (100%) create mode 100644 demos/Lazarus_any_OS/ExternalPumpBrowser/README.txt rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/cef.inc (100%) rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/create_mac_helper.sh (100%) create mode 100644 demos/Lazarus_any_OS/ExternalPumpBrowser/globalcefapplication.pas create mode 100644 demos/Lazarus_any_OS/ExternalPumpBrowser/initsubprocess.pas rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/uExternalPumpBrowser.lfm (100%) rename demos/{Lazarus_Mac => Lazarus_any_OS}/ExternalPumpBrowser/uExternalPumpBrowser.pas (90%) diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/README.txt b/demos/Lazarus_Mac/ExternalPumpBrowser/README.txt deleted file mode 100644 index 17caa0f2..00000000 --- a/demos/Lazarus_Mac/ExternalPumpBrowser/README.txt +++ /dev/null @@ -1,24 +0,0 @@ -ExternalPumpBrowser - -# ABOUT - -This example uses - TCEFLinkedWindowParent - TCEFWorkScheduler - -TCEFWorkScheduler feeds the CEF messageloop by calling DoMessageLoopWork(). On Mac this is currently the only way to run the CEF messageloop. - - -# SETUP - -1) Go to "project options" and create the "App Bundle" -2) Download the CEF framework and place the content of the "Release" folder into ExternalPumpBrowser.app/Contents/Frameworks/Chromium Embedded Framework.framework -You should have: - Chromium Embedded Framework - Libraries/* - Resources/* -3) Open project "AppHelper", create App Bundle and compile the AppHelper. - Run create_mac_helper.sh -4) Open project ExternalPumpBrowser, compile and run - - diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/00-Delete.bat b/demos/Lazarus_any_OS/ExternalPumpBrowser/00-Delete.bat similarity index 100% rename from demos/Lazarus_Mac/ExternalPumpBrowser/00-Delete.bat rename to demos/Lazarus_any_OS/ExternalPumpBrowser/00-Delete.bat diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.ico b/demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.ico similarity index 100% rename from demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.ico rename to demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.ico diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.lpi b/demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.lpi similarity index 84% rename from demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.lpi rename to demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.lpi index b1e82b93..9ab148fa 100644 --- a/demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.lpi +++ b/demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.lpi @@ -1,8 +1,11 @@ - + + + + @@ -33,7 +36,7 @@ <PackageName Value="LCL"/> </Item2> </RequiredPackages> - <Units Count="3"> + <Units Count="4"> <Unit0> <Filename Value="ExternalPumpBrowser.lpr"/> <IsPartOfProject Value="True"/> @@ -46,10 +49,15 @@ <ResourceBaseClass Value="Form"/> </Unit1> <Unit2> - <Filename Value="interfaces.pas"/> + <Filename Value="initsubprocess.pas"/> <IsPartOfProject Value="True"/> - <UnitName Value="Interfaces"/> + <UnitName Value="InitSubProcess"/> </Unit2> + <Unit3> + <Filename Value="globalcefapplication.pas"/> + <IsPartOfProject Value="True"/> + <UnitName Value="GlobalCefApplication"/> + </Unit3> </Units> </ProjectOptions> <CompilerOptions> diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.lpr b/demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.lpr similarity index 93% rename from demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.lpr rename to demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.lpr index cbe94fd2..905592fe 100644 --- a/demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.lpr +++ b/demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.lpr @@ -56,9 +56,12 @@ uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} + {$IFDEF LINUX} + InitSubProcess, // On Linux this unit must be used *before* the "interfaces" unit. + {$ENDIF} Interfaces, - uExternalPumpBrowser, - Forms + Forms, + uExternalPumpBrowser, GlobalCefApplication { you can add units after this } ; diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.res b/demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.res similarity index 100% rename from demos/Lazarus_Mac/ExternalPumpBrowser/ExternalPumpBrowser.res rename to demos/Lazarus_any_OS/ExternalPumpBrowser/ExternalPumpBrowser.res diff --git a/demos/Lazarus_any_OS/ExternalPumpBrowser/README.txt b/demos/Lazarus_any_OS/ExternalPumpBrowser/README.txt new file mode 100644 index 00000000..d0b200bc --- /dev/null +++ b/demos/Lazarus_any_OS/ExternalPumpBrowser/README.txt @@ -0,0 +1,40 @@ +ExternalPumpBrowser + +# ABOUT + +This example uses + TCEFLinkedWindowParent + TCEFWorkScheduler + +TCEFWorkScheduler feeds the CEF messageloop by calling DoMessageLoopWork(). On Mac this is currently the only way to run the CEF messageloop. + + +# SETUP + +** Windows +1) Download the CEF framework and place the content of the "Release" folder into the same folder as your exe. + Alternatively you can point "GlobalCEFApp.FrameworkDirPath" to the location with the libraries. +2) Run the project + +** Linux +1) Download the CEF framework and place the content of the "Release" folder into the same folder as your exe. + Alternatively you can point "GlobalCEFApp.FrameworkDirPath" to the location with the libraries. +2) Run the project + +Note: +- For your own Linux project you must modify the project source (lpr) and add "InitSubProcess" to the "uses" clause, so that it is in the list *before* the unit "Interfaces". +- The call to "DestroyGlobalCEFApp" must be in a unit *not* used by "unit InitSubProcess" (including not used in any nested way). + + +** Mac +1) Go to "project options" and create the "App Bundle" +2) Download the CEF framework and place the content of the "Release" folder into ExternalPumpBrowser.app/Contents/Frameworks/Chromium Embedded Framework.framework +You should have: + Chromium Embedded Framework + Libraries/* + Resources/* +3) Open project "AppHelper", create App Bundle and compile the AppHelper. + Run create_mac_helper.sh +4) Open project ExternalPumpBrowser, compile and run + + diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/cef.inc b/demos/Lazarus_any_OS/ExternalPumpBrowser/cef.inc similarity index 100% rename from demos/Lazarus_Mac/ExternalPumpBrowser/cef.inc rename to demos/Lazarus_any_OS/ExternalPumpBrowser/cef.inc diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/create_mac_helper.sh b/demos/Lazarus_any_OS/ExternalPumpBrowser/create_mac_helper.sh similarity index 100% rename from demos/Lazarus_Mac/ExternalPumpBrowser/create_mac_helper.sh rename to demos/Lazarus_any_OS/ExternalPumpBrowser/create_mac_helper.sh diff --git a/demos/Lazarus_any_OS/ExternalPumpBrowser/globalcefapplication.pas b/demos/Lazarus_any_OS/ExternalPumpBrowser/globalcefapplication.pas new file mode 100644 index 00000000..19c37e84 --- /dev/null +++ b/demos/Lazarus_any_OS/ExternalPumpBrowser/globalcefapplication.pas @@ -0,0 +1,47 @@ +unit GlobalCefApplication; + +{$mode ObjFPC}{$H+} + +interface + +uses + uCEFApplication, uCEFWorkScheduler; + +procedure CreateGlobalCEFApp; + +implementation + +procedure GlobalCEFApp_OnScheduleMessagePumpWork(const aDelayMS : int64); +begin + if (GlobalCEFWorkScheduler <> nil) then GlobalCEFWorkScheduler.ScheduleMessagePumpWork(aDelayMS); +end; + +procedure CreateGlobalCEFApp; +begin + if GlobalCEFApp <> nil then + exit; + + // TCEFWorkScheduler will call cef_do_message_loop_work when + // it's told in the GlobalCEFApp.OnScheduleMessagePumpWork event. + // GlobalCEFWorkScheduler needs to be created before the + // GlobalCEFApp.StartMainProcess call. + GlobalCEFWorkScheduler := TCEFWorkScheduler.Create(nil); + + GlobalCEFApp := TCefApplication.Create; + GlobalCEFApp.ExternalMessagePump := True; + GlobalCEFApp.MultiThreadedMessageLoop := False; + GlobalCEFApp.OnScheduleMessagePumpWork := @GlobalCEFApp_OnScheduleMessagePumpWork; + + {$IFDEF LINUX} + // This is a workaround for the 'GPU is not usable error' issue : + // https://bitbucket.org/chromiumembedded/cef/issues/2964/gpu-is-not-usable-error-during-cef + GlobalCEFApp.DisableZygote := True; // this property adds the "--no-zygote" command line switch + {$ENDIF} + { + GlobalCEFApp.LogFile := 'cef.log'; + GlobalCEFApp.LogSeverity := LOGSEVERITY_VERBOSE; + } +end; + +end. + diff --git a/demos/Lazarus_any_OS/ExternalPumpBrowser/initsubprocess.pas b/demos/Lazarus_any_OS/ExternalPumpBrowser/initsubprocess.pas new file mode 100644 index 00000000..61730473 --- /dev/null +++ b/demos/Lazarus_any_OS/ExternalPumpBrowser/initsubprocess.pas @@ -0,0 +1,55 @@ +// ************************************************************************ +// ***************************** CEF4Delphi ******************************* +// ************************************************************************ +// +// CEF4Delphi is based on DCEF3 which uses CEF to embed a chromium-based +// browser in Delphi applications. +// +// The original license of DCEF3 still applies to CEF4Delphi. +// +// For more information about CEF4Delphi visit : +// https://www.briskbard.com/index.php?lang=en&pageid=cef +// +// Copyright © 2021 Salvador Diaz Fau. All rights reserved. +// +// ************************************************************************ +// ************ vvvv Original license and comments below vvvv ************* +// ************************************************************************ +(* + * Delphi Chromium Embedded 3 + * + * Usage allowed under the restrictions of the Lesser GNU General Public License + * or alternatively the restrictions of the Mozilla Public License 1.1 + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for + * the specific language governing rights and limitations under the License. + * + * Unit owner : Henri Gourvest <hgourvest@gmail.com> + * Web site : http://www.progdigy.com + * Repository : http://code.google.com/p/delphichromiumembedded/ + * Group : http://groups.google.com/group/delphichromiumembedded + * + * Embarcadero Technologies, Inc is not permitted to use or redistribute + * this source code without explicit permission. + * + *) + +unit InitSubProcess; + +{$mode ObjFPC}{$H+} + +interface + +uses + GlobalCefApplication, uCEFApplication; + +implementation + +initialization + CreateGlobalCEFApp; + if not GlobalCEFApp.StartMainProcess then + halt(0); // exit the subprocess + +end. + diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/uExternalPumpBrowser.lfm b/demos/Lazarus_any_OS/ExternalPumpBrowser/uExternalPumpBrowser.lfm similarity index 100% rename from demos/Lazarus_Mac/ExternalPumpBrowser/uExternalPumpBrowser.lfm rename to demos/Lazarus_any_OS/ExternalPumpBrowser/uExternalPumpBrowser.lfm diff --git a/demos/Lazarus_Mac/ExternalPumpBrowser/uExternalPumpBrowser.pas b/demos/Lazarus_any_OS/ExternalPumpBrowser/uExternalPumpBrowser.pas similarity index 90% rename from demos/Lazarus_Mac/ExternalPumpBrowser/uExternalPumpBrowser.pas rename to demos/Lazarus_any_OS/ExternalPumpBrowser/uExternalPumpBrowser.pas index 68ccc1ad..ff1119dc 100644 --- a/demos/Lazarus_Mac/ExternalPumpBrowser/uExternalPumpBrowser.pas +++ b/demos/Lazarus_any_OS/ExternalPumpBrowser/uExternalPumpBrowser.pas @@ -42,6 +42,7 @@ unit uExternalPumpBrowser; interface uses + GlobalCefApplication, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, LMessages, uCEFChromium, uCEFWindowParent, uCEFConstants, uCEFTypes, uCEFInterfaces, uCEFChromiumEvents, uCEFLinkedWindowParent, uCEFWorkScheduler; @@ -97,7 +98,6 @@ type var Form1: TForm1; -procedure CreateGlobalCEFApp; implementation @@ -131,30 +131,6 @@ uses { TForm1 } -procedure GlobalCEFApp_OnScheduleMessagePumpWork(const aDelayMS : int64); -begin - if (GlobalCEFWorkScheduler <> nil) then GlobalCEFWorkScheduler.ScheduleMessagePumpWork(aDelayMS); -end; - -procedure CreateGlobalCEFApp; -begin - // TCEFWorkScheduler will call cef_do_message_loop_work when - // it's told in the GlobalCEFApp.OnScheduleMessagePumpWork event. - // GlobalCEFWorkScheduler needs to be created before the - // GlobalCEFApp.StartMainProcess call. - GlobalCEFWorkScheduler := TCEFWorkScheduler.Create(nil); - - GlobalCEFApp := TCefApplication.Create; - GlobalCEFApp.ExternalMessagePump := True; - GlobalCEFApp.MultiThreadedMessageLoop := False; - GlobalCEFApp.OnScheduleMessagePumpWork := @GlobalCEFApp_OnScheduleMessagePumpWork; - - { - GlobalCEFApp.LogFile := 'cef.log'; - GlobalCEFApp.LogSeverity := LOGSEVERITY_VERBOSE; - } -end; - procedure TForm1.FormCreate(Sender: TObject); begin FCanClose := False; @@ -219,7 +195,10 @@ end; procedure TForm1.Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction: TCefCloseBrowserAction); begin // continue closing the browser - aAction := cbaClose; + if CEFLinkedWindowParent1.DestroyChildWindow then + aAction := cbaDelay + else + aAction := cbaClose; end; procedure TForm1.Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser); @@ -295,11 +274,15 @@ begin end; initialization - CreateGlobalCEFApp; - GlobalCEFApp.StartMainProcess; + if GlobalCEFApp = nil then begin + CreateGlobalCEFApp; + if not GlobalCEFApp.StartMainProcess then + halt(0); // exit the subprocess + end; finalization - DestroyGlobalCEFApp; + (* Destroy from this unit, which is used after "Interfaces". So this happens before the Application object is destroyed *) + DestroyGlobalCEFApp; end.