Пример #1
0
def main():
    logfile="___log-nest-layout"
    dumpfile="___scs-nest-layout-dump-%d"%os.getpid()
    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)
    logfile=os.path.join(tmpdir,logfile)
    dumpfile=os.path.join(tmpdir,dumpfile)
    sys.stdout=open(logfile,'w')
    sys.stderr=sys.stdout
    session=sys.argv[1]
    tlayout=sys.argv[2]

    scs=ScreenSaver(session)
    homelayout,homelayoutname=scs.get_layout_number()
    while True:
        regions=sc.get_regions(scs.pid)
        try:
            focusminsize="%s %s"%(regions[3][0], regions[3][1])
            regions_c=regions[0]
            foff=regions[1]
            rsize=tuple([int(regions[4+foff][1]),int(regions[4+foff][2])])
            hsize=int(regions[2][0]),int(regions[2][1])
            if regions[4][0]:
                break
        except Exception,x:
            print(x)
            sys.exit(2)
            pass
Пример #2
0
def prepare_windows(scs):
    print('prepare_windows()')
    global focusminsize
    regions=[]
    while True:
        regions=sc.get_regions(scs.pid)
        try:
            focusminsize="%s %s"%(regions[3][0], regions[3][1])
            regions_c=regions[0]
            focus_offset=regions[1]
            if regions[4][0]:
                break
        except:
            pass
    print("regions = "+str(regions))
    scs.focusminsize('0 0')
    this_win_history=[]
    cmd=''
    for i in range(0,regions_c):
        cmd+='screen -S %s -X eval \'screen -t scs-regions-helper %s %s %s %d\' \'focus\'; '%(scs.pid,subprogram,subprogram_args,inputfile,i)
    os.system(cmd)
    
    regions_n=[]
    while True:
        regions_n=sc.get_regions(scs.pid)
        try:
            if regions_n[2+i][0]:
                break
        except:
            pass
    print("regions_n = "+str(regions_n))

    for r in regions[4+focus_offset:]:
        this_win_history.append(r[0])
    for r in regions[4:4+focus_offset]:
        this_win_history.append(r[0])

    new_windows=[]
    for r in regions_n[4+focus_offset:]:
        new_windows.append(r[0])
    for r in regions_n[4:4+focus_offset]:
        new_windows.append(r[0])
        
    return this_win_history,new_windows,regions_c
Пример #3
0
def prepare_windows(scs):
    global focusminsize
    regions = None
    regions = sc.get_regions(sc.dumpscreen_layout(scs.pid))
    sc.cleanup()
    focusminsize = "%s %s" % (regions.focusminsize_x, regions.focusminsize_x)
    regions_c = len(regions.regions)
    focus_offset = regions.focus_offset
    scs.focusminsize('0 0')
    this_win_history = []
    cmd = ''
    f = open(sourcefile, 'w')
    for i in range(0, regions_c):
        f.write("""screen -t scs-regions-helper %s %s %s %d
focus
""" %
                (subprogram, subprogram_args, inputfile, i))
    f.flush()
    f.close
    scs.source(sourcefile, "screen-session regions")
    remove(sourcefile)

    regions_n = []
    regions_n = sc.get_regions(sc.dumpscreen_layout(scs.pid))
    sc.cleanup()

    for r in (regions.regions)[focus_offset:]:
        this_win_history.append(r[0])
    for r in (regions.regions)[:focus_offset]:
        this_win_history.append(r[0])

    new_windows = []
    for r in (regions_n.regions)[focus_offset:]:
        new_windows.append(r[0])
    for r in (regions_n.regions)[:focus_offset]:
        new_windows.append(r[0])

    return (this_win_history, new_windows, regions_c)
Пример #4
0
            if targ > 0:
                ld = layhist.redo[targ-1][1]
            elif targ < 0:
                ld = layhist.undo[abs(targ)-1][1]
            else:
                ld = layhist.current[0]
        except IndexError:
            ld = layhist.current[0]
            targ = 0

        cur = layhist.current[0]
        term_x, term_y = map(int, ss.dinfo()[0:2])
        lay_f = sc.layout_begin(session)
        lay_f.write('only\n')
        sc.layout_load_dump(open(os.path.join(ld, layout_dump), 'r'))
        sc.layout_load_regions(sc.get_regions(os.path.join(ld, layout_regions)),
                None, term_x, term_y)
        sc.layout_end()

        os.rename(ld, os.path.join(tdir, os.path.basename(ld)))
        if targ > 0:
            ss.Xecho('layout redo ( %d left )' % (len(layhist.redo) - 1))
            os.rename(cur, os.path.join(tdir_u,
                os.path.basename(cur)))
        elif targ < 0:
            ss.Xecho('layout undo ( %d left )' % (len(layhist.undo) - 1))
            os.rename(cur, os.path.join(tdir_r,
                os.path.basename(cur)))
        else:
            ss.Xecho('layout current ( undo: %d redo: %d )' %
                    (len(layhist.undo), len(layhist.redo)))
Пример #5
0
    def __load_layouts(self):
        cdinfo = map(int, self.dinfo()[0:2])
        out('Terminal size: %s %s' % (cdinfo[0], cdinfo[1]))
        homewindow = self.homewindow
        (homelayout, homelayoutname) = self.get_layout_number()
        layout_trans = {}
        layout_c = len(glob.glob(os.path.join(self.basedir, self.savedir,
                       'winlayout_*')))
        if layout_c > 0:
            self.__layouts_loaded = True
        lc = 0
        layout_file = sc.layout_begin(self.pid)
        while lc < layout_c:
            filename = None
            try:
                filename = glob.glob(os.path.join(self.basedir, self.savedir,
                        'layout_%d' % lc))[0]
                layoutnumber = filename.rsplit("_", 1)[1]
                (head, tail) = os.path.split(filename)

                # the winlayout_NUM files contain "dumpscreen layout" output 
                # (see GNUScreen.Regions class)

                filename2 = os.path.join(head, "win" + tail)
                regions = sc.get_regions(filename2)
                status = self.get_layout_new(regions.title)
                if not status:
                    sys.stderr.write('\nMaximum number of layouts reached. Ignoring layout %s (%s).\n' %
                        (layoutnumber, regions.title))
                    break
                else:
                    if self.exact:
                        self.layout('number %s' % layoutnumber, False)
                        currentlayout = layoutnumber
                    else:
                        currentlayout = self.get_layout_number()[0]
                    layout_trans[layoutnumber] = currentlayout

                    sc.layout_select_layout(currentlayout)
                    # source the output produced by "layout dump"
                    sc.layout_load_dump(open(filename,'r'))

                    regions_size = []
                    winlist = []

                    for (window, sizex, sizey) in regions.regions:
                        winlist.append(window)
                        regions_size.append((sizex, sizey))
                    sc.layout_load_regions(regions, self.__wins_trans, cdinfo[0], cdinfo[1])
                    # sys.stdout.write(" %s (%s);" % (layoutnumber, regions.title))
            except:
                # import traceback
                # traceback.print_exc(file=sys.stderr)
                # raise
                layout_c += 1
                if layout_c > 2000:
                    sys.stderr.write('\nErrors during layouts loading.\n')
                    break
            lc += 1
        out('')
        if not lc == 0:

            # select last layout

            lastname = None
            lastid_l = None

            if homelayout != -1:
                out("Returning homelayout %s" % homelayout)
                layout_file.write('layout select %s' % homelayout)
            else:
                sys.stderr.write('No homelayout - unable to return.\n')

            if os.path.exists(os.path.join(self.basedir, self.savedir,
                              "last_layout")) and len(layout_trans) > 0:
                last = os.readlink(os.path.join(self.basedir, self.savedir,
                                   "last_layout"))
                (lasthead, lasttail) = os.path.split(last)
                last = lasttail.split("_", 2)
                lastid_l = last[1]
                try:
                    out("Selecting last layout: %s (%s)" % (layout_trans[lastid_l], lastid_l))
                    layout_file.write('layout select %s' % layout_trans[lastid_l])
                    # ^^ layout numbering may change, use layout_trans={}
                except:
                    sys.stderr.write("Unable to select last layout %s\n" % lastid_l)
        else:
            self.enable_layout = False
        sc.layout_end()
Пример #6
0
def nest_layout(session, src_layuot, dst_layout):
    src_dumpfile = os.path.join(tmpdir_source, 'nest_layout-dump-%d' % os.getpid())

    if not os.path.exists(tmpdir_source):
        os.makedirs(tmpdir_source)

    scs = ScreenSaver(session)

    print('layouts src: %s dst: %s' % (src_layout, dst_layout))

    regions_file_dst = regions_file = sc.dumpscreen_layout(scs.pid)
    regions_dst = sc.get_regions(regions_file)

    dst_focusminsize = "%s %s" % (regions_dst.focusminsize_x, regions_dst.focusminsize_y)
    dst_rsize = (int(regions_dst.regions[regions_dst.focus_offset][1]),
                int(regions_dst.regions[regions_dst.focus_offset][2]))
    dst_term_size = (int(regions_dst.term_size_x),
                    int(regions_dst.term_size_y))
    scs.layout('select %s' % src_layout, False)

    scs.layout('dump %s' % src_dumpfile, False)

    regions_file_src = sc.dumpscreen_layout(scs.pid)
    regions_src = sc.get_regions(regions_file_src)

    src_term_size = (int(regions_src.term_size_x), int(regions_src.term_size_y))

    print ('dst_rsize: %s' % str(dst_rsize))
    print ('src_term_size: %s' % str(src_term_size))

    scs.layout('select %s' % dst_layout, False)
    
    regions_new = sc.Regions()
    regions_new.focus_offset =  regions_dst.focus_offset + regions_src.focus_offset
    regions_new.term_size_x = regions_dst.term_size_x
    regions_new.term_size_y = regions_dst.term_size_y
    regions_new.focusminsize_x = regions_dst.focusminsize_x
    regions_new.focusminsize_y = regions_dst.focusminsize_y
    regions_new.regions = regions_dst.regions[:regions_dst.focus_offset]

    for (window, sizex, sizey) in regions_src.regions:
        print('SRC REGION' + str((window,sizex,sizey)))
        x = (int(sizex) * dst_rsize[0]) / src_term_size[0]
        y = (int(sizey) * dst_rsize[1]) / src_term_size[1]
        print( '%s * %d / %d = %d' % (sizex, dst_rsize[0], src_term_size[0], x))
        print( '%s * %d / %d = %d' % (sizey, dst_rsize[1], src_term_size[0], y))
        regions_new.regions.append((window, str(x), str(y)))

    regions_new.regions = regions_new.regions + regions_dst.regions[regions_dst.focus_offset+1:]
    
    print('destination regions: '+ str(regions_dst.regions))
    print('source regions: ' + str(regions_src.regions))
    print('new regions: ' + str(regions_new.regions))

    sc.layout_begin(session)
    sc.layout_load_dump(open(src_dumpfile, 'r'))
    sc.layout_load_regions(regions_new, None, dst_term_size[0], dst_term_size[1])
    sc.layout_end()

    remove(src_dumpfile)
    remove(regions_file_dst)
    remove(regions_file_src)
    sc.cleanup()
Пример #7
0
            hsize=int(regions[2][0]),int(regions[2][1])
            if regions[4][0]:
                break
        except Exception,x:
            print(x)
            sys.exit(2)
            pass
    print ("homelayout: %s"%homelayout)
    scs.focusminsize('0 0')
    rsize=rsize[0],rsize[1]+2 # +1 for caption +1 for hardstatus

    scs.layout('select %s'%tlayout)
    print ("tlayout : %s"%scs.get_layout_number()[0])
    scs.layout('dump %s'%dumpfile)
    while True:
        regions=sc.get_regions(scs.pid)
        try:
            tfocusminsize="%s %s"%(regions[3][0], regions[3][1])
            regions_c=regions[0]
            tfoff=regions[1]
            hsize=int(regions[2][0]),int(regions[2][1])
            if regions[4][0]:
                break
        except:
            pass

    if rsize[0]==hsize[0] and rsize[1]+2==hsize[1]:
        rsize=hsize
    else:
        rsize=rsize[0],rsize[1]+1 # +1 for caption +1 for hardstatus
    rsize=hsize
Пример #8
0
    def __load_layouts(self):
        cdinfo = map(int, self.dinfo()[0:2])
        out("Terminal size: %s %s" % (cdinfo[0], cdinfo[1]))
        homewindow = self.homewindow
        (homelayout, homelayoutname) = self.get_layout_number()
        layout_trans = {}
        layout_c = len(glob.glob(os.path.join(self.basedir, self.savedir, "winlayout_*")))
        if layout_c > 0:
            self.__layouts_loaded = True
        lc = 0
        layout_file = sc.layout_begin(self.pid)
        while lc < layout_c:
            filename = None
            try:
                filename = glob.glob(os.path.join(self.basedir, self.savedir, "layout_%d" % lc))[0]
                layoutnumber = filename.rsplit("_", 1)[1]
                (head, tail) = os.path.split(filename)

                # the winlayout_NUM files contain "dumpscreen layout" output
                # (see GNUScreen.Regions class)

                filename2 = os.path.join(head, "win" + tail)
                regions = sc.get_regions(filename2)
                status = self.get_layout_new(regions.title)
                if not status:
                    sys.stderr.write(
                        "\nMaximum number of layouts reached. Ignoring layout %s (%s).\n"
                        % (layoutnumber, regions.title)
                    )
                    break
                else:
                    if self.exact:
                        self.layout("number %s" % layoutnumber, False)
                        currentlayout = layoutnumber
                    else:
                        currentlayout = self.get_layout_number()[0]
                    layout_trans[layoutnumber] = currentlayout

                    sc.layout_select_layout(currentlayout)
                    # source the output produced by "layout dump"
                    sc.layout_load_dump(open(filename, "r"))

                    regions_size = []
                    winlist = []

                    for (window, sizex, sizey) in regions.regions:
                        winlist.append(window)
                        regions_size.append((sizex, sizey))
                    sc.layout_load_regions(regions, self.__wins_trans, cdinfo[0], cdinfo[1])
                    # sys.stdout.write(" %s (%s);" % (layoutnumber, regions.title))
            except:
                # import traceback
                # traceback.print_exc(file=sys.stderr)
                # raise
                layout_c += 1
                if layout_c > 2000:
                    sys.stderr.write("\nErrors during layouts loading.\n")
                    break
            lc += 1
        out("")
        if not lc == 0:

            # select last layout

            lastname = None
            lastid_l = None

            if homelayout != -1:
                out("Returning homelayout %s" % homelayout)
                layout_file.write("layout select %s" % homelayout)
            else:
                sys.stderr.write("No homelayout - unable to return.\n")

            if os.path.exists(os.path.join(self.basedir, self.savedir, "last_layout")) and len(layout_trans) > 0:
                last = os.readlink(os.path.join(self.basedir, self.savedir, "last_layout"))
                (lasthead, lasttail) = os.path.split(last)
                last = lasttail.split("_", 2)
                lastid_l = last[1]
                try:
                    out("Selecting last layout: %s (%s)" % (layout_trans[lastid_l], lastid_l))
                    layout_file.write("layout select %s" % layout_trans[lastid_l])
                    # ^^ layout numbering may change, use layout_trans={}
                except:
                    sys.stderr.write("Unable to select last layout %s\n" % lastid_l)
        else:
            self.enable_layout = False
        sc.layout_end()