Skip to content

Commit

Permalink
Merge pull request #8 from ESurge/squach
Browse files Browse the repository at this point in the history
  • Loading branch information
TalkingSasquach authored Jan 7, 2023
2 parents 90e5f18 + 8db5a55 commit b73117f
Show file tree
Hide file tree
Showing 25 changed files with 764 additions and 1 deletion.
13 changes: 13 additions & 0 deletions applications/plugins/namechanger/application.fam
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
App(
appid="NameChanger",
name="Name Changer",
apptype=FlipperAppType.EXTERNAL,
entry_point="namechanger_app",
cdefines=["APP_NAMECHANGER"],
requires=["gui","storage"],
stack_size=2 * 1024,
order=90,
fap_icon="namechanger_10px.png",
fap_category="Tools",
fap_icon_assets="icons",
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
157 changes: 157 additions & 0 deletions applications/plugins/namechanger/namechanger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "namechanger.h"
#include "scenes/namechanger_scene.h"

#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>

bool namechanger_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
NameChanger* namechanger = context;
return scene_manager_handle_custom_event(namechanger->scene_manager, event);
}

bool namechanger_back_event_callback(void* context) {
furi_assert(context);
NameChanger* namechanger = context;
return scene_manager_handle_back_event(namechanger->scene_manager);
}

NameChanger* namechanger_alloc() {
NameChanger* namechanger = malloc(sizeof(NameChanger));

namechanger->scene_manager = scene_manager_alloc(&namechanger_scene_handlers, namechanger);

namechanger->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(namechanger->view_dispatcher);
view_dispatcher_set_event_callback_context(namechanger->view_dispatcher, namechanger);
view_dispatcher_set_custom_event_callback(
namechanger->view_dispatcher, namechanger_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
namechanger->view_dispatcher, namechanger_back_event_callback);

namechanger->gui = furi_record_open(RECORD_GUI);
namechanger->storage = furi_record_open(RECORD_STORAGE);

namechanger->submenu = submenu_alloc();
view_dispatcher_add_view(
namechanger->view_dispatcher,
NameChangerViewSubmenu,
submenu_get_view(namechanger->submenu));

namechanger->text_input = text_input_alloc();
view_dispatcher_add_view(
namechanger->view_dispatcher,
NameChangerViewTextInput,
text_input_get_view(namechanger->text_input));

namechanger->popup = popup_alloc();
view_dispatcher_add_view(
namechanger->view_dispatcher, NameChangerViewPopup, popup_get_view(namechanger->popup));

namechanger->widget = widget_alloc();
view_dispatcher_add_view(
namechanger->view_dispatcher, NameChangerViewWidget, widget_get_view(namechanger->widget));

return namechanger;
}

void namechanger_free(NameChanger* namechanger) {
furi_assert(namechanger);

view_dispatcher_remove_view(namechanger->view_dispatcher, NameChangerViewWidget);
widget_free(namechanger->widget);
view_dispatcher_remove_view(namechanger->view_dispatcher, NameChangerViewPopup);
popup_free(namechanger->popup);

view_dispatcher_remove_view(namechanger->view_dispatcher, NameChangerViewTextInput);
text_input_free(namechanger->text_input);

view_dispatcher_remove_view(namechanger->view_dispatcher, NameChangerViewSubmenu);
submenu_free(namechanger->submenu);

view_dispatcher_free(namechanger->view_dispatcher);
scene_manager_free(namechanger->scene_manager);

furi_record_close(RECORD_STORAGE);

furi_record_close(RECORD_GUI);

free(namechanger);
}

void namechanger_text_store_set(NameChanger* namechanger, const char* text, ...) {
va_list args;
va_start(args, text);

vsnprintf(namechanger->text_store, NAMECHANGER_TEXT_STORE_SIZE, text, args);

va_end(args);
}

int32_t namechanger_app() {
NameChanger* namechanger = namechanger_alloc();

view_dispatcher_attach_to_gui(namechanger->view_dispatcher, namechanger->gui, ViewDispatcherTypeFullscreen);
scene_manager_next_scene(namechanger->scene_manager, NameChangerSceneStart);

view_dispatcher_run(namechanger->view_dispatcher);

namechanger_free(namechanger);
return 0;
}

bool namechanger_name_write(NameChanger* namechanger, char* name) {
FuriString* file_path = furi_string_alloc();
furi_string_set(file_path, "/ext/dolphin/name.txt");

bool result = false;

//If name is not "eraseerase" (set by Revert) then write name to file
//otherwise, remove name.txt

if(strcmp(name, "eraseerase") != 0) {
//save
FlipperFormat* file = flipper_format_file_alloc(namechanger->storage);

do {
// Open file for write
if(!flipper_format_file_open_always(file, furi_string_get_cstr(file_path))) {
break;
}

// Write header
if(!flipper_format_write_header_cstr(file, NAMECHANGER_HEADER, 1)) {
break;
}

// Write comments
if(!flipper_format_write_comment_cstr(file, "Changing the value below will change your FlipperZero device name.")) {
break;
}

if(!flipper_format_write_comment_cstr(file, "Note: This is limited to 8 characters using the following: a-z, A-Z, 0-9, and _")) {
break;
}

if(!flipper_format_write_comment_cstr(file, "It cannot contain any other characters.")) {
break;
}

if(!flipper_format_write_string_cstr(file, "Name", name)) {
break;
}

result = true;
} while(false);

flipper_format_free(file);

if(!result) {
FURI_LOG_E(TAG, "Cannot save name file.");
}
} else {
result = storage_simply_remove(namechanger->storage, furi_string_get_cstr(file_path));
}

return result;
}
49 changes: 49 additions & 0 deletions applications/plugins/namechanger/namechanger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <furi_hal.h>
#include <gui/gui.h>
#include <gui/view.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <storage/storage.h>

#include <NameChanger_icons.h>

#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include <gui/modules/text_input.h>
#include <gui/modules/widget.h>

#include "namechanger_custom_event.h"
#include "scenes/namechanger_scene.h"

#define NAMECHANGER_TEXT_STORE_SIZE 9
#define NAMECHANGER_HEADER "Flipper Name File"

#define TAG "NameChanger"

typedef struct {
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;

Gui* gui;
Storage* storage;

char text_store[NAMECHANGER_TEXT_STORE_SIZE + 1];

Submenu* submenu;
TextInput* text_input;
Popup* popup;
Widget* widget;
} NameChanger;

typedef enum {
NameChangerViewSubmenu,
NameChangerViewTextInput,
NameChangerViewPopup,
NameChangerViewWidget,
} NameChangerView;

bool namechanger_make_app_folder(NameChanger* namechanger);
bool namechanger_name_write(NameChanger* namechanger, char* name);
void namechanger_text_store_set(NameChanger* namechanger, const char* text, ...);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions applications/plugins/namechanger/namechanger_custom_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

enum NameChangerCustomEvent {
NameChangerCustomEventBack,
NameChangerCustomEventTextEditResult,
};
30 changes: 30 additions & 0 deletions applications/plugins/namechanger/scenes/namechanger_scene.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "namechanger_scene.h"

// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const namechanger_on_enter_handlers[])(void*) = {
#include "namechanger_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 namechanger_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "namechanger_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 namechanger_on_exit_handlers[])(void* context) = {
#include "namechanger_scene_config.h"
};
#undef ADD_SCENE

// Initialize scene handlers configuration structure
const SceneManagerHandlers namechanger_scene_handlers = {
.on_enter_handlers = namechanger_on_enter_handlers,
.on_event_handlers = namechanger_on_event_handlers,
.on_exit_handlers = namechanger_on_exit_handlers,
.scene_num = NameChangerSceneNum,
};
29 changes: 29 additions & 0 deletions applications/plugins/namechanger/scenes/namechanger_scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <gui/scene_manager.h>

// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) NameChangerScene##id,
typedef enum {
#include "namechanger_scene_config.h"
NameChangerSceneNum,
} NameChangerScene;
#undef ADD_SCENE

extern const SceneManagerHandlers namechanger_scene_handlers;

// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "namechanger_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 "namechanger_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 "namechanger_scene_config.h"
#undef ADD_SCENE
53 changes: 53 additions & 0 deletions applications/plugins/namechanger/scenes/namechanger_scene_change.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "../namechanger.h"

static void namechanger_scene_change_text_input_callback(void* context) {
NameChanger* namechanger = context;

view_dispatcher_send_custom_event(
namechanger->view_dispatcher, NameChangerCustomEventTextEditResult);
}

void namechanger_scene_change_on_enter(void* context) {
NameChanger* namechanger = context;
TextInput* text_input = namechanger->text_input;

namechanger_text_store_set(namechanger, "%s", furi_hal_version_get_name_ptr());

text_input_set_header_text(text_input, "Set Flipper Name");

text_input_set_result_callback(
text_input,
namechanger_scene_change_text_input_callback,
namechanger,
namechanger->text_store,
NAMECHANGER_TEXT_STORE_SIZE,
true);

view_dispatcher_switch_to_view(namechanger->view_dispatcher, NameChangerViewTextInput);
}

bool namechanger_scene_change_on_event(void* context, SceneManagerEvent event) {
NameChanger* namechanger = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == NameChangerCustomEventTextEditResult) {
if(namechanger_name_write(namechanger, namechanger->text_store)) {
scene_manager_next_scene(namechanger->scene_manager, NameChangerSceneChangeSuccess);
} else {
scene_manager_search_and_switch_to_previous_scene(namechanger->scene_manager, NameChangerSceneStart);
}
} else {
scene_manager_search_and_switch_to_previous_scene(namechanger->scene_manager, NameChangerSceneStart);
}
}
return consumed;
}

void namechanger_scene_change_on_exit(void* context) {
NameChanger* namechanger = context;
TextInput* text_input = namechanger->text_input;

text_input_reset(text_input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "../namechanger.h"

static void namechanger_scene_change_success_popup_callback(void* context) {
NameChanger* namechanger = context;
view_dispatcher_send_custom_event(namechanger->view_dispatcher, NameChangerCustomEventBack);
}

void namechanger_scene_change_success_on_enter(void* context) {
NameChanger* namechanger = context;
Popup* popup = namechanger->popup;

popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
popup_set_header(popup, "Saved!", 5, 5, AlignLeft, AlignTop);
popup_set_text(popup, "Rebooting...", 5, 17, AlignLeft, AlignTop);

popup_set_callback(popup, namechanger_scene_change_success_popup_callback);
popup_set_context(popup, namechanger);
popup_set_timeout(popup, 5000);
popup_enable_timeout(popup);

view_dispatcher_switch_to_view(namechanger->view_dispatcher, NameChangerViewPopup);
}

bool namechanger_scene_change_success_on_event(void* context, SceneManagerEvent event) {
NameChanger* namechanger = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == NameChangerCustomEventBack) {
scene_manager_search_and_switch_to_previous_scene(
namechanger->scene_manager, NameChangerSceneChange);
}
}

return consumed;
}

void namechanger_scene_change_success_on_exit(void* context) {
NameChanger* namechanger = context;
Popup* popup = namechanger->popup;

popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);

popup_disable_timeout(popup);
popup_set_context(popup, NULL);
popup_set_callback(popup, NULL);

furi_hal_power_reset();
}
Loading

0 comments on commit b73117f

Please sign in to comment.