From 6cb9ad12f1f8ce7a718295eb081d4d8cc4bb6925 Mon Sep 17 00:00:00 2001 From: gushmazuko Date: Sun, 21 Apr 2024 02:14:46 +0200 Subject: [PATCH 1/2] Enhance Random Interval and Movement Functionality --- .../system/hid_app/views/hid_mouse_jiggler.c | 118 ++++++++++++------ .../system/hid_app/views/hid_mouse_jiggler.h | 2 +- 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/applications/system/hid_app/views/hid_mouse_jiggler.c b/applications/system/hid_app/views/hid_mouse_jiggler.c index 09c14c6688..fc3211ca9d 100644 --- a/applications/system/hid_app/views/hid_mouse_jiggler.c +++ b/applications/system/hid_app/views/hid_mouse_jiggler.c @@ -4,6 +4,8 @@ #include "hid_icons.h" +#include // Include standard library for rand() function + #define TAG "HidMouseJiggler" struct HidMouseJiggler { @@ -15,12 +17,12 @@ struct HidMouseJiggler { typedef struct { bool connected; bool running; - int interval_idx; + int min_interval; // Minimum interval for random range + int max_interval; // Maximum interval for random range uint8_t counter; HidTransport transport; } HidMouseJigglerModel; -const int intervals[6] = {500, 2000, 5000, 10000, 30000, 60000}; static void hid_mouse_jiggler_draw_callback(Canvas* canvas, void* context) { furi_assert(context); @@ -35,22 +37,24 @@ static void hid_mouse_jiggler_draw_callback(Canvas* canvas, void* context) { } } + // Title "Mouse Jiggler" canvas_set_font(canvas, FontPrimary); elements_multiline_text_aligned(canvas, 17, 2, AlignLeft, AlignTop, "Mouse Jiggler"); - // Timeout - elements_multiline_text(canvas, AlignLeft, 26, "Interval (ms):"); - canvas_set_font(canvas, FontSecondary); - if(model->interval_idx != 0) canvas_draw_icon(canvas, 74, 19, &I_ButtonLeft_4x7); - if(model->interval_idx != (int)COUNT_OF(intervals) - 1) - canvas_draw_icon(canvas, 80, 19, &I_ButtonRight_4x7); - FuriString* interval_str = furi_string_alloc_printf("%d", intervals[model->interval_idx]); - elements_multiline_text(canvas, 91, 26, furi_string_get_cstr(interval_str)); - furi_string_free(interval_str); + // Display the current min interval in minutes + canvas_set_font(canvas, FontSecondary); // Assuming there's a smaller font available + FuriString* min_interval_str = furi_string_alloc_printf("Min: %d min", model->min_interval); + elements_multiline_text_aligned(canvas, 0, 16, AlignLeft, AlignTop, furi_string_get_cstr(min_interval_str)); + furi_string_free(min_interval_str); + + // Display the current max interval in minutes + FuriString* max_interval_str = furi_string_alloc_printf("Max: %d min", model->max_interval); + elements_multiline_text_aligned(canvas, 0, 28, AlignLeft, AlignTop, furi_string_get_cstr(max_interval_str)); + furi_string_free(max_interval_str); + // "Press Start to jiggle" canvas_set_font(canvas, FontPrimary); - elements_multiline_text(canvas, AlignLeft, 40, "Press Start\nto jiggle"); - canvas_set_font(canvas, FontSecondary); + elements_multiline_text(canvas, AlignLeft, 50, "Press Start\nto jiggle"); // Ok canvas_draw_icon(canvas, 63, 30, &I_Space_65x18); @@ -80,10 +84,25 @@ static void hid_mouse_jiggler_timer_callback(void* context) { { if(model->running) { model->counter++; - hid_hal_mouse_move( - hid_mouse_jiggler->hid, - (model->counter % 2 == 0) ? MOUSE_MOVE_SHORT : -MOUSE_MOVE_SHORT, - 0); + + // Use the counter as a seed for random number generation + // This isn't as ideal as using the current time, but in the absence of time.h, + // it's a reasonable alternative. + srand(model->counter); + + // Generate a random interval in minutes and convert to milliseconds + int randomIntervalMinutes = model->min_interval + rand() % (model->max_interval - model->min_interval + 1); + + // Randomize the mouse movement distance and direction + int move_x = (rand() % 2001) - 1000; // Randomly between -1000 and 1000 + int move_y = (rand() % 2001) - 1000; // Randomly between -1000 and 1000 + + // Perform the mouse move with the randomized values + hid_hal_mouse_move(hid_mouse_jiggler->hid, move_x, move_y); + + // Restart timer with the new random interval + furi_timer_stop(hid_mouse_jiggler->timer); + furi_timer_start(hid_mouse_jiggler->timer, randomIntervalMinutes * 60000); } }, false); @@ -105,23 +124,49 @@ static bool hid_mouse_jiggler_input_callback(InputEvent* event, void* context) { hid_mouse_jiggler->view, HidMouseJigglerModel * model, { - if(event->type == InputTypePress && event->key == InputKeyOk) { - model->running = !model->running; - if(model->running) { - furi_timer_stop(hid_mouse_jiggler->timer); - furi_timer_start(hid_mouse_jiggler->timer, intervals[model->interval_idx]); - }; - consumed = true; - } - if(event->type == InputTypePress && event->key == InputKeyRight && !model->running && - model->interval_idx < (int)COUNT_OF(intervals) - 1) { - model->interval_idx++; - consumed = true; - } - if(event->type == InputTypePress && event->key == InputKeyLeft && !model->running && - model->interval_idx > 0) { - model->interval_idx--; - consumed = true; + if(event->type == InputTypePress) { + switch (event->key) { + case InputKeyOk: + model->running = !model->running; + if(model->running) { + furi_timer_stop(hid_mouse_jiggler->timer); + int randomIntervalMinutes = model->min_interval + rand() % (model->max_interval - model->min_interval + 1); + furi_timer_start(hid_mouse_jiggler->timer, randomIntervalMinutes * 60000); + } + consumed = true; + break; + + case InputKeyUp: + if(!model->running && model->min_interval < model->max_interval) { + model->min_interval++; // Increment min interval by 1 minute + } + consumed = true; + break; + + case InputKeyDown: + if(!model->running && model->min_interval > 1) { // Minimum 1 minute + model->min_interval--; // Decrement min interval by 1 minute + } + consumed = true; + break; + + case InputKeyRight: + if(!model->running && model->max_interval < 30) { // Maximum 30 minutes + model->max_interval++; // Increment max interval by 1 minute + } + consumed = true; + break; + + case InputKeyLeft: + if(!model->running && model->max_interval > model->min_interval + 1) { + model->max_interval--; // Decrement max interval by 1 minute + } + consumed = true; + break; + + default: + break; + } } }, true); @@ -149,8 +194,9 @@ HidMouseJiggler* hid_mouse_jiggler_alloc(Hid* hid) { hid_mouse_jiggler->view, HidMouseJigglerModel * model, { - model->transport = hid->transport; - model->interval_idx = 2; + // Initialize the min and max interval values + model->min_interval = 5; // 5 minutes + model->max_interval = 15; // 15 minutes }, true); diff --git a/applications/system/hid_app/views/hid_mouse_jiggler.h b/applications/system/hid_app/views/hid_mouse_jiggler.h index 025a863852..45e2f49ec3 100644 --- a/applications/system/hid_app/views/hid_mouse_jiggler.h +++ b/applications/system/hid_app/views/hid_mouse_jiggler.h @@ -2,7 +2,7 @@ #include -#define MOUSE_MOVE_SHORT 5 +// #define MOUSE_MOVE_SHORT 5 typedef struct Hid Hid; typedef struct HidMouseJiggler HidMouseJiggler; From 887f6a20fc704f3d0bd1eec1a92cc9172703d7a0 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Fri, 3 May 2024 21:28:18 +0300 Subject: [PATCH 2/2] We already using furi random, cleanup unused parts, format code --- .../system/hid_app/views/hid_mouse_jiggler.c | 106 +++++++++--------- .../system/hid_app/views/hid_mouse_jiggler.h | 2 - 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/applications/system/hid_app/views/hid_mouse_jiggler.c b/applications/system/hid_app/views/hid_mouse_jiggler.c index fc3211ca9d..e36fa09666 100644 --- a/applications/system/hid_app/views/hid_mouse_jiggler.c +++ b/applications/system/hid_app/views/hid_mouse_jiggler.c @@ -4,8 +4,6 @@ #include "hid_icons.h" -#include // Include standard library for rand() function - #define TAG "HidMouseJiggler" struct HidMouseJiggler { @@ -19,11 +17,9 @@ typedef struct { bool running; int min_interval; // Minimum interval for random range int max_interval; // Maximum interval for random range - uint8_t counter; HidTransport transport; } HidMouseJigglerModel; - static void hid_mouse_jiggler_draw_callback(Canvas* canvas, void* context) { furi_assert(context); HidMouseJigglerModel* model = context; @@ -44,12 +40,14 @@ static void hid_mouse_jiggler_draw_callback(Canvas* canvas, void* context) { // Display the current min interval in minutes canvas_set_font(canvas, FontSecondary); // Assuming there's a smaller font available FuriString* min_interval_str = furi_string_alloc_printf("Min: %d min", model->min_interval); - elements_multiline_text_aligned(canvas, 0, 16, AlignLeft, AlignTop, furi_string_get_cstr(min_interval_str)); + elements_multiline_text_aligned( + canvas, 0, 16, AlignLeft, AlignTop, furi_string_get_cstr(min_interval_str)); furi_string_free(min_interval_str); // Display the current max interval in minutes FuriString* max_interval_str = furi_string_alloc_printf("Max: %d min", model->max_interval); - elements_multiline_text_aligned(canvas, 0, 28, AlignLeft, AlignTop, furi_string_get_cstr(max_interval_str)); + elements_multiline_text_aligned( + canvas, 0, 28, AlignLeft, AlignTop, furi_string_get_cstr(max_interval_str)); furi_string_free(max_interval_str); // "Press Start to jiggle" @@ -83,15 +81,9 @@ static void hid_mouse_jiggler_timer_callback(void* context) { HidMouseJigglerModel * model, { if(model->running) { - model->counter++; - - // Use the counter as a seed for random number generation - // This isn't as ideal as using the current time, but in the absence of time.h, - // it's a reasonable alternative. - srand(model->counter); - // Generate a random interval in minutes and convert to milliseconds - int randomIntervalMinutes = model->min_interval + rand() % (model->max_interval - model->min_interval + 1); + int randomIntervalMinutes = + model->min_interval + rand() % (model->max_interval - model->min_interval + 1); // Randomize the mouse movement distance and direction int move_x = (rand() % 2001) - 1000; // Randomly between -1000 and 1000 @@ -125,47 +117,49 @@ static bool hid_mouse_jiggler_input_callback(InputEvent* event, void* context) { HidMouseJigglerModel * model, { if(event->type == InputTypePress) { - switch (event->key) { - case InputKeyOk: - model->running = !model->running; - if(model->running) { - furi_timer_stop(hid_mouse_jiggler->timer); - int randomIntervalMinutes = model->min_interval + rand() % (model->max_interval - model->min_interval + 1); - furi_timer_start(hid_mouse_jiggler->timer, randomIntervalMinutes * 60000); - } - consumed = true; - break; - - case InputKeyUp: - if(!model->running && model->min_interval < model->max_interval) { - model->min_interval++; // Increment min interval by 1 minute - } - consumed = true; - break; - - case InputKeyDown: - if(!model->running && model->min_interval > 1) { // Minimum 1 minute - model->min_interval--; // Decrement min interval by 1 minute - } - consumed = true; - break; - - case InputKeyRight: - if(!model->running && model->max_interval < 30) { // Maximum 30 minutes - model->max_interval++; // Increment max interval by 1 minute - } - consumed = true; - break; - - case InputKeyLeft: - if(!model->running && model->max_interval > model->min_interval + 1) { - model->max_interval--; // Decrement max interval by 1 minute - } - consumed = true; - break; - - default: - break; + switch(event->key) { + case InputKeyOk: + model->running = !model->running; + if(model->running) { + furi_timer_stop(hid_mouse_jiggler->timer); + int randomIntervalMinutes = + model->min_interval + + rand() % (model->max_interval - model->min_interval + 1); + furi_timer_start(hid_mouse_jiggler->timer, randomIntervalMinutes * 60000); + } + consumed = true; + break; + + case InputKeyUp: + if(!model->running && model->min_interval < model->max_interval) { + model->min_interval++; // Increment min interval by 1 minute + } + consumed = true; + break; + + case InputKeyDown: + if(!model->running && model->min_interval > 1) { // Minimum 1 minute + model->min_interval--; // Decrement min interval by 1 minute + } + consumed = true; + break; + + case InputKeyRight: + if(!model->running && model->max_interval < 30) { // Maximum 30 minutes + model->max_interval++; // Increment max interval by 1 minute + } + consumed = true; + break; + + case InputKeyLeft: + if(!model->running && model->max_interval > model->min_interval + 1) { + model->max_interval--; // Decrement max interval by 1 minute + } + consumed = true; + break; + + default: + break; } } }, @@ -195,7 +189,7 @@ HidMouseJiggler* hid_mouse_jiggler_alloc(Hid* hid) { HidMouseJigglerModel * model, { // Initialize the min and max interval values - model->min_interval = 5; // 5 minutes + model->min_interval = 2; // 2 minutes model->max_interval = 15; // 15 minutes }, true); diff --git a/applications/system/hid_app/views/hid_mouse_jiggler.h b/applications/system/hid_app/views/hid_mouse_jiggler.h index 45e2f49ec3..4361e0bde6 100644 --- a/applications/system/hid_app/views/hid_mouse_jiggler.h +++ b/applications/system/hid_app/views/hid_mouse_jiggler.h @@ -2,8 +2,6 @@ #include -// #define MOUSE_MOVE_SHORT 5 - typedef struct Hid Hid; typedef struct HidMouseJiggler HidMouseJiggler;