1 /*
2  * Copyright 2019 LarsGit223
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 /*
20  * Code for the Workbench idle queue.
21  */
22 #include <glib/gstdio.h>
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #include <geanyplugin.h>
29 #include "wb_globals.h"
30 #include "idle_queue.h"
31 #include "tm_control.h"
32 
33 extern GeanyData *geany_data;
34 
35 typedef struct
36 {
37 	WB_IDLE_QUEUE_ACTION_ID id;
38 	gpointer param_a;
39 }WB_IDLE_QUEUE_ACTION;
40 
41 static GSList *s_idle_actions = NULL;
42 
43 
44 /* Clear idle queue */
wb_idle_queue_clear(void)45 static void wb_idle_queue_clear(void)
46 {
47 	if (s_idle_actions == NULL)
48 	{
49 		return;
50 	}
51 
52 	g_slist_free_full(s_idle_actions, g_free);
53 	s_idle_actions = NULL;
54 }
55 
56 
57 /* On-idle callback function. */
wb_idle_queue_callback(gpointer foo)58 static gboolean wb_idle_queue_callback(gpointer foo)
59 {
60 	static gboolean first = TRUE;
61 	GSList *elem = NULL;
62 	WB_IDLE_QUEUE_ACTION *action;
63 	static GMutex mutex;
64 
65 	if (first == TRUE)
66 	{
67 		first = FALSE;
68 		g_mutex_init (&mutex);
69 	}
70 
71 	g_mutex_lock(&mutex);
72 
73 	foreach_slist (elem, s_idle_actions)
74 	{
75 		action = elem->data;
76 		switch (action->id)
77 		{
78 			case WB_IDLE_ACTION_ID_TM_SOURCE_FILES_NEW:
79 				wb_tm_control_source_files_new(action->param_a);
80 			break;
81 
82 			case WB_IDLE_ACTION_ID_TM_SOURCE_FILE_ADD:
83 				wb_tm_control_source_file_add(action->param_a);
84 			break;
85 
86 			case WB_IDLE_ACTION_ID_TM_SOURCE_FILE_REMOVE:
87 				wb_tm_control_source_file_remove(action->param_a);
88 			break;
89 
90 			case WB_IDLE_ACTION_ID_TM_SOURCE_FILE_FREE:
91 				wb_tm_control_source_file_free(action->param_a);
92 			break;
93 
94 			case WB_IDLE_ACTION_ID_TM_SOURCE_FILES_REMOVE:
95 				wb_tm_control_source_files_remove(action->param_a);
96 			break;
97 		}
98 	}
99 
100 	wb_idle_queue_clear();
101 
102 	g_mutex_unlock(&mutex);
103 
104 	return FALSE;
105 }
106 
107 
108 /** Add a new idle action to the list.
109  *
110  * The function allocates a new WB_IDLE_QUEUE_ACTION structure and fills
111  * in the values passed. On-idle Geany will then call wb_idle_queue_callback
112  * and that function will call the function related to the action ID
113  * and pass the relevant parameters to it.
114  *
115  * @param id The action to execute on-idle
116  * @param param_a Parameter A
117  * @param param_a Parameter B
118  *
119  **/
wb_idle_queue_add_action(WB_IDLE_QUEUE_ACTION_ID id,gpointer param_a)120 void wb_idle_queue_add_action(WB_IDLE_QUEUE_ACTION_ID id, gpointer param_a)
121 {
122 	WB_IDLE_QUEUE_ACTION *action;
123 
124 	action = g_new0(WB_IDLE_QUEUE_ACTION, 1);
125 	action->id = id;
126 	action->param_a = param_a;
127 
128 	if (s_idle_actions == NULL)
129 	{
130 		plugin_idle_add(wb_globals.geany_plugin, (GSourceFunc)wb_idle_queue_callback, NULL);
131 	}
132 
133 	s_idle_actions = g_slist_prepend(s_idle_actions, action);
134 }
135