-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bc348a
commit 200c223
Showing
26 changed files
with
496 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include "app_i.h" | ||
|
||
#include <stdio.h> | ||
|
||
static AppFlags app_get_flags_default(AppType type); | ||
|
||
// region Alloc/free | ||
|
||
App app_alloc(const AppManifest* manifest, Bundle* _Nullable bundle) { | ||
AppData* data = malloc(sizeof(AppData)); | ||
*data = (AppData) { | ||
.mutex = furi_mutex_alloc(FuriMutexTypeRecursive), | ||
.state = APP_STATE_INITIAL, | ||
.flags = app_get_flags_default(manifest->type), | ||
.manifest = manifest, | ||
.bundle = bundle, | ||
.data = NULL | ||
}; | ||
return (App*)data; | ||
} | ||
|
||
void app_free(App app) { | ||
AppData* data = (AppData*)app; | ||
furi_mutex_free(data->mutex); | ||
free(data); | ||
} | ||
|
||
// endregion | ||
|
||
// region Internal | ||
|
||
static void app_lock(AppData* data) { | ||
furi_mutex_acquire(data->mutex, FuriMutexTypeRecursive); | ||
} | ||
|
||
static void app_unlock(AppData* data) { | ||
furi_mutex_release(data->mutex); | ||
} | ||
|
||
static AppFlags app_get_flags_default(AppType type) { | ||
static const AppFlags DEFAULT_DESKTOP_FLAGS = { | ||
.show_toolbar = false, | ||
.show_statusbar = true | ||
}; | ||
|
||
static const AppFlags DEFAULT_APP_FLAGS = { | ||
.show_toolbar = true, | ||
.show_statusbar = true | ||
}; | ||
|
||
return type == AppTypeDesktop | ||
? DEFAULT_DESKTOP_FLAGS | ||
: DEFAULT_APP_FLAGS; | ||
} | ||
|
||
// endregion Internal | ||
|
||
// region Public getters & setters | ||
|
||
void app_set_state(App app, AppState state) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
data->state = state; | ||
app_unlock(data); | ||
} | ||
|
||
AppState app_get_state(App app) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
AppState state = data->state; | ||
app_unlock(data); | ||
return state; | ||
} | ||
|
||
const AppManifest* app_get_manifest(App app) { | ||
AppData* data = (AppData*)app; | ||
// No need to lock const data; | ||
return data->manifest; | ||
} | ||
|
||
AppFlags app_get_flags(App app) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
AppFlags flags = data->flags; | ||
app_unlock(data); | ||
return flags; | ||
} | ||
|
||
void app_set_flags(App app, AppFlags flags) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
data->flags = flags; | ||
app_unlock(data); | ||
} | ||
|
||
void* app_get_data(App app) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
void* value = data->data; | ||
app_unlock(data); | ||
return value; | ||
} | ||
|
||
void app_set_data(App app, void* value) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
data->data = value; | ||
app_unlock(data); | ||
} | ||
|
||
/** TODO: Make this thread-safe. | ||
* In practice, the bundle is writeable, so someone could be writing to it | ||
* while it is being accessed from another thread. | ||
* Consider creating MutableBundle vs Bundle. | ||
* Consider not exposing bundle, but expose `app_get_bundle_int(key)` methods with locking in it. | ||
*/ | ||
Bundle* _Nullable app_get_bundle(App app) { | ||
AppData* data = (AppData*)app; | ||
app_lock(data); | ||
Bundle* bundle = data->bundle; | ||
app_unlock(data); | ||
return bundle; | ||
} | ||
|
||
// endregion Public getters & setters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include "app_manifest.h" | ||
#include "bundle.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef enum { | ||
APP_STATE_INITIAL, // App is being activated in loader | ||
APP_STATE_STARTED, // App is in memory | ||
APP_STATE_SHOWING, // App view is created | ||
APP_STATE_HIDING, // App view is destroyed | ||
APP_STATE_STOPPED // App is not in memory | ||
} AppState; | ||
|
||
typedef union { | ||
struct { | ||
bool show_statusbar : 1; | ||
bool show_toolbar : 1; | ||
}; | ||
unsigned char flags; | ||
} AppFlags; | ||
|
||
typedef void* App; | ||
|
||
/** @brief Create an app | ||
* @param manifest | ||
* @param bundle optional bundle. memory ownership is transferred to App | ||
* @return | ||
*/ | ||
App app_alloc(const AppManifest* manifest, Bundle* _Nullable bundle); | ||
void app_free(App app); | ||
|
||
void app_set_state(App app, AppState state); | ||
AppState app_get_state(App app); | ||
|
||
const AppManifest* app_get_manifest(App app); | ||
|
||
AppFlags app_get_flags(App app); | ||
void app_set_flags(App app, AppFlags flags); | ||
|
||
void* _Nullable app_get_data(App app); | ||
void app_set_data(App app, void* data); | ||
|
||
Bundle* _Nullable app_get_bundle(App app); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
#pragma once | ||
|
||
typedef struct { | ||
/** Contextual data related to the running app's instance | ||
* | ||
* The app can attach its data to this. | ||
* The lifecycle is determined by the on_start and on_stop methods in the AppManifest. | ||
* These manifest methods can optionally allocate/free data that is attached here. | ||
*/ | ||
void* data; | ||
|
||
} Context; |
Oops, something went wrong.