nxterm_register.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /****************************************************************************
  2. * nuttx/graphics/nxterm/nxterm_register.c
  3. *
  4. * Copyright (C) 2012, 2016-2017 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 <sys/types.h>
  40. #include <stdbool.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <debug.h>
  46. #include <nuttx/kmalloc.h>
  47. #include <nuttx/semaphore.h>
  48. #include <nuttx/fs/fs.h>
  49. #include "nxterm.h"
  50. /****************************************************************************
  51. * Public Functions
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Name: nxterm_allocate
  55. ****************************************************************************/
  56. FAR struct nxterm_state_s *
  57. nxterm_register(NXTERM handle, FAR struct nxterm_window_s *wndo,
  58. FAR const struct nxterm_operations_s *ops, int minor)
  59. {
  60. FAR struct nxterm_state_s *priv;
  61. FAR const struct nx_font_s *fontset;
  62. char devname[NX_DEVNAME_SIZE];
  63. NXHANDLE hfont;
  64. int ret;
  65. DEBUGASSERT(handle && wndo && ops && (unsigned)minor < 256);
  66. /* Allocate the driver structure */
  67. priv = (FAR struct nxterm_state_s *)kmm_zalloc(sizeof(struct nxterm_state_s));
  68. if (!priv)
  69. {
  70. gerr("ERROR: Failed to allocate the NX driver structure\n");
  71. return NULL;
  72. }
  73. /* Initialize the driver structure */
  74. priv->ops = ops;
  75. priv->handle = handle;
  76. priv->minor = minor;
  77. memcpy(&priv->wndo, wndo, sizeof(struct nxterm_window_s));
  78. nxsem_init(&priv->exclsem, 0, 1);
  79. #ifdef CONFIG_DEBUG_GRAPHICS
  80. priv->holder = NO_HOLDER;
  81. #endif
  82. #ifdef CONFIG_NXTERM_NXKBDIN
  83. /* The waitsem semaphore is used for signaling and, hence, should not have
  84. * priority inheritance enabled.
  85. */
  86. nxsem_init(&priv->waitsem, 0, 0);
  87. nxsem_setprotocol(&priv->waitsem, SEM_PRIO_NONE);
  88. #endif
  89. /* Connect to the font cache for the configured font characteristics */
  90. priv->fcache = nxf_cache_connect(wndo->fontid, wndo->fcolor[0],
  91. wndo->wcolor[0], CONFIG_NXTERM_BPP,
  92. CONFIG_NXTERM_CACHESIZE);
  93. if (priv->fcache == NULL)
  94. {
  95. gerr("ERROR: Failed to connect to font cache for font ID %d: %d\n",
  96. wndo->fontid, errno);
  97. goto errout;
  98. }
  99. /* Get the handle of the font managed by the font cache */
  100. hfont = nxf_cache_getfonthandle(priv->fcache);
  101. if (hfont == NULL)
  102. {
  103. gerr("ERROR: Failed to get handlr for font ID %d: %d\n",
  104. wndo->fontid, errno);
  105. goto errout;
  106. }
  107. /* Get information about the font set being used and save this in the
  108. * state structure
  109. */
  110. fontset = nxf_getfontset(hfont);
  111. priv->fheight = fontset->mxheight;
  112. priv->fwidth = fontset->mxwidth;
  113. priv->spwidth = fontset->spwidth;
  114. /* Set up the text cache */
  115. priv->maxchars = CONFIG_NXTERM_MXCHARS;
  116. /* Clear the display */
  117. nxterm_clear(priv);
  118. /* Set the initial display position */
  119. nxterm_home(priv);
  120. /* Show the cursor */
  121. priv->cursor.code = CONFIG_NXTERM_CURSORCHAR;
  122. nxterm_showcursor(priv);
  123. /* Register the driver */
  124. snprintf(devname, NX_DEVNAME_SIZE, NX_DEVNAME_FORMAT, minor);
  125. ret = register_driver(devname, &g_nxterm_drvrops, 0666, priv);
  126. if (ret < 0)
  127. {
  128. gerr("ERROR: Failed to register %s\n", devname);
  129. }
  130. return (NXTERM)priv;
  131. errout:
  132. kmm_free(priv);
  133. return NULL;
  134. }