ucmd_wm.c 99 KB

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