compositor.c 83 KB

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