summaryrefslogtreecommitdiff
path: root/engines/balou/balou.c
blob: 752021b94d502fb97a2565ece5f362e9c7fa249b (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* $Id$ */
/*-
 * Copyright (c) 2003-2004 Benedikt Meurer <benny@xfce.org>
 * All rights reserved.
 *
 * 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, 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <X11/Xlib.h>

#include <engines/balou/balou.h>


#define BALOU_INCREMENT   2




static void            balou_window_init     (BalouWindow  *window,
                                              GdkScreen    *screen,
                                              int           monitor,
                                              GdkWindow    *root,
                                              GdkCursor    *cursor);
static void            balou_window_destroy  (BalouWindow  *window);
#if 0
static void            balou_window_set_text (BalouWindow  *window,
                                              const gchar  *text);
#endif
static GdkFilterReturn balou_window_filter   (GdkXEvent    *xevent,
                                              GdkEvent     *event,
                                              gpointer      data);


struct _BalouWindow
{
  GdkWindow   *window;
  GdkPixmap   *backbuf;
  PangoLayout *layout;
  GdkGC       *gc_copy;
  GdkGC       *gc_set;
  GdkRectangle area;
  GdkRectangle logobox;
  GdkRectangle textbox;

  GtkWidget   *wmwindow;

  gboolean     dialog_active;
};



void
balou_init (Balou        *balou,
            GdkDisplay   *display,
            GdkScreen    *mainscreen,
            gint          mainmonitor,
            BalouTheme   *theme)
{
  PangoFontDescription *description;
  PangoFontMetrics     *metrics;
  PangoContext         *context;
  PangoLayout          *layout;
  BalouWindow          *window;
  GdkColormap          *cmap;
  GdkCursor            *cursor;
  GdkScreen            *screen;
  GdkWindow            *root;
  GdkPixbuf            *pb;
  GdkGCValues           gc_values;
  GdkGCValuesMask       gc_mask;
  GdkGC                *gc_copy;
  GdkGC                *gc_set;
  gint                  layout_height;
  gint                  nmonitors;
  gint                  nscreens;
  gint                  i;
  gint                  n;
  gint                  m;
  gint                  px;
  gint                  py;
  gint                  pw;
  gint                  ph;

  balou->theme = theme;

  balou_theme_get_bgcolor (theme, &balou->bgcolor);
  balou_theme_get_fgcolor (theme, &balou->fgcolor);

  cursor = gdk_cursor_new (GDK_WATCH);
  description = pango_font_description_from_string (balou_theme_get_font (theme));

  /* determine number of required windows */
  nscreens = gdk_display_get_n_screens (display);
  for (n = 0; n < nscreens; ++n)
    {
      screen = gdk_display_get_screen (display, n);
      nmonitors = gdk_screen_get_n_monitors (screen);
      for (m = 0; m < nmonitors; ++m)
        balou->nwindows++;
    }

  /* create windows */
  balou->windows = g_new (BalouWindow, balou->nwindows);
  for (i = n = 0; n < nscreens; ++n)
    {
      screen = gdk_display_get_screen (display, n);
      nmonitors = gdk_screen_get_n_monitors (screen);
      root = gdk_screen_get_root_window (screen);

      /* allocate fore/background colors */
      cmap = gdk_drawable_get_colormap (root);
      gdk_rgb_find_color (cmap, &balou->bgcolor);
      gdk_rgb_find_color (cmap, &balou->fgcolor);

      /* create pango layout for this screen */
      context = gdk_pango_context_get_for_screen (screen);
      pango_context_set_font_description (context, description);
      layout = pango_layout_new (context);
      metrics = pango_context_get_metrics (context, description, NULL);
      layout_height = (pango_font_metrics_get_ascent (metrics)
                      + pango_font_metrics_get_descent (metrics)) / PANGO_SCALE
                     + 3;
      pango_font_metrics_unref (metrics);

      /* create graphics contexts for this screen */
      gc_mask = GDK_GC_FUNCTION | GDK_GC_EXPOSURES;
      gc_values.function = GDK_COPY;
      gc_values.graphics_exposures = FALSE;
      gc_copy = gdk_gc_new_with_values (root, &gc_values, gc_mask);
      gc_mask |= GDK_GC_FOREGROUND | GDK_GC_BACKGROUND;
      gc_values.foreground = balou->bgcolor;
      gc_values.background = balou->fgcolor;
      gc_set = gdk_gc_new_with_values (root, &gc_values, gc_mask);

      for (m = 0; m < nmonitors; ++m)
        {
          window = balou->windows + i;
          balou_window_init (window, screen, m, root, cursor);

          window->gc_copy = GDK_GC (g_object_ref (gc_copy));
          window->gc_set = GDK_GC (g_object_ref (gc_set));
          window->layout = PANGO_LAYOUT (g_object_ref (layout));

          /* calculate box dimensions */
          window->logobox         = window->area;
          window->logobox.x       = 0;
          window->logobox.height -= layout_height;
          window->textbox         = window->area;
          window->textbox.x       = 0;
          window->textbox.y      += window->logobox.height;
          window->textbox.height -= window->logobox.height;

          balou_theme_draw_gradient (balou->theme,
                                     window->backbuf,
                                     gc_copy,
                                     window->logobox,
                                     window->textbox);

          gdk_gc_set_rgb_fg_color (gc_copy, &balou->fgcolor);

          if (mainscreen == screen && mainmonitor == m)
            balou->mainwin = window;

          ++i;
        }

      g_object_unref (context);
      g_object_unref (layout);
      g_object_unref (gc_copy);
      g_object_unref (gc_set);
    }

  /* show splash windows */
  for (i = 0; i < balou->nwindows; ++i)
    {
      window = balou->windows + i;

      gtk_widget_show_now (window->wmwindow);
      /*gdk_window_set_back_pixmap (window->wmwindow->window,
                                  window->backbuf, FALSE);*/
      gdk_window_add_filter (window->wmwindow->window,
                             balou_window_filter,
                             window);

      gdk_window_show (window->window);
      gdk_window_add_filter (window->window,
                             balou_window_filter,
                             window);
    }
  gdk_flush ();

  /* display logo pixbuf (if any) */
  window = balou->mainwin;
  pb = balou_theme_get_logo (balou->theme,
                             window->logobox.width,
                             window->logobox.height);
  if (G_LIKELY (pb != NULL))
    {
      pw = gdk_pixbuf_get_width (pb);
      ph = gdk_pixbuf_get_height (pb);
      px = (window->logobox.width - pw) / 2;
      py = (window->logobox.height - ph) / 2;

      gdk_draw_pixbuf (window->backbuf, window->gc_copy, pb, 0, 0,
                       px, py, pw, ph, GDK_RGB_DITHER_NONE, 0, 0);
      gdk_window_clear_area (window->window, px, py, pw, ph);
      g_object_unref (pb);
    }

  /* create fader pixmap */
  balou->fader_pm = gdk_pixmap_new (window->window,
                                    window->textbox.width,
                                    window->textbox.height,
                                    -1);
  gdk_draw_rectangle (balou->fader_pm, window->gc_set, TRUE, 0, 0,
                      window->textbox.width, window->textbox.height);

  pango_font_description_free (description);
  gdk_cursor_unref (cursor);
}


void
balou_fadein (Balou *balou, const gchar *text)
{
  BalouWindow *window = balou->mainwin;
  GdkRectangle area;
  gint         median;
  gint         th;
  gint         tw;
  gint         x;

  pango_layout_set_text (window->layout, text, -1);
  pango_layout_get_pixel_size (window->layout, &tw, &th);

  area.x      = window->textbox.x + BALOU_INCREMENT;
  area.y      = window->textbox.y + (window->textbox.height - th) / 2;
  area.width  = tw + BALOU_INCREMENT;
  area.height = th;

  gdk_draw_rectangle (balou->fader_pm, window->gc_set, TRUE, 0, 0,
                      window->textbox.width, window->textbox.height);
  gdk_draw_layout (balou->fader_pm, window->gc_copy,
                   BALOU_INCREMENT, 0, window->layout);

  median = (window->area.width - area.width) / 2;
  for (x = 0; (median - x) > BALOU_INCREMENT; x += BALOU_INCREMENT)
    {
      gdk_draw_drawable (window->window, window->gc_copy, balou->fader_pm,
                         0, 0, area.x + x, area.y, area.width, area.height);

      gdk_flush ();

      g_main_context_iteration (NULL, FALSE);
    }

  area.x += median;
  balou->fader_area = area;

  gdk_draw_rectangle (window->backbuf,
                      window->gc_set, TRUE,
                      window->textbox.x,
                      window->textbox.y,
                      window->textbox.width,
                      window->textbox.height);

  gdk_draw_drawable (window->backbuf, window->gc_copy, balou->fader_pm,
                     0, 0, area.x, area.y, area.width, area.height);

  gdk_window_clear_area (window->window,
                         window->textbox.x,
                         window->textbox.y,
                         window->textbox.width,
                         window->textbox.height);
}


void
balou_fadeout (Balou *balou)
{
  BalouWindow *window = balou->mainwin;
  GdkRectangle area = balou->fader_area;
  gint         left;

  left = (window->textbox.x + window->textbox.width) - BALOU_INCREMENT;
  for (; area.x < left; area.x += BALOU_INCREMENT)
    {
      gdk_draw_drawable (window->window, window->gc_copy, balou->fader_pm,
                         0, 0, area.x, area.y, area.width, area.height);

      gdk_flush ();

      g_main_context_iteration (NULL, FALSE);
    }

  gdk_draw_rectangle (window->backbuf,
                      window->gc_set, TRUE,
                      window->textbox.x,
                      window->textbox.y,
                      window->textbox.width,
                      window->textbox.height);

  gdk_window_clear_area (window->window,
                         window->textbox.x,
                         window->textbox.y,
                         window->textbox.width,
                         window->textbox.height);
}


int
balou_run (Balou     *balou,
           GtkWidget *dialog)
{
  GtkRequisition requisition;
  BalouWindow   *window = balou->mainwin;
  int            result;
  int            x;
  int            y;

  window->dialog_active = TRUE;

  gtk_widget_size_request (dialog, &requisition);
  x = window->area.x + (window->area.width - requisition.width) / 2;
  y = window->area.y + (window->area.height - requisition.height) / 2;
  gtk_window_move (GTK_WINDOW (dialog), x, y);
  result = gtk_dialog_run (GTK_DIALOG (dialog));

  window->dialog_active = FALSE;

  return result;
}


void
balou_destroy (Balou *balou)
{
  gint i;

  balou_theme_destroy (balou->theme);

  for (i = 0; i < balou->nwindows; ++i)
    balou_window_destroy (balou->windows + i);
  g_free (balou->windows);

  if (balou->fader_pm != NULL)
    g_object_unref (balou->fader_pm);
}


static void
balou_window_init (BalouWindow  *window,
                   GdkScreen    *screen,
                   int           monitor,
                   GdkWindow    *root,
                   GdkCursor    *cursor)
{
  GdkWindowAttr attr;

  /* acquire monitor geometry */
  gdk_screen_get_monitor_geometry (screen, monitor, &window->area);

  /* create splash window */
  attr.x                  = window->area.x;
  attr.y                  = window->area.y;
  attr.event_mask         = GDK_VISIBILITY_NOTIFY_MASK;
  attr.width              = window->area.width;
  attr.height             = window->area.height;
  attr.wclass             = GDK_INPUT_OUTPUT;
  attr.window_type        = GDK_WINDOW_TEMP;
  attr.cursor             = cursor;
  attr.override_redirect  = TRUE;

  window->window = gdk_window_new (root, &attr, GDK_WA_X | GDK_WA_Y
                                  | GDK_WA_CURSOR | GDK_WA_NOREDIR);

  /* create wm window (for tricking the window manager to avoid flicker) */
  window->wmwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_move (GTK_WINDOW (window->wmwindow), 0, 0);
  gtk_window_resize (GTK_WINDOW (window->wmwindow), 1, 1);
  gtk_window_set_decorated (GTK_WINDOW (window->wmwindow), FALSE);
  gtk_window_set_screen (GTK_WINDOW (window->wmwindow), screen);
  gtk_window_set_skip_pager_hint (GTK_WINDOW (window->wmwindow), TRUE);
  gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window->wmwindow), TRUE);

  window->backbuf = gdk_pixmap_new (window->window,
                                    window->area.width,
                                    window->area.height,
                                    -1);

  gdk_window_set_back_pixmap (window->window, window->backbuf, FALSE);
}


static void
balou_window_destroy (BalouWindow *window)
{
  gdk_window_remove_filter (window->window, balou_window_filter, window);
  if (GTK_WIDGET_REALIZED (window->wmwindow))
    {
      gdk_window_remove_filter (window->wmwindow->window,
                                balou_window_filter,
                                window);
    }

  gdk_window_destroy (window->window);
  gtk_widget_destroy (window->wmwindow);
  g_object_unref (window->backbuf);
  g_object_unref (window->layout);
  g_object_unref (window->gc_copy);
  g_object_unref (window->gc_set);
}


#if 0
static void
balou_window_set_text (BalouWindow *window, const gchar *text)
{
  gint text_x;
  gint text_y;
  gint text_width;
  gint text_height;

  pango_layout_set_markup (window->layout, text, -1);
  pango_layout_get_pixel_size (window->layout, &text_width, &text_height);

  text_x = (window->textbox.width - text_width) / 2
          + window->textbox.x;
  text_y = (window->textbox.height - text_height) / 2
          + window->textbox.y;

  gdk_draw_rectangle (window->backbuf,
                      window->gc_set,
                      TRUE,
                      window->textbox.x,
                      window->textbox.y,
                      window->textbox.width,
                      window->textbox.height);

  gdk_draw_layout (window->backbuf, window->gc_copy,
                   text_x, text_y, window->layout);

  gdk_window_clear_area (window->window,
                         window->textbox.x,
                         window->textbox.y,
                         window->textbox.width,
                         window->textbox.height);
}
#endif


static GdkFilterReturn
balou_window_filter (GdkXEvent *xevent,
                     GdkEvent  *event,
                     gpointer   data)
{
  XVisibilityEvent *xvisev = (XVisibilityEvent *) xevent;
  BalouWindow      *window = (BalouWindow *) data;

  if (!window->dialog_active)
    {
      switch (xvisev->type)
        {
        case VisibilityNotify:
          /* something obscured the splash window */
          gdk_window_raise (window->window);
          return GDK_FILTER_REMOVE;

        case ReparentNotify:
          /* window manager is up */
          gdk_window_raise (window->window);
          break;
        }
    }

  return GDK_FILTER_CONTINUE;
}