key.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "wonx/key.h"
  7. #include "WonX.h"
  8. /*****************************************************************************/
  9. /* 互換関数の定義 */
  10. /*****************************************************************************/
  11. /*
  12. * Xサーバとの同期の整合性がとれなくなるなどの問題が考えられるので,
  13. * 互換関数の内部は UNIXTimer_Pause(), UNIXTimer_Unpause() でくくり,
  14. * タイマ割り込みを一時停止して処理を行う.また,unpause するまえに,
  15. * かならず sync するようにする.
  16. */
  17. /*
  18. * タイマの一時停止の2重解除の問題が出てくるので,
  19. * 互換関数から互換関数を呼んではいけない.
  20. * (一時停止はネストされるが,いちおう)
  21. * 似たような処理をする関数の場合は,必ず static な別関数に処理をまとめ,
  22. * そっちを呼び出すようにすること.
  23. * 引数の表示の問題もあるしね.
  24. */
  25. int key_press_check(void)
  26. {
  27. XDisplay x_display;
  28. int ret;
  29. if (!WonX_IsCreated()) WonX_Create();
  30. /* タイマを一時停止する */
  31. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  32. printf("call : key_press_check() : \n"); fflush(stdout);
  33. x_display = WonXDisplay_GetXDisplay(WonX_GetWonXDisplay());
  34. XDisplay_Flush(x_display);
  35. ret = XDisplay_GetKeyPress(x_display);
  36. WonXDisplay_Sync(WonX_GetWonXDisplay());
  37. printf("call : key_press_check() : return value = 0x%04x\n", (int)ret);
  38. fflush(stdout);
  39. /* タイマをもとに戻す */
  40. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  41. return (ret);
  42. }
  43. int key_hit_check(void)
  44. {
  45. XDisplay x_display;
  46. int ret;
  47. if (!WonX_IsCreated()) WonX_Create();
  48. /* タイマを一時停止する */
  49. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  50. printf("call : key_hit_check() : \n"); fflush(stdout);
  51. x_display = WonXDisplay_GetXDisplay(WonX_GetWonXDisplay());
  52. XDisplay_Flush(x_display);
  53. ret = XDisplay_GetKeyPress(x_display);
  54. WonXDisplay_Sync(WonX_GetWonXDisplay());
  55. printf("call : key_hit_check() : return value = 0x%04x\n", (int)ret);
  56. fflush(stdout);
  57. /* タイマをもとに戻す */
  58. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  59. return (ret);
  60. }
  61. int key_wait(void)
  62. {
  63. XDisplay x_display;
  64. volatile int ret;
  65. if (!WonX_IsCreated()) WonX_Create();
  66. /* タイマを一時停止する */
  67. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  68. printf("call : key_wait() : \n"); fflush(stdout);
  69. x_display = WonXDisplay_GetXDisplay(WonX_GetWonXDisplay());
  70. /*
  71. * 以下はホットスポットになり得るので注意!
  72. * key_wait() 中に割り込みを受け付けるために一時的にタイマを Unpause する.
  73. * タイマ割り込みのコールバック関数中でキー入力の割り込み処理も行われるため,
  74. * do~while ループ中での XDisplay_Flush() は必要無し.
  75. */
  76. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  77. /*
  78. * タイマ割り込みによるキー入力処理に期待していったん Unpause するため,
  79. * 再び Pause するまでは XDisplay の描画関連の関数などを呼び出しては
  80. * いけないので注意.
  81. * (XDisplay_GetKeyPress() は単に変数の値を返すだけなので問題無い)
  82. */
  83. ret = 0;
  84. do {
  85. #if 0 /* タイマを Unpause しない場合には必要 */
  86. XDisplay_Flush(x_display);
  87. #endif
  88. ret = XDisplay_GetKeyPress(x_display);
  89. } while (ret == 0);
  90. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  91. WonXDisplay_Sync(WonX_GetWonXDisplay());
  92. printf("call : key_wait() : return value = 0x%04x\n", (int)ret);
  93. fflush(stdout);
  94. /* タイマをもとに戻す */
  95. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  96. return (ret);
  97. }
  98. void key_set_repeat(int rate, int delay)
  99. {
  100. if (!WonX_IsCreated()) WonX_Create();
  101. /* タイマを一時停止する */
  102. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  103. printf("call : key_set_repeat() : rate = %d, delay = %d\n", rate, delay);
  104. fflush(stdout);
  105. WonXDisplay_Sync(WonX_GetWonXDisplay());
  106. printf("call : key_set_repeat() : return value = none\n"); fflush(stdout);
  107. /* タイマをもとに戻す */
  108. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  109. return;
  110. }
  111. int key_get_repeat(void)
  112. {
  113. int ret;
  114. if (!WonX_IsCreated()) WonX_Create();
  115. /* タイマを一時停止する */
  116. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  117. printf("call : key_get_repeat() : \n"); fflush(stdout);
  118. ret = 0;
  119. WonXDisplay_Sync(WonX_GetWonXDisplay());
  120. printf("call : key_get_repeat() : return value = 0x%04x\n", (int)ret);
  121. fflush(stdout);
  122. /* タイマをもとに戻す */
  123. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  124. return (ret);
  125. }
  126. int key_hit_check_with_repeat(void)
  127. {
  128. XDisplay x_display;
  129. int ret;
  130. if (!WonX_IsCreated()) WonX_Create();
  131. /* タイマを一時停止する */
  132. UNIXTimer_Pause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  133. printf("call : key_hit_check_with_repeat() : \n"); fflush(stdout);
  134. x_display = WonXDisplay_GetXDisplay(WonX_GetWonXDisplay());
  135. XDisplay_Flush(x_display);
  136. ret = XDisplay_GetKeyPress(x_display);
  137. WonXDisplay_Sync(WonX_GetWonXDisplay());
  138. printf("call : key_hit_check_with_repeat() : return value = 0x%04x\n",
  139. (int)ret);
  140. fflush(stdout);
  141. /* タイマをもとに戻す */
  142. UNIXTimer_Unpause(WonXSystem_GetUNIXTimer(WonX_GetWonXSystem()));
  143. return (ret);
  144. }
  145. /*****************************************************************************/
  146. /* ここまで */
  147. /*****************************************************************************/
  148. /*****************************************************************************/
  149. /* End of File. */
  150. /*****************************************************************************/