File: script-fu-run-func.c

package info (click to toggle)
gimp 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,880 kB
  • sloc: ansic: 870,914; python: 10,965; lisp: 10,857; cpp: 7,355; perl: 4,536; sh: 1,753; xml: 972; yacc: 609; lex: 348; javascript: 150; makefile: 42
file content (338 lines) | stat: -rw-r--r-- 10,877 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>
#include <glib.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "scheme-wrapper.h"       /* type "pointer" */

#include "script-fu-lib.h"
#include "script-fu-types.h"
#include "script-fu-interface.h"  /* ScriptFu's GUI implementation. */
#include "script-fu-dialog.h"     /* Gimp's GUI implementation. */
#include "script-fu-script.h"
#include "script-fu-scripts.h"    /* script_fu_find_script */
#include "script-fu-command.h"
#include "script-fu-version.h"
#include "script-fu-progress.h"

#include "script-fu-run-func.h"

/* Outer run_funcs
 * One each for GimpProcedure and GimpImageProcedure.
 * These are called from Gimp, with two different signatures.
 * These form and interpret "commands" which are calls to inner run_funcs
 * defined in Scheme by a script.

 * These return the result of interpretation,
 * in a GimpValueArray whose only element is a status.
 * !!! ScriptFu does not let authors define procedures that return values.
 *
 * A prior script may have called (script-fu-use-v3) to opt in to interpret v3 dialect.
 * When this is long-running extension-script-fu process,
 * ensure initial dialect is v2, the default.
 * FUTURE: default is v3 and script must opt in to v2 dialect.
 */

/* run_func for a GimpImageProcedure
 *
 * Type is GimpRunImageFunc.
 *
 * Uses Gimp's config and gui.
 *
 * Since 3.0
 */
GimpValueArray *
script_fu_run_image_procedure (GimpProcedure        *procedure, /* GimpImageProcedure */
                               GimpRunMode           run_mode,
                               GimpImage            *image,
                               GimpDrawable        **drawables,
                               GimpProcedureConfig  *config,
                               gpointer              data)
{

  GimpValueArray *result = NULL;
  SFScript       *script;

  g_debug ("script_fu_run_image_procedure");
  script = script_fu_find_script (gimp_procedure_get_name (procedure));

  if (! script)
    return gimp_procedure_new_return_values (procedure, GIMP_PDB_CALLING_ERROR, NULL);

  ts_set_run_mode (run_mode);

  begin_interpret_default_dialect ();

  script_fu_progress_init (gimp_procedure_get_menu_label (procedure));

  switch (run_mode)
    {
    case GIMP_RUN_INTERACTIVE:
      {
        guint n_specs;

        gimp_ui_init ("script-fu");

        g_free (g_object_class_list_properties (G_OBJECT_GET_CLASS (config), &n_specs));
        if (n_specs > 1)
          {
            /* Let user choose "other" args in a dialog, then interpret. Maintain a config. */
            result = script_fu_dialog_run_image_proc (procedure, script, image, drawables, config);
          }
        else
          {
            /* No "other" args for user to choose. No config to maintain. */
            result = script_fu_interpret_image_proc (procedure, script, image, drawables, config);
          }
        break;
      }
    case GIMP_RUN_NONINTERACTIVE:
      {
        /* A call from another PDB procedure.
         * Use the given config, without interacting with user.
         * Since no user interaction, no config to maintain.
         */
        result = script_fu_interpret_image_proc (procedure, script, image, drawables, config);
        break;
      }
    case GIMP_RUN_WITH_LAST_VALS:
      {
        /* User invoked from a menu "Filter>Run with last values".
         * Do not show dialog. config are already last values.
         */
        result = script_fu_interpret_image_proc (procedure, script, image, drawables, config);
        break;
      }
    default:
      {
        result = gimp_procedure_new_return_values (procedure, GIMP_PDB_CALLING_ERROR, NULL);
      }
    }

  return result;
}

/* The number of pspecs in a config's pspecs to skip, for a regular GimpProcedure.
 * Skip procedure-name and run-mode.
 */
#define SF_ARGS_SKIPPED_REGULAR 2

/* run_func for a GimpProcedure
 *
 * Type is GimpRunFunc.
 *
 * Uses Gimp's config and gui.
 *
 * Since 3.0
 */
GimpValueArray *
script_fu_run_regular_procedure (GimpProcedure        *procedure,
                                 GimpProcedureConfig  *config,
                                 gpointer              data)
{

  GimpValueArray *result = NULL;
  SFScript       *script;
  GimpRunMode     run_mode;

  g_debug ("%s", G_STRFUNC);

  script = script_fu_find_script (gimp_procedure_get_name (procedure));

  if (! script)
    return gimp_procedure_new_return_values (procedure, GIMP_PDB_CALLING_ERROR, NULL);

  /* Unlike ImageProcedure, run-mode is a prop in the config. */
  g_object_get (config, "run-mode", &run_mode, NULL);
  ts_set_run_mode (run_mode);

  begin_interpret_default_dialect ();

  script_fu_progress_init (gimp_procedure_get_menu_label (procedure));

  switch (run_mode)
    {
    case GIMP_RUN_INTERACTIVE:
      {
        guint n_specs;

        gimp_ui_init ("script-fu");

        g_free (g_object_class_list_properties (G_OBJECT_GET_CLASS (config), &n_specs));
        if (n_specs > SF_ARGS_SKIPPED_REGULAR)
          {
            /* Let user choose non-skipped args in a dialog, then interpret. Maintain a config. */
            result = script_fu_dialog_run_regular_proc (procedure, script, config);
          }
        else
          {
            /* No args for user to choose. No config to maintain. */
            result = script_fu_interpret_regular_proc (procedure, script, config);
          }
        break;
      }
    case GIMP_RUN_NONINTERACTIVE:
      {
        /* A call from another PDB procedure.
         * Use the given config, without interacting with user.
         * Since no user interaction, no config to maintain.
         */
        result = script_fu_interpret_regular_proc (procedure, script, config);
        break;
      }
    case GIMP_RUN_WITH_LAST_VALS:
      {
        /* User invoked from a menu "Filter>Run with last values".
         * Do not show dialog. config are already last values.
         */
        result = script_fu_interpret_regular_proc (procedure, script, config);
        break;
      }
    default:
      {
        result = gimp_procedure_new_return_values (procedure, GIMP_PDB_CALLING_ERROR, NULL);
      }
    }

  return result;
}

/* run_func for a GimpProcedure registered using Scheme "script-fu-register"
 *
 * Type is GimpRunFunc
 *
 * Uses ScriptFu's own GUI implementation, and retains settings locally.
 *
 * Since prior to 3.0 but formerly named script_fu_script_proc
 */
GimpValueArray *
script_fu_run_procedure (GimpProcedure       *procedure,
                         GimpProcedureConfig *config,
                         gpointer             data)
{
  GimpPDBStatusType   status = GIMP_PDB_SUCCESS;
  SFScript           *script;
  GParamSpec        **pspecs;
  guint               n_pspecs;
  gint                n_aux_args;
  GimpRunMode         run_mode;
  GError             *error = NULL;

  script = script_fu_find_script (gimp_procedure_get_name (procedure));

  if (! script)
    return gimp_procedure_new_return_values (procedure,
                                             GIMP_PDB_CALLING_ERROR,
                                             NULL);

  pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (config), &n_pspecs);
  gimp_procedure_get_aux_arguments (procedure, &n_aux_args);

  g_object_get (config, "run-mode", &run_mode, NULL);

  ts_set_run_mode (run_mode);

  begin_interpret_default_dialect ();

  script_fu_progress_init (gimp_procedure_get_menu_label (procedure));

  switch (run_mode)
    {
    case GIMP_RUN_INTERACTIVE:
      {
        gint min_args = 0;

        gimp_ui_init ("script-fu");

        /*  First, try to collect the standard script arguments...  */
        min_args = script_fu_script_collect_standard_args (script, pspecs, n_pspecs, config);

        /*  If plugin has more than the standard args. */
        if (script->n_args > min_args)
          {
            /* Get the rest of arguments with a dialog, and run the command.  */
            status = script_fu_interface_dialog (script, min_args);

            if (status == GIMP_PDB_EXECUTION_ERROR)
              return gimp_procedure_new_return_values (procedure, status,
                                                       script_fu_get_gerror ());

            /* Else no error, or GIMP_PDB_CANCEL.
             * GIMP_PDB_CALLING_ERROR is emitted prior to this.
             * Break and return without an error message.
             */
            break;
          }
        /*  Else fallthrough to next case and run the script without dialog. */
      }

    case GIMP_RUN_NONINTERACTIVE:
      /* Verify actual args count equals declared arg count. */
      if (n_pspecs != script->n_args + n_aux_args + SF_ARG_TO_CONFIG_OFFSET)
        status = GIMP_PDB_CALLING_ERROR;

      if (status == GIMP_PDB_SUCCESS)
        {
          gchar *command;

          command = script_fu_script_get_command_from_params (script, pspecs, n_pspecs, config);

          /*  run the command through the interpreter  */
          if (! script_fu_run_command (command, &error))
            {
              return gimp_procedure_new_return_values (procedure,
                                                       GIMP_PDB_EXECUTION_ERROR,
                                                       error);
            }

          g_free (command);
        }
      break;

    case GIMP_RUN_WITH_LAST_VALS:
      {
        gchar *command;

        /*  First, try to collect the standard script arguments  */
        script_fu_script_collect_standard_args (script, pspecs, n_pspecs, config);

        command = script_fu_script_get_command (script);

        /*  run the command through the interpreter  */
        if (! script_fu_run_command (command, &error))
          {
            return gimp_procedure_new_return_values (procedure,
                                                     GIMP_PDB_EXECUTION_ERROR,
                                                     error);
          }

        g_free (command);
      }
      break;

    default:
      break;
    }

  return gimp_procedure_new_return_values (procedure, status, NULL);
}