From: Mitchell Livingston Date: Tue, 18 Sep 2012 00:58:10 +0000 (+0000) Subject: #4201 Automatic limit can't be stopped X-Git-Tag: 2.70~35 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e46309a0fac8d4c07d353a9f37b2e22b1b0290a7;p=transmission #4201 Automatic limit can't be stopped --- diff --git a/libtransmission/session.c b/libtransmission/session.c index 04cc1fe57..9a010616e 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -1325,9 +1325,7 @@ useAltSpeed( tr_session * s, struct tr_turtle_info * t, * @param changed whether that's different from the previous minute */ static void -testTurtleTime( const struct tr_turtle_info * t, - bool * enabled, - bool * changed ) +testTurtleTime( const struct tr_turtle_info * t, bool * enabled ) { bool e; struct tm tm; @@ -1345,28 +1343,31 @@ testTurtleTime( const struct tr_turtle_info * t, e = tr_bitfieldHas( &t->minutes, minute_of_the_week ); if( enabled != NULL ) *enabled = e; +} - if( changed != NULL ) - { - const size_t prev = minute_of_the_week > 0 ? minute_of_the_week - 1 - : MINUTES_PER_WEEK - 1; - *changed = e != tr_bitfieldHas( &t->minutes, prev ); - } +static inline tr_auto_switch_state_t +autoSwitchState( bool enabled ) +{ + return enabled ? TR_AUTO_SWITCH_ON : TR_AUTO_SWITCH_OFF; } static void turtleCheckClock( tr_session * s, struct tr_turtle_info * t ) { bool enabled; - bool changed; + bool alreadySwitched; + tr_auto_switch_state_t newAutoTurtleState; assert( t->isClockEnabled ); - testTurtleTime( t, &enabled, &changed ); + testTurtleTime( t, &enabled ); + newAutoTurtleState = autoSwitchState( enabled ); + alreadySwitched = ( t->autoTurtleState == newAutoTurtleState ); - if( changed ) + if( !alreadySwitched ) { tr_inf( "Time to turn %s turtle mode!", (enabled?"on":"off") ); + t->autoTurtleState = newAutoTurtleState; useAltSpeed( s, t, enabled, false ); } } @@ -1378,15 +1379,20 @@ static void turtleBootstrap( tr_session * session, struct tr_turtle_info * turtle ) { turtle->changedByUser = false; + turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED; tr_bitfieldConstruct( &turtle->minutes, MINUTES_PER_WEEK ); turtleUpdateTable( turtle ); if( turtle->isClockEnabled ) - testTurtleTime( turtle, &turtle->isEnabled, NULL ); + { + testTurtleTime( turtle, &turtle->isEnabled ); + turtle->autoTurtleState = autoSwitchState( turtle->isEnabled ); + } altSpeedToggled( session ); + } /*** @@ -1484,13 +1490,16 @@ userPokedTheClock( tr_session * s, struct tr_turtle_info * t ) { tr_dbg( "Refreshing the turtle mode clock due to user changes" ); + t->autoTurtleState = TR_AUTO_SWITCH_UNUSED; + turtleUpdateTable( t ); if( t->isClockEnabled ) { - bool enabled, changed; - testTurtleTime( t, &enabled, &changed ); + bool enabled; + testTurtleTime( t, &enabled ); useAltSpeed( s, t, enabled, true ); + t->autoTurtleState = autoSwitchState( enabled ); } } diff --git a/libtransmission/session.h b/libtransmission/session.h index c112eaa3e..022d16f80 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -34,6 +34,12 @@ typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t; +typedef enum { + TR_AUTO_SWITCH_UNUSED, + TR_AUTO_SWITCH_ON, + TR_AUTO_SWITCH_OFF, +} tr_auto_switch_state_t; + enum { PEER_ID_LEN = 20 @@ -87,6 +93,9 @@ struct tr_turtle_info * Each bit's value indicates whether the scheduler wants turtle * limits on or off at that given minute in the week. */ tr_bitfield minutes; + + /* recent action that was done by turtle's automatic switch */ + tr_auto_switch_state_t autoTurtleState; }; /** @brief handle to an active libtransmission session */