ucmd_wm.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536
  1. /* Ring-3 window manager: maps the shadow buffer and each window's content,
  2. * draws the desktop/chrome and composites the terminal cell grids or gfx
  3. * canvases. All glyphs and colours are userland; the kernel only routes
  4. * input, stores window state and copies damage to VRAM. */
  5. #include "ucmd_lib.h"
  6. #include "uwm_lib.h"
  7. #include "version.h"
  8. #define COL_BG 0x202030u
  9. /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
  10. * the inactive one stays neutral. Windows have no visible frame. */
  11. #define COL_TITLE_ON 0x365CA6u
  12. #define COL_TITLE_OFF 0x646464u
  13. /* The close control is a bare window-close icon in the title bar: dimmed
  14. * while idle, bright white while the pointer is over it. */
  15. #define COL_CLOSE_ICON 0xD3DAE6u
  16. #define COL_CLOSE_HOT 0xFFFFFFu
  17. #define COL_WHITE 0xFFFFFFu
  18. #define COL_BANNER 0x55FF55u
  19. /* Title-bar text inset from the window's left edge, and the close icon's
  20. * inset from its right edge. The Adwaita symbolic icon mask is 16x16. */
  21. #define WM_TITLE_PAD 8
  22. #define WM_CLOSE_INSET 3
  23. #define WM_CLOSE_ICON_PX 16
  24. struct uwin {
  25. struct wm_window_info info;
  26. struct wm_map_info map;
  27. int mapped;
  28. unsigned int ack_seq;
  29. /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
  30. * geometry to return to when the window leaves that state. */
  31. int state;
  32. int restore_x, restore_y;
  33. unsigned int restore_w, restore_h;
  34. };
  35. /* Double-click detection on the title bar. */
  36. #define WM_DBLCLICK_MS 500
  37. #define WM_DBLCLICK_SLOP 8
  38. /* A maximized window restores only after the pointer actually starts a drag,
  39. * so a plain click on its title bar leaves it maximized. */
  40. #define WM_DRAG_START 3
  41. /* Window placement states. Maximize fills the work area below the bar; the
  42. * snap states fill its left or right half at full height. */
  43. #define WIN_NORMAL 0
  44. #define WIN_MAXIMIZED 1
  45. #define WIN_SNAP_LEFT 2
  46. #define WIN_SNAP_RIGHT 3
  47. /* Releasing a title-bar drag with the pointer this close to the left/right
  48. * screen edge snaps the window to that half; at the top edge it maximizes. */
  49. #define WM_SNAP_EDGE 2
  50. /* Edge resistance: a dragged window's edge stops at a screen edge or at the
  51. * facing edge of another window until the pointer overshoots it by this
  52. * many pixels, which makes exact side-by-side placement easy. */
  53. #define WM_RESIST 24
  54. static struct usurf screen;
  55. static struct uwin wins[WM_MAX_WINDOWS];
  56. static unsigned int fbw, fbh;
  57. /* Desktop wallpaper: the viewer stores a header plus RGB565 pixels; the WM
  58. * only has to stretch that small buffer to the framebuffer, so no image
  59. * decoder is linked in. The static buffer avoids pulling malloc into the
  60. * WM and stays within the 4 MB user image window. */
  61. #define WALL_MAP_MAX 2048
  62. static unsigned char wall_data[sizeof(struct wm_wallpaper_hdr) +
  63. (unsigned int)WM_WALLPAPER_MAX_W * WM_WALLPAPER_MAX_H * 2u + 512];
  64. static unsigned int wall_w, wall_h;
  65. static int wall_ready;
  66. static unsigned int wall_seq_seen;
  67. static int wall_boot_seen;
  68. static int wall_retries;
  69. static unsigned int wall_retry_ms;
  70. static unsigned short wall_xmap[WALL_MAP_MAX];
  71. static unsigned short wall_ymap[WALL_MAP_MAX];
  72. /* RGB565 -> 0xFFRRGGBB, built when the wallpaper loads so the 32 bpp
  73. * desktop path is a table lookup instead of per-pixel shifts. */
  74. static unsigned int wall_rgb32[65536];
  75. /* Settings pushed by the Settings app through SYS_WM_PREFS. The kernel
  76. * keeps them across a WM restart; the app loads the file at boot and pushes
  77. * it here. */
  78. static struct wm_prefs prefs = { 1, 1, WM_DEFAULT_BG_RGB, { 0, 0, 0, 0 } };
  79. static unsigned int prefs_seq_seen;
  80. static int prefs_seen_valid;
  81. static unsigned int desktop_rgb = WM_DEFAULT_BG_RGB;
  82. /* Settings app launcher: SYS_WM_OPEN_SETTINGS bumps a request counter in
  83. * wm_status. One window is reused, and a section request for a running app
  84. * goes through /share/settings.req so it switches panes. */
  85. static int settings_pid;
  86. static unsigned int settings_req_seen;
  87. static int settings_req_seen_valid;
  88. static int prefs_boot_seen;
  89. static int prefs_retries;
  90. static unsigned int prefs_retry_ms;
  91. static int prefs_loaded;
  92. static int drag_id = -1;
  93. static int drag_dx, drag_dy;
  94. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  95. static int drag_moved; /* the pointer passed WM_DRAG_START */
  96. static int resize_id = -1;
  97. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  98. static int resize_ox, resize_oy;
  99. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  100. /* A live resize step reallocates both canvases and wakes the client to
  101. * reflow, so pointer-rate commits starve it (and make the content blink).
  102. * Commit when the edge moved at least WM_RESIZE_MIN_STEP pixels, else at
  103. * most every WM_RESIZE_COMMIT_MS; the button release forces the final one. */
  104. #define WM_RESIZE_COMMIT_MS 30
  105. #define WM_RESIZE_MIN_STEP 8
  106. static unsigned int resize_last_ms;
  107. static unsigned int last_title_ms;
  108. static int last_title_id = -1, last_title_x, last_title_y;
  109. /* Window whose close icon is under the pointer (-1 for none); a change
  110. * repaints only that button instead of the whole desktop. */
  111. static int close_hot = -1;
  112. static int close_hot_old = -1;
  113. static int close_dirty;
  114. static unsigned int prev_buttons;
  115. static int full_redraw = 1;
  116. static int boot_term_spawned;
  117. static int boot_scan_spawned;
  118. static int boot_scan_pid;
  119. static int boot_scan_placed;
  120. static unsigned int boot_scan_deadline;
  121. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  122. static void remap_window_contents(void);
  123. static int bar_temp_c = -1;
  124. static int bar_batt_pct = -1;
  125. static int bar_batt_charging;
  126. static int bar_ac_online = -1;
  127. /* Both top-bar buttons share this geometry: one rounded pill that stops one
  128. * pixel above the rainbow separator (which starts at WM_BAR_H - 5). */
  129. #define BAR_BTN_Y 2
  130. #define BAR_BTN_H 22
  131. #define BAR_BTN_RADIUS 6
  132. /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
  133. * with our own level fill otherwise. The rightmost bar item. */
  134. #define BAR_PWR_W 24
  135. #define BAR_PWR_Y 7
  136. /* The clock, temperature, network and AC/battery labels are one status
  137. * button. draw_bar() records their bounding rectangle; only that rectangle
  138. * opens the control panel, so the empty part of the bar is not clickable. */
  139. static int bar_status_x = -1;
  140. static unsigned int bar_status_w;
  141. static int bar_status_hot;
  142. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  143. * seq changes, then hidden after OSD_MS. */
  144. #define OSD_MS 3000
  145. #define OSD_W 210
  146. #define OSD_H 60
  147. static int osd_show; /* should be visible this frame */
  148. static int osd_visible; /* pixels are on screen */
  149. static int osd_level;
  150. static unsigned int osd_until;
  151. static unsigned int osd_seen_seq;
  152. static int osd_seen_valid;
  153. static int osd_x, osd_y;
  154. static unsigned int osd_w, osd_h;
  155. /* Alt+Tab switcher: the kernel consumes the chord (so the focused window
  156. * never sees the Tab) and queues WM_CHORD_NEXT/PREV events plus the Alt
  157. * held state in wm_status. The WM keeps the MRU order and the selection,
  158. * draws the icon strip, and focuses the selected window when Alt is
  159. * released. sw_visible tracks pixels on screen so a window drag under it
  160. * takes the full repaint path. */
  161. #define SW_ICON_MAX 48
  162. #define SW_CELL_W 68
  163. #define SW_PAD 16
  164. #define SW_LABEL_H 24
  165. static int sw_active;
  166. static unsigned int sw_sel;
  167. static unsigned int sw_count;
  168. static unsigned int sw_slots[WM_MAX_WINDOWS];
  169. static int sw_visible;
  170. static int sw_x, sw_y;
  171. static unsigned int sw_w, sw_h;
  172. /* Most-recently-focused order, maintained from the window focus flags (the
  173. * WM is the only process that moves focus, but the kernel can pick a new
  174. * focused window when one closes). Popovers are filtered out of the list. */
  175. static unsigned int sw_mru[WM_MAX_WINDOWS];
  176. static unsigned int sw_mru_count;
  177. static int sw_last_focus = -1;
  178. static unsigned int sw_chord_seen;
  179. static int sw_chord_seen_valid;
  180. /* Win/Super app runner: a centered query field with ranked app suggestions.
  181. * The kernel toggles the keyboard grab; this side mirrors
  182. * wm_status.runner_active, consumes the raw keys from the same chord ring
  183. * (WM_CHORD_KEY | key), and launches the selected app through sm_launch().
  184. * Enter/Esc are also released by the kernel, so a crashed WM cannot leave
  185. * the keyboard grabbed. */
  186. #define RUN_QUERY_MAX 48
  187. #define RUN_ROWS_MAX 7
  188. #define RUN_ROW_H 28
  189. #define RUN_FIELD_H 34
  190. #define RUN_HINT_H 18
  191. #define RUN_PAD 14
  192. #define RUN_W 560
  193. #define RUN_ICON 20
  194. #define RUN_MAX_APPS 32
  195. static int run_open;
  196. static char run_query[RUN_QUERY_MAX];
  197. static unsigned int run_len, run_caret;
  198. static int run_sel, run_top;
  199. static int run_matches[RUN_MAX_APPS];
  200. static int run_scores[RUN_MAX_APPS];
  201. static int run_nmatch;
  202. static int run_visible;
  203. static int run_x, run_y;
  204. static unsigned int run_w, run_h;
  205. /* The panel height depends on the match count, so a query edit can move and
  206. * resize it. Remember the painted rectangle and damage both the old and new
  207. * one, otherwise the old panel stays on screen. */
  208. static int run_prev_x, run_prev_y;
  209. static unsigned int run_prev_w, run_prev_h;
  210. static int run_prev_valid;
  211. static char bar_msg[24];
  212. static unsigned int bar_msg_until;
  213. static int bar_msg_dirty;
  214. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  215. /* ---- Start menu --------------------------------------------------------
  216. * The NebOS banner in the top-left corner is the start button. It opens a
  217. * Windows-like panel drawn by the WM itself (not a window, so it never
  218. * consumes a WM_MAX_WINDOWS slot and floats above every canvas). The panel
  219. * lists every GUI app in the pack and ends with a Power row whose submenu
  220. * reboots or powers off the machine through the privileged kernel command
  221. * registry (SYS_SPAWN_KCMD). */
  222. /* The panel mirrors ngui_theme_dark and the netmenu popover: a flat
  223. * theme.bg surface without a rim, rounded theme.panel summary cards,
  224. * theme.panel_alt hover rows and the selection colour for an active item. */
  225. #define SM_BG 0x181C28u
  226. #define SM_PANEL 0x262B3Au
  227. #define SM_PANEL_ALT 0x32384Au
  228. #define SM_TEXT 0xE4E6F0u
  229. #define SM_TEXT_DIM 0x9197AAu
  230. #define SM_SEL 0x365CA6u
  231. #define SM_BTN_HOT 0x46516Cu
  232. #define SM_ERROR 0xEE7474u
  233. #define SM_W 252
  234. #define SM_PAD 12
  235. #define SM_TOP 10
  236. #define SM_GAP 6
  237. #define SM_SECT_H 22
  238. #define SM_ROW_H 26
  239. #define SM_MIN_ROW 20
  240. #define SM_BOTTOM 10
  241. #define SM_RADIUS 6
  242. #define SM_SUB_W 160
  243. #define SM_TILE 18
  244. #define SM_HIT_NONE (-1)
  245. #define SM_HIT_START (-2)
  246. #define SM_HIT_POWER (-3)
  247. #define SM_HIT_REBOOT (-4)
  248. #define SM_HIT_POWEROFF (-5)
  249. #define SM_HIT_MENU (-6) /* inside the panel but not on an entry */
  250. #define SM_ACT_SPAWN 0
  251. #define SM_ACT_TERM 1
  252. #define SM_ACT_SETTINGS 2
  253. struct sm_app {
  254. const char *label;
  255. const char *path;
  256. const char *arg;
  257. unsigned int tile;
  258. char letter;
  259. unsigned char action;
  260. unsigned char icon; /* APP_ICON_* */
  261. const char *win_title; /* exact canvas title for running/focus */
  262. };
  263. /* Every self-contained GUI application in the pack. Terminal and Settings
  264. * are WM actions (a new terminal canvas and the Settings window); Textedit
  265. * starts with an empty Untitled buffer and prompts for a path when saved. */
  266. static const struct sm_app sm_apps[] = {
  267. { "Terminal", "", "", 0x3C8CD2u, 'T', SM_ACT_TERM, APP_ICON_TERMINAL, "term" },
  268. { "Files", "/ramfs/bin/files", "", 0xE0A030u, 'F', SM_ACT_SPAWN, APP_ICON_FILES, "Files" },
  269. { "Web", "/ramfs/bin/browser", "", 0x4CA0E0u, 'W', SM_ACT_SPAWN, APP_ICON_WEB, "Browser" },
  270. { "Gallery", "/ramfs/bin/gallery", "", 0xC864B4u, 'G', SM_ACT_SPAWN, APP_ICON_GALLERY, "ngui gallery" },
  271. { "Chat", "/ramfs/bin/chat", "", 0x55A055u, 'C', SM_ACT_SPAWN, APP_ICON_CHAT, "Chat" },
  272. { "Clock", "/ramfs/bin/clock", "", 0x8C8C9Cu, 'C', SM_ACT_SPAWN, APP_ICON_CLOCK, "clock" },
  273. { "Textedit", "/ramfs/bin/textedit", "", 0xD27850u, 'E', SM_ACT_SPAWN, APP_ICON_EDITOR, "Textedit" },
  274. { "Colors", "/ramfs/bin/colors", "", 0xC85A5Au, 'P', SM_ACT_SPAWN, APP_ICON_COLORS, "colors" },
  275. { "Mines", "/ramfs/bin/mines", "", 0x8C6CD2u, 'M', SM_ACT_SPAWN, APP_ICON_MINES, "mines" },
  276. { "Cards", "/ramfs/bin/solitaire","", 0x3CA08Cu, 'S', SM_ACT_SPAWN, APP_ICON_CARDS, "Solitaire" },
  277. { "Disks", "/ramfs/bin/disks", "", 0x5A8CC8u, 'D', SM_ACT_SPAWN, APP_ICON_DISKS, "Disks" },
  278. { "Tasks", "/ramfs/bin/process", "", 0xE0B040u, 'P', SM_ACT_SPAWN, APP_ICON_TASKS, "Processes" },
  279. { "Services", "/ramfs/bin/service", "--gui", 0x9A6AC8u, 'V', SM_ACT_SPAWN, APP_ICON_SERVICES, "Services" },
  280. { "Settings", "", "", 0x7A7A9Au, 'S', SM_ACT_SETTINGS, APP_ICON_SETTINGS, "Settings" },
  281. { "Date & Time", "/ramfs/bin/settime", "", 0x5AA0A0u, 'D', SM_ACT_SPAWN, APP_ICON_DATETIME, "Date & Time" },
  282. };
  283. #define SM_NAPP ((int)(sizeof(sm_apps) / sizeof(sm_apps[0])))
  284. struct sm_layout {
  285. int x, y, w, h;
  286. int row_h;
  287. int sect_y;
  288. int row_x, row_w;
  289. int list_y;
  290. int power_y;
  291. int sub_x, sub_y, sub_w, sub_h;
  292. };
  293. static int sm_open;
  294. static int sm_power_open;
  295. static int sm_start_hot;
  296. static int sm_hot; /* hovered app index, -1 = none */
  297. static int sm_power_hot; /* 0 none, 1 Power, 2 Reboot, 3 Power off */
  298. static int poweroff_pid; /* the kcmd spawned by Power off */
  299. static unsigned int poweroff_deadline;
  300. static void sm_layout_get(struct sm_layout *l);
  301. static void sm_draw(void);
  302. static void sm_add_present(struct wm_present_info *p);
  303. static int sm_update_hover(unsigned int mx, unsigned int my);
  304. static void sm_close(void);
  305. static void sm_launch(int idx);
  306. static void sm_tile(int x, int y, int size, unsigned int rgb, char letter);
  307. /* Alt+Tab switcher overlay (defined next to the app-icon helpers). */
  308. static void sw_geom(void);
  309. static void sw_draw(void);
  310. static void sw_add_present(struct wm_present_info *p);
  311. /* Win/Super app runner overlay (defined with the switcher state). */
  312. static void run_geom(void);
  313. static void run_draw(void);
  314. static void run_add_present(struct wm_present_info *p);
  315. /* Bottom dock (defined with the start-menu/launch helpers further down). */
  316. static void dock_update(unsigned int mx, unsigned int my);
  317. static void dock_draw(void);
  318. static void dock_damage(struct wm_present_info *p);
  319. static void dock_mark_drawn(void);
  320. static int dock_click_at(unsigned int mx, unsigned int my);
  321. static void dock_capture_update(unsigned int mx, unsigned int my,
  322. unsigned int buttons);
  323. static int dock_icon_at(unsigned int mx, unsigned int my);
  324. static int dock_pointer_inside(unsigned int mx, unsigned int my);
  325. /* Network status icon (right side of the bar). */
  326. #define BAR_NET_W 24
  327. #define BAR_NET_Y 7
  328. #define BAR_NET_NONE 0
  329. #define BAR_NET_WIRED 1
  330. #define BAR_NET_WIFI 2
  331. #define BAR_NET_USB 3
  332. static int bar_net_kind;
  333. static int bar_net_bars;
  334. static unsigned int bar_net_color = 0xFF5050u;
  335. static int control_pid;
  336. /* Bottom dock (the drawing and hit-test code lives near the launch
  337. * helpers, after the start menu; do_update needs the state earlier). */
  338. #define DOCK_BASE_MAX 48
  339. #define DOCK_BASE_MIN 24
  340. #define DOCK_PAD 14
  341. #define DOCK_PANEL_EXTRA 14
  342. #define DOCK_BOTTOM 8
  343. #define DOCK_ICON_BOTTOM 7
  344. #define DOCK_SCALE_ONE 256
  345. #define DOCK_SCALE_MAX 448 /* 1.75x in 8.8 fixed point */
  346. #define DOCK_SEP_GAP 6
  347. #define DOCK_LABEL_H 26
  348. #define DOCK_LABEL_GAP 32
  349. static const unsigned char dock_apps[] = {
  350. 0, 1, 2, 4, 6, 3, 5, 7, 8, 9, 10, 11, 13
  351. };
  352. #define DOCK_N ((int)(sizeof(dock_apps) / sizeof(dock_apps[0])))
  353. struct dock_layout {
  354. int base, gap, pad;
  355. int panel_x, panel_y, panel_w, panel_h, panel_bottom;
  356. int icon_bottom;
  357. int x[DOCK_N], y[DOCK_N], size[DOCK_N];
  358. int base_center[DOCK_N];
  359. int label_idx, label_x, label_y, label_w, label_h;
  360. struct wm_rect visual;
  361. };
  362. static struct dock_layout dock;
  363. static unsigned short dock_scale[DOCK_N];
  364. static int dock_scale_ready;
  365. static int dock_active;
  366. static int dock_hover;
  367. static int dock_dirty;
  368. static int dock_captured;
  369. static struct wm_rect dock_prev;
  370. static int dock_prev_valid;
  371. static unsigned int dock_last_paint_ms;
  372. static void bar_notice(const char *msg)
  373. {
  374. unsigned int i;
  375. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  376. bar_msg[i] = msg[i];
  377. bar_msg[i] = '\0';
  378. bar_msg_until = u_ticks_ms() + 2000;
  379. bar_msg_dirty = 1;
  380. }
  381. static int free_window_slot(void)
  382. {
  383. unsigned int i;
  384. for (i = 0; i < WM_MAX_WINDOWS; i++)
  385. if (!wins[i].info.used)
  386. return 1;
  387. return 0;
  388. }
  389. /* Find a window by owner pid without waiting for the cached sync state:
  390. * boot ordering needs it before the terminal is spawned. */
  391. static int window_index_pid(int pid, struct wm_window_info *out)
  392. {
  393. unsigned int i;
  394. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  395. struct wm_window_info info;
  396. if (u_wm_info(i, &info) == 1 && info.used &&
  397. (int)info.pid == pid) {
  398. if (out)
  399. *out = info;
  400. return (int)i;
  401. }
  402. }
  403. return -1;
  404. }
  405. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  406. {
  407. return (r << 16) | (g << 8) | b;
  408. }
  409. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  410. * integer interpolation the kernel bar uses. */
  411. static unsigned int rainbow_rgb(unsigned int hue)
  412. {
  413. unsigned int seg = hue / 256, f = hue % 256;
  414. unsigned int r1, g1, b1, r2, g2, b2;
  415. switch (seg) {
  416. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  417. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  418. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  419. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  420. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  421. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  422. }
  423. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  424. (g1 * (256 - f) + g2 * f) / 256,
  425. (b1 * (256 - f) + b2 * f) / 256);
  426. }
  427. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  428. unsigned int rgb, int cx, int cy,
  429. unsigned int cw, unsigned int ch)
  430. {
  431. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  432. if (x0 < cx) x0 = cx;
  433. if (y0 < cy) y0 = cy;
  434. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  435. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  436. if (x1 > x0 && y1 > y0)
  437. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  438. (unsigned int)(y1 - y0), rgb);
  439. }
  440. /* Rounded rectangle fill, matching ngui_fill_round_rect()'s pixel-centre
  441. * corner sampling (the popover's cards and tiles use a 6 px radius). */
  442. static int sm_isqrt(int v)
  443. {
  444. int r = 0, bit = 1 << 30;
  445. if (v <= 0)
  446. return 0;
  447. while (bit > v)
  448. bit >>= 2;
  449. while (bit) {
  450. if (v >= r + bit) {
  451. v -= r + bit;
  452. r = (r >> 1) + bit;
  453. } else {
  454. r >>= 1;
  455. }
  456. bit >>= 2;
  457. }
  458. return r;
  459. }
  460. static int sm_round_inset(int y, int h, int r)
  461. {
  462. int dy2, d2;
  463. if (r <= 0)
  464. return 0;
  465. if (y < r)
  466. dy2 = 2 * r - 2 * y - 1;
  467. else if (y >= h - r)
  468. dy2 = 2 * (y - (h - r)) + 1;
  469. else
  470. return 0;
  471. d2 = 4 * r * r - dy2 * dy2;
  472. if (d2 < 0)
  473. d2 = 0;
  474. return r - (sm_isqrt(d2) + 1) / 2;
  475. }
  476. static void sm_fill_round(int x, int y, int w, int h, int r,
  477. unsigned int rgb)
  478. {
  479. int yy, half;
  480. if (w <= 0 || h <= 0)
  481. return;
  482. half = w < h ? w / 2 : h / 2;
  483. if (r > half)
  484. r = half;
  485. if (r <= 0) {
  486. usurf_fill(&screen, x, y, (unsigned int)w, (unsigned int)h,
  487. rgb);
  488. return;
  489. }
  490. for (yy = 0; yy < h; yy++) {
  491. int li = 0, ri = 0;
  492. if (yy < r) {
  493. li = sm_round_inset(yy, h, r);
  494. ri = li;
  495. }
  496. if (yy >= h - r) {
  497. int i = sm_round_inset(yy, h, r);
  498. if (i > li)
  499. li = i;
  500. if (i > ri)
  501. ri = i;
  502. }
  503. if (w - li - ri > 0)
  504. usurf_fill(&screen, x + li, y + yy,
  505. (unsigned int)(w - li - ri), 1, rgb);
  506. }
  507. }
  508. /* One shared renderer for the start and status buttons: the same height,
  509. * corner radius and hover/active colours. */
  510. static void bar_button_bg(int x, int w, int active, int hot)
  511. {
  512. if (active)
  513. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  514. SM_SEL);
  515. else if (hot)
  516. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  517. SM_PANEL_ALT);
  518. }
  519. /* The NebOS banner doubles as the start button. Its width follows the
  520. * proportional font, so the hit area and the highlight use the same
  521. * geometry. */
  522. static void bar_start_geom(int *x, int *y, int *w, int *h)
  523. {
  524. *x = 2;
  525. *y = BAR_BTN_Y;
  526. *h = BAR_BTN_H;
  527. *w = (int)usurf_text_width(&screen, OS_NAME " v" OS_VERSION) + 20;
  528. }
  529. static int bar_start_hit(unsigned int mx, unsigned int my)
  530. {
  531. int x, y, w, h;
  532. bar_start_geom(&x, &y, &w, &h);
  533. return (int)mx >= x && (int)mx < x + w &&
  534. (int)my >= y && (int)my < y + h;
  535. }
  536. static void bar_start_button(void)
  537. {
  538. int x, y, w, h;
  539. bar_start_geom(&x, &y, &w, &h);
  540. bar_button_bg(x, w, sm_open, sm_start_hot);
  541. usurf_text(&screen, x + 10, y + (h - 16) / 2,
  542. OS_NAME " v" OS_VERSION,
  543. sm_open ? COL_WHITE : COL_BANNER, 0, 0, fbw, WM_BAR_H);
  544. }
  545. static int name_is(const char *a, const char *b)
  546. {
  547. while (*a && *b) {
  548. if (*a != *b)
  549. return 0;
  550. a++;
  551. b++;
  552. }
  553. return *a == *b;
  554. }
  555. static int net_rssi_bars(int rssi)
  556. {
  557. if (rssi >= -55)
  558. return 4;
  559. if (rssi >= -65)
  560. return 3;
  561. if (rssi >= -75)
  562. return 2;
  563. return 1;
  564. }
  565. /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
  566. * in their 24 px hit slots. */
  567. static void bar_draw_net_icon(int x, int y)
  568. {
  569. enum uwm_icon ic;
  570. switch (bar_net_kind) {
  571. case BAR_NET_WIRED:
  572. ic = UWM_ICON_NET_WIRED;
  573. break;
  574. case BAR_NET_WIFI:
  575. ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
  576. UWM_ICON_NET_WIFI_OFF;
  577. break;
  578. case BAR_NET_USB:
  579. ic = UWM_ICON_NET_USB;
  580. break;
  581. default:
  582. ic = UWM_ICON_NET_WIRED_OFF;
  583. break;
  584. }
  585. (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
  586. }
  587. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  588. * only made when WiFi is the active netdev, so a machine without a card does
  589. * not run a PCI scan on every tick. */
  590. static int poll_net_status(void)
  591. {
  592. struct sys_netinfo ni;
  593. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  594. unsigned int color = 0xFF5050u;
  595. int changed;
  596. int i;
  597. if (u_net_info(&ni) == 1 && ni.present) {
  598. for (i = 0; i < 4; i++)
  599. if (ni.ip[i])
  600. have_ip = 1;
  601. if (name_is(ni.name, "wifi")) {
  602. struct sys_wifi_status ws;
  603. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  604. ws.present) {
  605. kind = BAR_NET_WIFI;
  606. if (ws.state >= SYS_WIFI_ST_ASSOC) {
  607. bars = net_rssi_bars(ws.rssi);
  608. color = (ws.state >= SYS_WIFI_ST_LINK &&
  609. have_ip) ?
  610. 0x55FF55u : 0xFFD250u;
  611. } else {
  612. /* Card present, not associated. */
  613. bars = 0;
  614. color = 0xFF5050u;
  615. }
  616. }
  617. } else if (name_is(ni.name, "rndis")) {
  618. kind = BAR_NET_USB;
  619. color = have_ip ? 0x55FF55u : 0xFFD250u;
  620. } else {
  621. kind = BAR_NET_WIRED;
  622. color = have_ip ? 0x55FF55u : 0xFFD250u;
  623. }
  624. }
  625. changed = kind != bar_net_kind || bars != bar_net_bars ||
  626. color != bar_net_color;
  627. bar_net_kind = kind;
  628. bar_net_bars = bars;
  629. bar_net_color = color;
  630. return changed;
  631. }
  632. static unsigned int temp_color(int c)
  633. {
  634. if (c >= 80)
  635. return 0xFF5050u;
  636. if (c >= 60)
  637. return 0xFFD250u;
  638. return 0x55FF55u;
  639. }
  640. /* "43C" (no libc printf here). */
  641. static unsigned int temp_text(char *b, int c)
  642. {
  643. unsigned int n = 0, v = (unsigned int)c;
  644. if (v >= 100)
  645. b[n++] = (char)('0' + (v / 100) % 10);
  646. if (v >= 10)
  647. b[n++] = (char)('0' + (v / 10) % 10);
  648. b[n++] = (char)('0' + v % 10);
  649. b[n++] = 'C';
  650. b[n] = '\0';
  651. return n;
  652. }
  653. static unsigned int batt_color(int pct)
  654. {
  655. if (pct <= 10)
  656. return 0xFF5050u;
  657. return 0x55FF55u;
  658. }
  659. /* Battery: the Adwaita outline with our own level fill over the lower
  660. * interior. The symbol's body is striped, so the interior is cleared to a
  661. * dim shade of the state colour first; the level then reads as a bright
  662. * block inside the outline. A critical charge turns the outline red too.
  663. * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
  664. * y4..13. */
  665. static unsigned int batt_dim(unsigned int c)
  666. {
  667. return (c >> 2) & 0x3F3F3F;
  668. }
  669. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  670. {
  671. unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
  672. int ix = x + 4;
  673. if (fill > 10)
  674. fill = 10;
  675. if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
  676. return;
  677. usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
  678. if (fill)
  679. usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
  680. }
  681. /* AC adapter: the Adwaita plug symbol. */
  682. static void bar_icon_ac(int x, int y, unsigned int c)
  683. {
  684. (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
  685. }
  686. static void draw_bar(void)
  687. {
  688. char dt[48];
  689. unsigned int n, i, x, y;
  690. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  691. bar_start_button();
  692. if (bar_msg_until &&
  693. (int)(u_ticks_ms() - bar_msg_until) < 0)
  694. usurf_text(&screen, (int)(WM_BAR_LEFT_END + 8),
  695. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  696. /* Rainbow separator (same y as the kernel's old bar). */
  697. for (x = 0; x < fbw; x++) {
  698. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  699. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  700. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  701. }
  702. if (bar_kexec_busy) {
  703. static const char msg[] = "loading new kernel over serial";
  704. unsigned int dots = bar_kexec_total ?
  705. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  706. bar_status_x = -1;
  707. bar_status_w = 0;
  708. n = 0;
  709. while (msg[n])
  710. dt[n] = msg[n], n++;
  711. for (i = 0; i < 12; i++)
  712. dt[n + i] = (i < dots) ? '.' : ' ';
  713. n += 12;
  714. dt[n] = '\0';
  715. usurf_text(&screen,
  716. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  717. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  718. } else {
  719. int right = (int)fbw - 4;
  720. int min_x = (int)WM_BAR_LEFT_END + 4;
  721. int pwr_px = -1, net_nx = -1, clock_tx = -1, temp_tx = -1;
  722. int temp_tw;
  723. int left;
  724. char cdt[12], tb[12];
  725. const char *clock_txt = dt;
  726. /* Lay the cluster out right to left: AC/battery, network, the
  727. * date/time and the CPU temperature. */
  728. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  729. pwr_px = right - BAR_PWR_W;
  730. if (pwr_px >= min_x)
  731. right = pwr_px - 8;
  732. else
  733. pwr_px = -1;
  734. }
  735. net_nx = right - BAR_NET_W;
  736. if (net_nx >= min_x)
  737. right = net_nx - 8;
  738. else
  739. net_nx = -1;
  740. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  741. int tw = (int)usurf_text_width(&screen, dt);
  742. int tx = right - tw;
  743. unsigned int k;
  744. if (tx < min_x) {
  745. /* Compact form "MM-DD HH:MM" from
  746. * "YYYY-MM-DD HH:MM:SS" when the status
  747. * cluster is too wide for the full date. */
  748. for (k = 0; k < 11 && dt[5 + k]; k++)
  749. cdt[k] = dt[5 + k];
  750. cdt[k] = '\0';
  751. clock_txt = cdt;
  752. tw = (int)usurf_text_width(&screen, cdt);
  753. tx = right - tw;
  754. }
  755. if (tx >= min_x) {
  756. clock_tx = tx;
  757. right = tx - 8;
  758. }
  759. }
  760. if (bar_temp_c >= 0) {
  761. (void)temp_text(tb, bar_temp_c);
  762. temp_tw = (int)usurf_text_width(&screen, tb);
  763. temp_tx = right - temp_tw;
  764. if (temp_tx < min_x)
  765. temp_tx = -1;
  766. }
  767. /* The status labels form one click target: record the whole
  768. * cluster and paint one rounded hover pill behind it. */
  769. left = pwr_px;
  770. if (net_nx >= 0)
  771. left = net_nx;
  772. if (clock_tx >= 0)
  773. left = clock_tx;
  774. if (temp_tx >= 0)
  775. left = temp_tx;
  776. if (left >= 0) {
  777. bar_status_x = left - 4;
  778. if (bar_status_x < 0)
  779. bar_status_x = 0;
  780. bar_status_w = (unsigned int)((int)fbw - 2 -
  781. bar_status_x);
  782. bar_button_bg(bar_status_x, (int)bar_status_w,
  783. control_pid > 0, bar_status_hot);
  784. } else {
  785. bar_status_x = -1;
  786. bar_status_w = 0;
  787. }
  788. if (pwr_px >= 0) {
  789. if (bar_ac_online > 0)
  790. bar_icon_ac(pwr_px, BAR_PWR_Y, 0x55FF55u);
  791. else
  792. bar_icon_battery(pwr_px, BAR_PWR_Y,
  793. bar_batt_pct,
  794. batt_color(bar_batt_pct));
  795. }
  796. if (net_nx >= 0)
  797. bar_draw_net_icon(net_nx, BAR_NET_Y);
  798. if (clock_tx >= 0)
  799. usurf_text(&screen, clock_tx, 5, clock_txt, COL_BANNER,
  800. 0, 0, fbw, WM_BAR_H);
  801. if (temp_tx >= 0)
  802. usurf_text(&screen, temp_tx, 5, tb,
  803. temp_color(bar_temp_c), 0, 0, fbw,
  804. WM_BAR_H);
  805. }
  806. }
  807. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  808. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  809. unsigned int cw, unsigned int ch)
  810. {
  811. struct uwin *u = &wins[idx];
  812. struct wm_window_info *w = &u->info;
  813. unsigned int cwid = w->content_w, chei = w->content_h;
  814. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  815. struct uwm_canvas c;
  816. c.px = (const unsigned short *)u->map.va;
  817. c.w = u->map.w;
  818. c.h = u->map.h;
  819. if (c.w == cwid && c.h == chei) {
  820. usurf_canvas(&screen, &c, w->x, w->y,
  821. cx, cy, cw, ch);
  822. return;
  823. }
  824. }
  825. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  826. }
  827. static void draw_window_clip(unsigned int idx, int cx, int cy,
  828. unsigned int cw, unsigned int ch)
  829. {
  830. struct uwin *u = &wins[idx];
  831. struct wm_window_info *w = &u->info;
  832. int x = w->x, y = w->y;
  833. unsigned int pw = w->w;
  834. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W - WM_CLOSE_INSET;
  835. int cony = y + WM_BORDER_W + WM_TITLE_H;
  836. int content_x = x + WM_BORDER_W;
  837. unsigned int content_w = w->content_w;
  838. unsigned int content_h = w->content_h;
  839. if (w->popover) {
  840. draw_popover_clip(idx, cx, cy, cw, ch);
  841. return;
  842. }
  843. /* Title bar and close icon; windows have no visible frame. The title
  844. * text is clipped where the icon starts, so a long title never runs
  845. * under the button. */
  846. fill_clip(x, y, pw, WM_TITLE_H,
  847. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  848. {
  849. int tx0 = x + WM_TITLE_PAD, ty0 = y;
  850. int tx1 = bx - 4, ty1 = y + WM_TITLE_H;
  851. if (tx0 < cx) tx0 = cx;
  852. if (ty0 < cy) ty0 = cy;
  853. if (tx1 > cx + (int)cw) tx1 = cx + (int)cw;
  854. if (ty1 > cy + (int)ch) ty1 = cy + (int)ch;
  855. if (tx1 > tx0 && ty1 > ty0)
  856. usurf_text(&screen, x + WM_TITLE_PAD, y + 2,
  857. w->title, COL_WHITE, tx0, ty0,
  858. (unsigned int)(tx1 - tx0),
  859. (unsigned int)(ty1 - ty0));
  860. }
  861. (void)usurf_icon_clip(&screen, bx,
  862. y + (WM_TITLE_H - WM_CLOSE_ICON_PX) / 2,
  863. UWM_ICON_CLOSE,
  864. close_hot == (int)idx ? COL_CLOSE_HOT :
  865. COL_CLOSE_ICON,
  866. cx, cy, cw, ch);
  867. /* Content: every window presents an RGB565 canvas (the terminal
  868. * renders its cells into one; gfx commands own theirs). A mapped
  869. * canvas covers the whole content area, so it does not need the black
  870. * underlay. */
  871. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  872. struct uwm_canvas c;
  873. c.px = (const unsigned short *)u->map.va;
  874. c.w = u->map.w;
  875. c.h = u->map.h;
  876. if (c.w != content_w || c.h != content_h)
  877. fill_clip(content_x, cony, content_w, content_h,
  878. 0x000000u, cx, cy, cw, ch);
  879. usurf_canvas(&screen, &c, content_x, cony,
  880. cx, cy, cw, ch);
  881. } else {
  882. fill_clip(content_x, cony, content_w, content_h,
  883. 0x000000u, cx, cy, cw, ch);
  884. }
  885. }
  886. /* Front-to-back window order: popovers float above everything, then the
  887. * focused window, then the others in descending slot order. Painting and
  888. * hit-testing share it. */
  889. static unsigned int build_order(int *order)
  890. {
  891. unsigned int i, n = 0;
  892. for (i = 0; i < WM_MAX_WINDOWS; i++)
  893. if (wins[i].info.used && wins[i].info.popover)
  894. order[n++] = (int)i;
  895. for (i = 0; i < WM_MAX_WINDOWS; i++)
  896. if (wins[i].info.used && wins[i].info.focused &&
  897. !wins[i].info.popover)
  898. order[n++] = (int)i;
  899. for (i = WM_MAX_WINDOWS; i-- > 0;)
  900. if (wins[i].info.used && !wins[i].info.focused &&
  901. !wins[i].info.popover)
  902. order[n++] = (int)i;
  903. return n;
  904. }
  905. struct orect {
  906. int x, y;
  907. unsigned int w, h;
  908. };
  909. #define OCC_MAX 64
  910. /* r minus cut, split into up to four disjoint rectangles. */
  911. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  912. const struct orect *cut)
  913. {
  914. int rx0 = r->x, ry0 = r->y;
  915. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  916. int cx0 = cut->x, cy0 = cut->y;
  917. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  918. unsigned int n = 0;
  919. if (cx0 < rx0) cx0 = rx0;
  920. if (cy0 < ry0) cy0 = ry0;
  921. if (cx1 > rx1) cx1 = rx1;
  922. if (cy1 > ry1) cy1 = ry1;
  923. if (cx1 <= cx0 || cy1 <= cy0) {
  924. out[0] = *r;
  925. return 1;
  926. }
  927. if (cy0 > ry0) {
  928. out[n].x = rx0;
  929. out[n].y = ry0;
  930. out[n].w = (unsigned int)(rx1 - rx0);
  931. out[n].h = (unsigned int)(cy0 - ry0);
  932. n++;
  933. }
  934. if (cy1 < ry1) {
  935. out[n].x = rx0;
  936. out[n].y = cy1;
  937. out[n].w = (unsigned int)(rx1 - rx0);
  938. out[n].h = (unsigned int)(ry1 - cy1);
  939. n++;
  940. }
  941. if (cx0 > rx0) {
  942. out[n].x = rx0;
  943. out[n].y = cy0;
  944. out[n].w = (unsigned int)(cx0 - rx0);
  945. out[n].h = (unsigned int)(cy1 - cy0);
  946. n++;
  947. }
  948. if (cx1 < rx1) {
  949. out[n].x = cx1;
  950. out[n].y = cy0;
  951. out[n].w = (unsigned int)(rx1 - cx1);
  952. out[n].h = (unsigned int)(cy1 - cy0);
  953. n++;
  954. }
  955. return n;
  956. }
  957. static void wall_build_maps(void)
  958. {
  959. unsigned int i;
  960. if (screen.bpp == 32) {
  961. for (i = 0; i < 65536; i++) {
  962. unsigned int r = (i >> 11) & 31;
  963. unsigned int g = (i >> 5) & 63;
  964. unsigned int b = i & 31;
  965. wall_rgb32[i] = 0xFF000000u |
  966. ((r << 3 | r >> 2) << 16) |
  967. ((g << 2 | g >> 4) << 8) |
  968. (b << 3 | b >> 2);
  969. }
  970. }
  971. for (i = 0; i < WALL_MAP_MAX; i++) {
  972. unsigned int sx = 0, sy = 0;
  973. if (fbw && i < fbw)
  974. sx = (unsigned int)((unsigned long long)i * wall_w / fbw);
  975. if (fbh && i < fbh)
  976. sy = (unsigned int)((unsigned long long)i * wall_h / fbh);
  977. if (sx >= wall_w)
  978. sx = wall_w - 1;
  979. if (sy >= wall_h)
  980. sy = wall_h - 1;
  981. wall_xmap[i] = (unsigned short)sx;
  982. wall_ymap[i] = (unsigned short)sy;
  983. }
  984. }
  985. static int wall_magic_ok(const struct wm_wallpaper_hdr *h)
  986. {
  987. unsigned int i;
  988. for (i = 0; i < sizeof(h->magic); i++)
  989. if (h->magic[i] != WM_WALLPAPER_MAGIC[i])
  990. return 0;
  991. return 1;
  992. }
  993. /* Read the persistent copy first, then the RAM copy. The FAT driver may
  994. * write a final partial sector past the requested length, so wall_data has
  995. * 512 bytes of slack and the read is capped one sector below its size. */
  996. static void wall_load(void)
  997. {
  998. static const char *const paths[] = {
  999. WM_WALLPAPER_DATA, WM_WALLPAPER_SHARE
  1000. };
  1001. struct wm_wallpaper_hdr *h = (struct wm_wallpaper_hdr *)wall_data;
  1002. unsigned int i, pixbytes;
  1003. int rn;
  1004. wall_ready = 0;
  1005. for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  1006. rn = u_syscall3(SYS_READ_FILE, (int)paths[i], (int)wall_data,
  1007. (int)(sizeof(wall_data) - 512));
  1008. if (rn < (int)sizeof(*h) || !wall_magic_ok(h))
  1009. continue;
  1010. if (!h->width || !h->height ||
  1011. h->width > WM_WALLPAPER_MAX_W ||
  1012. h->height > WM_WALLPAPER_MAX_H)
  1013. continue;
  1014. pixbytes = h->width * h->height * 2u;
  1015. if ((unsigned int)rn < sizeof(*h) + pixbytes)
  1016. continue;
  1017. wall_w = h->width;
  1018. wall_h = h->height;
  1019. wall_ready = 1;
  1020. wall_build_maps();
  1021. return;
  1022. }
  1023. }
  1024. static void apply_prefs(void)
  1025. {
  1026. desktop_rgb = prefs.bg_rgb & 0x00FFFFFFu;
  1027. }
  1028. static int prefs_file_magic_ok(const struct wm_prefs_file *f)
  1029. {
  1030. unsigned int i;
  1031. for (i = 0; i < sizeof(f->magic); i++)
  1032. if (f->magic[i] != WM_PREFS_MAGIC[i])
  1033. return 0;
  1034. return 1;
  1035. }
  1036. /* Read one preference file and push it through the kernel, which also
  1037. * updates this WM on the next poll. Returns 0 when a file was accepted. */
  1038. static int prefs_read_file(const char *path)
  1039. {
  1040. /* Leave a sector of slack: the FAT driver may write the final partial
  1041. * sector past the requested length. */
  1042. unsigned char buf[sizeof(struct wm_prefs_file) + 512];
  1043. struct wm_prefs_file *f = (struct wm_prefs_file *)buf;
  1044. int n;
  1045. n = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)sizeof(*f));
  1046. if (n < (int)sizeof(*f) || !prefs_file_magic_ok(f) ||
  1047. f->version != WM_PREFS_VERSION)
  1048. return -1;
  1049. return u_wm_set_prefs(&f->prefs);
  1050. }
  1051. static int prefs_load(void)
  1052. {
  1053. if (prefs_read_file(WM_PREFS_DATA) == 0)
  1054. return 0;
  1055. return prefs_read_file(WM_PREFS_SHARE);
  1056. }
  1057. /* Paint the desktop part of one damage rectangle. */
  1058. static void wall_draw(int x, int y, unsigned int w, unsigned int h)
  1059. {
  1060. const unsigned short *src = (const unsigned short *)(wall_data +
  1061. sizeof(struct wm_wallpaper_hdr));
  1062. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  1063. unsigned int yy, xx;
  1064. if (!wall_ready) {
  1065. usurf_fill(&screen, x, y, w, h, desktop_rgb);
  1066. return;
  1067. }
  1068. if (x0 < 0) x0 = 0;
  1069. if (y0 < 0) y0 = 0;
  1070. if (x1 > (int)fbw) x1 = (int)fbw;
  1071. if (y1 > (int)fbh) y1 = (int)fbh;
  1072. if (x1 > WALL_MAP_MAX) x1 = WALL_MAP_MAX;
  1073. if (y1 > WALL_MAP_MAX) y1 = WALL_MAP_MAX;
  1074. if (x1 <= x0 || y1 <= y0)
  1075. return;
  1076. usurf_pixels += (unsigned int)(x1 - x0) * (unsigned int)(y1 - y0);
  1077. for (yy = (unsigned int)y0; yy < (unsigned int)y1; yy++) {
  1078. const unsigned short *srow = src + (unsigned int)wall_ymap[yy] *
  1079. wall_w;
  1080. /* d points at the start of the row; the loops index it with
  1081. * absolute screen x. Pointing d at x0 and still indexing with
  1082. * x shifted every damage rect's wallpaper right by x0, which
  1083. * left old window pixels in the uncovered strip and made
  1084. * dragging windows leave a trail. */
  1085. if (screen.bpp == 16) {
  1086. unsigned short *d = (unsigned short *)(screen.px +
  1087. (unsigned long)yy * screen.pitch);
  1088. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
  1089. d[xx] = srow[wall_xmap[xx]];
  1090. } else if (screen.bpp == 32) {
  1091. unsigned int *d = (unsigned int *)(screen.px +
  1092. (unsigned long)yy * screen.pitch);
  1093. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
  1094. d[xx] = wall_rgb32[srow[wall_xmap[xx]]];
  1095. } else {
  1096. unsigned char *d = screen.px +
  1097. (unsigned long)yy * screen.pitch + x0 * 3;
  1098. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++) {
  1099. unsigned int c = srow[wall_xmap[xx]];
  1100. unsigned int r = (c >> 11) & 31;
  1101. unsigned int g = (c >> 5) & 63;
  1102. unsigned int b = c & 31;
  1103. d[0] = (unsigned char)(b << 3 | b >> 2);
  1104. d[1] = (unsigned char)(g << 2 | g >> 4);
  1105. d[2] = (unsigned char)(r << 3 | r >> 2);
  1106. d += 3;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. /* Back-to-front repaint, used only if the visible-region list overflows. */
  1112. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  1113. {
  1114. unsigned int i;
  1115. wall_draw(x, y, w, h);
  1116. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1117. if (wins[i].info.used && !wins[i].info.focused &&
  1118. !wins[i].info.popover &&
  1119. x < wins[i].info.x + (int)wins[i].info.w &&
  1120. x + (int)w > wins[i].info.x &&
  1121. y < wins[i].info.y + (int)wins[i].info.h &&
  1122. y + (int)h > wins[i].info.y)
  1123. draw_window_clip(i, x, y, w, h);
  1124. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1125. if (wins[i].info.used && wins[i].info.focused &&
  1126. !wins[i].info.popover &&
  1127. x < wins[i].info.x + (int)wins[i].info.w &&
  1128. x + (int)w > wins[i].info.x &&
  1129. y < wins[i].info.y + (int)wins[i].info.h &&
  1130. y + (int)h > wins[i].info.y)
  1131. draw_window_clip(i, x, y, w, h);
  1132. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1133. if (wins[i].info.used && wins[i].info.popover &&
  1134. x < wins[i].info.x + (int)wins[i].info.w &&
  1135. x + (int)w > wins[i].info.x &&
  1136. y < wins[i].info.y + (int)wins[i].info.h &&
  1137. y + (int)h > wins[i].info.y)
  1138. draw_window_clip(i, x, y, w, h);
  1139. }
  1140. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  1141. {
  1142. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  1143. struct orect dmg;
  1144. int order[WM_MAX_WINDOWS];
  1145. unsigned int n, i, j, ncur, nnext;
  1146. int overflow = 0;
  1147. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  1148. if (w == 0 || h == 0)
  1149. return;
  1150. if (x0 < 0) x0 = 0;
  1151. if (y0 < 0) y0 = 0;
  1152. if (x1 > (int)fbw) x1 = (int)fbw;
  1153. if (y1 > (int)fbh) y1 = (int)fbh;
  1154. if (x1 <= x0 || y1 <= y0)
  1155. return;
  1156. dmg.x = x0;
  1157. dmg.y = y0;
  1158. dmg.w = (unsigned int)(x1 - x0);
  1159. dmg.h = (unsigned int)(y1 - y0);
  1160. wall_draw(dmg.x, dmg.y, dmg.w, dmg.h);
  1161. n = build_order(order);
  1162. for (i = 0; i < n; i++) {
  1163. struct uwin *u = &wins[order[i]];
  1164. int wx0 = u->info.x, wy0 = u->info.y;
  1165. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  1166. if (wx0 < dmg.x) wx0 = dmg.x;
  1167. if (wy0 < dmg.y) wy0 = dmg.y;
  1168. if (wx1 > x1) wx1 = x1;
  1169. if (wy1 > y1) wy1 = y1;
  1170. if (wx1 <= wx0 || wy1 <= wy0)
  1171. continue;
  1172. cur[0].x = wx0;
  1173. cur[0].y = wy0;
  1174. cur[0].w = (unsigned int)(wx1 - wx0);
  1175. cur[0].h = (unsigned int)(wy1 - wy0);
  1176. ncur = 1;
  1177. for (j = 0; j < i && ncur; j++) {
  1178. struct uwin *f = &wins[order[j]];
  1179. struct orect fr;
  1180. unsigned int k;
  1181. fr.x = f->info.x;
  1182. fr.y = f->info.y;
  1183. fr.w = f->info.w;
  1184. fr.h = f->info.h;
  1185. nnext = 0;
  1186. for (k = 0; k < ncur; k++) {
  1187. if (nnext + 4 > OCC_MAX) {
  1188. overflow = 1;
  1189. break;
  1190. }
  1191. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  1192. }
  1193. if (overflow)
  1194. break;
  1195. for (k = 0; k < nnext; k++)
  1196. cur[k] = nxt[k];
  1197. ncur = nnext;
  1198. }
  1199. if (overflow)
  1200. break;
  1201. for (j = 0; j < ncur; j++)
  1202. draw_window_clip((unsigned int)order[i], cur[j].x,
  1203. cur[j].y, cur[j].w, cur[j].h);
  1204. }
  1205. if (overflow)
  1206. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  1207. }
  1208. /* Invisible resize border: edge bits for a point in the band around the
  1209. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  1210. * The corner squares reach WM_CORNER_IN px along both edges. Popovers have
  1211. * no chrome and never resize. */
  1212. #define WM_EDGE_LEFT 1
  1213. #define WM_EDGE_RIGHT 2
  1214. #define WM_EDGE_TOP 4
  1215. #define WM_EDGE_BOTTOM 8
  1216. static int resize_edges_at(const struct uwin *u, int mx, int my)
  1217. {
  1218. const struct wm_window_info *w = &u->info;
  1219. int l = w->x, t = w->y;
  1220. int r = l + (int)w->w, b = t + (int)w->h;
  1221. int near_x, near_y, e = 0;
  1222. if (w->popover)
  1223. return 0;
  1224. /* Corners win over the thin edge bands so the diagonal grab is easy. */
  1225. if (mx >= l - WM_RESIZE_OUT && mx < l + WM_CORNER_IN &&
  1226. my >= t - WM_RESIZE_OUT && my < t + WM_CORNER_IN)
  1227. return WM_EDGE_LEFT | WM_EDGE_TOP;
  1228. if (mx >= r - WM_CORNER_IN && mx < r + WM_RESIZE_OUT &&
  1229. my >= t - WM_RESIZE_OUT && my < t + WM_CORNER_IN)
  1230. return WM_EDGE_RIGHT | WM_EDGE_TOP;
  1231. if (mx >= l - WM_RESIZE_OUT && mx < l + WM_CORNER_IN &&
  1232. my >= b - WM_CORNER_IN && my < b + WM_RESIZE_OUT)
  1233. return WM_EDGE_LEFT | WM_EDGE_BOTTOM;
  1234. if (mx >= r - WM_CORNER_IN && mx < r + WM_RESIZE_OUT &&
  1235. my >= b - WM_CORNER_IN && my < b + WM_RESIZE_OUT)
  1236. return WM_EDGE_RIGHT | WM_EDGE_BOTTOM;
  1237. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  1238. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  1239. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  1240. e |= WM_EDGE_LEFT;
  1241. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  1242. e |= WM_EDGE_RIGHT;
  1243. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  1244. e |= WM_EDGE_TOP;
  1245. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  1246. e |= WM_EDGE_BOTTOM;
  1247. return e;
  1248. }
  1249. /* Topmost window at (mx,my): its resize band when the point is inside one,
  1250. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  1251. * Bands are not considered inside the top bar, which the WM owns. */
  1252. static int resize_hit(int mx, int my, int *edges)
  1253. {
  1254. int order[WM_MAX_WINDOWS];
  1255. unsigned int n = build_order(order), i;
  1256. *edges = 0;
  1257. for (i = 0; i < n; i++) {
  1258. struct uwin *u = &wins[order[i]];
  1259. int e = 0;
  1260. if (!u->info.used)
  1261. continue;
  1262. if (my >= WM_BAR_H)
  1263. e = resize_edges_at(u, mx, my);
  1264. if (e) {
  1265. *edges = e;
  1266. return order[i];
  1267. }
  1268. if (mx >= u->info.x && my >= u->info.y &&
  1269. mx < u->info.x + (int)u->info.w &&
  1270. my < u->info.y + (int)u->info.h)
  1271. return order[i];
  1272. }
  1273. return -1;
  1274. }
  1275. static int resize_cursor(int edges)
  1276. {
  1277. switch (edges) {
  1278. case WM_EDGE_LEFT:
  1279. case WM_EDGE_RIGHT:
  1280. return CURSOR_EW_RESIZE;
  1281. case WM_EDGE_TOP:
  1282. case WM_EDGE_BOTTOM:
  1283. return CURSOR_NS_RESIZE;
  1284. case WM_EDGE_LEFT | WM_EDGE_TOP:
  1285. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  1286. return CURSOR_NWSE_RESIZE;
  1287. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  1288. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  1289. return CURSOR_NESW_RESIZE;
  1290. default:
  1291. return CURSOR_DEFAULT;
  1292. }
  1293. }
  1294. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  1295. static int close_hit(const struct wm_window_info *w, int mx, int my)
  1296. {
  1297. int bx;
  1298. if (w->popover)
  1299. return 0;
  1300. bx = w->x + (int)w->w - WM_BORDER_W - WM_CLOSE_W - WM_CLOSE_INSET;
  1301. return mx >= bx && mx < bx + WM_CLOSE_W &&
  1302. my >= w->y && my < w->y + WM_TITLE_H;
  1303. }
  1304. /* Window whose close icon is under the pointer, or -1. The topmost window
  1305. * decides, like a press does, so an icon covered by a popover or another
  1306. * window does not react. */
  1307. static int close_window_at(int mx, int my)
  1308. {
  1309. int edges = 0;
  1310. int idx = resize_hit(mx, my, &edges);
  1311. if (idx < 0 || !close_hit(&wins[idx].info, mx, my))
  1312. return -1;
  1313. return idx;
  1314. }
  1315. static void clamp_geom(struct uwin *u, int *x, int *y)
  1316. {
  1317. /* Match wm_clamp_window(): allow the window to hang off the left,
  1318. * right and bottom edges but keep the title bar reachable and below
  1319. * the system bar. */
  1320. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  1321. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  1322. int miny = WM_BAR_H;
  1323. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  1324. if (minx > maxx)
  1325. minx = maxx = 0;
  1326. if (miny > maxy)
  1327. miny = maxy = WM_BAR_H;
  1328. if (*x < minx)
  1329. *x = minx;
  1330. else if (*x > maxx)
  1331. *x = maxx;
  1332. if (*y < miny)
  1333. *y = miny;
  1334. else if (*y > maxy)
  1335. *y = maxy;
  1336. }
  1337. static void add_rect(struct wm_present_info *p, int x, int y,
  1338. unsigned int w, unsigned int h)
  1339. {
  1340. if (w == 0 || h == 0)
  1341. return;
  1342. if (p->count < 4) {
  1343. p->rect[p->count].x = x;
  1344. p->rect[p->count].y = y;
  1345. p->rect[p->count].w = w;
  1346. p->rect[p->count].h = h;
  1347. p->count++;
  1348. } else {
  1349. /* Four is enough for normal updates; fold the rest into the
  1350. * last rect rather than dropping pixels. */
  1351. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  1352. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  1353. int nx1 = x + (int)w, ny1 = y + (int)h;
  1354. if (x < x0) x0 = x;
  1355. if (y < y0) y0 = y;
  1356. if (nx1 > x1) x1 = nx1;
  1357. if (ny1 > y1) y1 = ny1;
  1358. p->rect[3].x = x0;
  1359. p->rect[3].y = y0;
  1360. p->rect[3].w = (unsigned int)(x1 - x0);
  1361. p->rect[3].h = (unsigned int)(y1 - y0);
  1362. }
  1363. }
  1364. /* Damage rectangle covering one window's close button, for a hover change. */
  1365. static void close_add_damage(struct wm_present_info *p, int idx)
  1366. {
  1367. struct wm_window_info *w;
  1368. if (idx < 0 || idx >= WM_MAX_WINDOWS)
  1369. return;
  1370. w = &wins[idx].info;
  1371. if (!w->used || w->popover)
  1372. return;
  1373. add_rect(p, w->x + (int)w->w - WM_BORDER_W - WM_CLOSE_W -
  1374. WM_CLOSE_INSET, w->y, WM_CLOSE_W, WM_TITLE_H);
  1375. }
  1376. /* Center the brightness bubble in the work area, clamped on small screens. */
  1377. static void osd_geom(void)
  1378. {
  1379. int x, y;
  1380. osd_w = OSD_W;
  1381. osd_h = OSD_H;
  1382. x = ((int)fbw - (int)osd_w) / 2;
  1383. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  1384. if (x < 0)
  1385. x = 0;
  1386. if (x + (int)osd_w > (int)fbw)
  1387. x = (int)fbw - (int)osd_w;
  1388. if (y < WM_BAR_H)
  1389. y = WM_BAR_H;
  1390. if (y + (int)osd_h > (int)fbh)
  1391. y = (int)fbh - (int)osd_h;
  1392. osd_x = x;
  1393. osd_y = y;
  1394. }
  1395. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  1396. static void osd_sun(int cx, int cy)
  1397. {
  1398. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  1399. int i;
  1400. for (i = -5; i <= 5; i++)
  1401. usurf_fill(&screen, cx - half[i + 5], cy + i,
  1402. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  1403. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  1404. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  1405. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  1406. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  1407. }
  1408. static void osd_draw(void)
  1409. {
  1410. char pct[8];
  1411. unsigned int n = 0;
  1412. unsigned int bar_x, bar_y, bar_w;
  1413. int x, y, tw;
  1414. osd_geom();
  1415. x = osd_x;
  1416. y = osd_y;
  1417. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  1418. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  1419. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  1420. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  1421. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  1422. 0x70709Au);
  1423. osd_sun(x + 22, y + (int)osd_h / 2);
  1424. if (osd_level >= 100) {
  1425. pct[n++] = '1';
  1426. pct[n++] = '0';
  1427. pct[n++] = '0';
  1428. } else {
  1429. if (osd_level >= 10)
  1430. pct[n++] = (char)('0' + osd_level / 10);
  1431. pct[n++] = (char)('0' + osd_level % 10);
  1432. }
  1433. pct[n++] = '%';
  1434. pct[n] = '\0';
  1435. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  1436. x, y, osd_w, osd_h);
  1437. tw = (int)usurf_text_width(&screen, pct);
  1438. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  1439. 0xFFD250u, x, y, osd_w, osd_h);
  1440. bar_x = (unsigned int)(x + 42);
  1441. bar_y = (unsigned int)(y + 35);
  1442. bar_w = osd_w - 42 - 14;
  1443. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  1444. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  1445. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  1446. 0xFFD250u);
  1447. }
  1448. /* Put the brightness OSD and the start menu back on top after a render pass
  1449. * that may have overwritten them. */
  1450. static void wm_overlays(struct wm_present_info *p)
  1451. {
  1452. /* The dock sits above the windows, so a window move/resize or a
  1453. * drag-shadow copy that crossed it must put it back. */
  1454. dock_damage(p);
  1455. dock_draw();
  1456. dock_mark_drawn();
  1457. if (osd_show) {
  1458. osd_draw();
  1459. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  1460. }
  1461. if (sm_open) {
  1462. sm_draw();
  1463. sm_add_present(p);
  1464. }
  1465. if (sw_active) {
  1466. sw_draw();
  1467. sw_add_present(p);
  1468. }
  1469. if (run_open) {
  1470. run_draw();
  1471. run_add_present(p);
  1472. }
  1473. }
  1474. /* True when a screen rectangle contains pixels from an overlay rather than
  1475. * the desktop/window stack. The dock is always on top, and the brightness
  1476. * OSD is drawn above the windows while it is visible; copying such a block
  1477. * as if it were the window would drag the overlay along with it. */
  1478. static int shadow_overlay_covers(int x, int y, int w, int h)
  1479. {
  1480. if (dock.visual.w && dock.visual.h &&
  1481. x < dock.visual.x + (int)dock.visual.w &&
  1482. x + w > dock.visual.x &&
  1483. y < dock.visual.y + (int)dock.visual.h &&
  1484. y + h > dock.visual.y)
  1485. return 1;
  1486. if (osd_visible &&
  1487. x < osd_x + (int)osd_w && x + w > osd_x &&
  1488. y < osd_y + (int)osd_h && y + h > osd_y)
  1489. return 1;
  1490. if (sw_visible &&
  1491. x < sw_x + (int)sw_w && x + w > sw_x &&
  1492. y < sw_y + (int)sw_h && y + h > sw_y)
  1493. return 1;
  1494. if (run_visible &&
  1495. x < run_x + (int)run_w && x + w > run_x &&
  1496. y < run_y + (int)run_h && y + h > run_y)
  1497. return 1;
  1498. return 0;
  1499. }
  1500. /* A focused window is topmost, so the shadow contains exactly its pixels in
  1501. * the old rectangle, unless the dock or the brightness OSD covers part of
  1502. * it. Move that block and repaint only the old part the new rectangle does
  1503. * not cover. When either rectangle is partly off-screen, or an overlay hid
  1504. * part of the source, the shadow has no valid pixels there, so repaint the
  1505. * new rect instead of copying overlay pixels into the window. */
  1506. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  1507. {
  1508. struct wm_present_info present;
  1509. struct orect oldr, newr, pieces[4];
  1510. unsigned int np, k;
  1511. int w = (int)u->info.w, h = (int)u->info.h;
  1512. int old_on = ox >= 0 && oy >= 0 &&
  1513. ox + w <= (int)fbw && oy + h <= (int)fbh &&
  1514. !shadow_overlay_covers(ox, oy, w, h);
  1515. int new_on = nx >= 0 && ny >= 0 &&
  1516. nx + w <= (int)fbw && ny + h <= (int)fbh;
  1517. if (u_wm_begin() != 0)
  1518. return;
  1519. remap_window_contents();
  1520. if (old_on && new_on)
  1521. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  1522. else
  1523. draw_rect(nx, ny, u->info.w, u->info.h);
  1524. oldr.x = ox;
  1525. oldr.y = oy;
  1526. oldr.w = u->info.w;
  1527. oldr.h = u->info.h;
  1528. newr.x = nx;
  1529. newr.y = ny;
  1530. newr.w = u->info.w;
  1531. newr.h = u->info.h;
  1532. np = rect_sub(pieces, &oldr, &newr);
  1533. for (k = 0; k < np; k++)
  1534. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1535. present.count = 0;
  1536. present.immediate = 1;
  1537. /* Present the union of the old and new rectangles as one rect.
  1538. * The shadow is correct for all of it, and one rect cannot be cut
  1539. * off by the kernel's four-entry present list. */
  1540. {
  1541. int ux0 = ox < nx ? ox : nx;
  1542. int uy0 = oy < ny ? oy : ny;
  1543. int ux1 = (ox > nx ? ox : nx) + (int)u->info.w;
  1544. int uy1 = (oy > ny ? oy : ny) + (int)u->info.h;
  1545. add_rect(&present, ux0, uy0, (unsigned int)(ux1 - ux0),
  1546. (unsigned int)(uy1 - uy0));
  1547. }
  1548. wm_overlays(&present);
  1549. u_wm_present(&present);
  1550. }
  1551. static int iabs(int v)
  1552. {
  1553. return v < 0 ? -v : v;
  1554. }
  1555. /* Repaint the old and new rectangles of a geometry change. */
  1556. static void redraw_geom(struct uwin *u, int ox, int oy,
  1557. unsigned int ow, unsigned int oh)
  1558. {
  1559. struct wm_present_info present;
  1560. unsigned int i;
  1561. present.count = 0;
  1562. present.immediate = 1;
  1563. add_rect(&present, ox, oy, ow, oh);
  1564. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1565. if (u_wm_begin() == 0) {
  1566. remap_window_contents();
  1567. for (i = 0; i < present.count; i++)
  1568. draw_rect(present.rect[i].x, present.rect[i].y,
  1569. present.rect[i].w, present.rect[i].h);
  1570. wm_overlays(&present);
  1571. u_wm_present(&present);
  1572. }
  1573. }
  1574. /* Resize (when needed), move and remap a window in one step. */
  1575. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1576. int nx, int ny, unsigned int nw, unsigned int nh)
  1577. {
  1578. struct wm_window_info ni;
  1579. int ox = u->info.x, oy = u->info.y;
  1580. unsigned int ow = u->info.w, oh = u->info.h;
  1581. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1582. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1583. nh - WM_TITLE_H - 2 * WM_BORDER_W,
  1584. nx - ox, ny - oy) != 0)
  1585. return -1;
  1586. }
  1587. if (nx != ox || ny != oy)
  1588. u_wm_set_geom(idx, nx, ny);
  1589. u->mapped = 0;
  1590. if (u_wm_info(idx, &ni) == 1)
  1591. u->info = ni;
  1592. if (u_wm_map(idx, &u->map) == 0)
  1593. u->mapped = 1;
  1594. redraw_geom(u, ox, oy, ow, oh);
  1595. return 0;
  1596. }
  1597. static int restore_window(struct uwin *u, unsigned int idx)
  1598. {
  1599. if (u->state == WIN_NORMAL)
  1600. return 0;
  1601. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1602. u->restore_w, u->restore_h) != 0)
  1603. return -1;
  1604. u->state = WIN_NORMAL;
  1605. return 0;
  1606. }
  1607. /* Maximize or snap a window to a half of the work area below the system
  1608. * bar. The restore geometry is recorded only when the window leaves the
  1609. * normal state, so maximize -> snap -> restore returns to the original. */
  1610. static int place_window(struct uwin *u, unsigned int idx, int state)
  1611. {
  1612. unsigned int half = fbw / 2;
  1613. unsigned int h = fbh - WM_BAR_H;
  1614. int x = 0;
  1615. unsigned int w = fbw;
  1616. if (state == u->state)
  1617. return 0;
  1618. if (state == WIN_SNAP_LEFT) {
  1619. w = half;
  1620. } else if (state == WIN_SNAP_RIGHT) {
  1621. x = (int)half;
  1622. w = fbw - half;
  1623. }
  1624. if (u->state == WIN_NORMAL) {
  1625. u->restore_x = u->info.x;
  1626. u->restore_y = u->info.y;
  1627. u->restore_w = u->info.w;
  1628. u->restore_h = u->info.h;
  1629. }
  1630. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1631. return -1;
  1632. u->state = state;
  1633. return 0;
  1634. }
  1635. /* Double-click title bar: fill the work area below the system bar, or go back
  1636. * to the geometry the window had before it was maximized. A snapped window
  1637. * maximizes (keeping its pre-snap restore geometry). */
  1638. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1639. {
  1640. if (u->state == WIN_MAXIMIZED)
  1641. return restore_window(u, idx);
  1642. return place_window(u, idx, WIN_MAXIMIZED);
  1643. }
  1644. /* One axis of edge resistance. lo/len is the window's span on that axis
  1645. * before the move, *pos the proposed start. dir < 0: the window's start side
  1646. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1647. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1648. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1649. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1650. {
  1651. int over;
  1652. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1653. over = e - *pos;
  1654. if (over <= WM_RESIST && over < *best) {
  1655. *best = over;
  1656. *pos = e;
  1657. }
  1658. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1659. over = *pos + len - e;
  1660. if (over <= WM_RESIST && over < *best) {
  1661. *best = over;
  1662. *pos = e - len;
  1663. }
  1664. }
  1665. }
  1666. /* Span overlap test: edges of another window only resist when the two
  1667. * windows share some extent on the other axis (they would actually touch). */
  1668. static int spans_overlap(int a, int alen, int b, int blen)
  1669. {
  1670. return a < b + blen && b < a + alen;
  1671. }
  1672. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1673. * screen edges and the facing edges of every other window. The proposed
  1674. * position is still pure pointer arithmetic, so once the overshoot passes
  1675. * WM_RESIST the window jumps to follow the pointer again. */
  1676. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1677. {
  1678. int ox = u->info.x, oy = u->info.y;
  1679. int w = (int)u->info.w, h = (int)u->info.h;
  1680. int px = *nx, py = *ny;
  1681. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1682. int i;
  1683. resist_edge(ox, w, &px, 0, -1, &bestx);
  1684. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1685. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1686. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1687. struct wm_window_info *o = &wins[i].info;
  1688. int t;
  1689. if (i == idx || !o->used)
  1690. continue;
  1691. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1692. /* our left side against its right, our right
  1693. * side against its left */
  1694. t = *nx;
  1695. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1696. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1697. if (t != *nx)
  1698. px = t;
  1699. }
  1700. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1701. t = *ny;
  1702. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1703. resist_edge(oy, h, &t, o->y, 1, &besty);
  1704. if (t != *ny)
  1705. py = t;
  1706. }
  1707. }
  1708. *nx = px;
  1709. *ny = py;
  1710. }
  1711. /* The top-bar date/time text is also a button: it opens the Date & Time
  1712. * editor. The rectangle is recorded by draw_bar(). */
  1713. /* The whole right status cluster is one button: the clock, temperature,
  1714. * network and power items plus the gaps between them. Only the rectangle
  1715. * draw_bar() recorded is clickable, so the empty bar between the start
  1716. * button and the cluster does nothing. */
  1717. static int bar_status_hit(unsigned int mx, unsigned int my)
  1718. {
  1719. return bar_status_w && bar_status_x >= 0 &&
  1720. (int)my >= BAR_BTN_Y && (int)my < BAR_BTN_Y + BAR_BTN_H &&
  1721. (int)mx >= bar_status_x &&
  1722. (int)mx < bar_status_x + (int)bar_status_w;
  1723. }
  1724. static void sm_layout_get(struct sm_layout *l)
  1725. {
  1726. int avail = (int)fbh - WM_BAR_H - 8;
  1727. int row = SM_ROW_H;
  1728. int total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1729. SM_BOTTOM;
  1730. /* Shrink the rows to the work area, so a 540-600 px panel still fits
  1731. * the whole list. The low bound is the smallest legible row. */
  1732. while (row > SM_MIN_ROW && total > avail) {
  1733. row--;
  1734. total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1735. SM_BOTTOM;
  1736. }
  1737. l->x = 4;
  1738. l->y = WM_BAR_H + 2;
  1739. l->w = SM_W;
  1740. l->h = total;
  1741. l->row_h = row;
  1742. l->sect_y = l->y + SM_TOP;
  1743. l->row_x = l->x + SM_PAD;
  1744. l->row_w = l->w - 2 * SM_PAD;
  1745. l->list_y = l->sect_y + SM_SECT_H;
  1746. l->power_y = l->list_y + SM_NAPP * row + SM_GAP;
  1747. l->sub_w = SM_SUB_W;
  1748. l->sub_h = 2 * row + 8;
  1749. l->sub_x = l->x + l->w + 4;
  1750. if (l->sub_x + l->sub_w > (int)fbw - 2) {
  1751. if (l->x - l->sub_w - 4 >= 0)
  1752. l->sub_x = l->x - l->sub_w - 4;
  1753. else
  1754. l->sub_x = (int)fbw - 2 - l->sub_w;
  1755. }
  1756. /* The power row is the last menu row: open the submenu upwards and
  1757. * align its bottom with that row. */
  1758. l->sub_y = l->power_y + row - l->sub_h;
  1759. if (l->sub_y < WM_BAR_H + 2)
  1760. l->sub_y = WM_BAR_H + 2;
  1761. if (l->sub_y + l->sub_h > (int)fbh - 2)
  1762. l->sub_y = (int)fbh - 2 - l->sub_h;
  1763. if (l->sub_y < 0)
  1764. l->sub_y = 0;
  1765. }
  1766. static void sm_tile(int x, int y, int size, unsigned int rgb, char letter)
  1767. {
  1768. char s[2];
  1769. int tw, tx, ty;
  1770. if (size < 12)
  1771. size = 12;
  1772. s[0] = letter;
  1773. s[1] = '\0';
  1774. sm_fill_round(x, y, size, size, 4, rgb);
  1775. tw = (int)usurf_text_width(&screen, s);
  1776. tx = x + (size - tw) / 2;
  1777. ty = y + (size - 16) / 2;
  1778. usurf_text(&screen, tx, ty, s, COL_WHITE,
  1779. x, y, (unsigned int)size, (unsigned int)size);
  1780. }
  1781. /* ---- Alt+Tab switcher -------------------------------------------------
  1782. * The kernel consumes the chord and reports it plus the Alt held state in
  1783. * wm_status; the WM keeps the MRU order and the selection, draws a
  1784. * horizontal icon strip with the selected app's name below it, and focuses
  1785. * the selection when Alt is released. The overlay itself is display
  1786. * only. */
  1787. static unsigned int sw_cell;
  1788. static unsigned int sw_icon;
  1789. static int sw_slot_usable(unsigned int idx)
  1790. {
  1791. return idx < WM_MAX_WINDOWS && wins[idx].info.used &&
  1792. !wins[idx].info.popover;
  1793. }
  1794. static void sw_mru_remove(unsigned int idx)
  1795. {
  1796. unsigned int i, j;
  1797. for (i = 0; i < sw_mru_count; i++) {
  1798. if (sw_mru[i] != idx)
  1799. continue;
  1800. for (j = i; j + 1 < sw_mru_count; j++)
  1801. sw_mru[j] = sw_mru[j + 1];
  1802. sw_mru_count--;
  1803. return;
  1804. }
  1805. }
  1806. static void sw_mru_touch(unsigned int idx)
  1807. {
  1808. unsigned int i;
  1809. sw_mru_remove(idx);
  1810. for (i = sw_mru_count < WM_MAX_WINDOWS ? sw_mru_count :
  1811. WM_MAX_WINDOWS - 1; i > 0; i--)
  1812. sw_mru[i] = sw_mru[i - 1];
  1813. sw_mru[0] = idx;
  1814. if (sw_mru_count < WM_MAX_WINDOWS)
  1815. sw_mru_count++;
  1816. }
  1817. /* Track focus transitions from the window info the WM refreshes every
  1818. * frame. The focused normal window goes to the front of the MRU; closed
  1819. * entries are dropped. */
  1820. static void sw_track_focus(void)
  1821. {
  1822. int f = -1;
  1823. unsigned int i;
  1824. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1825. if (wins[i].info.used && wins[i].info.focused &&
  1826. !wins[i].info.popover) {
  1827. f = (int)i;
  1828. break;
  1829. }
  1830. if (f != sw_last_focus) {
  1831. sw_last_focus = f;
  1832. if (f >= 0)
  1833. sw_mru_touch((unsigned int)f);
  1834. }
  1835. for (i = 0; i < sw_mru_count; ) {
  1836. if (sw_slot_usable(sw_mru[i]))
  1837. i++;
  1838. else
  1839. sw_mru_remove(sw_mru[i]);
  1840. }
  1841. }
  1842. /* Build the switcher list in MRU order, then append any normal window the
  1843. * MRU does not know yet. */
  1844. static void sw_build(void)
  1845. {
  1846. unsigned int i, j, n = 0;
  1847. for (i = 0; i < sw_mru_count && n < WM_MAX_WINDOWS; i++)
  1848. if (sw_slot_usable(sw_mru[i]))
  1849. sw_slots[n++] = sw_mru[i];
  1850. for (i = 0; i < WM_MAX_WINDOWS && n < WM_MAX_WINDOWS; i++) {
  1851. int seen = 0;
  1852. if (!sw_slot_usable(i))
  1853. continue;
  1854. for (j = 0; j < n; j++)
  1855. if (sw_slots[j] == i) {
  1856. seen = 1;
  1857. break;
  1858. }
  1859. if (!seen)
  1860. sw_slots[n++] = i;
  1861. }
  1862. sw_count = n;
  1863. }
  1864. /* One chord step. The first step opens the session with the focused window
  1865. * as entry 0, so the selection moves to the previously used window. */
  1866. static void sw_step(int dir)
  1867. {
  1868. if (run_open)
  1869. return;
  1870. if (!sw_active) {
  1871. sw_build();
  1872. if (sw_count < 2)
  1873. return;
  1874. sw_active = 1;
  1875. sw_sel = 0;
  1876. sm_close();
  1877. full_redraw = 1;
  1878. } else {
  1879. sw_build();
  1880. if (sw_count < 2) {
  1881. sw_active = 0;
  1882. sw_count = 0;
  1883. full_redraw = 1;
  1884. return;
  1885. }
  1886. }
  1887. if (dir > 0)
  1888. sw_sel = (sw_sel + 1) % sw_count;
  1889. else
  1890. sw_sel = (sw_sel + sw_count - 1) % sw_count;
  1891. }
  1892. /* Alt released: focus the selection. A missing/closed entry is ignored. */
  1893. static void sw_commit(void)
  1894. {
  1895. unsigned int idx;
  1896. if (!sw_active)
  1897. return;
  1898. sw_active = 0;
  1899. if (sw_sel < sw_count) {
  1900. idx = sw_slots[sw_sel];
  1901. if (sw_slot_usable(idx))
  1902. u_wm_focus(idx);
  1903. }
  1904. sw_count = 0;
  1905. sw_sel = 0;
  1906. full_redraw = 1;
  1907. }
  1908. /* ---- Win/Super app runner -------------------------------------------- */
  1909. static char run_lower(char c)
  1910. {
  1911. return c >= 'A' && c <= 'Z' ? (char)(c - 'A' + 'a') : c;
  1912. }
  1913. /* Case-insensitive prefix/substring tests for the app-name and command
  1914. * matches. The query length is known, so these avoid needing a lowered
  1915. * copy of every string. */
  1916. static int run_prefix(const char *s, const char *q, unsigned int n)
  1917. {
  1918. unsigned int i;
  1919. for (i = 0; i < n; i++) {
  1920. if (!s[i] || run_lower(s[i]) != run_lower(q[i]))
  1921. return 0;
  1922. }
  1923. return 1;
  1924. }
  1925. static int run_contains(const char *s, const char *q, unsigned int n)
  1926. {
  1927. unsigned int i;
  1928. if (!n)
  1929. return 1;
  1930. for (; *s; s++) {
  1931. for (i = 0; i < n; i++)
  1932. if (!s[i] || run_lower(s[i]) != run_lower(q[i]))
  1933. break;
  1934. if (i == n)
  1935. return 1;
  1936. }
  1937. return 0;
  1938. }
  1939. static const char *run_basename(const char *path)
  1940. {
  1941. const char *base = path;
  1942. while (*path) {
  1943. if (*path == '/')
  1944. base = path + 1;
  1945. path++;
  1946. }
  1947. return base;
  1948. }
  1949. /* Lower is a better match: app label, command name, window title, then the
  1950. * same three as substrings. */
  1951. static int run_score(const struct sm_app *a, const char *q, unsigned int n)
  1952. {
  1953. const char *cmd = run_basename(a->path);
  1954. if (!n)
  1955. return 0;
  1956. if (run_prefix(a->label, q, n))
  1957. return 0;
  1958. if (a->path[0] && run_prefix(cmd, q, n))
  1959. return 1;
  1960. if (a->win_title && run_prefix(a->win_title, q, n))
  1961. return 2;
  1962. if (run_contains(a->label, q, n))
  1963. return 3;
  1964. if (a->path[0] && run_contains(cmd, q, n))
  1965. return 4;
  1966. if (a->win_title && run_contains(a->win_title, q, n))
  1967. return 5;
  1968. return -1;
  1969. }
  1970. /* Rank the apps into run_matches. Insertion sort keeps equal scores in
  1971. * start-menu order, and the list is small enough to rebuild per keystroke. */
  1972. static void run_recompute(void)
  1973. {
  1974. int i, n = 0;
  1975. for (i = 0; i < SM_NAPP && i < RUN_MAX_APPS; i++) {
  1976. int score = run_score(&sm_apps[i], run_query, run_len);
  1977. int pos;
  1978. if (score < 0)
  1979. continue;
  1980. pos = n;
  1981. while (pos > 0 && score < run_scores[pos - 1]) {
  1982. run_matches[pos] = run_matches[pos - 1];
  1983. run_scores[pos] = run_scores[pos - 1];
  1984. pos--;
  1985. }
  1986. run_matches[pos] = i;
  1987. run_scores[pos] = score;
  1988. n++;
  1989. }
  1990. run_nmatch = n;
  1991. run_sel = 0;
  1992. run_top = 0;
  1993. }
  1994. static int run_rows_max(void)
  1995. {
  1996. int avail = (int)fbh - WM_BAR_H - 40;
  1997. int rows = (avail - 2 * RUN_PAD - RUN_FIELD_H - 8 - RUN_HINT_H) /
  1998. RUN_ROW_H;
  1999. if (rows > RUN_ROWS_MAX)
  2000. rows = RUN_ROWS_MAX;
  2001. if (rows < 1)
  2002. rows = 1;
  2003. return rows;
  2004. }
  2005. static void run_geom(void)
  2006. {
  2007. int rows = run_nmatch < run_rows_max() ? run_nmatch : run_rows_max();
  2008. if (rows < 1)
  2009. rows = 1;
  2010. run_w = RUN_W;
  2011. if ((int)run_w > (int)fbw - 48)
  2012. run_w = fbw - 48;
  2013. run_h = 2 * RUN_PAD + RUN_FIELD_H + 8 +
  2014. (unsigned int)rows * RUN_ROW_H + RUN_HINT_H;
  2015. run_x = ((int)fbw - (int)run_w) / 2;
  2016. run_y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)run_h) / 2;
  2017. if (run_x < 8)
  2018. run_x = 8;
  2019. if (run_x + (int)run_w > (int)fbw - 8)
  2020. run_x = (int)fbw - 8 - (int)run_w;
  2021. if (run_y < WM_BAR_H + 8)
  2022. run_y = WM_BAR_H + 8;
  2023. if (run_y + (int)run_h > (int)fbh - 8)
  2024. run_y = (int)fbh - 8 - (int)run_h;
  2025. }
  2026. static void run_sel_visible(void)
  2027. {
  2028. int rows = run_rows_max();
  2029. int max_top = run_nmatch - rows;
  2030. if (run_sel < run_top)
  2031. run_top = run_sel;
  2032. if (run_sel >= run_top + rows)
  2033. run_top = run_sel - rows + 1;
  2034. if (run_top < 0)
  2035. run_top = 0;
  2036. if (max_top < 0)
  2037. max_top = 0;
  2038. if (run_top > max_top)
  2039. run_top = max_top;
  2040. }
  2041. static void run_move(int delta)
  2042. {
  2043. if (!run_nmatch)
  2044. return;
  2045. run_sel += delta;
  2046. if (run_sel < 0)
  2047. run_sel = 0;
  2048. if (run_sel >= run_nmatch)
  2049. run_sel = run_nmatch - 1;
  2050. run_sel_visible();
  2051. }
  2052. /* The selected label's suffix after the typed prefix, for the inline ghost
  2053. * completion. NULL when the query is empty or the match came from another
  2054. * field. */
  2055. static const char *run_completion(void)
  2056. {
  2057. const struct sm_app *a;
  2058. unsigned int i;
  2059. if (!run_nmatch || !run_len || run_sel < 0 || run_sel >= run_nmatch)
  2060. return (void *)0;
  2061. a = &sm_apps[run_matches[run_sel]];
  2062. for (i = 0; i < run_len; i++)
  2063. if (!a->label[i] ||
  2064. run_lower(a->label[i]) != run_lower(run_query[i]))
  2065. return (void *)0;
  2066. return a->label;
  2067. }
  2068. static void run_complete(void)
  2069. {
  2070. const char *completion = run_completion();
  2071. unsigned int i;
  2072. if (!completion)
  2073. return;
  2074. for (i = 0; completion[i] && i + 1 < RUN_QUERY_MAX; i++)
  2075. run_query[i] = completion[i];
  2076. run_query[i] = '\0';
  2077. run_len = run_caret = i;
  2078. run_recompute();
  2079. }
  2080. static void run_insert(char c)
  2081. {
  2082. unsigned int i;
  2083. if (run_len + 1 >= RUN_QUERY_MAX)
  2084. return;
  2085. for (i = run_len; i > run_caret; i--)
  2086. run_query[i] = run_query[i - 1];
  2087. run_query[run_caret] = c;
  2088. run_len++;
  2089. run_caret++;
  2090. run_query[run_len] = '\0';
  2091. run_recompute();
  2092. }
  2093. static void run_erase_before(void)
  2094. {
  2095. unsigned int i;
  2096. if (!run_caret)
  2097. return;
  2098. for (i = run_caret - 1; i + 1 <= run_len; i++)
  2099. run_query[i] = run_query[i + 1];
  2100. run_len--;
  2101. run_caret--;
  2102. run_query[run_len] = '\0';
  2103. run_recompute();
  2104. }
  2105. static void run_erase_at(void)
  2106. {
  2107. unsigned int i;
  2108. if (run_caret >= run_len)
  2109. return;
  2110. for (i = run_caret; i + 1 <= run_len; i++)
  2111. run_query[i] = run_query[i + 1];
  2112. run_len--;
  2113. run_query[run_len] = '\0';
  2114. run_recompute();
  2115. }
  2116. static void run_open_ui(void)
  2117. {
  2118. run_open = 1;
  2119. run_prev_valid = 0;
  2120. run_query[0] = '\0';
  2121. run_len = 0;
  2122. run_caret = 0;
  2123. run_sel = 0;
  2124. run_top = 0;
  2125. run_recompute();
  2126. sw_active = 0;
  2127. sw_count = 0;
  2128. sm_close();
  2129. full_redraw = 1;
  2130. }
  2131. static void run_close_ui(void)
  2132. {
  2133. if (!run_open)
  2134. return;
  2135. run_open = 0;
  2136. run_nmatch = 0;
  2137. run_len = 0;
  2138. run_caret = 0;
  2139. run_query[0] = '\0';
  2140. run_prev_valid = 0;
  2141. full_redraw = 1;
  2142. }
  2143. static void run_launch(void)
  2144. {
  2145. int idx;
  2146. if (run_sel < 0 || run_sel >= run_nmatch) {
  2147. run_close_ui();
  2148. return;
  2149. }
  2150. idx = run_matches[run_sel];
  2151. run_close_ui();
  2152. if (idx >= 0 && idx < SM_NAPP)
  2153. sm_launch(idx);
  2154. }
  2155. static void run_key(unsigned int key)
  2156. {
  2157. if (!run_open)
  2158. return;
  2159. if (key == 0x1B) {
  2160. run_close_ui();
  2161. return;
  2162. }
  2163. if (key == '\n' || key == '\r') {
  2164. run_launch();
  2165. return;
  2166. }
  2167. if (key == '\t') {
  2168. run_complete();
  2169. return;
  2170. }
  2171. if (key == WM_RUNNER_KEY_UP) {
  2172. run_move(-1);
  2173. return;
  2174. }
  2175. if (key == WM_RUNNER_KEY_DOWN) {
  2176. run_move(1);
  2177. return;
  2178. }
  2179. if (key == WM_RUNNER_KEY_PAGE_UP) {
  2180. run_move(-run_rows_max());
  2181. return;
  2182. }
  2183. if (key == WM_RUNNER_KEY_PAGE_DOWN) {
  2184. run_move(run_rows_max());
  2185. return;
  2186. }
  2187. if (key == WM_RUNNER_KEY_LEFT) {
  2188. if (run_caret)
  2189. run_caret--;
  2190. return;
  2191. }
  2192. if (key == WM_RUNNER_KEY_RIGHT) {
  2193. if (run_caret < run_len)
  2194. run_caret++;
  2195. return;
  2196. }
  2197. if (key == WM_RUNNER_KEY_HOME) {
  2198. run_caret = 0;
  2199. return;
  2200. }
  2201. if (key == WM_RUNNER_KEY_END) {
  2202. run_caret = run_len;
  2203. return;
  2204. }
  2205. if (key == 0x08) {
  2206. run_erase_before();
  2207. return;
  2208. }
  2209. if (key == WM_RUNNER_KEY_DELETE) {
  2210. run_erase_at();
  2211. return;
  2212. }
  2213. if (key >= 32 && key < 127) {
  2214. run_insert((char)key);
  2215. return;
  2216. }
  2217. }
  2218. static void run_draw(void)
  2219. {
  2220. const struct sm_app *a;
  2221. const char *completion;
  2222. char saved;
  2223. unsigned int i, qw;
  2224. int fx, fy, fw, ry, rows, shown, hint_y;
  2225. if (!run_open)
  2226. return;
  2227. run_geom();
  2228. usurf_fill_round_alpha(&screen, run_x, run_y, (int)run_w, (int)run_h,
  2229. 10, 0x141824u, 242);
  2230. fx = run_x + RUN_PAD;
  2231. fy = run_y + RUN_PAD;
  2232. fw = (int)run_w - 2 * RUN_PAD;
  2233. sm_fill_round(fx, fy, fw, RUN_FIELD_H, 7, 0x0B0D14u);
  2234. /* The query text, a dim placeholder, and the caret at the edit point.
  2235. * Temporarily terminating at the caret gives the measured prefix. */
  2236. if (run_len)
  2237. usurf_text(&screen, fx + 10, fy + (RUN_FIELD_H - 16) / 2,
  2238. run_query, SM_TEXT, fx, fy, (unsigned int)fw,
  2239. RUN_FIELD_H);
  2240. else
  2241. usurf_text(&screen, fx + 10, fy + (RUN_FIELD_H - 16) / 2,
  2242. "Search apps", SM_TEXT_DIM, fx, fy,
  2243. (unsigned int)fw, RUN_FIELD_H);
  2244. saved = run_query[run_caret];
  2245. run_query[run_caret] = '\0';
  2246. qw = usurf_text_width(&screen, run_query);
  2247. run_query[run_caret] = saved;
  2248. usurf_fill(&screen, fx + 10 + (int)qw, fy + 9, 1, 16, 0xE8EAF6u);
  2249. completion = run_completion();
  2250. if (completion && run_caret == run_len)
  2251. usurf_text(&screen, fx + 10 + (int)qw + 1,
  2252. fy + (RUN_FIELD_H - 16) / 2,
  2253. completion + run_len, SM_TEXT_DIM, fx, fy,
  2254. (unsigned int)fw, RUN_FIELD_H);
  2255. rows = run_rows_max();
  2256. ry = fy + RUN_FIELD_H + 8;
  2257. shown = run_nmatch - run_top;
  2258. if (shown > rows)
  2259. shown = rows;
  2260. if (!run_nmatch) {
  2261. usurf_text(&screen, fx + 10, ry + 5, "No matching apps",
  2262. SM_TEXT_DIM, fx, ry, (unsigned int)fw,
  2263. RUN_ROW_H);
  2264. shown = 0;
  2265. }
  2266. for (i = 0; i < (unsigned int)shown; i++) {
  2267. int mi = run_top + (int)i;
  2268. int row_y = ry + (int)i * RUN_ROW_H;
  2269. const char *cmd;
  2270. int tx = fx + 34;
  2271. int tw;
  2272. a = &sm_apps[run_matches[mi]];
  2273. if (mi == run_sel)
  2274. sm_fill_round(fx, row_y, fw, RUN_ROW_H - 2, 6,
  2275. 0x365CA6);
  2276. if (!usurf_app_icon(&screen, fx + 6,
  2277. row_y + (RUN_ROW_H - RUN_ICON) / 2,
  2278. RUN_ICON, (enum app_icon)a->icon))
  2279. sm_tile(fx + 6, row_y + (RUN_ROW_H - RUN_ICON) / 2,
  2280. RUN_ICON, a->tile, a->letter);
  2281. usurf_text(&screen, tx, row_y + (RUN_ROW_H - 16) / 2,
  2282. a->label, mi == run_sel ? COL_WHITE : SM_TEXT,
  2283. fx, row_y, (unsigned int)fw, RUN_ROW_H);
  2284. cmd = run_basename(a->path);
  2285. if (a->path[0] && cmd[0]) {
  2286. tw = (int)usurf_text_width(&screen, cmd);
  2287. if (tx + tw + 10 < fx + fw)
  2288. usurf_text(&screen, fx + fw - 10 - tw,
  2289. row_y + (RUN_ROW_H - 16) / 2,
  2290. cmd, SM_TEXT_DIM, fx, row_y,
  2291. (unsigned int)fw, RUN_ROW_H);
  2292. }
  2293. }
  2294. hint_y = run_y + (int)run_h - RUN_PAD - RUN_HINT_H + 2;
  2295. usurf_text(&screen, fx + 2, hint_y,
  2296. "Tab complete Up/Down select Enter run Esc close",
  2297. SM_TEXT_DIM, run_x, run_y, run_w, run_h);
  2298. run_prev_x = run_x;
  2299. run_prev_y = run_y;
  2300. run_prev_w = run_w;
  2301. run_prev_h = run_h;
  2302. run_prev_valid = 1;
  2303. }
  2304. static void run_add_present(struct wm_present_info *p)
  2305. {
  2306. if (!run_open)
  2307. return;
  2308. run_geom();
  2309. add_rect(p, run_x, run_y, run_w, run_h);
  2310. }
  2311. /* Consume the kernel's WM input ring: Alt+Tab chords, runner keys, and the
  2312. * runner grab transition. Returns a bitmask of overlays that need a
  2313. * repaint (WM_INPUT_SW / WM_INPUT_RUN). */
  2314. #define WM_INPUT_SW 1
  2315. #define WM_INPUT_RUN 2
  2316. static int wm_input_process(struct wm_status *st)
  2317. {
  2318. int changed = 0;
  2319. unsigned int n;
  2320. if (!sw_chord_seen_valid) {
  2321. sw_chord_seen = st->chord_head;
  2322. sw_chord_seen_valid = 1;
  2323. }
  2324. /* The Win key already toggled the kernel grab, so open the runner
  2325. * before applying keys typed in the same interval. */
  2326. if (st->runner_active && !run_open) {
  2327. run_open_ui();
  2328. changed |= WM_INPUT_RUN;
  2329. }
  2330. while (sw_chord_seen != st->chord_head) {
  2331. unsigned int event =
  2332. st->chord_keys[sw_chord_seen % WM_CHORD_QUEUE];
  2333. sw_chord_seen++;
  2334. if (event & WM_CHORD_KEY) {
  2335. if (run_open)
  2336. run_key(event & 0xFFFFu);
  2337. changed |= WM_INPUT_RUN;
  2338. } else if (event == WM_CHORD_NEXT) {
  2339. sw_step(1);
  2340. changed |= WM_INPUT_SW;
  2341. } else if (event == WM_CHORD_PREV) {
  2342. sw_step(-1);
  2343. changed |= WM_INPUT_SW;
  2344. }
  2345. }
  2346. /* A runner launch or Esc/Enter already ended the local session; the
  2347. * kernel has released the grab for Enter/Esc and for a second Win. */
  2348. if (!st->runner_active && run_open) {
  2349. run_close_ui();
  2350. changed |= WM_INPUT_RUN;
  2351. }
  2352. if (sw_active) {
  2353. if (!st->alt_down) {
  2354. sw_commit();
  2355. changed |= WM_INPUT_SW;
  2356. } else {
  2357. /* A window may have closed while the session is open. */
  2358. unsigned int keep = sw_sel < sw_count ?
  2359. sw_slots[sw_sel] : WM_MAX_WINDOWS;
  2360. unsigned int i, sel = 0;
  2361. n = 0;
  2362. for (i = 0; i < sw_count; i++)
  2363. if (sw_slot_usable(sw_slots[i])) {
  2364. if (sw_slots[i] == keep)
  2365. sel = n;
  2366. sw_slots[n++] = sw_slots[i];
  2367. }
  2368. if (n != sw_count) {
  2369. sw_count = n;
  2370. sw_sel = sel;
  2371. changed |= WM_INPUT_SW;
  2372. }
  2373. if (sw_count < 2) {
  2374. sw_active = 0;
  2375. sw_count = 0;
  2376. full_redraw = 1;
  2377. }
  2378. }
  2379. }
  2380. return changed;
  2381. }
  2382. static const struct sm_app *sw_app_for_title(const char *title)
  2383. {
  2384. int i;
  2385. for (i = 0; i < SM_NAPP; i++) {
  2386. const char *t = sm_apps[i].win_title;
  2387. const char *a = title;
  2388. if (!t)
  2389. continue;
  2390. while (*a && *a == *t) {
  2391. a++;
  2392. t++;
  2393. }
  2394. if (*a == *t)
  2395. return &sm_apps[i];
  2396. }
  2397. return (void *)0;
  2398. }
  2399. static void sw_geom(void)
  2400. {
  2401. unsigned int n = sw_count;
  2402. if (!n)
  2403. n = 1;
  2404. sw_cell = SW_CELL_W;
  2405. if ((int)(n * sw_cell + 2 * SW_PAD) > (int)fbw - 16 &&
  2406. (int)fbw > (int)(2 * SW_PAD + 16 + n * 34))
  2407. sw_cell = (fbw - 16 - 2 * SW_PAD) / n;
  2408. sw_icon = sw_cell > 20 ? sw_cell - 20 : 16;
  2409. if (sw_icon > SW_ICON_MAX)
  2410. sw_icon = SW_ICON_MAX;
  2411. sw_w = n * sw_cell + 2 * SW_PAD;
  2412. sw_h = SW_PAD + sw_icon + 10 + SW_LABEL_H + SW_PAD;
  2413. sw_x = ((int)fbw - (int)sw_w) / 2;
  2414. if (sw_x < 8)
  2415. sw_x = 8;
  2416. sw_y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)sw_h) / 2;
  2417. if (sw_y < WM_BAR_H + 8)
  2418. sw_y = WM_BAR_H + 8;
  2419. if (sw_y + (int)sw_h > (int)fbh - 8)
  2420. sw_y = (int)fbh - 8 - (int)sw_h;
  2421. }
  2422. static void sw_draw(void)
  2423. {
  2424. const char *label;
  2425. unsigned int i;
  2426. int ty, tx, tw;
  2427. if (!sw_active || !sw_count)
  2428. return;
  2429. sw_geom();
  2430. usurf_fill_round_alpha(&screen, sw_x, sw_y, (int)sw_w, (int)sw_h, 10,
  2431. 0x141824u, 242);
  2432. for (i = 0; i < sw_count; i++) {
  2433. const struct wm_window_info *w = &wins[sw_slots[i]].info;
  2434. const struct sm_app *a = sw_app_for_title(w->title);
  2435. int cell_x = sw_x + SW_PAD + (int)i * (int)sw_cell;
  2436. int ix = cell_x + ((int)sw_cell - (int)sw_icon) / 2;
  2437. int iy = sw_y + SW_PAD;
  2438. if (i == sw_sel)
  2439. usurf_fill_round_alpha(&screen, cell_x + 3, iy - 5,
  2440. (int)sw_cell - 6,
  2441. (int)sw_icon + 10, 8,
  2442. 0x3C69B4u, 230);
  2443. if (a) {
  2444. if (!usurf_app_icon(&screen, ix, iy, (int)sw_icon,
  2445. (enum app_icon)a->icon))
  2446. sm_tile(ix, iy, (int)sw_icon, a->tile,
  2447. a->letter);
  2448. } else {
  2449. char letter = '?';
  2450. if (w->title[0]) {
  2451. letter = w->title[0];
  2452. if (letter >= 'a' && letter <= 'z')
  2453. letter = (char)(letter - 'a' + 'A');
  2454. }
  2455. sm_tile(ix, iy, (int)sw_icon, 0x4A4F62u, letter);
  2456. }
  2457. }
  2458. {
  2459. const struct wm_window_info *w =
  2460. &wins[sw_slots[sw_sel]].info;
  2461. const struct sm_app *a = sw_app_for_title(w->title);
  2462. label = a ? a->label : w->title;
  2463. }
  2464. ty = sw_y + SW_PAD + (int)sw_icon + 10 +
  2465. (SW_LABEL_H - 16) / 2;
  2466. tw = (int)usurf_text_width(&screen, label);
  2467. tx = sw_x + ((int)sw_w - tw) / 2;
  2468. usurf_text(&screen, tx, ty, label, SM_TEXT,
  2469. sw_x, sw_y, sw_w, sw_h);
  2470. }
  2471. static void sw_add_present(struct wm_present_info *p)
  2472. {
  2473. if (!sw_active || !sw_count)
  2474. return;
  2475. sw_geom();
  2476. add_rect(p, sw_x, sw_y, sw_w, sw_h);
  2477. }
  2478. static void sm_draw_app(int i, const struct sm_layout *l)
  2479. {
  2480. const struct sm_app *a = &sm_apps[i];
  2481. int ry = l->list_y + i * l->row_h;
  2482. int ty = ry + (l->row_h - 16) / 2;
  2483. int tile = l->row_h - 8;
  2484. if (tile > SM_TILE)
  2485. tile = SM_TILE;
  2486. if (sm_hot == i)
  2487. sm_fill_round(l->row_x, ry + 1, l->row_w,
  2488. l->row_h - 2, SM_RADIUS, SM_PANEL_ALT);
  2489. if (!usurf_app_icon(&screen, l->row_x + 10,
  2490. ry + (l->row_h - tile) / 2, tile,
  2491. (enum app_icon)a->icon))
  2492. sm_tile(l->row_x + 10, ry + (l->row_h - tile) / 2, tile,
  2493. a->tile, a->letter);
  2494. usurf_text(&screen, l->row_x + 36, ty, a->label, SM_TEXT,
  2495. l->x, l->y, (unsigned int)l->w, (unsigned int)l->h);
  2496. }
  2497. static void sm_draw(void)
  2498. {
  2499. struct sm_layout l;
  2500. int i, ty;
  2501. if (!sm_open)
  2502. return;
  2503. sm_layout_get(&l);
  2504. /* Flat theme.bg panel with no rim, exactly like the netmenu popover.
  2505. * The Applications caption is the first item: there is no separate
  2506. * banner card. */
  2507. usurf_fill(&screen, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h,
  2508. SM_BG);
  2509. usurf_text(&screen, l.x + SM_PAD, l.sect_y + 3, "Applications",
  2510. SM_TEXT_DIM, l.x, l.y, (unsigned int)l.w,
  2511. (unsigned int)l.h);
  2512. for (i = 0; i < SM_NAPP; i++)
  2513. sm_draw_app(i, &l);
  2514. /* The Power row is a quick-settings tile rather than a plain list
  2515. * row, so the submenu entry reads as the panel's last action. */
  2516. sm_fill_round(l.row_x, l.power_y, l.row_w, l.row_h, SM_RADIUS,
  2517. sm_power_open ? SM_SEL :
  2518. (sm_power_hot == 1 ? SM_BTN_HOT : SM_PANEL_ALT));
  2519. ty = l.power_y + (l.row_h - 16) / 2;
  2520. usurf_text(&screen, l.row_x + 12, ty, "Power",
  2521. sm_power_open || sm_power_hot == 1 ? COL_WHITE : SM_TEXT,
  2522. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  2523. {
  2524. const char *arrow = sm_power_open ? "v" : ">";
  2525. int aw = (int)usurf_text_width(&screen, arrow);
  2526. usurf_text(&screen, l.row_x + l.row_w - 12 - aw, ty, arrow,
  2527. sm_power_open || sm_power_hot == 1 ? COL_WHITE :
  2528. SM_TEXT_DIM,
  2529. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  2530. }
  2531. if (!sm_power_open)
  2532. return;
  2533. /* Submenu: a rounded panel card with two rounded selection rows. */
  2534. sm_fill_round(l.sub_x, l.sub_y, l.sub_w, l.sub_h, SM_RADIUS,
  2535. SM_PANEL);
  2536. for (i = 0; i < 2; i++) {
  2537. int sy = l.sub_y + 4 + i * l.row_h;
  2538. int sty = sy + (l.row_h - 16) / 2;
  2539. if (sm_power_hot == i + 2)
  2540. sm_fill_round(l.sub_x + 4, sy + 1, l.sub_w - 8,
  2541. l.row_h - 2, 4, SM_SEL);
  2542. usurf_text(&screen, l.sub_x + 12, sty,
  2543. i == 0 ? "Reboot" : "Power off",
  2544. i == 1 ? (sm_power_hot == 3 ? COL_WHITE : SM_ERROR)
  2545. : (sm_power_hot == 2 ? COL_WHITE : SM_TEXT),
  2546. l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  2547. (unsigned int)l.sub_h);
  2548. }
  2549. }
  2550. static int sm_hit(unsigned int mx, unsigned int my)
  2551. {
  2552. struct sm_layout l;
  2553. int x = (int)mx, y = (int)my;
  2554. if (bar_start_hit(mx, my))
  2555. return SM_HIT_START;
  2556. if (!sm_open)
  2557. return SM_HIT_NONE;
  2558. sm_layout_get(&l);
  2559. if (sm_power_open &&
  2560. x >= l.sub_x && x < l.sub_x + l.sub_w &&
  2561. y >= l.sub_y && y < l.sub_y + l.sub_h) {
  2562. if (y >= l.sub_y + 4 && y < l.sub_y + 4 + 2 * l.row_h)
  2563. return (y - (l.sub_y + 4)) / l.row_h == 0 ?
  2564. SM_HIT_REBOOT : SM_HIT_POWEROFF;
  2565. return SM_HIT_MENU;
  2566. }
  2567. if (x < l.x || x >= l.x + l.w || y < l.y || y >= l.y + l.h)
  2568. return SM_HIT_NONE;
  2569. if (y >= l.list_y && y < l.list_y + SM_NAPP * l.row_h)
  2570. return (y - l.list_y) / l.row_h;
  2571. if (y >= l.power_y && y < l.power_y + l.row_h)
  2572. return SM_HIT_POWER;
  2573. return SM_HIT_MENU;
  2574. }
  2575. static int sm_update_hover(unsigned int mx, unsigned int my)
  2576. {
  2577. int hot = -1, pwr = 0, hit;
  2578. if (sm_open) {
  2579. hit = sm_hit(mx, my);
  2580. if (hit >= 0)
  2581. hot = hit;
  2582. else if (hit == SM_HIT_POWER)
  2583. pwr = 1;
  2584. else if (hit == SM_HIT_REBOOT)
  2585. pwr = 2;
  2586. else if (hit == SM_HIT_POWEROFF)
  2587. pwr = 3;
  2588. }
  2589. if (hot == sm_hot && pwr == sm_power_hot)
  2590. return 0;
  2591. sm_hot = hot;
  2592. sm_power_hot = pwr;
  2593. return 1;
  2594. }
  2595. static void sm_add_present(struct wm_present_info *p)
  2596. {
  2597. struct sm_layout l;
  2598. if (!sm_open)
  2599. return;
  2600. sm_layout_get(&l);
  2601. add_rect(p, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  2602. if (sm_power_open)
  2603. add_rect(p, l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  2604. (unsigned int)l.sub_h);
  2605. }
  2606. static void sm_close(void)
  2607. {
  2608. if (!sm_open && !sm_power_open)
  2609. return;
  2610. sm_open = 0;
  2611. sm_power_open = 0;
  2612. sm_hot = -1;
  2613. sm_power_hot = 0;
  2614. full_redraw = 1;
  2615. }
  2616. /* A client can self-resize between the pre-frame info/map refresh and
  2617. * u_wm_begin(); that unmaps and remaps its content slot, leaving the map
  2618. * cached above pointing at released pages. Once the frame is active the
  2619. * kernel refuses further self-resizes, so re-reading the generation and
  2620. * remapping here makes the cached canvas safe for the draw calls. The
  2621. * window info itself is left for the next loop, which then sees the
  2622. * generation change and does a full redraw. */
  2623. static void remap_window_contents(void)
  2624. {
  2625. unsigned int i;
  2626. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2627. struct wm_window_info info;
  2628. struct wm_map_info map;
  2629. if (u_wm_info(i, &info) != 1 || !info.used) {
  2630. wins[i].mapped = 0;
  2631. continue;
  2632. }
  2633. if (wins[i].mapped &&
  2634. wins[i].map.generation == info.content_gen)
  2635. continue;
  2636. if (u_wm_map(i, &map) == 0) {
  2637. wins[i].map = map;
  2638. wins[i].mapped = 1;
  2639. } else {
  2640. wins[i].mapped = 0;
  2641. }
  2642. }
  2643. }
  2644. static void do_update(struct wm_status *st)
  2645. {
  2646. struct wm_present_info present;
  2647. int need = 0;
  2648. int bar_dirty = 0;
  2649. unsigned int i;
  2650. char dt[20];
  2651. static char last_dt[20];
  2652. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  2653. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  2654. unsigned int msg_now = u_ticks_ms();
  2655. int osd_hide = 0, osd_repaint = 0;
  2656. int sm_dirty;
  2657. int dock_redraw = 0, dock_overlap = 0;
  2658. int input_repaint = 0;
  2659. unsigned int dj;
  2660. /* The kernel bumps backlight_seq on every brightness key/command.
  2661. * Pop the OSD on a change and keep it up for OSD_MS after the last
  2662. * one. The first poll only records the current sequence, so a WM
  2663. * restart does not flash a stale bubble. */
  2664. if (st->backlight_present) {
  2665. if (!osd_seen_valid) {
  2666. osd_seen_valid = 1;
  2667. osd_seen_seq = st->backlight_seq;
  2668. } else if (st->backlight_seq != osd_seen_seq) {
  2669. osd_seen_seq = st->backlight_seq;
  2670. osd_level = (int)st->backlight_level;
  2671. osd_until = msg_now + OSD_MS;
  2672. osd_show = 1;
  2673. }
  2674. } else if (osd_show) {
  2675. osd_show = 0;
  2676. osd_hide = 1;
  2677. }
  2678. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  2679. osd_show = 0;
  2680. osd_hide = 1;
  2681. }
  2682. if (osd_hide && osd_visible)
  2683. osd_repaint = 1;
  2684. sm_dirty = sm_update_hover(st->mouse.x, st->mouse.y);
  2685. dock_update(st->mouse.x, st->mouse.y);
  2686. /* The boot-scan window is created before the terminal so the terminal
  2687. * stays the newest (last) window and keeps the keyboard focus. Wait
  2688. * for the scan window to exist before starting the terminal; a failed
  2689. * or wedged helper is bounded so the shell is never held back. */
  2690. if (st->boot_ready && !boot_term_spawned) {
  2691. if (!boot_scan_spawned) {
  2692. boot_scan_spawned = 1;
  2693. if (free_window_slot()) {
  2694. boot_scan_pid = u_spawn("/ramfs/bin/bootscan",
  2695. "", 0, "/");
  2696. if (boot_scan_pid > 0)
  2697. boot_scan_deadline = msg_now + 2000u;
  2698. }
  2699. if (boot_scan_pid <= 0)
  2700. boot_scan_placed = 1;
  2701. }
  2702. if (boot_scan_pid > 0 && !boot_scan_placed) {
  2703. struct wm_window_info wi;
  2704. int idx = window_index_pid(boot_scan_pid, &wi);
  2705. if (idx >= 0) {
  2706. int nx = (int)fbw - (int)wi.w - 16;
  2707. int ny = WM_BAR_H + 8;
  2708. if (nx < 8)
  2709. nx = 8;
  2710. u_wm_set_geom((unsigned int)idx, nx, ny);
  2711. boot_scan_placed = 1;
  2712. } else if (!u_alive((unsigned int)boot_scan_pid) ||
  2713. (int)(msg_now - boot_scan_deadline) >= 0) {
  2714. if (u_alive((unsigned int)boot_scan_pid))
  2715. u_kill((unsigned int)boot_scan_pid);
  2716. boot_scan_placed = 1;
  2717. }
  2718. }
  2719. if (boot_scan_placed) {
  2720. boot_term_spawned = 1;
  2721. if (!free_window_slot()) {
  2722. bar_notice("window limit");
  2723. full_redraw = 1;
  2724. } else if (u_wm_create_term() < 0) {
  2725. bar_notice("cannot start term");
  2726. full_redraw = 1;
  2727. } else {
  2728. full_redraw = 1;
  2729. }
  2730. }
  2731. }
  2732. if (bar_msg_dirty ||
  2733. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  2734. bar_dirty = 1;
  2735. bar_msg_dirty = 0;
  2736. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  2737. bar_msg_until = 0;
  2738. }
  2739. bar_kexec_busy = st->kexec_busy;
  2740. bar_kexec_progress = st->kexec_progress;
  2741. bar_kexec_total = st->kexec_total;
  2742. if (kexec_changed)
  2743. bar_dirty = 1;
  2744. {
  2745. int hot = bar_start_hit(st->mouse.x, st->mouse.y);
  2746. if (hot != sm_start_hot) {
  2747. sm_start_hot = hot;
  2748. bar_dirty = 1;
  2749. }
  2750. }
  2751. {
  2752. int hot = bar_status_hit(st->mouse.x, st->mouse.y);
  2753. if (hot != bar_status_hot) {
  2754. bar_status_hot = hot;
  2755. bar_dirty = 1;
  2756. }
  2757. }
  2758. /* Refresh cached window state and mappings before opening a frame:
  2759. * MAP may unmap/remap content. */
  2760. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2761. struct wm_window_info info;
  2762. int r = u_wm_info(i, &info);
  2763. if (r < 0) {
  2764. u_exit(1);
  2765. }
  2766. if (r == 0 || !info.used) {
  2767. if (wins[i].info.used)
  2768. full_redraw = 1;
  2769. wins[i].info.used = 0;
  2770. wins[i].mapped = 0;
  2771. continue;
  2772. }
  2773. {
  2774. int pid_changed = wins[i].info.used &&
  2775. info.pid != wins[i].info.pid;
  2776. if (!wins[i].info.used ||
  2777. info.content_gen != wins[i].info.content_gen ||
  2778. pid_changed)
  2779. full_redraw = 1;
  2780. wins[i].info = info;
  2781. if (!wins[i].mapped || pid_changed ||
  2782. wins[i].map.generation != info.content_gen) {
  2783. struct wm_map_info map;
  2784. if (u_wm_map(i, &map) == 0) {
  2785. wins[i].map = map;
  2786. wins[i].mapped = 1;
  2787. } else {
  2788. wins[i].mapped = 0;
  2789. }
  2790. }
  2791. }
  2792. }
  2793. /* Alt+Tab runs after the info refresh: focus transitions are visible
  2794. * in wins[] and a window that closed mid-session is filtered out. */
  2795. sw_track_focus();
  2796. input_repaint = wm_input_process(st);
  2797. /* Close-icon hover: only the topmost window under a free pointer
  2798. * reacts; an open menu, the dock and the top bar own the pointer when
  2799. * they are under it. A change repaints just the two button rects. */
  2800. {
  2801. int hot = -1;
  2802. if (resize_id < 0 && drag_id < 0 && !sm_open &&
  2803. !dock_pointer_inside(st->mouse.x, st->mouse.y) &&
  2804. st->mouse.y >= WM_BAR_H)
  2805. hot = close_window_at((int)st->mouse.x,
  2806. (int)st->mouse.y);
  2807. if (hot != close_hot) {
  2808. close_hot_old = close_hot;
  2809. close_hot = hot;
  2810. close_dirty = 1;
  2811. }
  2812. }
  2813. /* Popovers are WM-anchored overlays, flush against the bottom of the
  2814. * bar and the right screen edge. The WM owns the geometry, so the app
  2815. * does not need a placement ABI. */
  2816. {
  2817. int control_found = 0;
  2818. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2819. struct wm_window_info ni;
  2820. int nx, ny;
  2821. if (!wins[i].info.used || !wins[i].info.popover)
  2822. continue;
  2823. if (control_pid > 0 &&
  2824. wins[i].info.pid == (unsigned int)control_pid)
  2825. control_found = 1;
  2826. nx = (int)fbw - (int)wins[i].info.w;
  2827. ny = WM_BAR_H;
  2828. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  2829. if (u_wm_set_geom(i, nx, ny) == 0 &&
  2830. u_wm_info(i, &ni) == 1) {
  2831. wins[i].info = ni;
  2832. full_redraw = 1;
  2833. }
  2834. }
  2835. }
  2836. if (!control_found && control_pid > 0 &&
  2837. !u_alive((unsigned int)control_pid)) {
  2838. control_pid = 0;
  2839. bar_dirty = 1;
  2840. }
  2841. }
  2842. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  2843. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  2844. {
  2845. static unsigned int last_rtc_ms;
  2846. unsigned int now_ms = u_ticks_ms();
  2847. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  2848. last_rtc_ms = now_ms;
  2849. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  2850. for (i = 0; i < sizeof(dt); i++)
  2851. if (dt[i] != last_dt[i]) {
  2852. for (i = 0; i < sizeof(dt); i++)
  2853. last_dt[i] = dt[i];
  2854. bar_dirty = 1;
  2855. break;
  2856. }
  2857. }
  2858. {
  2859. struct sys_cputemp ct;
  2860. if (u_cpu_temp(&ct) == 0 &&
  2861. ct.temp_c != bar_temp_c) {
  2862. bar_temp_c = ct.temp_c;
  2863. bar_dirty = 1;
  2864. }
  2865. }
  2866. {
  2867. struct sys_powerinfo pi;
  2868. int pct = -1, charging = 0, ac = -1;
  2869. unsigned int bi;
  2870. if (u_power_info(&pi, 0) == 0) {
  2871. /* A laptop EC (ThinkPad or Chrome
  2872. * EC) marks a machine with AC/battery;
  2873. * desktops show neither. */
  2874. if (pi.present) {
  2875. ac = pi.ac_online;
  2876. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  2877. bi++) {
  2878. struct sys_battery *b =
  2879. &pi.battery[bi];
  2880. if (!b->present ||
  2881. !b->percent_valid)
  2882. continue;
  2883. pct = (int)b->percent;
  2884. charging = (int)b->charging;
  2885. break;
  2886. }
  2887. }
  2888. if (pct != bar_batt_pct ||
  2889. charging != bar_batt_charging ||
  2890. ac != bar_ac_online) {
  2891. bar_batt_pct = pct;
  2892. bar_batt_charging = charging;
  2893. bar_ac_online = ac;
  2894. bar_dirty = 1;
  2895. }
  2896. }
  2897. }
  2898. if (poll_net_status())
  2899. bar_dirty = 1;
  2900. }
  2901. }
  2902. if (full_redraw) {
  2903. if (u_wm_begin() != 0) {
  2904. full_redraw = 1;
  2905. return;
  2906. }
  2907. full_redraw = 0;
  2908. remap_window_contents();
  2909. need = 1;
  2910. {
  2911. draw_rect(0, 0, fbw, fbh);
  2912. draw_bar();
  2913. dock_draw();
  2914. dock_mark_drawn();
  2915. if (osd_show)
  2916. osd_draw();
  2917. osd_visible = osd_show;
  2918. sm_draw();
  2919. sw_draw();
  2920. sw_visible = sw_active;
  2921. run_draw();
  2922. run_visible = run_open;
  2923. present.count = 0;
  2924. add_rect(&present, 0, 0, fbw, fbh);
  2925. present.immediate = 0;
  2926. u_wm_present(&present);
  2927. /* Ack every window that was clean at the time of the
  2928. * redraw. If new damage arrived during it, its seq
  2929. * differs and the kernel leaves it dirty. */
  2930. for (i = 0; i < WM_MAX_WINDOWS; i++)
  2931. if (wins[i].info.used && wins[i].mapped)
  2932. u_wm_ack(i, wins[i].info.dirty_seq);
  2933. }
  2934. return;
  2935. }
  2936. /* Partial cell/canvas updates and the bar clock. */
  2937. present.count = 0;
  2938. present.immediate = 0;
  2939. if (close_dirty) {
  2940. close_dirty = 0;
  2941. close_add_damage(&present, close_hot);
  2942. close_add_damage(&present, close_hot_old);
  2943. close_hot_old = -1;
  2944. need = 1;
  2945. }
  2946. if (osd_repaint) {
  2947. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  2948. need = 1;
  2949. }
  2950. if ((input_repaint & WM_INPUT_SW) && sw_active) {
  2951. sw_geom();
  2952. add_rect(&present, sw_x, sw_y, sw_w, sw_h);
  2953. need = 1;
  2954. }
  2955. if ((input_repaint & WM_INPUT_RUN) && run_open) {
  2956. run_geom();
  2957. if (run_prev_valid)
  2958. add_rect(&present, run_prev_x, run_prev_y,
  2959. run_prev_w, run_prev_h);
  2960. add_rect(&present, run_x, run_y, run_w, run_h);
  2961. need = 1;
  2962. }
  2963. if (osd_show)
  2964. need = 1;
  2965. if (bar_dirty) {
  2966. need = 1;
  2967. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  2968. }
  2969. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2970. struct uwin *u = &wins[i];
  2971. struct wm_window_info *w = &u->info;
  2972. int rx, ry;
  2973. unsigned int rw, rh;
  2974. if (!w->used || !w->dirty)
  2975. continue;
  2976. if (!w->dmg_w || !w->dmg_h)
  2977. continue;
  2978. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  2979. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  2980. (int)w->dmg_y;
  2981. rw = w->dmg_w;
  2982. rh = w->dmg_h;
  2983. add_rect(&present, rx, ry, rw, rh);
  2984. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  2985. need = 1;
  2986. }
  2987. if (sm_dirty && sm_open)
  2988. need = 1;
  2989. for (dj = 0; dj < present.count; dj++) {
  2990. int rx = present.rect[dj].x;
  2991. int ry = present.rect[dj].y;
  2992. int rx1 = rx + (int)present.rect[dj].w;
  2993. int ry1 = ry + (int)present.rect[dj].h;
  2994. int dx1 = dock.visual.x + (int)dock.visual.w;
  2995. int dy1 = dock.visual.y + (int)dock.visual.h;
  2996. if (rx < dx1 && rx1 > dock.visual.x &&
  2997. ry < dy1 && ry1 > dock.visual.y) {
  2998. dock_overlap = 1;
  2999. break;
  3000. }
  3001. }
  3002. /* A window damage rectangle under an overlay panel would overwrite
  3003. * its pixels; repaint the panel after the window draw. */
  3004. if (sw_active && !(input_repaint & WM_INPUT_SW)) {
  3005. sw_geom();
  3006. for (dj = 0; dj < present.count; dj++) {
  3007. int rx = present.rect[dj].x;
  3008. int ry = present.rect[dj].y;
  3009. if (rx < sw_x + (int)sw_w &&
  3010. rx + (int)present.rect[dj].w > sw_x &&
  3011. ry < sw_y + (int)sw_h &&
  3012. ry + (int)present.rect[dj].h > sw_y) {
  3013. input_repaint |= WM_INPUT_SW;
  3014. add_rect(&present, sw_x, sw_y, sw_w, sw_h);
  3015. need = 1;
  3016. break;
  3017. }
  3018. }
  3019. }
  3020. if (run_open && !(input_repaint & WM_INPUT_RUN)) {
  3021. run_geom();
  3022. for (dj = 0; dj < present.count; dj++) {
  3023. int rx = present.rect[dj].x;
  3024. int ry = present.rect[dj].y;
  3025. if (rx < run_x + (int)run_w &&
  3026. rx + (int)present.rect[dj].w > run_x &&
  3027. ry < run_y + (int)run_h &&
  3028. ry + (int)present.rect[dj].h > run_y) {
  3029. input_repaint |= WM_INPUT_RUN;
  3030. add_rect(&present, run_x, run_y, run_w, run_h);
  3031. need = 1;
  3032. break;
  3033. }
  3034. }
  3035. }
  3036. if (dock_dirty || dock_overlap) {
  3037. dock_damage(&present);
  3038. dock_redraw = 1;
  3039. need = 1;
  3040. }
  3041. if (!need) {
  3042. /* Drag movement is handled below; a stationary mouse can be
  3043. * left to the kernel's cursor overlay. */
  3044. return;
  3045. }
  3046. if (sm_open)
  3047. sm_add_present(&present);
  3048. if (u_wm_begin() != 0)
  3049. return;
  3050. remap_window_contents();
  3051. for (i = 0; i < present.count; i++)
  3052. draw_rect(present.rect[i].x, present.rect[i].y,
  3053. present.rect[i].w, present.rect[i].h);
  3054. if (bar_dirty)
  3055. draw_bar();
  3056. if (dock_redraw) {
  3057. dock_draw();
  3058. dock_mark_drawn();
  3059. }
  3060. if (osd_show) {
  3061. osd_draw();
  3062. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  3063. }
  3064. osd_visible = osd_show;
  3065. sm_draw();
  3066. if (sw_active)
  3067. sw_draw();
  3068. sw_visible = sw_active;
  3069. if (sw_active)
  3070. sw_add_present(&present);
  3071. if (run_open)
  3072. run_draw();
  3073. run_visible = run_open;
  3074. if (run_open)
  3075. run_add_present(&present);
  3076. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  3077. struct uwin *u = &wins[i];
  3078. if (!u->info.used || !u->info.dirty || !u->mapped ||
  3079. !u->ack_seq)
  3080. continue;
  3081. if (u->ack_seq == u->info.dirty_seq)
  3082. u_wm_ack(i, u->info.dirty_seq);
  3083. }
  3084. u_wm_present(&present);
  3085. }
  3086. static void spawn_app_arg_checked(const char *path, const char *arg)
  3087. {
  3088. if (!free_window_slot()) {
  3089. bar_notice("window limit");
  3090. full_redraw = 1;
  3091. return;
  3092. }
  3093. if (u_spawn(path, arg, 0, "/") < 0) {
  3094. bar_notice("spawn failed");
  3095. full_redraw = 1;
  3096. } else {
  3097. full_redraw = 1;
  3098. }
  3099. }
  3100. static const char *settings_section_name(unsigned int section)
  3101. {
  3102. switch (section) {
  3103. case WM_SETTINGS_NETWORK: return "network";
  3104. case WM_SETTINGS_POWER: return "power";
  3105. case WM_SETTINGS_DATETIME: return "time";
  3106. case WM_SETTINGS_APPEARANCE: return "appearance";
  3107. case WM_SETTINGS_WINDOWS: return "windows";
  3108. case WM_SETTINGS_ABOUT: return "about";
  3109. default: return "network";
  3110. }
  3111. }
  3112. /* The running Settings window polls this file; a focus request for another
  3113. * section therefore switches the pane without a restart. */
  3114. static void settings_write_request(unsigned int section)
  3115. {
  3116. const char *name = settings_section_name(section);
  3117. char buf[20];
  3118. unsigned int n = 0;
  3119. while (name[n] && n + 2 < sizeof(buf)) {
  3120. buf[n] = name[n];
  3121. n++;
  3122. }
  3123. buf[n++] = '\n';
  3124. buf[n] = '\0';
  3125. u_syscall3(SYS_WRITE_FILE, (int)WM_SETTINGS_REQUEST, (int)buf, (int)n);
  3126. }
  3127. static int window_title_is(const struct wm_window_info *w, const char *title)
  3128. {
  3129. unsigned int i = 0;
  3130. while (w->title[i] && title[i] && w->title[i] == title[i])
  3131. i++;
  3132. return w->title[i] == title[i];
  3133. }
  3134. /* Open the Settings app at one section. The WM is the only process that
  3135. * may focus another window, so every launcher (popover pills, start menu
  3136. * and dock) goes through SYS_WM_OPEN_SETTINGS and lands here. */
  3137. static void open_settings(unsigned int section)
  3138. {
  3139. unsigned int i;
  3140. if (section >= WM_SETTINGS_COUNT)
  3141. section = WM_SETTINGS_NETWORK;
  3142. settings_write_request(section);
  3143. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  3144. if (!wins[i].info.used || !window_title_is(&wins[i].info,
  3145. "Settings"))
  3146. continue;
  3147. if (u_wm_focus(i) == 0) {
  3148. full_redraw = 1;
  3149. return;
  3150. }
  3151. }
  3152. if (settings_pid > 0 && u_alive((unsigned int)settings_pid))
  3153. return; /* window is still being created */
  3154. settings_pid = 0;
  3155. if (!free_window_slot()) {
  3156. bar_notice("window limit");
  3157. full_redraw = 1;
  3158. return;
  3159. }
  3160. settings_pid = u_spawn("/ramfs/bin/settings",
  3161. settings_section_name(section), 0, "/");
  3162. if (settings_pid < 0) {
  3163. settings_pid = 0;
  3164. bar_notice("cannot start settings");
  3165. full_redraw = 1;
  3166. } else {
  3167. full_redraw = 1;
  3168. }
  3169. }
  3170. /* Any status target opens one control panel; a second click closes it. */
  3171. static void open_control_panel(void)
  3172. {
  3173. unsigned int i;
  3174. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  3175. for (i = 0; i < WM_MAX_WINDOWS; i++)
  3176. if (wins[i].info.used &&
  3177. wins[i].info.pid == (unsigned int)control_pid) {
  3178. if (u_wm_close(i) == 0) {
  3179. control_pid = 0;
  3180. full_redraw = 1;
  3181. } else {
  3182. u_wm_focus(i);
  3183. full_redraw = 1;
  3184. }
  3185. return;
  3186. }
  3187. /* The process is alive but has not created its window yet. */
  3188. return;
  3189. }
  3190. if (!free_window_slot()) {
  3191. bar_notice("window limit");
  3192. full_redraw = 1;
  3193. return;
  3194. }
  3195. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  3196. if (control_pid < 0) {
  3197. control_pid = 0;
  3198. bar_notice("spawn failed");
  3199. full_redraw = 1;
  3200. } else {
  3201. full_redraw = 1;
  3202. }
  3203. }
  3204. /* Launch one start-menu entry. The terminal and the control panel are WM
  3205. * actions; every other entry spawns its ELF from the pack. */
  3206. static void sm_launch(int idx)
  3207. {
  3208. const struct sm_app *a;
  3209. if (idx < 0 || idx >= SM_NAPP) {
  3210. sm_close();
  3211. return;
  3212. }
  3213. a = &sm_apps[idx];
  3214. sm_close();
  3215. if (a->action == SM_ACT_TERM) {
  3216. if (!free_window_slot()) {
  3217. bar_notice("window limit");
  3218. full_redraw = 1;
  3219. } else if (u_wm_create_term() < 0) {
  3220. bar_notice("cannot start term");
  3221. full_redraw = 1;
  3222. } else {
  3223. full_redraw = 1;
  3224. }
  3225. } else if (a->action == SM_ACT_SETTINGS) {
  3226. open_settings(WM_SETTINGS_NETWORK);
  3227. } else {
  3228. spawn_app_arg_checked(a->path, a->arg);
  3229. }
  3230. }
  3231. /* Index of a popover containing the point, or -1. */
  3232. static int popover_under(unsigned int mx, unsigned int my)
  3233. {
  3234. unsigned int i;
  3235. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  3236. struct wm_window_info *w = &wins[i].info;
  3237. if (w->used && w->popover &&
  3238. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  3239. mx < (unsigned int)(w->x + (int)w->w) &&
  3240. my < (unsigned int)(w->y + (int)w->h))
  3241. return (int)i;
  3242. }
  3243. return -1;
  3244. }
  3245. /* A press anywhere outside an open popover dismisses it; the click then
  3246. * continues normally so the target under the pointer still reacts. */
  3247. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  3248. {
  3249. unsigned int i;
  3250. if (popover_under(mx, my) >= 0)
  3251. return;
  3252. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  3253. if (!wins[i].info.used || !wins[i].info.popover)
  3254. continue;
  3255. if (wins[i].info.pid == (unsigned int)control_pid)
  3256. control_pid = 0;
  3257. if (u_wm_close(i) == 0) {
  3258. wins[i].info.used = 0;
  3259. wins[i].mapped = 0;
  3260. full_redraw = 1;
  3261. }
  3262. break;
  3263. }
  3264. }
  3265. /* ---- Bottom dock -------------------------------------------------------
  3266. * A macOS-like pinned icon bar. The icons are full-colour GNOME app icons
  3267. * from /ramfs/icons/apps96.icn; the panel is drawn into the WM shadow after
  3268. * the windows and is therefore an overlay, not a work-area reservation.
  3269. * The pointer's x distance to each base slot sets a smooth 1.0..1.75 scale;
  3270. * the scaled icons are laid out again and the row is recentred, which makes
  3271. * the magnified icons push their neighbours aside as the pointer moves. */
  3272. static int dock_streq(const char *a, const char *b)
  3273. {
  3274. while (*a && *a == *b) {
  3275. a++;
  3276. b++;
  3277. }
  3278. return *a == *b;
  3279. }
  3280. static int dock_gap_size(void)
  3281. {
  3282. if (fbw < 560)
  3283. return 4;
  3284. if (fbw < 760)
  3285. return 6;
  3286. return 10;
  3287. }
  3288. static int dock_base_total(int base, int gap)
  3289. {
  3290. int i, total = DOCK_N * base;
  3291. for (i = 0; i + 1 < DOCK_N; i++)
  3292. total += gap + (i == DOCK_N - 2 ? DOCK_SEP_GAP : 0);
  3293. return total;
  3294. }
  3295. static int dock_base_size(int gap)
  3296. {
  3297. int base = DOCK_BASE_MAX;
  3298. while (base > DOCK_BASE_MIN &&
  3299. dock_base_total(base, gap) + 2 * DOCK_PAD > (int)fbw)
  3300. base--;
  3301. return base;
  3302. }
  3303. static int dock_gap_after(int i)
  3304. {
  3305. return dock.gap + (i == DOCK_N - 2 ? DOCK_SEP_GAP : 0);
  3306. }
  3307. /* The canvas title of a running app, or -1. The WM does not track an
  3308. * executable id, and a window title is the stable app identity the canvas
  3309. * ABI already carries. */
  3310. static int app_window_index(const struct sm_app *a)
  3311. {
  3312. int order[WM_MAX_WINDOWS];
  3313. unsigned int n = build_order(order), i;
  3314. if (!a->win_title)
  3315. return -1;
  3316. for (i = 0; i < n; i++) {
  3317. int idx = order[i];
  3318. if (wins[idx].info.used &&
  3319. dock_streq(wins[idx].info.title, a->win_title))
  3320. return idx;
  3321. }
  3322. return -1;
  3323. }
  3324. static void dock_update(unsigned int mx, unsigned int my)
  3325. {
  3326. int base, gap, i, bx, total, cursor;
  3327. int panel_top, max_extra, band_top, left, right;
  3328. int active, repaint = 0, moving = 0, still = 0;
  3329. int old_hover = dock_hover;
  3330. base = dock_base_size(dock_gap_size());
  3331. gap = dock_gap_size();
  3332. if (!dock_scale_ready) {
  3333. for (i = 0; i < DOCK_N; i++)
  3334. dock_scale[i] = DOCK_SCALE_ONE;
  3335. dock_scale_ready = 1;
  3336. }
  3337. /* Base slots: fixed centres, so the pointer does not chase a layout
  3338. * that is itself moving with the magnification. */
  3339. bx = (int)((fbw - (unsigned int)(dock_base_total(base, gap) +
  3340. 2 * DOCK_PAD)) / 2) + DOCK_PAD;
  3341. for (i = 0; i < DOCK_N; i++) {
  3342. dock.base_center[i] = bx + base / 2;
  3343. bx += base + (i + 1 < DOCK_N ? gap +
  3344. (i == DOCK_N - 2 ? DOCK_SEP_GAP : 0) : 0);
  3345. }
  3346. panel_top = (int)fbh - DOCK_BOTTOM - (base + DOCK_PANEL_EXTRA);
  3347. max_extra = base * DOCK_SCALE_MAX / DOCK_SCALE_ONE - base;
  3348. band_top = panel_top - max_extra - DOCK_LABEL_GAP - 12;
  3349. if (band_top < WM_BAR_H)
  3350. band_top = WM_BAR_H;
  3351. left = (int)((fbw - (unsigned int)dock_base_total(base, gap)) / 2) -
  3352. base * 3;
  3353. right = left + dock_base_total(base, gap) + base * 6;
  3354. active = (int)my >= band_top && (int)my <= (int)fbh &&
  3355. (int)mx >= left && (int)mx <= right;
  3356. if (active != dock_active) {
  3357. dock_active = active;
  3358. repaint = 1;
  3359. }
  3360. for (i = 0; i < DOCK_N; i++) {
  3361. int target = DOCK_SCALE_ONE;
  3362. if (active) {
  3363. int d = (int)mx - dock.base_center[i];
  3364. int reach = (base + gap) * 5 / 2;
  3365. if (d < 0)
  3366. d = -d;
  3367. if (d < reach) {
  3368. int w = (reach - d) * 256 / reach;
  3369. w = w * w / 256;
  3370. target = DOCK_SCALE_ONE +
  3371. ((DOCK_SCALE_MAX - DOCK_SCALE_ONE) *
  3372. w) / 256;
  3373. }
  3374. }
  3375. if (dock_scale[i] != target) {
  3376. int diff = target - (int)dock_scale[i];
  3377. int step = diff / 4;
  3378. if (!step)
  3379. step = diff > 0 ? 1 : -1;
  3380. dock_scale[i] = (unsigned short)((int)dock_scale[i] +
  3381. step);
  3382. diff = target - (int)dock_scale[i];
  3383. if (diff < 0)
  3384. diff = -diff;
  3385. if (diff <= 2)
  3386. dock_scale[i] = (unsigned short)target;
  3387. moving = 1;
  3388. }
  3389. if (dock_scale[i] != target)
  3390. still = 1;
  3391. }
  3392. dock.base = base;
  3393. dock.gap = gap;
  3394. dock.pad = DOCK_PAD;
  3395. dock.panel_h = base + DOCK_PANEL_EXTRA;
  3396. dock.panel_bottom = (int)fbh - DOCK_BOTTOM;
  3397. dock.panel_y = dock.panel_bottom - dock.panel_h;
  3398. dock.icon_bottom = dock.panel_bottom - DOCK_ICON_BOTTOM;
  3399. total = 2 * DOCK_PAD;
  3400. for (i = 0; i < DOCK_N; i++) {
  3401. dock.size[i] = (base * dock_scale[i] + DOCK_SCALE_ONE / 2) /
  3402. DOCK_SCALE_ONE;
  3403. total += dock.size[i];
  3404. if (i + 1 < DOCK_N)
  3405. total += dock_gap_after(i);
  3406. }
  3407. dock.panel_w = total;
  3408. dock.panel_x = (int)((fbw - (unsigned int)total) / 2);
  3409. cursor = dock.panel_x + DOCK_PAD;
  3410. for (i = 0; i < DOCK_N; i++) {
  3411. dock.x[i] = cursor;
  3412. dock.y[i] = dock.icon_bottom - dock.size[i];
  3413. cursor += dock.size[i];
  3414. if (i + 1 < DOCK_N)
  3415. cursor += dock_gap_after(i);
  3416. }
  3417. dock_hover = -1;
  3418. for (i = 0; i < DOCK_N; i++) {
  3419. if ((int)mx >= dock.x[i] &&
  3420. (int)mx < dock.x[i] + dock.size[i] &&
  3421. (int)my >= dock.y[i] &&
  3422. (int)my < dock.y[i] + dock.size[i]) {
  3423. dock_hover = i;
  3424. break;
  3425. }
  3426. }
  3427. if (dock_hover != old_hover)
  3428. repaint = 1;
  3429. dock.label_w = 0;
  3430. if (dock_hover >= 0) {
  3431. const struct sm_app *a = &sm_apps[dock_apps[dock_hover]];
  3432. int tw = (int)usurf_text_width(&screen, a->label);
  3433. dock.label_idx = dock_hover;
  3434. dock.label_w = tw + 20;
  3435. dock.label_h = DOCK_LABEL_H;
  3436. dock.label_x = dock.x[dock_hover] + dock.size[dock_hover] / 2 -
  3437. dock.label_w / 2;
  3438. if (dock.label_x < 4)
  3439. dock.label_x = 4;
  3440. if (dock.label_x + dock.label_w > (int)fbw - 4)
  3441. dock.label_x = (int)fbw - 4 - dock.label_w;
  3442. dock.label_y = dock.y[dock_hover] - DOCK_LABEL_GAP;
  3443. if (dock.label_y < WM_BAR_H + 2)
  3444. dock.label_y = WM_BAR_H + 2;
  3445. } else {
  3446. dock.label_idx = -1;
  3447. }
  3448. {
  3449. int x0 = dock.panel_x, y0 = dock.panel_y;
  3450. int x1 = dock.panel_x + dock.panel_w;
  3451. int y1 = dock.panel_bottom;
  3452. for (i = 0; i < DOCK_N; i++) {
  3453. if (dock.x[i] < x0)
  3454. x0 = dock.x[i];
  3455. if (dock.y[i] < y0)
  3456. y0 = dock.y[i];
  3457. if (dock.x[i] + dock.size[i] > x1)
  3458. x1 = dock.x[i] + dock.size[i];
  3459. if (dock.y[i] + dock.size[i] > y1)
  3460. y1 = dock.y[i] + dock.size[i];
  3461. }
  3462. if (dock.label_w) {
  3463. if (dock.label_x < x0)
  3464. x0 = dock.label_x;
  3465. if (dock.label_y < y0)
  3466. y0 = dock.label_y;
  3467. if (dock.label_x + dock.label_w > x1)
  3468. x1 = dock.label_x + dock.label_w;
  3469. if (dock.label_y + dock.label_h > y1)
  3470. y1 = dock.label_y + dock.label_h;
  3471. }
  3472. dock.visual.x = x0;
  3473. dock.visual.y = y0;
  3474. dock.visual.w = (unsigned int)(x1 - x0);
  3475. dock.visual.h = (unsigned int)(y1 - y0);
  3476. }
  3477. if (moving) {
  3478. unsigned int now = u_ticks_ms();
  3479. /* The WM loop runs at 100 Hz; cap the magnification redraw at
  3480. * about 60 Hz, but always paint the frame that settles. */
  3481. if ((int)(now - dock_last_paint_ms) >= 16 || !still)
  3482. repaint = 1;
  3483. }
  3484. if (repaint)
  3485. dock_dirty = 1;
  3486. }
  3487. static void dock_draw(void)
  3488. {
  3489. int i;
  3490. if (!dock.panel_w)
  3491. return;
  3492. /* A 1 px rim and a translucent interior; the WM shadow below already
  3493. * contains the wallpaper and windows, so this is a real overlay. */
  3494. usurf_fill_round_alpha(&screen, dock.panel_x, dock.panel_y,
  3495. dock.panel_w, dock.panel_h, 16,
  3496. 0x46506Cu, 190);
  3497. if (dock.panel_w > 2 && dock.panel_h > 2)
  3498. usurf_fill_round_alpha(&screen, dock.panel_x + 1,
  3499. dock.panel_y + 1, dock.panel_w - 2,
  3500. dock.panel_h - 2, 15,
  3501. 0x181C28u, 205);
  3502. {
  3503. int sx = dock.x[DOCK_N - 1] -
  3504. (dock.gap + DOCK_SEP_GAP) / 2;
  3505. usurf_fill_round_alpha(&screen, sx, dock.panel_y + 12, 1,
  3506. dock.panel_h - 24, 0, 0x5A6480u,
  3507. 120);
  3508. }
  3509. for (i = 0; i < DOCK_N; i++) {
  3510. const struct sm_app *a = &sm_apps[dock_apps[i]];
  3511. if (!usurf_app_icon(&screen, dock.x[i], dock.y[i],
  3512. dock.size[i], (enum app_icon)a->icon))
  3513. sm_tile(dock.x[i], dock.y[i], dock.size[i],
  3514. a->tile, a->letter);
  3515. if (app_window_index(a) >= 0)
  3516. usurf_fill(&screen,
  3517. dock.x[i] + dock.size[i] / 2 - 2,
  3518. dock.panel_bottom - 6, 4, 4, 0xE8EAF6u);
  3519. }
  3520. if (dock.label_w) {
  3521. const struct sm_app *a = &sm_apps[dock_apps[dock.label_idx]];
  3522. usurf_fill_round_alpha(&screen, dock.label_x, dock.label_y,
  3523. dock.label_w, dock.label_h, 8,
  3524. 0x10141Eu, 235);
  3525. usurf_text(&screen, dock.label_x + 10, dock.label_y + 5,
  3526. a->label, SM_TEXT, dock.label_x, dock.label_y,
  3527. (unsigned int)dock.label_w,
  3528. (unsigned int)dock.label_h);
  3529. }
  3530. }
  3531. static void dock_mark_drawn(void)
  3532. {
  3533. dock_prev = dock.visual;
  3534. dock_prev_valid = 1;
  3535. dock_dirty = 0;
  3536. dock_last_paint_ms = u_ticks_ms();
  3537. }
  3538. static void dock_damage(struct wm_present_info *p)
  3539. {
  3540. int x0, y0, x1, y1;
  3541. if (!dock.visual.w || !dock.visual.h)
  3542. return;
  3543. x0 = dock.visual.x;
  3544. y0 = dock.visual.y;
  3545. x1 = x0 + (int)dock.visual.w;
  3546. y1 = y0 + (int)dock.visual.h;
  3547. if (dock_prev_valid) {
  3548. if (dock_prev.x < x0)
  3549. x0 = dock_prev.x;
  3550. if (dock_prev.y < y0)
  3551. y0 = dock_prev.y;
  3552. if (dock_prev.x + (int)dock_prev.w > x1)
  3553. x1 = dock_prev.x + (int)dock_prev.w;
  3554. if (dock_prev.y + (int)dock_prev.h > y1)
  3555. y1 = dock_prev.y + (int)dock_prev.h;
  3556. }
  3557. add_rect(p, x0, y0, (unsigned int)(x1 - x0),
  3558. (unsigned int)(y1 - y0));
  3559. }
  3560. static int dock_icon_at(unsigned int mx, unsigned int my)
  3561. {
  3562. int i;
  3563. for (i = 0; i < DOCK_N; i++) {
  3564. if ((int)mx >= dock.x[i] - 1 &&
  3565. (int)mx < dock.x[i] + dock.size[i] + 1 &&
  3566. (int)my >= dock.y[i] - 1 &&
  3567. (int)my < dock.y[i] + dock.size[i] + 1)
  3568. return i;
  3569. }
  3570. return -1;
  3571. }
  3572. static int dock_pointer_inside(unsigned int mx, unsigned int my)
  3573. {
  3574. if (dock_icon_at(mx, my) >= 0)
  3575. return 1;
  3576. if ((int)mx >= dock.panel_x &&
  3577. (int)mx < dock.panel_x + dock.panel_w &&
  3578. (int)my >= dock.panel_y &&
  3579. (int)my < dock.panel_bottom)
  3580. return 1;
  3581. if (dock.label_w &&
  3582. (int)mx >= dock.label_x &&
  3583. (int)mx < dock.label_x + dock.label_w &&
  3584. (int)my >= dock.label_y &&
  3585. (int)my < dock.label_y + dock.label_h)
  3586. return 1;
  3587. return 0;
  3588. }
  3589. static void dock_activate(int idx)
  3590. {
  3591. const struct sm_app *a = &sm_apps[dock_apps[idx]];
  3592. int win = app_window_index(a);
  3593. if (win >= 0 && u_wm_focus((unsigned int)win) == 0) {
  3594. full_redraw = 1;
  3595. return;
  3596. }
  3597. if (a->action == SM_ACT_SETTINGS) {
  3598. open_settings(WM_SETTINGS_NETWORK);
  3599. return;
  3600. }
  3601. sm_launch(dock_apps[idx]);
  3602. }
  3603. static int dock_click_at(unsigned int mx, unsigned int my)
  3604. {
  3605. int i = dock_icon_at(mx, my);
  3606. if (i >= 0) {
  3607. const struct sm_app *a = &sm_apps[dock_apps[i]];
  3608. if (!(a->action == SM_ACT_SETTINGS &&
  3609. app_window_index(a) >= 0))
  3610. dismiss_popover_outside(mx, my);
  3611. dock_activate(i);
  3612. return 1;
  3613. }
  3614. if (dock_pointer_inside(mx, my)) {
  3615. dismiss_popover_outside(mx, my);
  3616. return 1;
  3617. }
  3618. return 0;
  3619. }
  3620. static void dock_capture_update(unsigned int mx, unsigned int my,
  3621. unsigned int buttons)
  3622. {
  3623. int inside;
  3624. /* A window drag/resize owns the pointer itself; do not touch its
  3625. * capture. */
  3626. if (resize_id >= 0 || drag_id >= 0) {
  3627. dock_captured = 0;
  3628. return;
  3629. }
  3630. inside = dock_pointer_inside(mx, my);
  3631. if (inside && !dock_captured && buttons == 0) {
  3632. if (u_wm_mouse_capture(1) == 0)
  3633. dock_captured = 1;
  3634. } else if (!inside && dock_captured && buttons == 0) {
  3635. u_wm_mouse_capture(0);
  3636. dock_captured = 0;
  3637. }
  3638. }
  3639. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  3640. * opposite edges stay put; the work area clamps the active edges. `force`
  3641. * commits the geometry even when the throttle would still defer it (button
  3642. * release). */
  3643. static void resize_window_drag(struct uwin *u, unsigned int idx,
  3644. unsigned int mx, unsigned int my, int force)
  3645. {
  3646. int dx = (int)mx - (int)resize_mx;
  3647. int dy = (int)my - (int)resize_my;
  3648. int left = resize_ox;
  3649. int right = resize_ox + (int)resize_ow;
  3650. int top = resize_oy;
  3651. int bottom = resize_oy + (int)resize_oh;
  3652. int nx, ny, nw, nh;
  3653. unsigned int now;
  3654. if (resize_edges & WM_EDGE_LEFT) {
  3655. left = resize_ox + dx;
  3656. if (left < 0)
  3657. left = 0;
  3658. } else if (resize_edges & WM_EDGE_RIGHT) {
  3659. right = resize_ox + (int)resize_ow + dx;
  3660. if (right > (int)fbw)
  3661. right = (int)fbw;
  3662. }
  3663. if (resize_edges & WM_EDGE_TOP) {
  3664. top = resize_oy + dy;
  3665. if (top < WM_BAR_H)
  3666. top = WM_BAR_H;
  3667. } else if (resize_edges & WM_EDGE_BOTTOM) {
  3668. bottom = resize_oy + (int)resize_oh + dy;
  3669. if (bottom > (int)fbh)
  3670. bottom = (int)fbh;
  3671. }
  3672. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  3673. right - left < 200) {
  3674. if (resize_edges & WM_EDGE_LEFT)
  3675. left = right - 200;
  3676. else
  3677. right = left + 200;
  3678. }
  3679. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  3680. bottom - top < 140) {
  3681. if (resize_edges & WM_EDGE_TOP)
  3682. top = bottom - 140;
  3683. else
  3684. bottom = top + 140;
  3685. }
  3686. if (left < 0)
  3687. left = 0;
  3688. if (top < WM_BAR_H)
  3689. top = WM_BAR_H;
  3690. nx = left;
  3691. ny = top;
  3692. nw = right - left;
  3693. nh = bottom - top;
  3694. if (nw > (int)fbw)
  3695. nw = (int)fbw;
  3696. if (nh > (int)fbh - WM_BAR_H)
  3697. nh = (int)fbh - WM_BAR_H;
  3698. if (nx == u->info.x && ny == u->info.y &&
  3699. nw == (int)u->info.w && nh == (int)u->info.h)
  3700. return;
  3701. now = u_ticks_ms();
  3702. if (!force &&
  3703. (unsigned int)(now - resize_last_ms) < WM_RESIZE_COMMIT_MS &&
  3704. iabs(nw - (int)u->info.w) < WM_RESIZE_MIN_STEP &&
  3705. iabs(nh - (int)u->info.h) < WM_RESIZE_MIN_STEP)
  3706. return;
  3707. resize_last_ms = now;
  3708. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  3709. (unsigned int)nh) == 0)
  3710. u->state = WIN_NORMAL;
  3711. }
  3712. /* Capture the pointer for an edge resize that started at (mx,my). */
  3713. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  3714. unsigned int my)
  3715. {
  3716. struct uwin *u = &wins[idx];
  3717. resize_id = (int)idx;
  3718. resize_edges = edges;
  3719. resize_mx = mx;
  3720. resize_my = my;
  3721. resize_ox = u->info.x;
  3722. resize_oy = u->info.y;
  3723. resize_ow = u->info.w;
  3724. resize_oh = u->info.h;
  3725. resize_last_ms = u_ticks_ms() - WM_RESIZE_COMMIT_MS;
  3726. u_wm_mouse_capture(1);
  3727. }
  3728. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  3729. * invisible resize border and while resizing, the move cross while a
  3730. * title-bar drag is under way, a hand over the start button, the open start
  3731. * menu, the status area, the dock or a window's close icon. Everywhere else
  3732. * the kernel picks the window's own content cursor (or the arrow). */
  3733. static void update_cursor(unsigned int mx, unsigned int my)
  3734. {
  3735. static int current = CURSOR_NO_OVERRIDE;
  3736. int want = CURSOR_NO_OVERRIDE;
  3737. if (resize_id >= 0) {
  3738. want = resize_cursor(resize_edges);
  3739. } else if (drag_id >= 0) {
  3740. if (drag_moved)
  3741. want = CURSOR_MOVE;
  3742. } else if (sm_open) {
  3743. /* The menu is modal: a hand over its items, the arrow
  3744. * elsewhere, never a window's content cursor underneath. */
  3745. int hit = sm_hit(mx, my);
  3746. want = (hit == SM_HIT_NONE || hit == SM_HIT_MENU) ?
  3747. CURSOR_DEFAULT : CURSOR_POINTER;
  3748. } else if (dock_pointer_inside(mx, my)) {
  3749. want = dock_icon_at(mx, my) >= 0 ?
  3750. CURSOR_POINTER : CURSOR_DEFAULT;
  3751. } else if (my < WM_BAR_H) {
  3752. if (bar_start_hit(mx, my) || bar_status_hit(mx, my))
  3753. want = CURSOR_POINTER;
  3754. } else {
  3755. int edges = 0;
  3756. int idx = resize_hit((int)mx, (int)my, &edges);
  3757. if (idx >= 0 && close_hit(&wins[idx].info, (int)mx, (int)my))
  3758. want = CURSOR_POINTER;
  3759. else if (idx >= 0 && edges)
  3760. want = resize_cursor(edges);
  3761. }
  3762. if (want != current && u_wm_set_cursor(want) == 0)
  3763. current = want;
  3764. }
  3765. static void handle_mouse(struct wm_status *st)
  3766. {
  3767. unsigned int buttons = st->mouse.buttons;
  3768. unsigned int mx = st->mouse.x, my = st->mouse.y;
  3769. int down = (buttons & 1) != 0;
  3770. int was = (prev_buttons & 1) != 0;
  3771. /* The app runner is modal: clicks cannot reach the start menu, the
  3772. * dock or a window below the overlay. Dismiss it with Win/Esc. */
  3773. if (run_open) {
  3774. prev_buttons = buttons;
  3775. return;
  3776. }
  3777. if (down && !was) {
  3778. int prev_title_id = last_title_id;
  3779. unsigned int now = u_ticks_ms();
  3780. int hit;
  3781. /* The start menu is modal: a press on an entry launches it,
  3782. * a press on Power opens its submenu, and any other press
  3783. * dismisses the menu without reaching the window below. */
  3784. if (sm_open) {
  3785. hit = sm_hit(mx, my);
  3786. last_title_id = -1;
  3787. if (hit == SM_HIT_START) {
  3788. sm_close();
  3789. } else if (hit == SM_HIT_POWER) {
  3790. sm_power_open = !sm_power_open;
  3791. full_redraw = 1;
  3792. } else if (hit == SM_HIT_REBOOT) {
  3793. sm_close();
  3794. if (u_spawn_kcmd("reboot", "", 0) < 0)
  3795. bar_notice("reboot failed");
  3796. } else if (hit == SM_HIT_POWEROFF) {
  3797. int pid;
  3798. sm_close();
  3799. pid = u_spawn_kcmd("poweroff", "", 0);
  3800. if (pid < 0) {
  3801. bar_notice("poweroff failed");
  3802. } else {
  3803. poweroff_pid = pid;
  3804. poweroff_deadline = u_ticks_ms() + 6000;
  3805. }
  3806. } else if (hit >= 0) {
  3807. sm_launch(hit);
  3808. } else if (hit != SM_HIT_MENU) {
  3809. sm_close();
  3810. }
  3811. goto mouse_done;
  3812. }
  3813. if (bar_start_hit(mx, my)) {
  3814. /* Opening the menu also closes an open control
  3815. * panel, so only one floating panel is visible. */
  3816. dismiss_popover_outside(mx, my);
  3817. last_title_id = -1;
  3818. sm_open = 1;
  3819. sm_power_open = 0;
  3820. sm_hot = -1;
  3821. sm_power_hot = 0;
  3822. full_redraw = 1;
  3823. goto mouse_done;
  3824. }
  3825. if (dock_click_at(mx, my)) {
  3826. last_title_id = -1;
  3827. goto mouse_done;
  3828. }
  3829. if (!bar_status_hit(mx, my))
  3830. dismiss_popover_outside(mx, my);
  3831. last_title_id = -1;
  3832. if (bar_status_hit(mx, my)) {
  3833. open_control_panel();
  3834. } else {
  3835. int edges = 0;
  3836. int idx = resize_hit((int)mx, (int)my, &edges);
  3837. if (idx >= 0) {
  3838. struct uwin *u = &wins[idx];
  3839. int pmx = (int)mx, pmy = (int)my;
  3840. u_wm_focus((unsigned int)idx);
  3841. if (u->info.popover) {
  3842. /* A popover has no title bar, close
  3843. * button or resize border; the app owns
  3844. * its content. */
  3845. } else if (close_hit(&u->info, pmx, pmy)) {
  3846. u_wm_close((unsigned int)idx);
  3847. full_redraw = 1;
  3848. } else if (edges) {
  3849. begin_resize((unsigned int)idx, edges,
  3850. mx, my);
  3851. } else if (pmy < u->info.y + WM_TITLE_H) {
  3852. if (prev_title_id == idx &&
  3853. now - last_title_ms <= WM_DBLCLICK_MS &&
  3854. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  3855. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  3856. last_title_ms = 0;
  3857. if (toggle_maximize(u,
  3858. (unsigned int)idx) != 0)
  3859. bar_notice("resize failed");
  3860. } else {
  3861. last_title_id = idx;
  3862. last_title_ms = now;
  3863. last_title_x = pmx;
  3864. last_title_y = pmy;
  3865. drag_id = idx;
  3866. drag_mx = pmx;
  3867. drag_my = pmy;
  3868. drag_dx = pmx - u->info.x;
  3869. drag_dy = pmy - u->info.y;
  3870. drag_moved = 0;
  3871. u_wm_mouse_capture(1);
  3872. }
  3873. }
  3874. }
  3875. }
  3876. } else if (down && was && resize_id >= 0) {
  3877. struct uwin *u = &wins[resize_id];
  3878. if (u->info.used)
  3879. resize_window_drag(u, (unsigned int)resize_id, mx, my, 0);
  3880. else {
  3881. resize_id = -1;
  3882. u_wm_mouse_capture(0);
  3883. }
  3884. } else if (down && was && drag_id >= 0) {
  3885. struct uwin *u = &wins[drag_id];
  3886. if (u->info.used) {
  3887. int ox, oy, nx, ny;
  3888. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  3889. iabs((int)my - drag_my) >= WM_DRAG_START)
  3890. drag_moved = 1;
  3891. /* A maximized or snapped window is restored as soon as
  3892. * the drag starts. Keep the grab point at the same
  3893. * fraction of the title bar, so the restored (narrower)
  3894. * window stays under the pointer. A click that does
  3895. * not move must leave it in place. */
  3896. if (u->state != WIN_NORMAL && drag_moved) {
  3897. unsigned int ow = u->info.w;
  3898. int gx = drag_mx - u->info.x;
  3899. int gy = drag_my - u->info.y;
  3900. if (restore_window(u,
  3901. (unsigned int)drag_id) == 0) {
  3902. if (ow)
  3903. gx = gx * (int)u->info.w /
  3904. (int)ow;
  3905. drag_dx = gx;
  3906. drag_dy = gy;
  3907. }
  3908. }
  3909. if (u->state == WIN_NORMAL) {
  3910. ox = u->info.x;
  3911. oy = u->info.y;
  3912. nx = (int)mx - drag_dx;
  3913. ny = (int)my - drag_dy;
  3914. if (prefs.resist)
  3915. resist_geom(u, drag_id, &nx, &ny);
  3916. clamp_geom(u, &nx, &ny);
  3917. if (nx != ox || ny != oy) {
  3918. struct wm_present_info present;
  3919. unsigned int i;
  3920. u_wm_set_geom((unsigned int)drag_id,
  3921. nx, ny);
  3922. u->info.x = nx;
  3923. u->info.y = ny;
  3924. if (u->info.focused) {
  3925. move_window_pixels(u, ox, oy,
  3926. nx, ny);
  3927. } else {
  3928. present.count = 0;
  3929. present.immediate = 1;
  3930. add_rect(&present, ox, oy,
  3931. u->info.w, u->info.h);
  3932. add_rect(&present, nx, ny,
  3933. u->info.w, u->info.h);
  3934. if (u_wm_begin() == 0) {
  3935. remap_window_contents();
  3936. for (i = 0;
  3937. i < present.count; i++)
  3938. draw_rect(
  3939. present.rect[i].x,
  3940. present.rect[i].y,
  3941. present.rect[i].w,
  3942. present.rect[i].h);
  3943. wm_overlays(&present);
  3944. u_wm_present(&present);
  3945. }
  3946. }
  3947. }
  3948. }
  3949. } else {
  3950. drag_id = -1;
  3951. u_wm_mouse_capture(0);
  3952. }
  3953. } else if (!down && was) {
  3954. /* A throttled resize may still be behind the last pointer
  3955. * position; commit the final geometry before dropping it. */
  3956. if (resize_id >= 0 && wins[resize_id].info.used)
  3957. resize_window_drag(&wins[resize_id],
  3958. (unsigned int)resize_id, mx, my, 1);
  3959. /* Dropping a window with the pointer on the left/right screen
  3960. * edge snaps it to that half; on the top edge it maximizes.
  3961. * The Settings app can turn edge snapping off, leaving the
  3962. * double-click maximize gesture alone. */
  3963. if (prefs.snap && drag_id >= 0 && drag_moved &&
  3964. wins[drag_id].info.used &&
  3965. wins[drag_id].state == WIN_NORMAL) {
  3966. struct uwin *u = &wins[drag_id];
  3967. int st = WIN_NORMAL;
  3968. if ((int)mx < WM_SNAP_EDGE)
  3969. st = WIN_SNAP_LEFT;
  3970. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  3971. st = WIN_SNAP_RIGHT;
  3972. else if ((int)my < WM_SNAP_EDGE)
  3973. st = WIN_MAXIMIZED;
  3974. if (st != WIN_NORMAL &&
  3975. place_window(u, (unsigned int)drag_id, st) != 0)
  3976. bar_notice("resize failed");
  3977. }
  3978. drag_id = -1;
  3979. resize_id = -1;
  3980. u_wm_mouse_capture(0);
  3981. }
  3982. mouse_done:
  3983. dock_capture_update(mx, my, buttons);
  3984. prev_buttons = buttons;
  3985. update_cursor(mx, my);
  3986. }
  3987. static char *put_str(char *p, const char *s)
  3988. {
  3989. while (*s)
  3990. *p++ = *s++;
  3991. return p;
  3992. }
  3993. static char *put_dec(char *p, unsigned int v)
  3994. {
  3995. char b[10];
  3996. unsigned int n = 0;
  3997. do {
  3998. b[n++] = (char)('0' + v % 10);
  3999. v /= 10;
  4000. } while (v);
  4001. while (n)
  4002. *p++ = b[--n];
  4003. return p;
  4004. }
  4005. /* Expose the last interval in /share so the target can be measured without
  4006. * a serial console. */
  4007. static void write_stats(void)
  4008. {
  4009. char b[80], *p = b;
  4010. unsigned int now = u_ticks_ms();
  4011. p = put_str(p, "t=");
  4012. p = put_dec(p, now / 1000);
  4013. p = put_str(p, "s frames=");
  4014. p = put_dec(p, stat_frames);
  4015. p = put_str(p, " ms=");
  4016. p = put_dec(p, stat_ms);
  4017. p = put_str(p, " max=");
  4018. p = put_dec(p, stat_max);
  4019. p = put_str(p, " px=");
  4020. p = put_dec(p, stat_px);
  4021. p = put_str(p, "\n");
  4022. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  4023. (int)(p - b));
  4024. }
  4025. void _start(const char *arg)
  4026. {
  4027. struct wm_screen_info si;
  4028. struct wm_status st;
  4029. unsigned int i;
  4030. (void)arg;
  4031. if (u_wm_attach(&si) != 0)
  4032. u_exit(1);
  4033. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  4034. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  4035. fbw = si.width;
  4036. fbh = si.height;
  4037. wall_load();
  4038. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  4039. wins[i].info.used = 0;
  4040. wins[i].mapped = 0;
  4041. }
  4042. full_redraw = 1;
  4043. for (;;) {
  4044. unsigned int t0, t1;
  4045. if (u_wm_poll(&st) != 0)
  4046. u_exit(1);
  4047. if (!prefs_seen_valid || st.prefs_seq != prefs_seq_seen) {
  4048. unsigned int old_bg = desktop_rgb;
  4049. prefs = st.prefs;
  4050. prefs_seq_seen = st.prefs_seq;
  4051. prefs_seen_valid = 1;
  4052. apply_prefs();
  4053. if (old_bg != desktop_rgb)
  4054. full_redraw = 1;
  4055. }
  4056. if (!settings_req_seen_valid) {
  4057. settings_req_seen = st.settings_req_seq;
  4058. settings_req_seen_valid = 1;
  4059. } else if (st.settings_req_seq != settings_req_seen) {
  4060. settings_req_seen = st.settings_req_seq;
  4061. open_settings(st.settings_req_section);
  4062. }
  4063. /* A poweroff that could not cut the power returns instead of
  4064. * halting; tell the user rather than leaving a dead menu. */
  4065. if (poweroff_pid > 0 &&
  4066. (int)(u_ticks_ms() - poweroff_deadline) >= 0) {
  4067. bar_notice("poweroff failed");
  4068. poweroff_pid = 0;
  4069. }
  4070. if (st.wallpaper_seq != wall_seq_seen) {
  4071. wall_seq_seen = st.wallpaper_seq;
  4072. wall_load();
  4073. full_redraw = 1;
  4074. }
  4075. /* The WM starts before the storage scan, so the first load may not
  4076. * see /data yet. Reload when boot_ready says the mounts are up,
  4077. * and retry for ten seconds for the deferred USB /data volume. */
  4078. if (st.boot_ready && !wall_boot_seen) {
  4079. wall_boot_seen = 1;
  4080. wall_retries = 0;
  4081. wall_retry_ms = u_ticks_ms() + 1000;
  4082. wall_load();
  4083. full_redraw = 1;
  4084. } else if (wall_boot_seen && !wall_ready && wall_retries < 10 &&
  4085. (int)(u_ticks_ms() - wall_retry_ms) >= 0) {
  4086. wall_retries++;
  4087. wall_retry_ms += 1000;
  4088. wall_load();
  4089. if (wall_ready)
  4090. full_redraw = 1;
  4091. }
  4092. /* Preferences live on /data like the wallpaper, so the same
  4093. * boot-order retry applies. A missing file keeps the kernel's
  4094. * defaults (and any values a previous WM set). */
  4095. if (st.boot_ready && !prefs_boot_seen) {
  4096. prefs_boot_seen = 1;
  4097. prefs_retries = 0;
  4098. prefs_retry_ms = u_ticks_ms() + 1000;
  4099. if (prefs_load() == 0)
  4100. prefs_loaded = 1;
  4101. } else if (prefs_boot_seen && !prefs_loaded &&
  4102. prefs_retries < 10 &&
  4103. (int)(u_ticks_ms() - prefs_retry_ms) >= 0) {
  4104. prefs_retries++;
  4105. prefs_retry_ms += 1000;
  4106. if (prefs_load() == 0)
  4107. prefs_loaded = 1;
  4108. }
  4109. if (st.full_redraw)
  4110. full_redraw = 1;
  4111. usurf_pixels = 0;
  4112. t0 = u_ticks_ms();
  4113. handle_mouse(&st);
  4114. do_update(&st);
  4115. t1 = u_ticks_ms();
  4116. if (usurf_pixels) {
  4117. unsigned int dt = t1 - t0;
  4118. stat_frames++;
  4119. stat_ms += dt;
  4120. stat_px += usurf_pixels;
  4121. if (dt > stat_max)
  4122. stat_max = dt;
  4123. }
  4124. if (t1 - stat_last >= 2000) {
  4125. stat_last = t1;
  4126. if (stat_frames) {
  4127. write_stats();
  4128. stat_frames = stat_ms = stat_px = stat_max = 0;
  4129. }
  4130. }
  4131. u_sleep_ms(10);
  4132. }
  4133. }