manager.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Plugins manager - The peTI-NESulator Project
  3. * plugins.c
  4. *
  5. * Created by Manoël Trapier on 02/04/07.
  6. * Copyright (c) 2002-2019 986-Studio.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <os_dependent.h>
  12. #include <plugins/manager.h>
  13. typedef struct Plugin_
  14. {
  15. char *name;
  16. PluginInit init;
  17. PluginDeinit deinit;
  18. } Plugin;
  19. typedef struct KeyHandler_
  20. {
  21. uint8_t key;
  22. PluginKeypress func;
  23. struct KeyHandler_ *next;
  24. } KeyHandler;
  25. KeyHandler *keyHandlersList = NULL;
  26. #include "plugins_list.h"
  27. void plugin_list()
  28. {
  29. Plugin *ptr = &(Plugins[0]);
  30. int i = 1;
  31. console_printf(Console_Default, "Available plugins:\n");
  32. while (ptr->name != NULL)
  33. {
  34. console_printf(Console_Default, "%d - %s\n", i, ptr->name);
  35. ptr++;
  36. i++;
  37. }
  38. }
  39. int plugin_load(int id)
  40. {
  41. Plugin *ptr = &(Plugins[0]);
  42. int i = id;
  43. console_printf(Console_Default, "%s(%d)", __func__, id);
  44. for (; i > 1 && ptr->name != NULL ; i--)
  45. {
  46. console_printf(Console_Default, "%d - %s\n", i, ptr->name);
  47. ptr++;
  48. }
  49. if (ptr == NULL)
  50. {
  51. return -1;
  52. }
  53. return ptr->init();
  54. }
  55. int plugin_unload(int id)
  56. {
  57. Plugin *ptr = &(Plugins[0]);
  58. for (; id == 0 && ptr != NULL ; id--)
  59. {
  60. ptr++;
  61. }
  62. if (ptr == NULL)
  63. {
  64. return -1;
  65. }
  66. return ptr->deinit();
  67. }
  68. /* Available functions for plugins */
  69. int plugin_install_keypressHandler(uint8_t key, PluginKeypress func)
  70. {
  71. KeyHandler *ptr;
  72. if (keyHandlersList == NULL)
  73. {
  74. keyHandlersList = (KeyHandler *)malloc(sizeof(KeyHandler));
  75. keyHandlersList->key = key;
  76. keyHandlersList->func = func;
  77. keyHandlersList->next = NULL;
  78. }
  79. else
  80. {
  81. ptr = keyHandlersList;
  82. while (ptr->next != NULL)
  83. {
  84. ptr = ptr->next;
  85. }
  86. ptr->next = (KeyHandler *)malloc(sizeof(KeyHandler));
  87. ptr = ptr->next;
  88. ptr->key = key;
  89. ptr->func = func;
  90. ptr->next = NULL;
  91. }
  92. return 0;
  93. }
  94. int plugin_remove_keypressHandler(uint8_t key, PluginKeypress func)
  95. { /* actually do nothing, we cant remove plugin online */
  96. return 0;
  97. }
  98. /* Available functions outside of plugins */
  99. int plugin_keypress(uint8_t key)
  100. {
  101. KeyHandler *ptr = keyHandlersList;
  102. while (ptr != NULL)
  103. {
  104. if (ptr->key == key)
  105. {
  106. ptr->func();
  107. }
  108. ptr = ptr->next;
  109. }
  110. return 0;
  111. }