manager.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Plugins manager - The peTI-NESulator Project
  3. * plugins.c
  4. *
  5. * Created by Manoel TRAPIER on 02/04/07.
  6. * Copyright (c) 2003-2018 986-Studio. All rights reserved.
  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. byte 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++; i++;
  36. }
  37. }
  38. int plugin_load(int id)
  39. {
  40. Plugin *ptr = &(Plugins[0]);
  41. int i = id;
  42. console_printf(Console_Default, "%s(%d)", __func__, id);
  43. for ( ; i > 1 && ptr->name != NULL; i -- )
  44. {
  45. console_printf(Console_Default, "%d - %s\n", i, ptr->name);
  46. ptr ++;
  47. }
  48. if (ptr == NULL)
  49. return -1;
  50. return ptr->init();
  51. }
  52. int plugin_unload(int id)
  53. {
  54. Plugin *ptr = &(Plugins[0]);
  55. for ( ; id == 0 && ptr != NULL; id -- )
  56. ptr ++;
  57. if (ptr == NULL)
  58. return -1;
  59. return ptr->deinit();
  60. }
  61. /* Available functions for plugins */
  62. int plugin_install_keypressHandler(byte key, PluginKeypress func)
  63. {
  64. KeyHandler *ptr;
  65. if (keyHandlersList == NULL)
  66. {
  67. keyHandlersList = (KeyHandler*) malloc(sizeof(KeyHandler));
  68. keyHandlersList->key = key;
  69. keyHandlersList->func = func;
  70. keyHandlersList->next = NULL;
  71. }
  72. else
  73. {
  74. ptr = keyHandlersList;
  75. while(ptr->next != NULL)
  76. ptr = ptr->next;
  77. ptr->next = (KeyHandler*) malloc(sizeof(KeyHandler));
  78. ptr = ptr->next;
  79. ptr->key = key;
  80. ptr->func = func;
  81. ptr->next = NULL;
  82. }
  83. return 0;
  84. }
  85. int plugin_remove_keypressHandler(byte key, PluginKeypress func)
  86. { /* actually do nothing, we cant remove plugin online */
  87. return 0;
  88. }
  89. /* Available functions outside of plugins */
  90. int plugin_keypress(byte key)
  91. {
  92. KeyHandler *ptr = keyHandlersList;
  93. while(ptr != NULL)
  94. {
  95. if (ptr->key == key)
  96. {
  97. ptr->func();
  98. }
  99. ptr = ptr->next;
  100. }
  101. return 0;
  102. }