TouchScreen.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:207k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2.  * FUNCTION
  3.  *  mmi_pen_test_clear_screen
  4.  * DESCRIPTION
  5.  *  
  6.  * PARAMETERS
  7.  *  void
  8.  * RETURNS
  9.  *  void
  10.  *****************************************************************************/
  11. static void mmi_pen_test_clear_screen(void)
  12. {
  13.     /*----------------------------------------------------------------*/
  14.     /* Local Variables                                                */
  15.     /*----------------------------------------------------------------*/
  16.     /*----------------------------------------------------------------*/
  17.     /* Code Body                                                      */
  18.     /*----------------------------------------------------------------*/
  19.     memset(mmi_pen_draw_buffer, 0, sizeof(mmi_pen_draw_buffer));
  20. }
  21. /*****************************************************************************
  22.  * FUNCTION
  23.  *  mmi_pen_test_draw_point
  24.  * DESCRIPTION
  25.  *  
  26.  * PARAMETERS
  27.  *  x       [IN]        
  28.  *  y       [IN]        
  29.  * RETURNS
  30.  *  void
  31.  *****************************************************************************/
  32. static void mmi_pen_test_draw_point(S32 x, S32 y)
  33. {
  34.     /*----------------------------------------------------------------*/
  35.     /* Local Variables                                                */
  36.     /*----------------------------------------------------------------*/
  37.     S32 offset;
  38.     /*----------------------------------------------------------------*/
  39.     /* Code Body                                                      */
  40.     /*----------------------------------------------------------------*/
  41.     if (x < 0 || x >= LCD_WIDTH || y < 0 || y >= LCD_HEIGHT)
  42.     {
  43.         return;
  44.     }
  45.     offset = y * LCD_WIDTH + x;
  46.     mmi_pen_draw_buffer[offset >> 3] |= (1 << (offset & 0x07));
  47. }
  48. /*****************************************************************************
  49.  * FUNCTION
  50.  *  mmi_pen_test_draw_from_buffer
  51.  * DESCRIPTION
  52.  *  
  53.  * PARAMETERS
  54.  *  x1      [IN]        
  55.  *  y1      [IN]        
  56.  *  x2      [IN]        
  57.  *  y2      [IN]        
  58.  * RETURNS
  59.  *  void
  60.  *****************************************************************************/
  61. static void mmi_pen_test_draw_from_buffer(S32 x1, S32 y1, S32 x2, S32 y2)
  62. {
  63.     /*----------------------------------------------------------------*/
  64.     /* Local Variables                                                */
  65.     /*----------------------------------------------------------------*/
  66.     S32 x, y;
  67.     S32 offset;
  68.     color c;
  69.     /*----------------------------------------------------------------*/
  70.     /* Code Body                                                      */
  71.     /*----------------------------------------------------------------*/
  72.     c.r = 255;
  73.     c.g = c.b = 0;
  74.     c.alpha = 255;
  75.     gui_push_clip();
  76.     gui_set_clip(x1, y1, x2, y2);
  77.     for (y = y1; y <= y2; y++)
  78.     {
  79.         offset = y * LCD_WIDTH;
  80.         for (x = x1; x <= x2; x++)
  81.         {
  82.             if (mmi_pen_draw_buffer[offset >> 3] & (1 << (offset & 0x07)))
  83.             {
  84.                 gui_putpixel(x, y, c);
  85.             }
  86.             offset++;
  87.         }
  88.     }
  89.     gui_pop_clip();
  90. }
  91. /*****************************************************************************
  92.  * FUNCTION
  93.  *  mmi_pen_test_refresh_screen
  94.  * DESCRIPTION
  95.  *  
  96.  * PARAMETERS
  97.  *  void
  98.  * RETURNS
  99.  *  void
  100.  *****************************************************************************/
  101. static void mmi_pen_test_refresh_screen(void)
  102. {
  103.     /*----------------------------------------------------------------*/
  104.     /* Local Variables                                                */
  105.     /*----------------------------------------------------------------*/
  106.     /*----------------------------------------------------------------*/
  107.     /* Code Body                                                      */
  108.     /*----------------------------------------------------------------*/
  109.     gui_lock_double_buffer();
  110.     mmi_pen_test_draw_from_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  111.     gui_unlock_double_buffer();
  112.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  113. }
  114. /*****************************************************************************
  115.  * FUNCTION
  116.  *  mmi_pen_test_draw_line
  117.  * DESCRIPTION
  118.  *  
  119.  * PARAMETERS
  120.  *  x1      [IN]        
  121.  *  y1      [IN]        
  122.  *  x2      [IN]        
  123.  *  y2      [IN]        
  124.  * RETURNS
  125.  *  void
  126.  *****************************************************************************/
  127. static void mmi_pen_test_draw_line(S16 x1, S16 y1, S16 x2, S16 y2)
  128. {
  129.     /*----------------------------------------------------------------*/
  130.     /* Local Variables                                                */
  131.     /*----------------------------------------------------------------*/
  132.     S16 x_diff = abs(x2 - x1);
  133.     S16 y_diff = abs(y2 - y1);
  134.     S16 x_incr, y_incr;
  135.     /*----------------------------------------------------------------*/
  136.     /* Code Body                                                      */
  137.     /*----------------------------------------------------------------*/
  138.     if (x1 > x2)
  139.     {
  140.         x_incr = -1;
  141.     }
  142.     else
  143.     {
  144.         x_incr = 1;
  145.     }
  146.     if (y1 > y2)
  147.     {
  148.         y_incr = -1;
  149.     }
  150.     else
  151.     {
  152.         y_incr = 1;
  153.     }
  154.     if (x_diff >= y_diff)
  155.     {
  156.         S16 pr_diff = y_diff << 1;
  157.         S16 pru_diff = pr_diff - (x_diff << 1);
  158.         S16 p = pr_diff - x_diff;
  159.         for (; x_diff >= 0; x_diff--)
  160.         {
  161.             mmi_pen_test_draw_point(x1, y1);
  162.             if (p > 0)
  163.             {
  164.                 x1 += x_incr;
  165.                 y1 += y_incr;
  166.                 p += pru_diff;
  167.             }
  168.             else
  169.             {
  170.                 x1 += x_incr;
  171.                 p += pr_diff;
  172.             }
  173.         }
  174.     }
  175.     else
  176.     {
  177.         S16 pr_diff = x_diff << 1;
  178.         S16 pru_diff = pr_diff - (y_diff << 1);
  179.         S16 p = pr_diff - y_diff;
  180.         for (; y_diff >= 0; y_diff--)
  181.         {
  182.             mmi_pen_test_draw_point(x1, y1);
  183.             if (p > 0)
  184.             {
  185.                 x1 += x_incr;
  186.                 y1 += y_incr;
  187.                 p += pru_diff;
  188.             }
  189.             else
  190.             {
  191.                 y1 += y_incr;
  192.                 p += pr_diff;
  193.             }
  194.         }
  195.     }
  196. }
  197. /*****************************************************************************
  198.  * FUNCTION
  199.  *  mmi_pen_test_draw_rectangle
  200.  * DESCRIPTION
  201.  *  
  202.  * PARAMETERS
  203.  *  x1      [IN]        
  204.  *  y1      [IN]        
  205.  *  x2      [IN]        
  206.  *  y2      [IN]        
  207.  * RETURNS
  208.  *  void
  209.  *****************************************************************************/
  210. static void mmi_pen_test_draw_rectangle(S16 x1, S16 y1, S16 x2, S16 y2)
  211. {
  212.     /*----------------------------------------------------------------*/
  213.     /* Local Variables                                                */
  214.     /*----------------------------------------------------------------*/
  215.     /*----------------------------------------------------------------*/
  216.     /* Code Body                                                      */
  217.     /*----------------------------------------------------------------*/
  218.     mmi_pen_test_draw_line(x1, y1, x1, y2);
  219.     mmi_pen_test_draw_line(x1, y2, x2, y2);
  220.     mmi_pen_test_draw_line(x2, y2, x2, y1);
  221.     mmi_pen_test_draw_line(x2, y1, x1, y1);
  222. }
  223. /*****************************************************************************
  224.  * FUNCTION
  225.  *  mmi_pen_test_draw_filled_rectangle
  226.  * DESCRIPTION
  227.  *  
  228.  * PARAMETERS
  229.  *  x1      [IN]        
  230.  *  y1      [IN]        
  231.  *  x2      [IN]        
  232.  *  y2      [IN]        
  233.  * RETURNS
  234.  *  void
  235.  *****************************************************************************/
  236. static void mmi_pen_test_draw_filled_rectangle(S16 x1, S16 y1, S16 x2, S16 y2)
  237. {
  238.     /*----------------------------------------------------------------*/
  239.     /* Local Variables                                                */
  240.     /*----------------------------------------------------------------*/
  241.     S16 x, y;
  242.     /*----------------------------------------------------------------*/
  243.     /* Code Body                                                      */
  244.     /*----------------------------------------------------------------*/
  245.     for (x = x1; x <= x2; x++)
  246.     {
  247.         for (y = y1; y <= y2; y++)
  248.         {
  249.             mmi_pen_test_draw_point(x, y);
  250.         }
  251.     }
  252. }
  253. /*****************************************************************************
  254.  * FUNCTION
  255.  *  mmi_pen_test_down
  256.  * DESCRIPTION
  257.  *  
  258.  * PARAMETERS
  259.  *  pos     [IN]        
  260.  * RETURNS
  261.  *  void
  262.  *****************************************************************************/
  263. static void mmi_pen_test_down(mmi_pen_point_struct pos)
  264. {
  265.     /*----------------------------------------------------------------*/
  266.     /* Local Variables                                                */
  267.     /*----------------------------------------------------------------*/
  268.     /*----------------------------------------------------------------*/
  269.     /* Code Body                                                      */
  270.     /*----------------------------------------------------------------*/
  271.     mmi_pen_test_last_pos = pos;
  272. }
  273. /*****************************************************************************
  274.  * FUNCTION
  275.  *  mmi_pen_test_move
  276.  * DESCRIPTION
  277.  *  
  278.  * PARAMETERS
  279.  *  pos     [IN]        
  280.  * RETURNS
  281.  *  void
  282.  *****************************************************************************/
  283. static void mmi_pen_test_move(mmi_pen_point_struct pos)
  284. {
  285.     /*----------------------------------------------------------------*/
  286.     /* Local Variables                                                */
  287.     /*----------------------------------------------------------------*/
  288.     /*----------------------------------------------------------------*/
  289.     /* Code Body                                                      */
  290.     /*----------------------------------------------------------------*/
  291.     mmi_pen_test_draw_line(mmi_pen_test_last_pos.x, mmi_pen_test_last_pos.y, pos.x, pos.y);
  292.     mmi_pen_test_last_pos = pos;
  293.     mmi_pen_test_refresh_screen();
  294. }
  295. /*****************************************************************************
  296.  * FUNCTION
  297.  *  mmi_pen_test_up
  298.  * DESCRIPTION
  299.  *  
  300.  * PARAMETERS
  301.  *  pos     [IN]        
  302.  * RETURNS
  303.  *  void
  304.  *****************************************************************************/
  305. static void mmi_pen_test_up(mmi_pen_point_struct pos)
  306. {
  307.     /*----------------------------------------------------------------*/
  308.     /* Local Variables                                                */
  309.     /*----------------------------------------------------------------*/
  310.     /*----------------------------------------------------------------*/
  311.     /* Code Body                                                      */
  312.     /*----------------------------------------------------------------*/
  313. }
  314. /*****************************************************************************
  315.  * FUNCTION
  316.  *  mmi_pen_test_long_tap
  317.  * DESCRIPTION
  318.  *  
  319.  * PARAMETERS
  320.  *  pos     [IN]        
  321.  * RETURNS
  322.  *  void
  323.  *****************************************************************************/
  324. static void mmi_pen_test_long_tap(mmi_pen_point_struct pos)
  325. {
  326.     /*----------------------------------------------------------------*/
  327.     /* Local Variables                                                */
  328.     /*----------------------------------------------------------------*/
  329.     /*----------------------------------------------------------------*/
  330.     /* Code Body                                                      */
  331.     /*----------------------------------------------------------------*/
  332.     mmi_pen_test_draw_filled_rectangle((S16) (pos.x - 2), (S16) (pos.y - 2), (S16) (pos.x + 2), (S16) (pos.y + 2));
  333.     mmi_pen_test_refresh_screen();
  334. }
  335. /*****************************************************************************
  336.  * FUNCTION
  337.  *  mmi_pen_test_repeat
  338.  * DESCRIPTION
  339.  *  
  340.  * PARAMETERS
  341.  *  pos     [IN]        
  342.  * RETURNS
  343.  *  void
  344.  *****************************************************************************/
  345. static void mmi_pen_test_repeat(mmi_pen_point_struct pos)
  346. {
  347.     /*----------------------------------------------------------------*/
  348.     /* Local Variables                                                */
  349.     /*----------------------------------------------------------------*/
  350.     /*----------------------------------------------------------------*/
  351.     /* Code Body                                                      */
  352.     /*----------------------------------------------------------------*/
  353.     mmi_pen_test_draw_rectangle((S16) (pos.x - 4), (S16) (pos.y - 4), (S16) (pos.x + 4), (S16) (pos.y + 4));
  354.     mmi_pen_test_refresh_screen();
  355. }
  356. /*****************************************************************************
  357.  * FUNCTION
  358.  *  mmi_exit_pen_test_screen
  359.  * DESCRIPTION
  360.  *  
  361.  * PARAMETERS
  362.  *  void
  363.  * RETURNS
  364.  *  void
  365.  *****************************************************************************/
  366. static void mmi_exit_pen_test_screen(void)
  367. {
  368.     /*----------------------------------------------------------------*/
  369.     /* Local Variables                                                */
  370.     /*----------------------------------------------------------------*/
  371.     /*----------------------------------------------------------------*/
  372.     /* Code Body                                                      */
  373.     /*----------------------------------------------------------------*/
  374.     mmi_pen_reset();
  375. }
  376. /*****************************************************************************
  377.  * FUNCTION
  378.  *  mmi_redraw_pen_test_screen
  379.  * DESCRIPTION
  380.  *  
  381.  * PARAMETERS
  382.  *  void
  383.  * RETURNS
  384.  *  void
  385.  *****************************************************************************/
  386. static void mmi_redraw_pen_test_screen(void)
  387. {
  388.     /*----------------------------------------------------------------*/
  389.     /* Local Variables                                                */
  390.     /*----------------------------------------------------------------*/
  391.     /*----------------------------------------------------------------*/
  392.     /* Code Body                                                      */
  393.     /*----------------------------------------------------------------*/
  394.     gui_lock_double_buffer();
  395.     RedrawCategoryFunction();
  396.     mmi_pen_test_draw_from_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  397.     gui_unlock_double_buffer();
  398.     gui_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
  399. }
  400. /*****************************************************************************
  401.  * FUNCTION
  402.  *  mmi_entry_pen_test_screen
  403.  * DESCRIPTION
  404.  *  
  405.  * PARAMETERS
  406.  *  void
  407.  * RETURNS
  408.  *  void
  409.  *****************************************************************************/
  410. static void mmi_entry_pen_test_screen(void)
  411. {
  412.     /*----------------------------------------------------------------*/
  413.     /* Local Variables                                                */
  414.     /*----------------------------------------------------------------*/
  415.     /*----------------------------------------------------------------*/
  416.     /* Code Body                                                      */
  417.     /*----------------------------------------------------------------*/
  418.     EntryNewScreen(0, NULL, mmi_exit_pen_test_screen, NULL);
  419.     ShowCategory66Screen(STR_GLOBAL_UNFINISHED, 0, 0, 0, STR_GLOBAL_BACK, 0, (U8*) L"", 0, NULL);
  420.     RedrawCategoryFunction = mmi_redraw_pen_test_screen;    /* For PC simulator architecture */
  421.     mmi_pen_test_clear_screen();
  422.     mmi_pen_register_down_handler(mmi_pen_test_down);
  423.     mmi_pen_register_move_handler(mmi_pen_test_move);
  424.     mmi_pen_register_up_handler(mmi_pen_test_up);
  425.     mmi_pen_register_long_tap_handler(mmi_pen_test_long_tap);
  426.     mmi_pen_register_repeat_handler(mmi_pen_test_repeat);
  427.     SetRightSoftkeyFunction(GoBackHistory, KEY_EVENT_UP);
  428. }
  429. /*****************************************************************************
  430.  * FUNCTION
  431.  *  mmi_pen_unit_test
  432.  * DESCRIPTION
  433.  *  
  434.  * PARAMETERS
  435.  *  void
  436.  * RETURNS
  437.  *  void
  438.  *****************************************************************************/
  439. void mmi_pen_unit_test(void)
  440. {
  441.     /*----------------------------------------------------------------*/
  442.     /* Local Variables                                                */
  443.     /*----------------------------------------------------------------*/
  444.     /*----------------------------------------------------------------*/
  445.     /* Code Body                                                      */
  446.     /*----------------------------------------------------------------*/
  447.     mmi_entry_pen_test_screen();
  448. }
  449. #endif /* MMI_PEN_UNIT_TEST */ 
  450. #elif ( !defined(__MMI_TOUCH_SCREEN__) || !defined(__MMI_HANDWRITING_PAD__) ) && !defined(__MTK_TARGET__) && !defined(MMI_ON_WIN32)
  451. #include "PixtelDataTypes.h"
  452. /*****************************************************************************
  453.  * FUNCTION
  454.  *  mmi_pen_MODIS_enqueue_down
  455.  * DESCRIPTION
  456.  *  
  457.  * PARAMETERS
  458.  *  x       [IN]        
  459.  *  y       [IN]        
  460.  * RETURNS
  461.  *  void
  462.  *****************************************************************************/
  463. void mmi_pen_MODIS_enqueue_down(S16 x, S16 y)
  464. {
  465.     /*----------------------------------------------------------------*/
  466.     /* Local Variables                                                */
  467.     /*----------------------------------------------------------------*/
  468.     /*----------------------------------------------------------------*/
  469.     /* Code Body                                                      */
  470.     /*----------------------------------------------------------------*/
  471. }
  472. /*****************************************************************************
  473.  * FUNCTION
  474.  *  mmi_pen_MODIS_enqueue_move
  475.  * DESCRIPTION
  476.  *  
  477.  * PARAMETERS
  478.  *  x       [IN]        
  479.  *  y       [IN]        
  480.  * RETURNS
  481.  *  void
  482.  *****************************************************************************/
  483. void mmi_pen_MODIS_enqueue_move(S16 x, S16 y)
  484. {
  485.     /*----------------------------------------------------------------*/
  486.     /* Local Variables                                                */
  487.     /*----------------------------------------------------------------*/
  488.     /*----------------------------------------------------------------*/
  489.     /* Code Body                                                      */
  490.     /*----------------------------------------------------------------*/
  491. }
  492. /*****************************************************************************
  493.  * FUNCTION
  494.  *  mmi_pen_MODIS_enqueue_up
  495.  * DESCRIPTION
  496.  *  
  497.  * PARAMETERS
  498.  *  x       [IN]        
  499.  *  y       [IN]        
  500.  * RETURNS
  501.  *  void
  502.  *****************************************************************************/
  503. void mmi_pen_MODIS_enqueue_up(S16 x, S16 y)
  504. {
  505.     /*----------------------------------------------------------------*/
  506.     /* Local Variables                                                */
  507.     /*----------------------------------------------------------------*/
  508.     /*----------------------------------------------------------------*/
  509.     /* Code Body                                                      */
  510.     /*----------------------------------------------------------------*/
  511. }
  512. #elif ( defined(__MMI_TOUCH_SCREEN__) || defined(__MMI_HANDWRITING_PAD__) ) && defined(__MTK_TARGET__)
  513. /*****************************************************************************
  514.  *
  515.  * Target integration 
  516.  *
  517.  ****************************************************************************/
  518. #include "stdC.h"
  519. #include "PixtelDataTypes.h"
  520. #include "TimerEvents.h"
  521. #include "KeyBrd.h"
  522. #include "TaskInit.h"
  523. #include "MMITaskProt.h"
  524. #include "FrameworkStruct.h"
  525. #include "EventsGprot.h"
  526. #include "EventsDcl.h"
  527. #include "EventsDef.h"
  528. #include "gui_data_types.h"
  529. #include "gui.h"
  530. #include "GpioInc.h"
  531. #include "IdleAppProt.h"
  532. #include "TouchScreenGprot.h"
  533. #include "DebugInitDef.h"
  534. #include "Touch_Panel.h"
  535. #include "Drv_Comm.h"   /*  mmi_pen_touch_panel_sendilm() */
  536. #include "gdi_include.h"
  537. #include "ScreenRotationGprot.h"
  538. #include "touch_panel_custom.h" /* get handwriting pad setting */
  539. // TODO: make ext_region as NULL
  540. /***************************************************************************** 
  541. * Define
  542. *****************************************************************************/
  543. /* Time between Pen Long-tap and Pen Repeat */
  544. #define MMI_PEN_POLLING_PERIOD            (4 * 10)      /* ms */
  545. /* Size of look-ahead buffer */
  546. #define MMI_PEN_LOOKAHEAD_BUFFER_SIZE     (25)
  547. /* 
  548.  * Delayed polling period time. (We allow only 5 Pen Down event per sec) 
  549.  * We restrict the frequency of incoming Pen Down events in MMI framework instead of 
  550.  * driver debounce time because driver debounce time needs to be smaller than sampling period. 
  551.  */
  552. #define MMI_PEN_DEBOUNCE_POLLING_DELAY    (1000 / 5 - MMI_PEN_POLLING_PERIOD)
  553. /***************************************************************************** 
  554. * Typedef 
  555. *****************************************************************************/
  556. typedef struct
  557. {
  558.     /* mmi_pen_block() & mmi_pen_unblock() */
  559.     U32 is_blocked:1;
  560.     /* mmi_pen_enable() & mmi_pen_disable() */
  561.     U32 is_enabled:1;
  562.     /* After PEN_DOWN, mmi_pen_reset() set 'is_pen_down' to 0 */
  563.     U32 is_pen_down:1;
  564.     /* Debug only. After STROKE_DOWN, mmi_pen_reset() set 'is_stroke_down' to 0 */
  565.     U32 is_stroke_down:1;
  566.     /* Pen down in hand-writing area, but we still do not know if it is a stroke */
  567.     U32 is_pending_stroke_event:1;
  568.     /* 
  569.      * Set by mmi_pen_change_handwriting_area() and mmi_pen_stop_capture_strokes().
  570.      * Close stroke and reset touch screen when pen is up. 
  571.      */
  572.     U32 reset_stroke_on_pen_up:1;
  573.     /*
  574.      * Reset sampling rate when pen is reset. 
  575.      * This is used when we change the sampling rate temporarily.
  576.      */
  577.     U32 restore_sampling_rate_on_reset:1;
  578.     /* Pen down position */
  579.     mmi_pen_point_struct pen_down_pos;
  580.     /* The current pen position */
  581.     mmi_pen_point_struct pen_current_pos;
  582.     /* Block index of handwriting area */
  583.     S32 stroke_down_block_index;
  584.     /* For stroke */
  585.     S32 num_points_queued;
  586.     /* Number of events in look-ahead buffer */
  587.     S32 num_lookahead_events;
  588. } mmi_pen_context_struct;
  589. /***************************************************************************** 
  590. * Local Variable
  591. *****************************************************************************/
  592. /* Global pen context that is reset in mmi_pen_reset() and mmi_pen_disable() */
  593. static mmi_pen_context_struct g_pen_cntx;
  594. /* Only normal poweron initialize touch screen. Otherwise, touch screen is not initialized */
  595. static kal_bool g_pen_initialized;
  596. /*
  597.  * Whether the timer for mmi_pen_poll_hdlr() is started
  598.  * 
  599.  * 'g_pen_polling_timer_started' is not put inside g_pen_cntx because 
  600.  * 1. We want to restrict the frequency of incoming pen events
  601.  * 2. When polling timer is started, we do not stop it.
  602.  * 3. 'g_pen_polling_timer_started' should not be cleared in mmi_pen_reset() or mmi_pen_disable()
  603.  */
  604. MMI_BOOL g_pen_polling_timer_started;
  605. /* Whether MSG_ID_TP_EVENT_IND is already en-queued in MMI external queue */
  606. /* It is not put inside g_pen_cntx for similar reason like g_pen_polling_timer_started */
  607. U32 g_pen_is_driver_indication_in_queue;
  608. static mmi_pen_hdlr g_pen_event_table[MMI_PEN_EVENT_TYPE_MAX];
  609. static mmi_pen_hdlr g_pen_stroke_table[MMI_PEN_STROKE_TYPE_MAX];
  610. static void (*g_pen_stroke_pre_move) (void);
  611. static void (*g_pen_stroke_post_move) (void);
  612. /* 
  613.  * Stroke buffer 
  614.  */
  615. static S32 g_pen_stroke_max_points;
  616. static mmi_pen_point_struct *g_pen_stroke_points;
  617. /* Used for the new multi-block feature.
  618.    Note: the data can be put into driver, but we do not want to change driver interface 
  619.    because customers may port their driver */
  620. static mmi_pen_handwriting_area_struct g_pen_stroke_areas[HAND_WRITING_AREA_NUM];
  621. static S32 g_pen_num_stroke_area;
  622. /* Minimum square distance (diff_x^2 + diff_y^2)  */
  623. static S16 g_pen_stroke_min_offset_square;
  624. /* 
  625.  * Lookahead buffer  
  626.  */
  627. static TouchPanelEventStruct g_pen_lookahead_buffer[MMI_PEN_LOOKAHEAD_BUFFER_SIZE];
  628. /***************************************************************************** 
  629. * Local Function
  630. *****************************************************************************/
  631. /*****************************************************************************
  632.  * FUNCTION
  633.  *  mmi_pen_get_distance_square
  634.  * DESCRIPTION
  635.  *  
  636.  * PARAMETERS
  637.  *  pos1        [IN]        
  638.  *  pos2        [IN]        
  639.  * RETURNS
  640.  *  
  641.  *****************************************************************************/
  642. static S16 mmi_pen_get_distance_square(mmi_pen_point_struct pos1, mmi_pen_point_struct pos2)
  643. {
  644.     /*----------------------------------------------------------------*/
  645.     /* Local Variables                                                */
  646.     /*----------------------------------------------------------------*/
  647.     S16 diff_x = pos1.x - pos2.x;
  648.     S16 diff_y = pos1.y - pos2.y;
  649.     /*----------------------------------------------------------------*/
  650.     /* Code Body                                                      */
  651.     /*----------------------------------------------------------------*/
  652.     if (diff_x < 0)
  653.     {
  654.         diff_x = -diff_x;
  655.     }
  656.     if (diff_y < 0)
  657.     {
  658.         diff_y = -diff_y;
  659.     }
  660.     return (diff_x * diff_x) + (diff_y * diff_y);
  661. }
  662. /*****************************************************************************
  663.  * FUNCTION
  664.  *  mmi_pen_push_stroke_point
  665.  * DESCRIPTION
  666.  *  
  667.  * PARAMETERS
  668.  *  x       [IN]        
  669.  *  y       [IN]        
  670.  * RETURNS
  671.  *  void
  672.  *****************************************************************************/
  673. static void mmi_pen_push_stroke_point(S16 x, S16 y)
  674. {
  675.     /*----------------------------------------------------------------*/
  676.     /* Local Variables                                                */
  677.     /*----------------------------------------------------------------*/
  678.     /*----------------------------------------------------------------*/
  679.     /* Code Body                                                      */
  680.     /*----------------------------------------------------------------*/
  681.     if (g_pen_cntx.num_points_queued >= g_pen_stroke_max_points - 2)
  682.     {
  683.         /* Leave the last two slots for (-1, 0) and (-1, -1) */
  684.         return;
  685.     }
  686.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = x;
  687.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = y;
  688.     g_pen_cntx.num_points_queued++;
  689. }
  690. /*****************************************************************************
  691.  * FUNCTION
  692.  *  mmi_pen_push_stroke_end
  693.  * DESCRIPTION
  694.  *  
  695.  * PARAMETERS
  696.  *  void
  697.  * RETURNS
  698.  *  void
  699.  *****************************************************************************/
  700. static void mmi_pen_push_stroke_end(void)
  701. {
  702.     /*----------------------------------------------------------------*/
  703.     /* Local Variables                                                */
  704.     /*----------------------------------------------------------------*/
  705.     /*----------------------------------------------------------------*/
  706.     /* Code Body                                                      */
  707.     /*----------------------------------------------------------------*/
  708.     if (g_pen_cntx.num_points_queued >= g_pen_stroke_max_points - 1)
  709.     {
  710.         return;
  711.     }
  712. #if defined(__MMI_HANWANG__)
  713.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = -1;
  714.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = 0;
  715. #elif defined(__MMI_PENPOWER__)
  716.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = -1;
  717.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = -1;
  718. #else 
  719.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = -1;
  720.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = 0;
  721. #endif 
  722.     g_pen_cntx.num_points_queued++;
  723. }
  724. /*****************************************************************************
  725.  * FUNCTION
  726.  *  mmi_pen_push_char_end
  727.  * DESCRIPTION
  728.  *  
  729.  * PARAMETERS
  730.  *  void
  731.  * RETURNS
  732.  *  void
  733.  *****************************************************************************/
  734. static void mmi_pen_push_char_end(void)
  735. {
  736.     /*----------------------------------------------------------------*/
  737.     /* Local Variables                                                */
  738.     /*----------------------------------------------------------------*/
  739.     /*----------------------------------------------------------------*/
  740.     /* Code Body                                                      */
  741.     /*----------------------------------------------------------------*/
  742.     if (g_pen_cntx.num_points_queued >= g_pen_stroke_max_points)
  743.     {
  744.         MMI_DBG_ASSERT(0);  /* queue full */
  745.         return;
  746.     }
  747. #if defined(__MMI_HANWANG__)
  748.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = -1;
  749.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = -1;
  750. #elif defined(__MMI_PENPOWER__)
  751.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = -1;
  752.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = 0;
  753. #else 
  754.     g_pen_stroke_points[g_pen_cntx.num_points_queued].x = -1;
  755.     g_pen_stroke_points[g_pen_cntx.num_points_queued].y = -1;
  756. #endif 
  757.     g_pen_cntx.num_points_queued++;
  758. }
  759. /*****************************************************************************
  760.  * FUNCTION
  761.  *  mmi_pen_lookup_handwriting_block
  762.  * DESCRIPTION
  763.  *  Search which handwriting block contains a point (multi-block mode)
  764.  * PARAMETERS
  765.  *  x       [IN]
  766.  *  y       [IN]
  767.  * RETURNS
  768.  *  The index of handwriting block. 0 for block-0 or extended area, -1 if not found.
  769.  *****************************************************************************/
  770. static S32 mmi_pen_lookup_handwriting_block(S32 x, S32 y)
  771. {
  772.     /*----------------------------------------------------------------*/
  773.     /* Local Variables                                                */
  774.     /*----------------------------------------------------------------*/
  775.     int i;
  776.     /*----------------------------------------------------------------*/
  777.     /* Code Body                                                      */
  778.     /*----------------------------------------------------------------*/
  779.     for (i = 0; i < g_pen_num_stroke_area; i++)
  780.     {
  781.         if (x >= g_pen_stroke_areas[i].x1 && x <= g_pen_stroke_areas[i].x2 &&
  782.             y >= g_pen_stroke_areas[i].y1 && y <= g_pen_stroke_areas[i].y2)
  783.         {
  784.             return i;
  785.         }
  786.     }
  787.     return -1;
  788. }
  789. /*****************************************************************************
  790.  * FUNCTION
  791.  *  mmi_pen_fix_multi_block_pen_position
  792.  * DESCRIPTION
  793.  *  Restrict stroke position to the current handwriting block
  794.  * PARAMETERS
  795.  *  pos       [IN/OUT]
  796.  * RETURNS
  797.  *  void
  798.  *****************************************************************************/
  799. void mmi_pen_fix_multi_block_pen_position(mmi_pen_point_struct *pos)
  800. {
  801.     /*----------------------------------------------------------------*/
  802.     /* Local Variables                                                */
  803.     /*----------------------------------------------------------------*/
  804.     mmi_pen_handwriting_area_struct *block;
  805.     /*----------------------------------------------------------------*/
  806.     /* Code Body                                                      */
  807.     /*----------------------------------------------------------------*/
  808.     MMI_ASSERT(g_pen_cntx.stroke_down_block_index >= 0 &&
  809.                g_pen_cntx.stroke_down_block_index < g_pen_num_stroke_area);
  810.     block = &g_pen_stroke_areas[g_pen_cntx.stroke_down_block_index];
  811.     
  812.     if (pos->x < block->x1)
  813.     {
  814.         pos->x = block->x1;
  815.     }
  816.     else if (pos->x > block->x2)
  817.     {
  818.         pos->x = block->x2;
  819.     }
  820.     
  821.     if (pos->y < block->y1)
  822.     {
  823.         pos->y = block->y1;
  824.     }
  825.     else if (pos->y > block->y2)
  826.     {
  827.         pos->y = block->y2;
  828.     }    
  829. }
  830. /*
  831.  * Look-ahead buffer of pen events (used in mmi_pen_poll_hdlr())
  832.  *
  833.  * If our MMI is blocked and fails to process incoming pen events fast enough, 
  834.  * it may have unexpected results (e.g. extq full) unless we drop certain pending events.
  835.  */
  836. /* 
  837.  * Fill the data in lookahead buffer. 
  838.  *
  839.  * The sampling rate of pen events (MMI_PEN_SAMPLING_PERIOD_1) is very high,
  840.  * If we find paired Pen Down/Up, it means that MMI is blocked.
  841.  *
  842.  * 1. If there are consecutive Pen Move events, keep the last one.
  843.  * 2. If there are paired "Pen Down ~ Pen Up", drop it.
  844.  *
  845.  * We do not drop stroke events because it is used by handwriting engine.
  846.  */
  847. /*****************************************************************************
  848.  * FUNCTION
  849.  *  mmi_pen_lookahead_buffer_fill_data
  850.  * DESCRIPTION
  851.  *  
  852.  * PARAMETERS
  853.  *  void
  854.  * RETURNS
  855.  *  void
  856.  *****************************************************************************/
  857. static void mmi_pen_lookahead_buffer_fill_data(void)
  858. {
  859.     /*----------------------------------------------------------------*/
  860.     /* Local Variables                                                */
  861.     /*----------------------------------------------------------------*/
  862.     S32 rindex, windex;
  863.     S32 first_down_index, last_up_index;
  864.     TouchPanelEventStruct touch_data;
  865. #ifdef __MMI_SCREEN_ROTATE__
  866.     /* U8 gdi_rotate_value; */
  867.     MMI_BOOL screen_rotated = MMI_FALSE;
  868.     /*----------------------------------------------------------------*/
  869.     /* Code Body                                                      */
  870.     /*----------------------------------------------------------------*/
  871.     if (mmi_frm_get_screen_rotate() != MMI_FRM_SCREEN_ROTATE_0)
  872.     {
  873.         /* Even if GDI base layer is rotated, we do not translate pen coordinates 
  874.            unless mmi_frm_screen_rotate() is invoked. */
  875.         screen_rotated = MMI_TRUE;
  876.     }
  877.     /* gdi_rotate_value = gdi_layer_get_base_layer_rotation(); */
  878. #endif /* __MMI_SCREEN_ROTATE__ */ 
  879.     for (windex = g_pen_cntx.num_lookahead_events; windex < MMI_PEN_LOOKAHEAD_BUFFER_SIZE; windex++)
  880.     {
  881.         if (!touch_panel_get_event(&touch_data))
  882.         {
  883.             break;
  884.         }
  885.     #ifdef __MMI_SCREEN_ROTATE__
  886.         if (screen_rotated)
  887.         {
  888.             S32 tmp_x = touch_data.x_adc, tmp_y = touch_data.y_adc;
  889.             /* W06.04 Replace functions for GDI LCD Rotate */
  890.             gdi_rotate_map_absolute_hw_to_lcd(&tmp_x, &tmp_y);
  891.             /* gdi_layer_map_rotated_coordinates(gdi_rotate_value, &tmp_x, &tmp_y); */
  892.             touch_data.x_adc = (kal_int16) tmp_x;
  893.             touch_data.y_adc = (kal_int16) tmp_y;
  894.         }
  895.     #endif /* __MMI_SCREEN_ROTATE__ */ 
  896.         g_pen_lookahead_buffer[windex] = touch_data;
  897.     }
  898.     g_pen_cntx.num_lookahead_events = windex;
  899.     if (g_pen_cntx.num_lookahead_events == 0)
  900.     {
  901.         return; /* Exit point */
  902.     }
  903.     /* Remove paired Pen Down/Up */
  904.     first_down_index = -1;
  905.     last_up_index = -1;
  906.     for (rindex = 1 /* not including the first event */ ; rindex < g_pen_cntx.num_lookahead_events; rindex++)
  907.     {
  908.         switch (g_pen_lookahead_buffer[rindex].event)
  909.         {
  910.             case PEN_DOWN:
  911.                 if (first_down_index < 0)
  912.                 {
  913.                     first_down_index = rindex;
  914.                 }
  915.                 break;
  916.             case PEN_UP:
  917.                 last_up_index = rindex;
  918.                 break;
  919.             case PEN_MOVE:
  920.             case PEN_LONGTAP:
  921.             case PEN_REPEAT:
  922.                 break;
  923.             default:
  924.                 /* Some events can not be dropped */
  925.                 first_down_index = -1;
  926.         }
  927.     }
  928.     if (first_down_index > 0 && last_up_index > first_down_index)
  929.     {
  930.         if (last_up_index < g_pen_cntx.num_lookahead_events - 1)
  931.         {
  932.             Trace2(MMI_TRACE_G1_FRM, "[Pen] Drop lookahead buffer (%d - %d) n", first_down_index, last_up_index);
  933.             memmove(
  934.                 &g_pen_lookahead_buffer[first_down_index],
  935.                 &g_pen_lookahead_buffer[last_up_index + 1],
  936.                 sizeof(TouchPanelEventStruct) * (g_pen_cntx.num_lookahead_events - 1 - last_up_index));
  937.         }
  938.         g_pen_cntx.num_lookahead_events -= (last_up_index - first_down_index + 1);
  939.     }
  940.     /* 
  941.      * If there are multiple Pen Move events queued, it means that pen handler can not 
  942.      * process the events fast enough. We should drop Pen Move events before the last one in queue.
  943.      */
  944.     for (windex = 0, rindex = 0; rindex < g_pen_cntx.num_lookahead_events; rindex++)
  945.     {
  946.         if (g_pen_lookahead_buffer[rindex].event == PEN_MOVE &&
  947.             rindex < g_pen_cntx.num_lookahead_events - 1 && g_pen_lookahead_buffer[rindex + 1].event == PEN_MOVE)
  948.         {
  949.             /* Drop this Pen Move event. */
  950.             continue;
  951.         }
  952.         if (windex != rindex)
  953.         {
  954.             g_pen_lookahead_buffer[windex] = g_pen_lookahead_buffer[rindex];
  955.         }
  956.         windex++;
  957.     }
  958.     g_pen_cntx.num_lookahead_events = windex;
  959. }
  960. /* Buffered version of touch_panel_peek_event() */
  961. /*****************************************************************************
  962.  * FUNCTION
  963.  *  mmi_pen_lookahead_buffer_peek_event
  964.  * DESCRIPTION
  965.  *  
  966.  * PARAMETERS
  967.  *  touch_data      [OUT]     
  968.  * RETURNS
  969.  *  
  970.  *****************************************************************************/
  971. static kal_bool mmi_pen_lookahead_buffer_peek_event(TouchPanelEventStruct *touch_data)
  972. {
  973.     /*----------------------------------------------------------------*/
  974.     /* Local Variables                                                */
  975.     /*----------------------------------------------------------------*/
  976.     /*----------------------------------------------------------------*/
  977.     /* Code Body                                                      */
  978.     /*----------------------------------------------------------------*/
  979.     if (!g_pen_cntx.num_lookahead_events)
  980.     {
  981.         mmi_pen_lookahead_buffer_fill_data();
  982.     }
  983.     if (g_pen_cntx.num_lookahead_events)
  984.     {
  985.         *touch_data = g_pen_lookahead_buffer[0];
  986.         return KAL_TRUE;
  987.     }
  988.     else
  989.     {
  990.         return KAL_FALSE;
  991.     }
  992. }
  993. /* Buffered version of touch_panel_get_event() */
  994. /*****************************************************************************
  995.  * FUNCTION
  996.  *  mmi_pen_lookahead_buffer_get_event
  997.  * DESCRIPTION
  998.  *  
  999.  * PARAMETERS
  1000.  *  touch_data      [OUT]     
  1001.  * RETURNS
  1002.  *  
  1003.  *****************************************************************************/
  1004. static kal_bool mmi_pen_lookahead_buffer_get_event(TouchPanelEventStruct *touch_data)
  1005. {
  1006.     /*----------------------------------------------------------------*/
  1007.     /* Local Variables                                                */
  1008.     /*----------------------------------------------------------------*/
  1009.     S32 i;
  1010.     /*----------------------------------------------------------------*/
  1011.     /* Code Body                                                      */
  1012.     /*----------------------------------------------------------------*/
  1013.     mmi_pen_lookahead_buffer_fill_data();
  1014.     if (g_pen_cntx.num_lookahead_events > 0)
  1015.     {
  1016.         *touch_data = g_pen_lookahead_buffer[0];
  1017.         g_pen_cntx.num_lookahead_events--;
  1018.         for (i = 0; i < g_pen_cntx.num_lookahead_events; i++)
  1019.         {
  1020.             g_pen_lookahead_buffer[i] = g_pen_lookahead_buffer[i + 1];
  1021.         }
  1022.         return KAL_TRUE;
  1023.     }
  1024.     else
  1025.     {
  1026.         return KAL_FALSE;
  1027.     }
  1028. }
  1029. /*
  1030.  * Core polling loop for pen events
  1031.  */
  1032. /*****************************************************************************
  1033.  * FUNCTION
  1034.  *  mmi_pen_poll_hdlr
  1035.  * DESCRIPTION
  1036.  *  
  1037.  * PARAMETERS
  1038.  *  void
  1039.  * RETURNS
  1040.  *  void
  1041.  *****************************************************************************/
  1042. static void mmi_pen_poll_hdlr(void)
  1043. {
  1044.     /*----------------------------------------------------------------*/
  1045.     /* Local Variables                                                */
  1046.     /*----------------------------------------------------------------*/
  1047.     TouchPanelEventStruct data;
  1048.     MMI_BOOL is_stroke_move = MMI_FALSE;
  1049.     MMI_BOOL has_incoming_event = MMI_FALSE;
  1050.     MMI_BOOL delay_polling_timer = MMI_FALSE;
  1051.     /*----------------------------------------------------------------*/
  1052.     /* Code Body                                                      */
  1053.     /*----------------------------------------------------------------*/
  1054.     g_pen_polling_timer_started = MMI_FALSE;
  1055.     Trace2(
  1056.         MMI_TRACE_G1_FRM,
  1057.         "[Pen] mmi_pen_poll_hdlr() - %d %d n",
  1058.         (int)g_pen_cntx.is_enabled,
  1059.         (int)g_pen_cntx.num_lookahead_events);
  1060.     if (!g_pen_cntx.is_enabled)
  1061.     {
  1062.         /* Note: We do not stop polling timer even in mmi_pen_disable() */
  1063.         return;
  1064.     }
  1065.     /* Although pen events (move, repeat) should keep backlight on, 
  1066.        Playing i-melody may turn on/off backlight actively. 
  1067.        We do not call mmi_pen_reset() when pen is down because some apps 
  1068.        may not handle Pen Abort event. */
  1069.     if (!IsBacklightOn() && !g_pen_cntx.is_pen_down)
  1070.     {
  1071.         Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_poll_hdlr() - no backlightn");
  1072.         TurnOnBacklight(MMI_TRUE);
  1073.         mmi_pen_reset();    /* flush queue */
  1074.         return;
  1075.     }
  1076.     while (!delay_polling_timer && mmi_pen_lookahead_buffer_get_event(&data))
  1077.     {
  1078.         mmi_pen_point_struct pos;
  1079.         if (!g_pen_cntx.is_enabled)
  1080.         {
  1081.             /* Previous pen handlers might call mmi_pen_disable(), but
  1082.                we should already flush driver pen queue on mmi_pen_disable() */
  1083.             MMI_DBG_ASSERT(0);
  1084.             break;
  1085.         }
  1086.         pos.x = (S16) data.x_adc;
  1087.         pos.y = (S16) data.y_adc;
  1088.     #if 0
  1089. /* under construction !*/
  1090. /* under construction !*/
  1091. /* under construction !*/
  1092. /* under construction !*/
  1093. /* under construction !*/
  1094. /* under construction !*/
  1095. /* under construction !*/
  1096. /* under construction !*/
  1097. /* under construction !*/
  1098. /* under construction !*/
  1099. /* under construction !*/
  1100. /* under construction !*/
  1101. /* under construction !*/
  1102. /* under construction !*/
  1103. /* under construction !*/
  1104. /* under construction !*/
  1105. /* under construction !*/
  1106. /* under construction !*/
  1107. /* under construction !*/
  1108. /* under construction !*/
  1109. /* under construction !*/
  1110. /* under construction !*/
  1111. /* under construction !*/
  1112. /* under construction !*/
  1113.     #endif /* 0 */ 
  1114.         has_incoming_event = MMI_TRUE;
  1115.         if (is_stroke_move && (data.event != STROKE_MOVE))
  1116.         {
  1117.             is_stroke_move = MMI_FALSE;
  1118.             if (g_pen_stroke_post_move)
  1119.             {
  1120.                 g_pen_stroke_post_move();
  1121.             }
  1122.         }
  1123.         g_pen_cntx.pen_current_pos = pos;
  1124.         switch (data.event)
  1125.         {
  1126.                 /* Call pen handler after setting context variables because pen handler might invoke mmi_pen_reset() */
  1127.             case PEN_DOWN:
  1128.                 MMI_DBG_ASSERT(!g_pen_cntx.is_pen_down);
  1129.                 g_pen_cntx.is_pen_down = 1;
  1130.                 g_pen_cntx.pen_down_pos = pos;
  1131.                 if (g_pen_event_table[MMI_PEN_EVENT_DOWN])
  1132.                 {
  1133.                     (g_pen_event_table[MMI_PEN_EVENT_DOWN]) (pos);
  1134.                 }
  1135.                 break;
  1136.                 
  1137.             case PEN_UP:
  1138.                 MMI_DBG_ASSERT(g_pen_cntx.is_pen_down);
  1139.                 g_pen_cntx.is_pen_down = 0;
  1140.                 if (g_pen_event_table[MMI_PEN_EVENT_UP])
  1141.                 {
  1142.                     (g_pen_event_table[MMI_PEN_EVENT_UP]) (pos);
  1143.                 }
  1144.                 if (g_pen_cntx.reset_stroke_on_pen_up)
  1145.                 {
  1146.                     if (g_pen_stroke_max_points > 0)
  1147.                     {
  1148.                         mmi_pen_end_strokes_of_character();
  1149.                         mmi_pen_reset();
  1150.                         mmi_pen_begin_strokes_of_character();
  1151.                     }
  1152.                     else
  1153.                     {
  1154.                         mmi_pen_reset();
  1155.                     }
  1156.                 }
  1157.                 /* Delay processing futher events */
  1158.                 delay_polling_timer = MMI_TRUE;
  1159.                 break;
  1160.                 
  1161.             case PEN_MOVE:
  1162.                 MMI_DBG_ASSERT(g_pen_cntx.is_pen_down);
  1163.                 if (g_pen_event_table[MMI_PEN_EVENT_MOVE])
  1164.                 {
  1165.                     (g_pen_event_table[MMI_PEN_EVENT_MOVE]) (pos);
  1166.                 }
  1167.                 break;
  1168.                 
  1169.             case PEN_LONGTAP:
  1170.                 MMI_DBG_ASSERT(g_pen_cntx.is_pen_down);
  1171.                 if (g_pen_event_table[MMI_PEN_EVENT_LONG_TAP])
  1172.                 {
  1173.                     (g_pen_event_table[MMI_PEN_EVENT_LONG_TAP]) (pos);
  1174.                 }
  1175.                 break;
  1176.                 
  1177.             case PEN_REPEAT:
  1178.                 MMI_DBG_ASSERT(g_pen_cntx.is_pen_down);
  1179.                 if (g_pen_event_table[MMI_PEN_EVENT_REPEAT])
  1180.                 {
  1181.                     (g_pen_event_table[MMI_PEN_EVENT_REPEAT]) (pos);
  1182.                 }
  1183.                 break;
  1184.                 
  1185.             case PEN_ABORT:
  1186.                 /* Driver queue full */
  1187.                 mmi_pen_reset();
  1188.                 break;
  1189.                 
  1190.             case TP_UNKNOWN_EVENT:
  1191.                 MMI_ASSERT(0);
  1192.                 break;
  1193.                 
  1194.             case STROKE_MOVE:
  1195.                 MMI_DBG_ASSERT(g_pen_cntx.is_stroke_down && g_pen_cntx.is_pen_down);
  1196.                 /* mmi_pen_reset() should be used after mmi_pen_stop_capture_strokes() such that enqueued stroke events are flushed. */
  1197.                 MMI_DBG_ASSERT(g_pen_stroke_max_points > 0);
  1198.                 if (g_pen_cntx.is_pending_stroke_event)
  1199.                 {
  1200.                     if (mmi_pen_get_distance_square(g_pen_cntx.pen_down_pos, pos) > g_pen_stroke_min_offset_square)
  1201.                     {
  1202.                         g_pen_cntx.is_pending_stroke_event = 0;
  1203.                         g_pen_stroke_min_offset_square = 0; /* The next stroke does not have minimum offset */
  1204.                         /* Pen Down -> Pen Move -> Pen Abort -> Stroke Down -> Stroke Move -> Stroke Up 
  1205.                            There is no stroke points queued before the first stroke is created
  1206.                            If we set 'reset_stroke_on_pen_up' in previous Pen Down handler and invoke mmi_pen_reset() in Stroke Up, 
  1207.                            all stroke points in between are discarded */
  1208.                         g_pen_cntx.reset_stroke_on_pen_up = 0;
  1209.                         /* Abort previous Pen Down event */
  1210.                         if (g_pen_event_table[MMI_PEN_EVENT_ABORT])
  1211.                         {
  1212.                             (g_pen_event_table[MMI_PEN_EVENT_ABORT]) (g_pen_cntx.pen_down_pos);
  1213.                         }
  1214.                         /* Handle Stroke Down */
  1215.                         mmi_pen_push_stroke_point(g_pen_cntx.pen_down_pos.x, g_pen_cntx.pen_down_pos.y);
  1216.                         if (g_pen_stroke_table[MMI_PEN_STROKE_DOWN])
  1217.                         {
  1218.                             (g_pen_stroke_table[MMI_PEN_STROKE_DOWN]) (g_pen_cntx.pen_down_pos);
  1219.                         }
  1220.                         /* Pre-Stroke-Move */
  1221.                         MMI_DBG_ASSERT(!is_stroke_move);
  1222.                         is_stroke_move = MMI_TRUE;
  1223.                         if (g_pen_stroke_pre_move)
  1224.                         {
  1225.                             g_pen_stroke_pre_move();
  1226.                         }
  1227.                         /* Stroke Move */
  1228.                         mmi_pen_push_stroke_point(pos.x, pos.y);
  1229.                         if (g_pen_stroke_table[MMI_PEN_STROKE_MOVE])
  1230.                         {
  1231.                             (g_pen_stroke_table[MMI_PEN_STROKE_MOVE]) (pos);
  1232.                         }
  1233.                     }
  1234.                 }
  1235.                 else
  1236.                 {
  1237.                     if (g_pen_num_stroke_area > 1)
  1238.                     {
  1239.                         mmi_pen_fix_multi_block_pen_position(&pos);
  1240.                     }
  1241.                     
  1242.                     if (!is_stroke_move)
  1243.                     {
  1244.                         is_stroke_move = MMI_TRUE;
  1245.                         if (g_pen_stroke_pre_move)
  1246.                         {
  1247.                             g_pen_stroke_pre_move();
  1248.                         }
  1249.                     }
  1250.                     mmi_pen_push_stroke_point(pos.x, pos.y);
  1251.                     if (g_pen_stroke_table[MMI_PEN_STROKE_MOVE])
  1252.                     {
  1253.                         (g_pen_stroke_table[MMI_PEN_STROKE_MOVE]) (pos);
  1254.                     }
  1255.                 }
  1256.                 break;
  1257.                 
  1258.             case STROKE_DOWN:
  1259.                 MMI_DBG_ASSERT(!g_pen_cntx.is_stroke_down && !g_pen_cntx.is_pen_down);
  1260.                 /* mmi_pen_reset() should be used after mmi_pen_stop_capture_strokes() such that enqueued stroke events are flushed. */
  1261.                 MMI_DBG_ASSERT(g_pen_stroke_max_points > 0);
  1262.                 g_pen_cntx.is_stroke_down = 1;
  1263.                 g_pen_cntx.is_pen_down = 1;
  1264.                 g_pen_cntx.pen_down_pos = pos;
  1265.                 g_pen_cntx.stroke_down_block_index = mmi_pen_lookup_handwriting_block(pos.x, pos.y);
  1266.                 if (g_pen_num_stroke_area > 1 && g_pen_cntx.stroke_down_block_index < 0)
  1267.                 {
  1268.                     /* MMI framework and driver have intergration problem.
  1269.                        Maybe the current driver does not support multi-block, 
  1270.                        or driver does not disable extended handwriting area properly in
  1271.                        multi-block mode. */
  1272.                     MMI_ASSERT(0);
  1273.                 }
  1274.                 if (g_pen_stroke_min_offset_square > 0)
  1275.                 {
  1276.                     g_pen_cntx.is_pending_stroke_event = 1;
  1277.                     /* Fire pen down event. For example, it will display key down on virtual keyboard. */
  1278.                     if (g_pen_event_table[MMI_PEN_EVENT_DOWN])
  1279.                     {
  1280.                         (g_pen_event_table[MMI_PEN_EVENT_DOWN]) (g_pen_cntx.pen_down_pos);
  1281.                     }
  1282.                 }
  1283.                 else
  1284.                 {
  1285.                     g_pen_cntx.is_pending_stroke_event = 0;
  1286.                     if (g_pen_stroke_table[MMI_PEN_STROKE_DOWN])
  1287.                     {
  1288.                         (g_pen_stroke_table[MMI_PEN_STROKE_DOWN]) (pos);
  1289.                     }
  1290.                     
  1291.                     /* Put stroke point after stroke down handler because multi-block handwriting may invoke
  1292.                      * mmi_pen_begin_strokes_of_character() and mmi_pen_end_strokes_of_character() in 
  1293.                      * Stroke Down handler.
  1294.                      */
  1295.                     if (g_pen_cntx.is_enabled && g_pen_cntx.is_pen_down && g_pen_stroke_max_points > 0)
  1296.                     {
  1297.                         mmi_pen_push_stroke_point(pos.x, pos.y);
  1298.                     }                    
  1299.                 }
  1300.                 break;
  1301.                 
  1302.             case STROKE_UP:
  1303.                 MMI_DBG_ASSERT(g_pen_cntx.is_stroke_down && g_pen_cntx.is_pen_down);
  1304.                 /* mmi_pen_reset() should be used after mmi_pen_stop_capture_strokes() such that enqueued stroke events are flushed. */
  1305.                 MMI_DBG_ASSERT(g_pen_stroke_max_points > 0);
  1306.                 g_pen_cntx.is_stroke_down = 0;
  1307.                 g_pen_cntx.is_pen_down = 0;
  1308.                 if (g_pen_cntx.is_pending_stroke_event)
  1309.                 {
  1310.                     g_pen_cntx.is_pending_stroke_event = 0;
  1311.                     /*
  1312.                      * Flush driver queue and lookahead buffer:
  1313.                      * Otherwise, it might restore handwriting area (touch_panel_reset_handwriting)
  1314.                      * while stroke-based mode is already entered.
  1315.                      */
  1316.                     g_pen_cntx.reset_stroke_on_pen_up = 1;
  1317.                     /* Note: if pen up handler invokes mmi_pen_reset(), g_pen_cntx.reset_stroke_on_pen_up is cleared */
  1318.                     if (g_pen_event_table[MMI_PEN_EVENT_UP])
  1319.                     {
  1320.                         (g_pen_event_table[MMI_PEN_EVENT_UP]) (g_pen_cntx.pen_down_pos);
  1321.                     }
  1322.                     /* Delay processing further events */
  1323.                     delay_polling_timer = MMI_TRUE;
  1324.                 }
  1325.                 else
  1326.                 {
  1327.                     /* 
  1328.                      * Because touch panel can not detect pen position in Stroke Up event,
  1329.                      * we do not put the point into stroke queue
  1330.                      */
  1331.                     if (g_pen_num_stroke_area > 1)
  1332.                     {
  1333.                         mmi_pen_fix_multi_block_pen_position(&pos);
  1334.                     }
  1335.                     
  1336.                     mmi_pen_push_stroke_end();
  1337.                     if (g_pen_stroke_table[MMI_PEN_STROKE_UP])
  1338.                     {
  1339.                         (g_pen_stroke_table[MMI_PEN_STROKE_UP]) (pos);
  1340.                     }
  1341.                 }
  1342.                 /* Clear strokes and reset handwriting area */
  1343.                 if (g_pen_cntx.reset_stroke_on_pen_up)
  1344.                 {
  1345.                     if (g_pen_stroke_max_points > 0)
  1346.                     {
  1347.                         mmi_pen_end_strokes_of_character();
  1348.                         mmi_pen_reset();
  1349.                         mmi_pen_begin_strokes_of_character();
  1350.                     }
  1351.                     else
  1352.                     {
  1353.                         mmi_pen_reset();
  1354.                     }
  1355.                 }
  1356.                 break;
  1357.             #ifdef MMI_PEN_SUPPORT_STROKE_LONGTAP
  1358.             case STROKE_LONGTAP:
  1359.                 /* 
  1360.                  * 1. degrade sampling rate
  1361.                  * 2. reset strokes on pen on
  1362.                  */
  1363.                 mmi_pen_config_sampling_period(MMI_PEN_SAMPLING_PERIOD_1, MMI_PEN_SAMPLING_PERIOD_1);
  1364.                 g_pen_cntx.reset_stroke_on_pen_up = 1;
  1365.                 g_pen_cntx.restore_sampling_rate_on_reset = 1;
  1366.                 if (g_pen_stroke_table[MMI_PEN_STROKE_LONGTAP])
  1367.                 {
  1368.                     (g_pen_stroke_table[MMI_PEN_STROKE_LONGTAP]) (pos);
  1369.                 }
  1370.                 break;
  1371.             #else /* MMI_PEN_SUPPORT_STROKE_LONGTAP */ 
  1372.             case STROKE_LONGTAP:
  1373.                 break;
  1374.             #endif /* MMI_PEN_SUPPORT_STROKE_LONGTAP */ 
  1375.             default:
  1376.                 MMI_ASSERT(0);
  1377.         }
  1378.         mmi_frm_fetch_msg_from_extQ_to_circularQ();
  1379.     }
  1380.     if (is_stroke_move)
  1381.     {
  1382.         if (g_pen_stroke_post_move)
  1383.         {
  1384.             g_pen_stroke_post_move();
  1385.         }
  1386.     }
  1387.     if (has_incoming_event)
  1388.     {
  1389.         TurnOnBacklight(MMI_TRUE);
  1390.     #ifdef __MMI_LCD_PARTIAL_ON__
  1391.         /* Switch screen and flush pen events */
  1392.         LeavePartialOnScreenSaverIfOk();
  1393.     #endif /* __MMI_LCD_PARTIAL_ON__ */ 
  1394.     }
  1395.     /* Either look-ahead buffer is empty or we need to skip the polling  */
  1396.     MMI_ASSERT(g_pen_cntx.num_lookahead_events == 0 || delay_polling_timer);
  1397.     if (delay_polling_timer)
  1398.     {
  1399.         /* Start a timer even if !g_pen_cntx.is_enabled && !g_pen_cntx.is_pen_down && g_pen_cntx.num_lookahead_events == 0 */
  1400.         StartTimer(PEN_POLLING_TIMER, MMI_PEN_DEBOUNCE_POLLING_DELAY, mmi_pen_poll_hdlr);
  1401.         g_pen_polling_timer_started = MMI_TRUE;
  1402.         Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_poll_hdlr() - delay polling timer n");
  1403.     }
  1404.     else if (g_pen_cntx.is_pen_down)
  1405.     {
  1406.         /* Previous pen handlers might call mmi_pen_disable(), then g_pen_cntx.is_pen_down = 0.  */
  1407.         MMI_DBG_ASSERT(g_pen_cntx.is_enabled);
  1408.         StartTimer(PEN_POLLING_TIMER, MMI_PEN_POLLING_PERIOD, mmi_pen_poll_hdlr);
  1409.         g_pen_polling_timer_started = MMI_TRUE;
  1410.     }
  1411. }
  1412. /*
  1413.  * Refernece: touch_panel_sendilm() 
  1414.  * THIS FUNCTION IS EXECUTED IN DRIVER CONTEXT
  1415.  */
  1416. /*****************************************************************************
  1417.  * FUNCTION
  1418.  *  mmi_pen_touch_panel_sendilm
  1419.  * DESCRIPTION
  1420.  *  
  1421.  * PARAMETERS
  1422.  *  param       [IN]         unused
  1423.  *  state       [IN]        
  1424.  * RETURNS
  1425.  *  void
  1426.  *****************************************************************************/
  1427. static void mmi_pen_touch_panel_sendilm(void *param /* unused */ , Touch_Panel_Event_enum state)
  1428. {
  1429.     /*----------------------------------------------------------------*/
  1430.     /* Local Variables                                                */
  1431.     /*----------------------------------------------------------------*/
  1432.     ilm_struct *tp_ilm;
  1433.     /*----------------------------------------------------------------*/
  1434.     /* Code Body                                                      */
  1435.     /*----------------------------------------------------------------*/
  1436.     /* Dummy function */
  1437.     if (state == PEN_DOWN || state == STROKE_DOWN)
  1438.     {
  1439.         if (!g_pen_is_driver_indication_in_queue)
  1440.         {
  1441.             g_pen_is_driver_indication_in_queue = 1;
  1442.             DRV_BuildPrimitive(tp_ilm, MOD_TP_TASK, MOD_MMI, MSG_ID_TP_EVENT_IND, NULL);
  1443.             msg_send_ext_queue(tp_ilm);
  1444.         }
  1445.     }
  1446. }
  1447. /* Protocol event handler of MSG_ID_TP_EVENT_IND */
  1448. /*****************************************************************************
  1449.  * FUNCTION
  1450.  *  mmi_pen_touch_panel_event_ind
  1451.  * DESCRIPTION
  1452.  *  
  1453.  * PARAMETERS
  1454.  *  param       [IN]     unused
  1455.  * RETURNS
  1456.  *  void
  1457.  *****************************************************************************/
  1458. static void mmi_pen_touch_panel_event_ind(void *param /* unused */ )
  1459. {
  1460.     /*----------------------------------------------------------------*/
  1461.     /* Local Variables                                                */
  1462.     /*----------------------------------------------------------------*/
  1463.     /*----------------------------------------------------------------*/
  1464.     /* Code Body                                                      */
  1465.     /*----------------------------------------------------------------*/
  1466.     Trace2(
  1467.         MMI_TRACE_G1_FRM,
  1468.         "[Pen] mmi_pen_touch_panel_event_in - %d, %dn",
  1469.         (int)g_pen_polling_timer_started,
  1470.         (int)g_pen_cntx.is_enabled);
  1471.     g_pen_is_driver_indication_in_queue = 0;
  1472.     if (!g_pen_polling_timer_started && g_pen_cntx.is_enabled)
  1473.     {
  1474.         mmi_pen_poll_hdlr();
  1475.     }
  1476. }
  1477. /***************************************************************************** 
  1478. * Global Variable
  1479. *****************************************************************************/
  1480. /***************************************************************************** 
  1481. * Global Function
  1482. *****************************************************************************/
  1483. /* Dummy pen handler */
  1484. /*****************************************************************************
  1485.  * FUNCTION
  1486.  *  mmi_pen_dummy_hdlr
  1487.  * DESCRIPTION
  1488.  *  
  1489.  * PARAMETERS
  1490.  *  pos     [IN]        
  1491.  * RETURNS
  1492.  *  void
  1493.  *****************************************************************************/
  1494. void mmi_pen_dummy_hdlr(mmi_pen_point_struct pos)
  1495. {
  1496.     /*----------------------------------------------------------------*/
  1497.     /* Local Variables                                                */
  1498.     /*----------------------------------------------------------------*/
  1499.     /*----------------------------------------------------------------*/
  1500.     /* Code Body                                                      */
  1501.     /*----------------------------------------------------------------*/
  1502.     UI_UNUSED_PARAMETER(pos);
  1503. }
  1504. /*****************************************************************************
  1505.  * FUNCTION
  1506.  *  mmi_pen_init
  1507.  * DESCRIPTION
  1508.  *  Initialize pen system
  1509.  * PARAMETERS
  1510.  *  void
  1511.  * RETURNS
  1512.  *  void
  1513.  *****************************************************************************/
  1514. void mmi_pen_init(void)
  1515. {
  1516.     /*----------------------------------------------------------------*/
  1517.     /* Local Variables                                                */
  1518.     /*----------------------------------------------------------------*/
  1519.     /*----------------------------------------------------------------*/
  1520.     /* Code Body                                                      */
  1521.     /*----------------------------------------------------------------*/
  1522. #ifdef __MMI_HANDWRITING_PAD__
  1523.     mmi_frm_setup_default_pen_handler();
  1524. #endif 
  1525.     g_pen_initialized = KAL_TRUE;
  1526.     mmi_pen_stop_capture_strokes();
  1527.     mmi_pen_config_sampling_period(MMI_PEN_SAMPLING_PERIOD_1, MMI_PEN_SAMPLING_PERIOD_2);
  1528.     mmi_pen_config_timeout_period(MMI_PEN_LONGTAP_TIME, MMI_PEN_REPEAT_TIME, MMI_PEN_STROKE_LONGTAP_TIME);
  1529.     mmi_pen_config_move_offset(
  1530.         MMI_PEN_SKIP_MOVE_OFFSET,
  1531.         MMI_PEN_SKIP_STROKE_MOVE_OFFSET,
  1532.         MMI_PEN_SKIP_LONGTAP_OFFSET,
  1533.         MMI_PEN_SKIP_STROKE_LONGTAP_OFFSET);
  1534.     touch_panel_cb_registration(mmi_pen_touch_panel_sendilm, NULL);
  1535.     SetProtocolEventHandler(mmi_pen_touch_panel_event_ind, MSG_ID_TP_EVENT_IND);
  1536. }
  1537. /*****************************************************************************
  1538.  * FUNCTION
  1539.  *  mmi_pen_block
  1540.  * DESCRIPTION
  1541.  *  Block pen system
  1542.  *  
  1543.  *  Note: typically used for keypad lock in idle screen
  1544.  *  mmi_pen_enable()/mmi_pen_disable() are ignored when pen is blocked.
  1545.  * PARAMETERS
  1546.  *  void
  1547.  * RETURNS
  1548.  *  void
  1549.  *****************************************************************************/
  1550. void mmi_pen_block(void)
  1551. {
  1552.     /*----------------------------------------------------------------*/
  1553.     /* Local Variables                                                */
  1554.     /*----------------------------------------------------------------*/
  1555.     /*----------------------------------------------------------------*/
  1556.     /* Code Body                                                      */
  1557.     /*----------------------------------------------------------------*/
  1558.     mmi_pen_disable();
  1559.     g_pen_cntx.is_blocked = 1;
  1560. }
  1561. //KP Jerry add on 2007-4-11 start
  1562. #ifdef __MMI_PEN_BLOCK_WHEN_KAYPAD_LOCKED__
  1563. /*****************************************************************************
  1564.  * FUNCTION
  1565.  *  mmi_pen_forced_block
  1566.  * DESCRIPTION
  1567.  *  Block pen system forced
  1568.  *  
  1569.  *  Note: typically used for keypad lock in idle screen
  1570.  * PARAMETERS
  1571.  *  void
  1572.  * RETURNS
  1573.  *  void
  1574.  *****************************************************************************/
  1575. void mmi_pen_forced_block(void)
  1576. {
  1577.     /*----------------------------------------------------------------*/
  1578.     /* Local Variables                                                */
  1579.     /*----------------------------------------------------------------*/
  1580.     /*----------------------------------------------------------------*/
  1581.     /* Code Body                                                      */
  1582.     /*----------------------------------------------------------------*/
  1583.     mmi_pen_forced_disable();
  1584.     g_pen_cntx.is_blocked = 1;
  1585. }
  1586. #endif/*__MMI_PEN_BLOCK_WHEN_KAYPAD_LOCKED__*/
  1587. //KP Jerry add on 2007-4-11 end
  1588. /*****************************************************************************
  1589.  * FUNCTION
  1590.  *  mmi_pen_unblock
  1591.  * DESCRIPTION
  1592.  *  Unblock pen system
  1593.  *  
  1594.  *  Note: typically used for keypad lock in idle screen
  1595.  *  mmi_pen_enable()/mmi_pen_disable() are ignored when pen is blocked.
  1596.  * PARAMETERS
  1597.  *  void
  1598.  * RETURNS
  1599.  *  void
  1600.  *****************************************************************************/
  1601. void mmi_pen_unblock(void)
  1602. {
  1603.     /*----------------------------------------------------------------*/
  1604.     /* Local Variables                                                */
  1605.     /*----------------------------------------------------------------*/
  1606.     /*----------------------------------------------------------------*/
  1607.     /* Code Body                                                      */
  1608.     /*----------------------------------------------------------------*/
  1609.     g_pen_cntx.is_blocked = 0;
  1610.     mmi_pen_enable();
  1611. }
  1612. /*****************************************************************************
  1613.  * FUNCTION
  1614.  *  mmi_pen_config_sampling_period
  1615.  * DESCRIPTION
  1616.  *  Config sampling period of Event-based and Stroke-Based
  1617.  * PARAMETERS
  1618.  *  low         [IN]        Sampling period in non-handwriting area (multiple of 10ms)
  1619.  *  high        [IN]        Sampling period in handwriting area (multiple of 10ms)
  1620.  * RETURNS
  1621.  *  void
  1622.  *****************************************************************************/
  1623. void mmi_pen_config_sampling_period(kal_uint32 low, kal_uint32 high)
  1624. {
  1625.     /*----------------------------------------------------------------*/
  1626.     /* Local Variables                                                */
  1627.     /*----------------------------------------------------------------*/
  1628.     /*----------------------------------------------------------------*/
  1629.     /* Code Body                                                      */
  1630.     /*----------------------------------------------------------------*/
  1631.     touch_panle_conf_sample_period(low, high);
  1632. }
  1633. /*****************************************************************************
  1634.  * FUNCTION
  1635.  *  mmi_pen_config_timeout_period
  1636.  * DESCRIPTION
  1637.  *  Config timeout period for LongTap and Repeat
  1638.  * PARAMETERS
  1639.  *  longtap             [IN]        Timeout period for Pen LongTap event (multiple of 10ms).
  1640.  *  repeat              [IN]        Timeout period for Pen Repeat event (multiple of 10ms).
  1641.  *  stroke_longtap      [IN]        Timeout period for Stroke LongTap event (multiple of 10ms).
  1642.  * RETURNS
  1643.  *  void
  1644.  *****************************************************************************/
  1645. void mmi_pen_config_timeout_period(kal_uint32 longtap, kal_uint32 repeat, kal_uint32 stroke_longtap)
  1646. {
  1647.     /*----------------------------------------------------------------*/
  1648.     /* Local Variables                                                */
  1649.     /*----------------------------------------------------------------*/
  1650.     /*----------------------------------------------------------------*/
  1651.     /* Code Body                                                      */
  1652.     /*----------------------------------------------------------------*/
  1653.     touch_panle_conf_timeout_period(longtap, repeat, stroke_longtap);
  1654. }
  1655. /*****************************************************************************
  1656.  * FUNCTION
  1657.  *  mmi_pen_config_move_offset
  1658.  * DESCRIPTION
  1659.  *  Config
  1660.  * PARAMETERS
  1661.  *  event_based         [IN]        Minimum move offset for Pen Move event
  1662.  *  stroke_based        [IN]        Minimum move offset for Stroke Move event
  1663.  *  long_tap            [IN]        Maximum move offset for Pen LongTap event
  1664.  *  stroke_long_tap     [IN]        Maximum move offset for Stroke LongTap event
  1665.  * RETURNS
  1666.  *  void
  1667.  *****************************************************************************/
  1668. void mmi_pen_config_move_offset(
  1669.         kal_uint32 event_based,
  1670.         kal_uint32 stroke_based,
  1671.         kal_uint32 long_tap,
  1672.         kal_uint32 stroke_long_tap)
  1673. {
  1674.     /*----------------------------------------------------------------*/
  1675.     /* Local Variables                                                */
  1676.     /*----------------------------------------------------------------*/
  1677.     /*----------------------------------------------------------------*/
  1678.     /* Code Body                                                      */
  1679.     /*----------------------------------------------------------------*/
  1680.     touch_panel_conf_move_offset(
  1681.         (kal_uint16) event_based,
  1682.         (kal_uint16) stroke_based,
  1683.         (kal_uint16) long_tap,
  1684.         (kal_uint16) stroke_long_tap);
  1685. }
  1686. /*****************************************************************************
  1687.  * FUNCTION
  1688.  *  mmi_pen_enable
  1689.  * DESCRIPTION
  1690.  *  Enable pen system
  1691.  *  
  1692.  *  Note: typically on Keypad Up
  1693.  * PARAMETERS
  1694.  *  void
  1695.  * RETURNS
  1696.  *  void
  1697.  * REMARKS
  1698.  *  If g_pen_initialized is not true, we do not enable touch screen because
  1699.  *  TP task might not be running
  1700.  *****************************************************************************/
  1701. void mmi_pen_enable(void)
  1702. {
  1703.     /*----------------------------------------------------------------*/
  1704.     /* Local Variables                                                */
  1705.     /*----------------------------------------------------------------*/
  1706.     /*----------------------------------------------------------------*/
  1707.     /* Code Body                                                      */
  1708.     /*----------------------------------------------------------------*/
  1709.     Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_enable()n");
  1710.     if (!g_pen_initialized || g_pen_cntx.is_blocked || g_pen_cntx.is_enabled)
  1711.     {
  1712.         return;
  1713.     }
  1714.     memset(&g_pen_cntx, 0, sizeof(g_pen_cntx));
  1715.     g_pen_cntx.is_enabled = 1;
  1716.     touch_panel_enable(KAL_TRUE);
  1717. }
  1718. /*****************************************************************************
  1719.  * FUNCTION
  1720.  *  mmi_pen_disable
  1721.  * DESCRIPTION
  1722.  *  Disable pen system
  1723.  *  
  1724.  *  Note: typically on Keypad Down because we don't want to use keypad and touch screen
  1725.  *  at the same time
  1726.  * PARAMETERS
  1727.  *  void
  1728.  * RETURNS
  1729.  *  void
  1730.  *****************************************************************************/
  1731. void mmi_pen_disable(void)
  1732. {
  1733.     /*----------------------------------------------------------------*/
  1734.     /* Local Variables                                                */
  1735.     /*----------------------------------------------------------------*/
  1736.     /*----------------------------------------------------------------*/
  1737.     /* Code Body                                                      */
  1738.     /*----------------------------------------------------------------*/
  1739.     Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_disable()n");
  1740.     if (!g_pen_initialized || g_pen_cntx.is_blocked || !g_pen_cntx.is_enabled)
  1741.     {
  1742.         return;
  1743.     }
  1744.     touch_panel_enable(KAL_FALSE);
  1745.     touch_panel_flush();
  1746.     if (g_pen_cntx.is_pen_down)
  1747.     {
  1748.         /* For both stroke-based and event-based */
  1749.         if (g_pen_event_table[MMI_PEN_EVENT_ABORT])
  1750.         {
  1751.             (g_pen_event_table[MMI_PEN_EVENT_ABORT]) (g_pen_cntx.pen_current_pos);
  1752.         }
  1753.     }
  1754.     memset(&g_pen_cntx, 0, sizeof(g_pen_cntx));
  1755. }
  1756. //KP Jerry add on 2007-4-11 start
  1757. #ifdef __MMI_PEN_BLOCK_WHEN_KAYPAD_LOCKED__
  1758. /*****************************************************************************
  1759.  * FUNCTION
  1760.  *  mmi_pen_forced_disable
  1761.  * DESCRIPTION
  1762.  *  Disable pen system
  1763.  *  
  1764.  *  Note: typically on Keypad Down because we don't want to use keypad and touch screen
  1765.  *  at the same time
  1766.  * PARAMETERS
  1767.  *  void
  1768.  * RETURNS
  1769.  *  void
  1770.  *****************************************************************************/
  1771. void mmi_pen_forced_disable(void)
  1772. {
  1773.     /*----------------------------------------------------------------*/
  1774.     /* Local Variables                                                */
  1775.     /*----------------------------------------------------------------*/
  1776.     /*----------------------------------------------------------------*/
  1777.     /* Code Body                                                      */
  1778.     /*----------------------------------------------------------------*/
  1779.     Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_forced_disable()n");
  1780.     if (!g_pen_initialized || g_pen_cntx.is_blocked || !g_pen_cntx.is_enabled)
  1781.     {
  1782.         return;
  1783.     }
  1784.     touch_panel_enable(KAL_FALSE);
  1785.     touch_panel_flush();
  1786.     if (g_pen_cntx.is_pen_down)
  1787.     {
  1788.         /* For both stroke-based and event-based */
  1789.         if (g_pen_event_table[MMI_PEN_EVENT_ABORT])
  1790.         {
  1791.             (g_pen_event_table[MMI_PEN_EVENT_ABORT]) (g_pen_cntx.pen_current_pos);
  1792.         }
  1793.     }
  1794. }
  1795. #endif/*__MMI_PEN_BLOCK_WHEN_KAYPAD_LOCKED__*/
  1796. //KP Jerry add on 2007-4-11 end
  1797. /*****************************************************************************
  1798.  * FUNCTION
  1799.  *  mmi_pen_reset
  1800.  * DESCRIPTION
  1801.  *  Reset the status of touch screen
  1802.  *  - Flush event queue
  1803.  *  - If the pen is currently tapped down, ignore all subsequent pen events until the pen is up.
  1804.  *  
  1805.  *  Note: typically on MMI screen switching
  1806.  * PARAMETERS
  1807.  *  void
  1808.  * RETURNS
  1809.  *  void
  1810.  *****************************************************************************/
  1811. void mmi_pen_reset(void)
  1812. {
  1813.     /*----------------------------------------------------------------*/
  1814.     /* Local Variables                                                */
  1815.     /*----------------------------------------------------------------*/
  1816.     /*----------------------------------------------------------------*/
  1817.     /* Code Body                                                      */
  1818.     /*----------------------------------------------------------------*/
  1819.     Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_reset()n");
  1820.     if (!g_pen_initialized || g_pen_cntx.is_blocked || !g_pen_cntx.is_enabled)
  1821.     {
  1822.         return;
  1823.     }
  1824.     if (g_pen_cntx.restore_sampling_rate_on_reset)
  1825.     {
  1826.         mmi_pen_config_sampling_period(MMI_PEN_SAMPLING_PERIOD_1, MMI_PEN_SAMPLING_PERIOD_2);
  1827.     }
  1828.     if (g_pen_cntx.is_pen_down)
  1829.     {
  1830.         /* For both stroke-based and event-based */
  1831.         if (g_pen_event_table[MMI_PEN_EVENT_ABORT])
  1832.         {
  1833.             (g_pen_event_table[MMI_PEN_EVENT_ABORT]) (g_pen_cntx.pen_current_pos);
  1834.         }
  1835.     }
  1836.     touch_panel_reset(KAL_TRUE);
  1837.     memset(&g_pen_cntx, 0, sizeof(g_pen_cntx));
  1838.     g_pen_cntx.is_enabled = 1;
  1839. }
  1840. /*****************************************************************************
  1841.  * FUNCTION
  1842.  *  mmi_pen_get_state
  1843.  * DESCRIPTION
  1844.  *  Get the current state of touch screen
  1845.  * PARAMETERS
  1846.  *  is_enabled      [OUT]         
  1847.  *  is_pen_down     [OUT]         
  1848.  * RETURNS
  1849.  *  void
  1850.  * REMARKS
  1851.  *  It returns the current state *inside MMI task*, real pen state might be changed, but
  1852.  *  the driver indication might not be processed yet.
  1853.  *****************************************************************************/
  1854. void mmi_pen_get_state(kal_bool *is_enabled, kal_bool *is_pen_down)
  1855. {
  1856.     /*----------------------------------------------------------------*/
  1857.     /* Local Variables                                                */
  1858.     /*----------------------------------------------------------------*/
  1859.     /*----------------------------------------------------------------*/
  1860.     /* Code Body                                                      */
  1861.     /*----------------------------------------------------------------*/
  1862.     if (g_pen_cntx.is_enabled)
  1863.     {
  1864.         *is_enabled = KAL_TRUE;
  1865.         if (g_pen_cntx.is_pen_down)
  1866.         {
  1867.             *is_pen_down = KAL_TRUE;
  1868.         }
  1869.         else
  1870.         {
  1871.             *is_pen_down = KAL_FALSE;
  1872.         }
  1873.     }
  1874.     else
  1875.     {
  1876.         *is_enabled = KAL_FALSE;
  1877.         *is_pen_down = KAL_FALSE;
  1878.     }
  1879. }
  1880. /*****************************************************************************
  1881.  * FUNCTION
  1882.  *  mmi_pen_start_calibration
  1883.  * DESCRIPTION
  1884.  *  Start pen calibration
  1885.  * PARAMETERS
  1886.  *  num         [IN]        Number of calibration points
  1887.  *  points      [IN]        Calibration points
  1888.  * RETURNS
  1889.  *  void
  1890.  * REMARKS
  1891.  *  After mmi_pen_reset(), the calibration process is terminated.
  1892.  *****************************************************************************/
  1893. void mmi_pen_start_calibration(kal_uint16 num, const mmi_pen_point_struct *points)
  1894. {
  1895.     /*----------------------------------------------------------------*/
  1896.     /* Local Variables                                                */
  1897.     /*----------------------------------------------------------------*/
  1898.     /*----------------------------------------------------------------*/
  1899.     /* Code Body                                                      */
  1900.     /*----------------------------------------------------------------*/
  1901.     Trace2(MMI_TRACE_G1_FRM, "[Pen] mmi_pen_start_calibration()n");
  1902.     MMI_ASSERT(g_pen_initialized);
  1903.     touch_panel_start_cali((TouchPanelCoordStruct*) points, num);
  1904. }
  1905. /*****************************************************************************
  1906.  * FUNCTION
  1907.  *  mmi_pen_set_calibration_data
  1908.  * DESCRIPTION
  1909.  *  Assign driver calibration data
  1910.  * PARAMETERS
  1911.  *  data        [IN]        
  1912.  * RETURNS
  1913.  *  void
  1914.  * REMARKS
  1915.  *  
  1916.  *****************************************************************************/
  1917. void mmi_pen_set_calibration_data(const mmi_pen_calibration_struct *data)
  1918. {
  1919.     /*----------------------------------------------------------------*/
  1920.     /* Local Variables                                                */
  1921.     /*----------------------------------------------------------------*/
  1922.     TouchPanelCaliStruct cali;
  1923.     /*----------------------------------------------------------------*/
  1924.     /* Code Body                                                      */
  1925.     /*----------------------------------------------------------------*/
  1926.     MMI_DBG_ASSERT(sizeof(mmi_pen_calibration_struct) == sizeof(TouchPanelCaliStruct));
  1927.     memcpy(&cali, data, sizeof(TouchPanelCaliStruct));
  1928.     touch_panel_set_cali(cali);
  1929. }
  1930. /*****************************************************************************
  1931.  * FUNCTION
  1932.  *  mmi_pen_read_calibration_data
  1933.  * DESCRIPTION
  1934.  *  Read the current value of driver calibration data
  1935.  * PARAMETERS
  1936.  *  data        [OUT]       
  1937.  * RETURNS
  1938.  *  void
  1939.  * REMARKS
  1940.  *  
  1941.  *****************************************************************************/
  1942. void mmi_pen_read_calibration_data(mmi_pen_calibration_struct *data)
  1943. {
  1944.     /*----------------------------------------------------------------*/
  1945.     /* Local Variables                                                */
  1946.     /*----------------------------------------------------------------*/
  1947.     TouchPanelCaliStruct cali;
  1948.     /*----------------------------------------------------------------*/
  1949.     /* Code Body                                                      */
  1950.     /*----------------------------------------------------------------*/
  1951.     MMI_DBG_ASSERT(sizeof(mmi_pen_calibration_struct) == sizeof(TouchPanelCaliStruct));
  1952.     touch_panel_read_cali(&cali);
  1953.     memcpy(data, &cali, sizeof(TouchPanelCaliStruct));
  1954. }
  1955. /*****************************************************************************
  1956.  * FUNCTION
  1957.  *  mmi_pen_register_down_handler
  1958.  * DESCRIPTION
  1959.  *  Register the Pen Down handler
  1960.  * PARAMETERS
  1961.  *  pen_fp      [IN]        Callback handler
  1962.  * RETURNS
  1963.  *  void
  1964.  *****************************************************************************/
  1965. void mmi_pen_register_down_handler(mmi_pen_hdlr pen_fp)
  1966. {
  1967.     /*----------------------------------------------------------------*/
  1968.     /* Local Variables                                                */
  1969.     /*----------------------------------------------------------------*/
  1970.     /*----------------------------------------------------------------*/
  1971.     /* Code Body                                                      */
  1972.     /*----------------------------------------------------------------*/
  1973.     g_pen_event_table[MMI_PEN_EVENT_DOWN] = pen_fp;
  1974. }
  1975. /*****************************************************************************
  1976.  * FUNCTION
  1977.  *  mmi_pen_register_long_tap_handler
  1978.  * DESCRIPTION
  1979.  *  Register the Pen LongTap handler
  1980.  *  
  1981.  *  LongTap handler is invoked when  the pen is tapped for a period of time
  1982.  *  and stays at the same place where it is tapped down.
  1983.  *  
  1984.  *  It is invoked atmost one time before pen up.
  1985.  * PARAMETERS
  1986.  *  pen_fp      [IN]        Callback handler
  1987.  * RETURNS
  1988.  *  void
  1989.  *****************************************************************************/
  1990. void mmi_pen_register_long_tap_handler(mmi_pen_hdlr pen_fp)
  1991. {
  1992.     /*----------------------------------------------------------------*/
  1993.     /* Local Variables                                                */
  1994.     /*----------------------------------------------------------------*/
  1995.     /*----------------------------------------------------------------*/
  1996.     /* Code Body                                                      */
  1997.     /*----------------------------------------------------------------*/
  1998.     g_pen_event_table[MMI_PEN_EVENT_LONG_TAP] = pen_fp;
  1999. }
  2000. /*****************************************************************************
  2001.  * FUNCTION
  2002.  *  mmi_pen_register_repeat_handler
  2003.  * DESCRIPTION
  2004.  *  Register the Pen Repeat handler.
  2005.  *  
  2006.  *  Repeat handler is invoked after LongTap handler.
  2007.  *  However, unlike LongTap handler, Repeat handler is invoked even if
  2008.  *  it does not stays at the same place as Pen Down.
  2009.  *  
  2010.  *  it might be invoked more than one times before pen up.
  2011.  * PARAMETERS
  2012.  *  pen_fp      [IN]        Callback handler
  2013.  * RETURNS
  2014.  *  void
  2015.  *****************************************************************************/
  2016. void mmi_pen_register_repeat_handler(mmi_pen_hdlr pen_fp)
  2017. {
  2018.     /*----------------------------------------------------------------*/
  2019.     /* Local Variables                                                */
  2020.     /*----------------------------------------------------------------*/
  2021.     /*----------------------------------------------------------------*/
  2022.     /* Code Body                                                      */
  2023.     /*----------------------------------------------------------------*/
  2024.     g_pen_event_table[MMI_PEN_EVENT_REPEAT] = pen_fp;
  2025. }
  2026. /*****************************************************************************
  2027.  * FUNCTION
  2028.  *  mmi_pen_register_move_handler
  2029.  * DESCRIPTION
  2030.  *  Register the Pen Move handler.
  2031.  *  
  2032.  *  The invocation frequency of Pen Move handler is typically less than driver sampling rate.
  2033.  * PARAMETERS
  2034.  *  pen_fp      [IN]        Callback handler
  2035.  * RETURNS
  2036.  *  void
  2037.  *****************************************************************************/
  2038. void mmi_pen_register_move_handler(mmi_pen_hdlr pen_fp)
  2039. {
  2040.     /*----------------------------------------------------------------*/
  2041.     /* Local Variables                                                */
  2042.     /*----------------------------------------------------------------*/
  2043.     /*----------------------------------------------------------------*/
  2044.     /* Code Body                                                      */
  2045.     /*----------------------------------------------------------------*/
  2046.     g_pen_event_table[MMI_PEN_EVENT_MOVE] = pen_fp;
  2047. }
  2048. /*****************************************************************************
  2049.  * FUNCTION
  2050.  *  mmi_pen_register_up_handler
  2051.  * DESCRIPTION
  2052.  *  Register the Pen Up handler.
  2053.  * PARAMETERS
  2054.  *  pen_fp      [IN]        
  2055.  * RETURNS
  2056.  *  void
  2057.  *****************************************************************************/
  2058. void mmi_pen_register_up_handler(mmi_pen_hdlr pen_fp)
  2059. {
  2060.     /*----------------------------------------------------------------*/
  2061.     /* Local Variables                                                */
  2062.     /*----------------------------------------------------------------*/
  2063.     /*----------------------------------------------------------------*/
  2064.     /* Code Body                                                      */
  2065.     /*----------------------------------------------------------------*/
  2066.     g_pen_event_table[MMI_PEN_EVENT_UP] = pen_fp;
  2067. }
  2068. /*****************************************************************************
  2069.  * FUNCTION
  2070.  *  mmi_pen_register_abort_handler
  2071.  * DESCRIPTION
  2072.  *  Register the Pen Abort handler.
  2073.  * PARAMETERS
  2074.  *  pen_fp      [IN]        
  2075.  * RETURNS
  2076.  *  void
  2077.  *****************************************************************************/
  2078. void mmi_pen_register_abort_handler(mmi_pen_hdlr pen_fp)
  2079. {
  2080.     /*----------------------------------------------------------------*/
  2081.     /* Local Variables                                                */
  2082.     /*----------------------------------------------------------------*/
  2083.     /*----------------------------------------------------------------*/
  2084.     /* Code Body                                                      */
  2085.     /*----------------------------------------------------------------*/
  2086.     g_pen_event_table[MMI_PEN_EVENT_ABORT] = pen_fp;
  2087. }
  2088. /*****************************************************************************
  2089.  * FUNCTION
  2090.  *  mmi_pen_start_capture_strokes
  2091.  * DESCRIPTION
  2092.  *  Start to capture stroke inside the handwriting area.
  2093.  *
  2094.  *  This API is typically used in entry function of editor screen.
  2095.  *
  2096.  *  The points are en-queued in 'point_array' in the following format:
  2097.  * 
  2098.  *  Each point contains two 16-bit integers for x, y coordinates. Two strokes are separated with
  2099.  *  marker (0xffff, 0), and all strokes of one character are ended with (0xffff, 0xffff) by
  2100.  *  mmi_pen_end_strokes_of_character(). The parameter 'max_points' should also include the memory 
  2101.  *  used by markers.
  2102.  *  (x0, y0) (x1, y1)