Add tools to the Workbench Tools menu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wb.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. ToolsMenu - Add tools to the Workbench Tools menu
  3. Copyright (C) 2015, 2018 Kim Fastrup Larsen
  4. This program is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation, either ver-
  7. sion 3 of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be use-
  9. ful, but WITHOUT ANY WARRANTY; without even the implied war-
  10. ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public Li-
  13. cense along with this program. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. The author can be contacted on <kimflarsen@hotmail.com>
  16. */
  17. #include <dos/dos.h>
  18. #include <exec/memory.h>
  19. #include <workbench/startup.h>
  20. #include <workbench/workbench.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/icon_protos.h>
  24. #include <clib/wb_protos.h>
  25. #ifdef USE_PRAGMAS
  26. #include <proto/dos.h>
  27. #include <proto/exec.h>
  28. #include <proto/icon.h>
  29. #include <proto/wb.h>
  30. #endif
  31. #include <stdlib.h>
  32. #include <strings.h>
  33. #include "common.h"
  34. #include "cx.h"
  35. #include "toolrun.h"
  36. #include "wb.h"
  37. static struct MsgPort *wb_port;
  38. static void add_tool_from_icon(char *name)
  39. {
  40. char path[MAX_PATH_LENGTH + 1];
  41. BPTR lock;
  42. if ((lock = Lock(name, ACCESS_READ)) == NULL)
  43. return;
  44. if (NameFromLock(lock, path, MAX_PATH_LENGTH + 1))
  45. cx_add_tool(name, path);
  46. UnLock(lock);
  47. }
  48. static void handle_app_window_msg(struct AppMessage *msg)
  49. {
  50. struct WBArg *arg;
  51. struct WBArg *end = msg->am_ArgList + msg->am_NumArgs;
  52. BPTR old_dir;
  53. struct DiskObject *disk_obj;
  54. cx_show();
  55. for (arg = msg->am_ArgList; arg != end; ++arg) {
  56. old_dir = CurrentDir(arg->wa_Lock);
  57. /* Check that name isn't NULL and isn't empty, and that the icon is
  58. a Workbench tool. */
  59. if (arg->wa_Name != NULL && *arg->wa_Name != '\0')
  60. if ((disk_obj = GetDiskObject(arg->wa_Name)) != NULL) {
  61. if (disk_obj->do_Type == WBTOOL)
  62. add_tool_from_icon(arg->wa_Name);
  63. FreeDiskObject(disk_obj);
  64. }
  65. CurrentDir(old_dir);
  66. }
  67. ReplyMsg((struct Message *) msg);
  68. }
  69. static void free_args(struct WBArg *arg_list, LONG num_args)
  70. {
  71. FreeVec(arg_list[0].wa_Name);
  72. if (arg_list[0].wa_Lock != NULL)
  73. UnLock(arg_list[0].wa_Lock);
  74. FreeMem(arg_list, sizeof(struct WBArg) * num_args);
  75. }
  76. /* Creates the directory locks and tool names to pass to tr_run_tool(). It
  77. actually only creates the first element, ie. the home directory and name
  78. of the tool itself, the rest are copied from the AppMessage that
  79. workbench.library sent us. */
  80. static struct WBArg *create_args(STRPTR path, struct WBArg *arg_list,
  81. LONG num_args)
  82. {
  83. struct WBArg *result;
  84. size_t args_size;
  85. UBYTE t;
  86. UBYTE *file_part;
  87. /* Create an array one element bigger than the one received from
  88. workbench.library, so we can put the tool itself at the front. */
  89. args_size = sizeof(struct WBArg) * num_args;
  90. result = AllocMem(args_size+sizeof(struct WBArg),MEMF_PUBLIC|MEMF_CLEAR);
  91. if (result == NULL)
  92. return NULL;
  93. /* Temporarily separate the file part and directory part of the tool's
  94. path with a string terminator. */
  95. t = *(file_part = FilePart(path));
  96. *file_part = '\0';
  97. /* Create the tool's directory lock. */
  98. result[0].wa_Lock = Lock(path, ACCESS_READ);
  99. /* Restore original string. */
  100. *file_part = t;
  101. if (result[0].wa_Lock == NULL)
  102. goto create_args_failed;
  103. /* Copy the tool's name. */
  104. if ((result[0].wa_Name = copy_of(file_part)) == NULL)
  105. goto create_args_failed;
  106. /* The rest of the array is just a copy of the AppMessage array. */
  107. if (args_size > 0)
  108. memcpy(result + 1, arg_list, args_size);
  109. return result;
  110. create_args_failed:
  111. free_args(result, num_args + 1);
  112. return NULL;
  113. }
  114. /* This function is called whenever a tool we started quits. At this point,
  115. we reply to the workbench.library so it can free the arguments that we
  116. borrowed. Called indirectly by tr_clean_up(), so we must not clean up
  117. anything needed by reply_app_msg() until after the call to tr_clean_up().
  118. */
  119. static void reply_app_msg(struct WBArg *arg_list, LONG num_args,
  120. struct AppMessage *app_message)
  121. {
  122. if (arg_list != NULL)
  123. free_args(arg_list, num_args);
  124. ReplyMsg((struct Message *) app_message);
  125. }
  126. static void handle_app_menu_item_msg(struct AppMessage *msg)
  127. {
  128. Tool *t = (Tool *) msg->am_UserData;
  129. struct WBArg *args = NULL;
  130. LONG num_args = msg->am_NumArgs + 1;
  131. /* Check that the path isn't empty and that the tool hasn't been removed
  132. prior to handling this event, then create the Workbench arguments and
  133. run the tool. */
  134. if (*t->path == '\0' || !is_in_list(tools, &t->node) ||
  135. (args = create_args(t->path,msg->am_ArgList,msg->am_NumArgs)) == NULL ||
  136. !tr_run_tool(args, num_args, reply_app_msg, msg))
  137. /* Failed to run, reply to the AppMessage right away. */
  138. reply_app_msg(args, num_args, msg);
  139. }
  140. ULONG wb_set_up()
  141. {
  142. if ((wb_port = CreateMsgPort()) == NULL)
  143. exit(RETURN_FAIL);
  144. return 1UL << wb_port->mp_SigBit;
  145. }
  146. struct AppWindow *wb_add_window(struct Window *window)
  147. {
  148. return AddAppWindow(0, 0, window, wb_port, TAG_DONE);
  149. }
  150. void wb_remove_window(struct AppWindow *app_window)
  151. {
  152. RemoveAppWindow(app_window);
  153. }
  154. void wb_add_tool(Tool *tool)
  155. {
  156. tool->menu_item = AddAppMenuItemA(0, (ULONG) tool, tool->node.ln_Name,
  157. wb_port, TAG_DONE);
  158. }
  159. void wb_remove_tool(Tool *tool)
  160. {
  161. RemoveAppMenuItem(tool->menu_item);
  162. tool->menu_item = NULL;
  163. }
  164. void wb_handle_signal(void)
  165. {
  166. struct AppMessage *msg;
  167. while ((msg = (struct AppMessage *) GetMsg(wb_port)) != NULL)
  168. switch (msg->am_Type) {
  169. case AMTYPE_APPWINDOW:
  170. handle_app_window_msg(msg);
  171. break;
  172. case AMTYPE_APPMENUITEM:
  173. handle_app_menu_item_msg(msg);
  174. break;
  175. }
  176. }
  177. void wb_clean_up()
  178. {
  179. delete_port(wb_port);
  180. }