ucmd_wm.c 94 KB

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