ucmd_wm.c 104 KB

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