/src/wm-tester/main.c

https://github.com/AlexWillisson/metacity-physics · C · 245 lines · 172 code · 49 blank · 24 comment · 29 complexity · db2a0c6113fa02d8df3b7e216377881a MD5 · raw file

  1. /* WM tester main() */
  2. /*
  3. * Copyright (C) 2001 Havoc Pennington
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  18. * 02111-1307, USA.
  19. */
  20. #include <gtk/gtk.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. static void set_up_the_evil (void);
  27. static void set_up_icon_windows (void);
  28. static void
  29. usage (void)
  30. {
  31. g_print ("wm-tester [--evil] [--icon-windows]\n");
  32. exit (0);
  33. }
  34. int
  35. main (int argc, char **argv)
  36. {
  37. int i;
  38. gboolean do_evil;
  39. gboolean do_icon_windows;
  40. gtk_init (&argc, &argv);
  41. do_evil = FALSE;
  42. do_icon_windows = FALSE;
  43. i = 1;
  44. while (i < argc)
  45. {
  46. const char *arg = argv[i];
  47. if (strcmp (arg, "--help") == 0 ||
  48. strcmp (arg, "-h") == 0 ||
  49. strcmp (arg, "-?") == 0)
  50. usage ();
  51. else if (strcmp (arg, "--evil") == 0)
  52. do_evil = TRUE;
  53. else if (strcmp (arg, "--icon-windows") == 0)
  54. do_icon_windows = TRUE;
  55. else
  56. usage ();
  57. ++i;
  58. }
  59. /* Be sure some option was provided */
  60. if (! (do_evil || do_icon_windows))
  61. return 1;
  62. if (do_evil)
  63. set_up_the_evil ();
  64. if (do_icon_windows)
  65. set_up_icon_windows ();
  66. gtk_main ();
  67. return 0;
  68. }
  69. static GSList *evil_windows = NULL;
  70. static gint
  71. evil_timeout (gpointer data)
  72. {
  73. int i;
  74. int n_windows;
  75. int len;
  76. int create_count;
  77. int destroy_count;
  78. len = g_slist_length (evil_windows);
  79. if (len > 35)
  80. {
  81. create_count = 2;
  82. destroy_count = 5;
  83. }
  84. else
  85. {
  86. create_count = 5;
  87. destroy_count = 5;
  88. }
  89. /* Create some windows */
  90. n_windows = g_random_int_range (0, create_count);
  91. i = 0;
  92. while (i < n_windows)
  93. {
  94. GtkWidget *w;
  95. GtkWidget *c;
  96. int t;
  97. GtkWidget *parent;
  98. w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  99. gtk_widget_set_uposition (w,
  100. g_random_int_range (0,
  101. gdk_screen_width ()),
  102. g_random_int_range (0,
  103. gdk_screen_height ()));
  104. parent = NULL;
  105. /* set transient for random window (may create all kinds of weird cycles) */
  106. if (len > 0)
  107. {
  108. t = g_random_int_range (- (len / 3), len);
  109. if (t >= 0)
  110. {
  111. parent = g_slist_nth_data (evil_windows, t);
  112. if (parent != NULL)
  113. gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (parent));
  114. }
  115. }
  116. if (parent != NULL)
  117. c = gtk_button_new_with_label ("Evil Transient!");
  118. else
  119. c = gtk_button_new_with_label ("Evil Window!");
  120. gtk_container_add (GTK_CONTAINER (w), c);
  121. gtk_widget_show_all (w);
  122. evil_windows = g_slist_prepend (evil_windows, w);
  123. ++i;
  124. }
  125. /* Destroy some windows */
  126. if (len > destroy_count)
  127. {
  128. n_windows = g_random_int_range (0, destroy_count);
  129. i = 0;
  130. while (i < n_windows)
  131. {
  132. GtkWidget *w;
  133. w = g_slist_nth_data (evil_windows,
  134. g_random_int_range (0, len));
  135. if (w)
  136. {
  137. --len;
  138. evil_windows = g_slist_remove (evil_windows, w);
  139. gtk_widget_destroy (w);
  140. }
  141. ++i;
  142. }
  143. }
  144. return TRUE;
  145. }
  146. static void
  147. set_up_the_evil (void)
  148. {
  149. g_timeout_add (400, evil_timeout, NULL);
  150. }
  151. static void
  152. set_up_icon_windows (void)
  153. {
  154. int i;
  155. int n_windows;
  156. /* Create some windows */
  157. n_windows = 9;
  158. i = 0;
  159. while (i < n_windows)
  160. {
  161. GtkWidget *w;
  162. GtkWidget *c;
  163. GList *icons;
  164. GdkPixbuf *pix;
  165. w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  166. c = gtk_button_new_with_label ("Icon window");
  167. gtk_container_add (GTK_CONTAINER (w), c);
  168. icons = NULL;
  169. pix = gtk_widget_render_icon (w,
  170. GTK_STOCK_SAVE,
  171. GTK_ICON_SIZE_LARGE_TOOLBAR,
  172. NULL);
  173. icons = g_list_append (icons, pix);
  174. if (i % 2)
  175. {
  176. pix = gtk_widget_render_icon (w,
  177. GTK_STOCK_SAVE,
  178. GTK_ICON_SIZE_DIALOG,
  179. NULL);
  180. icons = g_list_append (icons, pix);
  181. }
  182. if (i % 3)
  183. {
  184. pix = gtk_widget_render_icon (w,
  185. GTK_STOCK_SAVE,
  186. GTK_ICON_SIZE_MENU,
  187. NULL);
  188. icons = g_list_append (icons, pix);
  189. }
  190. gtk_window_set_icon_list (GTK_WINDOW (w), icons);
  191. g_list_foreach (icons, (GFunc) g_object_unref, NULL);
  192. g_list_free (icons);
  193. gtk_widget_show_all (w);
  194. ++i;
  195. }
  196. }