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