ucmd_wm.c 91 KB

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