compositor.c 84 KB

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