ucmd_wm.c 109 KB

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