compositor.c 85 KB

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