XDisplay.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. 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)
  190. Wonx_Error("XDisplay_Create", "Cannot allocate memory.");
  191. x_display->width = width;
  192. x_display->height = height;
  193. x_display->toplevel = XtAppInitialize(&(x_display->app_context),
  194. "Wonx",
  195. NULL, 0, &argc, argv, NULL, NULL, 0);
  196. XtVaSetValues(x_display->toplevel, XtNinput, True, NULL);
  197. XtVaSetValues(x_display->toplevel, XtNtitle, "Wonx", NULL);
  198. XtVaSetValues(x_display->toplevel, XtNwidth , x_display->width , NULL);
  199. XtVaSetValues(x_display->toplevel, XtNminWidth , x_display->width , NULL);
  200. XtVaSetValues(x_display->toplevel, XtNmaxWidth , x_display->width , NULL);
  201. XtVaSetValues(x_display->toplevel, XtNheight , x_display->height, NULL);
  202. XtVaSetValues(x_display->toplevel, XtNminHeight, x_display->height, NULL);
  203. XtVaSetValues(x_display->toplevel, XtNmaxHeight, x_display->height, NULL);
  204. x_display->key_press = 0;
  205. x_display->lcd_draw = 1;
  206. XtRealizeWidget(x_display->toplevel);
  207. while (!XtIsRealized(x_display->toplevel)) { /* None */ }
  208. x_display->display = XtDisplay(x_display->toplevel);
  209. x_display->root_window = DefaultRootWindow(x_display->display);
  210. x_display->lcd_window = XtWindow(x_display->toplevel);
  211. x_display->colormap = DefaultColormap(x_display->display,
  212. DefaultScreen(x_display->display));
  213. x_display->depth = DefaultDepth(x_display->display,
  214. DefaultScreen(x_display->display));
  215. x_display->lcd_pixmap = XCreatePixmap(x_display->display,
  216. x_display->lcd_window,
  217. x_display->width,
  218. x_display->height,
  219. x_display->depth);
  220. x_display->copy_gc = XCreateGC(x_display->display, x_display->lcd_window,
  221. 0, 0);
  222. XSetFunction(x_display->display, x_display->copy_gc, GXcopy);
  223. for (i = 0; i < 16; i++) {
  224. XParseColor(x_display->display, x_display->colormap,
  225. color_name[i], &color); /* 色の名前 → RGB値*/
  226. XAllocColor(x_display->display, x_display->colormap,
  227. &color); /* 色を確保し,ピクセル値を得る */
  228. x_display->color_gc[i] = XCreateGC(x_display->display,
  229. x_display->lcd_window, 0, 0);
  230. XSetForeground(x_display->display, x_display->color_gc[i], color.pixel);
  231. XSetFunction(x_display->display, x_display->color_gc[i], GXcopy);
  232. }
  233. /* フォントの確保 */
  234. x_display->font = XLoadFont(x_display->display, "8x16");
  235. x_display->font_gc = XCreateGC(x_display->display,
  236. x_display->lcd_window, 0, 0);
  237. XSetFont(x_display->display, x_display->font_gc, x_display->font);
  238. XSetFunction(x_display->display, x_display->font_gc, GXcopy);
  239. XSetForeground(x_display->display, x_display->font_gc,
  240. XDisplay_GetPixelFromColorName(x_display, "white"));
  241. XSetBackground(x_display->display, x_display->font_gc,
  242. XDisplay_GetPixelFromColorName(x_display, "black"));
  243. XFillRectangle(x_display->display, x_display->lcd_window,
  244. x_display->color_gc[0],
  245. 0, 0, x_display->width, x_display->height);
  246. /* イベントハンドラの登録 */
  247. XtAddEventHandler(x_display->toplevel, KeyPressMask | KeyReleaseMask,
  248. False, KeyHandler, x_display);
  249. XtAddEventHandler(x_display->toplevel, ExposureMask,
  250. False, ExposeHandler, x_display);
  251. XtAddEventHandler(x_display->toplevel, LeaveWindowMask | FocusChangeMask,
  252. False, LeaveWindowHandler, x_display);
  253. /* アイコンの設定 */
  254. #if 0
  255. XtVaSetValues(x_display->toplevel, XtNiconPixmap,
  256. x_display->icon_pixmap, NULL);
  257. XtVaSetValues(x_display->toplevel, XtNiconMask ,
  258. x_display->icon_mask , NULL);
  259. #endif
  260. /* アクションの設定 */
  261. XtAppAddActions(x_display->app_context, actions, XtNumber(actions));
  262. /* トランスレーションの設定 */
  263. XtOverrideTranslations(x_display->toplevel,
  264. XtParseTranslationTable(translations));
  265. /* ウィンドウマネージャからの f.delete への対応 */
  266. wm_delete_window = XInternAtom(x_display->display,
  267. "WM_DELETE_WINDOW", False);
  268. XSetWMProtocols(x_display->display, x_display->lcd_window,
  269. &wm_delete_window, 1);
  270. /* Xサーバと同期をとる */
  271. /* True だと,イベントキュー内のイベントを廃棄する */
  272. XSync(x_display->display, True);
  273. return (x_display);
  274. }
  275. /*---------------------------------------------------------------------------*/
  276. /* オブジェクトの消去 */
  277. /*---------------------------------------------------------------------------*/
  278. XDisplay XDisplay_Destroy(XDisplay x_display)
  279. {
  280. int i;
  281. if (x_display == NULL) return (NULL);
  282. /* あとでリソースの解放を追加すること */
  283. if (x_display->color_gc != NULL) {
  284. for (i = 0; i < 16; i++) {
  285. if (x_display->color_gc[i])
  286. XFreeGC(x_display->display, x_display->color_gc[i]);
  287. x_display->color_gc[i] = 0;
  288. }
  289. }
  290. free(x_display);
  291. return (NULL);
  292. }
  293. /*---------------------------------------------------------------------------*/
  294. /* Xサーバとの同期 */
  295. /*---------------------------------------------------------------------------*/
  296. int XDisplay_Sync(XDisplay x_display)
  297. {
  298. XEvent event;
  299. XFlush(x_display->display);
  300. /* Xサーバと同期をとる */
  301. /* False だと,イベントキュー内のイベントを廃棄しない */
  302. XSync(x_display->display, False);
  303. /* イベントの処理 */
  304. while (XtAppPending(x_display->app_context)) {
  305. XtAppNextEvent(x_display->app_context, &event);
  306. XtDispatchEvent(&event);
  307. }
  308. return (0);
  309. }
  310. /*---------------------------------------------------------------------------*/
  311. /* 描画 */
  312. /*---------------------------------------------------------------------------*/
  313. int XDisplay_DrawLCDWindow(XDisplay x_display, WWLCDPanel ww_lcd_panel)
  314. {
  315. int x, y;
  316. int px, py, ph;
  317. int num;
  318. int n[16];
  319. XRectangle * rectangles[16];
  320. int pixel;
  321. int ww_lcd_width, ww_lcd_height;
  322. /* 隣接しているピクセルはまとめて描画するので,ピクセル数の最大値は */
  323. /* 最悪の場合(縞々模様のとき)で,width * height / 2 になる. */
  324. num =
  325. WWLCDPanel_GetHeight(ww_lcd_panel) * WWLCDPanel_GetWidth(ww_lcd_panel) / 2;
  326. /*
  327. * この malloc() は,実際にはメモリはほとんど使用されていないので,
  328. * そのうちなんとかする必要がある
  329. */
  330. for (pixel = 0; pixel < 16; pixel++) {
  331. n[pixel] = 0;
  332. rectangles[pixel] = (XRectangle *)malloc(sizeof(XRectangle) * num);
  333. }
  334. if (rectangles == NULL)
  335. Wonx_Error("XDisplay_DrawLCDWindow", "Cannot allocate memory.");
  336. ww_lcd_width = WWLCDPanel_GetWidth( ww_lcd_panel);
  337. ww_lcd_height = WWLCDPanel_GetHeight(ww_lcd_panel);
  338. /* ここの処理はホットスポットになるので,のちのちにチューニングすること */
  339. for (y = 0; y < ww_lcd_height; y++) {
  340. py = (y * x_display->height) / ww_lcd_height;
  341. ph = (y+1) * x_display->height / ww_lcd_height- py;
  342. for (x = 0; x < ww_lcd_width; x++) {
  343. if (!WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) {
  344. continue;
  345. }
  346. pixel = WWLCDPanel_GetPixel(ww_lcd_panel, x, y);
  347. px = (x * x_display->width ) / ww_lcd_width;
  348. rectangles[pixel][n[pixel]].x = px;
  349. rectangles[pixel][n[pixel]].y = py;
  350. rectangles[pixel][n[pixel]].width =
  351. (x+1) * x_display->width / ww_lcd_width - px;
  352. rectangles[pixel][n[pixel]].height = ph;
  353. /* 隣接してる同色のピクセルは,極力いっしょに描画する */
  354. x++;
  355. while ( (x < ww_lcd_width) &&
  356. (pixel == WWLCDPanel_GetPixel(ww_lcd_panel, x, y)) &&
  357. (WWLCDPanel_IsPixelChanged(ww_lcd_panel, x, y)) ) {
  358. rectangles[pixel][n[pixel]].width =
  359. (x+1) * x_display->width / ww_lcd_width - px;
  360. x++;
  361. }
  362. x--;
  363. n[pixel]++;
  364. }
  365. }
  366. for (pixel = 0; pixel < 16; pixel++) {
  367. if (n[pixel] > 0) {
  368. XFillRectangles(x_display->display,
  369. x_display->lcd_pixmap,
  370. x_display->color_gc[pixel],
  371. rectangles[pixel], n[pixel]);
  372. }
  373. }
  374. XCopyArea(x_display->display, x_display->lcd_pixmap,
  375. x_display->lcd_window, x_display->copy_gc,
  376. 0, 0, x_display->width, x_display->height, 0, 0);
  377. WWLCDPanel_ResetAllDraw(ww_lcd_panel);
  378. WWLCDPanel_ReverseCurrent(ww_lcd_panel);
  379. XDisplay_Sync(x_display);
  380. for (pixel = 0; pixel < 16; pixel++) {
  381. free(rectangles[pixel]);
  382. }
  383. return (0);
  384. }
  385. /*****************************************************************************/
  386. /* ここまで */
  387. /*****************************************************************************/
  388. /*****************************************************************************/
  389. /* End of File. */
  390. /*****************************************************************************/