changeset 3216:70d12d1f88e0

Fixed #update and #save weirdness
author unexist
date Fri, 15 Jun 2012 19:18:12 +0200
parents 09ffa2ec6fbf
children 0c57ccd923e5
files src/subtlext/client.c src/subtlext/gravity.c src/subtlext/subtlext.c src/subtlext/subtlext.h src/subtlext/tag.c src/subtlext/view.c
diffstat 6 files changed, 101 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/src/subtlext/client.c	Fri Jun 01 14:30:23 2012 +0200
+++ b/src/subtlext/client.c	Fri Jun 15 19:18:12 2012 +0200
@@ -558,11 +558,8 @@
   rb_check_frozen(self);
   subSubtlextConnect(NULL); ///< Implicit open connection
 
-  /* Get client values */
-  win = NUM2LONG(rb_iv_get(self, "@win"));
-
   /* Check values */
-  if(0 <= win)
+  if(0 <= (win = NUM2LONG(rb_iv_get(self, "@win"))))
     {
       int *tags = NULL, *flags = NULL;
       char *wmname = NULL, *wminstance = NULL, *wmclass = NULL, *role = NULL;
@@ -1100,7 +1097,7 @@
           snprintf(buf, sizeof(buf), "%d", *id);
           gravity = subGravityInstantiate(buf);
 
-          subGravityUpdate(gravity);
+          subGravitySave(gravity);
 
           rb_iv_set(self, "@gravity", gravity);
 
--- a/src/subtlext/gravity.c	Fri Jun 01 14:30:23 2012 +0200
+++ b/src/subtlext/gravity.c	Fri Jun 15 19:18:12 2012 +0200
@@ -251,18 +251,18 @@
   return self;
 } /* }}} */
 
-/* subGravityUpdate {{{ */
+/* subGravitySave {{{ */
 /*
- * call-seq: update -> Subtlext::Gravity
+ * call-seq: save -> Subtlext::Gravity
  *
- * Update Gravity properties based on <b>required</b> Gravity index.
+ * Save new Gravity object.
  *
- *  gravity.update
+ *  gravity.save
  *  => nil
  */
 
 VALUE
-subGravityUpdate(VALUE self)
+subGravitySave(VALUE self)
 {
   int id = -1;
   XRectangle geom = { 0 };
@@ -319,6 +319,7 @@
       XFreeStringList(gravities);
     }
 
+  /* Set properties */
   rb_iv_set(self, "@id", INT2FIX(id));
 
   return self;
--- a/src/subtlext/subtlext.c	Fri Jun 01 14:30:23 2012 +0200
+++ b/src/subtlext/subtlext.c	Fri Jun 15 19:18:12 2012 +0200
@@ -1571,15 +1571,16 @@
       XInternAtom(display, prop_name, False), &nstrings)))
     {
       int selid = -1;
-      VALUE meth = Qnil, klass = Qnil, obj = Qnil;
+      VALUE meth_new = Qnil, meth_update = Qnil, klass = Qnil, obj = Qnil;
       regex_t *preg = subSharedRegexNew(source);
 
       /* Special values */
       if(isdigit(source[0])) selid = atoi(source);
 
       /* Fetch data */
-      meth  = rb_intern("new");
-      klass = rb_const_get(mod, rb_intern(class_name));
+      meth_new    = rb_intern("new");
+      meth_update = rb_intern("update");
+      klass       = rb_const_get(mod, rb_intern(class_name));
 
       /* Check each string */
       for(i = 0; i < nstrings; i++)
@@ -1591,11 +1592,15 @@
                 subSharedRegexMatch(preg, strings[i])))))
             {
               /* Create new object */
-              if(RTEST((obj = rb_funcall(klass, meth, 1,
+              if(RTEST((obj = rb_funcall(klass, meth_new, 1,
                   rb_str_new2(strings[i])))))
                 {
                   rb_iv_set(obj, "@id", INT2FIX(i));
 
+                  /* Call update method of object */
+                  if(rb_respond_to(obj, meth_update))
+                    rb_funcall(obj, meth_update, 0, Qnil);
+
                   /* Select first or many */
                   if(first)
                     {
@@ -2005,7 +2010,7 @@
 
   /* Class methods */
   rb_define_method(gravity, "initialize",   subGravityInit,           -1);
-  rb_define_method(gravity, "update",       subGravityUpdate,          0);
+  rb_define_method(gravity, "save",         subGravitySave,            0);
   rb_define_method(gravity, "clients",      subGravityClients,         0);
   rb_define_method(gravity, "geometry_for", subGravityGeometryFor,     1);
   rb_define_method(gravity, "geometry",     subGravityGeometryReader,  0);
@@ -2020,7 +2025,6 @@
   rb_define_alias(rb_singleton_class(gravity), "all", "list");
 
   /* Aliases */
-  rb_define_alias(gravity, "save", "update");
   rb_define_alias(gravity, "to_s", "to_str");
 
   /*
@@ -2204,7 +2208,7 @@
 
   /* Class methods */
   rb_define_method(tag, "initialize", subTagInit,     1);
-  rb_define_method(tag, "update",     subTagUpdate,   0);
+  rb_define_method(tag, "save",       subTagSave,     0);
   rb_define_method(tag, "clients",    subTagClients,  0);
   rb_define_method(tag, "views",      subTagViews,    0);
   rb_define_method(tag, "to_str",     subTagToString, 0);
@@ -2215,7 +2219,6 @@
   rb_define_alias(rb_singleton_class(tag), "all", "list");
 
   /* Aliases */
-  rb_define_alias(tag, "save", "update");
   rb_define_alias(tag, "to_s", "to_str");
 
   /*
@@ -2308,6 +2311,7 @@
   /* Class methods */
   rb_define_method(view, "initialize", subViewInit,          1);
   rb_define_method(view, "update",     subViewUpdate,        0);
+  rb_define_method(view, "save",       subViewSave,          0);
   rb_define_method(view, "clients",    subViewClients,       0);
   rb_define_method(view, "jump",       subViewJump,          0);
   rb_define_method(view, "next",       subViewSelectNext,    0);
@@ -2325,7 +2329,6 @@
   rb_define_alias(view, "+",     "tag");
   rb_define_alias(view, "-",     "untag");
   rb_define_alias(view, "click", "jump");
-  rb_define_alias(view, "save",  "update");
   rb_define_alias(view, "to_s",  "to_str");
 
   /*
--- a/src/subtlext/subtlext.h	Fri Jun 01 14:30:23 2012 +0200
+++ b/src/subtlext/subtlext.h	Fri Jun 15 19:18:12 2012 +0200
@@ -128,7 +128,7 @@
 /* Class */
 VALUE subGravityInstantiate(char *name);                          ///< Instantiate gravity
 VALUE subGravityInit(int argc, VALUE *argv, VALUE self);          ///< Create new gravity
-VALUE subGravityUpdate(VALUE self);                               ///< Update gravity
+VALUE subGravitySave(VALUE self);                                 ///< Save gravity
 VALUE subGravityClients(VALUE self);                              ///< List clients with gravity
 VALUE subGravityGeometryFor(VALUE self, VALUE value);             ///< Get geometry gravity for screen
 VALUE subGravityGeometryReader(VALUE self);                       ///< Get geometry gravity
@@ -233,7 +233,7 @@
 /* Class */
 VALUE subTagInstantiate(char *name);                              ///< Instantiate tag
 VALUE subTagInit(VALUE self, VALUE name);                         ///< Create tag
-VALUE subTagUpdate(VALUE self);                                   ///< Update tag
+VALUE subTagSave(VALUE self);                                     ///< Save tag
 VALUE subTagClients(VALUE self);                                  ///< Get clients with tag
 VALUE subTagViews(VALUE self);                                    ///< Get views with tag
 VALUE subTagToString(VALUE self);                                 ///< Tag to string
@@ -266,6 +266,7 @@
 VALUE subViewInstantiate(char *name);                             ///< Instantiate view
 VALUE subViewInit(VALUE self, VALUE name);                        ///< Create view
 VALUE subViewUpdate(VALUE self);                                  ///< Update view
+VALUE subViewSave(VALUE self);                                    ///< Save view
 VALUE subViewClients(VALUE self);                                 ///< Get clients of view
 VALUE subViewJump(VALUE self);                                    ///< Jump to view
 VALUE subViewSelectNext(VALUE self);                              ///< Select next view
--- a/src/subtlext/tag.c	Fri Jun 01 14:30:23 2012 +0200
+++ b/src/subtlext/tag.c	Fri Jun 15 19:18:12 2012 +0200
@@ -251,18 +251,18 @@
   return self;
 } /* }}} */
 
-/* subTagUpdate {{{ */
+/* subTagSave {{{ */
 /*
- * call-seq: update -> Subtlext::Tag
+ * call-seq: save -> Subtlext::Tag
  *
- * Update Tag properties based on Tag index.
+ * Save new Tag object.
  *
  *  tag.update
  *  => #<Subtlext::Tag:xxx>
  */
 
 VALUE
-subTagUpdate(VALUE self)
+subTagSave(VALUE self)
 {
   int id = -1;
   VALUE name = Qnil;
@@ -294,14 +294,17 @@
       char **tags = NULL;
 
       /* Get names of tags */
-      tags = subSharedPropertyGetStrings(display, DefaultRootWindow(display),
-        XInternAtom(display, "SUBTLE_TAG_LIST", False), &ntags);
+      if((tags = subSharedPropertyGetStrings(display, DefaultRootWindow(display),
+          XInternAtom(display, "SUBTLE_TAG_LIST", False), &ntags)))
+        {
 
-      id = ntags; ///< New id should be last
+          id = ntags; ///< New id should be last
 
-      if(tags) XFreeStringList(tags);
+          XFreeStringList(tags);
+        }
     }
 
+  /* Set properties */
   rb_iv_set(self, "@id", INT2FIX(id));
 
   return self;
--- a/src/subtlext/view.c	Fri Jun 01 14:30:23 2012 +0200
+++ b/src/subtlext/view.c	Fri Jun 15 19:18:12 2012 +0200
@@ -205,7 +205,7 @@
 VALUE
 subViewSingVisible(VALUE self)
 {
-  int i, nnames = 0;
+  int i, nnames = 0, *tags = NULL;
   char **names = NULL;
   unsigned long *visible = NULL;
   VALUE meth = Qnil, klass = Qnil, array = Qnil, v = Qnil;
@@ -221,9 +221,11 @@
   visible = (unsigned long *)subSharedPropertyGet(display,
     DefaultRootWindow(display), XA_CARDINAL, XInternAtom(display,
     "SUBTLE_VISIBLE_VIEWS", False), NULL);
+  tags  = (int *)subSharedPropertyGet(display, ROOT, XA_CARDINAL,
+      XInternAtom(display, "SUBTLE_VIEW_TAGS", False), NULL);
 
   /* Check results */
-  if(names && visible)
+  if(names && visible && tags)
     {
       for(i = 0; i < nnames; i++)
         {
@@ -231,14 +233,17 @@
           if(*visible & (1L << (i + 1)) &&
               !NIL_P(v = rb_funcall(klass, meth, 1, rb_str_new2(names[i]))))
             {
-              rb_iv_set(v, "@id", INT2FIX(i));
+              rb_iv_set(v, "@id",   INT2FIX(i));
+              rb_iv_set(v, "@tags", INT2FIX(tags[i]));
+
               rb_ary_push(array, v);
             }
         }
+    }
 
-      XFreeStringList(names);
-      free(visible);
-    }
+  if(names)   XFreeStringList(names);
+  if(visible) free(visible);
+  if(tags)    free(tags);
 
   return array;
 } /* }}} */
@@ -261,6 +266,7 @@
 subViewSingList(VALUE self)
 {
   int i, nnames = 0;
+  long *tags = NULL;
   char **names = NULL;
   VALUE meth = Qnil, klass = Qnil, array = Qnil, v = Qnil;
 
@@ -270,22 +276,28 @@
   klass = rb_const_get(mod, rb_intern("View"));
   meth  = rb_intern("new");
   array = rb_ary_new();
+  names = subSharedPropertyGetStrings(display, DefaultRootWindow(display),
+      XInternAtom(display, "_NET_DESKTOP_NAMES", False), &nnames);
+  tags  = (long *)subSharedPropertyGet(display, ROOT, XA_CARDINAL,
+      XInternAtom(display, "SUBTLE_VIEW_TAGS", False), NULL);
 
   /* Check results */
-  if((names = subSharedPropertyGetStrings(display, DefaultRootWindow(display),
-      XInternAtom(display, "_NET_DESKTOP_NAMES", False), &nnames)))
+  if(names && tags)
     {
       for(i = 0; i < nnames; i++)
         {
           if(!NIL_P(v = rb_funcall(klass, meth, 1, rb_str_new2(names[i]))))
             {
-              rb_iv_set(v, "@id",  INT2FIX(i));
+              rb_iv_set(v, "@id",   INT2FIX(i));
+              rb_iv_set(v, "@tags", LONG2NUM(tags[i]));
+
               rb_ary_push(array, v);
             }
         }
+    }
 
-      XFreeStringList(names);
-    }
+  if(names) XFreeStringList(names);
+  if(tags)  free(tags);
 
   return array;
 } /* }}} */
@@ -341,7 +353,7 @@
 
 /* subViewUpdate {{{ */
 /*
- * call-seq: Update -> Subtlext::View
+ * call-seq: update -> Subtlext::View
  *
  * Update View properties based on <b>required</b> View index.
  *
@@ -352,7 +364,43 @@
 VALUE
 subViewUpdate(VALUE self)
 {
-  int id = -1, *tags = NULL, ntags = 0;
+  long *tags = NULL, ntags = 0;
+  VALUE id = Qnil;
+
+  /* Check ruby object */
+  rb_check_frozen(self);
+  GET_ATTR(self, "@id", id);
+
+  subSubtlextConnect(NULL); ///< Implicit open connection
+
+  /* Fetch tags */
+  if((tags = (long *)subSharedPropertyGet(display, ROOT, XA_CARDINAL,
+      XInternAtom(display, "SUBTLE_VIEW_TAGS", False), (unsigned long *)&ntags)))
+    {
+      int idx = FIX2INT(id);
+
+      rb_iv_set(self, "@tags", LONG2NUM(idx < ntags ? tags[idx] : 0));
+
+      free(tags);
+    }
+
+  return self;
+} /* }}} */
+
+/* subViewSave {{{ */
+/*
+ * call-seq: save -> Subtlext::View
+ *
+ * Save new View object.
+ *
+ *  view.save
+ *  => #<Subtlext::View:xxx>
+ */
+
+VALUE
+subViewSave(VALUE self)
+{
+  int id = -1;
   VALUE name = Qnil;
 
   /* Check ruby object */
@@ -382,26 +430,18 @@
       char **names = NULL;
 
       /* Get names of views */
-      names = subSharedPropertyGetStrings(display, DefaultRootWindow(display),
-        XInternAtom(display, "_NET_DESKTOP_NAMES", False), &nnames);
+      if((names = subSharedPropertyGetStrings(display, DefaultRootWindow(display),
+          XInternAtom(display, "_NET_DESKTOP_NAMES", False), &nnames)))
+        {
+          id = nnames; ///< New id should be last
 
-      id = nnames; ///< New id should be last
-
-      if(names) XFreeStringList(names);
+          if(names) XFreeStringList(names);
+        }
     }
 
   /* Set properties */
   rb_iv_set(self, "@id", INT2FIX(id));
 
-  /* Fetch tags */
-  if((tags = (int *)subSharedPropertyGet(display, ROOT, XA_CARDINAL,
-      XInternAtom(display, "SUBTLE_VIEW_TAGS", False), (unsigned long *)&ntags)))
-    {
-      rb_iv_set(self, "@tags", INT2FIX(id < ntags ? tags[id] : 0));
-
-      free(tags);
-    }
-
   return self;
 } /* }}} */