ucmd_wm.c 90 KB

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