compositor.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K. Lange
  5. *
  6. * Yutani - The ToaruOS Compositor.
  7. *
  8. * Yutani is a canvas-based window compositor and manager.
  9. * It employs shared memory to provide clients access to
  10. * canvases in which they may render, while using a packet-based
  11. * socket interface to communicate actions between the server
  12. * and client such as keyboard activity, mouse movement, responses
  13. * to client events, etc., as well as to communicate requests from
  14. * the client to the server, such as creation of new windows,
  15. * movement, resizing, and display updates.
  16. */
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <syscall.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <signal.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <time.h>
  26. #include <math.h>
  27. #include <assert.h>
  28. #include <getopt.h>
  29. #include <errno.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <sys/fswait.h>
  33. #include <pthread.h>
  34. #include <toaru/graphics.h>
  35. #include <toaru/mouse.h>
  36. #include <toaru/kbd.h>
  37. #include <toaru/pex.h>
  38. #include <toaru/yutani.h>
  39. #include <toaru/yutani-internal.h>
  40. #include <toaru/hashmap.h>
  41. #include <toaru/list.h>
  42. #include <toaru/spinlock.h>
  43. //#define _DEBUG_YUTANI
  44. #ifdef _DEBUG_YUTANI
  45. #include <toaru/trace.h>
  46. #define TRACE_APP_NAME "yutani"
  47. #else
  48. #define TRACE(msg,...)
  49. #endif
  50. /* Mouse resolution scaling */
  51. #define MOUSE_SCALE 3
  52. #define YUTANI_INCOMING_MOUSE_SCALE * 3
  53. /* Mouse cursor hotspot */
  54. #define MOUSE_OFFSET_X 26
  55. #define MOUSE_OFFSET_Y 26
  56. /* Mouse cursor size */
  57. #define MOUSE_WIDTH 64
  58. #define MOUSE_HEIGHT 64
  59. /* How much the mouse needs to move to break off a tiled window */
  60. #define UNTILE_SENSITIVITY (MOUSE_SCALE * 5)
  61. /* Screenshot modes */
  62. #define YUTANI_SCREENSHOT_FULL 1
  63. #define YUTANI_SCREENSHOT_WINDOW 2
  64. /*
  65. * Animation effect types.
  66. * XXX: Should this be in the client library?
  67. */
  68. typedef enum {
  69. YUTANI_EFFECT_NONE,
  70. /* Basic animations */
  71. YUTANI_EFFECT_FADE_IN,
  72. YUTANI_EFFECT_FADE_OUT,
  73. /* XXX: Are these used? */
  74. YUTANI_EFFECT_MINIMIZE,
  75. YUTANI_EFFECT_UNMINIMIZE,
  76. } yutani_effect;
  77. /* Animation lengths */
  78. static int yutani_animation_lengths[] = {
  79. 0, /* None */
  80. 200, /* Fade In */
  81. 200, /* Fade Out */
  82. 0, /* Minimize */
  83. 0, /* Unminimized */
  84. };
  85. /* Debug Options */
  86. #define YUTANI_DEBUG_WINDOW_BOUNDS 1
  87. #define YUTANI_DEBUG_WINDOW_SHAPES 1
  88. /* Command line flag values */
  89. struct {
  90. int nested;
  91. int nest_width;
  92. int nest_height;
  93. } yutani_options = {
  94. .nested = 0,
  95. .nest_width = 640,
  96. .nest_height = 480,
  97. };
  98. /*
  99. * Server window definitions
  100. */
  101. typedef struct YutaniServerWindow {
  102. /* Window identifier number */
  103. yutani_wid_t wid;
  104. /* Window location */
  105. signed long x;
  106. signed long y;
  107. /* Stack order */
  108. unsigned short z;
  109. /* Window size */
  110. int32_t width;
  111. int32_t height;
  112. /* Canvas buffer */
  113. uint8_t * buffer;
  114. uint32_t bufid;
  115. uint32_t newbufid;
  116. uint8_t * newbuffer;
  117. /* Connection that owns this window */
  118. uint32_t owner;
  119. /* Rotation of windows XXX */
  120. int16_t rotation;
  121. /* Client advertisements */
  122. uint32_t client_flags;
  123. uint16_t client_offsets[5];
  124. uint32_t client_length;
  125. char * client_strings;
  126. /* Window animations */
  127. int anim_mode;
  128. uint32_t anim_start;
  129. /* Alpha shaping threshold */
  130. int alpha_threshold;
  131. /*
  132. * Mouse cursor selection
  133. * Originally, this specified whether the mouse was
  134. * hidden, but it plays double duty since client
  135. * control over mouse cursors was added.
  136. */
  137. int show_mouse;
  138. int default_mouse;
  139. /* Tiling / untiling information */
  140. int tiled;
  141. int32_t untiled_width;
  142. int32_t untiled_height;
  143. int32_t untiled_left;
  144. int32_t untiled_top;
  145. /* Client-configurable server behavior flags */
  146. uint32_t server_flags;
  147. /* Window opacity */
  148. int opacity;
  149. } yutani_server_window_t;
  150. typedef struct YutaniGlobals {
  151. /* Display resolution */
  152. unsigned int width;
  153. unsigned int height;
  154. uint32_t stride;
  155. /* TODO: What about multiple screens?
  156. *
  157. * Obviously this is the whole canvas size,
  158. * but we need to be able to track different
  159. * monitors if/when we ever get support for that.
  160. */
  161. /* Core graphics context */
  162. void * backend_framebuffer;
  163. gfx_context_t * backend_ctx;
  164. /* Mouse location */
  165. signed int mouse_x;
  166. signed int mouse_y;
  167. /*
  168. * Previous mouse location, so that events can have
  169. * both the new and old mouse location together
  170. */
  171. signed int last_mouse_x;
  172. signed int last_mouse_y;
  173. /* List of all windows */
  174. list_t * windows;
  175. /* Hash of window IDs to their objects */
  176. hashmap_t * wids_to_windows;
  177. /*
  178. * Window stacking information
  179. * TODO: Support multiple top and bottom windows.
  180. */
  181. yutani_server_window_t * bottom_z;
  182. list_t * mid_zs;
  183. yutani_server_window_t * top_z;
  184. /* Damage region list */
  185. list_t * update_list;
  186. volatile int update_list_lock;
  187. /* Mouse cursors */
  188. sprite_t mouse_sprite;
  189. sprite_t mouse_sprite_drag;
  190. sprite_t mouse_sprite_resize_v;
  191. sprite_t mouse_sprite_resize_h;
  192. sprite_t mouse_sprite_resize_da;
  193. sprite_t mouse_sprite_resize_db;
  194. int current_cursor;
  195. /* Server backend communication identifier */
  196. char * server_ident;
  197. FILE * server;
  198. /* Pointer to focused window */
  199. yutani_server_window_t * focused_window;
  200. /* Mouse movement state */
  201. int mouse_state;
  202. /* Pointer to window being manipulated by mouse actions */
  203. yutani_server_window_t * mouse_window;
  204. /* Buffered information on mouse-moved window */
  205. int mouse_win_x;
  206. int mouse_win_y;
  207. int mouse_init_x;
  208. int mouse_init_y;
  209. int mouse_init_r;
  210. int32_t mouse_click_x_orig;
  211. int32_t mouse_click_y_orig;
  212. int mouse_drag_button;
  213. int mouse_moved;
  214. int32_t mouse_click_x;
  215. int32_t mouse_click_y;
  216. /* Keyboard library state machine state */
  217. key_event_state_t kbd_state;
  218. /* Pointer to window being resized */
  219. yutani_server_window_t * resizing_window;
  220. int32_t resizing_w;
  221. int32_t resizing_h;
  222. yutani_scale_direction_t resizing_direction;
  223. int32_t resizing_offset_x;
  224. int32_t resizing_offset_y;
  225. int resizing_button;
  226. /* List of clients subscribing to window information events */
  227. list_t * window_subscribers;
  228. /* When the server started, used for timing functions */
  229. time_t start_time;
  230. suseconds_t start_subtime;
  231. /* Basic lock to prevent redraw thread and communication thread interference */
  232. volatile int redraw_lock;
  233. /* Pointer to last hovered window to allow exit events */
  234. yutani_server_window_t * old_hover_window;
  235. /* Key bindigns */
  236. hashmap_t * key_binds;
  237. /* Windows to remove after the end of the rendering pass */
  238. list_t * windows_to_remove;
  239. /* For nested mode, the host Yutani context and window */
  240. yutani_t * host_context;
  241. yutani_window_t * host_window;
  242. /* Map of clients to their windows */
  243. hashmap_t * clients_to_windows;
  244. /* Toggles for debugging window locations */
  245. int debug_bounds;
  246. int debug_shapes;
  247. /* If the next rendered frame should be saved as a screenshot */
  248. int screenshot_frame;
  249. /* Next frame should resize host context */
  250. int resize_on_next;
  251. /* Last mouse buttons - used for some specialized mouse drivers */
  252. uint32_t last_mouse_buttons;
  253. /* Clipboard buffer */
  254. char clipboard[512];
  255. int clipboard_size;
  256. /* VirtualBox Seamless mode support information */
  257. int vbox_rects;
  258. int vbox_pointer;
  259. } yutani_globals_t;
  260. struct key_bind {
  261. unsigned int owner;
  262. int response;
  263. };
  264. /* Early definitions */
  265. static void mark_window(yutani_globals_t * yg, yutani_server_window_t * window);
  266. static void window_actually_close(yutani_globals_t * yg, yutani_server_window_t * w);
  267. static void notify_subscribers(yutani_globals_t * yg);
  268. /**
  269. * Print usage information.
  270. */
  271. static int usage(char * argv[]) {
  272. fprintf(stderr,
  273. "Yutani - Window Compositor\n"
  274. "\n"
  275. "usage: %s [-n [-g WxH]] [-h]\n"
  276. "\n"
  277. " -n --nested \033[3mRun in a window.\033[0m\n"
  278. " -h --help \033[3mShow this help message.\033[0m\n"
  279. " -g --geometry \033[3mSet the size of the server framebuffer.\033[0m\n"
  280. "\n"
  281. " Yutani is the standard system compositor.\n"
  282. "\n",
  283. argv[0]);
  284. return 1;
  285. }
  286. /**
  287. * Parse arguments
  288. */
  289. static int parse_args(int argc, char * argv[], int * out) {
  290. static struct option long_opts[] = {
  291. {"nested", no_argument, 0, 'n'},
  292. {"geometry", required_argument, 0, 'g'},
  293. {"help", no_argument, 0, 'h'},
  294. {0,0,0,0}
  295. };
  296. int index, c;
  297. while ((c = getopt_long(argc, argv, "hg:n", long_opts, &index)) != -1) {
  298. if (!c) {
  299. if (long_opts[index].flag == 0) {
  300. c = long_opts[index].val;
  301. }
  302. }
  303. switch (c) {
  304. case 'h':
  305. return usage(argv);
  306. case 'n':
  307. yutani_options.nested = 1;
  308. break;
  309. case 'g':
  310. {
  311. char * c = strstr(optarg, "x");
  312. if (c) {
  313. *c = '\0';
  314. c++;
  315. yutani_options.nest_width = atoi(optarg);
  316. yutani_options.nest_height = atoi(c);
  317. }
  318. }
  319. break;
  320. default:
  321. fprintf(stderr, "Unrecognized option: %c\n", c);
  322. break;
  323. }
  324. }
  325. *out = optind;
  326. return 0;
  327. }
  328. int32_t min(int32_t a, int32_t b) {
  329. return (a < b) ? a : b;
  330. }
  331. int32_t max(int32_t a, int32_t b) {
  332. return (a > b) ? a : b;
  333. }
  334. static int next_buf_id(void) {
  335. static int _next = 1;
  336. return _next++;
  337. }
  338. static int next_wid(void) {
  339. static int _next = 1;
  340. return _next++;
  341. }
  342. static uint32_t yutani_current_time(yutani_globals_t * yg) {
  343. struct timeval t;
  344. gettimeofday(&t, NULL);
  345. time_t sec_diff = t.tv_sec - yg->start_time;
  346. suseconds_t usec_diff = t.tv_usec - yg->start_subtime;
  347. if (t.tv_usec < yg->start_subtime) {
  348. sec_diff -= 1;
  349. usec_diff = (1000000 + t.tv_usec) - yg->start_subtime;
  350. }
  351. return (uint32_t)(sec_diff * 1000 + usec_diff / 1000);
  352. }
  353. static uint32_t yutani_time_since(yutani_globals_t * yg, uint32_t start_time) {
  354. uint32_t now = yutani_current_time(yg);
  355. uint32_t diff = now - start_time; /* Milliseconds */
  356. return diff;
  357. }
  358. /**
  359. * Translate and transform coordinate from screen-relative to window-relative.
  360. */
  361. static void device_to_window(yutani_server_window_t * window, int32_t x, int32_t y, int32_t * out_x, int32_t * out_y) {
  362. if (!window) {
  363. *out_x = 0;
  364. *out_y = 0;
  365. return;
  366. }
  367. *out_x = x - window->x;
  368. *out_y = y - window->y;
  369. if (!window->rotation) return;
  370. #if 0
  371. double t_x = *out_x - (window->width / 2);
  372. double t_y = *out_y - (window->height / 2);
  373. double s = __builtin_sin(-M_PI * (window->rotation/ 180.0));
  374. double c = __builtin_cos(-M_PI * (window->rotation/ 180.0));
  375. double n_x = t_x * c - t_y * s;
  376. double n_y = t_x * s + t_y * c;
  377. *out_x = (int32_t)n_x + (window->width / 2);
  378. *out_y = (int32_t)n_y + (window->height / 2);
  379. #endif
  380. }
  381. /**
  382. * Translate and transform coordinate from window-relative to screen-relative.
  383. */
  384. static void window_to_device(yutani_server_window_t * window, int32_t x, int32_t y, int32_t * out_x, int32_t * out_y) {
  385. #if 0
  386. if (!window->rotation) {
  387. #endif
  388. *out_x = window->x + x;
  389. *out_y = window->y + y;
  390. #if 0
  391. return;
  392. }
  393. double t_x = x - (window->width / 2);
  394. double t_y = y - (window->height / 2);
  395. double s = __builtin_sin(M_PI * (window->rotation/ 180.0));
  396. double c = __builtin_cos(M_PI * (window->rotation/ 180.0));
  397. double n_x = t_x * c - t_y * s;
  398. double n_y = t_x * s + t_y * c;
  399. *out_x = (int32_t)n_x + (window->width / 2) + window->x;
  400. *out_y = (int32_t)n_y + (window->height / 2) + window->y;
  401. #endif
  402. }
  403. /**
  404. * Remove a window from the z stack.
  405. */
  406. static void unorder_window(yutani_globals_t * yg, yutani_server_window_t * w) {
  407. unsigned short index = w->z;
  408. w->z = -1;
  409. if (index == YUTANI_ZORDER_BOTTOM) {
  410. yg->bottom_z = NULL;
  411. return;
  412. }
  413. if (index == YUTANI_ZORDER_TOP) {
  414. yg->top_z = NULL;
  415. return;
  416. }
  417. node_t * n = list_find(yg->mid_zs, w);
  418. if (!n) return;
  419. list_delete(yg->mid_zs, n);
  420. free(n);
  421. }
  422. /**
  423. * Move a window to a new stack order.
  424. */
  425. static void reorder_window(yutani_globals_t * yg, yutani_server_window_t * window, uint16_t new_zed) {
  426. if (!window) {
  427. return;
  428. }
  429. spin_lock(&yg->redraw_lock);
  430. unorder_window(yg, window);
  431. spin_unlock(&yg->redraw_lock);
  432. window->z = new_zed;
  433. if (new_zed != YUTANI_ZORDER_TOP && new_zed != YUTANI_ZORDER_BOTTOM) {
  434. spin_lock(&yg->redraw_lock);
  435. list_insert(yg->mid_zs, window);
  436. spin_unlock(&yg->redraw_lock);
  437. return;
  438. }
  439. if (new_zed == YUTANI_ZORDER_TOP) {
  440. if (yg->top_z) {
  441. spin_lock(&yg->redraw_lock);
  442. unorder_window(yg, yg->top_z);
  443. spin_unlock(&yg->redraw_lock);
  444. }
  445. yg->top_z = window;
  446. return;
  447. }
  448. if (new_zed == YUTANI_ZORDER_BOTTOM) {
  449. if (yg->bottom_z) {
  450. spin_lock(&yg->redraw_lock);
  451. unorder_window(yg, yg->bottom_z);
  452. spin_unlock(&yg->redraw_lock);
  453. }
  454. yg->bottom_z = window;
  455. return;
  456. }
  457. }
  458. /**
  459. * Move a window to the top of the basic z stack, if valid.
  460. */
  461. static void make_top(yutani_globals_t * yg, yutani_server_window_t * w) {
  462. unsigned short index = w->z;
  463. if (index == YUTANI_ZORDER_BOTTOM) return;
  464. if (index == YUTANI_ZORDER_TOP) return;
  465. node_t * n = list_find(yg->mid_zs, w);
  466. if (!n) return; /* wat */
  467. list_delete(yg->mid_zs, n);
  468. list_append(yg->mid_zs, n);
  469. }
  470. /**
  471. * Set a window as the focused window.
  472. *
  473. * Currently, we only support one focused window.
  474. * In the future, we should support multiple windows as "focused" to account
  475. * for multiple "seats" on a single display.
  476. */
  477. static void set_focused_window(yutani_globals_t * yg, yutani_server_window_t * w) {
  478. if (w == yg->focused_window) {
  479. return; /* Already focused */
  480. }
  481. if (yg->focused_window) {
  482. /* Send focus change to old focused window */
  483. yutani_msg_buildx_window_focus_change_alloc(response);
  484. yutani_msg_buildx_window_focus_change(response, yg->focused_window->wid, 0);
  485. pex_send(yg->server, yg->focused_window->owner, response->size, (char *)response);
  486. }
  487. yg->focused_window = w;
  488. if (w) {
  489. /* Send focus change to new focused window */
  490. yutani_msg_buildx_window_focus_change_alloc(response);
  491. yutani_msg_buildx_window_focus_change(response, w->wid, 1);
  492. pex_send(yg->server, w->owner, response->size, (char *)response);
  493. make_top(yg, w);
  494. mark_window(yg, w);
  495. } else {
  496. /*
  497. * There is no window to focus (we're unsetting focus);
  498. * default to the bottom window (background)
  499. */
  500. yg->focused_window = yg->bottom_z;
  501. }
  502. /* Notify all subscribers of window changes */
  503. notify_subscribers(yg);
  504. }
  505. /**
  506. * Get the focused window.
  507. *
  508. * In case there is no focused window, we return the bottom window.
  509. */
  510. static yutani_server_window_t * get_focused(yutani_globals_t * yg) {
  511. if (yg->focused_window) return yg->focused_window;
  512. return yg->bottom_z;
  513. }
  514. /**
  515. * Create a server window object.
  516. *
  517. * Initializes a window of the particular size for a given client.
  518. */
  519. static yutani_server_window_t * server_window_create(yutani_globals_t * yg, int width, int height, uint32_t owner, uint32_t flags) {
  520. yutani_server_window_t * win = malloc(sizeof(yutani_server_window_t));
  521. win->wid = next_wid();
  522. win->owner = owner;
  523. list_insert(yg->windows, win);
  524. hashmap_set(yg->wids_to_windows, (void*)win->wid, win);
  525. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)owner);
  526. list_insert(client_list, win);
  527. win->x = 0;
  528. win->y = 0;
  529. win->z = 1;
  530. win->width = width;
  531. win->height = height;
  532. win->bufid = next_buf_id();
  533. win->rotation = 0;
  534. win->newbufid = 0;
  535. win->client_flags = 0;
  536. win->client_offsets[0] = 0;
  537. win->client_offsets[1] = 0;
  538. win->client_offsets[2] = 0;
  539. win->client_offsets[3] = 0;
  540. win->client_offsets[4] = 0;
  541. win->client_length = 0;
  542. win->client_strings = NULL;
  543. win->anim_mode = YUTANI_EFFECT_FADE_IN;
  544. win->anim_start = yutani_current_time(yg);
  545. win->alpha_threshold = 0;
  546. win->show_mouse = 1;
  547. win->tiled = 0;
  548. win->untiled_width = 0;
  549. win->untiled_height = 0;
  550. win->default_mouse = 1;
  551. win->server_flags = flags;
  552. win->opacity = 255;
  553. char key[1024];
  554. YUTANI_SHMKEY(yg->server_ident, key, 1024, win);
  555. size_t size = (width * height * 4);
  556. win->buffer = (uint8_t *)syscall_shm_obtain(key, &size);
  557. memset(win->buffer, 0, size);
  558. list_insert(yg->mid_zs, win);
  559. return win;
  560. }
  561. /**
  562. * Update the shape threshold for a window.
  563. *
  564. * A shaping threshold is a byte representing the minimum
  565. * required alpha for a window to be considered "solid".
  566. * Eg., a value of 0 says all windows are solid, while a value of
  567. * 1 requires a window to have at least some opacity to it,
  568. * and a value of 255 requires fully opaque pixels.
  569. *
  570. * Not actually stored as a byte, so a value over 255 can be used.
  571. * This results in a window that passes through all clicks.
  572. */
  573. static void server_window_update_shape(yutani_globals_t * yg, yutani_server_window_t * window, int set) {
  574. window->alpha_threshold = set;
  575. }
  576. /**
  577. * Start resizing a window.
  578. *
  579. * Resizing a multi-stage process.
  580. * The client and server agree on a size and the server prepares a buffer.
  581. * The client then needs to accept the resize, fill the buffer, and then
  582. * inform the server that it is ready, at which point we'll swap the
  583. * buffer we are rendering from.
  584. */
  585. static uint32_t server_window_resize(yutani_globals_t * yg, yutani_server_window_t * win, int width, int height) {
  586. /* A client has accepted our offer, let's make a buffer for them */
  587. if (win->newbufid) {
  588. /* Already in the middle of an accept/done, bail */
  589. return win->newbufid;
  590. }
  591. win->newbufid = next_buf_id();
  592. {
  593. char key[1024];
  594. YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, win->newbufid);
  595. size_t size = (width * height * 4);
  596. win->newbuffer = (uint8_t *)syscall_shm_obtain(key, &size);
  597. }
  598. return win->newbufid;
  599. }
  600. /**
  601. * Finish the resize process.
  602. *
  603. * We delete the unlink the old buffer and then swap the pointers
  604. * for the new buffer.
  605. */
  606. static void server_window_resize_finish(yutani_globals_t * yg, yutani_server_window_t * win, int width, int height) {
  607. if (!win->newbufid) {
  608. return;
  609. }
  610. int oldbufid = win->bufid;
  611. mark_window(yg, win);
  612. spin_lock(&yg->redraw_lock);
  613. win->width = width;
  614. win->height = height;
  615. win->bufid = win->newbufid;
  616. win->buffer = win->newbuffer;
  617. win->newbuffer = NULL;
  618. win->newbufid = 0;
  619. {
  620. char key[1024];
  621. YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, oldbufid);
  622. syscall_shm_release(key);
  623. }
  624. spin_unlock(&yg->redraw_lock);
  625. mark_window(yg, win);
  626. }
  627. /**
  628. * Add a clip region from a rectangle.
  629. */
  630. static void yutani_add_clip(yutani_globals_t * yg, double x, double y, double w, double h) {
  631. gfx_add_clip(yg->backend_ctx, (int)x, (int)y, (int)w, (int)h);
  632. }
  633. typedef struct {
  634. int32_t start;
  635. int32_t end;
  636. void * next;
  637. } _clip_t;
  638. /**
  639. * Mark a screen region as damaged.
  640. */
  641. static void mark_screen(yutani_globals_t * yg, int32_t x, int32_t y, int32_t width, int32_t height) {
  642. yutani_damage_rect_t * rect = malloc(sizeof(yutani_damage_rect_t));
  643. rect->x = x;
  644. rect->y = y;
  645. rect->width = width;
  646. rect->height = height;
  647. spin_lock(&yg->update_list_lock);
  648. list_insert(yg->update_list, rect);
  649. spin_unlock(&yg->update_list_lock);
  650. }
  651. /**
  652. * Draw the cursor sprite.
  653. */
  654. static void draw_cursor(yutani_globals_t * yg, int x, int y, int cursor) {
  655. sprite_t * sprite = &yg->mouse_sprite;
  656. static sprite_t * previous = NULL;
  657. if (yg->resizing_window) {
  658. switch (yg->resizing_direction) {
  659. case SCALE_UP:
  660. case SCALE_DOWN:
  661. sprite = &yg->mouse_sprite_resize_v;
  662. break;
  663. case SCALE_LEFT:
  664. case SCALE_RIGHT:
  665. sprite = &yg->mouse_sprite_resize_h;
  666. break;
  667. case SCALE_DOWN_RIGHT:
  668. case SCALE_UP_LEFT:
  669. sprite = &yg->mouse_sprite_resize_da;
  670. break;
  671. case SCALE_DOWN_LEFT:
  672. case SCALE_UP_RIGHT:
  673. sprite = &yg->mouse_sprite_resize_db;
  674. break;
  675. default:
  676. break;
  677. }
  678. } else if (yg->mouse_state == YUTANI_MOUSE_STATE_MOVING) {
  679. sprite = &yg->mouse_sprite_drag;
  680. } else {
  681. switch (cursor) {
  682. case YUTANI_CURSOR_TYPE_DRAG: sprite = &yg->mouse_sprite_drag; break;
  683. case YUTANI_CURSOR_TYPE_RESIZE_VERTICAL: sprite = &yg->mouse_sprite_resize_v; break;
  684. case YUTANI_CURSOR_TYPE_RESIZE_HORIZONTAL: sprite = &yg->mouse_sprite_resize_h; break;
  685. case YUTANI_CURSOR_TYPE_RESIZE_UP_DOWN: sprite = &yg->mouse_sprite_resize_da; break;
  686. case YUTANI_CURSOR_TYPE_RESIZE_DOWN_UP: sprite = &yg->mouse_sprite_resize_db; break;
  687. }
  688. }
  689. if (sprite != previous) {
  690. mark_screen(yg, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  691. previous = sprite;
  692. }
  693. if (yg->vbox_pointer > 0) {
  694. if (write(yg->vbox_pointer, sprite->bitmap, 48*48*4) > 0) {
  695. /* if that was successful, we don't need to draw the cursor */
  696. return;
  697. }
  698. }
  699. draw_sprite(yg->backend_ctx, sprite, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y);
  700. }
  701. /**
  702. * Determine if a window has a solid pixel at a given screen-space coordinate.
  703. *
  704. * This is where we evaluate alpha thresholds. We only do this underneath
  705. * the cursor, and only when we move the cursor. It's reasonably fast
  706. * in those circumstances, but shouldn't be used for large regions. We
  707. * do have one debug method that indicates the top window in a box
  708. * around the cursor, but it is relatively slow.
  709. */
  710. yutani_server_window_t * check_top_at(yutani_globals_t * yg, yutani_server_window_t * w, uint16_t x, uint16_t y){
  711. if (!w) return NULL;
  712. int32_t _x = -1, _y = -1;
  713. device_to_window(w, x, y, &_x, &_y);
  714. if (_x < 0 || _x >= w->width || _y < 0 || _y >= w->height) return NULL;
  715. uint32_t c = ((uint32_t *)w->buffer)[(w->width * _y + _x)];
  716. uint8_t a = _ALP(c);
  717. if (a >= w->alpha_threshold) {
  718. return w;
  719. }
  720. return NULL;
  721. }
  722. /**
  723. * Find the window that is at the top at a particular screen-space coordinate.
  724. *
  725. * This walks through each window from top to bottom (foreachr - reverse foreach)
  726. * until it finds one with a pixel at this coordinate. Again, we only call this
  727. * at the cursor coordinates, and it is not particularly fast, so don't use it
  728. * anywhere that needs to hit a lot of coordinates.
  729. */
  730. yutani_server_window_t * top_at(yutani_globals_t * yg, uint16_t x, uint16_t y) {
  731. if (check_top_at(yg, yg->top_z, x, y)) return yg->top_z;
  732. foreachr(node, yg->mid_zs) {
  733. yutani_server_window_t * w = node->value;
  734. if (check_top_at(yg, w, x, y)) return w;
  735. }
  736. if (check_top_at(yg, yg->bottom_z, x, y)) return yg->bottom_z;
  737. return NULL;
  738. }
  739. /**
  740. * Get the window at a coordinate and focus it.
  741. *
  742. * See the docs for the proceeding functions, but added to this
  743. * focusing windows is also not particular fast as the reshuffle
  744. * is complicated.
  745. */
  746. static void set_focused_at(yutani_globals_t * yg, int x, int y) {
  747. yutani_server_window_t * n_focused = top_at(yg, x, y);
  748. set_focused_window(yg, n_focused);
  749. }
  750. /*
  751. * Convenience functions for checking if a window is in the top/bottom stack.
  752. *
  753. * In the future, these single-item "stacks" will be replaced with dedicated stacks
  754. * so we can have multiple background windows and multiple panels / always-top windows.
  755. */
  756. static int window_is_top(yutani_globals_t * yg, yutani_server_window_t * window) {
  757. /* For now, just use simple z-order */
  758. return window->z == YUTANI_ZORDER_TOP;
  759. }
  760. static int window_is_bottom(yutani_globals_t * yg, yutani_server_window_t * window) {
  761. /* For now, just use simple z-order */
  762. return window->z == YUTANI_ZORDER_BOTTOM;
  763. }
  764. /**
  765. * Get a color for a wid for debugging.
  766. *
  767. * Makes a pretty rainbow pattern.
  768. */
  769. static uint32_t color_for_wid(yutani_wid_t wid) {
  770. static uint32_t colors[] = {
  771. 0xFF19aeff,
  772. 0xFFff4141,
  773. 0xFFffff3e,
  774. 0xFFff6600,
  775. 0xFF9ade00,
  776. 0xFFd76cff,
  777. 0xFF364e59,
  778. 0xFF0084c8,
  779. 0xFFdc0000,
  780. 0xFFff9900,
  781. 0xFF009100,
  782. 0xFFba00ff,
  783. 0xFFb88100,
  784. 0xFF9eabb0
  785. };
  786. int i = wid % (sizeof(colors) / sizeof(uint32_t));
  787. return colors[i];
  788. }
  789. /**
  790. * Blit a window to the framebuffer.
  791. *
  792. * Applies transformations (rotation, animations) and then renders
  793. * the window through alpha blitting.
  794. */
  795. static int yutani_blit_window(yutani_globals_t * yg, yutani_server_window_t * window, int x, int y) {
  796. sprite_t _win_sprite;
  797. _win_sprite.width = window->width;
  798. _win_sprite.height = window->height;
  799. _win_sprite.bitmap = (uint32_t *)window->buffer;
  800. _win_sprite.masks = NULL;
  801. _win_sprite.blank = 0;
  802. _win_sprite.alpha = ALPHA_EMBEDDED;
  803. if (window->anim_mode) {
  804. int frame = yutani_time_since(yg, window->anim_start);
  805. if (frame >= yutani_animation_lengths[window->anim_mode]) {
  806. /* XXX handle animation-end things like cleanup of closing windows */
  807. if (window->anim_mode == YUTANI_EFFECT_FADE_OUT) {
  808. list_insert(yg->windows_to_remove, window);
  809. goto draw_finish;
  810. }
  811. window->anim_mode = 0;
  812. window->anim_start = 0;
  813. goto draw_window;
  814. } else {
  815. switch (window->anim_mode) {
  816. case YUTANI_EFFECT_FADE_OUT:
  817. {
  818. frame = yutani_animation_lengths[window->anim_mode] - frame;
  819. }
  820. case YUTANI_EFFECT_FADE_IN:
  821. {
  822. double time_diff = ((double)frame / (float)yutani_animation_lengths[window->anim_mode]);
  823. double x = 0.75 + time_diff * 0.25;
  824. int t_x = (window->width * (1.0 - x)) / 2;
  825. int t_y = (window->height * (1.0 - x)) / 2;
  826. double opacity = time_diff * (double)(window->opacity) / 255.0;
  827. if (!window_is_top(yg, window) && !window_is_bottom(yg, window) &&
  828. !(window->server_flags & YUTANI_WINDOW_FLAG_ALT_ANIMATION)) {
  829. draw_sprite_scaled_alpha(yg->backend_ctx, &_win_sprite, window->x + t_x, window->y + t_y, window->width * x, window->height * x, opacity);
  830. } else {
  831. draw_sprite_alpha(yg->backend_ctx, &_win_sprite, window->x, window->y, opacity);
  832. }
  833. }
  834. break;
  835. default:
  836. goto draw_window;
  837. break;
  838. }
  839. }
  840. } else {
  841. draw_window:
  842. if (window->opacity != 255) {
  843. double opacity = (double)(window->opacity) / 255.0;
  844. if (window == yg->resizing_window) {
  845. draw_sprite_scaled_alpha(yg->backend_ctx, &_win_sprite, window->x + (int)yg->resizing_offset_x, window->y + (int)yg->resizing_offset_y, yg->resizing_w, yg->resizing_h, opacity);
  846. } else {
  847. draw_sprite_alpha(yg->backend_ctx, &_win_sprite, window->x, window->y, opacity);
  848. }
  849. } else {
  850. if (window == yg->resizing_window) {
  851. draw_sprite_scaled(yg->backend_ctx, &_win_sprite, window->x + (int)yg->resizing_offset_x, window->y + (int)yg->resizing_offset_y, yg->resizing_w, yg->resizing_h);
  852. } else {
  853. draw_sprite(yg->backend_ctx, &_win_sprite, window->x, window->y);
  854. }
  855. }
  856. }
  857. draw_finish:
  858. return 0;
  859. }
  860. /**
  861. * VirtualBox Seamless desktop driver.
  862. *
  863. * Sends rectangles describing all the non-background windows
  864. * to the VirtualBox Guest Additions driver for use with the
  865. * seamless desktop mode.
  866. */
  867. static void yutani_post_vbox_rects(yutani_globals_t * yg) {
  868. if (yg->vbox_rects <= 0) return;
  869. char tmp[4096];
  870. uint32_t * count = (uint32_t *)tmp;
  871. *count = 0;
  872. int32_t * magic = (int32_t *)tmp;
  873. magic++;
  874. /* Add top window if it exists */
  875. if (yg->top_z) {
  876. *magic = yg->top_z->x; magic++;
  877. *magic = yg->top_z->y; magic++;
  878. *magic = yg->top_z->x + yg->top_z->width; magic++;
  879. *magic = yg->top_z->y + yg->top_z->height; magic++;
  880. (*count)++;
  881. }
  882. /* Add regular windows */
  883. foreach (node, yg->mid_zs) {
  884. yutani_server_window_t * w = node->value;
  885. if (w) {
  886. *magic = w->x; magic++;
  887. *magic = w->y; magic++;
  888. *magic = w->x + w->width; magic++;
  889. *magic = w->y + w->height; magic++;
  890. (*count)++;
  891. if (*count == 254) break;
  892. }
  893. }
  894. /*
  895. * If there were no windows, show the whole desktop
  896. * so we can see, eg., the login screen.
  897. */
  898. if (*count == 0) {
  899. *count = 1;
  900. *magic = 0; magic++;
  901. *magic = 0; magic++;
  902. *magic = yg->width; magic++;
  903. *magic = yg->height; magic++;
  904. }
  905. /* Post rectangle data to driver */
  906. write(yg->vbox_rects, tmp, sizeof(tmp));
  907. }
  908. /**
  909. * Blit all windows into the given context.
  910. *
  911. * This is called for rendering and for screenshots.
  912. */
  913. static void yutani_blit_windows(yutani_globals_t * yg) {
  914. if (yg->bottom_z) yutani_blit_window(yg, yg->bottom_z, yg->bottom_z->x, yg->bottom_z->y);
  915. foreach (node, yg->mid_zs) {
  916. yutani_server_window_t * w = node->value;
  917. if (w) yutani_blit_window(yg, w, w->x, w->y);
  918. }
  919. if (yg->top_z) yutani_blit_window(yg, yg->top_z, yg->top_z->x, yg->top_z->y);
  920. }
  921. /**
  922. * Take a screenshot
  923. */
  924. static void yutani_screenshot(yutani_globals_t * yg) {
  925. /* TODO Render bitmap screenshot */
  926. }
  927. /**
  928. * Redraw all windows, as well as the mouse cursor.
  929. *
  930. * This is the main redraw function.
  931. */
  932. static void redraw_windows(yutani_globals_t * yg) {
  933. int has_updates = 0;
  934. /* We keep our own temporary mouse coordinates as they may change while we're drawing. */
  935. int tmp_mouse_x = yg->mouse_x;
  936. int tmp_mouse_y = yg->mouse_y;
  937. if (yg->resize_on_next) {
  938. spin_lock(&yg->redraw_lock);
  939. TRACE("Resizing display.");
  940. if (!yutani_options.nested) {
  941. reinit_graphics_fullscreen(yg->backend_ctx);
  942. yg->stride = framebuffer_stride();
  943. } else {
  944. reinit_graphics_yutani(yg->backend_ctx, yg->host_window);
  945. yg->stride = yg->backend_ctx->width * 4;
  946. yutani_window_resize_done(yg->host_context, yg->host_window);
  947. }
  948. TRACE("graphics context resized...");
  949. yg->width = yg->backend_ctx->width;
  950. yg->height = yg->backend_ctx->height;
  951. yg->backend_framebuffer = yg->backend_ctx->backbuffer;
  952. TRACE("Marking...");
  953. yg->resize_on_next = 0;
  954. mark_screen(yg, 0, 0, yg->width, yg->height);
  955. TRACE("Sending welcome messages...");
  956. yutani_msg_buildx_welcome_alloc(response);
  957. yutani_msg_buildx_welcome(response, yg->width, yg->height);
  958. pex_broadcast(yg->server, response->size, (char *)response);
  959. TRACE("Done.");
  960. spin_unlock(&yg->redraw_lock);
  961. }
  962. /* If the mouse has moved, that counts as two damage regions */
  963. if ((yg->last_mouse_x != tmp_mouse_x) || (yg->last_mouse_y != tmp_mouse_y)) {
  964. has_updates = 2;
  965. yutani_add_clip(yg, yg->last_mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->last_mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  966. yutani_add_clip(yg, tmp_mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, tmp_mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  967. }
  968. yg->last_mouse_x = tmp_mouse_x;
  969. yg->last_mouse_y = tmp_mouse_y;
  970. if (yg->bottom_z && yg->bottom_z->anim_mode) mark_window(yg, yg->bottom_z);
  971. if (yg->top_z && yg->top_z->anim_mode) mark_window(yg, yg->top_z);
  972. foreach (node, yg->mid_zs) {
  973. yutani_server_window_t * w = node->value;
  974. if (w && w->anim_mode) mark_window(yg, w);
  975. }
  976. /* Calculate damage regions from currently queued updates */
  977. spin_lock(&yg->update_list_lock);
  978. while (yg->update_list->length) {
  979. node_t * win = list_dequeue(yg->update_list);
  980. yutani_damage_rect_t * rect = (void *)win->value;
  981. /* We add a clip region for each window in the update queue */
  982. has_updates = 1;
  983. yutani_add_clip(yg, rect->x, rect->y, rect->width, rect->height);
  984. free(rect);
  985. free(win);
  986. }
  987. spin_unlock(&yg->update_list_lock);
  988. /* Render */
  989. if (has_updates) {
  990. if (!yg->bottom_z || yg->bottom_z->anim_mode) {
  991. draw_fill(yg->backend_ctx, rgb(110,110,110));
  992. }
  993. yg->windows_to_remove = list_create();
  994. /*
  995. * In theory, we should restrict this to windows within the clip region,
  996. * but calculating that may be more trouble than it's worth;
  997. * we also need to render windows in stacking order...
  998. */
  999. spin_lock(&yg->redraw_lock);
  1000. yutani_blit_windows(yg);
  1001. /* Send VirtualBox rects */
  1002. yutani_post_vbox_rects(yg);
  1003. #if YUTANI_DEBUG_WINDOW_SHAPES
  1004. #define WINDOW_SHAPE_VIEWER_SIZE 20
  1005. /*
  1006. * Debugging window shapes: draw a box around the mouse cursor
  1007. * showing which window is at the top and will accept mouse events.
  1008. */
  1009. if (yg->debug_shapes) {
  1010. int _ly = max(0,tmp_mouse_y/MOUSE_SCALE - WINDOW_SHAPE_VIEWER_SIZE);
  1011. int _hy = min(yg->height,tmp_mouse_y/MOUSE_SCALE + WINDOW_SHAPE_VIEWER_SIZE);
  1012. int _lx = max(0,tmp_mouse_x/MOUSE_SCALE - 20);
  1013. int _hx = min(yg->width,tmp_mouse_x/MOUSE_SCALE + WINDOW_SHAPE_VIEWER_SIZE);
  1014. for (int y = _ly; y < _hy; ++y) {
  1015. for (int x = _lx; x < _hx; ++x) {
  1016. yutani_server_window_t * w = top_at(yg, x, y);
  1017. if (w) { GFX(yg->backend_ctx, x, y) = color_for_wid(w->wid); }
  1018. }
  1019. }
  1020. }
  1021. #endif
  1022. if (yutani_options.nested) {
  1023. flip(yg->backend_ctx);
  1024. /*
  1025. * We should be able to flip only the places we need to flip, but
  1026. * instead we're going to flip the whole thing.
  1027. *
  1028. * TODO: Do a better job of this.
  1029. */
  1030. yutani_flip(yg->host_context, yg->host_window);
  1031. yutani_server_window_t * tmp_window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1032. if (yg->mouse_state == YUTANI_MOUSE_STATE_MOVING) {
  1033. yutani_window_show_mouse(yg->host_context, yg->host_window, YUTANI_CURSOR_TYPE_DRAG);
  1034. } else if (!tmp_window || tmp_window->show_mouse) {
  1035. yutani_window_show_mouse(yg->host_context, yg->host_window, tmp_window ? tmp_window->show_mouse : 1);
  1036. }
  1037. } else {
  1038. /*
  1039. * Draw the cursor.
  1040. * We may also want to draw other compositor elements, like effects, but those
  1041. * can also go in the stack order of the windows.
  1042. */
  1043. yutani_server_window_t * tmp_window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1044. if (!tmp_window || tmp_window->show_mouse) {
  1045. draw_cursor(yg, tmp_mouse_x, tmp_mouse_y, tmp_window ? tmp_window->show_mouse : 1);
  1046. }
  1047. /*
  1048. * Flip the updated areas. This minimizes writes to video memory,
  1049. * which is very important on real hardware where these writes are slow.
  1050. */
  1051. flip(yg->backend_ctx);
  1052. }
  1053. gfx_clear_clip(yg->backend_ctx);
  1054. spin_unlock(&yg->redraw_lock);
  1055. /*
  1056. * If any windows were marked for removal,
  1057. * then remove them.
  1058. */
  1059. while (yg->windows_to_remove->head) {
  1060. node_t * node = list_pop(yg->windows_to_remove);
  1061. window_actually_close(yg, node->value);
  1062. free(node);
  1063. }
  1064. free(yg->windows_to_remove);
  1065. }
  1066. if (yg->screenshot_frame) {
  1067. yutani_screenshot(yg);
  1068. }
  1069. }
  1070. /**
  1071. * Initialize clipping regions.
  1072. */
  1073. void yutani_clip_init(yutani_globals_t * yg) {
  1074. yg->update_list = list_create();
  1075. yg->update_list_lock = 0;
  1076. }
  1077. /**
  1078. * Redraw thread.
  1079. *
  1080. * Calls the redraw functions in a loop, with some
  1081. * additional yielding and sleeping.
  1082. */
  1083. void * redraw(void * in) {
  1084. syscall_system_function(11,(char *[]){"compositor","render thread",NULL});
  1085. yutani_globals_t * yg = in;
  1086. while (yg->server) {
  1087. /*
  1088. * Perform whatever redraw work is required.
  1089. */
  1090. redraw_windows(yg);
  1091. /*
  1092. * Attempt to run at about 60fps...
  1093. * we should actually see how long it took to render so
  1094. * we can sleep *less* if it took a long time to render
  1095. * this particular frame. We are definitely not
  1096. * going to run at 60fps unless there's nothing to do
  1097. * (and even then we've wasted cycles checking).
  1098. */
  1099. usleep(16666);
  1100. }
  1101. return NULL;
  1102. }
  1103. /**
  1104. * Mark a region within a window as damaged.
  1105. *
  1106. * If the window is rotated, we calculate the minimum rectangle that covers
  1107. * the whole region specified and then mark that.
  1108. */
  1109. static void mark_window_relative(yutani_globals_t * yg, yutani_server_window_t * window, int32_t x, int32_t y, int32_t width, int32_t height) {
  1110. yutani_damage_rect_t * rect = malloc(sizeof(yutani_damage_rect_t));
  1111. if (window == yg->resizing_window) {
  1112. double x_scale = (double)yg->resizing_w / (double)yg->resizing_window->width;
  1113. double y_scale = (double)yg->resizing_h / (double)yg->resizing_window->height;
  1114. x *= x_scale;
  1115. x += yg->resizing_offset_x - 1;
  1116. y *= y_scale;
  1117. y += yg->resizing_offset_y - 1;
  1118. width *= x_scale;
  1119. height *= y_scale;
  1120. width += 2;
  1121. height += 2;
  1122. }
  1123. if (window->rotation == 0) {
  1124. rect->x = window->x + x;
  1125. rect->y = window->y + y;
  1126. rect->width = width;
  1127. rect->height = height;
  1128. } else {
  1129. int32_t ul_x, ul_y;
  1130. int32_t ll_x, ll_y;
  1131. int32_t ur_x, ur_y;
  1132. int32_t lr_x, lr_y;
  1133. window_to_device(window, x, y, &ul_x, &ul_y);
  1134. window_to_device(window, x, y + height, &ll_x, &ll_y);
  1135. window_to_device(window, x + width, y, &ur_x, &ur_y);
  1136. window_to_device(window, x + width, y + height, &lr_x, &lr_y);
  1137. /* Calculate bounds */
  1138. int32_t left_bound = min(min(ul_x, ll_x), min(ur_x, lr_x));
  1139. int32_t top_bound = min(min(ul_y, ll_y), min(ur_y, lr_y));
  1140. int32_t right_bound = max(max(ul_x, ll_x), max(ur_x, lr_x));
  1141. int32_t bottom_bound = max(max(ul_y, ll_y), max(ur_y, lr_y));
  1142. rect->x = left_bound;
  1143. rect->y = top_bound;
  1144. rect->width = right_bound - left_bound;
  1145. rect->height = bottom_bound - top_bound;
  1146. }
  1147. spin_lock(&yg->update_list_lock);
  1148. list_insert(yg->update_list, rect);
  1149. spin_unlock(&yg->update_list_lock);
  1150. }
  1151. /**
  1152. * (Convenience function) Mark a whole a window as damaged.
  1153. */
  1154. static void mark_window(yutani_globals_t * yg, yutani_server_window_t * window) {
  1155. mark_window_relative(yg, window, 0, 0, window->width, window->height);
  1156. }
  1157. /**
  1158. * Set a window as closed. It will be removed after rendering has completed.
  1159. */
  1160. static void window_mark_for_close(yutani_globals_t * yg, yutani_server_window_t * w) {
  1161. w->anim_mode = YUTANI_EFFECT_FADE_OUT;
  1162. w->anim_start = yutani_current_time(yg);
  1163. }
  1164. /**
  1165. * Remove a window from its owner's child set.
  1166. */
  1167. static void window_remove_from_client(yutani_globals_t * yg, yutani_server_window_t * w) {
  1168. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)w->owner);
  1169. if (client_list) {
  1170. node_t * n = list_find(client_list, w);
  1171. if (n) {
  1172. list_delete(client_list, n);
  1173. free(n);
  1174. }
  1175. }
  1176. }
  1177. /**
  1178. * Actually remove a window and free the associated resources.
  1179. */
  1180. static void window_actually_close(yutani_globals_t * yg, yutani_server_window_t * w) {
  1181. /* Remove from the wid -> window mapping */
  1182. hashmap_remove(yg->wids_to_windows, (void *)w->wid);
  1183. /* Remove from the general list of windows. */
  1184. list_remove(yg->windows, list_index_of(yg->windows, w));
  1185. /* Unstack the window */
  1186. unorder_window(yg, w);
  1187. /* Mark the region where the window was */
  1188. mark_window(yg, w);
  1189. /* And if it was focused, unfocus it. */
  1190. if (w == yg->focused_window) {
  1191. /* find the top z-ordered window */
  1192. yg->focused_window = NULL;
  1193. if (yg->mid_zs->tail && yg->mid_zs->tail->value) {
  1194. set_focused_window(yg, yg->mid_zs->tail->value);
  1195. }
  1196. }
  1197. {
  1198. char key[1024];
  1199. YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, w->bufid);
  1200. /*
  1201. * Normally we would acquire a lock before doing this, but the render
  1202. * thread holds that lock already and we are only called from the
  1203. * render thread, so we don't bother.
  1204. */
  1205. syscall_shm_release(key);
  1206. }
  1207. /* Notify subscribers that there are changes to windows */
  1208. notify_subscribers(yg);
  1209. }
  1210. /**
  1211. * Generate flags for client advertisements.
  1212. *
  1213. * Currently, we only have one flag (focused).
  1214. */
  1215. static uint32_t ad_flags(yutani_globals_t * yg, yutani_server_window_t * win) {
  1216. uint32_t flags = win->client_flags;
  1217. if (win == yg->focused_window) {
  1218. flags |= 1;
  1219. }
  1220. return flags;
  1221. }
  1222. /**
  1223. * Send a result for a window query.
  1224. */
  1225. static void yutani_query_result(yutani_globals_t * yg, uint32_t dest, yutani_server_window_t * win) {
  1226. if (win && win->client_length) {
  1227. yutani_msg_buildx_window_advertise_alloc(response, win->client_length);
  1228. yutani_msg_buildx_window_advertise(response, win->wid, ad_flags(yg, win), win->client_offsets, win->client_length, win->client_strings);
  1229. pex_send(yg->server, dest, response->size, (char *)response);
  1230. }
  1231. }
  1232. /**
  1233. * Send a notice to all subscribed clients that windows have updated.
  1234. */
  1235. static void notify_subscribers(yutani_globals_t * yg) {
  1236. yutani_msg_buildx_notify_alloc(response);
  1237. yutani_msg_buildx_notify(response);
  1238. list_t * remove = NULL;
  1239. foreach(node, yg->window_subscribers) {
  1240. uint32_t subscriber = (uint32_t)node->value;
  1241. if (!hashmap_has(yg->clients_to_windows, (void *)subscriber)) {
  1242. if (!remove) {
  1243. remove = list_create();
  1244. }
  1245. list_insert(remove, node);
  1246. } else {
  1247. pex_send(yg->server, subscriber, response->size, (char *)response);
  1248. }
  1249. }
  1250. if (remove) {
  1251. while (remove->length) {
  1252. node_t * n = list_pop(remove);
  1253. list_delete(yg->window_subscribers, n->value);
  1254. free(n);
  1255. }
  1256. free(remove);
  1257. }
  1258. }
  1259. static void window_move(yutani_globals_t * yg, yutani_server_window_t * window, int x, int y) {
  1260. mark_window(yg, window);
  1261. window->x = x;
  1262. window->y = y;
  1263. mark_window(yg, window);
  1264. yutani_msg_buildx_window_move_alloc(response);
  1265. yutani_msg_buildx_window_move(response, window->wid, x, y);
  1266. pex_send(yg->server, window->owner, response->size, (char *)response);
  1267. }
  1268. /**
  1269. * Move and resize a window to fit a particular tiling pattern.
  1270. *
  1271. * x and y are 0-based
  1272. * width_div and height_div are the number of cells in each dimension
  1273. */
  1274. static void window_tile(yutani_globals_t * yg, yutani_server_window_t * window, int width_div, int height_div, int x, int y) {
  1275. int panel_h = 0;
  1276. yutani_server_window_t * panel = yg->top_z;
  1277. if (panel) {
  1278. panel_h = panel->height;
  1279. if (panel->y < 1) {
  1280. panel_h += panel->y; /* We can move the panel up to "hide" it. */
  1281. }
  1282. }
  1283. if (!window->tiled) {
  1284. window->untiled_width = window->width;
  1285. window->untiled_height = window->height;
  1286. window->untiled_left = window->x;
  1287. window->untiled_top = window->y;
  1288. window->tiled = 1;
  1289. }
  1290. int w = yg->width / width_div;
  1291. int h = (yg->height - panel_h) / height_div;
  1292. /* Calculate, move, etc. */
  1293. window_move(yg, window, w * x, panel_h + h * y);
  1294. if (x == width_div - 1) {
  1295. w = yg->width - w * x;
  1296. }
  1297. if (y == height_div - 1) {
  1298. h = (yg->height - panel_h) - h * y;
  1299. }
  1300. yutani_msg_buildx_window_resize_alloc(response);
  1301. yutani_msg_buildx_window_resize(response, YUTANI_MSG_RESIZE_OFFER, window->wid, w, h, 0);
  1302. pex_send(yg->server, window->owner, response->size, (char *)response);
  1303. }
  1304. /**
  1305. * Take a previously tiled window and "untile" it, eg. restore its original size.
  1306. */
  1307. static void window_untile(yutani_globals_t * yg, yutani_server_window_t * window) {
  1308. window->tiled = 0;
  1309. yutani_msg_buildx_window_resize_alloc(response);
  1310. yutani_msg_buildx_window_resize(response,YUTANI_MSG_RESIZE_OFFER, window->wid, window->untiled_width, window->untiled_height, 0);
  1311. pex_send(yg->server, window->owner, response->size, (char *)response);
  1312. }
  1313. /**
  1314. * Process a key event.
  1315. *
  1316. * These are mostly compositor shortcuts and bindings.
  1317. * We also process key bindings for other applications.
  1318. */
  1319. static void handle_key_event(yutani_globals_t * yg, struct yutani_msg_key_event * ke) {
  1320. yutani_server_window_t * focused = get_focused(yg);
  1321. memcpy(&yg->kbd_state, &ke->state, sizeof(key_event_state_t));
  1322. if (focused) {
  1323. #if 0
  1324. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1325. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1326. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1327. (ke->event.keycode == 'z')) {
  1328. mark_window(yg,focused);
  1329. focused->rotation -= 5;
  1330. mark_window(yg,focused);
  1331. return;
  1332. }
  1333. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1334. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1335. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1336. (ke->event.keycode == 'x')) {
  1337. mark_window(yg,focused);
  1338. focused->rotation += 5;
  1339. mark_window(yg,focused);
  1340. return;
  1341. }
  1342. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1343. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1344. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1345. (ke->event.keycode == 'c')) {
  1346. mark_window(yg,focused);
  1347. focused->rotation = 0;
  1348. mark_window(yg,focused);
  1349. return;
  1350. }
  1351. #endif
  1352. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1353. (ke->event.modifiers & KEY_MOD_LEFT_ALT) &&
  1354. (ke->event.keycode == KEY_F10)) {
  1355. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1356. if (focused->tiled) {
  1357. window_untile(yg, focused);
  1358. window_move(yg, focused, focused->untiled_left, focused->untiled_top);
  1359. } else {
  1360. window_tile(yg, focused, 1, 1, 0, 0);
  1361. }
  1362. return;
  1363. }
  1364. }
  1365. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1366. (ke->event.modifiers & KEY_MOD_LEFT_ALT) &&
  1367. (ke->event.keycode == KEY_F4)) {
  1368. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1369. yutani_msg_buildx_window_close_alloc(response);
  1370. yutani_msg_buildx_window_close(response, focused->wid);
  1371. pex_send(yg->server, focused->owner, response->size, (char *)response);
  1372. return;
  1373. }
  1374. }
  1375. #if YUTANI_DEBUG_WINDOW_SHAPES
  1376. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1377. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1378. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1379. (ke->event.keycode == 'n')) {
  1380. yg->debug_shapes = (1-yg->debug_shapes);
  1381. return;
  1382. }
  1383. #endif
  1384. #if YUTANI_DEBUG_WINDOW_BOUNDS
  1385. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1386. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1387. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1388. (ke->event.keycode == 'b')) {
  1389. yg->debug_bounds = (1-yg->debug_bounds);
  1390. return;
  1391. }
  1392. #endif
  1393. /*
  1394. * Tiling hooks.
  1395. * These are based on the compiz grid plugin.
  1396. */
  1397. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1398. (ke->event.modifiers & KEY_MOD_LEFT_SUPER)) {
  1399. if ((ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1400. (ke->event.keycode == KEY_ARROW_LEFT)) {
  1401. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1402. window_tile(yg, focused, 2, 2, 0, 0);
  1403. return;
  1404. }
  1405. }
  1406. if ((ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1407. (ke->event.keycode == KEY_ARROW_RIGHT)) {
  1408. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1409. window_tile(yg, focused, 2, 2, 1, 0);
  1410. return;
  1411. }
  1412. }
  1413. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1414. (ke->event.keycode == KEY_ARROW_LEFT)) {
  1415. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1416. window_tile(yg, focused, 2, 2, 0, 1);
  1417. return;
  1418. }
  1419. }
  1420. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1421. (ke->event.keycode == KEY_ARROW_RIGHT)) {
  1422. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1423. window_tile(yg, focused, 2, 2, 1, 1);
  1424. return;
  1425. }
  1426. }
  1427. if ((ke->event.keycode == KEY_ARROW_LEFT)) {
  1428. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1429. window_tile(yg, focused, 2, 1, 0, 0);
  1430. return;
  1431. }
  1432. }
  1433. if ((ke->event.keycode == KEY_ARROW_RIGHT)) {
  1434. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1435. window_tile(yg, focused, 2, 1, 1, 0);
  1436. return;
  1437. }
  1438. }
  1439. if ((ke->event.keycode == KEY_ARROW_UP)) {
  1440. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1441. window_tile(yg, focused, 1, 2, 0, 0);
  1442. return;
  1443. }
  1444. }
  1445. if ((ke->event.keycode == KEY_ARROW_DOWN)) {
  1446. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1447. window_tile(yg, focused, 1, 2, 0, 1);
  1448. return;
  1449. }
  1450. }
  1451. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1452. (ke->event.keycode == 's')) {
  1453. yg->screenshot_frame = YUTANI_SCREENSHOT_FULL;
  1454. return;
  1455. }
  1456. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1457. (ke->event.keycode == 'w')) {
  1458. yg->screenshot_frame = YUTANI_SCREENSHOT_WINDOW;
  1459. return;
  1460. }
  1461. }
  1462. }
  1463. /*
  1464. * External bindings registered by clients.
  1465. */
  1466. uint32_t key_code = ((ke->event.modifiers << 24) | (ke->event.keycode));
  1467. if (hashmap_has(yg->key_binds, (void*)key_code)) {
  1468. struct key_bind * bind = hashmap_get(yg->key_binds, (void*)key_code);
  1469. yutani_msg_buildx_key_event_alloc(response);
  1470. yutani_msg_buildx_key_event(response,focused ? focused->wid : UINT32_MAX, &ke->event, &ke->state);
  1471. pex_send(yg->server, bind->owner, response->size, (char *)response);
  1472. if (bind->response == YUTANI_BIND_STEAL) {
  1473. /* If this keybinding was registered as "steal", we'll stop here. */
  1474. return;
  1475. }
  1476. }
  1477. /* Finally, send the key to the focused client. */
  1478. if (focused) {
  1479. yutani_msg_buildx_key_event_alloc(response);
  1480. yutani_msg_buildx_key_event(response,focused->wid, &ke->event, &ke->state);
  1481. pex_send(yg->server, focused->owner, response->size, (char *)response);
  1482. }
  1483. }
  1484. /**
  1485. * Register a new keybinding.
  1486. *
  1487. * req - bind message
  1488. * owner - client to assign the binding to
  1489. */
  1490. static void add_key_bind(yutani_globals_t * yg, struct yutani_msg_key_bind * req, unsigned int owner) {
  1491. uint32_t key_code = (((uint8_t)req->modifiers << 24) | ((uint32_t)req->key & 0xFFFFFF));
  1492. struct key_bind * bind = hashmap_get(yg->key_binds, (void*)key_code);
  1493. if (!bind) {
  1494. bind = malloc(sizeof(struct key_bind));
  1495. bind->owner = owner;
  1496. bind->response = req->response;
  1497. hashmap_set(yg->key_binds, (void*)key_code, bind);
  1498. } else {
  1499. bind->owner = owner;
  1500. bind->response = req->response;
  1501. }
  1502. }
  1503. static void adjust_window_opacity(yutani_globals_t * yg, int direction) {
  1504. yutani_server_window_t * window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1505. if (window && window->z != YUTANI_ZORDER_BOTTOM) {
  1506. window->opacity += direction;
  1507. if (window->opacity < 0) {
  1508. window->opacity = 0;
  1509. }
  1510. if (window->opacity > 255) {
  1511. window->opacity = 255;
  1512. }
  1513. mark_window(yg, window);
  1514. }
  1515. }
  1516. static void mouse_start_drag(yutani_globals_t * yg, yutani_server_window_t * w) {
  1517. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1518. if (!w) {
  1519. yg->mouse_window = get_focused(yg);
  1520. } else {
  1521. yg->mouse_window = w;
  1522. }
  1523. if (yg->mouse_window) {
  1524. if (yg->mouse_window->z == YUTANI_ZORDER_BOTTOM || yg->mouse_window->z == YUTANI_ZORDER_TOP
  1525. || yg->mouse_window->server_flags & YUTANI_WINDOW_FLAG_DISALLOW_DRAG) {
  1526. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1527. yg->mouse_window = NULL;
  1528. } else {
  1529. yg->mouse_state = YUTANI_MOUSE_STATE_MOVING;
  1530. yg->mouse_init_x = yg->mouse_x;
  1531. yg->mouse_init_y = yg->mouse_y;
  1532. yg->mouse_win_x = yg->mouse_window->x;
  1533. yg->mouse_win_y = yg->mouse_window->y;
  1534. yg->mouse_drag_button = yg->last_mouse_buttons;
  1535. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  1536. make_top(yg, yg->mouse_window);
  1537. }
  1538. }
  1539. }
  1540. static void mouse_start_rotate(yutani_globals_t * yg) {
  1541. #if 0
  1542. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1543. yg->mouse_window = get_focused(yg);
  1544. if (yg->mouse_window) {
  1545. if (yg->mouse_window->z == YUTANI_ZORDER_BOTTOM || yg->mouse_window->z == YUTANI_ZORDER_TOP) {
  1546. /* Prevent rotating panel and wallpaper */
  1547. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1548. yg->mouse_window = NULL;
  1549. return;
  1550. }
  1551. yg->mouse_state = YUTANI_MOUSE_STATE_ROTATING;
  1552. yg->mouse_init_x = yg->mouse_x;
  1553. yg->mouse_init_y = yg->mouse_y;
  1554. int32_t x_diff = yg->mouse_x / MOUSE_SCALE - (yg->mouse_window->x + yg->mouse_window->width / 2);
  1555. int32_t y_diff = yg->mouse_y / MOUSE_SCALE - (yg->mouse_window->y + yg->mouse_window->height / 2);
  1556. int new_r = __builtin_atan2(x_diff, y_diff) * 180.0 / (-M_PI);
  1557. yg->mouse_init_r = yg->mouse_window->rotation - new_r;
  1558. make_top(yg, yg->mouse_window);
  1559. }
  1560. #endif
  1561. }
  1562. static void mouse_start_resize(yutani_globals_t * yg, yutani_scale_direction_t direction) {
  1563. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1564. yg->mouse_window = get_focused(yg);
  1565. if (yg->mouse_window) {
  1566. if (yg->mouse_window->z == YUTANI_ZORDER_BOTTOM || yg->mouse_window->z == YUTANI_ZORDER_TOP
  1567. || yg->mouse_window->server_flags & YUTANI_WINDOW_FLAG_DISALLOW_RESIZE) {
  1568. /* Prevent resizing panel and wallpaper */
  1569. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1570. yg->mouse_window = NULL;
  1571. yg->resizing_window = NULL;
  1572. } else {
  1573. TRACE("resize starting for wid=%d", yg->mouse_window -> wid);
  1574. yg->mouse_state = YUTANI_MOUSE_STATE_RESIZING;
  1575. yg->mouse_init_x = yg->mouse_x;
  1576. yg->mouse_init_y = yg->mouse_y;
  1577. yg->mouse_win_x = yg->mouse_window->x;
  1578. yg->mouse_win_y = yg->mouse_window->y;
  1579. yg->resizing_window = yg->mouse_window;
  1580. yg->resizing_w = yg->mouse_window->width;
  1581. yg->resizing_h = yg->mouse_window->height;
  1582. yg->resizing_offset_x = 0;
  1583. yg->resizing_offset_y = 0;
  1584. if (direction == SCALE_AUTO) {
  1585. /* Determine the best direction to scale in based on simple 9-cell system. */
  1586. int32_t x, y;
  1587. device_to_window(yg->resizing_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1588. int h_d = 0;
  1589. int v_d = 0;
  1590. if (y <= yg->resizing_h / 3) {
  1591. v_d = -1;
  1592. } else if (y >= (yg->resizing_h / 3) * 2) {
  1593. v_d = 1;
  1594. }
  1595. if (x <= yg->resizing_w / 3) {
  1596. h_d = -1;
  1597. } else if (x >= (yg->resizing_w / 3) * 2) {
  1598. h_d = 1;
  1599. }
  1600. /* Fall back */
  1601. if (h_d == 0 && v_d == 0) direction = SCALE_DOWN_RIGHT;
  1602. else if (h_d == 1 && v_d == 1) direction = SCALE_DOWN_RIGHT;
  1603. else if (h_d == 1 && v_d == -1) direction = SCALE_UP_RIGHT;
  1604. else if (h_d == -1 && v_d == 1) direction = SCALE_DOWN_LEFT;
  1605. else if (h_d == -1 && v_d == -1) direction = SCALE_UP_LEFT;
  1606. else if (h_d == 1 && v_d == 0) direction = SCALE_RIGHT;
  1607. else if (h_d == -1 && v_d == 0) direction = SCALE_LEFT;
  1608. else if (h_d == 0 && v_d == 1) direction = SCALE_DOWN;
  1609. else if (h_d == 0 && v_d == -1) direction = SCALE_UP;
  1610. }
  1611. yg->resizing_direction = direction;
  1612. make_top(yg, yg->mouse_window);
  1613. mark_window(yg, yg->resizing_window);
  1614. }
  1615. }
  1616. }
  1617. static void handle_mouse_event(yutani_globals_t * yg, struct yutani_msg_mouse_event * me) {
  1618. if (me->type == YUTANI_MOUSE_EVENT_TYPE_RELATIVE) {
  1619. yg->mouse_x += me->event.x_difference YUTANI_INCOMING_MOUSE_SCALE;
  1620. yg->mouse_y -= me->event.y_difference YUTANI_INCOMING_MOUSE_SCALE;
  1621. } else if (me->type == YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE) {
  1622. yg->mouse_x = me->event.x_difference * MOUSE_SCALE;
  1623. yg->mouse_y = me->event.y_difference * MOUSE_SCALE;
  1624. }
  1625. if (yg->mouse_x < 0) yg->mouse_x = 0;
  1626. if (yg->mouse_y < 0) yg->mouse_y = 0;
  1627. if (yg->mouse_x > (int)(yg->width) * MOUSE_SCALE) yg->mouse_x = (yg->width) * MOUSE_SCALE;
  1628. if (yg->mouse_y > (int)(yg->height) * MOUSE_SCALE) yg->mouse_y = (yg->height) * MOUSE_SCALE;
  1629. switch (yg->mouse_state) {
  1630. case YUTANI_MOUSE_STATE_NORMAL:
  1631. {
  1632. if ((me->event.buttons & YUTANI_MOUSE_BUTTON_LEFT) && (yg->kbd_state.k_alt)) {
  1633. mouse_start_drag(yg, NULL);
  1634. } else if ((me->event.buttons & YUTANI_MOUSE_SCROLL_UP) && (yg->kbd_state.k_alt)) {
  1635. adjust_window_opacity(yg, 8);
  1636. } else if ((me->event.buttons & YUTANI_MOUSE_SCROLL_DOWN) && (yg->kbd_state.k_alt)) {
  1637. adjust_window_opacity(yg, -8);
  1638. } else if ((me->event.buttons & YUTANI_MOUSE_BUTTON_RIGHT) && (yg->kbd_state.k_alt)) {
  1639. mouse_start_rotate(yg);
  1640. } else if ((me->event.buttons & YUTANI_MOUSE_BUTTON_MIDDLE) && (yg->kbd_state.k_alt)) {
  1641. yg->resizing_button = YUTANI_MOUSE_BUTTON_MIDDLE;
  1642. mouse_start_resize(yg, SCALE_AUTO);
  1643. } else if ((me->event.buttons & YUTANI_MOUSE_BUTTON_LEFT) && (!yg->kbd_state.k_alt)) {
  1644. yg->mouse_state = YUTANI_MOUSE_STATE_DRAGGING;
  1645. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1646. yg->mouse_window = get_focused(yg);
  1647. yg->mouse_moved = 0;
  1648. yg->mouse_drag_button = YUTANI_MOUSE_BUTTON_LEFT;
  1649. if (yg->mouse_window) {
  1650. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &yg->mouse_click_x, &yg->mouse_click_y);
  1651. yutani_msg_buildx_window_mouse_event_alloc(response);
  1652. yutani_msg_buildx_window_mouse_event(response,yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_DOWN);
  1653. yg->mouse_click_x_orig = yg->mouse_click_x;
  1654. yg->mouse_click_y_orig = yg->mouse_click_y;
  1655. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1656. }
  1657. } else {
  1658. yg->mouse_window = get_focused(yg);
  1659. yutani_server_window_t * tmp_window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1660. if (yg->mouse_window) {
  1661. int32_t x, y;
  1662. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1663. yutani_msg_buildx_window_mouse_event_alloc(response);
  1664. yutani_msg_buildx_window_mouse_event(response,yg->mouse_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_MOVE);
  1665. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1666. }
  1667. if (tmp_window) {
  1668. int32_t x, y;
  1669. yutani_msg_buildx_window_mouse_event_alloc(response);
  1670. if (tmp_window != yg->old_hover_window) {
  1671. device_to_window(tmp_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1672. yutani_msg_buildx_window_mouse_event(response, tmp_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_ENTER);
  1673. pex_send(yg->server, tmp_window->owner, response->size, (char *)response);
  1674. if (yg->old_hover_window) {
  1675. device_to_window(yg->old_hover_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1676. yutani_msg_buildx_window_mouse_event(response, yg->old_hover_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_LEAVE);
  1677. pex_send(yg->server, yg->old_hover_window->owner, response->size, (char *)response);
  1678. }
  1679. yg->old_hover_window = tmp_window;
  1680. }
  1681. if (tmp_window != yg->mouse_window) {
  1682. device_to_window(tmp_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1683. yutani_msg_buildx_window_mouse_event(response, tmp_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_MOVE);
  1684. pex_send(yg->server, tmp_window->owner, response->size, (char *)response);
  1685. }
  1686. }
  1687. }
  1688. }
  1689. break;
  1690. case YUTANI_MOUSE_STATE_MOVING:
  1691. {
  1692. int button_down = (me->event.buttons & YUTANI_MOUSE_BUTTON_LEFT);
  1693. int drag_stop = yg->mouse_drag_button != 0 ? (!button_down) : (button_down);
  1694. if (drag_stop) {
  1695. yg->mouse_window = NULL;
  1696. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1697. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  1698. } else {
  1699. if (yg->mouse_y / MOUSE_SCALE < 10) {
  1700. if (!yg->mouse_window->tiled) {
  1701. window_tile(yg, yg->mouse_window, 1, 1, 0, 0);
  1702. }
  1703. break;
  1704. }
  1705. if (yg->mouse_window->tiled) {
  1706. if ((abs(yg->mouse_x - yg->mouse_init_x) > UNTILE_SENSITIVITY) || (abs(yg->mouse_y - yg->mouse_init_y) > UNTILE_SENSITIVITY)) {
  1707. /* Untile it */
  1708. window_untile(yg,yg->mouse_window);
  1709. /* Position the window such that it's representative of where it was, percentage-wise, in the untiled window */
  1710. float percent_x = (float)(yg->mouse_x / MOUSE_SCALE - yg->mouse_window->x) / (float)yg->mouse_window->width;
  1711. float percent_y = (float)(yg->mouse_y / MOUSE_SCALE - yg->mouse_window->y) / (float)yg->mouse_window->height;
  1712. window_move(yg, yg->mouse_window,
  1713. yg->mouse_x / MOUSE_SCALE - yg->mouse_window->untiled_width * percent_x,
  1714. yg->mouse_y / MOUSE_SCALE - yg->mouse_window->untiled_height * percent_y);
  1715. /* reset init_x / init_y */
  1716. yg->mouse_init_x = yg->mouse_x;
  1717. yg->mouse_init_y = yg->mouse_y;
  1718. yg->mouse_win_x = yg->mouse_window->x;
  1719. yg->mouse_win_y = yg->mouse_window->y;
  1720. }
  1721. } else {
  1722. int x, y;
  1723. x = yg->mouse_win_x + (yg->mouse_x - yg->mouse_init_x) / MOUSE_SCALE;
  1724. y = yg->mouse_win_y + (yg->mouse_y - yg->mouse_init_y) / MOUSE_SCALE;
  1725. window_move(yg, yg->mouse_window, x, y);
  1726. }
  1727. }
  1728. }
  1729. break;
  1730. case YUTANI_MOUSE_STATE_ROTATING:
  1731. {
  1732. #if 0
  1733. if (!(me->event.buttons & YUTANI_MOUSE_BUTTON_RIGHT)) {
  1734. yg->mouse_window = NULL;
  1735. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1736. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  1737. } else if (yg->mouse_window) {
  1738. /* Calculate rotation and make relative to initial rotation */
  1739. int32_t x_diff = yg->mouse_x / MOUSE_SCALE - (yg->mouse_window->x + yg->mouse_window->width / 2);
  1740. int32_t y_diff = yg->mouse_y / MOUSE_SCALE - (yg->mouse_window->y + yg->mouse_window->height / 2);
  1741. int new_r = __builtin_atan2(x_diff, y_diff) * 180.0 / (-M_PI);
  1742. mark_window(yg, yg->mouse_window);
  1743. yg->mouse_window->rotation = new_r + yg->mouse_init_r;
  1744. mark_window(yg, yg->mouse_window);
  1745. }
  1746. #endif
  1747. }
  1748. break;
  1749. case YUTANI_MOUSE_STATE_DRAGGING:
  1750. {
  1751. if (!(me->event.buttons & yg->mouse_drag_button)) {
  1752. /* Mouse released */
  1753. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1754. int32_t old_x = yg->mouse_click_x_orig;
  1755. int32_t old_y = yg->mouse_click_y_orig;
  1756. if (yg->mouse_window) {
  1757. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &yg->mouse_click_x, &yg->mouse_click_y);
  1758. if (!yg->mouse_moved) {
  1759. yutani_msg_buildx_window_mouse_event_alloc(response);
  1760. yutani_msg_buildx_window_mouse_event(response,yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_CLICK);
  1761. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1762. } else {
  1763. yutani_msg_buildx_window_mouse_event_alloc(response);
  1764. yutani_msg_buildx_window_mouse_event(response,yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, old_x, old_y, me->event.buttons, YUTANI_MOUSE_EVENT_RAISE);
  1765. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1766. }
  1767. }
  1768. } else {
  1769. yg->mouse_state = YUTANI_MOUSE_STATE_DRAGGING;
  1770. yg->mouse_moved = 1;
  1771. int32_t old_x = yg->mouse_click_x;
  1772. int32_t old_y = yg->mouse_click_y;
  1773. if (yg->mouse_window) {
  1774. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &yg->mouse_click_x, &yg->mouse_click_y);
  1775. if (old_x != yg->mouse_click_x || old_y != yg->mouse_click_y) {
  1776. yutani_msg_buildx_window_mouse_event_alloc(response);
  1777. yutani_msg_buildx_window_mouse_event(response,yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, old_x, old_y, me->event.buttons, YUTANI_MOUSE_EVENT_DRAG);
  1778. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1779. }
  1780. }
  1781. }
  1782. }
  1783. break;
  1784. case YUTANI_MOUSE_STATE_RESIZING:
  1785. {
  1786. int32_t relative_x, relative_y;
  1787. int32_t relative_init_x, relative_init_y;
  1788. device_to_window(yg->resizing_window, yg->mouse_init_x / MOUSE_SCALE, yg->mouse_init_y / MOUSE_SCALE, &relative_init_x, &relative_init_y);
  1789. device_to_window(yg->resizing_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &relative_x, &relative_y);
  1790. int width_diff = (relative_x - relative_init_x);
  1791. int height_diff = (relative_y - relative_init_y);
  1792. mark_window(yg, yg->resizing_window);
  1793. if (yg->resizing_direction == SCALE_UP || yg->resizing_direction == SCALE_DOWN) {
  1794. width_diff = 0;
  1795. yg->resizing_offset_x = 0;
  1796. }
  1797. if (yg->resizing_direction == SCALE_LEFT || yg->resizing_direction == SCALE_RIGHT) {
  1798. height_diff = 0;
  1799. yg->resizing_offset_y = 0;
  1800. }
  1801. if (yg->resizing_direction == SCALE_LEFT ||
  1802. yg->resizing_direction == SCALE_UP_LEFT ||
  1803. yg->resizing_direction == SCALE_DOWN_LEFT) {
  1804. yg->resizing_offset_x = width_diff;
  1805. width_diff = -width_diff;
  1806. } else if (yg->resizing_direction == SCALE_RIGHT ||
  1807. yg->resizing_direction == SCALE_UP_RIGHT ||
  1808. yg->resizing_direction == SCALE_DOWN_RIGHT) {
  1809. yg->resizing_offset_x = 0;
  1810. }
  1811. if (yg->resizing_direction == SCALE_UP ||
  1812. yg->resizing_direction == SCALE_UP_LEFT ||
  1813. yg->resizing_direction == SCALE_UP_RIGHT) {
  1814. yg->resizing_offset_y = height_diff;
  1815. height_diff = -height_diff;
  1816. } else if (yg->resizing_direction == SCALE_DOWN ||
  1817. yg->resizing_direction == SCALE_DOWN_LEFT ||
  1818. yg->resizing_direction == SCALE_DOWN_RIGHT) {
  1819. yg->resizing_offset_y = 0;
  1820. }
  1821. yg->resizing_w = yg->resizing_window->width + width_diff;
  1822. yg->resizing_h = yg->resizing_window->height + height_diff;
  1823. /* Enforce logical boundaries */
  1824. if (yg->resizing_w < 1) {
  1825. yg->resizing_w = 1;
  1826. }
  1827. if (yg->resizing_h < 1) {
  1828. yg->resizing_h = 1;
  1829. }
  1830. if (yg->resizing_offset_x > yg->resizing_window->width) {
  1831. yg->resizing_offset_x = yg->resizing_window->width;
  1832. }
  1833. if (yg->resizing_offset_y > yg->resizing_window->height) {
  1834. yg->resizing_offset_y = yg->resizing_window->height;
  1835. }
  1836. mark_window(yg, yg->resizing_window);
  1837. if (!(me->event.buttons & yg->resizing_button)) {
  1838. int32_t x, y;
  1839. if (yg->resizing_window->rotation) {
  1840. /* If the window is rotated, we need to move the center to be where the new center should be, but x/y are based on the unrotated upper left corner. */
  1841. /* The center always moves by one-half the resize dimensions */
  1842. int32_t center_x, center_y;
  1843. window_to_device(yg->resizing_window, yg->resizing_offset_x + yg->resizing_w / 2, yg->resizing_offset_y + yg->resizing_h / 2, &center_x, &center_y);
  1844. x = center_x - yg->resizing_w / 2;
  1845. y = center_y - yg->resizing_h / 2;
  1846. } else {
  1847. window_to_device(yg->resizing_window, yg->resizing_offset_x, yg->resizing_offset_y, &x, &y);
  1848. }
  1849. TRACE("resize complete, now %d x %d", yg->resizing_w, yg->resizing_h);
  1850. window_move(yg, yg->resizing_window, x,y);
  1851. yutani_msg_buildx_window_resize_alloc(response);
  1852. yutani_msg_buildx_window_resize(response,YUTANI_MSG_RESIZE_OFFER, yg->resizing_window->wid, yg->resizing_w, yg->resizing_h, 0);
  1853. pex_send(yg->server, yg->resizing_window->owner, response->size, (char *)response);
  1854. yg->resizing_window = NULL;
  1855. yg->mouse_window = NULL;
  1856. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1857. }
  1858. }
  1859. break;
  1860. default:
  1861. /* XXX ? */
  1862. break;
  1863. }
  1864. }
  1865. static yutani_globals_t * _static_yg;
  1866. static void yutani_display_resize_handle(int signum) {
  1867. (void)signum;
  1868. TRACE("Display change request, one moment.");
  1869. _static_yg->resize_on_next = 1;
  1870. }
  1871. /**
  1872. * main
  1873. */
  1874. int main(int argc, char * argv[]) {
  1875. int argx = 0;
  1876. int results = parse_args(argc, argv, &argx);
  1877. if (results) return results;
  1878. yutani_globals_t * yg = malloc(sizeof(yutani_globals_t));
  1879. memset(yg, 0x00, sizeof(yutani_globals_t));
  1880. if (yutani_options.nested) {
  1881. yg->host_context = yutani_init();
  1882. yg->host_window = yutani_window_create(yg->host_context, yutani_options.nest_width, yutani_options.nest_height);
  1883. yutani_window_move(yg->host_context, yg->host_window, 50, 50);
  1884. yutani_window_advertise_icon(yg->host_context, yg->host_window, "Compositor", "compositor");
  1885. yg->backend_ctx = init_graphics_yutani_double_buffer(yg->host_window);
  1886. yg->stride = yg->backend_ctx->width * 4;
  1887. } else {
  1888. char * d = getenv("DISPLAY");
  1889. if (d && *d) {
  1890. fprintf(stderr, "DISPLAY is already set but not running nested. This is probably wrong.\n");
  1891. return 1;
  1892. }
  1893. _static_yg = yg;
  1894. signal(SIGWINEVENT, yutani_display_resize_handle);
  1895. yg->backend_ctx = init_graphics_fullscreen_double_buffer();
  1896. yg->stride = framebuffer_stride();
  1897. }
  1898. if (!yg->backend_ctx) {
  1899. free(yg);
  1900. TRACE("Failed to open framebuffer, bailing.");
  1901. return 1;
  1902. }
  1903. {
  1904. struct timeval t;
  1905. gettimeofday(&t, NULL);
  1906. yg->start_time = t.tv_sec;
  1907. yg->start_subtime = t.tv_usec;
  1908. }
  1909. yg->width = yg->backend_ctx->width;
  1910. yg->height = yg->backend_ctx->height;
  1911. draw_fill(yg->backend_ctx, rgb(110,110,110));
  1912. flip(yg->backend_ctx);
  1913. yg->backend_framebuffer = yg->backend_ctx->backbuffer;
  1914. if (yutani_options.nested) {
  1915. char * name = malloc(sizeof(char) * 64);
  1916. sprintf(name, "compositor-nest-%d", getpid());
  1917. yg->server_ident = name;
  1918. } else {
  1919. /* XXX check if this already exists? */
  1920. yg->server_ident = "compositor";
  1921. }
  1922. setenv("DISPLAY", yg->server_ident, 1);
  1923. FILE * server = pex_bind(yg->server_ident);
  1924. TRACE("pex bound? %d", server);
  1925. yg->server = server;
  1926. TRACE("Loading fonts...");
  1927. {
  1928. sprite_t _font_data[6];
  1929. load_sprite(&_font_data[0], "/usr/share/sdf_thin.bmp");
  1930. load_sprite(&_font_data[1], "/usr/share/sdf_bold.bmp");
  1931. load_sprite(&_font_data[2], "/usr/share/sdf_mono.bmp");
  1932. load_sprite(&_font_data[3], "/usr/share/sdf_mono_bold.bmp");
  1933. load_sprite(&_font_data[4], "/usr/share/sdf_mono_oblique.bmp");
  1934. load_sprite(&_font_data[5], "/usr/share/sdf_mono_bold_oblique.bmp");
  1935. TRACE(" Data loaded...");
  1936. size_t font_data_size = sizeof(unsigned int) * (1 + 6 * 3);
  1937. for (int i = 0; i < 6; ++i) {
  1938. font_data_size += 4 * _font_data[i].width * _font_data[i].height;
  1939. }
  1940. TRACE(" Size calculated: %d", font_data_size);
  1941. char tmp[100];
  1942. sprintf(tmp, "sys.%s.fonts", yg->server_ident);
  1943. size_t s = font_data_size;
  1944. char * font = (char *)syscall_shm_obtain(tmp, &s);
  1945. assert((s >= font_data_size) && "Font server failure.");
  1946. uint32_t * data = (uint32_t *)font;
  1947. data[0] = 6;
  1948. data[1] = _font_data[0].width;
  1949. data[2] = _font_data[0].height;
  1950. data[3] = (6 * 3 + 1) * sizeof(unsigned int);
  1951. memcpy(&font[data[3]], _font_data[0].bitmap, _font_data[0].width * _font_data[0].height * 4);
  1952. free(_font_data[0].bitmap);
  1953. for (int i = 1; i < 6; ++i) {
  1954. TRACE(" Loaded %d font(s)... %d %d %d", i, data[(i - 1) * 3 + 2], data[(i - 1) * 3 + 1], data[(i - 1) * 3 + 3]);
  1955. data[i * 3 + 1] = _font_data[i].width;
  1956. data[i * 3 + 2] = _font_data[i].height;
  1957. data[i * 3 + 3] = data[(i - 1) * 3 + 3] + data[(i - 1) * 3 + 2] * data[(i - 1) * 3 + 1] * 4;
  1958. memcpy(&font[data[i * 3 + 3]], _font_data[i].bitmap, _font_data[i].width * _font_data[i].height * 4);
  1959. free(_font_data[i].bitmap);
  1960. }
  1961. TRACE("Done loading fonts.");
  1962. }
  1963. TRACE("Loading sprites...");
  1964. #define MOUSE_DIR "/usr/share/cursor/"
  1965. load_sprite(&yg->mouse_sprite, MOUSE_DIR "mouse.bmp");
  1966. yg->mouse_sprite.alpha = ALPHA_EMBEDDED;
  1967. load_sprite(&yg->mouse_sprite_drag, MOUSE_DIR "drag.bmp");
  1968. yg->mouse_sprite_drag.alpha = ALPHA_EMBEDDED;
  1969. load_sprite(&yg->mouse_sprite_resize_v, MOUSE_DIR "resize-vertical.bmp");
  1970. yg->mouse_sprite_resize_v.alpha = ALPHA_EMBEDDED;
  1971. load_sprite(&yg->mouse_sprite_resize_h, MOUSE_DIR "resize-horizontal.bmp");
  1972. yg->mouse_sprite_resize_h.alpha = ALPHA_EMBEDDED;
  1973. load_sprite(&yg->mouse_sprite_resize_da, MOUSE_DIR "resize-uldr.bmp");
  1974. yg->mouse_sprite_resize_da.alpha = ALPHA_EMBEDDED;
  1975. load_sprite(&yg->mouse_sprite_resize_db, MOUSE_DIR "resize-dlur.bmp");
  1976. yg->mouse_sprite_resize_db.alpha = ALPHA_EMBEDDED;
  1977. TRACE("Done.");
  1978. TRACE("Initializing variables...");
  1979. yg->last_mouse_x = 0;
  1980. yg->last_mouse_y = 0;
  1981. yg->mouse_x = yg->width * MOUSE_SCALE / 2;
  1982. yg->mouse_y = yg->height * MOUSE_SCALE / 2;
  1983. yg->windows = list_create();
  1984. yg->wids_to_windows = hashmap_create_int(10);
  1985. yg->key_binds = hashmap_create_int(10);
  1986. yg->clients_to_windows = hashmap_create_int(10);
  1987. yg->mid_zs = list_create();
  1988. yg->window_subscribers = list_create();
  1989. yg->last_mouse_buttons = 0;
  1990. TRACE("Done.");
  1991. yutani_clip_init(yg);
  1992. pthread_t render_thread;
  1993. TRACE("Starting render thread.");
  1994. pthread_create(&render_thread, NULL, redraw, yg);
  1995. if (!fork()) {
  1996. if (argx < argc) {
  1997. TRACE("Starting alternate startup app: %s", argv[argx]);
  1998. execvp(argv[argx], &argv[argx]);
  1999. } else {
  2000. TRACE("Starting application");
  2001. char * args[] = {"/bin/session", NULL};
  2002. execvp(args[0], args);
  2003. TRACE("Failed to start app?");
  2004. }
  2005. }
  2006. int fds[4];
  2007. int mfd = -1;
  2008. int kfd = -1;
  2009. int amfd = -1;
  2010. int vmmouse = 0;
  2011. mouse_device_packet_t packet;
  2012. key_event_t event;
  2013. key_event_state_t state = {0};
  2014. fds[0] = fileno(server);
  2015. if (yutani_options.nested) {
  2016. fds[1] = fileno(yg->host_context->sock);
  2017. } else {
  2018. mfd = open("/dev/mouse", O_RDONLY);
  2019. kfd = open("/dev/kbd", O_RDONLY);
  2020. amfd = open("/dev/absmouse", O_RDONLY);
  2021. if (amfd < 0) {
  2022. amfd = open("/dev/vmmouse", O_RDONLY);
  2023. vmmouse = 1;
  2024. }
  2025. yg->vbox_rects = open("/dev/vboxrects", O_WRONLY);
  2026. yg->vbox_pointer = open("/dev/vboxpointer", O_WRONLY);
  2027. fds[1] = mfd;
  2028. fds[2] = kfd;
  2029. fds[3] = amfd;
  2030. }
  2031. while (1) {
  2032. if (yutani_options.nested) {
  2033. int index = fswait(2, fds);
  2034. if (index == 1) {
  2035. yutani_msg_t * m = yutani_poll(yg->host_context);
  2036. if (m) {
  2037. switch (m->type) {
  2038. case YUTANI_MSG_KEY_EVENT:
  2039. {
  2040. struct yutani_msg_key_event * ke = (void*)m->data;
  2041. yutani_msg_buildx_key_event_alloc(m_);
  2042. yutani_msg_buildx_key_event(m_, 0, &ke->event, &ke->state);
  2043. handle_key_event(yg, (struct yutani_msg_key_event *)m_->data);
  2044. }
  2045. break;
  2046. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  2047. {
  2048. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  2049. mouse_device_packet_t packet;
  2050. packet.buttons = me->buttons;
  2051. packet.x_difference = me->new_x;
  2052. packet.y_difference = me->new_y;
  2053. yg->last_mouse_buttons = packet.buttons;
  2054. yutani_msg_buildx_mouse_event_alloc(m_);
  2055. yutani_msg_buildx_mouse_event(m_, 0, &packet, YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE);
  2056. handle_mouse_event(yg, (struct yutani_msg_mouse_event *)m_->data);
  2057. }
  2058. break;
  2059. case YUTANI_MSG_RESIZE_OFFER:
  2060. {
  2061. struct yutani_msg_window_resize * wr = (void*)m->data;
  2062. TRACE("Resize request from host compositor for size %dx%d", wr->width, wr->height);
  2063. yutani_window_resize_accept(yg->host_context, yg->host_window, wr->width, wr->height);
  2064. yg->resize_on_next = 1;
  2065. }
  2066. break;
  2067. case YUTANI_MSG_WINDOW_CLOSE:
  2068. case YUTANI_MSG_SESSION_END:
  2069. {
  2070. TRACE("Host session ended. Should exit.");
  2071. yutani_msg_buildx_session_end_alloc(response);
  2072. yutani_msg_buildx_session_end(response);
  2073. pex_broadcast(server, response->size, (char *)response);
  2074. yg->server = NULL;
  2075. kill(render_thread.id, SIGINT);
  2076. exit(0);
  2077. }
  2078. break;
  2079. default:
  2080. break;
  2081. }
  2082. }
  2083. free(m);
  2084. continue;
  2085. }
  2086. } else {
  2087. int index = fswait(amfd == -1 ? 3 : 4, fds);
  2088. if (index == 2) {
  2089. unsigned char buf[1];
  2090. int r = read(kfd, buf, 1);
  2091. if (r > 0) {
  2092. kbd_scancode(&state, buf[0], &event);
  2093. yutani_msg_buildx_key_event_alloc(m);
  2094. yutani_msg_buildx_key_event(m,0, &event, &state);
  2095. handle_key_event(yg, (struct yutani_msg_key_event *)m->data);
  2096. }
  2097. continue;
  2098. } else if (index == 1) {
  2099. int r = read(mfd, (char *)&packet, sizeof(mouse_device_packet_t));
  2100. if (r > 0) {
  2101. yg->last_mouse_buttons = packet.buttons;
  2102. yutani_msg_buildx_mouse_event_alloc(m);
  2103. yutani_msg_buildx_mouse_event(m,0, &packet, YUTANI_MOUSE_EVENT_TYPE_RELATIVE);
  2104. handle_mouse_event(yg, (struct yutani_msg_mouse_event *)m->data);
  2105. }
  2106. continue;
  2107. } else if (index == 3) {
  2108. int r = read(amfd, (char *)&packet, sizeof(mouse_device_packet_t));
  2109. if (r > 0) {
  2110. if (!vmmouse) {
  2111. packet.buttons = yg->last_mouse_buttons & 0xF;
  2112. } else {
  2113. yg->last_mouse_buttons = packet.buttons;
  2114. }
  2115. yutani_msg_buildx_mouse_event_alloc(m);
  2116. yutani_msg_buildx_mouse_event(m,0, &packet, YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE);
  2117. handle_mouse_event(yg, (struct yutani_msg_mouse_event *)m->data);
  2118. }
  2119. continue;
  2120. }
  2121. }
  2122. pex_packet_t * p = calloc(PACKET_SIZE, 1);
  2123. pex_listen(server, p);
  2124. yutani_msg_t * m = (yutani_msg_t *)p->data;
  2125. if (p->size == 0) {
  2126. /* Connection closed for client */
  2127. TRACE("Connection closed for client %x", p->source);
  2128. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)p->source);
  2129. if (client_list) {
  2130. foreach(node, client_list) {
  2131. yutani_server_window_t * win = node->value;
  2132. TRACE("Killing window %d", win->wid);
  2133. window_mark_for_close(yg, win);
  2134. }
  2135. hashmap_remove(yg->clients_to_windows, (void *)p->source);
  2136. list_free(client_list);
  2137. free(client_list);
  2138. }
  2139. if (hashmap_is_empty(yg->clients_to_windows)) {
  2140. TRACE("Last compositor client disconnected, exiting.");
  2141. yg->server = NULL;
  2142. kill(render_thread.id, SIGINT);
  2143. exit(0);
  2144. }
  2145. free(p);
  2146. continue;
  2147. }
  2148. if (m->magic != YUTANI_MSG__MAGIC) {
  2149. TRACE("Message has bad magic. (Should eject client, but will instead skip this message.) 0x%x", m->magic);
  2150. free(p);
  2151. continue;
  2152. }
  2153. switch(m->type) {
  2154. case YUTANI_MSG_HELLO:
  2155. {
  2156. TRACE("And hello to you, %08x!", p->source);
  2157. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)p->source);
  2158. if (!client_list) {
  2159. TRACE("Client is new: %x", p->source);
  2160. client_list = list_create();
  2161. hashmap_set(yg->clients_to_windows, (void *)p->source, client_list);
  2162. }
  2163. yutani_msg_buildx_welcome_alloc(response);
  2164. yutani_msg_buildx_welcome(response,yg->width, yg->height);
  2165. pex_send(server, p->source, response->size, (char *)response);
  2166. }
  2167. break;
  2168. case YUTANI_MSG_WINDOW_NEW:
  2169. case YUTANI_MSG_WINDOW_NEW_FLAGS:
  2170. {
  2171. struct yutani_msg_window_new_flags * wn = (void *)m->data;
  2172. TRACE("Client %08x requested a new window (%dx%d).", p->source, wn->width, wn->height);
  2173. yutani_server_window_t * w = server_window_create(yg, wn->width, wn->height, p->source, m->type != YUTANI_MSG_WINDOW_NEW ? wn->flags : 0);
  2174. yutani_msg_buildx_window_init_alloc(response);
  2175. yutani_msg_buildx_window_init(response,w->wid, w->width, w->height, w->bufid);
  2176. pex_send(server, p->source, response->size, (char *)response);
  2177. if (!(w->server_flags & YUTANI_WINDOW_FLAG_NO_STEAL_FOCUS)) {
  2178. set_focused_window(yg, w);
  2179. }
  2180. notify_subscribers(yg);
  2181. }
  2182. break;
  2183. case YUTANI_MSG_FLIP:
  2184. {
  2185. struct yutani_msg_flip * wf = (void *)m->data;
  2186. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wf->wid);
  2187. if (w) {
  2188. mark_window(yg, w);
  2189. }
  2190. }
  2191. break;
  2192. case YUTANI_MSG_FLIP_REGION:
  2193. {
  2194. struct yutani_msg_flip_region * wf = (void *)m->data;
  2195. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wf->wid);
  2196. if (w) {
  2197. mark_window_relative(yg, w, wf->x, wf->y, wf->width, wf->height);
  2198. }
  2199. }
  2200. break;
  2201. case YUTANI_MSG_KEY_EVENT:
  2202. {
  2203. /* XXX Verify this is from a valid device client */
  2204. struct yutani_msg_key_event * ke = (void *)m->data;
  2205. handle_key_event(yg, ke);
  2206. }
  2207. break;
  2208. case YUTANI_MSG_MOUSE_EVENT:
  2209. {
  2210. /* XXX Verify this is from a valid device client */
  2211. struct yutani_msg_mouse_event * me = (void *)m->data;
  2212. handle_mouse_event(yg, me);
  2213. }
  2214. break;
  2215. case YUTANI_MSG_WINDOW_MOVE:
  2216. {
  2217. struct yutani_msg_window_move * wm = (void *)m->data;
  2218. //TRACE("%08x wanted to move window %d to %d, %d", p->source, wm->wid, (int)wm->x, (int)wm->y);
  2219. if (wm->x > (int)yg->width + 100 || wm->x < -(int)yg->width || wm->y > (int)yg->height + 100 || wm->y < -(int)yg->height) {
  2220. TRACE("Refusing to move window to these coordinates.");
  2221. break;
  2222. }
  2223. yutani_server_window_t * win = hashmap_get(yg->wids_to_windows, (void*)wm->wid);
  2224. if (win) {
  2225. window_move(yg, win, wm->x, wm->y);
  2226. } else {
  2227. TRACE("%08x wanted to move window %d, but I can't find it?", p->source, wm->wid);
  2228. }
  2229. }
  2230. break;
  2231. case YUTANI_MSG_WINDOW_CLOSE:
  2232. {
  2233. struct yutani_msg_window_close * wc = (void *)m->data;
  2234. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wc->wid);
  2235. if (w) {
  2236. window_mark_for_close(yg, w);
  2237. window_remove_from_client(yg, w);
  2238. }
  2239. }
  2240. break;
  2241. case YUTANI_MSG_WINDOW_STACK:
  2242. {
  2243. struct yutani_msg_window_stack * ws = (void *)m->data;
  2244. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)ws->wid);
  2245. if (w) {
  2246. reorder_window(yg, w, ws->z);
  2247. }
  2248. }
  2249. break;
  2250. case YUTANI_MSG_RESIZE_REQUEST:
  2251. {
  2252. struct yutani_msg_window_resize * wr = (void *)m->data;
  2253. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  2254. if (w) {
  2255. yutani_msg_buildx_window_resize_alloc(response);
  2256. yutani_msg_buildx_window_resize(response,YUTANI_MSG_RESIZE_OFFER, w->wid, wr->width, wr->height, 0);
  2257. pex_send(server, p->source, response->size, (char *)response);
  2258. }
  2259. }
  2260. break;
  2261. case YUTANI_MSG_RESIZE_OFFER:
  2262. {
  2263. struct yutani_msg_window_resize * wr = (void *)m->data;
  2264. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  2265. if (w) {
  2266. yutani_msg_buildx_window_resize_alloc(response);
  2267. yutani_msg_buildx_window_resize(response,YUTANI_MSG_RESIZE_OFFER, w->wid, wr->width, wr->height, 0);
  2268. pex_send(server, p->source, response->size, (char *)response);
  2269. }
  2270. }
  2271. break;
  2272. case YUTANI_MSG_RESIZE_ACCEPT:
  2273. {
  2274. struct yutani_msg_window_resize * wr = (void *)m->data;
  2275. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  2276. if (w) {
  2277. uint32_t newbufid = server_window_resize(yg, w, wr->width, wr->height);
  2278. yutani_msg_buildx_window_resize_alloc(response);
  2279. yutani_msg_buildx_window_resize(response,YUTANI_MSG_RESIZE_BUFID, w->wid, wr->width, wr->height, newbufid);
  2280. pex_send(server, p->source, response->size, (char *)response);
  2281. }
  2282. }
  2283. break;
  2284. case YUTANI_MSG_RESIZE_DONE:
  2285. {
  2286. struct yutani_msg_window_resize * wr = (void *)m->data;
  2287. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  2288. if (w) {
  2289. server_window_resize_finish(yg, w, wr->width, wr->height);
  2290. }
  2291. }
  2292. break;
  2293. case YUTANI_MSG_QUERY_WINDOWS:
  2294. {
  2295. yutani_query_result(yg, p->source, yg->bottom_z);
  2296. foreach (node, yg->mid_zs) {
  2297. yutani_query_result(yg, p->source, node->value);
  2298. }
  2299. yutani_query_result(yg, p->source, yg->top_z);
  2300. yutani_msg_buildx_window_advertise_alloc(response, 0);
  2301. yutani_msg_buildx_window_advertise(response,0, 0, NULL, 0, NULL);
  2302. pex_send(server, p->source, response->size, (char *)response);
  2303. }
  2304. break;
  2305. case YUTANI_MSG_SUBSCRIBE:
  2306. {
  2307. foreach(node, yg->window_subscribers) {
  2308. if ((uint32_t)node->value == p->source) {
  2309. break;
  2310. }
  2311. }
  2312. list_insert(yg->window_subscribers, (void*)p->source);
  2313. }
  2314. break;
  2315. case YUTANI_MSG_UNSUBSCRIBE:
  2316. {
  2317. node_t * node = list_find(yg->window_subscribers, (void*)p->source);
  2318. if (node) {
  2319. list_delete(yg->window_subscribers, node);
  2320. }
  2321. }
  2322. break;
  2323. case YUTANI_MSG_WINDOW_ADVERTISE:
  2324. {
  2325. struct yutani_msg_window_advertise * wa = (void *)m->data;
  2326. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2327. if (w) {
  2328. if (w->client_strings) free(w->client_strings);
  2329. for (int i = 0; i < 5; ++i) {
  2330. w->client_offsets[i] = wa->offsets[i];
  2331. }
  2332. w->client_flags = wa->flags;
  2333. w->client_length = wa->size;
  2334. w->client_strings = malloc(wa->size);
  2335. memcpy(w->client_strings, wa->strings, wa->size);
  2336. notify_subscribers(yg);
  2337. }
  2338. }
  2339. break;
  2340. case YUTANI_MSG_SESSION_END:
  2341. {
  2342. yutani_msg_buildx_session_end_alloc(response);
  2343. yutani_msg_buildx_session_end(response);
  2344. pex_broadcast(server, response->size, (char *)response);
  2345. }
  2346. break;
  2347. case YUTANI_MSG_WINDOW_FOCUS:
  2348. {
  2349. struct yutani_msg_window_focus * wa = (void *)m->data;
  2350. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2351. if (w) {
  2352. set_focused_window(yg, w);
  2353. }
  2354. }
  2355. break;
  2356. case YUTANI_MSG_KEY_BIND:
  2357. {
  2358. struct yutani_msg_key_bind * wa = (void *)m->data;
  2359. add_key_bind(yg, wa, p->source);
  2360. }
  2361. break;
  2362. case YUTANI_MSG_WINDOW_DRAG_START:
  2363. {
  2364. struct yutani_msg_window_drag_start * wa = (void *)m->data;
  2365. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2366. if (w) {
  2367. /* Start dragging */
  2368. mouse_start_drag(yg, w);
  2369. }
  2370. }
  2371. break;
  2372. case YUTANI_MSG_WINDOW_UPDATE_SHAPE:
  2373. {
  2374. struct yutani_msg_window_update_shape * wa = (void *)m->data;
  2375. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2376. if (w) {
  2377. /* Set shape parameter */
  2378. server_window_update_shape(yg, w, wa->set_shape);
  2379. }
  2380. }
  2381. break;
  2382. case YUTANI_MSG_WINDOW_WARP_MOUSE:
  2383. {
  2384. struct yutani_msg_window_warp_mouse * wa = (void *)m->data;
  2385. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2386. if (w) {
  2387. if (yg->focused_window == w) {
  2388. int32_t x, y;
  2389. window_to_device(w, wa->x, wa->y, &x, &y);
  2390. struct yutani_msg_mouse_event me;
  2391. me.event.x_difference = x;
  2392. me.event.y_difference = y;
  2393. me.event.buttons = 0;
  2394. me.type = YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE;
  2395. me.wid = wa->wid;
  2396. handle_mouse_event(yg, &me);
  2397. }
  2398. }
  2399. }
  2400. break;
  2401. case YUTANI_MSG_WINDOW_SHOW_MOUSE:
  2402. {
  2403. struct yutani_msg_window_show_mouse * wa = (void *)m->data;
  2404. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2405. if (w) {
  2406. if (wa->show_mouse == -1) {
  2407. w->show_mouse = w->default_mouse;
  2408. } else if (wa->show_mouse < 2) {
  2409. w->default_mouse = wa->show_mouse;
  2410. w->show_mouse = wa->show_mouse;
  2411. } else {
  2412. w->show_mouse = wa->show_mouse;
  2413. }
  2414. if (yg->focused_window == w) {
  2415. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  2416. }
  2417. }
  2418. }
  2419. break;
  2420. case YUTANI_MSG_WINDOW_RESIZE_START:
  2421. {
  2422. struct yutani_msg_window_resize_start * wa = (void *)m->data;
  2423. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2424. if (w) {
  2425. if (yg->focused_window == w && !yg->resizing_window) {
  2426. yg->resizing_window = w;
  2427. yg->resizing_button = YUTANI_MOUSE_BUTTON_LEFT; /* XXX Uh, what if we used something else */
  2428. mouse_start_resize(yg, wa->direction);
  2429. }
  2430. }
  2431. }
  2432. break;
  2433. case YUTANI_MSG_SPECIAL_REQUEST:
  2434. {
  2435. struct yutani_msg_special_request * sr = (void *)m->data;
  2436. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)sr->wid);
  2437. switch (sr->request) {
  2438. case YUTANI_SPECIAL_REQUEST_MAXIMIZE:
  2439. if (w) {
  2440. if (w->tiled) {
  2441. window_untile(yg,w);
  2442. window_move(yg,w,w->untiled_left,w->untiled_top);
  2443. } else {
  2444. window_tile(yg, w, 1, 1, 0, 0);
  2445. }
  2446. }
  2447. break;
  2448. case YUTANI_SPECIAL_REQUEST_PLEASE_CLOSE:
  2449. if (w) {
  2450. yutani_msg_buildx_window_close_alloc(response);
  2451. yutani_msg_buildx_window_close(response, w->wid);
  2452. pex_send(yg->server, w->owner, response->size, (char *)response);
  2453. }
  2454. break;
  2455. case YUTANI_SPECIAL_REQUEST_CLIPBOARD:
  2456. {
  2457. yutani_msg_buildx_clipboard_alloc(response, yg->clipboard_size);
  2458. yutani_msg_buildx_clipboard(response, yg->clipboard);
  2459. pex_send(server, p->source, response->size, (char *)response);
  2460. }
  2461. break;
  2462. default:
  2463. TRACE("Unknown special request type: 0x%x", sr->request);
  2464. break;
  2465. }
  2466. }
  2467. break;
  2468. case YUTANI_MSG_CLIPBOARD:
  2469. {
  2470. struct yutani_msg_clipboard * cb = (void *)m->data;
  2471. yg->clipboard_size = min(cb->size, 511);
  2472. memcpy(yg->clipboard, cb->content, yg->clipboard_size);
  2473. yg->clipboard[yg->clipboard_size] = '\0';
  2474. TRACE("Copied text to clipbard (size=%d)", yg->clipboard_size);
  2475. }
  2476. break;
  2477. default:
  2478. {
  2479. TRACE("Unknown type: 0x%8x", m->type);
  2480. }
  2481. break;
  2482. }
  2483. free(p);
  2484. }
  2485. return 0;
  2486. }