compositor.c 83 KB

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