#!/usr/bin/env python3 """Exercise the display API in a private compositor, never the active desktop.""" import json, os, socket, struct, subprocess, tempfile, time from PIL import Image from pathlib import Path stage=Path(os.environ.get('WCM_TEST_STAGE','/var/tmp/wayfire-physical-grid')) runtime=Path(tempfile.mkdtemp(prefix='scope-toggle-test-')) env=os.environ.copy() for key in ['WAYLAND_DISPLAY','DISPLAY','WAYFIRE_SOCKET','_WAYFIRE_SOCKET','WAYFIRE_CONFIG_FILE','WAYFIRE_DEFAULT_CONFIG_BACKEND']: env.pop(key,None) env.update(GIO_USE_VFS='local',GTK_USE_PORTAL='0',XDG_RUNTIME_DIR=str(runtime),WLR_BACKENDS='headless',WLR_HEADLESS_OUTPUTS='4',WLR_RENDERER='gles2', WLR_RENDER_DRM_DEVICE='/dev/dri/renderD128',WAYFIRE_PLUGIN_PATH=str(stage/'plugins'),WAYFIRE_PLUGIN_XML_PATH=str(stage/'metadata')) config=runtime/'wayfire.ini' config.write_text('''# Preserve this comment and unrelated settings. [core] plugins = ipc stipc ipc-rules output-control grid place move resize wm-actions wobbly follow-focus simple-tile cube vswitch xwayland = false stitch_enabled = true stitch_outputs = HEADLESS-1 HEADLESS-2 HEADLESS-3 default_output = VIRTUAL-1 window_scope = physical window_scope_rules = [{"app_id":"tile-wide*","scope":"virtual"},{"app_id":"scope-pinned","scope":"HEADLESS-3"}] [simple-tile] key_toggle = none tile_by_default = none float_on_top = false inner_gap_size = 10 outer_horiz_gap_size = 5 outer_vert_gap_size = 5 [wm-actions] toggle_always_on_top = KEY_T [cube] rotate_left = KEY_LEFT rotate_right = KEY_RIGHT [autostart] autostart_wf_shell = false [grid] toggle_scope = KEY_ENTER type = none zone_gap = 20 [output:HEADLESS-1] mode = 640x720@60 position = 0,0 [output:HEADLESS-2] zone_gap = 20 mode = 640x720@60 position = 640,0 [output:HEADLESS-3] mode = 640x720@60 position = 1280,0 [output:HEADLESS-4] mode = 320x240@60 position = 1920,0 masked = true ''') if os.environ.get('GRID_TEST_UPGRADE'): config.write_text(config.read_text().replace('output-control grid place', 'output-control '+str(stage/'libgrid-r19.so')+' place')) if os.environ.get('SCOPE_TEST_ANIMATION'): config.write_text(config.read_text().replace('type = none', 'type = crossfade\nduration = 300ms circle')) log=(runtime/'wayfire.log').open('w') compositor_env=env.copy() if os.environ.get('WCM_TEST_ASAN'): compositor_env.update(LD_PRELOAD=os.environ['WCM_TEST_ASAN'],ASAN_OPTIONS='detect_leaks=0:abort_on_error=1',UBSAN_OPTIONS='halt_on_error=1:print_stacktrace=1') process=subprocess.Popen([os.environ.get('WCM_TEST_COMPOSITOR','/usr/bin/wayfire'),'-c',str(config)],env=compositor_env,stdout=log,stderr=log) def wait(fn,timeout=10): deadline=time.monotonic()+timeout while time.monotonic()=x and g['y']>=y and g['x']+g['width']<=x+w and g['y']+g['height']<=y+h,(vid,g,(x,y,w,h)) def toggle_scope(vid, keyboard=False): was=info(vid)['window-scope'] if keyboard: focus(vid);keys('KEY_LEFTMETA','KEY_ENTER') else:ipc('grid/toggle_scope',{'view-id':vid}) wait(lambda:(info(vid)['window-scope']=='virtual') != (was=='virtual')) time.sleep(.4 if os.environ.get('SCOPE_TEST_ANIMATION') else .1) def same_geometry(vid, before): expect(vid,**before) def rect(x,y,w,h):return dict(x=x,y=y,width=w,height=h) def slot_rect(area,slot): x,y,w,h=(area[k] for k in ('x','y','width','height')) if slot%3==1:w=int(w/2) elif slot%3==0:x+=int(w/2);w=int(w/2) if slot>=7:h=int(h/2) elif slot<=3:y+=int(h/2);h=int(h/2) return rect(x,y,w,h) def move(x,y):ipc('stipc/move_cursor',{'x':x,'y':y});time.sleep(.04) def button(mode):ipc('stipc/feed_button',{'combo':'S-BTN_LEFT','mode':mode}) def prepare(vid,monitor=1): ipc('window-rules/set-window-scope',{'id':vid,'scope':'physical'}) ipc('grid/restore',{'view-id':vid}) g=rect((monitor-1)*640+140,180,300,180) ipc('window-rules/configure-view',{'id':vid,'geometry':g}) expect(vid,**g);time.sleep(.4 if os.environ.get("SCOPE_TEST_ANIMATION") else .05);focus(vid) def begin(vid,offset=150): g=geometry(vid);move(g['x']+offset,g['y']+60);button('press') move(g['x']+offset+30,g['y']+60) def preview(expected): def found(): return any(v.get('mapped') and v.get('type')!='toplevel' and v.get('app-id')!='scope-bar' and v.get('geometry')==expected for v in ipc('window-rules/list-views')) try:wait(found,3) except Exception: print('PREVIEW expected',expected,'views',ipc('window-rules/list-views'),flush=True);raise def end(vid,expected): button('release');expect(vid,**expected);time.sleep(.25) slot_names=['','bl','b','br','l','c','r','tl','t','tr'] slot_keys=['','KEY_M','KEY_COMMA','KEY_DOT','KEY_J','KEY_K','KEY_L','KEY_U','KEY_I','KEY_O'] def scaled(area,slot,step): g=slot_rect(area,slot) if step: scale=[1,.8,.6][step] w=int(g['width']*scale+.5);h=int(g['height']*scale+.5) g['x']+=(g['width']-w)*(0 if slot%3==1 else 1 if slot%3==0 else .5) g['y']+=(g['height']-h)*(0 if slot>=7 else 1 if slot<=3 else .5) g.update(width=w,height=h) return g def action(vid,slot):ipc('grid/slot_'+slot_names[slot],{'view-id':vid}) try: env['WAYFIRE_SOCKET']=str(wait(lambda:next(runtime.glob('wayfire-*.socket'),None))) env['WAYLAND_DISPLAY']=next(p.name for p in runtime.glob('wayland-*') if not p.name.endswith('.lock')) print('Runtime:',runtime,flush=True) wait(lambda:names()==['VIRTUAL-1']) bar,_=spawn('cycle-bar','bar') a,aid=spawn_at('cycle-test',2) if os.environ.get('GRID_TEST_UPGRADE'): action(aid,6);expect(aid,**rect(960,44,300,656));time.sleep(.4) config.write_text(config.read_text().replace(str(stage/'libgrid-r19.so'),'grid')) wait(lambda:str(stage/'plugins/libgrid.so') in Path('/proc/'+str(process.pid)+'/maps').read_text()) action(aid,6);expect(aid,**scaled(rect(660,44,600,656),6,1)) print('PASS cached r19 upgrade and existing snapped view',flush=True) for m in [1,2,3]: area=rect((m-1)*640+20,44,600,656) prepare(aid,m) for slot in range(1,10): for step in [0,1,2,0]: action(aid,slot);expect(aid,**scaled(area,slot,step)) print('PASS all nine slots cycle 100/80/60/100 on monitor',m,flush=True) # Exercise actual bindings independently of IPC activators. for slot in range(1,10): ipc('wayfire/set-config-options',{'grid/slot_'+slot_names[slot]:' '+slot_keys[slot]}) prepare(aid,2);move(5,715);focus(aid) for slot in range(1,10): for step in [0,1,2,0]: keys('KEY_LEFTCTRL','KEY_LEFTALT',slot_keys[slot]);expect(aid,**scaled(rect(660,44,600,656),slot,step)) print('PASS all nine keyboard bindings',flush=True) action(aid,9);expect(aid,**scaled(rect(660,44,600,656),9,1)) toggle_scope(aid);expect(aid,**scaled(rect(0,24,1920,696),9,1)) toggle_scope(aid);expect(aid,**scaled(rect(660,44,600,656),9,1)) command(a,'fullscreen 1');expect(aid,**rect(640,0,640,720)) command(a,'fullscreen 0');expect(aid,**scaled(rect(660,44,600,656),9,1)) action(aid,9);expect(aid,**scaled(rect(660,44,600,656),9,2)) print('PASS scope and fullscreen round trips retain cycle size',flush=True) # Reflow for panel reservation retains the step without advancing it. bar.terminate();bar.wait(timeout=3) expect(aid,**scaled(rect(660,20,600,680),9,2)) action(aid,9);expect(aid,**scaled(rect(660,20,600,680),9,0)) print('PASS workarea reflow preserves cycle size',flush=True) ipc('grid/restore',{'view-id':aid});wait(lambda:info(aid)['tiled-edges']==0) action(aid,9);expect(aid,**scaled(rect(660,20,600,680),9,0)) b,bid=spawn_at('cycle-second',1) action(bid,4);expect(bid,**rect(20,20,300,680)) action(bid,4);expect(bid,**scaled(rect(20,20,600,680),4,1)) action(aid,9);expect(aid,**scaled(rect(660,20,600,680),9,1)) print('PASS restore reset and independent windows',flush=True) b.terminate();b.wait(timeout=3) # Repeated actions before pending transactions settle still cycle by request. for slot in [7,7,7,7,7,3,3,3,5,5]:action(aid,slot) expect(aid,**scaled(rect(660,20,600,680),5,1)) time.sleep(.5) print('PASS rapid position and size changes',flush=True) # A drag drop is always a full slot; the next key cycles from that slot. prepare(aid,2);begin(aid);move(642,1) expected=rect(660,20,300,340);preview(expected);end(aid,expected) action(aid,7);expect(aid,**scaled(rect(660,20,600,680),7,1)) print('PASS dragging selects full slot and keyboard continues at 80 percent',flush=True) time.sleep(.5) g=geometry(aid);move(g['x']+g['width']-10,g['y']+g['height']-10) ipc('stipc/feed_button',{'combo':'S-BTN_RIGHT','mode':'press'}) move(g['x']+g['width']+40,g['y']+g['height']+20) ipc('stipc/feed_button',{'combo':'S-BTN_RIGHT','mode':'release'}) wait(lambda:info(aid)['tiled-edges']==0) action(aid,7);expect(aid,**scaled(rect(660,20,600,680),7,0)) print('PASS manual resize resets the cycle',flush=True) print('ALL PASS',flush=True) finally: for p,f in clients: if p.poll() is None:p.terminate() p.wait(timeout=3);f.close() process.terminate();process.wait(timeout=5);log.close()