gnome-shell-3.6.3.1/src/run-js-test.c

No issues found

  1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
  2 /*
  3  * Based on gjs/console.c from GJS
  4  *
  5  * Copyright (c) 2008  litl, LLC
  6  * Copyright (c) 2010  Red Hat, Inc.
  7  *
  8  * Permission is hereby granted, free of charge, to any person obtaining a copy
  9  * of this software and associated documentation files (the "Software"), to
 10  * deal in the Software without restriction, including without limitation the
 11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12  * sell copies of the Software, and to permit persons to whom the Software is
 13  * furnished to do so, subject to the following conditions:
 14  *
 15  * The above copyright notice and this permission notice shall be included in
 16  * all copies or substantial portions of the Software.
 17  *
 18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 24  * IN THE SOFTWARE.
 25  */
 26 
 27 #include "config.h"
 28 
 29 #include <locale.h>
 30 #include <stdlib.h>
 31 #include <string.h>
 32 
 33 #include <clutter/x11/clutter-x11.h>
 34 #include <gdk/gdkx.h>
 35 #include <girepository.h>
 36 #include <gjs/gjs.h>
 37 #include <gtk/gtk.h>
 38 
 39 #include "shell-global.h"
 40 #include "shell-global-private.h"
 41 
 42 static char *command = NULL;
 43 
 44 static GOptionEntry entries[] = {
 45   { "command", 'c', 0, G_OPTION_ARG_STRING, &command, "Program passed in as a string", "COMMAND" },
 46   { NULL }
 47 };
 48 
 49 static GdkFilterReturn
 50 event_filter (GdkXEvent *xevent,
 51               GdkEvent  *event,
 52               gpointer   data)
 53 {
 54   XEvent *xev = (XEvent *)xevent;
 55 
 56   if (clutter_x11_handle_event (xev) == CLUTTER_X11_FILTER_CONTINUE)
 57     return GDK_FILTER_CONTINUE;
 58   else
 59     return GDK_FILTER_REMOVE;
 60 }
 61 
 62 int
 63 main(int argc, char **argv)
 64 {
 65   GOptionContext *context;
 66   GError *error = NULL;
 67   ShellGlobal *global;
 68   GjsContext *js_context;
 69   char *script;
 70   const char *filename;
 71   char *title;
 72   gsize len;
 73   int code;
 74 
 75   gtk_init (&argc, &argv);
 76 
 77   clutter_x11_set_display (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()));
 78   clutter_x11_disable_event_retrieval ();
 79 
 80   if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
 81     return 1;
 82 
 83   gdk_window_add_filter (NULL, event_filter, NULL);
 84 
 85   context = g_option_context_new (NULL);
 86 
 87   /* pass unknown through to the JS script */
 88   g_option_context_set_ignore_unknown_options (context, TRUE);
 89 
 90   g_option_context_add_main_entries (context, entries, NULL);
 91   if (!g_option_context_parse (context, &argc, &argv, &error))
 92     g_error ("option parsing failed: %s", error->message);
 93 
 94   setlocale (LC_ALL, "");
 95   g_type_init ();
 96 
 97   _shell_global_init (NULL);
 98   global = shell_global_get ();
 99   js_context = _shell_global_get_gjs_context (global);
100 
101   /* prepare command line arguments */
102   if (!gjs_context_define_string_array (js_context, "ARGV",
103                                         argc - 2, (const char**)argv + 2,
104                                         &error)) {
105     g_printerr ("Failed to defined ARGV: %s", error->message);
106     exit (1);
107   }
108 
109   if (command != NULL) {
110     script = command;
111     len = strlen (script);
112     filename = "<command line>";
113   } else if (argc <= 1) {
114     script = g_strdup ("const Console = imports.console; Console.interact();");
115     len = strlen (script);
116     filename = "<stdin>";
117   } else /*if (argc >= 2)*/ {
118     error = NULL;
119     if (!g_file_get_contents (argv[1], &script, &len, &error)) {
120       g_printerr ("%s\n", error->message);
121       exit (1);
122     }
123     filename = argv[1];
124   }
125 
126   title = g_filename_display_basename (filename);
127   g_set_prgname (title);
128   g_free (title);
129 
130 #if HAVE_BLUETOOTH
131   /* The module imports are all so intertwined that if the test
132    * imports anything in js/ui, it will probably eventually end up
133    * pulling in ui/status/bluetooth.js. So we need this.
134    */
135   g_irepository_prepend_search_path (BLUETOOTH_DIR);
136 #endif
137 
138   /* evaluate the script */
139   error = NULL;
140   if (!gjs_context_eval (js_context, script, len,
141                          filename, &code, &error)) {
142     g_free (script);
143     g_printerr ("%s\n", error->message);
144     exit (1);
145   }
146 
147   gjs_context_gc (js_context);
148   gjs_context_gc (js_context);
149 
150   g_free (script);
151   exit (code);
152 }