XDisplay.c 15 KB

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