nx_open.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. * graphics/nxsu/nx_open.c
  3. *
  4. * Copyright (C) 2008-2010, 2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdbool.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <nuttx/kmalloc.h>
  45. #include <nuttx/nx/nx.h>
  46. #include "nxfe.h"
  47. /****************************************************************************
  48. * Pre-Processor Definitions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Private Types
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Function Prototypes
  55. ****************************************************************************/
  56. static void nxsu_bkgdredraw(NXWINDOW hwnd,
  57. FAR const struct nxgl_rect_s *rect,
  58. bool more, FAR void *arg);
  59. /****************************************************************************
  60. * Private Data
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Public Data
  64. ****************************************************************************/
  65. const struct nx_callback_s g_bkgdcb =
  66. {
  67. nxsu_bkgdredraw, /* redraw */
  68. NULL /* position */
  69. #ifdef CONFIG_NX_MOUSE
  70. , NULL /* mousein */
  71. #endif
  72. #ifdef CONFIG_NX_KBD
  73. , NULL /* my kbdin */
  74. #endif
  75. };
  76. /****************************************************************************
  77. * Private Functions
  78. ****************************************************************************/
  79. /****************************************************************************
  80. * Name: nxsu_bkgdredraw
  81. ****************************************************************************/
  82. static void nxsu_bkgdredraw(NXWINDOW hwnd,
  83. FAR const struct nxgl_rect_s *rect,
  84. bool more, FAR void *arg)
  85. {
  86. FAR struct nxbe_window_s *wnd = (FAR struct nxbe_window_s *)hwnd;
  87. FAR struct nxbe_state_s *be = wnd->be;
  88. gvdbg("BG redraw rect={(%d,%d),(%d,%d)}\n",
  89. rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y);
  90. nxbe_fill(wnd, &wnd->bounds, be->bgcolor);
  91. }
  92. /****************************************************************************
  93. * Name: nxsu_setup
  94. ****************************************************************************/
  95. static inline int nxsu_setup(FAR NX_DRIVERTYPE *dev,
  96. FAR struct nxfe_state_s *fe)
  97. {
  98. int ret;
  99. /* Configure the framebuffer device */
  100. ret = nxbe_configure(dev, &fe->be);
  101. if (ret < 0)
  102. {
  103. gdbg("nxbe_configure failed: %d\n", -ret);
  104. errno = -ret;
  105. return ERROR;
  106. }
  107. #if CONFIG_FB_CMAP
  108. ret = nxbe_colormap(dev);
  109. if (ret < 0)
  110. {
  111. gdbg("nxbe_colormap failed: %d\n", -ret);
  112. errno = -ret;
  113. return ERROR;
  114. }
  115. #endif
  116. /* Initialize the non-NULL elements of the back-end structure window */
  117. /* Initialize the background window */
  118. fe->be.bkgd.be = &fe->be;
  119. fe->be.bkgd.cb = &g_bkgdcb;
  120. fe->be.bkgd.bounds.pt2.x = fe->be.vinfo.xres - 1;
  121. fe->be.bkgd.bounds.pt2.y = fe->be.vinfo.yres - 1;
  122. /* Complete initialization of the server state structure. The
  123. * window list contains only one element: The background window
  124. * with nothing else above or below it
  125. */
  126. fe->be.topwnd = &fe->be.bkgd;
  127. /* Initialize the mouse position */
  128. #ifdef CONFIG_NX_MOUSE
  129. nxsu_mouseinit(fe->be.vinfo.xres, fe->be.vinfo.yres);
  130. #endif
  131. return OK;
  132. }
  133. /****************************************************************************
  134. * Public Functions
  135. ****************************************************************************/
  136. /****************************************************************************
  137. * Name: nx_open
  138. *
  139. * Description:
  140. * Create, initialize and return an NX handle for use in subsequent
  141. * NX API calls. nx_open is the single user equivalent of nx_connect
  142. * plus nx_run.
  143. *
  144. * Input Parameters:
  145. * dev - Vtable "object" of the framebuffer/LCD "driver" to use
  146. *
  147. * Return:
  148. * Success: A non-NULL handle used with subsequent NX accesses
  149. * Failure: NULL is returned and errno is set appropriately
  150. *
  151. ****************************************************************************/
  152. NXHANDLE nx_open(FAR NX_DRIVERTYPE *dev)
  153. {
  154. FAR struct nxfe_state_s *fe;
  155. int ret;
  156. /* Sanity checking */
  157. #ifdef CONFIG_DEBUG
  158. if (!dev)
  159. {
  160. errno = EINVAL;
  161. return NULL;
  162. }
  163. #endif
  164. /* Allocate the NX state structure */
  165. fe = (FAR struct nxfe_state_s *)kzalloc(sizeof(struct nxfe_state_s));
  166. if (!fe)
  167. {
  168. errno = ENOMEM;
  169. return NULL;
  170. }
  171. /* Initialize and configure the server */
  172. ret = nxsu_setup(dev, fe);
  173. if (ret < 0)
  174. {
  175. return NULL; /* nxsu_setup sets errno */
  176. }
  177. /* Fill the initial background window */
  178. nxbe_fill(&fe->be.bkgd, &fe->be.bkgd.bounds, fe->be.bgcolor);
  179. return (NXHANDLE)fe;
  180. }