compositor.c 83 KB

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