XDisplay.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 "etc.h"
  11. /*****************************************************************************/
  12. /* メンバ関数の定義 */
  13. /*****************************************************************************/
  14. Display * XDisplay_GetDisplay(XDisplay d) { return (d->display); }
  15. Colormap XDisplay_GetColormap(XDisplay d) { return (d->colormap); }
  16. XColorGCDatabase XDisplay_GetColorGCDatabase(XDisplay x_display)
  17. { return (x_display->color_gc_database); }
  18. unsigned int XDisplay_GetKeyPress(XDisplay d) { return (d->key_press); }
  19. int XDisplay_GetLCDDraw(XDisplay d) { return (d->lcd_draw); }
  20. int XDisplay_GetColorMapPrint(XDisplay d) {return (d->color_map_print); }
  21. int XDisplay_GetPalettePrint(XDisplay d) {return (d->palette_print); }
  22. int XDisplay_GetCharacterPrint(XDisplay d) {return (d->character_print); }
  23. int XDisplay_GetSpritePrint(XDisplay d) {return (d->sprite_print); }
  24. int XDisplay_SetColorMapPrint(XDisplay d, int f)
  25. { return (d->color_map_print = f); }
  26. int XDisplay_SetPalettePrint(XDisplay d, int f)
  27. { return (d->palette_print = f); }
  28. int XDisplay_SetCharacterPrint(XDisplay d, int f)
  29. { return(d->character_print = f); }
  30. int XDisplay_SetSpritePrint(XDisplay d, int f)
  31. { return (d->sprite_print = f); }
  32. /*****************************************************************************/
  33. /* 内部で使用する関数などの定義 */
  34. /*****************************************************************************/
  35. #if 0
  36. static XrmOptionDescRec options[] = {};
  37. #endif
  38. static Atom wm_delete_window;
  39. static void die(Widget w)
  40. {
  41. kill(getpid(), SIGINT);
  42. }
  43. static void quit(Widget w, XEvent * event, String * params, Cardinal * num)
  44. {
  45. die(w);
  46. }
  47. static void wm_protocols_proc(Widget w, XEvent * event, String * params,
  48. Cardinal * num)
  49. {
  50. if ((event->type == ClientMessage) &&
  51. (event->xclient.data.l[0] != wm_delete_window)) {
  52. XBell(XtDisplay(w), 0);
  53. } else {
  54. die(w);
  55. }
  56. }
  57. static void iconify(Widget w, XEvent * event, String * params, Cardinal * num)
  58. {
  59. XIconifyWindow(XtDisplay(w), XtWindow(w), DefaultScreen(XtDisplay(w)));
  60. }
  61. static void sleep_10(Widget w, XEvent * event, String * params, Cardinal * num)
  62. {
  63. time_t old_t;
  64. time_t t;
  65. int i;
  66. /* UNIXTimer.c 内部で SIGALRM を使用しているので,sleep() は使用できない */
  67. #if 0
  68. sleep(10);
  69. #else
  70. for (i = 0; i < 10; i++) {
  71. time(&t);
  72. old_t = t;
  73. while (t == old_t)
  74. time(&t);
  75. }
  76. #endif
  77. }
  78. static XtActionsRec actions[] = {
  79. {"quit", quit},
  80. {"wm_protocols_proc", wm_protocols_proc},
  81. {"iconify", iconify},
  82. {"pause", sleep_10}
  83. };
  84. static char * translations =
  85. "<Message>WM_PROTOCOLS: wm_protocols_proc()\n"
  86. "None<Key>F11: pause()\n"
  87. "Ctrl<Key>i: iconify()\n"
  88. "Ctrl<Key>c: quit()\n"
  89. "None<Key>F12: quit()\n"
  90. "None<Key>q: quit()";
  91. /*===========================================================================*/
  92. /* 色名からピクセル値を取得する */
  93. /*===========================================================================*/
  94. static unsigned long XDisplay_GetPixelFromColorName(XDisplay x_display,
  95. char * color_name)
  96. {
  97. XColor c0, c1;
  98. XAllocNamedColor(x_display->display, x_display->colormap, color_name,
  99. &c0, &c1);
  100. return (c0.pixel);
  101. }
  102. /*===========================================================================*/
  103. /* イベントハンドラ */
  104. /*===========================================================================*/
  105. /*---------------------------------------------------------------------------*/
  106. /* イクスポーズ */
  107. /*---------------------------------------------------------------------------*/
  108. static void ExposeHandler(Widget w, XtPointer p, XEvent * event,
  109. Boolean * dispatch)
  110. {
  111. XDisplay x_display = (XDisplay)p;
  112. XCopyArea(x_display->display, x_display->lcd_pixmap,
  113. x_display->lcd_window, x_display->copy_gc,
  114. 0, 0, x_display->width, x_display->height, 0, 0);
  115. return;
  116. }
  117. /*---------------------------------------------------------------------------*/
  118. /* キーの押下 */
  119. /*---------------------------------------------------------------------------*/
  120. static void KeyHandler(Widget w, XtPointer p, XEvent * event,
  121. Boolean * dispatch)
  122. {
  123. XDisplay x_display = (XDisplay)p;
  124. KeySym key_sym;
  125. int press = 0;
  126. if ((event->type == KeyPress) || (event->type == KeyRelease)) {
  127. key_sym = XKeycodeToKeysym(x_display->display, event->xkey.keycode, 0);
  128. switch (key_sym) {
  129. /* WonderSwan用 */
  130. case XK_Up : press = KEY_UP1; break;
  131. case XK_Right : press = KEY_RIGHT1; break;
  132. case XK_Down : press = KEY_DOWN1; break;
  133. case XK_Left : press = KEY_LEFT1; break;
  134. case XK_i : press = KEY_UP2; break;
  135. case XK_l : press = KEY_RIGHT2; break;
  136. case XK_k : press = KEY_DOWN2; break;
  137. case XK_j : press = KEY_LEFT2; break;
  138. case XK_s : press = KEY_START; break;
  139. case XK_space : press = KEY_A; break;
  140. case XK_Shift_L : press = KEY_B; break;
  141. default : press = 0; break;
  142. }
  143. /* WonX 操作用 */
  144. if (event->type == KeyPress) {
  145. switch (key_sym) {
  146. /* 表示モード変更 */
  147. case XK_F10 :
  148. x_display->lcd_draw = !(x_display->lcd_draw);
  149. if (x_display->lcd_draw) {
  150. #if 1
  151. WonXDisplay_Flush(WonX_GetWonXDisplay());
  152. #else
  153. ExposeHandler(w, p, event, dispatch);
  154. #endif
  155. }
  156. break;
  157. /* データのダンプ操作 */
  158. case XK_F1 : x_display->color_map_print = 1; break;
  159. case XK_F2 : x_display->palette_print = 1; break;
  160. case XK_F3 : x_display->character_print = 1; break;
  161. case XK_F4 : x_display->sprite_print = 1; break;
  162. }
  163. }
  164. if (press) {
  165. if (event->type == KeyPress) x_display->key_press |= press;
  166. else x_display->key_press &= ~press;
  167. }
  168. }
  169. return;
  170. }
  171. /*---------------------------------------------------------------------------*/
  172. /* マウスがウインドウを離れたら,キーを初期化する */
  173. /*---------------------------------------------------------------------------*/
  174. static void LeaveWindowHandler(Widget w, XtPointer p, XEvent * event,
  175. Boolean * dispatch)
  176. {
  177. XDisplay x_display = (XDisplay)p;
  178. x_display->key_press = 0;
  179. return;
  180. }
  181. /*===========================================================================*/
  182. /* オブジェクトの生成と消去 */
  183. /*===========================================================================*/
  184. /*---------------------------------------------------------------------------*/
  185. /* オブジェクトの生成 */
  186. /*---------------------------------------------------------------------------*/
  187. XDisplay XDisplay_Create(int width, int height)
  188. {
  189. XDisplay x_display;
  190. int argc = 0;
  191. char ** argv = NULL;
  192. XColor color;
  193. char * color_name[] = {"white", "gray84", "gray78", "gray72",
  194. "gray66", "gray60", "gray54", "gray48",
  195. "gray42", "gray36", "gray30", "gray24",
  196. "gray18", "gray12", "gray6", "black"};
  197. int i;
  198. x_display = (XDisplay)malloc(sizeof(_XDisplay));
  199. if (x_display == NULL)
  200. WonX_Error("XDisplay_Create", "Cannot allocate memory.");
  201. x_display->width = width;
  202. x_display->height = height;
  203. x_display->toplevel = XtAppInitialize(&(x_display->app_context),
  204. "WonX",
  205. NULL, 0, &argc, argv, NULL, NULL, 0);
  206. XtVaSetValues(x_display->toplevel, XtNinput, True, NULL);
  207. XtVaSetValues(x_display->toplevel, XtNtitle, "WonX", NULL);
  208. XtVaSetValues(x_display->toplevel, XtNwidth , x_display->width , NULL);
  209. XtVaSetValues(x_display->toplevel, XtNminWidth , x_display->width , NULL);
  210. XtVaSetValues(x_display->toplevel, XtNmaxWidth , x_display->width , NULL);
  211. XtVaSetValues(x_display->toplevel, XtNheight , x_display->height, NULL);
  212. XtVaSetValues(x_display->toplevel, XtNminHeight, x_display->height, NULL);
  213. XtVaSetValues(x_display->toplevel, XtNmaxHeight, x_display->height, NULL);
  214. x_display->key_press = 0;
  215. x_display->lcd_draw = 1;
  216. XtRealizeWidget(x_display->toplevel);
  217. while (!XtIsRealized(x_display->toplevel)) { /* None */ }
  218. x_display->display = XtDisplay(x_display->toplevel);
  219. x_display->root_window = DefaultRootWindow(x_display->display);
  220. x_display->lcd_window = XtWindow(x_display->toplevel);
  221. x_display->colormap = DefaultColormap(x_display->display,
  222. DefaultScreen(x_display->display));
  223. x_display->depth = DefaultDepth(x_display->display,
  224. DefaultScreen(x_display->display));
  225. x_display->lcd_pixmap = XCreatePixmap(x_display->display,
  226. x_display->lcd_window,
  227. x_display->width,
  228. x_display->height,
  229. x_display->depth);
  230. x_display->copy_gc = XCreateGC(x_display->display, x_display->lcd_window,
  231. 0, 0);
  232. XSetFunction(x_display->display, x_display->copy_gc, GXcopy);
  233. for (i = 0; i < 16; i++) {
  234. XParseColor(x_display->display, x_display->colormap,
  235. color_name[i], &color); /* 色の名前 → RGB値*/
  236. XAllocColor(x_display->display, x_display->colormap,
  237. &color); /* 色を確保し,ピクセル値を得る */
  238. x_display->color_gc[i] = XCreateGC(x_display->display,
  239. x_display->lcd_window, 0, 0);
  240. XSetForeground(x_display->display, x_display->color_gc[i], color.pixel);
  241. XSetFunction(x_display->display, x_display->color_gc[i], GXcopy);
  242. }
  243. /* GCのデータベース初期化 */
  244. x_display->color_gc_database =
  245. XColorGCDatabase_Create(x_display,
  246. 0, /* studying_flag */
  247. 1, /* cache_flag */
  248. 3, /* cache_size */
  249. 256, /* hash_number */
  250. "black", /* background_color */
  251. 16 /* gradation */);
  252. /* フォントの確保 */
  253. x_display->font = XLoadFont(x_display->display, "8x16");
  254. x_display->font_gc = XCreateGC(x_display->display,
  255. x_display->lcd_window, 0, 0);
  256. XSetFont(x_display->display, x_display->font_gc, x_display->font);
  257. XSetFunction(x_display->display, x_display->font_gc, GXcopy);
  258. XSetForeground(x_display->display, x_display->font_gc,
  259. XDisplay_GetPixelFromColorName(x_display, "white"));
  260. XSetBackground(x_display->display, x_display->font_gc,
  261. XDisplay_GetPixelFromColorName(x_display, "black"));
  262. XFillRectangle(x_display->display, x_display->lcd_window,
  263. x_display->color_gc[0],
  264. 0, 0, x_display->width, x_display->height);
  265. /* イベントハンドラの登録 */
  266. XtAddEventHandler(x_display->toplevel, KeyPressMask | KeyReleaseMask,
  267. False, KeyHandler, x_display);
  268. XtAddEventHandler(x_display->toplevel, ExposureMask,
  269. False, ExposeHandler, x_display);
  270. XtAddEventHandler(x_display->toplevel, LeaveWindowMask | FocusChangeMask,
  271. False, LeaveWindowHandler, x_display);
  272. /* アイコンの設定 */
  273. #if 0
  274. XtVaSetValues(x_display->toplevel, XtNiconPixmap,
  275. x_display->icon_pixmap, NULL);
  276. XtVaSetValues(x_display->toplevel, XtNiconMask ,
  277. x_display->icon_mask , NULL);
  278. #endif
  279. /* アクションの設定 */
  280. XtAppAddActions(x_display->app_context, actions, XtNumber(actions));
  281. /* トランスレーションの設定 */
  282. XtOverrideTranslations(x_display->toplevel,
  283. XtParseTranslationTable(translations));
  284. /* ウィンドウマネージャからの f.delete への対応 */
  285. wm_delete_window = XInternAtom(x_display->display,
  286. "WM_DELETE_WINDOW", False);
  287. XSetWMProtocols(x_display->display, x_display->lcd_window,
  288. &wm_delete_window, 1);
  289. /* Xサーバと同期をとる */
  290. /* True だと,イベントキュー内のイベントを廃棄する */
  291. XSync(x_display->display, True);
  292. return (x_display);
  293. }
  294. /*---------------------------------------------------------------------------*/
  295. /* オブジェクトの消去 */
  296. /*---------------------------------------------------------------------------*/
  297. XDisplay XDisplay_Destroy(XDisplay x_display)
  298. {
  299. int i;
  300. if (x_display == NULL) return (NULL);
  301. /* あとでリソースの解放を追加すること */
  302. if (x_display->color_gc != NULL) {
  303. for (i = 0; i < 16; i++) {
  304. if (x_display->color_gc[i])
  305. XFreeGC(x_display->display, x_display->color_gc[i]);
  306. x_display->color_gc[i] = 0;
  307. }
  308. }
  309. free(x_display);
  310. return (NULL);
  311. }
  312. /*---------------------------------------------------------------------------*/
  313. /* Xサーバとの同期 */
  314. /*---------------------------------------------------------------------------*/
  315. int XDisplay_Sync(XDisplay x_display)
  316. {
  317. XEvent event;
  318. XFlush(x_display->display);
  319. /* Xサーバと同期をとる */
  320. /* False だと,イベントキュー内のイベントを廃棄しない */
  321. XSync(x_display->display, False);
  322. /* イベントの処理 */
  323. while (XtAppPending(x_display->app_context)) {
  324. XtAppNextEvent(x_display->app_context, &event);
  325. XtDispatchEvent(&event);
  326. }
  327. return (0);
  328. }
  329. /*---------------------------------------------------------------------------*/
  330. /* 描画 */
  331. /*---------------------------------------------------------------------------*/
  332. int XDisplay_DrawLCDWindow(XDisplay x_display, WWDisplay ww_display,
  333. WWLCDPanel ww_lcd_panel)
  334. {
  335. int x, y;
  336. int px, py, ph;
  337. int num;
  338. int n[16];
  339. XRectangle rectangle;
  340. XRectangle * rectangles[16];
  341. unsigned short int pixel;
  342. int ww_lcd_width, ww_lcd_height;
  343. int red, green, blue;
  344. XColorGCDatabase database;
  345. XColorGC x_color_gc;
  346. GC gc;
  347. ww_lcd_width = WWLCDPanel_GetWidth( ww_lcd_panel);
  348. ww_lcd_height = WWLCDPanel_GetHeight(ww_lcd_panel);
  349. switch (WWDisplay_GetColorMode(ww_display)) {
  350. case COLOR_MODE_GRAYSCALE:
  351. /* 隣接しているピクセルはまとめて描画するので,ピクセル数の最大値は */
  352. /* 最悪の場合(縞々模様のとき)で,width * height / 2 になる. */
  353. num =
  354. WWLCDPanel_GetHeight(ww_lcd_panel) *
  355. WWLCDPanel_GetWidth(ww_lcd_panel) / 2;
  356. /*
  357. * この malloc() は,実際にはメモリはほとんど使用されていないので,
  358. * そのうちなんとかする必要がある
  359. */
  360. for (pixel = 0; pixel < 16; pixel++) {
  361. n[pixel] = 0;
  362. rectangles[pixel] = (XRectangle *)malloc(sizeof(XRectangle) * num);
  363. }
  364. if (rectangles == NULL)
  365. WonX_Error("XDisplay_DrawLCDWindow", "Cannot allocate memory.");
  366. /* ここの処理はホットスポットになるので,のちのちにチューニングすること */
  367. for (y = 0; y < ww_lcd_height; y++) {
  368. py = (y * x_display->height) / ww_lcd_height;
  369. ph = (y+1) * x_display->height / ww_lcd_height- py;
  370. for (x = 0; x < ww_lcd_width; x++) {
  371. if (!WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) {
  372. continue;
  373. }
  374. pixel = WWLCDPanel_GetPixel(ww_lcd_panel, x, y);
  375. px = (x * x_display->width ) / ww_lcd_width;
  376. rectangles[pixel][n[pixel]].x = px;
  377. rectangles[pixel][n[pixel]].y = py;
  378. rectangles[pixel][n[pixel]].width =
  379. (x+1) * x_display->width / ww_lcd_width - px;
  380. rectangles[pixel][n[pixel]].height = ph;
  381. /* 隣接してる同色のピクセルは,極力いっしょに描画する */
  382. x++;
  383. while ( (x < ww_lcd_width) &&
  384. (pixel == WWLCDPanel_GetPixel(ww_lcd_panel, x, y)) &&
  385. (WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) ) {
  386. rectangles[pixel][n[pixel]].width =
  387. (x+1) * x_display->width / ww_lcd_width - px;
  388. x++;
  389. }
  390. x--;
  391. n[pixel]++;
  392. }
  393. }
  394. for (pixel = 0; pixel < 16; pixel++) {
  395. if (n[pixel] > 0) {
  396. XFillRectangles(x_display->display,
  397. x_display->lcd_pixmap,
  398. x_display->color_gc[pixel],
  399. rectangles[pixel], n[pixel]);
  400. }
  401. }
  402. for (pixel = 0; pixel < 16; pixel++) {
  403. free(rectangles[pixel]);
  404. }
  405. break;
  406. case COLOR_MODE_4COLOR:
  407. case COLOR_MODE_16COLOR:
  408. case COLOR_MODE_16PACKED:
  409. database = XDisplay_GetColorGCDatabase(x_display);
  410. for (y = 0; y < ww_lcd_height; y++) {
  411. py = (y * x_display->height) / ww_lcd_height;
  412. ph = (y+1) * x_display->height / ww_lcd_height- py;
  413. for (x = 0; x < ww_lcd_width; x++) {
  414. if (!WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) {
  415. continue;
  416. }
  417. pixel = WWLCDPanel_GetPixel(ww_lcd_panel, x, y);
  418. px = (x * x_display->width ) / ww_lcd_width;
  419. rectangle.x = px;
  420. rectangle.y = py;
  421. rectangle.width = (x+1) * x_display->width / ww_lcd_width - px;
  422. rectangle.height = ph;
  423. /* 隣接してる同色のピクセルは,極力いっしょに描画する */
  424. x++;
  425. while ( (x < ww_lcd_width) &&
  426. (pixel == WWLCDPanel_GetPixel(ww_lcd_panel, x, y)) &&
  427. (WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) ) {
  428. rectangle.width =
  429. (x+1) * x_display->width / ww_lcd_width - px;
  430. x++;
  431. }
  432. x--;
  433. red = (pixel >> 8) & 0xf;
  434. green = (pixel >> 4) & 0xf;
  435. blue = (pixel >> 0) & 0xf;
  436. red = (red == 15) ? 65535 : red * 4096;
  437. green = (green == 15) ? 65535 : green * 4096;
  438. blue = (blue == 15) ? 65535 : blue * 4096;
  439. x_color_gc = XColorGC_CreateFromRGB(database, red, green, blue);
  440. gc = XColorGC_GetGC(x_color_gc);
  441. XFillRectangle(x_display->display,
  442. x_display->lcd_pixmap,
  443. gc,
  444. rectangle.x,
  445. rectangle.y,
  446. rectangle.width,
  447. rectangle.height);
  448. XColorGC_Destroy(x_color_gc);
  449. }
  450. }
  451. break;
  452. default:
  453. WonX_Error("XDisplay_DrawLCDWindow", "Unknown color mode.");
  454. }
  455. XCopyArea(x_display->display, x_display->lcd_pixmap,
  456. x_display->lcd_window, x_display->copy_gc,
  457. 0, 0, x_display->width, x_display->height, 0, 0);
  458. WWLCDPanel_ResetAllDraw(ww_lcd_panel);
  459. WWLCDPanel_ReverseCurrent(ww_lcd_panel);
  460. XDisplay_Sync(x_display);
  461. return (0);
  462. }
  463. /*---------------------------------------------------------------------------*/
  464. /* GC の作成 */
  465. /*---------------------------------------------------------------------------*/
  466. GC XDisplay_CreateGC(XDisplay x_display)
  467. {
  468. GC gc;
  469. gc = XCreateGC(x_display->display, x_display->root_window, 0, 0);
  470. return (gc);
  471. }
  472. /*---------------------------------------------------------------------------*/
  473. /* GC の解放 */
  474. /*---------------------------------------------------------------------------*/
  475. int XDisplay_DestroyGC(XDisplay x_display, GC gc)
  476. {
  477. XFreeGC(x_display->display, gc);
  478. return (0);
  479. }
  480. /*****************************************************************************/
  481. /* ここまで */
  482. /*****************************************************************************/
  483. /*****************************************************************************/
  484. /* End of File. */
  485. /*****************************************************************************/