summaryrefslogtreecommitdiff
path: root/src/wm-tester/main.c
blob: f58742d640ce831826d6f9b741ed0a16c3a3f76a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* WM tester main() */

/* 
 * Copyright (C) 2001 Havoc Pennington
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <gtk/gtk.h>

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static void set_up_the_evil (void);
static void set_up_icon_windows (void);

static void
usage (void)
{
  g_print ("wm-tester [--evil] [--icon-windows]\n");
  exit (0);
}

int
main (int argc, char **argv)
{
  int i;
  gboolean do_evil;
  gboolean do_icon_windows;
  
  gtk_init (&argc, &argv);
  
  do_evil = FALSE;
  do_icon_windows = FALSE;
  
  i = 1;
  while (i < argc)
    {
      const char *arg = argv[i];
      
      if (strcmp (arg, "--help") == 0 ||
          strcmp (arg, "-h") == 0 ||
          strcmp (arg, "-?") == 0)
        usage ();
      else if (strcmp (arg, "--evil") == 0)
        do_evil = TRUE;
      else if (strcmp (arg, "--icon-windows") == 0)
        do_icon_windows = TRUE;
      else
        usage ();
      
      ++i;
    }

  /* Be sure some option was provided */
  if (! (do_evil || do_icon_windows))
    return 1;
  
  if (do_evil)
    set_up_the_evil ();

  if (do_icon_windows)
    set_up_icon_windows ();
  
  gtk_main ();

  return 0;
}

static GSList *evil_windows = NULL;

static gint
evil_timeout (gpointer data)
{
  int i;
  int n_windows;
  int len;
  int create_count;
  int destroy_count;

  len = g_slist_length (evil_windows);  
  
  if (len > 35)
    {
      create_count = 2;
      destroy_count = 5;
    }
  else
    {
      create_count = 5;
      destroy_count = 5;
    }

  /* Create some windows */
  n_windows = g_random_int_range (0, create_count);
  
  i = 0;
  while (i < n_windows)
    {
      GtkWidget *w;
      GtkWidget *c;
      
      w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      c = gtk_button_new_with_label ("Evil Window!");
      gtk_container_add (GTK_CONTAINER (w), c);

      gtk_widget_set_uposition (w,
                                g_random_int_range (0,
                                                    gdk_screen_width ()),
                                g_random_int_range (0,
                                                    gdk_screen_height ()));
      
      gtk_widget_show_all (w);
      
      evil_windows = g_slist_prepend (evil_windows, w);
      
      ++i;
    }

  /* Destroy some windows */
  if (len > destroy_count)
    {
      n_windows = g_random_int_range (0, destroy_count);
      i = 0;
      while (i < n_windows)
        {
          GtkWidget *w;
          
          w = g_slist_nth_data (evil_windows,
                                g_random_int_range (0, len));
          if (w)
            {
              --len;
              evil_windows = g_slist_remove (evil_windows, w);
              gtk_widget_destroy (w);
            }
          
          ++i;
        }
    }
  
  return TRUE;
}

static void
set_up_the_evil (void)
{
  g_timeout_add (40, evil_timeout, NULL);
}

static void
set_up_icon_windows (void)
{
  int i;
  int n_windows;

  /* Create some windows */
  n_windows = 9;
  
  i = 0;
  while (i < n_windows)
    {
      GtkWidget *w;
      GtkWidget *c;
      GList *icons;
      GdkPixbuf *pix;
      
      w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      c = gtk_button_new_with_label ("Icon window");
      gtk_container_add (GTK_CONTAINER (w), c);

      gtk_widget_realize (w);

      icons = NULL;

      pix = gtk_widget_render_icon (w,
                                    GTK_STOCK_SAVE,
                                    GTK_ICON_SIZE_LARGE_TOOLBAR,
                                    NULL);
      
      icons = g_list_append (icons, pix);

      if (i % 2)
        {
          pix = gtk_widget_render_icon (w,
                                        GTK_STOCK_SAVE,
                                        GTK_ICON_SIZE_DIALOG,
                                        NULL);
          icons = g_list_append (icons, pix);
        }

      if (i % 3)
        {
          pix = gtk_widget_render_icon (w,
                                        GTK_STOCK_SAVE,
                                        GTK_ICON_SIZE_MENU,
                                        NULL);
          icons = g_list_append (icons, pix);
        }
      
      if (!gdk_window_set_icon_list (w->window, icons))
        g_warning ("_NET_WM_ICON not supported?");

      g_list_foreach (icons, (GFunc) g_object_unref, NULL);
      g_list_free (icons);
      
      gtk_widget_show_all (w);
      
      ++i;
    }
}