Thanks Tycho, 

At the end, i checked if the number of plugged screen was the same as the 
number of enabled screens and either called xrandr or restarted qtile 
depending on that.
Here's the simple, not very clean, but working code:
def get_nb_plugged_screens():                                         
    cmd_output = run(['xrandr', '-q'], stdout=PIPE)                   
    occurences = re.findall(b'^[\w-]+ connected', cmd_output.stdout,
                            re.MULTILINE)             
    return len(occurences)                                                 
                              
def get_nb_enabled_screens():                                         
    cmd_output = run(['mons'], stdout=PIPE)                           
    return cmd_output.stdout.count(b'(enabled)')

def setup_monitors(nb_plugged_screens):                              
    """Configure the screens with randr (through mons)."""          
    if nb_plugged_screens == 1:                                       
        subprocess.run(['mons', '-a'])                               
    elif nb_plugged_screens == 2:                                     
        subprocess.run(['mons', '-e', 'left'])                             
     
@hook.subscribe.screen_change      
def configure_screens(qtile, ev):                                     
    nb_enabled = get_nb_enabled_screens()                             
    nb_plugged = get_nb_plugged_screens()                             
    if nb_plugged != nb_enabled:                                     
        setup_monitors(nb_plugged)                                   
    else:                                                             
        qtile.cmd_restart()

I wish I could use a python library to get the info and change my config 
but unfortunately I haven't find one high level enough for me to understand 
how to do what i'm doing here. So i'm just using xrandr and mons (which is 
just a script over xrandr with simpler commands).

On Sunday, November 19, 2017 at 2:30:35 AM UTC+1, Tycho Andersen wrote:
>
> On Fri, Nov 17, 2017 at 05:32:32PM -0800, Sweenu wrote: 
> > Thank you Tycho, 
> > 
> > I looked into it, and it seems like, it is useless in my case. Indeed, 
> ev's 
> > values don't change between a screen plugging and a screen configuration 
> > (beside the timestamps and sequence ofc). 
> > It's really too bad because I feel like the screen_change hook isn't 
> really 
> > all that powerful without this information. For exemple if we tell qtile 
> to 
> > restart on screen_change but have a script that automatically setup the 
> > monitors when a new one is plugged, then we enter a non-ending cycle of 
> > qtile restarting... 
>
> What if you just save the last screen configuration, and then check a 
> diff? Given that the numbers don't change (except for on the first 
> event, when the screen is actually attached), won't that be enough? 
>
> Cheers, 
>
> Tycho 
>
> > But yeah, that's life. I guess we'll continue using the pyudev technique 
> > for now. It's just that I've had a lot of troubles and inconsistency 
> with 
> > it. 
> > Anyway, thanks for your help! 
> > 
> > Cheers, 
> > 
> > Bruno (Sweenu) 
> > 
> > On Thursday, November 16, 2017 at 6:44:56 PM UTC+1, Tycho Andersen 
> wrote: 
> > > 
> > > Hey Bruny, 
> > > 
> > > On Wed, Nov 15, 2017 at 11:20:00PM -0800, Bruno Inec wrote: 
> > > > I have a very hard time finding the use of ev in: 
> > > > 
> > > > @libqtile.hook.subscribe.screen_change 
> > > > def restart_on_randr(qtile, ev): 
> > > >     qtile.cmd_restart() 
> > > > 
> > > > 
> > > > I'd like to know when it's just a screen plugged or if it is a 
> change in 
> > > > the configuration through randr. 
> > > > Is it possbile to do so with the 'ev' argument? 
> > > 
> > > Probably, but you'd have to do more digging than I have time for at 
> > > the moment. 'ev' is a ScreenChangeNotifyEvent object, 
> > > 
> > >         <event name="ScreenChangeNotify" number="0"> 
> > >                 <field type="CARD8" name="rotation" mask="Rotation" /> 
> > >                 <field type="TIMESTAMP" name="timestamp" /> 
> > >                 <field type="TIMESTAMP" name="config_timestamp" /> 
> > >                 <field type="WINDOW" name="root" /> 
> > >                 <field type="WINDOW" name="request_window" /> 
> > >                 <field type="CARD16" name="sizeID" /> 
> > >                 <field type="CARD16" name="subpixel_order" 
> enum="SubPixel" 
> > > /> 
> > >                 <field type="CARD16" name="width" /> 
> > >                 <field type="CARD16" name="height" /> 
> > >                 <field type="CARD16" name="mwidth" /> 
> > >                 <field type="CARD16" name="mheight" /> 
> > >         </event> 
> > > 
> > > I guess looking at some properties of the root window combined with 
> > > the width/height you'll be able to figure this out. 
> > > 
> > > Cheers, 
> > > 
> > > Tycho 
> > > 
> > > > Thank you! 
> > > > 
> > > > -- 
> > > > You received this message because you are subscribed to the Google 
> > > Groups "qtile-dev" group. 
> > > > To unsubscribe from this group and stop receiving emails from it, 
> send 
> > > an email to [email protected] <javascript:>. 
> > > > For more options, visit https://groups.google.com/d/optout. 
> > > 
> > > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "qtile-dev" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to [email protected] <javascript:>. 
> > For more options, visit https://groups.google.com/d/optout. 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"qtile-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to