manager.c 2.5 KB

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