XDisplay.c 19 KB

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