XDisplay.c 16 KB

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