[giv] Added remote command "giv_string".



commit a1b5a65a3d0aadb0e214712c0a6ebe5b236c2798
Author: Dov Grobgeld <dov grobgeld gmail com>
Date:   Tue Dec 27 06:46:26 2011 +0200

    Added remote command "giv_string".

 ChangeLog                  |   12 ++++++++++++
 python/giv-client-marks.py |   32 ++++++++++++++++++++++++++++++++
 src/giv-win.gob            |   43 +++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 85 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5f95d0b..481eb03 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2011-12-27  Dov Grobgeld  <dov grobgeld gmail com>
+
+        * python/giv-client-marks.py : An example of how to use the new jsonrpc command "giv_string".
+
+        * giv-win.gob : Added remote command "giv_string".
+
+2011-12-26  Dov Grobgeld  <dov grobgeld gmail com>
+
+        * giv-win.gob, glib-jsonrpc/*: Added remote control support to giv.
+
+        * python/giv-client.py : An example python client showing how to remote control giv.
+
 2011-11-13  Dov Grobgeld  <dov grobgeld gmail com>
 
         * configure.in: Bumped version to 0.1.20
diff --git a/python/giv-client-marks.py b/python/giv-client-marks.py
new file mode 100644
index 0000000..b381753
--- /dev/null
+++ b/python/giv-client-marks.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+"""
+An example of how to create an external jsonrpc client for controlling
+giv.
+"""
+
+import httplib
+import random
+import math
+
+conn = httplib.HTTPConnection('localhost:8222')
+headers = {"Content-type": "application/x-www-form-urlencoded",
+           "Accept": "text/plain",
+           }
+
+for i in range(1024):
+  giv_string = ("$marks fcircle\n"
+                "$color red\n")
+  for j in range(20):
+    x = 512 * 1.0*j/20
+    phase = 512*1.0*i/20/10
+    y = 256+100*math.sin((x+phase) / 512.0 * 2*3.1459)
+    giv_string += "%f %f\n"%(x,y)
+
+  request = ('{"jsonrpc": "2.0", '
+             '"method": "giv_string", '
+             '"params": [\"'+giv_string+'"], '
+             '"id": %d}'%(random.randint(1,100)))
+  
+  conn.request("POST", url="", body= request, headers=headers)
+  response = conn.getresponse()
+  print response.read()
diff --git a/src/giv-win.gob b/src/giv-win.gob
index 293cd30..2c79773 100644
--- a/src/giv-win.gob
+++ b/src/giv-win.gob
@@ -213,7 +213,8 @@ void draw_to_cairo(cairo_t *cr,
                    GivWin *self);
 bool is_file_ext(const char *fn,
                  const char* ext_match);
-gboolean cb_load_image_when_idle(gpointer user_data);
+static gboolean cb_load_image_when_idle(gpointer user_data);
+static gboolean cb_redraw_when_idle(gpointer user_data);
 static void
 cb_contrast_changed(GtkWidget* widget,
                     double contrast_min,
@@ -3789,7 +3790,7 @@ bool is_file_ext(const char *filename,
     return res;
 }
 
-gboolean
+static gboolean
 cb_load_image_when_idle(gpointer user_data)
 {
     GivWin *self = (GivWin *)user_data;
@@ -3799,6 +3800,14 @@ cb_load_image_when_idle(gpointer user_data)
     return false;
 }
 
+static gboolean
+cb_redraw_when_idle(gpointer user_data)
+{
+    GivWin *self = (GivWin *)user_data;
+    giv_win_redraw(self);
+    return false;
+}
+
 // Turn off backing store if annotating (and thus redrawing).
 static void
 cb_image_annotate(GivWidget *imgv,
@@ -4141,6 +4150,32 @@ static int cmd_load_file(GLibJsonRpcServer *server,
   return 0;
 }
 
+static int cmd_giv_string(GLibJsonRpcServer *server,
+                          const char *method,
+                          JsonNode *params,
+                          JsonNode **response,
+                          gpointer user_data)
+{
+  GivWin *self = (GivWin*)user_data;
+  *response = json_node_new(JSON_NODE_VALUE);
+  JsonReader *reader = json_reader_new(params);
+  
+  // Assume array at the moment
+  json_reader_read_element(reader, 0);
+  const char *giv_string = json_reader_get_string_value(reader);
+
+  // Erase the old giv string
+  // TBD, make this configurable?
+  giv_widget_clear_giv(GIV_WIDGET(selfp->w_imgv));
+  giv_widget_add_giv_from_string(GIV_WIDGET(selfp->w_imgv), giv_string);
+
+  g_object_unref(reader);
+  json_node_set_string(*response, "Ok");
+  g_idle_add(cb_redraw_when_idle, self);
+  
+  return 0;
+}
+
 // Define remote commands
 static void create_remote_commands(GivWin *self)
 {
@@ -4152,6 +4187,10 @@ static void create_remote_commands(GivWin *self)
                                        "load_file",
                                        cmd_load_file,
                                        self);
+  glib_jsonrpc_server_register_command(selfp->jsonrpc_server,
+                                       "giv_string",
+                                       cmd_giv_string,
+                                       self);
 }
 
 %}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]