Ticket #4201: turtle.diff

File turtle.diff, 3.5 KB (added by dim-an, 9 years ago)
  • libtransmission/session.c

     
    13251325 * @param changed whether that's different from the previous minute
    13261326 */
    13271327static void
    1328 testTurtleTime( const struct tr_turtle_info * t,
    1329                 bool * enabled,
    1330                 bool * changed )
     1328testTurtleTime( const struct tr_turtle_info * t, bool * enabled )
    13311329{
    13321330    bool e;
    13331331    struct tm tm;
     
    13451343    e = tr_bitfieldHas( &t->minutes, minute_of_the_week );
    13461344    if( enabled != NULL )
    13471345        *enabled = e;
     1346}
    13481347
    1349     if( changed != NULL )
    1350     {
    1351         const size_t prev = minute_of_the_week > 0 ? minute_of_the_week - 1
    1352                                                    : MINUTES_PER_WEEK - 1;
    1353         *changed = e != tr_bitfieldHas( &t->minutes, prev );
    1354     }
     1348static inline tr_auto_switch_state_t
     1349autoSwitchState( bool enabled )
     1350{
     1351    return enabled ? TR_AUTO_SWITCH_ON : TR_AUTO_SWITCH_OFF;
    13551352}
    13561353
    13571354static void
    13581355turtleCheckClock( tr_session * s, struct tr_turtle_info * t )
    13591356{
    13601357    bool enabled;
    1361     bool changed;
     1358    bool alreadySwitched;
     1359    tr_auto_switch_state_t newAutoTurtleState;
    13621360
    13631361    assert( t->isClockEnabled );
    13641362
    1365     testTurtleTime( t, &enabled, &changed );
     1363    testTurtleTime( t, &enabled );
     1364    newAutoTurtleState = autoSwitchState( enabled );
     1365    alreadySwitched = ( t->autoTurtleState == newAutoTurtleState );
    13661366
    1367     if( changed )
     1367    if( !alreadySwitched )
    13681368    {
    13691369        tr_inf( "Time to turn %s turtle mode!", (enabled?"on":"off") );
     1370        t->autoTurtleState = newAutoTurtleState;
    13701371        useAltSpeed( s, t, enabled, false );
    13711372    }
    13721373}
     
    13781379turtleBootstrap( tr_session * session, struct tr_turtle_info * turtle )
    13791380{
    13801381    turtle->changedByUser = false;
     1382    turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
    13811383
    13821384    tr_bitfieldConstruct( &turtle->minutes, MINUTES_PER_WEEK );
    13831385
    13841386    turtleUpdateTable( turtle );
    13851387
    13861388    if( turtle->isClockEnabled )
    1387         testTurtleTime( turtle, &turtle->isEnabled, NULL );
     1389    {
     1390        testTurtleTime( turtle, &turtle->isEnabled );
     1391        turtle->autoTurtleState = autoSwitchState( turtle->isEnabled );
     1392    }
    13881393
    13891394    altSpeedToggled( session );
     1395
    13901396}
    13911397
    13921398/***
     
    14841490{
    14851491    tr_dbg( "Refreshing the turtle mode clock due to user changes" );
    14861492
     1493    t->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
     1494
    14871495    turtleUpdateTable( t );
    14881496
    14891497    if( t->isClockEnabled )
    14901498    {
    1491         bool enabled, changed;
    1492         testTurtleTime( t, &enabled, &changed );
     1499        bool enabled;
     1500        testTurtleTime( t, &enabled );
    14931501        useAltSpeed( s, t, enabled, true );
     1502        t->autoTurtleState = autoSwitchState( enabled );
    14941503    }
    14951504}
    14961505
  • libtransmission/session.h

     
    3434
    3535typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
    3636
     37typedef enum {
     38    TR_AUTO_SWITCH_UNUSED,
     39    TR_AUTO_SWITCH_ON,
     40    TR_AUTO_SWITCH_OFF,
     41} tr_auto_switch_state_t;
     42
    3743enum
    3844{
    3945  PEER_ID_LEN = 20
     
    8793     * Each bit's value indicates whether the scheduler wants turtle
    8894     * limits on or off at that given minute in the week. */
    8995    tr_bitfield minutes;
     96
     97    /* recent action that was done by turtle's automatic switch */
     98    tr_auto_switch_state_t autoTurtleState;
    9099};
    91100
    92101/** @brief handle to an active libtransmission session */