XDisplay.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8. #include "XDisplayP.h"
  9. #include "WonX.h"
  10. #include "WonXDisplay.h"
  11. #include "etc.h"
  12. /*****************************************************************************/
  13. /* メンバ関数の定義 */
  14. /*****************************************************************************/
  15. Display * XDisplay_GetDisplay(XDisplay d) { return (d->display); }
  16. Colormap XDisplay_GetColormap(XDisplay d) { return (d->colormap); }
  17. XColorGCDatabase XDisplay_GetColorGCDatabase(XDisplay x_display)
  18. { return (x_display->color_gc_database); }
  19. unsigned int XDisplay_GetKeyPress(XDisplay d) { return (d->key_press); }
  20. int XDisplay_GetLCDDrawLevel(XDisplay d) { return (d->lcd_draw_level); }
  21. int XDisplay_SetLCDDrawLevel(XDisplay d, int level)
  22. {
  23. d->lcd_draw_level = level;
  24. WonXDisplay_Flush(WonX_GetWonXDisplay());
  25. return (d->lcd_draw_level);
  26. }
  27. int XDisplay_LCDDrawLevelDown(XDisplay d)
  28. { return (XDisplay_SetLCDDrawLevel(d, d->lcd_draw_level - 1)); }
  29. int XDisplay_LCDDrawLevelUp( XDisplay d)
  30. { return (XDisplay_SetLCDDrawLevel(d, d->lcd_draw_level + 1)); }
  31. int XDisplay_GetColorMapPrint(XDisplay d) {return (d->color_map_print); }
  32. int XDisplay_GetPalettePrint(XDisplay d) {return (d->palette_print); }
  33. int XDisplay_GetCharacterPrint(XDisplay d) {return (d->character_print); }
  34. int XDisplay_GetSpritePrint(XDisplay d) {return (d->sprite_print); }
  35. int XDisplay_SetColorMapPrint(XDisplay d, int f)
  36. { return (d->color_map_print = f); }
  37. int XDisplay_SetPalettePrint(XDisplay d, int f)
  38. { return (d->palette_print = f); }
  39. int XDisplay_SetCharacterPrint(XDisplay d, int f)
  40. { return(d->character_print = f); }
  41. int XDisplay_SetSpritePrint(XDisplay d, int f)
  42. { return (d->sprite_print = f); }
  43. /*****************************************************************************/
  44. /* 内部で使用する関数などの定義 */
  45. /*****************************************************************************/
  46. #if 0
  47. static XrmOptionDescRec options[] = {};
  48. #endif
  49. static Atom wm_delete_window;
  50. static void die(Widget w)
  51. {
  52. kill(getpid(), SIGINT);
  53. }
  54. static void quit(Widget w, XEvent * event, String * params, Cardinal * num)
  55. {
  56. die(w);
  57. }
  58. static void wm_protocols_proc(Widget w, XEvent * event, String * params,
  59. Cardinal * num)
  60. {
  61. if ((event->type == ClientMessage) &&
  62. (event->xclient.data.l[0] != wm_delete_window)) {
  63. XBell(XtDisplay(w), 0);
  64. } else {
  65. die(w);
  66. }
  67. }
  68. static void iconify(Widget w, XEvent * event, String * params, Cardinal * num)
  69. {
  70. XIconifyWindow(XtDisplay(w), XtWindow(w), DefaultScreen(XtDisplay(w)));
  71. }
  72. static void sleep_10(Widget w, XEvent * event, String * params, Cardinal * num)
  73. {
  74. time_t old_t;
  75. time_t t;
  76. int i;
  77. /* UNIXTimer.c 内部で SIGALRM を使用しているので,sleep() は使用できない */
  78. #if 0
  79. sleep(10);
  80. #else
  81. for (i = 0; i < 10; i++) {
  82. time(&t);
  83. old_t = t;
  84. while (t == old_t)
  85. time(&t);
  86. }
  87. #endif
  88. }
  89. static XtActionsRec actions[] = {
  90. {"quit", quit},
  91. {"wm_protocols_proc", wm_protocols_proc},
  92. {"iconify", iconify},
  93. {"pause", sleep_10}
  94. };
  95. static char * translations =
  96. "<Message>WM_PROTOCOLS: wm_protocols_proc()\n"
  97. "None<Key>F11: pause()\n"
  98. "Ctrl<Key>i: iconify()\n"
  99. "Ctrl<Key>c: quit()\n"
  100. "None<Key>F12: quit()\n"
  101. "None<Key>q: quit()";
  102. /*===========================================================================*/
  103. /* 色名からピクセル値を取得する */
  104. /*===========================================================================*/
  105. #ifdef USE_X_FONT
  106. static unsigned long XDisplay_GetPixelFromColorName(XDisplay x_display,
  107. char * color_name)
  108. {
  109. XColor c0, c1;
  110. XAllocNamedColor(x_display->display, x_display->colormap, color_name,
  111. &c0, &c1);
  112. return (c0.pixel);
  113. }
  114. #endif
  115. /*===========================================================================*/
  116. /* イベントハンドラ */
  117. /*===========================================================================*/
  118. /*---------------------------------------------------------------------------*/
  119. /* イクスポーズ */
  120. /*---------------------------------------------------------------------------*/
  121. static void ExposeHandler(Widget w, XtPointer p, XEvent * event,
  122. Boolean * dispatch)
  123. {
  124. XDisplay x_display = (XDisplay)p;
  125. XCopyArea(x_display->display, x_display->lcd_pixmap,
  126. x_display->lcd_window, x_display->copy_gc,
  127. 0, 0, x_display->width, x_display->height, 0, 0);
  128. return;
  129. }
  130. /*---------------------------------------------------------------------------*/
  131. /* キーの押下 */
  132. /*---------------------------------------------------------------------------*/
  133. static void KeyHandler(Widget w, XtPointer p, XEvent * event,
  134. Boolean * dispatch)
  135. {
  136. XDisplay x_display = (XDisplay)p;
  137. KeySym key_sym;
  138. int press = 0;
  139. if ((event->type == KeyPress) || (event->type == KeyRelease)) {
  140. key_sym = XKeycodeToKeysym(x_display->display, event->xkey.keycode, 0);
  141. switch (key_sym) {
  142. /* WonderSwan用 */
  143. case XK_Up : press = KEY_UP1; printf("key : Up1 " ); break;
  144. case XK_Right : press = KEY_RIGHT1; printf("key : Right1 "); break;
  145. case XK_Down : press = KEY_DOWN1; printf("key : Down1 " ); break;
  146. case XK_Left : press = KEY_LEFT1; printf("key : Left1 " ); break;
  147. case XK_i : press = KEY_UP2; printf("key : Up2 " ); break;
  148. case XK_l : press = KEY_RIGHT2; printf("key : Right2 "); break;
  149. case XK_k : press = KEY_DOWN2; printf("key : Down2 " ); break;
  150. case XK_j : press = KEY_LEFT2; printf("key : Left2 " ); break;
  151. case XK_s : press = KEY_START; printf("key : Start " ); break;
  152. case XK_space : press = KEY_A; printf("key : A " ); break;
  153. case XK_Shift_L : press = KEY_B; printf("key : B " ); break;
  154. default : press = 0; break;
  155. }
  156. if (press) {
  157. if (event->type == KeyPress) {
  158. x_display->key_press |= press;
  159. printf("Pressed\n");
  160. } else {
  161. x_display->key_press &= ~press;
  162. printf("Released\n");
  163. }
  164. }
  165. /* WonX 操作用 */
  166. if (event->type == KeyPress) {
  167. switch (key_sym) {
  168. /* 表示モード変更 */
  169. case XK_F9 :
  170. case XK_F10 :
  171. if (key_sym == XK_F9) {
  172. printf("key : F9 Pressed\n");
  173. printf("lcd_draw_level : down (%d", x_display->lcd_draw_level);
  174. (x_display->lcd_draw_level)--;
  175. printf(" -> %d)\n", x_display->lcd_draw_level);
  176. } else {
  177. printf("key : F10 Pressed\n");
  178. printf("lcd_draw_level : up (%d", x_display->lcd_draw_level);
  179. (x_display->lcd_draw_level)++;
  180. printf(" -> %d)\n", x_display->lcd_draw_level);
  181. }
  182. if (x_display->lcd_draw_level > 0) {
  183. #if 1
  184. /*
  185. * WonXDisplay_Flush() を実行すると,XDisplay_Flush() が呼ばれて
  186. * イベントのディスパッチが行われてしまうため,イベントの処理中に
  187. * 別のイベントのディスパッチを行うことになってしまう.
  188. * X サーバが,XtDispatchEvent() によるイベントのディスパッチ時に,
  189. * イベントキューからイベントを出してからイベントの処理をするような
  190. * 実装になっている場合には問題ないが,イベントの処理をした後に
  191. * キューからイベントを取り除くような実装になっている場合には,
  192. * XtDispatchEvent() によるイベントのディスパッチ中に再び
  193. * XtDispatchEvent() が呼ばれたときに,イベントがキューから
  194. * 取り除かれることがなく,イベント処理の無限ループに陥ってしまう
  195. * 可能性があるかもしれない.
  196. * (ちなみに XFree86 では大丈夫なようであるが,いちおう)
  197. * このため WonXDisplay_Flush() は呼び出さずに,
  198. * WonXDisplay_DrawLCDWindow() でウインドウの描画のみを行うように
  199. * WonX-2.2 で修正した.
  200. */
  201. WonXDisplay_DrawLCDWindow(WonX_GetWonXDisplay());
  202. #elif 1
  203. WonXDisplay_Flush(WonX_GetWonXDisplay());
  204. #else
  205. ExposeHandler(w, p, event, dispatch);
  206. #endif
  207. }
  208. break;
  209. /* データのダンプ操作 */
  210. case XK_F1 :
  211. printf("key : F1 Pressed\n");
  212. x_display->color_map_print = 1;
  213. printf("dump : color_map\n");
  214. break;
  215. case XK_F2 :
  216. printf("key : F2 Pressed\n");
  217. x_display->palette_print = 1;
  218. printf("dump : palette\n");
  219. break;
  220. case XK_F3 :
  221. printf("key : F3 Pressed\n");
  222. x_display->character_print = 1;
  223. printf("dump : character\n");
  224. break;
  225. case XK_F4 :
  226. printf("key : F4 Pressed\n");
  227. x_display->sprite_print = 1;
  228. printf("dump : sprite\n");
  229. break;
  230. }
  231. }
  232. }
  233. return;
  234. }
  235. /*---------------------------------------------------------------------------*/
  236. /* マウスがウインドウを離れたら,キーを初期化する */
  237. /*---------------------------------------------------------------------------*/
  238. static void LeaveWindowHandler(Widget w, XtPointer p, XEvent * event,
  239. Boolean * dispatch)
  240. {
  241. XDisplay x_display = (XDisplay)p;
  242. x_display->key_press = 0;
  243. return;
  244. }
  245. /*===========================================================================*/
  246. /* オブジェクトの生成と消去 */
  247. /*===========================================================================*/
  248. /*---------------------------------------------------------------------------*/
  249. /* オブジェクトの生成 */
  250. /*---------------------------------------------------------------------------*/
  251. XDisplay XDisplay_Create(int width, int height)
  252. {
  253. XDisplay x_display;
  254. int argc = 0;
  255. char ** argv = NULL;
  256. XColor color;
  257. char * color_name[] = {"white", "gray84", "gray78", "gray72",
  258. "gray66", "gray60", "gray54", "gray48",
  259. "gray42", "gray36", "gray30", "gray24",
  260. "gray18", "gray12", "gray6", "black"};
  261. int i;
  262. x_display = (XDisplay)malloc(sizeof(_XDisplay));
  263. if (x_display == NULL)
  264. WonX_Error("XDisplay_Create", "Cannot allocate memory.");
  265. x_display->width = width;
  266. x_display->height = height;
  267. x_display->toplevel = XtAppInitialize(&(x_display->app_context),
  268. "WonX",
  269. NULL, 0, &argc, argv, NULL, NULL, 0);
  270. XtVaSetValues(x_display->toplevel, XtNinput, True, NULL);
  271. XtVaSetValues(x_display->toplevel, XtNtitle, "WonX", NULL);
  272. XtVaSetValues(x_display->toplevel, XtNwidth , x_display->width , NULL);
  273. XtVaSetValues(x_display->toplevel, XtNminWidth , x_display->width , NULL);
  274. XtVaSetValues(x_display->toplevel, XtNmaxWidth , x_display->width , NULL);
  275. XtVaSetValues(x_display->toplevel, XtNheight , x_display->height, NULL);
  276. XtVaSetValues(x_display->toplevel, XtNminHeight, x_display->height, NULL);
  277. XtVaSetValues(x_display->toplevel, XtNmaxHeight, x_display->height, NULL);
  278. x_display->key_press = 0;
  279. x_display->lcd_draw_level = 1;
  280. XtRealizeWidget(x_display->toplevel);
  281. while (!XtIsRealized(x_display->toplevel)) { /* None */ }
  282. x_display->display = XtDisplay(x_display->toplevel);
  283. x_display->root_window = DefaultRootWindow(x_display->display);
  284. x_display->lcd_window = XtWindow(x_display->toplevel);
  285. x_display->colormap = DefaultColormap(x_display->display,
  286. DefaultScreen(x_display->display));
  287. x_display->depth = DefaultDepth(x_display->display,
  288. DefaultScreen(x_display->display));
  289. x_display->lcd_pixmap = XCreatePixmap(x_display->display,
  290. x_display->lcd_window,
  291. x_display->width,
  292. x_display->height,
  293. x_display->depth);
  294. x_display->copy_gc = XCreateGC(x_display->display, x_display->lcd_window,
  295. 0, 0);
  296. XSetFunction(x_display->display, x_display->copy_gc, GXcopy);
  297. for (i = 0; i < 16; i++) {
  298. XParseColor(x_display->display, x_display->colormap,
  299. color_name[i], &color); /* 色の名前 → RGB値*/
  300. XAllocColor(x_display->display, x_display->colormap,
  301. &color); /* 色を確保し,ピクセル値を得る */
  302. x_display->color_gc[i] = XCreateGC(x_display->display,
  303. x_display->lcd_window, 0, 0);
  304. XSetForeground(x_display->display, x_display->color_gc[i], color.pixel);
  305. XSetFunction(x_display->display, x_display->color_gc[i], GXcopy);
  306. }
  307. #ifdef USE_X_FONT
  308. /* フォントの確保 */
  309. x_display->font = XLoadFont(x_display->display, "8x16");
  310. x_display->font_gc = XCreateGC(x_display->display,
  311. x_display->lcd_window, 0, 0);
  312. XSetFont(x_display->display, x_display->font_gc, x_display->font);
  313. XSetFunction(x_display->display, x_display->font_gc, GXcopy);
  314. XSetForeground(x_display->display, x_display->font_gc,
  315. XDisplay_GetPixelFromColorName(x_display, "white"));
  316. XSetBackground(x_display->display, x_display->font_gc,
  317. XDisplay_GetPixelFromColorName(x_display, "black"));
  318. #endif
  319. /* GCのデータベース初期化 */
  320. /*
  321. * XColorGCDatabase_Create() の後に XCreateGC() で他の GC の生成を行うと,
  322. * なぜか XFreeGC() するときに Segmentation Fault を起こす
  323. * (FreeBSD の場合の話.原因不明.他の OS (ていうか正確にはXライブラリ)
  324. * では試してない)ので,XColorGCDatabase_Create() はすべての GC を
  325. * 作成した後に行う必要がある.
  326. */
  327. x_display->color_gc_database =
  328. XColorGCDatabase_Create(x_display,
  329. 0, /* studying_flag */
  330. 1, /* cache_flag */
  331. 3, /* cache_size */
  332. 256, /* hash_number */
  333. "black", /* background_color */
  334. 16 /* gradation */);
  335. XFillRectangle(x_display->display, x_display->lcd_window,
  336. x_display->color_gc[0],
  337. 0, 0, x_display->width, x_display->height);
  338. /* イベントハンドラの登録 */
  339. XtAddEventHandler(x_display->toplevel, KeyPressMask | KeyReleaseMask,
  340. False, KeyHandler, x_display);
  341. XtAddEventHandler(x_display->toplevel, ExposureMask,
  342. False, ExposeHandler, x_display);
  343. XtAddEventHandler(x_display->toplevel, LeaveWindowMask | FocusChangeMask,
  344. False, LeaveWindowHandler, x_display);
  345. /* アイコンの設定 */
  346. #if 0
  347. XtVaSetValues(x_display->toplevel, XtNiconPixmap,
  348. x_display->icon_pixmap, NULL);
  349. XtVaSetValues(x_display->toplevel, XtNiconMask ,
  350. x_display->icon_mask , NULL);
  351. #endif
  352. /* アクションの設定 */
  353. XtAppAddActions(x_display->app_context, actions, XtNumber(actions));
  354. /* トランスレーションの設定 */
  355. XtOverrideTranslations(x_display->toplevel,
  356. XtParseTranslationTable(translations));
  357. /* ウィンドウマネージャからの f.delete への対応 */
  358. wm_delete_window = XInternAtom(x_display->display,
  359. "WM_DELETE_WINDOW", False);
  360. XSetWMProtocols(x_display->display, x_display->lcd_window,
  361. &wm_delete_window, 1);
  362. /* Xサーバと同期をとる */
  363. /* True だと,イベントキュー内のイベントを廃棄する */
  364. XSync(x_display->display, True);
  365. return (x_display);
  366. }
  367. /*---------------------------------------------------------------------------*/
  368. /* オブジェクトの消去 */
  369. /*---------------------------------------------------------------------------*/
  370. XDisplay XDisplay_Destroy(XDisplay x_display)
  371. {
  372. int i;
  373. if (x_display == NULL)
  374. WonX_Error("XDisplay_Destroy", "Object is not created.");
  375. /* Xサーバと同期をとる */
  376. /* True だと,イベントキュー内のイベントを廃棄する */
  377. XSync(x_display->display, True);
  378. /* トランスレーションのアンインストール */
  379. XtUninstallTranslations(x_display->toplevel);
  380. /* アクションの削除 */
  381. #if 0
  382. /*
  383. * 一度登録したアクションを削除することはできない.
  384. * (同じアクション名で新しいアクションを登録して,上書きすることはできる)
  385. */
  386. #endif
  387. /* アイコンの削除 */
  388. #if 0
  389. /* アイコンの削除処理が必要な場合にはここに書く.現状では必要無し */
  390. #endif
  391. /* イベントハンドラの削除 */
  392. XtRemoveEventHandler(x_display->toplevel, KeyPressMask | KeyReleaseMask,
  393. False, KeyHandler, x_display);
  394. XtRemoveEventHandler(x_display->toplevel, ExposureMask,
  395. False, ExposeHandler, x_display);
  396. XtRemoveEventHandler(x_display->toplevel, LeaveWindowMask | FocusChangeMask,
  397. False, LeaveWindowHandler, x_display);
  398. /* GCのデータベース終了処理 */
  399. if (x_display->color_gc_database) {
  400. x_display->color_gc_database =
  401. XColorGCDatabase_Destroy(x_display->color_gc_database);
  402. }
  403. #ifdef USE_X_FONT
  404. /* フォントの解放 */
  405. if (x_display->font_gc) {
  406. XFreeGC(x_display->display, x_display->font_gc);
  407. x_display->font_gc = 0;
  408. }
  409. if (x_display->font) {
  410. XUnloadFont(x_display->display, x_display->font);
  411. x_display->font = 0;
  412. }
  413. #endif
  414. for (i = 0; i < 16; i++) {
  415. if (x_display->color_gc[i]) {
  416. XFreeGC(x_display->display, x_display->color_gc[i]);
  417. x_display->color_gc[i] = 0;
  418. }
  419. }
  420. if (x_display->copy_gc) {
  421. XFreeGC(x_display->display, x_display->copy_gc);
  422. x_display->copy_gc = 0;
  423. }
  424. if (x_display->lcd_pixmap) {
  425. XFreePixmap(x_display->display, x_display->lcd_pixmap);
  426. x_display->lcd_pixmap = 0;
  427. }
  428. x_display->key_press = 0;
  429. x_display->lcd_draw_level = 0;
  430. #if 0
  431. if (x_display->app_context) {
  432. XtDestroyApplicationContext(x_display->app_context);
  433. x_display->app_context = 0;
  434. }
  435. #endif
  436. if (x_display->toplevel) {
  437. XtDestroyWidget(x_display->toplevel);
  438. x_display->toplevel = 0;
  439. }
  440. free(x_display);
  441. return (NULL);
  442. }
  443. /*---------------------------------------------------------------------------*/
  444. /* Xサーバとの同期 */
  445. /*---------------------------------------------------------------------------*/
  446. int XDisplay_Sync(XDisplay x_display)
  447. {
  448. XFlush(x_display->display);
  449. /* Xサーバと同期をとる */
  450. /* False だと,イベントキュー内のイベントを廃棄しない */
  451. XSync(x_display->display, False);
  452. return (0);
  453. }
  454. int XDisplay_Flush(XDisplay x_display)
  455. {
  456. XEvent event;
  457. XDisplay_Sync(x_display);
  458. /* イベントの処理 */
  459. while (XtAppPending(x_display->app_context)) {
  460. XtAppNextEvent(x_display->app_context, &event);
  461. XtDispatchEvent(&event);
  462. }
  463. return (0);
  464. }
  465. /*---------------------------------------------------------------------------*/
  466. /* 描画 */
  467. /*---------------------------------------------------------------------------*/
  468. int XDisplay_DrawLCDWindow(XDisplay x_display, WWDisplay ww_display,
  469. WWLCDPanel ww_lcd_panel)
  470. {
  471. int x, y;
  472. int px, py, ph;
  473. int num;
  474. int n[16];
  475. XRectangle rectangle;
  476. XRectangle * rectangles[16];
  477. unsigned short int pixel;
  478. int ww_lcd_width, ww_lcd_height;
  479. int red, green, blue;
  480. XColorGCDatabase database;
  481. XColorGC x_color_gc;
  482. GC gc;
  483. ww_lcd_width = WWLCDPanel_GetWidth( ww_lcd_panel);
  484. ww_lcd_height = WWLCDPanel_GetHeight(ww_lcd_panel);
  485. switch (WWDisplay_GetColorMode(ww_display)) {
  486. case COLOR_MODE_GRAYSCALE:
  487. /* 隣接しているピクセルはまとめて描画するので,ピクセル数の最大値は */
  488. /* 最悪の場合(縞々模様のとき)で,width * height / 2 になる. */
  489. num =
  490. WWLCDPanel_GetHeight(ww_lcd_panel) *
  491. WWLCDPanel_GetWidth(ww_lcd_panel) / 2;
  492. /*
  493. * この malloc() は,実際にはメモリはほとんど使用されていないので,
  494. * そのうちなんとかする必要がある
  495. */
  496. for (pixel = 0; pixel < 16; pixel++) {
  497. n[pixel] = 0;
  498. rectangles[pixel] = (XRectangle *)malloc(sizeof(XRectangle) * num);
  499. }
  500. if (rectangles == NULL)
  501. WonX_Error("XDisplay_DrawLCDWindow", "Cannot allocate memory.");
  502. /* ここの処理はホットスポットになるので,のちのちにチューニングすること */
  503. for (y = 0; y < ww_lcd_height; y++) {
  504. py = (y * x_display->height) / ww_lcd_height;
  505. ph = (y+1) * x_display->height / ww_lcd_height- py;
  506. for (x = 0; x < ww_lcd_width; x++) {
  507. if (!WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) {
  508. continue;
  509. }
  510. pixel = WWLCDPanel_GetPixel(ww_lcd_panel, x, y);
  511. px = (x * x_display->width ) / ww_lcd_width;
  512. rectangles[pixel][n[pixel]].x = px;
  513. rectangles[pixel][n[pixel]].y = py;
  514. rectangles[pixel][n[pixel]].width =
  515. (x+1) * x_display->width / ww_lcd_width - px;
  516. rectangles[pixel][n[pixel]].height = ph;
  517. /* 隣接してる同色のピクセルは,極力いっしょに描画する */
  518. x++;
  519. while ( (x < ww_lcd_width) &&
  520. (pixel == WWLCDPanel_GetPixel(ww_lcd_panel, x, y)) &&
  521. (WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) ) {
  522. rectangles[pixel][n[pixel]].width =
  523. (x+1) * x_display->width / ww_lcd_width - px;
  524. x++;
  525. }
  526. x--;
  527. n[pixel]++;
  528. }
  529. }
  530. for (pixel = 0; pixel < 16; pixel++) {
  531. if (n[pixel] > 0) {
  532. XFillRectangles(x_display->display,
  533. x_display->lcd_pixmap,
  534. x_display->color_gc[pixel],
  535. rectangles[pixel], n[pixel]);
  536. }
  537. }
  538. for (pixel = 0; pixel < 16; pixel++) {
  539. free(rectangles[pixel]);
  540. }
  541. break;
  542. case COLOR_MODE_4COLOR:
  543. case COLOR_MODE_16COLOR:
  544. case COLOR_MODE_16PACKED:
  545. database = XDisplay_GetColorGCDatabase(x_display);
  546. for (y = 0; y < ww_lcd_height; y++) {
  547. py = (y * x_display->height) / ww_lcd_height;
  548. ph = (y+1) * x_display->height / ww_lcd_height- py;
  549. for (x = 0; x < ww_lcd_width; x++) {
  550. if (!WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) {
  551. continue;
  552. }
  553. pixel = WWLCDPanel_GetPixel(ww_lcd_panel, x, y);
  554. px = (x * x_display->width ) / ww_lcd_width;
  555. rectangle.x = px;
  556. rectangle.y = py;
  557. rectangle.width = (x+1) * x_display->width / ww_lcd_width - px;
  558. rectangle.height = ph;
  559. /* 隣接してる同色のピクセルは,極力いっしょに描画する */
  560. x++;
  561. while ( (x < ww_lcd_width) &&
  562. (pixel == WWLCDPanel_GetPixel(ww_lcd_panel, x, y)) &&
  563. (WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) ) {
  564. rectangle.width =
  565. (x+1) * x_display->width / ww_lcd_width - px;
  566. x++;
  567. }
  568. x--;
  569. red = (pixel >> 8) & 0xf;
  570. green = (pixel >> 4) & 0xf;
  571. blue = (pixel >> 0) & 0xf;
  572. red = (red == 15) ? 65535 : red * 4096;
  573. green = (green == 15) ? 65535 : green * 4096;
  574. blue = (blue == 15) ? 65535 : blue * 4096;
  575. x_color_gc = XColorGC_CreateFromRGB(database, red, green, blue);
  576. gc = XColorGC_GetGC(x_color_gc);
  577. XFillRectangle(x_display->display,
  578. x_display->lcd_pixmap,
  579. gc,
  580. rectangle.x,
  581. rectangle.y,
  582. rectangle.width,
  583. rectangle.height);
  584. XColorGC_Destroy(x_color_gc);
  585. }
  586. }
  587. break;
  588. default:
  589. WonX_Error("XDisplay_DrawLCDWindow", "Unknown color mode.");
  590. }
  591. XCopyArea(x_display->display, x_display->lcd_pixmap,
  592. x_display->lcd_window, x_display->copy_gc,
  593. 0, 0, x_display->width, x_display->height, 0, 0);
  594. WWLCDPanel_ResetAllDraw(ww_lcd_panel);
  595. WWLCDPanel_ReverseCurrent(ww_lcd_panel);
  596. XDisplay_Sync(x_display);
  597. return (0);
  598. }
  599. /*---------------------------------------------------------------------------*/
  600. /* GC の作成 */
  601. /*---------------------------------------------------------------------------*/
  602. GC XDisplay_CreateGC(XDisplay x_display)
  603. {
  604. GC gc;
  605. gc = XCreateGC(x_display->display, x_display->lcd_window, 0, 0);
  606. return (gc);
  607. }
  608. /*---------------------------------------------------------------------------*/
  609. /* GC の解放 */
  610. /*---------------------------------------------------------------------------*/
  611. int XDisplay_DestroyGC(XDisplay x_display, GC gc)
  612. {
  613. XFreeGC(x_display->display, gc);
  614. return (0);
  615. }
  616. /*****************************************************************************/
  617. /* ここまで */
  618. /*****************************************************************************/
  619. /*****************************************************************************/
  620. /* End of File. */
  621. /*****************************************************************************/