[haiku-commits] haiku: hrev54671 - src/apps/autoraise

  • From: Kyle Ambroff-Kao <kyle@xxxxxxxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 22 Oct 2020 13:52:57 -0400 (EDT)

hrev54671 adds 1 changeset to branch 'master'
old head: a5e4976d39d790942b064f777d107518ea95be83
new head: 107712bf01c9777592fd24f7c06c455d1110df0a
overview: 
https://git.haiku-os.org/haiku/log/?qt=range&q=107712bf01c9+%5Ea5e4976d39d7

----------------------------------------------------------------------------

107712bf01c9: AutoRaise: Properly load tray icon bitmap
  
  AutoRaise needs to load bitmaps stored as resources in the AppRaise
  image. The strategy it used before was to store the path to itself in
  its settings file.
  
  It stored the entry_ref to the image in the settings, which may not be
  stable across reboots with packagefs. In addition, the path could
  change at some point for any other reason.
  
  This patch changes AutoRaise to just use the same method as other tray
  applets, which is to search the process for the image containing the
  AutoRaise code, and then to get the path from the image_info.
  
  Most of the initialization work is performed by TrayView::_init(),
  which is called by the constructor. Some of the operations performed
  by this method can fail, leaving TrayView partially initialized. Any
  error is handled by returning early after telling Deskbar to remove
  this item so that it will not continue running in a partially
  initialized state. Deskbar will invoke the destructor so even a
  partially initialized TrayView will be cleaned up.
  
  Fixes #16473
  
  Change-Id: Id1ac1ef78d410a7d191cfe88f1ba1db30e258adc
  Reviewed-on: https://review.haiku-os.org/c/haiku/+/3341
  Reviewed-by: Jérôme Duval <jerome.duval@xxxxxxxxx>

                                  [ Kyle Ambroff-Kao <kyle@xxxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev54671
Commit:      107712bf01c9777592fd24f7c06c455d1110df0a
URL:         https://git.haiku-os.org/haiku/commit/?id=107712bf01c9
Author:      Kyle Ambroff-Kao <kyle@xxxxxxxxxxxxxx>
Date:        Fri Oct 16 18:28:41 2020 UTC

Ticket:      https://dev.haiku-os.org/ticket/16473

----------------------------------------------------------------------------

5 files changed, 48 insertions(+), 49 deletions(-)
src/apps/autoraise/AutoRaiseApp.cpp  | 10 -----
src/apps/autoraise/AutoRaiseIcon.cpp | 70 ++++++++++++++++++++++----------
src/apps/autoraise/AutoRaiseIcon.h   |  1 -
src/apps/autoraise/settings.cpp      | 10 -----
src/apps/autoraise/settings.h        |  6 ---

----------------------------------------------------------------------------

diff --git a/src/apps/autoraise/AutoRaiseApp.cpp 
b/src/apps/autoraise/AutoRaiseApp.cpp
index 3cf84459f2..c14d18e0ef 100644
--- a/src/apps/autoraise/AutoRaiseApp.cpp
+++ b/src/apps/autoraise/AutoRaiseApp.cpp
@@ -15,16 +15,6 @@ AutoRaiseApp::AutoRaiseApp()
        removeFromDeskbar(NULL);
        fPersist = true;
        fDone = false;
-
-       //since the tray item shows an icon, and the class TrayView needs to be
-       //able to know the location of the executing binary, we write into the
-       //settings file this critical information when the app is fired up
-       app_info info;
-       be_app->GetAppInfo(&info);
-
-       //now, put the path into the settings file
-       AutoRaiseSettings settings;
-       settings.SetAppPath(info.ref);
 }
 
 AutoRaiseApp::~AutoRaiseApp()
diff --git a/src/apps/autoraise/AutoRaiseIcon.cpp 
b/src/apps/autoraise/AutoRaiseIcon.cpp
index c5697607de..a3cd3d40d1 100644
--- a/src/apps/autoraise/AutoRaiseIcon.cpp
+++ b/src/apps/autoraise/AutoRaiseIcon.cpp
@@ -31,6 +31,21 @@ status_t removeFromDeskbar(void *)
        return B_OK;
 }
 
+
+static status_t
+our_image(image_info& image)
+{
+       int32 cookie = 0;
+       while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) {
+               if ((char *)our_image >= (char *)image.text
+                       && (char *)our_image <= (char *)image.text + 
image.text_size)
+                       return B_OK;
+       }
+
+       return B_ERROR;
+}
+
+
 //**************************************************
 
 ConfigMenu::ConfigMenu(TrayView *tv, bool useMag)
@@ -201,13 +216,10 @@ void TrayView::GetPreferredSize(float *w, float *h)
 void TrayView::_init()
 {
        thread_info ti;
-       status_t err;
 
        watching = false;
        _settings = new AutoRaiseSettings;
 
-       _appPath = _settings->AppPath();
-
        raise_delay = _settings->Delay();
        current_window = 0;
        polling_delay = 100000;
@@ -224,16 +236,31 @@ void TrayView::_init()
        resume_thread(poller_thread = spawn_thread(poller, "AutoRaise desktop "
                "poller", B_NORMAL_PRIORITY, (void *)this));
 
-       //determine paths to icon files based on app path in settings file
+       image_info info;
+       {
+               status_t result = our_image(info);
+               if (result != B_OK) {
+                       printf("Unable to lookup image_info for the AutoRaise 
image: %s\n",
+                               strerror(result));
+                       removeFromDeskbar(NULL);
+                       return;
+               }
+       }
 
-       BResources res;
-       BFile theapp(&_appPath, B_READ_ONLY);
-       if ((err = res.SetTo(&theapp)) != B_OK) {
+       BFile file(info.name, B_READ_ONLY);
+       if (file.InitCheck() != B_OK) {
+               printf("Unable to access AutoRaise image file: %s\n",
+                       strerror(file.InitCheck()));
+               removeFromDeskbar(NULL);
+               return;
+       }
 
-               printf("Unable to find the app to get the resources !!!\n");
-//             removeFromDeskbar(NULL);
-//             delete _settings;
-//             return;
+       BResources res(&file);
+       if (res.InitCheck() != B_OK) {
+               printf("Unable to load image resources: %s\n",
+                       strerror(res.InitCheck()));
+               removeFromDeskbar(NULL);
+               return;
        }
 
        size_t bmsz;
@@ -242,19 +269,22 @@ void TrayView::_init()
        p = (char *)res.LoadResource('MICN', ACTIVE_ICON, &bmsz);
        _activeIcon = new BBitmap(BRect(0, 0, B_MINI_ICON-1, B_MINI_ICON -1),
                B_CMAP8);
-       if (!p)
+       if (p == NULL) {
                puts("ERROR loading active icon");
-       else
-               _activeIcon->SetBits(p, B_MINI_ICON*B_MINI_ICON, 0, B_CMAP8);
+               removeFromDeskbar(NULL);
+               return;
+       }
+       _activeIcon->SetBits(p, B_MINI_ICON * B_MINI_ICON, 0, B_CMAP8);
 
        p = (char *)res.LoadResource('MICN', INACTIVE_ICON, &bmsz);
        _inactiveIcon = new BBitmap(BRect(0, 0, B_MINI_ICON-1, B_MINI_ICON -1),
                B_CMAP8);
-       if (!p)
+       if (p == NULL) {
                puts("ERROR loading inactive icon");
-       else
-               _inactiveIcon->SetBits(p, B_MINI_ICON*B_MINI_ICON, 0, B_CMAP8);
-
+               removeFromDeskbar(NULL);
+               return;
+       }
+       _inactiveIcon->SetBits(p, B_MINI_ICON * B_MINI_ICON, 0, B_CMAP8);
 
        SetDrawingMode(B_OP_ALPHA);
        SetFlags(Flags() | B_WILL_DRAW);
@@ -281,12 +311,8 @@ TrayView::~TrayView(){
 
 //Dehydrate into a message (called by the DeskBar)
 status_t TrayView::Archive(BMessage *data, bool deep) const {
-//     BEntry appentry(&_appPath, true);
-//     BPath appPath(&appentry);
        status_t error=BView::Archive(data, deep);
        data->AddString("add_on", APP_SIG);
-//     data->AddFlat("_appPath", (BFlattenable *) &_appPath);
-       data->AddRef("_appPath", &_appPath);
 
        return error;
 }
diff --git a/src/apps/autoraise/AutoRaiseIcon.h 
b/src/apps/autoraise/AutoRaiseIcon.h
index 83164166cc..304e83f2b9 100644
--- a/src/apps/autoraise/AutoRaiseIcon.h
+++ b/src/apps/autoraise/AutoRaiseIcon.h
@@ -36,7 +36,6 @@ class TrayView:public BView{
        private:
 
                BBitmap *_activeIcon, *_inactiveIcon;
-               entry_ref _appPath;
                bool watching;
 
                void _init(void); //initialization common to all constructors
diff --git a/src/apps/autoraise/settings.cpp b/src/apps/autoraise/settings.cpp
index bee3f1a1a8..9d83541977 100644
--- a/src/apps/autoraise/settings.cpp
+++ b/src/apps/autoraise/settings.cpp
@@ -16,8 +16,6 @@ _type AutoRaiseSettings::_name()\
 CONF_ADDPROP(bool, Active)
 CONF_ADDPROP(bigtime_t, Delay)
 CONF_ADDPROP(int32, Mode)
-//CONF_ADDPROP(BPath, AppPath)
-CONF_ADDPROP(entry_ref, AppPath)
 #undef CONF_ADDPROP
 
 AutoRaiseSettings::AutoRaiseSettings()
@@ -42,12 +40,6 @@ AutoRaiseSettings::AutoRaiseSettings()
 
                if (_settingsMessage.FindInt32(AR_MODE, &_confMode) != B_OK)
                        printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed 
to load mode from settings file. Using default\n");
-
-//             if (_settingsMessage.FindFlat(AR_APP_PATH, (BFlattenable *) 
&_appPath) != B_OK)
-//                     printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed 
to load application path.\n");
-
-               if (_settingsMessage.FindRef(AR_APP_PATH, &_confAppPath) != 
B_OK)
-                       printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed 
to load application path.\n");
        }
        else
        {
@@ -73,8 +65,6 @@ AutoRaiseSettings::~AutoRaiseSettings()
        _settingsMessage.AddBool(AR_ACTIVE, _confActive);
        _settingsMessage.AddInt64(AR_DELAY, _confDelay);
        _settingsMessage.AddInt32(AR_MODE, _confMode);
-//     _settingsMessage.AddFlat(AR_APP_PATH, &_appPath);
-       _settingsMessage.AddRef(AR_APP_PATH, &_confAppPath);
 
        //write message to settings file
        if (_settingsMessage.Flatten(&_settingsFile) != B_OK)
diff --git a/src/apps/autoraise/settings.h b/src/apps/autoraise/settings.h
index 9302b12931..37555ecac8 100644
--- a/src/apps/autoraise/settings.h
+++ b/src/apps/autoraise/settings.h
@@ -40,8 +40,6 @@ class AutoRaiseSettings
 {
        protected:
                BFile _settingsFile;
-//             BPath _appPath;
-
                BMessage _settingsMessage;
                
 
@@ -56,13 +54,9 @@ class AutoRaiseSettings
 CONF_ADDPROP(bool, Active)
 CONF_ADDPROP(bigtime_t, Delay)
 CONF_ADDPROP(int32, Mode)
-//CONF_ADDPROP(BPath, AppPath)
-CONF_ADDPROP(entry_ref, AppPath)
 
 };
 
 #undef CONF_ADDPROP
 
-#define AR_APP_PATH "ar:app_path"
-
 #endif


Other related posts:

  • » [haiku-commits] haiku: hrev54671 - src/apps/autoraise - Kyle Ambroff-Kao