[FL-2627] Flipper applications: SDK, build and debug system (#1387)

* Added support for running applications from SD card (FAPs - Flipper Application Packages)
* Added plugin_dist target for fbt to build FAPs
* All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default
* Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them
* Added debugging support for FAPs with fbt debug & VSCode
* Added public firmware API with automated versioning

Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2022-09-15 02:11:38 +10:00
committed by Aleksandr Kutuzov
parent 0f6f9ad52e
commit b9a766d909
895 changed files with 8862 additions and 1465 deletions

View File

@@ -0,0 +1,575 @@
#include <gui/view_stack.h>
#include <stdint.h>
#include <furi.h>
#include <furi_hal.h>
#include <m-string.h>
#include <portmacro.h>
#include <dolphin/dolphin.h>
#include <power/power_service/power.h>
#include <storage/storage.h>
#include <assets_icons.h>
#include "views/bubble_animation_view.h"
#include "views/one_shot_animation_view.h"
#include "animation_storage.h"
#include "animation_manager.h"
#define TAG "AnimationManager"
#define HARDCODED_ANIMATION_NAME "L1_Tv_128x47"
#define NO_SD_ANIMATION_NAME "L1_NoSd_128x49"
#define BAD_BATTERY_ANIMATION_NAME "L1_BadBattery_128x47"
#define NO_DB_ANIMATION_NAME "L0_NoDb_128x51"
#define BAD_SD_ANIMATION_NAME "L0_SdBad_128x51"
#define SD_OK_ANIMATION_NAME "L0_SdOk_128x51"
#define URL_ANIMATION_NAME "L0_Url_128x51"
#define NEW_MAIL_ANIMATION_NAME "L0_NewMail_128x51"
typedef enum {
AnimationManagerStateIdle,
AnimationManagerStateBlocked,
AnimationManagerStateFreezedIdle,
AnimationManagerStateFreezedBlocked,
} AnimationManagerState;
struct AnimationManager {
bool sd_show_url;
bool sd_shown_no_db;
bool sd_shown_sd_ok;
bool levelup_pending;
bool levelup_active;
AnimationManagerState state;
FuriPubSubSubscription* pubsub_subscription_storage;
FuriPubSubSubscription* pubsub_subscription_dolphin;
BubbleAnimationView* animation_view;
OneShotView* one_shot_view;
FuriTimer* idle_animation_timer;
StorageAnimation* current_animation;
AnimationManagerInteractCallback interact_callback;
AnimationManagerSetNewIdleAnimationCallback new_idle_callback;
AnimationManagerSetNewIdleAnimationCallback check_blocking_callback;
void* context;
string_t freezed_animation_name;
int32_t freezed_animation_time_left;
ViewStack* view_stack;
};
static StorageAnimation*
animation_manager_select_idle_animation(AnimationManager* animation_manager);
static void animation_manager_replace_current_animation(
AnimationManager* animation_manager,
StorageAnimation* storage_animation);
static void animation_manager_start_new_idle(AnimationManager* animation_manager);
static bool animation_manager_check_blocking(AnimationManager* animation_manager);
static bool animation_manager_is_valid_idle_animation(
const StorageAnimationManifestInfo* info,
const DolphinStats* stats);
static void animation_manager_switch_to_one_shot_view(AnimationManager* animation_manager);
static void animation_manager_switch_to_animation_view(AnimationManager* animation_manager);
void animation_manager_set_context(AnimationManager* animation_manager, void* context) {
furi_assert(animation_manager);
animation_manager->context = context;
}
void animation_manager_set_new_idle_callback(
AnimationManager* animation_manager,
AnimationManagerSetNewIdleAnimationCallback callback) {
furi_assert(animation_manager);
animation_manager->new_idle_callback = callback;
}
void animation_manager_set_check_callback(
AnimationManager* animation_manager,
AnimationManagerCheckBlockingCallback callback) {
furi_assert(animation_manager);
animation_manager->check_blocking_callback = callback;
}
void animation_manager_set_interact_callback(
AnimationManager* animation_manager,
AnimationManagerInteractCallback callback) {
furi_assert(animation_manager);
animation_manager->interact_callback = callback;
}
static void animation_manager_check_blocking_callback(const void* message, void* context) {
const StorageEvent* storage_event = message;
switch(storage_event->type) {
case StorageEventTypeCardMount:
case StorageEventTypeCardUnmount:
case StorageEventTypeCardMountError:
furi_assert(context);
AnimationManager* animation_manager = context;
if(animation_manager->check_blocking_callback) {
animation_manager->check_blocking_callback(animation_manager->context);
}
break;
default:
break;
}
}
static void animation_manager_timer_callback(void* context) {
furi_assert(context);
AnimationManager* animation_manager = context;
if(animation_manager->new_idle_callback) {
animation_manager->new_idle_callback(animation_manager->context);
}
}
static void animation_manager_interact_callback(void* context) {
furi_assert(context);
AnimationManager* animation_manager = context;
if(animation_manager->interact_callback) {
animation_manager->interact_callback(animation_manager->context);
}
}
/* reaction to animation_manager->check_blocking_callback() */
void animation_manager_check_blocking_process(AnimationManager* animation_manager) {
furi_assert(animation_manager);
if(animation_manager->state == AnimationManagerStateIdle) {
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
const StorageAnimationManifestInfo* manifest_info =
animation_storage_get_meta(animation_manager->current_animation);
bool valid = animation_manager_is_valid_idle_animation(manifest_info, &stats);
if(!valid) {
animation_manager_start_new_idle(animation_manager);
}
}
}
}
/* reaction to animation_manager->new_idle_callback() */
void animation_manager_new_idle_process(AnimationManager* animation_manager) {
furi_assert(animation_manager);
if(animation_manager->state == AnimationManagerStateIdle) {
animation_manager_start_new_idle(animation_manager);
}
}
/* reaction to animation_manager->interact_callback() */
bool animation_manager_interact_process(AnimationManager* animation_manager) {
furi_assert(animation_manager);
bool consumed = true;
if(animation_manager->levelup_pending) {
animation_manager->levelup_pending = false;
animation_manager->levelup_active = true;
animation_manager_switch_to_one_shot_view(animation_manager);
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
dolphin_upgrade_level(dolphin);
furi_record_close(RECORD_DOLPHIN);
} else if(animation_manager->levelup_active) {
animation_manager->levelup_active = false;
animation_manager_start_new_idle(animation_manager);
animation_manager_switch_to_animation_view(animation_manager);
} else if(animation_manager->state == AnimationManagerStateBlocked) {
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
animation_manager_start_new_idle(animation_manager);
}
} else {
consumed = false;
}
return consumed;
}
static void animation_manager_start_new_idle(AnimationManager* animation_manager) {
furi_assert(animation_manager);
StorageAnimation* new_animation = animation_manager_select_idle_animation(animation_manager);
animation_manager_replace_current_animation(animation_manager, new_animation);
const BubbleAnimation* bubble_animation =
animation_storage_get_bubble_animation(animation_manager->current_animation);
animation_manager->state = AnimationManagerStateIdle;
furi_timer_start(animation_manager->idle_animation_timer, bubble_animation->duration * 1000);
}
static bool animation_manager_check_blocking(AnimationManager* animation_manager) {
furi_assert(animation_manager);
StorageAnimation* blocking_animation = NULL;
Storage* storage = furi_record_open(RECORD_STORAGE);
FS_Error sd_status = storage_sd_status(storage);
if(sd_status == FSE_INTERNAL) {
blocking_animation = animation_storage_find_animation(BAD_SD_ANIMATION_NAME);
furi_assert(blocking_animation);
} else if(sd_status == FSE_NOT_READY) {
animation_manager->sd_shown_sd_ok = false;
animation_manager->sd_shown_no_db = false;
} else if(sd_status == FSE_OK) {
if(!animation_manager->sd_shown_sd_ok) {
blocking_animation = animation_storage_find_animation(SD_OK_ANIMATION_NAME);
furi_assert(blocking_animation);
animation_manager->sd_shown_sd_ok = true;
} else if(!animation_manager->sd_shown_no_db) {
if(!storage_file_exists(storage, EXT_PATH("Manifest"))) {
blocking_animation = animation_storage_find_animation(NO_DB_ANIMATION_NAME);
furi_assert(blocking_animation);
animation_manager->sd_shown_no_db = true;
animation_manager->sd_show_url = true;
}
} else if(animation_manager->sd_show_url) {
blocking_animation = animation_storage_find_animation(URL_ANIMATION_NAME);
furi_assert(blocking_animation);
animation_manager->sd_show_url = false;
}
}
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
if(!blocking_animation && stats.level_up_is_pending) {
blocking_animation = animation_storage_find_animation(NEW_MAIL_ANIMATION_NAME);
furi_assert(blocking_animation);
if(blocking_animation) {
animation_manager->levelup_pending = true;
}
}
if(blocking_animation) {
furi_timer_stop(animation_manager->idle_animation_timer);
animation_manager_replace_current_animation(animation_manager, blocking_animation);
/* no timer starting because this is blocking animation */
animation_manager->state = AnimationManagerStateBlocked;
}
furi_record_close(RECORD_STORAGE);
return !!blocking_animation;
}
static void animation_manager_replace_current_animation(
AnimationManager* animation_manager,
StorageAnimation* storage_animation) {
furi_assert(storage_animation);
StorageAnimation* previous_animation = animation_manager->current_animation;
const BubbleAnimation* animation = animation_storage_get_bubble_animation(storage_animation);
bubble_animation_view_set_animation(animation_manager->animation_view, animation);
const char* new_name = animation_storage_get_meta(storage_animation)->name;
FURI_LOG_I(TAG, "Select \'%s\' animation", new_name);
animation_manager->current_animation = storage_animation;
if(previous_animation) {
animation_storage_free_storage_animation(&previous_animation);
}
}
AnimationManager* animation_manager_alloc(void) {
AnimationManager* animation_manager = malloc(sizeof(AnimationManager));
animation_manager->animation_view = bubble_animation_view_alloc();
animation_manager->view_stack = view_stack_alloc();
View* animation_view = bubble_animation_get_view(animation_manager->animation_view);
view_stack_add_view(animation_manager->view_stack, animation_view);
string_init(animation_manager->freezed_animation_name);
animation_manager->idle_animation_timer =
furi_timer_alloc(animation_manager_timer_callback, FuriTimerTypeOnce, animation_manager);
bubble_animation_view_set_interact_callback(
animation_manager->animation_view, animation_manager_interact_callback, animation_manager);
Storage* storage = furi_record_open(RECORD_STORAGE);
animation_manager->pubsub_subscription_storage = furi_pubsub_subscribe(
storage_get_pubsub(storage), animation_manager_check_blocking_callback, animation_manager);
furi_record_close(RECORD_STORAGE);
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
animation_manager->pubsub_subscription_dolphin = furi_pubsub_subscribe(
dolphin_get_pubsub(dolphin), animation_manager_check_blocking_callback, animation_manager);
furi_record_close(RECORD_DOLPHIN);
animation_manager->sd_shown_sd_ok = true;
if(!animation_manager_check_blocking(animation_manager)) {
animation_manager_start_new_idle(animation_manager);
}
return animation_manager;
}
void animation_manager_free(AnimationManager* animation_manager) {
furi_assert(animation_manager);
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
furi_pubsub_unsubscribe(
dolphin_get_pubsub(dolphin), animation_manager->pubsub_subscription_dolphin);
furi_record_close(RECORD_DOLPHIN);
Storage* storage = furi_record_open(RECORD_STORAGE);
furi_pubsub_unsubscribe(
storage_get_pubsub(storage), animation_manager->pubsub_subscription_storage);
furi_record_close(RECORD_STORAGE);
string_clear(animation_manager->freezed_animation_name);
View* animation_view = bubble_animation_get_view(animation_manager->animation_view);
view_stack_remove_view(animation_manager->view_stack, animation_view);
bubble_animation_view_free(animation_manager->animation_view);
furi_timer_free(animation_manager->idle_animation_timer);
}
View* animation_manager_get_animation_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
return view_stack_get_view(animation_manager->view_stack);
}
static bool animation_manager_is_valid_idle_animation(
const StorageAnimationManifestInfo* info,
const DolphinStats* stats) {
furi_assert(info);
furi_assert(info->name);
bool result = true;
if(!strcmp(info->name, BAD_BATTERY_ANIMATION_NAME)) {
Power* power = furi_record_open(RECORD_POWER);
bool battery_is_well = power_is_battery_healthy(power);
furi_record_close(RECORD_POWER);
result = !battery_is_well;
}
if(!strcmp(info->name, NO_SD_ANIMATION_NAME)) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FS_Error sd_status = storage_sd_status(storage);
furi_record_close(RECORD_STORAGE);
result = (sd_status == FSE_NOT_READY);
}
if((stats->butthurt < info->min_butthurt) || (stats->butthurt > info->max_butthurt)) {
result = false;
}
if((stats->level < info->min_level) || (stats->level > info->max_level)) {
result = false;
}
return result;
}
static StorageAnimation*
animation_manager_select_idle_animation(AnimationManager* animation_manager) {
UNUSED(animation_manager);
StorageAnimationList_t animation_list;
StorageAnimationList_init(animation_list);
animation_storage_fill_animation_list(&animation_list);
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
uint32_t whole_weight = 0;
StorageAnimationList_it_t it;
for(StorageAnimationList_it(it, animation_list); !StorageAnimationList_end_p(it);) {
StorageAnimation* storage_animation = *StorageAnimationList_ref(it);
const StorageAnimationManifestInfo* manifest_info =
animation_storage_get_meta(storage_animation);
bool valid = animation_manager_is_valid_idle_animation(manifest_info, &stats);
if(valid) {
whole_weight += manifest_info->weight;
StorageAnimationList_next(it);
} else {
animation_storage_free_storage_animation(&storage_animation);
/* remove and increase iterator */
StorageAnimationList_remove(animation_list, it);
}
}
uint32_t lucky_number = furi_hal_random_get() % whole_weight;
uint32_t weight = 0;
StorageAnimation* selected = NULL;
for
M_EACH(item, animation_list, StorageAnimationList_t) {
if(lucky_number < weight) {
break;
}
weight += animation_storage_get_meta(*item)->weight;
selected = *item;
}
for
M_EACH(item, animation_list, StorageAnimationList_t) {
if(*item != selected) {
animation_storage_free_storage_animation(item);
}
}
StorageAnimationList_clear(animation_list);
/* cache animation, if failed - choose reliable animation */
if(!animation_storage_get_bubble_animation(selected)) {
const char* name = animation_storage_get_meta(selected)->name;
FURI_LOG_E(TAG, "Can't upload animation described in manifest: \'%s\'", name);
animation_storage_free_storage_animation(&selected);
selected = animation_storage_find_animation(HARDCODED_ANIMATION_NAME);
}
furi_assert(selected);
return selected;
}
bool animation_manager_is_animation_loaded(AnimationManager* animation_manager) {
furi_assert(animation_manager);
return animation_manager->current_animation;
}
void animation_manager_unload_and_stall_animation(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(animation_manager->current_animation);
furi_assert(!string_size(animation_manager->freezed_animation_name));
furi_assert(
(animation_manager->state == AnimationManagerStateIdle) ||
(animation_manager->state == AnimationManagerStateBlocked));
if(animation_manager->state == AnimationManagerStateBlocked) {
animation_manager->state = AnimationManagerStateFreezedBlocked;
} else if(animation_manager->state == AnimationManagerStateIdle) {
animation_manager->state = AnimationManagerStateFreezedIdle;
animation_manager->freezed_animation_time_left =
xTimerGetExpiryTime(animation_manager->idle_animation_timer) - xTaskGetTickCount();
if(animation_manager->freezed_animation_time_left < 0) {
animation_manager->freezed_animation_time_left = 0;
}
furi_timer_stop(animation_manager->idle_animation_timer);
} else {
furi_assert(0);
}
FURI_LOG_I(
TAG,
"Unload animation \'%s\'",
animation_storage_get_meta(animation_manager->current_animation)->name);
StorageAnimationManifestInfo* meta =
animation_storage_get_meta(animation_manager->current_animation);
/* copy str, not move, because it can be internal animation */
string_set_str(animation_manager->freezed_animation_name, meta->name);
bubble_animation_freeze(animation_manager->animation_view);
animation_storage_free_storage_animation(&animation_manager->current_animation);
}
void animation_manager_load_and_continue_animation(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(!animation_manager->current_animation);
furi_assert(string_size(animation_manager->freezed_animation_name));
furi_assert(
(animation_manager->state == AnimationManagerStateFreezedIdle) ||
(animation_manager->state == AnimationManagerStateFreezedBlocked));
if(animation_manager->state == AnimationManagerStateFreezedBlocked) {
StorageAnimation* restore_animation = animation_storage_find_animation(
string_get_cstr(animation_manager->freezed_animation_name));
/* all blocked animations must be in flipper -> we can
* always find blocking animation */
furi_assert(restore_animation);
animation_manager_replace_current_animation(animation_manager, restore_animation);
animation_manager->state = AnimationManagerStateBlocked;
} else if(animation_manager->state == AnimationManagerStateFreezedIdle) {
/* check if we missed some system notifications, and set current_animation */
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
/* if no blocking - try restore last one idle */
StorageAnimation* restore_animation = animation_storage_find_animation(
string_get_cstr(animation_manager->freezed_animation_name));
if(restore_animation) {
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
const StorageAnimationManifestInfo* manifest_info =
animation_storage_get_meta(restore_animation);
bool valid = animation_manager_is_valid_idle_animation(manifest_info, &stats);
if(valid) {
animation_manager_replace_current_animation(
animation_manager, restore_animation);
animation_manager->state = AnimationManagerStateIdle;
if(animation_manager->freezed_animation_time_left) {
furi_timer_start(
animation_manager->idle_animation_timer,
animation_manager->freezed_animation_time_left);
} else {
const BubbleAnimation* animation = animation_storage_get_bubble_animation(
animation_manager->current_animation);
furi_timer_start(
animation_manager->idle_animation_timer, animation->duration * 1000);
}
}
} else {
FURI_LOG_E(
TAG,
"Failed to restore \'%s\'",
string_get_cstr(animation_manager->freezed_animation_name));
}
}
} else {
/* Unknown state is an error. But not in release version.*/
furi_assert(0);
}
/* if can't restore previous animation - select new */
if(!animation_manager->current_animation) {
animation_manager_start_new_idle(animation_manager);
}
FURI_LOG_I(
TAG,
"Load animation \'%s\'",
animation_storage_get_meta(animation_manager->current_animation)->name);
bubble_animation_unfreeze(animation_manager->animation_view);
string_reset(animation_manager->freezed_animation_name);
furi_assert(animation_manager->current_animation);
}
static void animation_manager_switch_to_one_shot_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(!animation_manager->one_shot_view);
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
animation_manager->one_shot_view = one_shot_view_alloc();
one_shot_view_set_interact_callback(
animation_manager->one_shot_view, animation_manager_interact_callback, animation_manager);
View* prev_view = bubble_animation_get_view(animation_manager->animation_view);
View* next_view = one_shot_view_get_view(animation_manager->one_shot_view);
view_stack_remove_view(animation_manager->view_stack, prev_view);
view_stack_add_view(animation_manager->view_stack, next_view);
if(stats.level == 1) {
one_shot_view_start_animation(animation_manager->one_shot_view, &A_Levelup1_128x64);
} else if(stats.level == 2) {
one_shot_view_start_animation(animation_manager->one_shot_view, &A_Levelup2_128x64);
} else {
furi_assert(0);
}
}
static void animation_manager_switch_to_animation_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(animation_manager->one_shot_view);
View* prev_view = one_shot_view_get_view(animation_manager->one_shot_view);
View* next_view = bubble_animation_get_view(animation_manager->animation_view);
view_stack_remove_view(animation_manager->view_stack, prev_view);
view_stack_add_view(animation_manager->view_stack, next_view);
one_shot_view_free(animation_manager->one_shot_view);
animation_manager->one_shot_view = NULL;
}

View File

@@ -0,0 +1,159 @@
#pragma once
#include <gui/view.h>
#include <gui/icon_i.h>
#include <stdint.h>
#include <dolphin/dolphin.h>
typedef struct AnimationManager AnimationManager;
typedef struct {
uint8_t x;
uint8_t y;
const char* text;
Align align_h;
Align align_v;
} Bubble;
typedef struct FrameBubble {
Bubble bubble;
uint8_t start_frame;
uint8_t end_frame;
const struct FrameBubble* next_bubble;
} FrameBubble;
typedef struct {
const FrameBubble* const* frame_bubble_sequences;
uint8_t frame_bubble_sequences_count;
const Icon icon_animation;
const uint8_t* frame_order;
uint8_t passive_frames;
uint8_t active_frames;
uint8_t active_cycles;
uint16_t duration;
uint16_t active_cooldown;
} BubbleAnimation;
typedef void (*AnimationManagerSetNewIdleAnimationCallback)(void* context);
typedef void (*AnimationManagerCheckBlockingCallback)(void* context);
typedef void (*AnimationManagerInteractCallback)(void*);
/**
* Allocate Animation Manager
*
* @return animation manager instance
*/
AnimationManager* animation_manager_alloc(void);
/**
* Free Animation Manager
*
* @animation_manager instance
*/
void animation_manager_free(AnimationManager* animation_manager);
/**
* Get View of Animation Manager
*
* @animation_manager instance
* @return view
*/
View* animation_manager_get_animation_view(AnimationManager* animation_manager);
/**
* Set context for all callbacks for Animation Manager
*
* @animation_manager instance
* @context context
*/
void animation_manager_set_context(AnimationManager* animation_manager, void* context);
/**
* Set callback for Animation Manager for defered calls
* for animation_manager_new_idle_process().
* Animation Manager doesn't have it's own thread, so main thread gives
* callbacks to A.M. to call when it should perform some inner manipulations.
* This callback is called from other threads and should notify main thread
* when to call animation_manager_new_idle_process().
* So scheme is this:
* A.M. sets callbacks,
* callbacks notifies main thread
* main thread in its own context calls appropriate *_process() function.
*
* @animation_manager instance
* @callback callback
*/
void animation_manager_set_new_idle_callback(
AnimationManager* animation_manager,
AnimationManagerSetNewIdleAnimationCallback callback);
/**
* Function to call in main thread as a response to
* set_new_idle_callback's call.
*
* @animation_manager instance
*/
void animation_manager_new_idle_process(AnimationManager* animation_manager);
/**
* Set callback for Animation Manager for defered calls
* for animation_manager_check_blocking_process().
*
* @animation_manager instance
* @callback callback
*/
void animation_manager_set_check_callback(
AnimationManager* animation_manager,
AnimationManagerCheckBlockingCallback callback);
/**
* Function to call in main thread as a response to
* set_new_idle_callback's call.
*
* @animation_manager instance
*/
void animation_manager_check_blocking_process(AnimationManager* animation_manager);
/**
* Set callback for Animation Manager for defered calls
* for animation_manager_interact_process().
*
* @animation_manager instance
* @callback callback
*/
void animation_manager_set_interact_callback(
AnimationManager* animation_manager,
AnimationManagerInteractCallback callback);
/**
* Function to call in main thread as a response to
* set_new_idle_callback's call.
*
* @animation_manager instance
* @return true if event was consumed
*/
bool animation_manager_interact_process(AnimationManager* animation_manager);
/** Check if animation loaded
*
* @animation_manager instance
*/
bool animation_manager_is_animation_loaded(AnimationManager* animation_manager);
/**
* Unload and Stall animation actions. Draw callback in view
* paints first frame of current animation until
* animation_manager_load_and_continue_animation() is called.
* Can't be called multiple times. Every Stall has to be finished
* with Continue.
*
* @animation_manager instance
*/
void animation_manager_unload_and_stall_animation(AnimationManager* animation_manager);
/**
* Load and Contunue execution of animation manager.
*
* @animation_manager instance
*/
void animation_manager_load_and_continue_animation(AnimationManager* animation_manager);

View File

@@ -0,0 +1,535 @@
#include <stdint.h>
#include <flipper_format/flipper_format.h>
#include <furi.h>
#include <core/dangerous_defines.h>
#include <storage/storage.h>
#include <gui/icon_i.h>
#include <m-string.h>
#include "animation_manager.h"
#include "animation_storage.h"
#include "animation_storage_i.h"
#include <assets_dolphin_internal.h>
#include <assets_dolphin_blocking.h>
#define ANIMATION_META_FILE "meta.txt"
#define ANIMATION_DIR EXT_PATH("dolphin")
#define ANIMATION_MANIFEST_FILE ANIMATION_DIR "/manifest.txt"
#define TAG "AnimationStorage"
static void animation_storage_free_bubbles(BubbleAnimation* animation);
static void animation_storage_free_frames(BubbleAnimation* animation);
static void animation_storage_free_animation(BubbleAnimation** storage_animation);
static BubbleAnimation* animation_storage_load_animation(const char* name);
static bool animation_storage_load_single_manifest_info(
StorageAnimationManifestInfo* manifest_info,
const char* name) {
furi_assert(manifest_info);
bool result = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
flipper_format_set_strict_mode(file, true);
string_t read_string;
string_init(read_string);
do {
uint32_t u32value;
if(FSE_OK != storage_sd_status(storage)) break;
if(!flipper_format_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
if(!flipper_format_read_header(file, read_string, &u32value)) break;
if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
manifest_info->name = NULL;
/* skip other animation names */
flipper_format_set_strict_mode(file, false);
while(flipper_format_read_string(file, "Name", read_string) &&
string_cmp_str(read_string, name))
;
if(string_cmp_str(read_string, name)) break;
flipper_format_set_strict_mode(file, true);
manifest_info->name = malloc(string_size(read_string) + 1);
strcpy((char*)manifest_info->name, string_get_cstr(read_string));
if(!flipper_format_read_uint32(file, "Min butthurt", &u32value, 1)) break;
manifest_info->min_butthurt = u32value;
if(!flipper_format_read_uint32(file, "Max butthurt", &u32value, 1)) break;
manifest_info->max_butthurt = u32value;
if(!flipper_format_read_uint32(file, "Min level", &u32value, 1)) break;
manifest_info->min_level = u32value;
if(!flipper_format_read_uint32(file, "Max level", &u32value, 1)) break;
manifest_info->max_level = u32value;
if(!flipper_format_read_uint32(file, "Weight", &u32value, 1)) break;
manifest_info->weight = u32value;
result = true;
} while(0);
if(!result && manifest_info->name) {
free((void*)manifest_info->name);
}
string_clear(read_string);
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
return result;
}
void animation_storage_fill_animation_list(StorageAnimationList_t* animation_list) {
furi_assert(sizeof(StorageAnimationList_t) == sizeof(void*));
furi_assert(!StorageAnimationList_size(*animation_list));
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
/* Forbid skipping fields */
flipper_format_set_strict_mode(file, true);
string_t read_string;
string_init(read_string);
do {
uint32_t u32value;
StorageAnimation* storage_animation = NULL;
if(FSE_OK != storage_sd_status(storage)) break;
if(!flipper_format_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
if(!flipper_format_read_header(file, read_string, &u32value)) break;
if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
do {
storage_animation = malloc(sizeof(StorageAnimation));
storage_animation->external = true;
storage_animation->animation = NULL;
storage_animation->manifest_info.name = NULL;
if(!flipper_format_read_string(file, "Name", read_string)) break;
storage_animation->manifest_info.name = malloc(string_size(read_string) + 1);
strcpy((char*)storage_animation->manifest_info.name, string_get_cstr(read_string));
if(!flipper_format_read_uint32(file, "Min butthurt", &u32value, 1)) break;
storage_animation->manifest_info.min_butthurt = u32value;
if(!flipper_format_read_uint32(file, "Max butthurt", &u32value, 1)) break;
storage_animation->manifest_info.max_butthurt = u32value;
if(!flipper_format_read_uint32(file, "Min level", &u32value, 1)) break;
storage_animation->manifest_info.min_level = u32value;
if(!flipper_format_read_uint32(file, "Max level", &u32value, 1)) break;
storage_animation->manifest_info.max_level = u32value;
if(!flipper_format_read_uint32(file, "Weight", &u32value, 1)) break;
storage_animation->manifest_info.weight = u32value;
StorageAnimationList_push_back(*animation_list, storage_animation);
} while(1);
animation_storage_free_storage_animation(&storage_animation);
} while(0);
string_clear(read_string);
flipper_format_free(file);
// add hard-coded animations
for(size_t i = 0; i < dolphin_internal_size; ++i) {
StorageAnimationList_push_back(*animation_list, (StorageAnimation*)&dolphin_internal[i]);
}
furi_record_close(RECORD_STORAGE);
}
StorageAnimation* animation_storage_find_animation(const char* name) {
furi_assert(name);
furi_assert(strlen(name));
StorageAnimation* storage_animation = NULL;
for(size_t i = 0; i < dolphin_blocking_size; ++i) {
if(!strcmp(dolphin_blocking[i].manifest_info.name, name)) {
storage_animation = (StorageAnimation*)&dolphin_blocking[i];
break;
}
}
if(!storage_animation) {
for(size_t i = 0; i < dolphin_internal_size; ++i) {
if(!strcmp(dolphin_internal[i].manifest_info.name, name)) {
storage_animation = (StorageAnimation*)&dolphin_internal[i];
break;
}
}
}
/* look through external animations */
if(!storage_animation) {
storage_animation = malloc(sizeof(StorageAnimation));
storage_animation->external = true;
bool result = false;
result =
animation_storage_load_single_manifest_info(&storage_animation->manifest_info, name);
if(result) {
storage_animation->animation = animation_storage_load_animation(name);
result = !!storage_animation->animation;
}
if(!result) {
animation_storage_free_storage_animation(&storage_animation);
}
}
return storage_animation;
}
StorageAnimationManifestInfo* animation_storage_get_meta(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
return &storage_animation->manifest_info;
}
const BubbleAnimation*
animation_storage_get_bubble_animation(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
animation_storage_cache_animation(storage_animation);
return storage_animation->animation;
}
void animation_storage_cache_animation(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
if(storage_animation->external) {
if(!storage_animation->animation) {
storage_animation->animation =
animation_storage_load_animation(storage_animation->manifest_info.name);
}
}
}
static void animation_storage_free_animation(BubbleAnimation** animation) {
furi_assert(animation);
if(*animation) {
animation_storage_free_bubbles(*animation);
animation_storage_free_frames(*animation);
if((*animation)->frame_order) {
free((void*)(*animation)->frame_order);
}
free(*animation);
*animation = NULL;
}
}
void animation_storage_free_storage_animation(StorageAnimation** storage_animation) {
furi_assert(storage_animation);
furi_assert(*storage_animation);
if((*storage_animation)->external) {
animation_storage_free_animation((BubbleAnimation**)&(*storage_animation)->animation);
if((*storage_animation)->manifest_info.name) {
free((void*)(*storage_animation)->manifest_info.name);
}
free(*storage_animation);
}
*storage_animation = NULL;
}
static bool animation_storage_cast_align(string_t align_str, Align* align) {
if(!string_cmp_str(align_str, "Bottom")) {
*align = AlignBottom;
} else if(!string_cmp_str(align_str, "Top")) {
*align = AlignTop;
} else if(!string_cmp_str(align_str, "Left")) {
*align = AlignLeft;
} else if(!string_cmp_str(align_str, "Right")) {
*align = AlignRight;
} else if(!string_cmp_str(align_str, "Center")) {
*align = AlignCenter;
} else {
return false;
}
return true;
}
static void animation_storage_free_frames(BubbleAnimation* animation) {
furi_assert(animation);
const Icon* icon = &animation->icon_animation;
for(int i = 0; i < icon->frame_count; ++i) {
if(icon->frames[i]) {
free((void*)icon->frames[i]);
}
}
free((void*)icon->frames);
}
static bool animation_storage_load_frames(
Storage* storage,
const char* name,
BubbleAnimation* animation,
uint32_t* frame_order,
uint8_t width,
uint8_t height) {
uint16_t frame_order_count = animation->passive_frames + animation->active_frames;
/* The frames should go in order (0...N), without omissions */
size_t max_frame_count = 0;
for(int i = 0; i < frame_order_count; ++i) {
max_frame_count = MAX(max_frame_count, frame_order[i]);
}
if((max_frame_count >= frame_order_count) || (max_frame_count >= 256 /* max uint8_t */)) {
return false;
}
Icon* icon = (Icon*)&animation->icon_animation;
FURI_CONST_ASSIGN(icon->frame_count, max_frame_count + 1);
FURI_CONST_ASSIGN(icon->frame_rate, 0);
FURI_CONST_ASSIGN(icon->height, height);
FURI_CONST_ASSIGN(icon->width, width);
icon->frames = malloc(sizeof(const uint8_t*) * icon->frame_count);
bool frames_ok = false;
File* file = storage_file_alloc(storage);
FileInfo file_info;
string_t filename;
string_init(filename);
size_t max_filesize = ROUND_UP_TO(width, 8) * height + 1;
for(int i = 0; i < icon->frame_count; ++i) {
frames_ok = false;
string_printf(filename, ANIMATION_DIR "/%s/frame_%d.bm", name, i);
if(storage_common_stat(storage, string_get_cstr(filename), &file_info) != FSE_OK) break;
if(file_info.size > max_filesize) {
FURI_LOG_E(
TAG,
"Filesize %d, max: %d (width %d, height %d)",
file_info.size,
max_filesize,
width,
height);
break;
}
if(!storage_file_open(file, string_get_cstr(filename), FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Can't open file \'%s\'", string_get_cstr(filename));
break;
}
FURI_CONST_ASSIGN_PTR(icon->frames[i], malloc(file_info.size));
if(storage_file_read(file, (void*)icon->frames[i], file_info.size) != file_info.size) {
FURI_LOG_E(TAG, "Read failed: \'%s\'", string_get_cstr(filename));
break;
}
storage_file_close(file);
frames_ok = true;
}
if(!frames_ok) {
FURI_LOG_E(
TAG,
"Load \'%s\' failed, %dx%d, size: %d",
string_get_cstr(filename),
width,
height,
file_info.size);
animation_storage_free_frames(animation);
} else {
furi_check(animation->icon_animation.frames);
for(int i = 0; i < animation->icon_animation.frame_count; ++i) {
furi_check(animation->icon_animation.frames[i]);
}
}
storage_file_free(file);
string_clear(filename);
return frames_ok;
}
static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFormat* ff) {
uint32_t u32value;
string_t str;
string_init(str);
bool success = false;
furi_assert(!animation->frame_bubble_sequences);
do {
if(!flipper_format_read_uint32(ff, "Bubble slots", &u32value, 1)) break;
if(u32value > 20) break;
animation->frame_bubble_sequences_count = u32value;
if(animation->frame_bubble_sequences_count == 0) {
animation->frame_bubble_sequences = NULL;
success = true;
break;
}
animation->frame_bubble_sequences =
malloc(sizeof(FrameBubble*) * animation->frame_bubble_sequences_count);
int32_t current_slot = 0;
for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
FURI_CONST_ASSIGN_PTR(
animation->frame_bubble_sequences[i], malloc(sizeof(FrameBubble)));
}
const FrameBubble* bubble = animation->frame_bubble_sequences[0];
int8_t index = -1;
for(;;) {
if(!flipper_format_read_int32(ff, "Slot", &current_slot, 1)) break;
if((current_slot != 0) && (index == -1)) break;
if(current_slot == index) {
FURI_CONST_ASSIGN_PTR(bubble->next_bubble, malloc(sizeof(FrameBubble)));
bubble = bubble->next_bubble;
} else if(current_slot == index + 1) {
++index;
bubble = animation->frame_bubble_sequences[index];
} else {
/* slots have to start from 0, be ascending sorted, and
* have exact number of slots as specified in "Bubble slots" */
break;
}
if(index >= animation->frame_bubble_sequences_count) break;
if(!flipper_format_read_uint32(ff, "X", &u32value, 1)) break;
FURI_CONST_ASSIGN(bubble->bubble.x, u32value);
if(!flipper_format_read_uint32(ff, "Y", &u32value, 1)) break;
FURI_CONST_ASSIGN(bubble->bubble.y, u32value);
if(!flipper_format_read_string(ff, "Text", str)) break;
if(string_size(str) > 100) break;
string_replace_all_str(str, "\\n", "\n");
FURI_CONST_ASSIGN_PTR(bubble->bubble.text, malloc(string_size(str) + 1));
strcpy((char*)bubble->bubble.text, string_get_cstr(str));
if(!flipper_format_read_string(ff, "AlignH", str)) break;
if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_h)) break;
if(!flipper_format_read_string(ff, "AlignV", str)) break;
if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_v)) break;
if(!flipper_format_read_uint32(ff, "StartFrame", &u32value, 1)) break;
FURI_CONST_ASSIGN(bubble->start_frame, u32value);
if(!flipper_format_read_uint32(ff, "EndFrame", &u32value, 1)) break;
FURI_CONST_ASSIGN(bubble->end_frame, u32value);
}
success = (index + 1) == animation->frame_bubble_sequences_count;
} while(0);
if(!success) {
if(animation->frame_bubble_sequences) {
FURI_LOG_E(TAG, "Failed to load animation bubbles");
animation_storage_free_bubbles(animation);
}
}
string_clear(str);
return success;
}
static BubbleAnimation* animation_storage_load_animation(const char* name) {
furi_assert(name);
BubbleAnimation* animation = malloc(sizeof(BubbleAnimation));
uint32_t height = 0;
uint32_t width = 0;
uint32_t* u32array = NULL;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_file_alloc(storage);
/* Forbid skipping fields */
flipper_format_set_strict_mode(ff, true);
string_t str;
string_init(str);
animation->frame_bubble_sequences = NULL;
bool success = false;
do {
uint32_t u32value;
if(FSE_OK != storage_sd_status(storage)) break;
string_printf(str, ANIMATION_DIR "/%s/" ANIMATION_META_FILE, name);
if(!flipper_format_file_open_existing(ff, string_get_cstr(str))) break;
if(!flipper_format_read_header(ff, str, &u32value)) break;
if(string_cmp_str(str, "Flipper Animation")) break;
if(!flipper_format_read_uint32(ff, "Width", &width, 1)) break;
if(!flipper_format_read_uint32(ff, "Height", &height, 1)) break;
if(!flipper_format_read_uint32(ff, "Passive frames", &u32value, 1)) break;
animation->passive_frames = u32value;
if(!flipper_format_read_uint32(ff, "Active frames", &u32value, 1)) break;
animation->active_frames = u32value;
uint8_t frames = animation->passive_frames + animation->active_frames;
uint32_t count = 0;
if(!flipper_format_get_value_count(ff, "Frames order", &count)) break;
if(count != frames) {
FURI_LOG_E(TAG, "Error loading animation: frames order");
break;
}
u32array = malloc(sizeof(uint32_t) * frames);
if(!flipper_format_read_uint32(ff, "Frames order", u32array, frames)) break;
animation->frame_order = malloc(sizeof(uint8_t) * frames);
for(int i = 0; i < frames; ++i) {
FURI_CONST_ASSIGN(animation->frame_order[i], u32array[i]);
}
/* passive and active frames must be loaded up to this point */
if(!animation_storage_load_frames(storage, name, animation, u32array, width, height))
break;
if(!flipper_format_read_uint32(ff, "Active cycles", &u32value, 1)) break;
animation->active_cycles = u32value;
if(!flipper_format_read_uint32(ff, "Frame rate", &u32value, 1)) break;
FURI_CONST_ASSIGN(animation->icon_animation.frame_rate, u32value);
if(!flipper_format_read_uint32(ff, "Duration", &u32value, 1)) break;
animation->duration = u32value;
if(!flipper_format_read_uint32(ff, "Active cooldown", &u32value, 1)) break;
animation->active_cooldown = u32value;
if(!animation_storage_load_bubbles(animation, ff)) break;
success = true;
} while(0);
string_clear(str);
flipper_format_free(ff);
if(u32array) {
free(u32array);
}
if(!success) {
if(animation->frame_order) {
free((void*)animation->frame_order);
}
free(animation);
animation = NULL;
}
return animation;
}
static void animation_storage_free_bubbles(BubbleAnimation* animation) {
if(!animation->frame_bubble_sequences) return;
for(int i = 0; i < animation->frame_bubble_sequences_count;) {
const FrameBubble* const* bubble = &animation->frame_bubble_sequences[i];
if((*bubble) == NULL) break;
while((*bubble)->next_bubble != NULL) {
bubble = &(*bubble)->next_bubble;
}
if((*bubble)->bubble.text) {
free((void*)(*bubble)->bubble.text);
}
if((*bubble) == animation->frame_bubble_sequences[i]) {
++i;
}
free((void*)*bubble);
FURI_CONST_ASSIGN_PTR(*bubble, NULL);
}
free((void*)animation->frame_bubble_sequences);
animation->frame_bubble_sequences = NULL;
}

View File

@@ -0,0 +1,89 @@
#pragma once
#include <stdint.h>
#include <m-list.h>
#include "views/bubble_animation_view.h"
#include <m-string.h>
/** Main structure to handle animation data.
* Contains all, including animation playing data (BubbleAnimation),
* data for random animation selection (StorageAnimationMeta) and
* flag of location internal/external */
typedef struct StorageAnimation StorageAnimation;
typedef struct {
const char* name;
uint8_t min_butthurt;
uint8_t max_butthurt;
uint8_t min_level;
uint8_t max_level;
uint8_t weight;
} StorageAnimationManifestInfo;
/** Container to return available animations list */
LIST_DEF(StorageAnimationList, StorageAnimation*, M_PTR_OPLIST)
#define M_OPL_StorageAnimationList_t() LIST_OPLIST(StorageAnimationList)
/**
* Fill list of available animations.
* List will contain all idle animations on inner flash
* and all available on SD-card, mentioned in manifest.txt.
* Performs caching of animation. If fail - falls back to
* inner animation.
* List has to be initialized.
*
* @list list to fill with animations data
*/
void animation_storage_fill_animation_list(StorageAnimationList_t* list);
/**
* Get bubble animation of storage animation.
* Bubble Animation is a structure which describes animation
* independent of it's place of storage and meta data.
* It contain all what is need to be played.
* If storage_animation is not cached - caches it.
*
* @storage_animation animation from which extract bubble animation
* @return bubble_animation, NULL if failed to cache data.
*/
const BubbleAnimation* animation_storage_get_bubble_animation(StorageAnimation* storage_animation);
/**
* Performs caching animation data (Bubble Animation)
* if this is not done yet.
*
* @storage_animation animation to cache
*/
void animation_storage_cache_animation(StorageAnimation* storage_animation);
/**
* Find animation by name.
* Search through the inner flash, and SD-card if has.
*
* @name name of animation
* @return found animation. NULL if nothing found.
*/
StorageAnimation* animation_storage_find_animation(const char* name);
/**
* Get meta information of storage animation.
* This information allows to randomly select animation.
* Also it contains name. Never returns NULL.
*
* @storage_animation item of whom we have to extract meta.
* @return meta itself
*/
StorageAnimationManifestInfo* animation_storage_get_meta(StorageAnimation* storage_animation);
/**
* Free storage_animation, which previously acquired
* by Animation Storage.
*
* @storage_animation item to free. NULL-ed after all.
*/
void animation_storage_free_storage_animation(StorageAnimation** storage_animation);
/**
* Has to be called at least 1 time to initialize runtime structures
* of animations in inner flash.
*/
void animation_storage_initialize_internal_animations(void);

View File

@@ -0,0 +1,9 @@
#pragma once
#include "animation_storage.h"
#include "animation_manager.h"
struct StorageAnimation {
const BubbleAnimation* animation;
bool external;
StorageAnimationManifestInfo manifest_info;
};

View File

@@ -0,0 +1,409 @@
#include "../animation_manager.h"
#include "../animation_storage.h"
#include "bubble_animation_view.h"
#include <furi_hal.h>
#include <furi.h>
#include <gui/canvas.h>
#include <gui/elements.h>
#include <gui/view.h>
#include <gui/icon_i.h>
#include <input/input.h>
#include <stdint.h>
#include <core/dangerous_defines.h>
#define ACTIVE_SHIFT 2
typedef struct {
const BubbleAnimation* current;
const FrameBubble* current_bubble;
uint8_t current_frame;
uint8_t active_cycle;
uint8_t active_bubbles;
uint8_t passive_bubbles;
uint8_t active_shift;
TickType_t active_ended_at;
Icon* freeze_frame;
} BubbleAnimationViewModel;
struct BubbleAnimationView {
View* view;
FuriTimer* timer;
BubbleAnimationInteractCallback interact_callback;
void* interact_callback_context;
};
static void bubble_animation_activate(BubbleAnimationView* view, bool force);
static void bubble_animation_activate_right_now(BubbleAnimationView* view);
static uint8_t bubble_animation_get_frame_index(BubbleAnimationViewModel* model) {
furi_assert(model);
uint8_t icon_index = 0;
const BubbleAnimation* animation = model->current;
if(model->current_frame < animation->passive_frames) {
icon_index = model->current_frame;
} else {
icon_index =
(model->current_frame - animation->passive_frames) % animation->active_frames +
animation->passive_frames;
}
furi_assert(icon_index < (animation->passive_frames + animation->active_frames));
return animation->frame_order[icon_index];
}
static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
furi_assert(model_);
furi_assert(canvas);
BubbleAnimationViewModel* model = model_;
const BubbleAnimation* animation = model->current;
if(model->freeze_frame) {
uint8_t y_offset = canvas_height(canvas) - icon_get_height(model->freeze_frame);
canvas_draw_icon(canvas, 0, y_offset, model->freeze_frame);
return;
}
if(!animation) {
return;
}
furi_assert(model->current_frame < 255);
uint8_t index = bubble_animation_get_frame_index(model);
uint8_t width = icon_get_width(&animation->icon_animation);
uint8_t height = icon_get_height(&animation->icon_animation);
uint8_t y_offset = canvas_height(canvas) - height;
canvas_draw_bitmap(
canvas, 0, y_offset, width, height, animation->icon_animation.frames[index]);
const FrameBubble* bubble = model->current_bubble;
if(bubble) {
if((model->current_frame >= bubble->start_frame) &&
(model->current_frame <= bubble->end_frame)) {
const Bubble* b = &bubble->bubble;
elements_bubble_str(canvas, b->x, b->y, b->text, b->align_h, b->align_v);
}
}
}
static const FrameBubble*
bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
const FrameBubble* bubble = NULL;
if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
return NULL;
}
uint8_t index =
furi_hal_random_get() % (active ? model->active_bubbles : model->passive_bubbles);
const BubbleAnimation* animation = model->current;
for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
if((animation->frame_bubble_sequences[i]->start_frame < animation->passive_frames) ^
active) {
if(!index) {
bubble = animation->frame_bubble_sequences[i];
}
--index;
}
}
return bubble;
}
static bool bubble_animation_input_callback(InputEvent* event, void* context) {
furi_assert(context);
furi_assert(event);
BubbleAnimationView* animation_view = context;
bool consumed = false;
if(event->type == InputTypePress) {
bubble_animation_activate(animation_view, false);
}
if(event->key == InputKeyRight) {
/* Right button reserved for animation activation, so consume */
consumed = true;
if(event->type == InputTypeShort) {
if(animation_view->interact_callback) {
animation_view->interact_callback(animation_view->interact_callback_context);
}
}
}
return consumed;
}
static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
furi_assert(view);
bool activate = true;
BubbleAnimationViewModel* model = view_get_model(view->view);
if(model->current == NULL) {
activate = false;
} else if(model->freeze_frame) {
activate = false;
} else if(model->current->active_frames == 0) {
activate = false;
}
if(model->current != NULL) {
if(!force) {
if((model->active_ended_at + model->current->active_cooldown * 1000) >
xTaskGetTickCount()) {
activate = false;
} else if(model->active_shift) {
activate = false;
} else if(model->current_frame >= model->current->passive_frames) {
activate = false;
}
}
}
view_commit_model(view->view, false);
if(!activate && !force) {
return;
}
if(ACTIVE_SHIFT > 0) {
BubbleAnimationViewModel* model = view_get_model(view->view);
model->active_shift = ACTIVE_SHIFT;
view_commit_model(view->view, false);
} else {
bubble_animation_activate_right_now(view);
}
}
static void bubble_animation_activate_right_now(BubbleAnimationView* view) {
furi_assert(view);
uint8_t frame_rate = 0;
BubbleAnimationViewModel* model = view_get_model(view->view);
if(model->current && (model->current->active_frames > 0) && (!model->freeze_frame)) {
model->current_frame = model->current->passive_frames;
model->current_bubble = bubble_animation_pick_bubble(model, true);
frame_rate = model->current->icon_animation.frame_rate;
}
view_commit_model(view->view, true);
if(frame_rate) {
furi_timer_start(view->timer, 1000 / frame_rate);
}
}
static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
furi_assert(model);
if(!model->current) {
return;
}
if(model->current_frame < model->current->passive_frames) {
model->current_frame = (model->current_frame + 1) % model->current->passive_frames;
} else {
++model->current_frame;
model->active_cycle +=
!((model->current_frame - model->current->passive_frames) %
model->current->active_frames);
if(model->active_cycle >= model->current->active_cycles) {
// switch to passive
model->active_cycle = 0;
model->current_frame = 0;
model->current_bubble = bubble_animation_pick_bubble(model, false);
model->active_ended_at = xTaskGetTickCount();
}
if(model->current_bubble) {
if(model->current_frame > model->current_bubble->end_frame) {
model->current_bubble = model->current_bubble->next_bubble;
}
}
}
}
static void bubble_animation_timer_callback(void* context) {
furi_assert(context);
BubbleAnimationView* view = context;
bool activate = false;
BubbleAnimationViewModel* model = view_get_model(view->view);
if(model->active_shift > 0) {
activate = (--model->active_shift == 0);
}
if(!model->freeze_frame && !activate) {
bubble_animation_next_frame(model);
}
view_commit_model(view->view, !activate);
if(activate) {
bubble_animation_activate_right_now(view);
}
}
/* always freeze first passive frame, because
* animation is always activated at unfreezing and played
* passive frame first, and 2 frames after - active
*/
static Icon* bubble_animation_clone_first_frame(const Icon* icon_orig) {
furi_assert(icon_orig);
furi_assert(icon_orig->frames);
furi_assert(icon_orig->frames[0]);
Icon* icon_clone = malloc(sizeof(Icon));
memcpy(icon_clone, icon_orig, sizeof(Icon));
icon_clone->frames = malloc(sizeof(uint8_t*));
/* icon bitmap can be either compressed or not. It is compressed if
* compressed size is less than original, so max size for bitmap is
* uncompressed (width * height) + 1 byte (in uncompressed case)
* for compressed header
*/
size_t max_bitmap_size = ROUND_UP_TO(icon_orig->width, 8) * icon_orig->height + 1;
FURI_CONST_ASSIGN_PTR(icon_clone->frames[0], malloc(max_bitmap_size));
memcpy((void*)icon_clone->frames[0], icon_orig->frames[0], max_bitmap_size);
FURI_CONST_ASSIGN(icon_clone->frame_count, 1);
return icon_clone;
}
static void bubble_animation_release_frame(Icon** icon) {
furi_assert(icon);
furi_assert(*icon);
free((void*)(*icon)->frames[0]);
free((void*)(*icon)->frames);
free(*icon);
*icon = NULL;
}
static void bubble_animation_enter(void* context) {
furi_assert(context);
BubbleAnimationView* view = context;
bubble_animation_activate(view, false);
BubbleAnimationViewModel* model = view_get_model(view->view);
uint8_t frame_rate = 0;
if(model->current != NULL) {
frame_rate = model->current->icon_animation.frame_rate;
}
view_commit_model(view->view, false);
if(frame_rate) {
furi_timer_start(view->timer, 1000 / frame_rate);
}
}
static void bubble_animation_exit(void* context) {
furi_assert(context);
BubbleAnimationView* view = context;
furi_timer_stop(view->timer);
}
BubbleAnimationView* bubble_animation_view_alloc(void) {
BubbleAnimationView* view = malloc(sizeof(BubbleAnimationView));
view->view = view_alloc();
view->interact_callback = NULL;
view->timer = furi_timer_alloc(bubble_animation_timer_callback, FuriTimerTypePeriodic, view);
view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
view_set_context(view->view, view);
view_set_draw_callback(view->view, bubble_animation_draw_callback);
view_set_input_callback(view->view, bubble_animation_input_callback);
view_set_enter_callback(view->view, bubble_animation_enter);
view_set_exit_callback(view->view, bubble_animation_exit);
return view;
}
void bubble_animation_view_free(BubbleAnimationView* view) {
furi_assert(view);
view_set_draw_callback(view->view, NULL);
view_set_input_callback(view->view, NULL);
view_set_context(view->view, NULL);
view_free(view->view);
view->view = NULL;
free(view);
}
void bubble_animation_view_set_interact_callback(
BubbleAnimationView* view,
BubbleAnimationInteractCallback callback,
void* context) {
furi_assert(view);
view->interact_callback_context = context;
view->interact_callback = callback;
}
void bubble_animation_view_set_animation(
BubbleAnimationView* view,
const BubbleAnimation* new_animation) {
furi_assert(view);
furi_assert(new_animation);
BubbleAnimationViewModel* model = view_get_model(view->view);
furi_assert(model);
model->current = new_animation;
model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
model->active_bubbles = 0;
model->passive_bubbles = 0;
for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
if(new_animation->frame_bubble_sequences[i]->start_frame < new_animation->passive_frames) {
++model->passive_bubbles;
} else {
++model->active_bubbles;
}
}
/* select bubble sequence */
model->current_bubble = bubble_animation_pick_bubble(model, false);
model->current_frame = 0;
model->active_cycle = 0;
view_commit_model(view->view, true);
furi_timer_start(view->timer, 1000 / new_animation->icon_animation.frame_rate);
}
void bubble_animation_freeze(BubbleAnimationView* view) {
furi_assert(view);
BubbleAnimationViewModel* model = view_get_model(view->view);
furi_assert(model->current);
furi_assert(!model->freeze_frame);
model->freeze_frame = bubble_animation_clone_first_frame(&model->current->icon_animation);
model->current = NULL;
view_commit_model(view->view, false);
furi_timer_stop(view->timer);
}
void bubble_animation_unfreeze(BubbleAnimationView* view) {
furi_assert(view);
uint8_t frame_rate;
BubbleAnimationViewModel* model = view_get_model(view->view);
furi_assert(model->freeze_frame);
bubble_animation_release_frame(&model->freeze_frame);
furi_assert(model->current);
frame_rate = model->current->icon_animation.frame_rate;
view_commit_model(view->view, true);
furi_timer_start(view->timer, 1000 / frame_rate);
bubble_animation_activate(view, false);
}
View* bubble_animation_get_view(BubbleAnimationView* view) {
furi_assert(view);
return view->view;
}

View File

@@ -0,0 +1,89 @@
#pragma once
#include <gui/view.h>
#include "../animation_manager.h"
/** Bubble Animation instance */
typedef struct BubbleAnimationView BubbleAnimationView;
/** Callback type to be called when interact button pressed */
typedef void (*BubbleAnimationInteractCallback)(void*);
/**
* Allocate bubble animation view.
* This is animation with bubbles, and 2 phases:
* active and passive.
*
* @return instance of new bubble animation
*/
BubbleAnimationView* bubble_animation_view_alloc(void);
/**
* Free bubble animation view.
*
* @view bubble animation view instance
*/
void bubble_animation_view_free(BubbleAnimationView* view);
/**
* Set callback for interact action for animation.
* Currently this is right button.
*
* @view bubble animation view instance
* @callback callback to call when button pressed
* @context context
*/
void bubble_animation_view_set_interact_callback(
BubbleAnimationView* view,
BubbleAnimationInteractCallback callback,
void* context);
/**
* Set new animation.
* BubbleAnimation doesn't posses Bubble Animation object
* so it doesn't handle any memory manipulation on Bubble Animations.
*
* @view bubble animation view instance
* @new_bubble_animation new animation to set
*/
void bubble_animation_view_set_animation(
BubbleAnimationView* view,
const BubbleAnimation* new_bubble_animation);
/**
* Get view of bubble animation.
*
* @view bubble animation view instance
* @return view
*/
View* bubble_animation_get_view(BubbleAnimationView* view);
/**
* Freeze current playing animation. Saves a frame to be shown
* during next unfreeze called.
* bubble_animation_freeze() stops any reference to 'current' animation
* so it can be freed. Therefore lock unfreeze should be preceeded with
* new animation set.
*
* Freeze/Unfreeze usage example:
*
* animation_view_alloc()
* set_animation()
* ...
* freeze_animation()
* // release animation
* ...
* // allocate animation
* set_animation()
* unfreeze()
*
* @view bubble animation view instance
*/
void bubble_animation_freeze(BubbleAnimationView* view);
/**
* Starts bubble animation after freezing.
*
* @view bubble animation view instance
*/
void bubble_animation_unfreeze(BubbleAnimationView* view);

View File

@@ -0,0 +1,130 @@
#include "one_shot_animation_view.h"
#include <furi.h>
#include <portmacro.h>
#include <gui/canvas.h>
#include <gui/view.h>
#include <gui/icon_i.h>
#include <stdint.h>
typedef void (*OneShotInteractCallback)(void*);
struct OneShotView {
View* view;
TimerHandle_t update_timer;
OneShotInteractCallback interact_callback;
void* interact_callback_context;
};
typedef struct {
const Icon* icon;
uint32_t index;
bool block_input;
} OneShotViewModel;
static void one_shot_view_update_timer_callback(TimerHandle_t xTimer) {
OneShotView* view = (void*)pvTimerGetTimerID(xTimer);
OneShotViewModel* model = view_get_model(view->view);
if((model->index + 1) < model->icon->frame_count) {
++model->index;
} else {
model->block_input = false;
model->index = model->icon->frame_count - 2;
}
view_commit_model(view->view, true);
}
static void one_shot_view_draw(Canvas* canvas, void* model_) {
furi_assert(canvas);
furi_assert(model_);
OneShotViewModel* model = model_;
furi_check(model->index < model->icon->frame_count);
uint8_t y_offset = canvas_height(canvas) - model->icon->height;
canvas_draw_bitmap(
canvas,
0,
y_offset,
model->icon->width,
model->icon->height,
model->icon->frames[model->index]);
}
static bool one_shot_view_input(InputEvent* event, void* context) {
furi_assert(context);
furi_assert(event);
OneShotView* view = context;
bool consumed = false;
OneShotViewModel* model = view_get_model(view->view);
consumed = model->block_input;
view_commit_model(view->view, false);
if(!consumed) {
if(event->key == InputKeyRight) {
/* Right button reserved for animation activation, so consume */
consumed = true;
if(event->type == InputTypeShort) {
if(view->interact_callback) {
view->interact_callback(view->interact_callback_context);
}
}
}
}
return consumed;
}
OneShotView* one_shot_view_alloc(void) {
OneShotView* view = malloc(sizeof(OneShotView));
view->view = view_alloc();
view->update_timer =
xTimerCreate(NULL, 1000, pdTRUE, view, one_shot_view_update_timer_callback);
view_allocate_model(view->view, ViewModelTypeLocking, sizeof(OneShotViewModel));
view_set_context(view->view, view);
view_set_draw_callback(view->view, one_shot_view_draw);
view_set_input_callback(view->view, one_shot_view_input);
return view;
}
void one_shot_view_free(OneShotView* view) {
furi_assert(view);
xTimerDelete(view->update_timer, portMAX_DELAY);
view_free(view->view);
view->view = NULL;
free(view);
}
void one_shot_view_set_interact_callback(
OneShotView* view,
OneShotInteractCallback callback,
void* context) {
furi_assert(view);
view->interact_callback_context = context;
view->interact_callback = callback;
}
void one_shot_view_start_animation(OneShotView* view, const Icon* icon) {
furi_assert(view);
furi_assert(icon);
furi_check(icon->frame_count >= 2);
OneShotViewModel* model = view_get_model(view->view);
model->index = 0;
model->icon = icon;
model->block_input = true;
view_commit_model(view->view, true);
xTimerChangePeriod(view->update_timer, 1000 / model->icon->frame_rate, portMAX_DELAY);
}
View* one_shot_view_get_view(OneShotView* view) {
furi_assert(view);
return view->view;
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include <furi.h>
#include <gui/view.h>
#include <stdint.h>
typedef void (*OneShotInteractCallback)(void*);
typedef struct OneShotView OneShotView;
OneShotView* one_shot_view_alloc(void);
void one_shot_view_free(OneShotView* view);
void one_shot_view_set_interact_callback(
OneShotView* view,
OneShotInteractCallback callback,
void* context);
void one_shot_view_start_animation(OneShotView* view, const Icon* icon);
View* one_shot_view_get_view(OneShotView* view);

View File

@@ -0,0 +1,17 @@
App(
appid="desktop",
name="DesktopSrv",
apptype=FlipperAppType.SERVICE,
entry_point="desktop_srv",
cdefines=["SRV_DESKTOP"],
requires=[
"gui",
"dolphin",
"storage",
"input",
],
provides=["desktop_settings"],
conflicts=["updater"],
stack_size=2 * 1024,
order=60,
)

View File

@@ -0,0 +1,338 @@
#include <storage/storage.h>
#include <assets_icons.h>
#include <gui/gui.h>
#include <gui/view_stack.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include <furi.h>
#include <furi_hal.h>
#include "animations/animation_manager.h"
#include "desktop/scenes/desktop_scene.h"
#include "desktop/scenes/desktop_scene_i.h"
#include "desktop/views/desktop_view_locked.h"
#include "desktop/views/desktop_view_pin_input.h"
#include "desktop/views/desktop_view_pin_timeout.h"
#include "desktop_i.h"
#include "helpers/pin_lock.h"
#include "helpers/slideshow_filename.h"
static void desktop_auto_lock_arm(Desktop*);
static void desktop_auto_lock_inhibit(Desktop*);
static void desktop_start_auto_lock_timer(Desktop*);
static void desktop_loader_callback(const void* message, void* context) {
furi_assert(context);
Desktop* desktop = context;
const LoaderEvent* event = message;
if(event->type == LoaderEventTypeApplicationStarted) {
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalBeforeAppStarted);
} else if(event->type == LoaderEventTypeApplicationStopped) {
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAfterAppFinished);
}
}
static void desktop_lock_icon_callback(Canvas* canvas, void* context) {
UNUSED(context);
furi_assert(canvas);
canvas_draw_icon(canvas, 0, 0, &I_Lock_8x8);
}
static bool desktop_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Desktop* desktop = (Desktop*)context;
switch(event) {
case DesktopGlobalBeforeAppStarted:
animation_manager_unload_and_stall_animation(desktop->animation_manager);
desktop_auto_lock_inhibit(desktop);
return true;
case DesktopGlobalAfterAppFinished:
animation_manager_load_and_continue_animation(desktop->animation_manager);
// TODO: Implement a message mechanism for loading settings and (optionally)
// locking and unlocking
LOAD_DESKTOP_SETTINGS(&desktop->settings);
desktop_auto_lock_arm(desktop);
return true;
case DesktopGlobalAutoLock:
if(!loader_is_locked(desktop->loader)) {
desktop_lock(desktop);
}
return true;
}
return scene_manager_handle_custom_event(desktop->scene_manager, event);
}
static bool desktop_back_event_callback(void* context) {
furi_assert(context);
Desktop* desktop = (Desktop*)context;
return scene_manager_handle_back_event(desktop->scene_manager);
}
static void desktop_tick_event_callback(void* context) {
furi_assert(context);
Desktop* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
static void desktop_input_event_callback(const void* value, void* context) {
furi_assert(value);
furi_assert(context);
const InputEvent* event = value;
Desktop* desktop = context;
if(event->type == InputTypePress) {
desktop_start_auto_lock_timer(desktop);
}
}
static void desktop_auto_lock_timer_callback(void* context) {
furi_assert(context);
Desktop* desktop = context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAutoLock);
}
static void desktop_start_auto_lock_timer(Desktop* desktop) {
furi_timer_start(
desktop->auto_lock_timer, furi_ms_to_ticks(desktop->settings.auto_lock_delay_ms));
}
static void desktop_stop_auto_lock_timer(Desktop* desktop) {
furi_timer_stop(desktop->auto_lock_timer);
}
static void desktop_auto_lock_arm(Desktop* desktop) {
if(desktop->settings.auto_lock_delay_ms) {
desktop->input_events_subscription = furi_pubsub_subscribe(
desktop->input_events_pubsub, desktop_input_event_callback, desktop);
desktop_start_auto_lock_timer(desktop);
}
}
static void desktop_auto_lock_inhibit(Desktop* desktop) {
desktop_stop_auto_lock_timer(desktop);
if(desktop->input_events_subscription) {
furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription);
desktop->input_events_subscription = NULL;
}
}
void desktop_lock(Desktop* desktop) {
desktop_auto_lock_inhibit(desktop);
scene_manager_set_scene_state(
desktop->scene_manager, DesktopSceneLocked, SCENE_LOCKED_FIRST_ENTER);
scene_manager_next_scene(desktop->scene_manager, DesktopSceneLocked);
notification_message(desktop->notification, &sequence_display_backlight_off_delay_1000);
}
void desktop_unlock(Desktop* desktop) {
view_port_enabled_set(desktop->lock_viewport, false);
Gui* gui = furi_record_open(RECORD_GUI);
gui_set_lockdown(gui, false);
furi_record_close(RECORD_GUI);
desktop_view_locked_unlock(desktop->locked_view);
scene_manager_search_and_switch_to_previous_scene(desktop->scene_manager, DesktopSceneMain);
desktop_auto_lock_arm(desktop);
}
Desktop* desktop_alloc() {
Desktop* desktop = malloc(sizeof(Desktop));
desktop->animation_manager = animation_manager_alloc();
desktop->gui = furi_record_open(RECORD_GUI);
desktop->scene_thread = furi_thread_alloc();
desktop->view_dispatcher = view_dispatcher_alloc();
desktop->scene_manager = scene_manager_alloc(&desktop_scene_handlers, desktop);
view_dispatcher_enable_queue(desktop->view_dispatcher);
view_dispatcher_attach_to_gui(
desktop->view_dispatcher, desktop->gui, ViewDispatcherTypeDesktop);
view_dispatcher_set_tick_event_callback(
desktop->view_dispatcher, desktop_tick_event_callback, 500);
view_dispatcher_set_event_callback_context(desktop->view_dispatcher, desktop);
view_dispatcher_set_custom_event_callback(
desktop->view_dispatcher, desktop_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
desktop->view_dispatcher, desktop_back_event_callback);
desktop->lock_menu = desktop_lock_menu_alloc();
desktop->debug_view = desktop_debug_alloc();
desktop->hw_mismatch_popup = popup_alloc();
desktop->locked_view = desktop_view_locked_alloc();
desktop->pin_input_view = desktop_view_pin_input_alloc();
desktop->pin_timeout_view = desktop_view_pin_timeout_alloc();
desktop->slideshow_view = desktop_view_slideshow_alloc();
desktop->main_view_stack = view_stack_alloc();
desktop->main_view = desktop_main_alloc();
View* dolphin_view = animation_manager_get_animation_view(desktop->animation_manager);
view_stack_add_view(desktop->main_view_stack, desktop_main_get_view(desktop->main_view));
view_stack_add_view(desktop->main_view_stack, dolphin_view);
view_stack_add_view(
desktop->main_view_stack, desktop_view_locked_get_view(desktop->locked_view));
/* locked view (as animation view) attends in 2 scenes: main & locked,
* because it has to draw "Unlocked" label on main scene */
desktop->locked_view_stack = view_stack_alloc();
view_stack_add_view(desktop->locked_view_stack, dolphin_view);
view_stack_add_view(
desktop->locked_view_stack, desktop_view_locked_get_view(desktop->locked_view));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdMain,
view_stack_get_view(desktop->main_view_stack));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdLocked,
view_stack_get_view(desktop->locked_view_stack));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdLockMenu,
desktop_lock_menu_get_view(desktop->lock_menu));
view_dispatcher_add_view(
desktop->view_dispatcher, DesktopViewIdDebug, desktop_debug_get_view(desktop->debug_view));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdHwMismatch,
popup_get_view(desktop->hw_mismatch_popup));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdPinTimeout,
desktop_view_pin_timeout_get_view(desktop->pin_timeout_view));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdPinInput,
desktop_view_pin_input_get_view(desktop->pin_input_view));
view_dispatcher_add_view(
desktop->view_dispatcher,
DesktopViewIdSlideshow,
desktop_view_slideshow_get_view(desktop->slideshow_view));
// Lock icon
desktop->lock_viewport = view_port_alloc();
view_port_set_width(desktop->lock_viewport, icon_get_width(&I_Lock_8x8));
view_port_draw_callback_set(desktop->lock_viewport, desktop_lock_icon_callback, desktop);
view_port_enabled_set(desktop->lock_viewport, false);
gui_add_view_port(desktop->gui, desktop->lock_viewport, GuiLayerStatusBarLeft);
// Special case: autostart application is already running
desktop->loader = furi_record_open(RECORD_LOADER);
if(loader_is_locked(desktop->loader) &&
animation_manager_is_animation_loaded(desktop->animation_manager)) {
animation_manager_unload_and_stall_animation(desktop->animation_manager);
}
desktop->notification = furi_record_open(RECORD_NOTIFICATION);
desktop->app_start_stop_subscription = furi_pubsub_subscribe(
loader_get_pubsub(desktop->loader), desktop_loader_callback, desktop);
desktop->input_events_pubsub = furi_record_open(RECORD_INPUT_EVENTS);
desktop->input_events_subscription = NULL;
desktop->auto_lock_timer =
furi_timer_alloc(desktop_auto_lock_timer_callback, FuriTimerTypeOnce, desktop);
return desktop;
}
void desktop_free(Desktop* desktop) {
furi_assert(desktop);
furi_pubsub_unsubscribe(
loader_get_pubsub(desktop->loader), desktop->app_start_stop_subscription);
if(desktop->input_events_subscription) {
furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription);
desktop->input_events_subscription = NULL;
}
desktop->loader = NULL;
desktop->input_events_pubsub = NULL;
furi_record_close(RECORD_LOADER);
furi_record_close(RECORD_NOTIFICATION);
furi_record_close(RECORD_INPUT_EVENTS);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdMain);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdLockMenu);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdLocked);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdDebug);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdHwMismatch);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdPinInput);
view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdPinTimeout);
view_dispatcher_free(desktop->view_dispatcher);
scene_manager_free(desktop->scene_manager);
animation_manager_free(desktop->animation_manager);
view_stack_free(desktop->main_view_stack);
desktop_main_free(desktop->main_view);
view_stack_free(desktop->locked_view_stack);
desktop_view_locked_free(desktop->locked_view);
desktop_lock_menu_free(desktop->lock_menu);
desktop_view_locked_free(desktop->locked_view);
desktop_debug_free(desktop->debug_view);
popup_free(desktop->hw_mismatch_popup);
desktop_view_pin_timeout_free(desktop->pin_timeout_view);
furi_record_close(RECORD_GUI);
desktop->gui = NULL;
furi_thread_free(desktop->scene_thread);
furi_record_close("menu");
furi_timer_free(desktop->auto_lock_timer);
free(desktop);
}
static bool desktop_check_file_flag(const char* flag_path) {
Storage* storage = furi_record_open(RECORD_STORAGE);
bool exists = storage_common_stat(storage, flag_path, NULL) == FSE_OK;
furi_record_close(RECORD_STORAGE);
return exists;
}
int32_t desktop_srv(void* p) {
UNUSED(p);
Desktop* desktop = desktop_alloc();
bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
if(!loaded) {
memset(&desktop->settings, 0, sizeof(desktop->settings));
SAVE_DESKTOP_SETTINGS(&desktop->settings);
}
scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
desktop_pin_lock_init(&desktop->settings);
if(!desktop_pin_lock_is_locked()) {
if(!loader_is_locked(desktop->loader)) {
desktop_auto_lock_arm(desktop);
}
} else {
desktop_lock(desktop);
}
if(desktop_check_file_flag(SLIDESHOW_FS_PATH)) {
scene_manager_next_scene(desktop->scene_manager, DesktopSceneSlideshow);
}
if(!furi_hal_version_do_i_belong_here()) {
scene_manager_next_scene(desktop->scene_manager, DesktopSceneHwMismatch);
}
if(furi_hal_rtc_get_fault_data()) {
scene_manager_next_scene(desktop->scene_manager, DesktopSceneFault);
}
view_dispatcher_run(desktop->view_dispatcher);
desktop_free(desktop);
return 0;
}

View File

@@ -0,0 +1,3 @@
#pragma once
typedef struct Desktop Desktop;

View File

@@ -0,0 +1,77 @@
#pragma once
#include "desktop.h"
#include "animations/animation_manager.h"
#include "views/desktop_view_pin_timeout.h"
#include "views/desktop_view_pin_input.h"
#include "views/desktop_view_locked.h"
#include "views/desktop_view_main.h"
#include "views/desktop_view_lock_menu.h"
#include "views/desktop_view_debug.h"
#include "views/desktop_view_slideshow.h"
#include <desktop/desktop_settings.h>
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_stack.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/popup.h>
#include <gui/scene_manager.h>
#include <loader/loader.h>
#include <notification/notification_app.h>
#define STATUS_BAR_Y_SHIFT 13
typedef enum {
DesktopViewIdMain,
DesktopViewIdLockMenu,
DesktopViewIdLocked,
DesktopViewIdDebug,
DesktopViewIdHwMismatch,
DesktopViewIdPinInput,
DesktopViewIdPinTimeout,
DesktopViewIdSlideshow,
DesktopViewIdTotal,
} DesktopViewId;
struct Desktop {
// Scene
FuriThread* scene_thread;
// GUI
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
Popup* hw_mismatch_popup;
DesktopLockMenuView* lock_menu;
DesktopDebugView* debug_view;
DesktopViewLocked* locked_view;
DesktopMainView* main_view;
DesktopViewPinTimeout* pin_timeout_view;
DesktopSlideshowView* slideshow_view;
ViewStack* main_view_stack;
ViewStack* locked_view_stack;
DesktopSettings settings;
DesktopViewPinInput* pin_input_view;
ViewPort* lock_viewport;
AnimationManager* animation_manager;
Loader* loader;
NotificationApp* notification;
FuriPubSubSubscription* app_start_stop_subscription;
FuriPubSub* input_events_pubsub;
FuriPubSubSubscription* input_events_subscription;
FuriTimer* auto_lock_timer;
};
Desktop* desktop_alloc();
void desktop_free(Desktop* desktop);
void desktop_lock(Desktop* desktop);
void desktop_unlock(Desktop* desktop);

View File

@@ -0,0 +1,49 @@
#pragma once
#include "desktop_settings_filename.h"
#include <furi_hal.h>
#include <stdint.h>
#include <stdbool.h>
#include <toolbox/saved_struct.h>
#include <storage/storage.h>
#define DESKTOP_SETTINGS_VER (4)
#define DESKTOP_SETTINGS_PATH INT_PATH(DESKTOP_SETTINGS_FILE_NAME)
#define DESKTOP_SETTINGS_MAGIC (0x17)
#define PIN_MAX_LENGTH 12
#define DESKTOP_SETTINGS_RUN_PIN_SETUP_ARG "run_pin_setup"
#define SAVE_DESKTOP_SETTINGS(x) \
saved_struct_save( \
DESKTOP_SETTINGS_PATH, \
(x), \
sizeof(DesktopSettings), \
DESKTOP_SETTINGS_MAGIC, \
DESKTOP_SETTINGS_VER)
#define LOAD_DESKTOP_SETTINGS(x) \
saved_struct_load( \
DESKTOP_SETTINGS_PATH, \
(x), \
sizeof(DesktopSettings), \
DESKTOP_SETTINGS_MAGIC, \
DESKTOP_SETTINGS_VER)
#define MAX_PIN_SIZE 10
#define MIN_PIN_SIZE 4
typedef struct {
InputKey data[MAX_PIN_SIZE];
uint8_t length;
} PinCode;
typedef struct {
uint16_t favorite_primary;
uint16_t favorite_secondary;
PinCode pin_code;
uint8_t is_locked;
uint32_t auto_lock_delay_ms;
} DesktopSettings;

View File

@@ -0,0 +1,3 @@
#pragma once
#define DESKTOP_SETTINGS_FILE_NAME ".desktop.settings"

View File

@@ -0,0 +1,140 @@
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include <stddef.h>
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include "../helpers/pin_lock.h"
#include "../desktop_i.h"
#include <cli/cli.h>
#include <cli/cli_vcp.h>
static const NotificationSequence sequence_pin_fail = {
&message_display_backlight_on,
&message_red_255,
&message_vibro_on,
&message_delay_100,
&message_vibro_off,
&message_red_0,
&message_delay_250,
&message_red_255,
&message_vibro_on,
&message_delay_100,
&message_vibro_off,
&message_red_0,
NULL,
};
static const uint8_t desktop_helpers_fails_timeout[] = {
0,
0,
0,
0,
30,
60,
90,
120,
150,
180,
/* +60 for every next fail */
};
void desktop_pin_lock_error_notify() {
NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
notification_message(notification, &sequence_pin_fail);
furi_record_close(RECORD_NOTIFICATION);
}
uint32_t desktop_pin_lock_get_fail_timeout() {
uint32_t pin_fails = furi_hal_rtc_get_pin_fails();
uint32_t pin_timeout = 0;
uint32_t max_index = COUNT_OF(desktop_helpers_fails_timeout) - 1;
if(pin_fails <= max_index) {
pin_timeout = desktop_helpers_fails_timeout[pin_fails];
} else {
pin_timeout = desktop_helpers_fails_timeout[max_index] + (pin_fails - max_index) * 60;
}
return pin_timeout;
}
void desktop_pin_lock(DesktopSettings* settings) {
furi_assert(settings);
furi_hal_rtc_set_pin_fails(0);
furi_hal_rtc_set_flag(FuriHalRtcFlagLock);
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_close(cli);
furi_record_close(RECORD_CLI);
settings->is_locked = 1;
SAVE_DESKTOP_SETTINGS(settings);
}
void desktop_pin_unlock(DesktopSettings* settings) {
furi_assert(settings);
furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_open(cli, &cli_vcp);
furi_record_close(RECORD_CLI);
settings->is_locked = 0;
SAVE_DESKTOP_SETTINGS(settings);
}
void desktop_pin_lock_init(DesktopSettings* settings) {
furi_assert(settings);
if(settings->pin_code.length > 0) {
if(settings->is_locked == 1) {
furi_hal_rtc_set_flag(FuriHalRtcFlagLock);
} else {
if(desktop_pin_lock_is_locked()) {
settings->is_locked = 1;
SAVE_DESKTOP_SETTINGS(settings);
}
}
} else {
furi_hal_rtc_set_pin_fails(0);
furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
}
if(desktop_pin_lock_is_locked()) {
Cli* cli = furi_record_open(RECORD_CLI);
cli_session_close(cli);
furi_record_close(RECORD_CLI);
}
}
bool desktop_pin_lock_verify(const PinCode* pin_set, const PinCode* pin_entered) {
bool result = false;
if(desktop_pins_are_equal(pin_set, pin_entered)) {
furi_hal_rtc_set_pin_fails(0);
result = true;
} else {
uint32_t pin_fails = furi_hal_rtc_get_pin_fails();
furi_hal_rtc_set_pin_fails(pin_fails + 1);
result = false;
}
return result;
}
bool desktop_pin_lock_is_locked() {
return furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock);
}
bool desktop_pins_are_equal(const PinCode* pin_code1, const PinCode* pin_code2) {
furi_assert(pin_code1);
furi_assert(pin_code2);
bool result = false;
if(pin_code1->length == pin_code2->length) {
result = !memcmp(pin_code1->data, pin_code2->data, pin_code1->length);
}
return result;
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "../desktop.h"
#include <desktop/desktop_settings.h>
void desktop_pin_lock_error_notify();
uint32_t desktop_pin_lock_get_fail_timeout();
void desktop_pin_lock(DesktopSettings* settings);
void desktop_pin_unlock(DesktopSettings* settings);
bool desktop_pin_lock_is_locked();
void desktop_pin_lock_init(DesktopSettings* settings);
bool desktop_pin_lock_verify(const PinCode* pin_set, const PinCode* pin_entered);
bool desktop_pins_are_equal(const PinCode* pin_code1, const PinCode* pin_code2);

View File

@@ -0,0 +1,125 @@
#include "slideshow.h"
#include <stddef.h>
#include <storage/storage.h>
#include <gui/icon.h>
#include <gui/icon_i.h>
#include <core/dangerous_defines.h>
#define SLIDESHOW_MAGIC 0x72676468
#define SLIDESHOW_MAX_SUPPORTED_VERSION 1
struct Slideshow {
Icon icon;
uint32_t current_frame;
bool loaded;
};
#pragma pack(push, 1)
typedef struct {
uint32_t magic;
uint8_t version;
uint8_t width;
uint8_t height;
uint8_t frame_count;
} SlideshowFileHeader;
_Static_assert(sizeof(SlideshowFileHeader) == 8, "Incorrect SlideshowFileHeader size");
typedef struct {
uint16_t size;
} SlideshowFrameHeader;
_Static_assert(sizeof(SlideshowFrameHeader) == 2, "Incorrect SlideshowFrameHeader size");
#pragma pack(pop)
Slideshow* slideshow_alloc() {
Slideshow* ret = malloc(sizeof(Slideshow));
ret->loaded = false;
return ret;
}
void slideshow_free(Slideshow* slideshow) {
Icon* icon = &slideshow->icon;
if(icon) {
for(int frame_idx = 0; frame_idx < icon->frame_count; ++frame_idx) {
uint8_t* frame_data = (uint8_t*)icon->frames[frame_idx];
free(frame_data);
}
free((uint8_t**)icon->frames);
}
free(slideshow);
}
bool slideshow_load(Slideshow* slideshow, const char* fspath) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* slideshow_file = storage_file_alloc(storage);
slideshow->loaded = false;
do {
if(!storage_file_open(slideshow_file, fspath, FSAM_READ, FSOM_OPEN_EXISTING)) {
break;
}
SlideshowFileHeader header;
if((storage_file_read(slideshow_file, &header, sizeof(header)) != sizeof(header)) ||
(header.magic != SLIDESHOW_MAGIC) ||
(header.version > SLIDESHOW_MAX_SUPPORTED_VERSION)) {
break;
}
Icon* icon = &slideshow->icon;
FURI_CONST_ASSIGN(icon->frame_count, header.frame_count);
FURI_CONST_ASSIGN(icon->width, header.width);
FURI_CONST_ASSIGN(icon->height, header.height);
icon->frames = malloc(header.frame_count * sizeof(uint8_t*));
for(int frame_idx = 0; frame_idx < header.frame_count; ++frame_idx) {
SlideshowFrameHeader frame_header;
if(storage_file_read(slideshow_file, &frame_header, sizeof(frame_header)) !=
sizeof(frame_header)) {
break;
}
FURI_CONST_ASSIGN_PTR(icon->frames[frame_idx], malloc(frame_header.size));
uint8_t* frame_data = (uint8_t*)icon->frames[frame_idx];
if(storage_file_read(slideshow_file, frame_data, frame_header.size) !=
frame_header.size) {
break;
}
slideshow->loaded = (frame_idx + 1) == header.frame_count;
}
} while(false);
storage_file_free(slideshow_file);
furi_record_close(RECORD_STORAGE);
return slideshow->loaded;
}
bool slideshow_is_loaded(Slideshow* slideshow) {
return slideshow->loaded;
}
bool slideshow_is_one_page(Slideshow* slideshow) {
return slideshow->loaded && (slideshow->icon.frame_count == 1);
}
bool slideshow_advance(Slideshow* slideshow) {
uint8_t next_frame = slideshow->current_frame + 1;
if(next_frame < slideshow->icon.frame_count) {
slideshow->current_frame = next_frame;
return true;
}
return false;
}
void slideshow_goback(Slideshow* slideshow) {
if(slideshow->current_frame > 0) {
slideshow->current_frame--;
}
}
void slideshow_draw(Slideshow* slideshow, Canvas* canvas, uint8_t x, uint8_t y) {
furi_assert(slideshow->current_frame < slideshow->icon.frame_count);
canvas_draw_bitmap(
canvas,
x,
y,
slideshow->icon.width,
slideshow->icon.height,
slideshow->icon.frames[slideshow->current_frame]);
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <gui/canvas.h>
typedef struct Slideshow Slideshow;
Slideshow* slideshow_alloc();
void slideshow_free(Slideshow* slideshow);
bool slideshow_load(Slideshow* slideshow, const char* fspath);
bool slideshow_is_loaded(Slideshow* slideshow);
bool slideshow_is_one_page(Slideshow* slideshow);
void slideshow_goback(Slideshow* slideshow);
bool slideshow_advance(Slideshow* slideshow);
void slideshow_draw(Slideshow* slideshow, Canvas* canvas, uint8_t x, uint8_t y);

View File

@@ -0,0 +1,3 @@
#pragma once
#define SLIDESHOW_FILE_NAME ".slideshow"

View File

@@ -0,0 +1,30 @@
#include "desktop_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const desktop_on_enter_handlers[])(void*) = {
#include "desktop_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const desktop_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "desktop_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const desktop_on_exit_handlers[])(void* context) = {
#include "desktop_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers desktop_scene_handlers = {
.on_enter_handlers = desktop_on_enter_handlers,
.on_event_handlers = desktop_on_event_handlers,
.on_exit_handlers = desktop_on_exit_handlers,
.scene_num = DesktopSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) DesktopScene##id,
typedef enum {
#include "desktop_scene_config.h"
DesktopSceneNum,
} DesktopScene;
#undef ADD_SCENE
extern const SceneManagerHandlers desktop_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "desktop_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "desktop_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "desktop_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,9 @@
ADD_SCENE(desktop, main, Main)
ADD_SCENE(desktop, lock_menu, LockMenu)
ADD_SCENE(desktop, debug, Debug)
ADD_SCENE(desktop, hw_mismatch, HwMismatch)
ADD_SCENE(desktop, fault, Fault)
ADD_SCENE(desktop, locked, Locked)
ADD_SCENE(desktop, pin_input, PinInput)
ADD_SCENE(desktop, pin_timeout, PinTimeout)
ADD_SCENE(desktop, slideshow, Slideshow)

View File

@@ -0,0 +1,65 @@
#include <dolphin/dolphin.h>
#include <dolphin/helpers/dolphin_deed.h>
#include "../desktop_i.h"
#include "../views/desktop_view_debug.h"
#include "desktop_scene.h"
void desktop_scene_debug_callback(DesktopEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
}
void desktop_scene_debug_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
desktop_debug_get_dolphin_data(desktop->debug_view);
desktop_debug_set_callback(desktop->debug_view, desktop_scene_debug_callback, desktop);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdDebug);
}
bool desktop_scene_debug_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopDebugEventExit:
scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
dolphin_flush(dolphin);
consumed = true;
break;
case DesktopDebugEventDeed:
dolphin_deed(dolphin, DolphinDeedTestRight);
desktop_debug_get_dolphin_data(desktop->debug_view);
consumed = true;
break;
case DesktopDebugEventWrongDeed:
dolphin_deed(dolphin, DolphinDeedTestLeft);
desktop_debug_get_dolphin_data(desktop->debug_view);
consumed = true;
break;
case DesktopDebugEventSaveState:
dolphin_flush(dolphin);
consumed = true;
break;
default:
break;
}
}
furi_record_close(RECORD_DOLPHIN);
return consumed;
}
void desktop_scene_debug_on_exit(void* context) {
Desktop* desktop = (Desktop*)context;
desktop_debug_reset_screen_idx(desktop->debug_view);
}

View File

@@ -0,0 +1,52 @@
#include <furi_hal.h>
#include "../desktop_i.h"
#define DesktopFaultEventExit 0x00FF00FF
void desktop_scene_fault_callback(void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopFaultEventExit);
}
void desktop_scene_fault_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
Popup* popup = desktop->hw_mismatch_popup;
popup_set_context(popup, desktop);
popup_set_header(
popup,
"Flipper crashed\n and was rebooted",
60,
14 + STATUS_BAR_Y_SHIFT,
AlignCenter,
AlignCenter);
char* message = (char*)furi_hal_rtc_get_fault_data();
popup_set_text(popup, message, 60, 37 + STATUS_BAR_Y_SHIFT, AlignCenter, AlignCenter);
popup_set_callback(popup, desktop_scene_fault_callback);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdHwMismatch);
}
bool desktop_scene_fault_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopFaultEventExit:
scene_manager_previous_scene(desktop->scene_manager);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void desktop_scene_fault_on_exit(void* context) {
UNUSED(context);
furi_hal_rtc_set_fault_data(0);
}

View File

@@ -0,0 +1,67 @@
#include <gui/scene_manager.h>
#include <furi_hal.h>
#include "desktop_scene.h"
#include "../desktop_i.h"
#define HW_MISMATCH_BACK_EVENT (0UL)
void desktop_scene_hw_mismatch_callback(void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, HW_MISMATCH_BACK_EVENT);
}
void desktop_scene_hw_mismatch_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
furi_assert(desktop);
Popup* popup = desktop->hw_mismatch_popup;
char* text_buffer = malloc(256);
scene_manager_set_scene_state(
desktop->scene_manager, DesktopSceneHwMismatch, (uint32_t)text_buffer);
snprintf(
text_buffer,
256,
"HW target: %d\nFW target: %d",
furi_hal_version_get_hw_target(),
version_get_target(NULL));
popup_set_context(popup, desktop);
popup_set_header(
popup, "!!!! HW Mismatch !!!!", 60, 14 + STATUS_BAR_Y_SHIFT, AlignCenter, AlignCenter);
popup_set_text(popup, text_buffer, 60, 37 + STATUS_BAR_Y_SHIFT, AlignCenter, AlignCenter);
popup_set_callback(popup, desktop_scene_hw_mismatch_callback);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdHwMismatch);
}
bool desktop_scene_hw_mismatch_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case HW_MISMATCH_BACK_EVENT:
scene_manager_previous_scene(desktop->scene_manager);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void desktop_scene_hw_mismatch_on_exit(void* context) {
Desktop* desktop = (Desktop*)context;
furi_assert(desktop);
Popup* popup = desktop->hw_mismatch_popup;
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_callback(popup, NULL);
popup_set_context(popup, NULL);
char* text_buffer =
(char*)scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneHwMismatch);
free(text_buffer);
scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneHwMismatch, 0);
}

View File

@@ -0,0 +1,4 @@
#pragma once
#define SCENE_LOCKED_FIRST_ENTER 0
#define SCENE_LOCKED_REPEAT_ENTER 1

View File

@@ -0,0 +1,85 @@
#include <gui/scene_manager.h>
#include <applications.h>
#include <furi_hal.h>
#include <toolbox/saved_struct.h>
#include <stdbool.h>
#include <loader/loader.h>
#include "../desktop_i.h"
#include <desktop/desktop_settings.h>
#include "../views/desktop_view_lock_menu.h"
#include "desktop_scene_i.h"
#include "desktop_scene.h"
#include "../helpers/pin_lock.h"
#define TAG "DesktopSceneLock"
void desktop_scene_lock_menu_callback(DesktopEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
}
void desktop_scene_lock_menu_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
LOAD_DESKTOP_SETTINGS(&desktop->settings);
scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneLockMenu, 0);
desktop_lock_menu_set_callback(desktop->lock_menu, desktop_scene_lock_menu_callback, desktop);
desktop_lock_menu_pin_set(desktop->lock_menu, desktop->settings.pin_code.length > 0);
desktop_lock_menu_set_idx(desktop->lock_menu, 0);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdLockMenu);
}
bool desktop_scene_lock_menu_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeTick) {
bool check_pin_changed =
scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneLockMenu);
if(check_pin_changed) {
LOAD_DESKTOP_SETTINGS(&desktop->settings);
if(desktop->settings.pin_code.length > 0) {
desktop_lock_menu_pin_set(desktop->lock_menu, 1);
scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneLockMenu, 0);
}
}
} else if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopLockMenuEventLock:
scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneLockMenu, 0);
desktop_lock(desktop);
consumed = true;
break;
case DesktopLockMenuEventPinLock:
if(desktop->settings.pin_code.length > 0) {
desktop_pin_lock(&desktop->settings);
desktop_lock(desktop);
} else {
LoaderStatus status =
loader_start(desktop->loader, "Desktop", DESKTOP_SETTINGS_RUN_PIN_SETUP_ARG);
if(status == LoaderStatusOk) {
scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneLockMenu, 1);
} else {
FURI_LOG_E(TAG, "Unable to start desktop settings");
}
}
consumed = true;
break;
case DesktopLockMenuEventExit:
scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneLockMenu, 0);
scene_manager_search_and_switch_to_previous_scene(
desktop->scene_manager, DesktopSceneMain);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void desktop_scene_lock_menu_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,113 @@
#include <furi.h>
#include <furi_hal.h>
#include <gui/scene_manager.h>
#include <gui/view_stack.h>
#include <stdint.h>
#include <portmacro.h>
#include "../desktop.h"
#include "../desktop_i.h"
#include "../helpers/pin_lock.h"
#include "../animations/animation_manager.h"
#include "../views/desktop_events.h"
#include "../views/desktop_view_pin_input.h"
#include "../views/desktop_view_locked.h"
#include "desktop_scene.h"
#include "desktop_scene_i.h"
#define WRONG_PIN_HEADER_TIMEOUT 3000
#define INPUT_PIN_VIEW_TIMEOUT 15000
static void desktop_scene_locked_callback(DesktopEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
}
static void desktop_scene_locked_new_idle_animation_callback(void* context) {
furi_assert(context);
Desktop* desktop = context;
view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopAnimationEventNewIdleAnimation);
}
void desktop_scene_locked_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
// callbacks for 1-st layer
animation_manager_set_new_idle_callback(
desktop->animation_manager, desktop_scene_locked_new_idle_animation_callback);
animation_manager_set_check_callback(desktop->animation_manager, NULL);
animation_manager_set_interact_callback(desktop->animation_manager, NULL);
// callbacks for 2-nd layer
desktop_view_locked_set_callback(desktop->locked_view, desktop_scene_locked_callback, desktop);
bool switch_to_timeout_scene = false;
uint32_t state = scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneLocked);
if(state == SCENE_LOCKED_FIRST_ENTER) {
bool pin_locked = desktop_pin_lock_is_locked();
view_port_enabled_set(desktop->lock_viewport, true);
Gui* gui = furi_record_open(RECORD_GUI);
gui_set_lockdown(gui, true);
furi_record_close(RECORD_GUI);
if(pin_locked) {
LOAD_DESKTOP_SETTINGS(&desktop->settings);
desktop_view_locked_lock(desktop->locked_view, true);
uint32_t pin_timeout = desktop_pin_lock_get_fail_timeout();
if(pin_timeout > 0) {
scene_manager_set_scene_state(
desktop->scene_manager, DesktopScenePinTimeout, pin_timeout);
switch_to_timeout_scene = true;
} else {
desktop_view_locked_close_doors(desktop->locked_view);
}
} else {
desktop_view_locked_lock(desktop->locked_view, false);
desktop_view_locked_close_doors(desktop->locked_view);
}
scene_manager_set_scene_state(
desktop->scene_manager, DesktopSceneLocked, SCENE_LOCKED_REPEAT_ENTER);
}
if(switch_to_timeout_scene) {
scene_manager_next_scene(desktop->scene_manager, DesktopScenePinTimeout);
} else {
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdLocked);
}
}
bool desktop_scene_locked_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopLockedEventUnlocked:
desktop_unlock(desktop);
consumed = true;
break;
case DesktopLockedEventUpdate:
if(desktop_view_locked_is_locked_hint_visible(desktop->locked_view)) {
notification_message(desktop->notification, &sequence_display_backlight_off);
}
desktop_view_locked_update(desktop->locked_view);
consumed = true;
break;
case DesktopLockedEventShowPinInput:
scene_manager_next_scene(desktop->scene_manager, DesktopScenePinInput);
consumed = true;
break;
case DesktopAnimationEventNewIdleAnimation:
animation_manager_new_idle_process(desktop->animation_manager);
consumed = true;
break;
}
}
return consumed;
}
void desktop_scene_locked_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,180 @@
#include <furi.h>
#include <furi_hal.h>
#include <applications.h>
#include <assets_icons.h>
#include <loader/loader.h>
#include "../desktop_i.h"
#include "../views/desktop_events.h"
#include "../views/desktop_view_main.h"
#include "desktop_scene.h"
#include "desktop_scene_i.h"
#define TAG "DesktopSrv"
static void desktop_scene_main_new_idle_animation_callback(void* context) {
furi_assert(context);
Desktop* desktop = context;
view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopAnimationEventNewIdleAnimation);
}
static void desktop_scene_main_check_animation_callback(void* context) {
furi_assert(context);
Desktop* desktop = context;
view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopAnimationEventCheckAnimation);
}
static void desktop_scene_main_interact_animation_callback(void* context) {
furi_assert(context);
Desktop* desktop = context;
view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopAnimationEventInteractAnimation);
}
#ifdef APP_ARCHIVE
static void desktop_switch_to_app(Desktop* desktop, const FlipperApplication* flipper_app) {
furi_assert(desktop);
furi_assert(flipper_app);
furi_assert(flipper_app->app);
furi_assert(flipper_app->name);
if(furi_thread_get_state(desktop->scene_thread) != FuriThreadStateStopped) {
FURI_LOG_E("Desktop", "Thread is already running");
return;
}
furi_thread_set_name(desktop->scene_thread, flipper_app->name);
furi_thread_set_stack_size(desktop->scene_thread, flipper_app->stack_size);
furi_thread_set_callback(desktop->scene_thread, flipper_app->app);
furi_thread_start(desktop->scene_thread);
}
#endif
void desktop_scene_main_callback(DesktopEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
}
void desktop_scene_main_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
DesktopMainView* main_view = desktop->main_view;
animation_manager_set_context(desktop->animation_manager, desktop);
animation_manager_set_new_idle_callback(
desktop->animation_manager, desktop_scene_main_new_idle_animation_callback);
animation_manager_set_check_callback(
desktop->animation_manager, desktop_scene_main_check_animation_callback);
animation_manager_set_interact_callback(
desktop->animation_manager, desktop_scene_main_interact_animation_callback);
desktop_main_set_callback(main_view, desktop_scene_main_callback, desktop);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdMain);
}
bool desktop_scene_main_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopMainEventOpenMenu:
loader_show_menu();
consumed = true;
break;
case DesktopMainEventOpenLockMenu:
scene_manager_next_scene(desktop->scene_manager, DesktopSceneLockMenu);
consumed = true;
break;
case DesktopMainEventOpenDebug:
scene_manager_next_scene(desktop->scene_manager, DesktopSceneDebug);
consumed = true;
break;
case DesktopMainEventOpenArchive:
#ifdef APP_ARCHIVE
desktop_switch_to_app(desktop, &FLIPPER_ARCHIVE);
#endif
consumed = true;
break;
case DesktopMainEventOpenPowerOff: {
LoaderStatus status = loader_start(desktop->loader, "Power", "off");
if(status != LoaderStatusOk) {
FURI_LOG_E(TAG, "loader_start failed: %d", status);
}
consumed = true;
break;
}
case DesktopMainEventOpenFavoritePrimary:
LOAD_DESKTOP_SETTINGS(&desktop->settings);
if(desktop->settings.favorite_primary < FLIPPER_APPS_COUNT) {
LoaderStatus status = loader_start(
desktop->loader, FLIPPER_APPS[desktop->settings.favorite_primary].name, NULL);
if(status != LoaderStatusOk) {
FURI_LOG_E(TAG, "loader_start failed: %d", status);
}
} else {
FURI_LOG_E(TAG, "Can't find primary favorite application");
}
consumed = true;
break;
case DesktopMainEventOpenFavoriteSecondary:
LOAD_DESKTOP_SETTINGS(&desktop->settings);
if(desktop->settings.favorite_secondary < FLIPPER_APPS_COUNT) {
LoaderStatus status = loader_start(
desktop->loader,
FLIPPER_APPS[desktop->settings.favorite_secondary].name,
NULL);
if(status != LoaderStatusOk) {
FURI_LOG_E(TAG, "loader_start failed: %d", status);
}
} else {
FURI_LOG_E(TAG, "Can't find secondary favorite application");
}
consumed = true;
break;
case DesktopAnimationEventCheckAnimation:
animation_manager_check_blocking_process(desktop->animation_manager);
consumed = true;
break;
case DesktopAnimationEventNewIdleAnimation:
animation_manager_new_idle_process(desktop->animation_manager);
consumed = true;
break;
case DesktopAnimationEventInteractAnimation:
if(!animation_manager_interact_process(desktop->animation_manager)) {
LoaderStatus status = loader_start(desktop->loader, "Passport", NULL);
if(status != LoaderStatusOk) {
FURI_LOG_E(TAG, "loader_start failed: %d", status);
}
}
consumed = true;
break;
case DesktopLockedEventUpdate:
desktop_view_locked_update(desktop->locked_view);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void desktop_scene_main_on_exit(void* context) {
Desktop* desktop = (Desktop*)context;
animation_manager_set_new_idle_callback(desktop->animation_manager, NULL);
animation_manager_set_check_callback(desktop->animation_manager, NULL);
animation_manager_set_interact_callback(desktop->animation_manager, NULL);
animation_manager_set_context(desktop->animation_manager, desktop);
}

View File

@@ -0,0 +1,157 @@
#include <furi.h>
#include <furi_hal.h>
#include <gui/scene_manager.h>
#include <gui/view_stack.h>
#include <stdint.h>
#include <portmacro.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include "../desktop.h"
#include "../desktop_i.h"
#include "../animations/animation_manager.h"
#include "../views/desktop_events.h"
#include "../views/desktop_view_pin_input.h"
#include "../helpers/pin_lock.h"
#include "desktop_scene.h"
#include "desktop_scene_i.h"
#define WRONG_PIN_HEADER_TIMEOUT 3000
#define INPUT_PIN_VIEW_TIMEOUT 15000
typedef struct {
TimerHandle_t timer;
} DesktopScenePinInputState;
static void desktop_scene_locked_light_red(bool value) {
NotificationApp* app = furi_record_open(RECORD_NOTIFICATION);
if(value) {
notification_message(app, &sequence_set_only_red_255);
} else {
notification_message(app, &sequence_reset_red);
}
furi_record_close(RECORD_NOTIFICATION);
}
static void
desktop_scene_pin_input_set_timer(Desktop* desktop, bool enable, TickType_t new_period) {
furi_assert(desktop);
DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
desktop->scene_manager, DesktopScenePinInput);
furi_assert(state);
if(enable) {
xTimerChangePeriod(state->timer, new_period, portMAX_DELAY);
} else {
xTimerStop(state->timer, portMAX_DELAY);
}
}
static void desktop_scene_pin_input_back_callback(void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventBack);
}
static void desktop_scene_pin_input_done_callback(const PinCode* pin_code, void* context) {
Desktop* desktop = (Desktop*)context;
if(desktop_pin_lock_verify(&desktop->settings.pin_code, pin_code)) {
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinInputEventUnlocked);
} else {
view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopPinInputEventUnlockFailed);
}
}
static void desktop_scene_pin_input_timer_callback(TimerHandle_t timer) {
Desktop* desktop = pvTimerGetTimerID(timer);
view_dispatcher_send_custom_event(
desktop->view_dispatcher, DesktopPinInputEventResetWrongPinLabel);
}
void desktop_scene_pin_input_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
desktop_view_pin_input_set_context(desktop->pin_input_view, desktop);
desktop_view_pin_input_set_back_callback(
desktop->pin_input_view, desktop_scene_pin_input_back_callback);
desktop_view_pin_input_set_timeout_callback(
desktop->pin_input_view, desktop_scene_pin_input_back_callback);
desktop_view_pin_input_set_done_callback(
desktop->pin_input_view, desktop_scene_pin_input_done_callback);
DesktopScenePinInputState* state = malloc(sizeof(DesktopScenePinInputState));
state->timer =
xTimerCreate(NULL, 10000, pdFALSE, desktop, desktop_scene_pin_input_timer_callback);
scene_manager_set_scene_state(desktop->scene_manager, DesktopScenePinInput, (uint32_t)state);
desktop_view_pin_input_hide_pin(desktop->pin_input_view, true);
desktop_view_pin_input_set_label_button(desktop->pin_input_view, "OK");
desktop_view_pin_input_set_label_secondary(desktop->pin_input_view, 44, 25, "Enter PIN:");
desktop_view_pin_input_set_pin_position(desktop->pin_input_view, 64, 37);
desktop_view_pin_input_reset_pin(desktop->pin_input_view);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdPinInput);
}
bool desktop_scene_pin_input_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
uint32_t pin_timeout = 0;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopPinInputEventUnlockFailed:
pin_timeout = desktop_pin_lock_get_fail_timeout();
if(pin_timeout > 0) {
desktop_pin_lock_error_notify();
scene_manager_set_scene_state(
desktop->scene_manager, DesktopScenePinTimeout, pin_timeout);
scene_manager_next_scene(desktop->scene_manager, DesktopScenePinTimeout);
} else {
desktop_scene_locked_light_red(true);
desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
desktop_view_pin_input_set_label_secondary(
desktop->pin_input_view, 25, 25, "Wrong PIN try again:");
desktop_scene_pin_input_set_timer(desktop, true, WRONG_PIN_HEADER_TIMEOUT);
desktop_view_pin_input_reset_pin(desktop->pin_input_view);
}
consumed = true;
break;
case DesktopPinInputEventResetWrongPinLabel:
desktop_scene_locked_light_red(false);
desktop_view_pin_input_set_label_primary(desktop->pin_input_view, 0, 0, NULL);
desktop_view_pin_input_set_label_secondary(
desktop->pin_input_view, 44, 25, "Enter PIN:");
consumed = true;
break;
case DesktopPinInputEventUnlocked:
desktop_pin_unlock(&desktop->settings);
desktop_unlock(desktop);
consumed = true;
break;
case DesktopPinInputEventBack:
scene_manager_search_and_switch_to_previous_scene(
desktop->scene_manager, DesktopSceneLocked);
notification_message(desktop->notification, &sequence_display_backlight_off);
consumed = true;
break;
}
}
return consumed;
}
void desktop_scene_pin_input_on_exit(void* context) {
Desktop* desktop = (Desktop*)context;
desktop_scene_locked_light_red(false);
DesktopScenePinInputState* state = (DesktopScenePinInputState*)scene_manager_get_scene_state(
desktop->scene_manager, DesktopScenePinInput);
xTimerStop(state->timer, portMAX_DELAY);
while(xTimerIsTimerActive(state->timer)) {
furi_delay_tick(1);
}
xTimerDelete(state->timer, portMAX_DELAY);
free(state);
}

View File

@@ -0,0 +1,47 @@
#include <furi.h>
#include <FreeRTOS.h>
#include <portmacro.h>
#include <timer.h>
#include <gui/scene_manager.h>
#include "../desktop_i.h"
#include "../views/desktop_view_pin_timeout.h"
#include "desktop_scene.h"
#include "desktop_scene_i.h"
static void desktop_scene_pin_timeout_callback(void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopPinTimeoutExit);
}
void desktop_scene_pin_timeout_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
uint32_t timeout =
scene_manager_get_scene_state(desktop->scene_manager, DesktopScenePinTimeout);
desktop_view_pin_timeout_start(desktop->pin_timeout_view, timeout);
desktop_view_pin_timeout_set_callback(
desktop->pin_timeout_view, desktop_scene_pin_timeout_callback, desktop);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdPinTimeout);
}
bool desktop_scene_pin_timeout_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopPinTimeoutExit:
scene_manager_previous_scene(desktop->scene_manager);
consumed = true;
break;
}
}
return consumed;
}
void desktop_scene_pin_timeout_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,53 @@
#include <storage/storage.h>
#include "../desktop_i.h"
#include "../views/desktop_view_slideshow.h"
#include "../views/desktop_events.h"
#include <power/power_service/power.h>
void desktop_scene_slideshow_callback(DesktopEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
}
void desktop_scene_slideshow_on_enter(void* context) {
Desktop* desktop = (Desktop*)context;
DesktopSlideshowView* slideshow_view = desktop->slideshow_view;
desktop_view_slideshow_set_callback(slideshow_view, desktop_scene_slideshow_callback, desktop);
view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdSlideshow);
}
bool desktop_scene_slideshow_on_event(void* context, SceneManagerEvent event) {
Desktop* desktop = (Desktop*)context;
bool consumed = false;
Storage* storage = NULL;
Power* power = NULL;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DesktopSlideshowCompleted:
storage = furi_record_open(RECORD_STORAGE);
storage_common_remove(storage, SLIDESHOW_FS_PATH);
furi_record_close(RECORD_STORAGE);
scene_manager_previous_scene(desktop->scene_manager);
consumed = true;
break;
case DesktopSlideshowPoweroff:
power = furi_record_open(RECORD_POWER);
power_off(power);
furi_record_close(RECORD_POWER);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void desktop_scene_slideshow_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,44 @@
#pragma once
typedef enum {
DesktopMainEventOpenLockMenu,
DesktopMainEventOpenArchive,
DesktopMainEventOpenFavoritePrimary,
DesktopMainEventOpenFavoriteSecondary,
DesktopMainEventOpenMenu,
DesktopMainEventOpenDebug,
DesktopMainEventOpenPassport, /**< Broken, don't use it */
DesktopMainEventOpenPowerOff,
DesktopLockedEventUnlocked,
DesktopLockedEventUpdate,
DesktopLockedEventShowPinInput,
DesktopPinInputEventResetWrongPinLabel,
DesktopPinInputEventUnlocked,
DesktopPinInputEventUnlockFailed,
DesktopPinInputEventBack,
DesktopPinTimeoutExit,
DesktopDebugEventDeed,
DesktopDebugEventWrongDeed,
DesktopDebugEventSaveState,
DesktopDebugEventExit,
DesktopLockMenuEventLock,
DesktopLockMenuEventPinLock,
DesktopLockMenuEventExit,
DesktopAnimationEventCheckAnimation,
DesktopAnimationEventNewIdleAnimation,
DesktopAnimationEventInteractAnimation,
DesktopSlideshowCompleted,
DesktopSlideshowPoweroff,
// Global events
DesktopGlobalBeforeAppStarted,
DesktopGlobalAfterAppFinished,
DesktopGlobalAutoLock,
} DesktopEvent;

View File

@@ -0,0 +1,200 @@
#include <toolbox/version.h>
#include <furi.h>
#include <furi_hal.h>
#include <dolphin/helpers/dolphin_state.h>
#include <dolphin/dolphin.h>
#include "../desktop_i.h"
#include "desktop_view_debug.h"
void desktop_debug_set_callback(
DesktopDebugView* debug_view,
DesktopDebugViewCallback callback,
void* context) {
furi_assert(debug_view);
furi_assert(callback);
debug_view->callback = callback;
debug_view->context = context;
}
void desktop_debug_render(Canvas* canvas, void* model) {
canvas_clear(canvas);
DesktopDebugViewModel* m = model;
const Version* ver;
char buffer[64];
static const char* headers[] = {"Device Info:", "Dolphin Info:"};
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 1 + STATUS_BAR_Y_SHIFT, AlignCenter, AlignTop, headers[m->screen]);
canvas_set_font(canvas, FontSecondary);
if(m->screen != DesktopViewStatsMeta) {
// Hardware version
const char* my_name = furi_hal_version_get_name_ptr();
snprintf(
buffer,
sizeof(buffer),
"%d.F%dB%dC%d %s:%s %s",
furi_hal_version_get_hw_version(),
furi_hal_version_get_hw_target(),
furi_hal_version_get_hw_body(),
furi_hal_version_get_hw_connect(),
furi_hal_version_get_hw_region_name(),
furi_hal_region_get_name(),
my_name ? my_name : "Unknown");
canvas_draw_str(canvas, 0, 19 + STATUS_BAR_Y_SHIFT, buffer);
ver = furi_hal_version_get_firmware_version();
const BleGlueC2Info* c2_ver = NULL;
#ifdef SRV_BT
c2_ver = ble_glue_get_c2_info();
#endif
if(!ver) {
canvas_draw_str(canvas, 0, 30 + STATUS_BAR_Y_SHIFT, "No info");
return;
}
snprintf(
buffer,
sizeof(buffer),
"%s [%s]",
version_get_version(ver),
version_get_builddate(ver));
canvas_draw_str(canvas, 0, 30 + STATUS_BAR_Y_SHIFT, buffer);
snprintf(
buffer,
sizeof(buffer),
"%s%s [%s] %s",
version_get_dirty_flag(ver) ? "[!] " : "",
version_get_githash(ver),
version_get_gitbranchnum(ver),
c2_ver ? c2_ver->StackTypeString : "<none>");
canvas_draw_str(canvas, 0, 40 + STATUS_BAR_Y_SHIFT, buffer);
snprintf(
buffer, sizeof(buffer), "[%d] %s", version_get_target(ver), version_get_gitbranch(ver));
canvas_draw_str(canvas, 0, 50 + STATUS_BAR_Y_SHIFT, buffer);
} else {
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
uint32_t current_lvl = stats.level;
uint32_t remaining = dolphin_state_xp_to_levelup(m->icounter);
canvas_set_font(canvas, FontSecondary);
snprintf(buffer, sizeof(buffer), "Icounter: %ld Butthurt %ld", m->icounter, m->butthurt);
canvas_draw_str(canvas, 5, 19 + STATUS_BAR_Y_SHIFT, buffer);
snprintf(
buffer,
sizeof(buffer),
"Level: %ld To level up: %ld",
current_lvl,
(remaining == (uint32_t)(-1) ? remaining : 0));
canvas_draw_str(canvas, 5, 29 + STATUS_BAR_Y_SHIFT, buffer);
// even if timestamp is uint64_t, it's safe to cast it to uint32_t, because furi_hal_rtc_datetime_to_timestamp only returns uint32_t
snprintf(buffer, sizeof(buffer), "%ld", (uint32_t)m->timestamp);
canvas_draw_str(canvas, 5, 39 + STATUS_BAR_Y_SHIFT, buffer);
canvas_draw_str(canvas, 0, 49 + STATUS_BAR_Y_SHIFT, "[< >] icounter value [ok] save");
}
}
View* desktop_debug_get_view(DesktopDebugView* debug_view) {
furi_assert(debug_view);
return debug_view->view;
}
bool desktop_debug_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
DesktopDebugView* debug_view = context;
if(event->type != InputTypeShort && event->type != InputTypeRepeat) {
return false;
}
DesktopViewStatsScreens current = 0;
with_view_model(
debug_view->view, (DesktopDebugViewModel * model) {
#ifdef SRV_DOLPHIN_STATE_DEBUG
if((event->key == InputKeyDown) || (event->key == InputKeyUp)) {
model->screen = !model->screen;
}
#endif
current = model->screen;
return true;
});
size_t count = (event->type == InputTypeRepeat) ? 10 : 1;
if(current == DesktopViewStatsMeta) {
if(event->key == InputKeyLeft) {
while(count-- > 0) {
debug_view->callback(DesktopDebugEventWrongDeed, debug_view->context);
}
} else if(event->key == InputKeyRight) {
while(count-- > 0) {
debug_view->callback(DesktopDebugEventDeed, debug_view->context);
}
} else if(event->key == InputKeyOk) {
debug_view->callback(DesktopDebugEventSaveState, debug_view->context);
} else {
return false;
}
}
if(event->key == InputKeyBack) {
debug_view->callback(DesktopDebugEventExit, debug_view->context);
}
return true;
}
DesktopDebugView* desktop_debug_alloc() {
DesktopDebugView* debug_view = malloc(sizeof(DesktopDebugView));
debug_view->view = view_alloc();
view_allocate_model(debug_view->view, ViewModelTypeLocking, sizeof(DesktopDebugViewModel));
view_set_context(debug_view->view, debug_view);
view_set_draw_callback(debug_view->view, (ViewDrawCallback)desktop_debug_render);
view_set_input_callback(debug_view->view, desktop_debug_input);
return debug_view;
}
void desktop_debug_free(DesktopDebugView* debug_view) {
furi_assert(debug_view);
view_free(debug_view->view);
free(debug_view);
}
void desktop_debug_get_dolphin_data(DesktopDebugView* debug_view) {
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
with_view_model(
debug_view->view, (DesktopDebugViewModel * model) {
model->icounter = stats.icounter;
model->butthurt = stats.butthurt;
model->timestamp = stats.timestamp;
return true;
});
furi_record_close(RECORD_DOLPHIN);
}
void desktop_debug_reset_screen_idx(DesktopDebugView* debug_view) {
with_view_model(
debug_view->view, (DesktopDebugViewModel * model) {
model->screen = 0;
return true;
});
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include <stdint.h>
#include <gui/view.h>
#include "desktop_events.h"
typedef struct DesktopDebugView DesktopDebugView;
typedef void (*DesktopDebugViewCallback)(DesktopEvent event, void* context);
// Debug info
typedef enum {
DesktopViewStatsFw,
DesktopViewStatsMeta,
DesktopViewStatsTotalCount,
} DesktopViewStatsScreens;
struct DesktopDebugView {
View* view;
DesktopDebugViewCallback callback;
void* context;
};
typedef struct {
uint32_t icounter;
uint32_t butthurt;
uint64_t timestamp;
DesktopViewStatsScreens screen;
} DesktopDebugViewModel;
void desktop_debug_set_callback(
DesktopDebugView* debug_view,
DesktopDebugViewCallback callback,
void* context);
View* desktop_debug_get_view(DesktopDebugView* debug_view);
DesktopDebugView* desktop_debug_alloc();
void desktop_debug_free(DesktopDebugView* debug_view);
void desktop_debug_get_dolphin_data(DesktopDebugView* debug_view);
void desktop_debug_reset_screen_idx(DesktopDebugView* debug_view);

View File

@@ -0,0 +1,130 @@
#include <furi.h>
#include <gui/elements.h>
#include "../desktop_i.h"
#include "desktop_view_lock_menu.h"
#define LOCK_MENU_ITEMS_NB 3
void desktop_lock_menu_set_callback(
DesktopLockMenuView* lock_menu,
DesktopLockMenuViewCallback callback,
void* context) {
furi_assert(lock_menu);
furi_assert(callback);
lock_menu->callback = callback;
lock_menu->context = context;
}
void desktop_lock_menu_pin_set(DesktopLockMenuView* lock_menu, bool pin_is_set) {
with_view_model(
lock_menu->view, (DesktopLockMenuViewModel * model) {
model->pin_set = pin_is_set;
return true;
});
}
void desktop_lock_menu_set_idx(DesktopLockMenuView* lock_menu, uint8_t idx) {
furi_assert(idx < LOCK_MENU_ITEMS_NB);
with_view_model(
lock_menu->view, (DesktopLockMenuViewModel * model) {
model->idx = idx;
return true;
});
}
static void lock_menu_callback(void* context, uint8_t index) {
furi_assert(context);
DesktopLockMenuView* lock_menu = context;
switch(index) {
case 0: // lock
lock_menu->callback(DesktopLockMenuEventLock, lock_menu->context);
break;
case 1: // lock
lock_menu->callback(DesktopLockMenuEventPinLock, lock_menu->context);
break;
default: // wip message
with_view_model(
lock_menu->view, (DesktopLockMenuViewModel * model) {
model->hint_timeout = HINT_TIMEOUT;
return true;
});
break;
}
}
void desktop_lock_menu_render(Canvas* canvas, void* model) {
const char* Lockmenu_Items[LOCK_MENU_ITEMS_NB] = {"Lock", "Lock with PIN", "DUMB mode"};
DesktopLockMenuViewModel* m = model;
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon(canvas, -57, 0 + STATUS_BAR_Y_SHIFT, &I_DoorLeft_70x55);
canvas_draw_icon(canvas, 116, 0 + STATUS_BAR_Y_SHIFT, &I_DoorRight_70x55);
canvas_set_font(canvas, FontSecondary);
for(uint8_t i = 0; i < LOCK_MENU_ITEMS_NB; ++i) {
const char* str = Lockmenu_Items[i];
if(i == 1 && !m->pin_set) str = "Set PIN";
if(m->hint_timeout && m->idx == 2 && m->idx == i) str = "Not Implemented";
if(str != NULL)
canvas_draw_str_aligned(
canvas, 64, 9 + (i * 17) + STATUS_BAR_Y_SHIFT, AlignCenter, AlignCenter, str);
if(m->idx == i) elements_frame(canvas, 15, 1 + (i * 17) + STATUS_BAR_Y_SHIFT, 98, 15);
}
}
View* desktop_lock_menu_get_view(DesktopLockMenuView* lock_menu) {
furi_assert(lock_menu);
return lock_menu->view;
}
bool desktop_lock_menu_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
DesktopLockMenuView* lock_menu = context;
uint8_t idx;
if(event->type != InputTypeShort) return false;
with_view_model(
lock_menu->view, (DesktopLockMenuViewModel * model) {
model->hint_timeout = 0; // clear hint timeout
if(event->key == InputKeyUp) {
model->idx = CLAMP(model->idx - 1, LOCK_MENU_ITEMS_NB - 1, 0);
} else if(event->key == InputKeyDown) {
model->idx = CLAMP(model->idx + 1, LOCK_MENU_ITEMS_NB - 1, 0);
}
idx = model->idx;
return true;
});
if(event->key == InputKeyBack) {
lock_menu->callback(DesktopLockMenuEventExit, lock_menu->context);
} else if(event->key == InputKeyOk) {
lock_menu_callback(lock_menu, idx);
}
return true;
}
DesktopLockMenuView* desktop_lock_menu_alloc() {
DesktopLockMenuView* lock_menu = malloc(sizeof(DesktopLockMenuView));
lock_menu->view = view_alloc();
view_allocate_model(lock_menu->view, ViewModelTypeLocking, sizeof(DesktopLockMenuViewModel));
view_set_context(lock_menu->view, lock_menu);
view_set_draw_callback(lock_menu->view, (ViewDrawCallback)desktop_lock_menu_render);
view_set_input_callback(lock_menu->view, desktop_lock_menu_input);
return lock_menu;
}
void desktop_lock_menu_free(DesktopLockMenuView* lock_menu_view) {
furi_assert(lock_menu_view);
view_free(lock_menu_view->view);
free(lock_menu_view);
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include <gui/view.h>
#include "desktop_events.h"
#define HINT_TIMEOUT 2
typedef struct DesktopLockMenuView DesktopLockMenuView;
typedef void (*DesktopLockMenuViewCallback)(DesktopEvent event, void* context);
struct DesktopLockMenuView {
View* view;
DesktopLockMenuViewCallback callback;
void* context;
};
typedef struct {
uint8_t idx;
uint8_t hint_timeout;
bool pin_set;
} DesktopLockMenuViewModel;
void desktop_lock_menu_set_callback(
DesktopLockMenuView* lock_menu,
DesktopLockMenuViewCallback callback,
void* context);
View* desktop_lock_menu_get_view(DesktopLockMenuView* lock_menu);
void desktop_lock_menu_pin_set(DesktopLockMenuView* lock_menu, bool pin_is_set);
void desktop_lock_menu_set_idx(DesktopLockMenuView* lock_menu, uint8_t idx);
DesktopLockMenuView* desktop_lock_menu_alloc();
void desktop_lock_menu_free(DesktopLockMenuView* lock_menu);

View File

@@ -0,0 +1,245 @@
#include <projdefs.h>
#include <stdint.h>
#include <furi.h>
#include <gui/elements.h>
#include <gui/icon.h>
#include <gui/view.h>
#include <portmacro.h>
#include <desktop/desktop_settings.h>
#include "../desktop_i.h"
#include "desktop_view_locked.h"
#define DOOR_MOVING_INTERVAL_MS (1000 / 16)
#define LOCKED_HINT_TIMEOUT_MS (1000)
#define UNLOCKED_HINT_TIMEOUT_MS (2000)
#define DOOR_OFFSET_START -55
#define DOOR_OFFSET_END 0
#define DOOR_L_FINAL_POS 0
#define DOOR_R_FINAL_POS 60
#define UNLOCK_CNT 3
#define UNLOCK_RST_TIMEOUT 600
struct DesktopViewLocked {
View* view;
DesktopViewLockedCallback callback;
void* context;
TimerHandle_t timer;
uint8_t lock_count;
uint32_t lock_lastpress;
};
typedef enum {
DesktopViewLockedStateUnlocked,
DesktopViewLockedStateLocked,
DesktopViewLockedStateDoorsClosing,
DesktopViewLockedStateLockedHintShown,
DesktopViewLockedStateUnlockedHintShown
} DesktopViewLockedState;
typedef struct {
bool pin_locked;
int8_t door_offset;
DesktopViewLockedState view_state;
} DesktopViewLockedModel;
void desktop_view_locked_set_callback(
DesktopViewLocked* locked_view,
DesktopViewLockedCallback callback,
void* context) {
furi_assert(locked_view);
furi_assert(callback);
locked_view->callback = callback;
locked_view->context = context;
}
static void locked_view_timer_callback(TimerHandle_t timer) {
DesktopViewLocked* locked_view = pvTimerGetTimerID(timer);
locked_view->callback(DesktopLockedEventUpdate, locked_view->context);
}
static void desktop_view_locked_doors_draw(Canvas* canvas, DesktopViewLockedModel* model) {
int8_t offset = model->door_offset;
uint8_t door_left_x = DOOR_L_FINAL_POS + offset;
uint8_t door_right_x = DOOR_R_FINAL_POS - offset;
uint8_t height = icon_get_height(&I_DoorLeft_70x55);
canvas_draw_icon(canvas, door_left_x, canvas_height(canvas) - height, &I_DoorLeft_70x55);
canvas_draw_icon(canvas, door_right_x, canvas_height(canvas) - height, &I_DoorRight_70x55);
}
static bool desktop_view_locked_doors_move(DesktopViewLockedModel* model) {
bool stop = false;
if(model->door_offset < DOOR_OFFSET_END) {
model->door_offset = CLAMP(model->door_offset + 5, DOOR_OFFSET_END, DOOR_OFFSET_START);
stop = true;
}
return stop;
}
static void desktop_view_locked_update_hint_icon_timeout(DesktopViewLocked* locked_view) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
const bool change_state = (model->view_state == DesktopViewLockedStateLocked) &&
!model->pin_locked;
if(change_state) {
model->view_state = DesktopViewLockedStateLockedHintShown;
}
view_commit_model(locked_view->view, change_state);
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(LOCKED_HINT_TIMEOUT_MS), portMAX_DELAY);
}
void desktop_view_locked_update(DesktopViewLocked* locked_view) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
DesktopViewLockedState view_state = model->view_state;
if(view_state == DesktopViewLockedStateDoorsClosing &&
!desktop_view_locked_doors_move(model)) {
model->view_state = DesktopViewLockedStateLocked;
} else if(view_state == DesktopViewLockedStateLockedHintShown) {
model->view_state = DesktopViewLockedStateLocked;
} else if(view_state == DesktopViewLockedStateUnlockedHintShown) {
model->view_state = DesktopViewLockedStateUnlocked;
}
view_commit_model(locked_view->view, true);
if(view_state != DesktopViewLockedStateDoorsClosing) {
xTimerStop(locked_view->timer, portMAX_DELAY);
}
}
static void desktop_view_locked_draw(Canvas* canvas, void* model) {
DesktopViewLockedModel* m = model;
DesktopViewLockedState view_state = m->view_state;
canvas_set_color(canvas, ColorBlack);
if(view_state == DesktopViewLockedStateDoorsClosing) {
desktop_view_locked_doors_draw(canvas, m);
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_framed(canvas, 42, 30 + STATUS_BAR_Y_SHIFT, "Locked");
} else if(view_state == DesktopViewLockedStateLockedHintShown) {
canvas_set_font(canvas, FontSecondary);
elements_bold_rounded_frame(canvas, 14, 2 + STATUS_BAR_Y_SHIFT, 99, 48);
elements_multiline_text(canvas, 65, 20 + STATUS_BAR_Y_SHIFT, "To unlock\npress:");
canvas_draw_icon(canvas, 65, 36 + STATUS_BAR_Y_SHIFT, &I_Pin_back_arrow_10x8);
canvas_draw_icon(canvas, 80, 36 + STATUS_BAR_Y_SHIFT, &I_Pin_back_arrow_10x8);
canvas_draw_icon(canvas, 95, 36 + STATUS_BAR_Y_SHIFT, &I_Pin_back_arrow_10x8);
canvas_draw_icon(canvas, 16, 7 + STATUS_BAR_Y_SHIFT, &I_WarningDolphin_45x42);
canvas_draw_dot(canvas, 17, 61);
} else if(view_state == DesktopViewLockedStateUnlockedHintShown) {
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_framed(canvas, 42, 30 + STATUS_BAR_Y_SHIFT, "Unlocked");
}
}
View* desktop_view_locked_get_view(DesktopViewLocked* locked_view) {
furi_assert(locked_view);
return locked_view->view;
}
static bool desktop_view_locked_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
bool is_changed = false;
const uint32_t press_time = xTaskGetTickCount();
DesktopViewLocked* locked_view = context;
DesktopViewLockedModel* model = view_get_model(locked_view->view);
if(model->view_state == DesktopViewLockedStateUnlockedHintShown &&
event->type == InputTypePress) {
model->view_state = DesktopViewLockedStateUnlocked;
is_changed = true;
}
const DesktopViewLockedState view_state = model->view_state;
const bool pin_locked = model->pin_locked;
view_commit_model(locked_view->view, is_changed);
if(view_state == DesktopViewLockedStateUnlocked) {
return view_state != DesktopViewLockedStateUnlocked;
} else if(view_state == DesktopViewLockedStateLocked && pin_locked) {
locked_view->callback(DesktopLockedEventShowPinInput, locked_view->context);
} else if(
view_state == DesktopViewLockedStateLocked ||
view_state == DesktopViewLockedStateLockedHintShown) {
if(press_time - locked_view->lock_lastpress > UNLOCK_RST_TIMEOUT) {
locked_view->lock_lastpress = press_time;
locked_view->lock_count = 0;
}
desktop_view_locked_update_hint_icon_timeout(locked_view);
if(event->key == InputKeyBack) {
if(event->type == InputTypeShort) {
locked_view->lock_lastpress = press_time;
locked_view->lock_count++;
if(locked_view->lock_count == UNLOCK_CNT) {
locked_view->callback(DesktopLockedEventUnlocked, locked_view->context);
}
}
} else {
locked_view->lock_count = 0;
}
locked_view->lock_lastpress = press_time;
}
return true;
}
DesktopViewLocked* desktop_view_locked_alloc() {
DesktopViewLocked* locked_view = malloc(sizeof(DesktopViewLocked));
locked_view->view = view_alloc();
locked_view->timer =
xTimerCreate(NULL, 1000 / 16, pdTRUE, locked_view, locked_view_timer_callback);
view_allocate_model(locked_view->view, ViewModelTypeLocking, sizeof(DesktopViewLockedModel));
view_set_context(locked_view->view, locked_view);
view_set_draw_callback(locked_view->view, desktop_view_locked_draw);
view_set_input_callback(locked_view->view, desktop_view_locked_input);
return locked_view;
}
void desktop_view_locked_free(DesktopViewLocked* locked_view) {
furi_assert(locked_view);
furi_timer_free(locked_view->timer);
view_free(locked_view->view);
free(locked_view);
}
void desktop_view_locked_close_doors(DesktopViewLocked* locked_view) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
furi_assert(model->view_state == DesktopViewLockedStateLocked);
model->view_state = DesktopViewLockedStateDoorsClosing;
model->door_offset = DOOR_OFFSET_START;
view_commit_model(locked_view->view, true);
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(DOOR_MOVING_INTERVAL_MS), portMAX_DELAY);
}
void desktop_view_locked_lock(DesktopViewLocked* locked_view, bool pin_locked) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
furi_assert(model->view_state == DesktopViewLockedStateUnlocked);
model->view_state = DesktopViewLockedStateLocked;
model->pin_locked = pin_locked;
view_commit_model(locked_view->view, true);
}
void desktop_view_locked_unlock(DesktopViewLocked* locked_view) {
locked_view->lock_count = 0;
DesktopViewLockedModel* model = view_get_model(locked_view->view);
model->view_state = DesktopViewLockedStateUnlockedHintShown;
model->pin_locked = false;
view_commit_model(locked_view->view, true);
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(UNLOCKED_HINT_TIMEOUT_MS), portMAX_DELAY);
}
bool desktop_view_locked_is_locked_hint_visible(DesktopViewLocked* locked_view) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
const DesktopViewLockedState view_state = model->view_state;
view_commit_model(locked_view->view, false);
return view_state == DesktopViewLockedStateLockedHintShown;
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include <desktop/desktop_settings.h>
#include "../views/desktop_events.h"
#include <gui/view.h>
typedef struct DesktopViewLocked DesktopViewLocked;
typedef void (*DesktopViewLockedCallback)(DesktopEvent event, void* context);
void desktop_view_locked_set_callback(
DesktopViewLocked* locked_view,
DesktopViewLockedCallback callback,
void* context);
void desktop_view_locked_update(DesktopViewLocked* locked_view);
View* desktop_view_locked_get_view(DesktopViewLocked* locked_view);
DesktopViewLocked* desktop_view_locked_alloc();
void desktop_view_locked_free(DesktopViewLocked* locked_view);
void desktop_view_locked_lock(DesktopViewLocked* locked_view, bool pin_locked);
void desktop_view_locked_unlock(DesktopViewLocked* locked_view);
void desktop_view_locked_close_doors(DesktopViewLocked* locked_view);
bool desktop_view_locked_is_locked_hint_visible(DesktopViewLocked* locked_view);

View File

@@ -0,0 +1,104 @@
#include <gui/gui_i.h>
#include <gui/view.h>
#include <gui/elements.h>
#include <gui/canvas.h>
#include <furi.h>
#include <input/input.h>
#include <dolphin/dolphin.h>
#include "../desktop_i.h"
#include "desktop_view_main.h"
struct DesktopMainView {
View* view;
DesktopMainViewCallback callback;
void* context;
TimerHandle_t poweroff_timer;
};
#define DESKTOP_MAIN_VIEW_POWEROFF_TIMEOUT 5000
static void desktop_main_poweroff_timer_callback(TimerHandle_t timer) {
DesktopMainView* main_view = pvTimerGetTimerID(timer);
main_view->callback(DesktopMainEventOpenPowerOff, main_view->context);
}
void desktop_main_set_callback(
DesktopMainView* main_view,
DesktopMainViewCallback callback,
void* context) {
furi_assert(main_view);
furi_assert(callback);
main_view->callback = callback;
main_view->context = context;
}
View* desktop_main_get_view(DesktopMainView* main_view) {
furi_assert(main_view);
return main_view->view;
}
bool desktop_main_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
DesktopMainView* main_view = context;
if(event->type == InputTypeShort) {
if(event->key == InputKeyOk) {
main_view->callback(DesktopMainEventOpenMenu, main_view->context);
} else if(event->key == InputKeyUp) {
main_view->callback(DesktopMainEventOpenLockMenu, main_view->context);
} else if(event->key == InputKeyDown) {
main_view->callback(DesktopMainEventOpenArchive, main_view->context);
} else if(event->key == InputKeyLeft) {
main_view->callback(DesktopMainEventOpenFavoritePrimary, main_view->context);
} else if(event->key == InputKeyRight) {
main_view->callback(DesktopMainEventOpenPassport, main_view->context);
}
} else if(event->type == InputTypeLong) {
if(event->key == InputKeyDown) {
main_view->callback(DesktopMainEventOpenDebug, main_view->context);
} else if(event->key == InputKeyLeft) {
main_view->callback(DesktopMainEventOpenFavoriteSecondary, main_view->context);
}
}
if(event->key == InputKeyBack) {
if(event->type == InputTypePress) {
xTimerChangePeriod(
main_view->poweroff_timer,
pdMS_TO_TICKS(DESKTOP_MAIN_VIEW_POWEROFF_TIMEOUT),
portMAX_DELAY);
} else if(event->type == InputTypeRelease) {
xTimerStop(main_view->poweroff_timer, portMAX_DELAY);
}
}
return true;
}
DesktopMainView* desktop_main_alloc() {
DesktopMainView* main_view = malloc(sizeof(DesktopMainView));
main_view->view = view_alloc();
view_allocate_model(main_view->view, ViewModelTypeLockFree, 1);
view_set_context(main_view->view, main_view);
view_set_input_callback(main_view->view, desktop_main_input);
main_view->poweroff_timer = xTimerCreate(
NULL,
pdMS_TO_TICKS(DESKTOP_MAIN_VIEW_POWEROFF_TIMEOUT),
pdFALSE,
main_view,
desktop_main_poweroff_timer_callback);
return main_view;
}
void desktop_main_free(DesktopMainView* main_view) {
furi_assert(main_view);
view_free(main_view->view);
furi_timer_free(main_view->poweroff_timer);
free(main_view);
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include <gui/view.h>
#include "desktop_events.h"
typedef struct DesktopMainView DesktopMainView;
typedef void (*DesktopMainViewCallback)(DesktopEvent event, void* context);
void desktop_main_set_callback(
DesktopMainView* main_view,
DesktopMainViewCallback callback,
void* context);
View* desktop_main_get_view(DesktopMainView* main_view);
DesktopMainView* desktop_main_alloc();
void desktop_main_free(DesktopMainView* main_view);

View File

@@ -0,0 +1,340 @@
#include <gui/canvas.h>
#include <furi.h>
#include <gui/view.h>
#include <gui/elements.h>
#include <stdint.h>
#include <portmacro.h>
#include "desktop_view_pin_input.h"
#include <desktop/desktop_settings.h>
#define NO_ACTIVITY_TIMEOUT 15000
#define PIN_CELL_WIDTH 13
#define DEFAULT_PIN_X 64
#define DEFAULT_PIN_Y 32
struct DesktopViewPinInput {
View* view;
DesktopViewPinInputCallback back_callback;
DesktopViewPinInputCallback timeout_callback;
DesktopViewPinInputDoneCallback done_callback;
void* context;
TimerHandle_t timer;
};
typedef struct {
PinCode pin;
bool pin_hidden;
bool locked_input;
uint8_t pin_x;
uint8_t pin_y;
const char* primary_str;
uint8_t primary_str_x;
uint8_t primary_str_y;
const char* secondary_str;
uint8_t secondary_str_x;
uint8_t secondary_str_y;
const char* button_label;
} DesktopViewPinInputModel;
static bool desktop_view_pin_input_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
DesktopViewPinInput* pin_input = context;
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
bool call_back_callback = false;
bool call_done_callback = false;
PinCode pin_code = {0};
if(event->type == InputTypeShort) {
switch(event->key) {
case InputKeyRight:
case InputKeyLeft:
case InputKeyDown:
case InputKeyUp:
if(!model->locked_input) {
if(model->pin.length < MAX_PIN_SIZE) {
model->pin.data[model->pin.length++] = event->key;
}
}
break;
case InputKeyOk:
if(model->pin.length >= MIN_PIN_SIZE) {
call_done_callback = true;
pin_code = model->pin;
}
break;
case InputKeyBack:
if(!model->locked_input) {
if(model->pin.length > 0) {
model->pin.length = 0;
} else {
call_back_callback = true;
}
}
break;
default:
furi_assert(0);
break;
}
}
view_commit_model(pin_input->view, true);
if(call_done_callback && pin_input->done_callback) {
pin_input->done_callback(&pin_code, pin_input->context);
} else if(call_back_callback && pin_input->back_callback) {
pin_input->back_callback(pin_input->context);
}
xTimerStart(pin_input->timer, 0);
return true;
}
static void desktop_view_pin_input_draw_cells(Canvas* canvas, DesktopViewPinInputModel* model) {
furi_assert(canvas);
furi_assert(model);
uint8_t draw_pin_size = MAX(4, model->pin.length + 1);
if(model->locked_input || (model->pin.length == MAX_PIN_SIZE)) {
draw_pin_size = model->pin.length;
}
uint8_t x = model->pin_x - (draw_pin_size * (PIN_CELL_WIDTH - 1)) / 2;
uint8_t y = model->pin_y - (PIN_CELL_WIDTH / 2);
for(int i = 0; i < draw_pin_size; ++i) {
canvas_draw_frame(canvas, x, y, PIN_CELL_WIDTH, PIN_CELL_WIDTH);
if(i < model->pin.length) {
if(model->pin_hidden) {
canvas_draw_icon(canvas, x + 3, y + 3, &I_Pin_star_7x7);
} else {
switch(model->pin.data[i]) {
case InputKeyDown:
canvas_draw_icon(canvas, x + 3, y + 2, &I_Pin_arrow_down_7x9);
break;
case InputKeyUp:
canvas_draw_icon(canvas, x + 3, y + 2, &I_Pin_arrow_up7x9);
break;
case InputKeyLeft:
canvas_draw_icon(canvas, x + 2, y + 3, &I_Pin_arrow_left_9x7);
break;
case InputKeyRight:
canvas_draw_icon(canvas, x + 2, y + 3, &I_Pin_arrow_right_9x7);
break;
default:
furi_assert(0);
break;
}
}
} else if(i == model->pin.length) {
canvas_draw_icon(canvas, x + 4, y + PIN_CELL_WIDTH + 1, &I_Pin_pointer_5x3);
}
x += PIN_CELL_WIDTH - 1;
}
}
static void desktop_view_pin_input_draw(Canvas* canvas, void* context) {
furi_assert(canvas);
furi_assert(context);
canvas_set_font(canvas, FontSecondary);
DesktopViewPinInputModel* model = context;
desktop_view_pin_input_draw_cells(canvas, model);
if((model->pin.length > 0) && !model->locked_input) {
canvas_draw_icon(canvas, 4, 53, &I_Pin_back_full_40x8);
}
if(model->button_label && ((model->pin.length >= MIN_PIN_SIZE) || model->locked_input)) {
elements_button_center(canvas, model->button_label);
}
if(model->primary_str) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, model->primary_str_x, model->primary_str_y, model->primary_str);
canvas_set_font(canvas, FontSecondary);
}
if(model->secondary_str) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(
canvas, model->secondary_str_x, model->secondary_str_y, model->secondary_str);
}
}
void desktop_view_pin_input_timer_callback(TimerHandle_t timer) {
DesktopViewPinInput* pin_input = pvTimerGetTimerID(timer);
if(pin_input->timeout_callback) {
pin_input->timeout_callback(pin_input->context);
}
}
static void desktop_view_pin_input_enter(void* context) {
DesktopViewPinInput* pin_input = context;
xTimerStart(pin_input->timer, portMAX_DELAY);
}
static void desktop_view_pin_input_exit(void* context) {
DesktopViewPinInput* pin_input = context;
xTimerStop(pin_input->timer, portMAX_DELAY);
}
DesktopViewPinInput* desktop_view_pin_input_alloc(void) {
DesktopViewPinInput* pin_input = malloc(sizeof(DesktopViewPinInput));
pin_input->view = view_alloc();
view_allocate_model(pin_input->view, ViewModelTypeLocking, sizeof(DesktopViewPinInputModel));
view_set_context(pin_input->view, pin_input);
view_set_draw_callback(pin_input->view, desktop_view_pin_input_draw);
view_set_input_callback(pin_input->view, desktop_view_pin_input_input);
pin_input->timer = xTimerCreate(
NULL,
pdMS_TO_TICKS(NO_ACTIVITY_TIMEOUT),
pdFALSE,
pin_input,
desktop_view_pin_input_timer_callback);
view_set_enter_callback(pin_input->view, desktop_view_pin_input_enter);
view_set_exit_callback(pin_input->view, desktop_view_pin_input_exit);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->pin_x = DEFAULT_PIN_X;
model->pin_y = DEFAULT_PIN_Y;
model->pin.length = 0;
view_commit_model(pin_input->view, false);
return pin_input;
}
void desktop_view_pin_input_free(DesktopViewPinInput* pin_input) {
furi_assert(pin_input);
xTimerStop(pin_input->timer, portMAX_DELAY);
while(xTimerIsTimerActive(pin_input->timer)) {
furi_delay_tick(1);
}
xTimerDelete(pin_input->timer, portMAX_DELAY);
view_free(pin_input->view);
free(pin_input);
}
void desktop_view_pin_input_lock_input(DesktopViewPinInput* pin_input) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->locked_input = true;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_unlock_input(DesktopViewPinInput* pin_input) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->locked_input = false;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_set_pin(DesktopViewPinInput* pin_input, const PinCode* pin) {
furi_assert(pin_input);
furi_assert(pin);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->pin = *pin;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_reset_pin(DesktopViewPinInput* pin_input) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->pin.length = 0;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_hide_pin(DesktopViewPinInput* pin_input, bool pin_hidden) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->pin_hidden = pin_hidden;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_set_label_button(DesktopViewPinInput* pin_input, const char* label) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->button_label = label;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_set_label_primary(
DesktopViewPinInput* pin_input,
uint8_t x,
uint8_t y,
const char* label) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->primary_str = label;
model->primary_str_x = x;
model->primary_str_y = y;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_set_label_secondary(
DesktopViewPinInput* pin_input,
uint8_t x,
uint8_t y,
const char* label) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->secondary_str = label;
model->secondary_str_x = x;
model->secondary_str_y = y;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_set_pin_position(DesktopViewPinInput* pin_input, uint8_t x, uint8_t y) {
furi_assert(pin_input);
DesktopViewPinInputModel* model = view_get_model(pin_input->view);
model->pin_x = x;
model->pin_y = y;
view_commit_model(pin_input->view, true);
}
void desktop_view_pin_input_set_context(DesktopViewPinInput* pin_input, void* context) {
furi_assert(pin_input);
pin_input->context = context;
}
void desktop_view_pin_input_set_timeout_callback(
DesktopViewPinInput* pin_input,
DesktopViewPinInputCallback callback) {
furi_assert(pin_input);
pin_input->timeout_callback = callback;
}
void desktop_view_pin_input_set_back_callback(
DesktopViewPinInput* pin_input,
DesktopViewPinInputCallback callback) {
furi_assert(pin_input);
pin_input->back_callback = callback;
}
void desktop_view_pin_input_set_done_callback(
DesktopViewPinInput* pin_input,
DesktopViewPinInputDoneCallback callback) {
furi_assert(pin_input);
pin_input->done_callback = callback;
}
View* desktop_view_pin_input_get_view(DesktopViewPinInput* pin_input) {
furi_assert(pin_input);
return pin_input->view;
}

View File

@@ -0,0 +1,40 @@
#pragma once
#include <gui/view.h>
#include <desktop/desktop_settings.h>
typedef void (*DesktopViewPinInputCallback)(void*);
typedef void (*DesktopViewPinInputDoneCallback)(const PinCode* pin_code, void*);
typedef struct DesktopViewPinInput DesktopViewPinInput;
DesktopViewPinInput* desktop_view_pin_input_alloc(void);
void desktop_view_pin_input_free(DesktopViewPinInput*);
void desktop_view_pin_input_set_pin(DesktopViewPinInput* pin_input, const PinCode* pin);
void desktop_view_pin_input_reset_pin(DesktopViewPinInput* pin_input);
void desktop_view_pin_input_hide_pin(DesktopViewPinInput* pin_input, bool pin_hidden);
void desktop_view_pin_input_set_label_button(DesktopViewPinInput* pin_input, const char* label);
void desktop_view_pin_input_set_label_primary(
DesktopViewPinInput* pin_input,
uint8_t x,
uint8_t y,
const char* label);
void desktop_view_pin_input_set_label_secondary(
DesktopViewPinInput* pin_input,
uint8_t x,
uint8_t y,
const char* label);
void desktop_view_pin_input_set_pin_position(DesktopViewPinInput* pin_input, uint8_t x, uint8_t y);
View* desktop_view_pin_input_get_view(DesktopViewPinInput*);
void desktop_view_pin_input_set_done_callback(
DesktopViewPinInput* pin_input,
DesktopViewPinInputDoneCallback callback);
void desktop_view_pin_input_set_back_callback(
DesktopViewPinInput* pin_input,
DesktopViewPinInputCallback callback);
void desktop_view_pin_input_set_timeout_callback(
DesktopViewPinInput* pin_input,
DesktopViewPinInputCallback callback);
void desktop_view_pin_input_set_context(DesktopViewPinInput* pin_input, void* context);
void desktop_view_pin_input_lock_input(DesktopViewPinInput* pin_input);
void desktop_view_pin_input_unlock_input(DesktopViewPinInput* pin_input);

View File

@@ -0,0 +1,80 @@
#include <furi.h>
#include <furi_hal.h>
#include <gui/elements.h>
#include <gui/canvas.h>
#include <toolbox/version.h>
#include <assets_icons.h>
#include <dolphin/helpers/dolphin_state.h>
#include <dolphin/dolphin.h>
#include "../desktop_i.h"
#include "desktop_view_pin_setup_done.h"
struct DesktopViewPinSetupDone {
View* view;
DesktopViewPinSetupDoneDoneCallback callback;
void* context;
};
static void desktop_view_pin_done_draw(Canvas* canvas, void* model) {
furi_assert(canvas);
furi_assert(model);
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_aligned(
canvas, 64, 0, AlignCenter, AlignTop, "Prepare to use\narrows as\nPIN symbols");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text(canvas, 58, 24, "Prepare to use\narrows as\nPIN symbols");
canvas_draw_icon(canvas, 16, 18, &I_Pin_attention_dpad_29x29);
elements_button_right(canvas, "Next");
}
static bool desktop_view_pin_done_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
DesktopViewPinSetupDone* instance = context;
bool consumed = false;
if((event->key == InputKeyRight) && (event->type == InputTypeShort)) {
instance->callback(instance->context);
consumed = true;
}
return consumed;
}
void desktop_view_pin_done_set_callback(
DesktopViewPinSetupDone* instance,
DesktopViewPinSetupDoneDoneCallback callback,
void* context) {
furi_assert(instance);
furi_assert(callback);
instance->callback = callback;
instance->context = context;
}
DesktopViewPinSetupDone* desktop_view_pin_done_alloc() {
DesktopViewPinSetupDone* view = malloc(sizeof(DesktopViewPinSetupDone));
view->view = view_alloc();
view_allocate_model(view->view, ViewModelTypeLockFree, 1);
view_set_context(view->view, view);
view_set_draw_callback(view->view, desktop_view_pin_done_draw);
view_set_input_callback(view->view, desktop_view_pin_done_input);
return view;
}
void desktop_view_pin_done_free(DesktopViewPinSetupDone* instance) {
furi_assert(instance);
view_free(instance->view);
free(instance);
}
View* desktop_view_pin_done_get_view(DesktopViewPinSetupDone* instance) {
furi_assert(instance);
return instance->view;
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <gui/view.h>
typedef struct DesktopViewPinSetupDone DesktopViewPinSetupDone;
typedef void (*DesktopViewPinSetupDoneDoneCallback)(void*);
void desktop_view_pin_done_set_callback(
DesktopViewPinSetupDone* instance,
DesktopViewPinSetupDoneDoneCallback callback,
void* context);
DesktopViewPinSetupDone* desktop_view_pin_done_alloc();
void desktop_view_pin_done_free(DesktopViewPinSetupDone* instance);
View* desktop_view_pin_done_get_view(DesktopViewPinSetupDone* instance);

View File

@@ -0,0 +1,111 @@
#include <furi.h>
#include <stdint.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <portmacro.h>
#include <projdefs.h>
#include <input/input.h>
#include <gui/canvas.h>
#include <gui/view.h>
#include "desktop_view_pin_timeout.h"
struct DesktopViewPinTimeout {
View* view;
TimerHandle_t timer;
DesktopViewPinTimeoutDoneCallback callback;
void* context;
};
typedef struct {
uint32_t time_left;
} DesktopViewPinTimeoutModel;
void desktop_view_pin_timeout_set_callback(
DesktopViewPinTimeout* instance,
DesktopViewPinTimeoutDoneCallback callback,
void* context) {
furi_assert(instance);
instance->callback = callback;
instance->context = context;
}
static void desktop_view_pin_timeout_timer_callback(TimerHandle_t timer) {
DesktopViewPinTimeout* instance = pvTimerGetTimerID(timer);
bool stop = false;
DesktopViewPinTimeoutModel* model = view_get_model(instance->view);
if(model->time_left > 0) {
--model->time_left;
} else {
stop = true;
}
view_commit_model(instance->view, true);
if(stop) {
xTimerStop(instance->timer, portMAX_DELAY);
instance->callback(instance->context);
}
}
static bool desktop_view_pin_timeout_input(InputEvent* event, void* context) {
UNUSED(event);
UNUSED(context);
return true;
}
static void desktop_view_pin_timeout_draw(Canvas* canvas, void* _model) {
furi_assert(canvas);
furi_assert(_model);
DesktopViewPinTimeoutModel* model = _model;
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 36, 31, "Wrong PIN!");
canvas_set_font(canvas, FontSecondary);
char str[30] = {0};
snprintf(str, sizeof(str), "Timeout: %lds", model->time_left);
canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, str);
}
void desktop_view_pin_timeout_free(DesktopViewPinTimeout* instance) {
view_free(instance->view);
xTimerDelete(instance->timer, portMAX_DELAY);
free(instance);
}
DesktopViewPinTimeout* desktop_view_pin_timeout_alloc(void) {
DesktopViewPinTimeout* instance = malloc(sizeof(DesktopViewPinTimeout));
instance->timer = xTimerCreate(
NULL, pdMS_TO_TICKS(1000), pdTRUE, instance, desktop_view_pin_timeout_timer_callback);
instance->view = view_alloc();
view_allocate_model(instance->view, ViewModelTypeLockFree, sizeof(DesktopViewPinTimeoutModel));
view_set_context(instance->view, instance);
view_set_draw_callback(instance->view, desktop_view_pin_timeout_draw);
view_set_input_callback(instance->view, desktop_view_pin_timeout_input);
return instance;
}
void desktop_view_pin_timeout_start(DesktopViewPinTimeout* instance, uint32_t time_left) {
furi_assert(instance);
DesktopViewPinTimeoutModel* model = view_get_model(instance->view);
// no race - always called when timer is stopped
model->time_left = time_left;
view_commit_model(instance->view, true);
xTimerStart(instance->timer, portMAX_DELAY);
}
View* desktop_view_pin_timeout_get_view(DesktopViewPinTimeout* instance) {
furi_assert(instance);
return instance->view;
}

View File

@@ -0,0 +1,16 @@
#pragma once
#include <stdint.h>
#include <gui/view.h>
typedef void (*DesktopViewPinTimeoutDoneCallback)(void*);
typedef struct DesktopViewPinTimeout DesktopViewPinTimeout;
void desktop_view_pin_timeout_set_callback(
DesktopViewPinTimeout* instance,
DesktopViewPinTimeoutDoneCallback callback,
void* context);
DesktopViewPinTimeout* desktop_view_pin_timeout_alloc(void);
void desktop_view_pin_timeout_free(DesktopViewPinTimeout*);
void desktop_view_pin_timeout_start(DesktopViewPinTimeout* instance, uint32_t time_left);
View* desktop_view_pin_timeout_get_view(DesktopViewPinTimeout* instance);

View File

@@ -0,0 +1,141 @@
#include <furi.h>
#include <furi_hal.h>
#include <gui/elements.h>
#include "desktop_view_slideshow.h"
#include "../desktop_i.h"
#include "../helpers/slideshow.h"
#include "../helpers/slideshow_filename.h"
#define DESKTOP_SLIDESHOW_POWEROFF_SHORT 5000
#define DESKTOP_SLIDESHOW_POWEROFF_LONG (60 * 60 * 1000)
struct DesktopSlideshowView {
View* view;
DesktopSlideshowViewCallback callback;
void* context;
FuriTimer* timer;
};
typedef struct {
uint8_t page;
Slideshow* slideshow;
} DesktopSlideshowViewModel;
static void desktop_view_slideshow_draw(Canvas* canvas, void* model) {
DesktopSlideshowViewModel* m = model;
canvas_clear(canvas);
if(slideshow_is_loaded(m->slideshow)) {
slideshow_draw(m->slideshow, canvas, 0, 0);
}
}
static bool desktop_view_slideshow_input(InputEvent* event, void* context) {
furi_assert(event);
DesktopSlideshowView* instance = context;
DesktopSlideshowViewModel* model = view_get_model(instance->view);
bool update_view = false;
if(event->type == InputTypeShort) {
bool end_slideshow = false;
switch(event->key) {
case InputKeyLeft:
slideshow_goback(model->slideshow);
break;
case InputKeyRight:
case InputKeyOk:
end_slideshow = !slideshow_advance(model->slideshow);
break;
case InputKeyBack:
end_slideshow = true;
default:
break;
}
if(end_slideshow) {
instance->callback(DesktopSlideshowCompleted, instance->context);
}
update_view = true;
} else if(event->key == InputKeyOk) {
if(event->type == InputTypePress) {
furi_timer_start(instance->timer, DESKTOP_SLIDESHOW_POWEROFF_SHORT);
} else if(event->type == InputTypeRelease) {
furi_timer_stop(instance->timer);
if(!slideshow_is_one_page(model->slideshow)) {
furi_timer_start(instance->timer, DESKTOP_SLIDESHOW_POWEROFF_LONG);
}
}
}
view_commit_model(instance->view, update_view);
return true;
}
static void desktop_first_start_timer_callback(void* context) {
DesktopSlideshowView* instance = context;
instance->callback(DesktopSlideshowPoweroff, instance->context);
}
static void desktop_view_slideshow_enter(void* context) {
DesktopSlideshowView* instance = context;
furi_assert(instance->timer == NULL);
instance->timer =
furi_timer_alloc(desktop_first_start_timer_callback, FuriTimerTypeOnce, instance);
DesktopSlideshowViewModel* model = view_get_model(instance->view);
model->slideshow = slideshow_alloc();
if(!slideshow_load(model->slideshow, SLIDESHOW_FS_PATH)) {
instance->callback(DesktopSlideshowCompleted, instance->context);
} else if(!slideshow_is_one_page(model->slideshow)) {
furi_timer_start(instance->timer, DESKTOP_SLIDESHOW_POWEROFF_LONG);
}
view_commit_model(instance->view, false);
}
static void desktop_view_slideshow_exit(void* context) {
DesktopSlideshowView* instance = context;
furi_timer_stop(instance->timer);
furi_timer_free(instance->timer);
instance->timer = NULL;
DesktopSlideshowViewModel* model = view_get_model(instance->view);
slideshow_free(model->slideshow);
view_commit_model(instance->view, false);
}
DesktopSlideshowView* desktop_view_slideshow_alloc() {
DesktopSlideshowView* instance = malloc(sizeof(DesktopSlideshowView));
instance->view = view_alloc();
view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(DesktopSlideshowViewModel));
view_set_context(instance->view, instance);
view_set_draw_callback(instance->view, (ViewDrawCallback)desktop_view_slideshow_draw);
view_set_input_callback(instance->view, desktop_view_slideshow_input);
view_set_enter_callback(instance->view, desktop_view_slideshow_enter);
view_set_exit_callback(instance->view, desktop_view_slideshow_exit);
return instance;
}
void desktop_view_slideshow_free(DesktopSlideshowView* instance) {
furi_assert(instance);
view_free(instance->view);
free(instance);
}
View* desktop_view_slideshow_get_view(DesktopSlideshowView* instance) {
furi_assert(instance);
return instance->view;
}
void desktop_view_slideshow_set_callback(
DesktopSlideshowView* instance,
DesktopSlideshowViewCallback callback,
void* context) {
furi_assert(instance);
furi_assert(callback);
instance->callback = callback;
instance->context = context;
}

View File

@@ -0,0 +1,23 @@
#pragma once
#include <gui/view.h>
#include "desktop_events.h"
#include "../helpers/slideshow_filename.h"
#define SLIDESHOW_FS_PATH INT_PATH(SLIDESHOW_FILE_NAME)
typedef struct DesktopSlideshowView DesktopSlideshowView;
typedef void (*DesktopSlideshowViewCallback)(DesktopEvent event, void* context);
DesktopSlideshowView* desktop_view_slideshow_alloc();
void desktop_view_slideshow_free(DesktopSlideshowView* main_view);
View* desktop_view_slideshow_get_view(DesktopSlideshowView* main_view);
void desktop_view_slideshow_set_callback(
DesktopSlideshowView* main_view,
DesktopSlideshowViewCallback callback,
void* context);