ucmd_wm.c 99 KB

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