Add a geometry command line option

When mtplot was launched, it always occupied the whole screen. This
patch provides a geometry command line option to modify the window
size and the window offset.

BUG=chromium-os:33669
TEST=Type the following command on a chrome machine, and see its
window size is smaller than the screen size.
$ mtplot -d :0 -g 600x400+100+60

Change-Id: I4c3bfc23ad561199c19bb68c4a65328eb3b3db48
Reviewed-on: https://gerrit.chromium.org/gerrit/30846
Commit-Ready: Joseph Shyh-In Hwang <[email protected]>
Reviewed-by: Joseph Shyh-In Hwang <[email protected]>
Tested-by: Joseph Shyh-In Hwang <[email protected]>
diff --git a/mtplot.c b/mtplot.c
index 42cc84e..b1da1f5 100644
--- a/mtplot.c
+++ b/mtplot.c
@@ -471,7 +471,7 @@
 static unsigned int w_width;
 static unsigned int w_height;
 
-static Display *InitDisplay(const char *displayname) {
+static Display *InitDisplay(const char *displayname, const char *geometry) {
   Window root;
   int x, y;
   unsigned int border_width;
@@ -484,9 +484,15 @@
 
   blackColor = BlackPixel(dpy, DefaultScreen(dpy));
 
+  // Determine the window geometry
+  if (!geometry || XParseGeometry(geometry, &x, &y, &w_width, &w_height) == 0) {
+    x = y = 0;
+    w_width = DisplayWidth(dpy, 0);
+    w_height = DisplayHeight(dpy, 0);
+  }
+
   // Create a window
-  w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
-                          DisplayWidth(dpy, 0), DisplayHeight(dpy, 0),
+  w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), x, y, w_width, w_height,
                           0, blackColor, blackColor);
 
   // We want to get MapNotify events
@@ -872,6 +878,7 @@
 static const struct option long_options[] = {
   { "clickclear", optional_argument, NULL, 'c' },
   { "display", required_argument, NULL, 'd' },
+  { "geometry", optional_argument, NULL, 'g' },
   { "monotonic", optional_argument, NULL, 'm' },
   { "persist", optional_argument, NULL, 'p' },
   { 0, },
@@ -883,6 +890,8 @@
   printf("   %s /dev/input/eventX [--display DISPLAY] [--monotonic] [--persist]\n",
          program_invocation_short_name);
   printf("\n");
+  printf(" -g, --geometry[=wxh+x+y] : sets the window geometry\n");
+  printf("                            e.g., 1280x800+0+0\n");
   printf(" -m, --monotonic[=0|1]  =1: requests input events with monotonic timestamps\n"
          "                            if supported by the kernel (default)\n"
          "                        =0: request input events with wallclock timestamps\n");
@@ -900,6 +909,7 @@
   const char *device = NULL;
   char *displayname = NULL;
   char *filename;
+  char *geometry;
   struct input_event ev[64];
   int i, rd;
   struct mt_state mt_state;
@@ -907,7 +917,7 @@
   fd_set rdfs;
 
   while (1) {
-    int c = getopt_long_only(argc, argv, "c:d:m:p:", long_options, NULL);
+    int c = getopt_long_only(argc, argv, "c:d:g:m:p:", long_options, NULL);
     if (c == -1)
       break;
     switch (c) {
@@ -917,6 +927,9 @@
       case 'd':
         displayname = optarg;
         break;
+      case 'g':
+        geometry = optarg;
+        break;
       case 'm':
         monotonic = (optarg) ? atoi(optarg) : true;
         break;
@@ -984,7 +997,7 @@
     printf("***********************************************\n");
   }
 
-  dpy = InitDisplay(displayname);
+  dpy = InitDisplay(displayname, geometry);
   if (!dpy) {
     fprintf (stderr, "mtplot:  unable to open display '%s'\n",
              XDisplayName (displayname));