~unity-team/compiz/plugins-main-trunk.fix_wrong_window_move_expo

« back to all changes in this revision

Viewing changes to mag/src/mag.cpp

  • Committer: David Barth
  • Date: 2011-03-29 16:36:40 UTC
  • Revision ID: david.barth@canonical.com-20110329163640-fpen5qsoo0lbjode
initial import from compiz-plugins-main_0.9.4git20110322.orig.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Compiz magnifier plugin
 
4
 *
 
5
 * mag.c
 
6
 *
 
7
 * Copyright : (C) 2008 by Dennis Kasprzyk
 
8
 * E-mail    : onestone@opencompositing.org
 
9
 *
 
10
 *
 
11
 * This program is free software; you can redistribute it and/or
 
12
 * modify it under the terms of the GNU General Public License
 
13
 * as published by the Free Software Foundation; either version 2
 
14
 * of the License, or (at your option) any later version.
 
15
 *
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 */
 
22
 
 
23
#include "mag.h"
 
24
 
 
25
COMPIZ_PLUGIN_20090315 (mag, MagPluginVTable);
 
26
 
 
27
void
 
28
MagScreen::cleanup ()
 
29
{
 
30
    if (overlay.size ())
 
31
    {
 
32
        overlay.clear ();
 
33
    }
 
34
    if (mask.size ())
 
35
    {
 
36
        mask.clear ();
 
37
    }
 
38
 
 
39
    if (program)
 
40
    {
 
41
        GL::deletePrograms (1, &program);
 
42
        program = 0;
 
43
    }
 
44
}
 
45
 
 
46
bool
 
47
MagScreen::loadFragmentProgram ()
 
48
{
 
49
    char  buffer[1024];
 
50
    GLsizei bufSize;
 
51
    GLint errorPos;
 
52
 
 
53
    if (!GL::fragmentProgram)
 
54
        return false;
 
55
 
 
56
    if (target == GL_TEXTURE_2D)
 
57
        sprintf (buffer, fisheyeFpString, "2D");
 
58
    else
 
59
        sprintf (buffer, fisheyeFpString, "RECT");
 
60
 
 
61
    /* clear errors */
 
62
    glGetError ();
 
63
 
 
64
    if (!program)
 
65
        GL::genPrograms (1, &program);
 
66
 
 
67
    bufSize = (GLsizei) strlen (buffer);
 
68
 
 
69
    GL::bindProgram (GL_FRAGMENT_PROGRAM_ARB, program);
 
70
    GL::programString (GL_FRAGMENT_PROGRAM_ARB,
 
71
                         GL_PROGRAM_FORMAT_ASCII_ARB,
 
72
                         bufSize, buffer);
 
73
 
 
74
    glGetIntegerv (GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
 
75
    if (glGetError () != GL_NO_ERROR || errorPos != -1)
 
76
    {
 
77
        compLogMessage ("mag", CompLogLevelError,
 
78
                        "failed to load fisheye fragment program");
 
79
 
 
80
        GL::deletePrograms (1, &program);
 
81
        program = 0;
 
82
 
 
83
        return false;
 
84
    }
 
85
    GL::bindProgram (GL_FRAGMENT_PROGRAM_ARB, 0);
 
86
 
 
87
    return true;
 
88
}
 
89
 
 
90
bool
 
91
MagScreen::loadImages ()
 
92
{
 
93
    CompString overlay_s = optionGetOverlay ();
 
94
    CompString mask_s = optionGetMask ();
 
95
    CompString pname ("mag");
 
96
    if (!GL::multiTexCoord2f)
 
97
        return false;
 
98
 
 
99
    overlay = GLTexture::readImageToTexture (overlay_s, pname,
 
100
                                               overlaySize);
 
101
    
 
102
    if (!overlay.size ())
 
103
    {
 
104
        compLogMessage ("mag", CompLogLevelWarn,
 
105
                        "Could not load magnifier overlay image \"%s\"!",
 
106
                        overlay_s.c_str ());
 
107
        return false;
 
108
    }
 
109
 
 
110
    mask = GLTexture::readImageToTexture (mask_s, pname,
 
111
                                          maskSize);
 
112
 
 
113
    if (!mask.size ())
 
114
    {
 
115
        compLogMessage ("mag", CompLogLevelWarn,
 
116
                        "Could not load magnifier mask image \"%s\"!",
 
117
                        mask_s.c_str ());
 
118
        overlay.clear ();
 
119
        return false;
 
120
    }
 
121
 
 
122
    if (overlaySize.width () != maskSize.width () ||
 
123
        overlaySize.height () != maskSize.height ())
 
124
    {
 
125
        compLogMessage ("mag", CompLogLevelWarn,
 
126
                        "Image dimensions do not match!");
 
127
        overlay.clear ();
 
128
        mask.clear ();
 
129
        return false;
 
130
    }
 
131
 
 
132
    return true;
 
133
}
 
134
 
 
135
void
 
136
MagScreen::optionChanged (CompOption          *opt,
 
137
                          MagOptions::Options num)
 
138
{
 
139
    cleanup ();
 
140
 
 
141
    switch (optionGetMode ())
 
142
    {
 
143
    case ModeImageOverlay:
 
144
        if (loadImages ())
 
145
            mode = MagOptions::ModeImageOverlay;
 
146
        else
 
147
            mode = MagOptions::ModeSimple;
 
148
        break;
 
149
    case MagOptions::ModeFisheye:
 
150
        if (loadFragmentProgram ())
 
151
            mode = MagOptions::ModeFisheye;
 
152
        else
 
153
            mode = MagOptions::ModeSimple;
 
154
        break;
 
155
    default:
 
156
        mode = MagOptions::ModeSimple;
 
157
    }
 
158
    
 
159
    if (zoom != 1.0)
 
160
        cScreen->damageScreen ();
 
161
}
 
162
 
 
163
void
 
164
MagScreen::damageRegion ()
 
165
{
 
166
    CompRegion region;
 
167
 
 
168
    int  w, h, x, y;
 
169
 
 
170
    switch (mode)
 
171
    {
 
172
    case MagOptions::ModeSimple:
 
173
        {
 
174
            int b;
 
175
            w = optionGetBoxWidth ();
 
176
            h = optionGetBoxHeight ();
 
177
            b = optionGetBorder ();
 
178
            w += 2 * b;
 
179
            h += 2 * b;
 
180
            x = MAX (0, MIN (posX - (w / 2), screen->width () - w));
 
181
            y = MAX (0, MIN (posY - (h / 2), screen->height () - h));
 
182
 
 
183
            CompRegion tmpRegion (x, y, w, h);
 
184
 
 
185
            region = tmpRegion;
 
186
        }
 
187
        break;
 
188
    case MagOptions::ModeImageOverlay:
 
189
        {
 
190
            x = posX - optionGetXOffset ();
 
191
            y = posY - optionGetYOffset ();
 
192
            w = overlaySize.width ();
 
193
            h = overlaySize.height ();
 
194
 
 
195
            CompRegion tmpRegion (x, y, w, h);
 
196
 
 
197
            region = tmpRegion;
 
198
        }
 
199
        break;
 
200
    case MagOptions::ModeFisheye:
 
201
        {
 
202
            int radius = optionGetRadius ();
 
203
            int x2, y2;
 
204
 
 
205
            x = MAX (0.0, posX - radius);
 
206
            y = MAX (0.0, posY - radius);
 
207
            x2 = MIN (screen->width (), posX + radius);
 
208
            y2 = MIN (screen->height (), posY + radius);
 
209
            w = x2 - x;
 
210
            h = y2 - y;
 
211
 
 
212
            CompRegion tmpRegion (x, y, w, h);
 
213
 
 
214
            region = tmpRegion;
 
215
        }
 
216
        break;
 
217
    default:
 
218
        break;
 
219
    }
 
220
 
 
221
    cScreen->damageRegion (region);
 
222
}
 
223
 
 
224
void
 
225
MagScreen::positionUpdate (const CompPoint &pos)
 
226
{
 
227
    damageRegion ();
 
228
 
 
229
    posX = pos.x ();
 
230
    posY = pos.y ();
 
231
 
 
232
    damageRegion ();
 
233
}
 
234
 
 
235
int
 
236
MagScreen::adjustZoom (float chunk)
 
237
{
 
238
    float dx, adjust, amount;
 
239
    float change;
 
240
 
 
241
    dx = zTarget - zoom;
 
242
 
 
243
    adjust = dx * 0.15f;
 
244
    amount = fabs(dx) * 1.5f;
 
245
    if (amount < 0.2f)
 
246
        amount = 0.2f;
 
247
    else if (amount > 2.0f)
 
248
        amount = 2.0f;
 
249
 
 
250
    zVelocity = (amount * zVelocity + adjust) / (amount + 1.0f);
 
251
 
 
252
    if (fabs (dx) < 0.002f && fabs (zVelocity) < 0.004f)
 
253
    {
 
254
        zVelocity = 0.0f;
 
255
        zoom = zTarget;
 
256
        return false;
 
257
    }
 
258
 
 
259
    change = zVelocity * chunk;
 
260
    if (!change)
 
261
    {
 
262
        if (zVelocity)
 
263
            change = (dx > 0) ? 0.01 : -0.01;
 
264
    }
 
265
 
 
266
    zoom += change;
 
267
 
 
268
    return true;
 
269
}
 
270
 
 
271
void
 
272
MagScreen::preparePaint (int        time)
 
273
{
 
274
    if (adjust)
 
275
    {
 
276
        int   steps;
 
277
        float amount, chunk;
 
278
 
 
279
        amount = time * 0.35f * optionGetSpeed ();
 
280
        steps  = amount / (0.5f * optionGetTimestep ());
 
281
 
 
282
        if (!steps)
 
283
            steps = 1;
 
284
 
 
285
        chunk  = amount / (float) steps;
 
286
 
 
287
        while (steps--)
 
288
        {
 
289
            adjust = adjustZoom (chunk);
 
290
            if (adjust)
 
291
                break;
 
292
        }
 
293
    }
 
294
 
 
295
    if (zoom != 1.0)
 
296
    {
 
297
        if (!poller.active ())
 
298
        {
 
299
            CompPoint pos;
 
300
            pos = poller.getCurrentPosition ();
 
301
            posX = pos.x ();
 
302
            posY = pos.y ();
 
303
            poller.start ();
 
304
        }
 
305
        damageRegion ();
 
306
    }
 
307
 
 
308
    cScreen->preparePaint (time);
 
309
}
 
310
 
 
311
void
 
312
MagScreen::donePaint ()
 
313
{
 
314
 
 
315
    if (adjust)
 
316
        damageRegion ();
 
317
 
 
318
    if (!adjust && zoom == 1.0 && (width || height))
 
319
    {
 
320
        glEnable (target);
 
321
 
 
322
        glBindTexture (target, texture);
 
323
 
 
324
        glTexImage2D (target, 0, GL_RGB, 0, 0, 0,
 
325
                      GL_RGB, GL_UNSIGNED_BYTE, NULL);
 
326
 
 
327
        width = 0;
 
328
        height = 0;
 
329
        
 
330
        glBindTexture (target, 0);
 
331
 
 
332
        glDisable (target);
 
333
    }
 
334
 
 
335
    if (zoom == 1.0 && !adjust)
 
336
    {
 
337
        // Mag mode has ended
 
338
        cScreen->preparePaintSetEnabled (this, false);
 
339
        cScreen->donePaintSetEnabled (this, false);
 
340
        gScreen->glPaintOutputSetEnabled (this, false);
 
341
 
 
342
        if (poller.active ())
 
343
            poller.stop ();
 
344
    }
 
345
 
 
346
    cScreen->donePaint ();
 
347
}
 
348
 
 
349
void
 
350
MagScreen::paintSimple ()
 
351
{
 
352
    float          pw, ph, bw, bh;
 
353
    int            x1, x2, y1, y2;
 
354
    float          vc[4];
 
355
    float          tc[4];
 
356
    int            w, h, cw, ch, cx, cy;
 
357
    bool           kScreen;
 
358
    unsigned short *color;
 
359
    float          tmp;
 
360
 
 
361
    w = optionGetBoxWidth ();
 
362
    h = optionGetBoxHeight ();
 
363
 
 
364
    kScreen = optionGetKeepScreen ();
 
365
 
 
366
    x1 = posX - (w / 2);
 
367
    if (kScreen)
 
368
        x1 = MAX (0, MIN (x1, screen->width () - w));
 
369
    x2 = x1 + w;
 
370
    y1 = posY - (h / 2);
 
371
    if (kScreen)
 
372
        y1 = MAX (0, MIN (y1, screen->height () - h));
 
373
    y2 = y1 + h;
 
374
 
 
375
    cw = ceil ((float)w / (zoom * 2.0)) * 2.0;
 
376
    ch = ceil ((float)h / (zoom * 2.0)) * 2.0;
 
377
    cw = MIN (w, cw + 2);
 
378
    ch = MIN (h, ch + 2);
 
379
    cx = (w - cw) / 2;
 
380
    cy = (h - ch) / 2;
 
381
 
 
382
    cx = MAX (0, MIN (w - cw, cx));
 
383
    cy = MAX (0, MIN (h - ch, cy));
 
384
 
 
385
    if (x1 != (posX - (w / 2)))
 
386
    {
 
387
        cx = 0;
 
388
        cw = w;
 
389
    }
 
390
    if (y1 != (posY - (h / 2)))
 
391
    {
 
392
        cy = 0;
 
393
        ch = h;
 
394
    }
 
395
 
 
396
    glEnable (target);
 
397
 
 
398
    glBindTexture (target, texture);
 
399
 
 
400
    if (width != w || height != h)
 
401
    {
 
402
        glCopyTexImage2D(target, 0, GL_RGB, x1, screen->height () - y2,
 
403
                         w, h, 0);
 
404
        width = w;
 
405
        height = h;
 
406
    }
 
407
    else
 
408
        glCopyTexSubImage2D (target, 0, cx, cy,
 
409
                             x1 + cx, screen->height () - y2 + cy, cw, ch);
 
410
 
 
411
    if (target == GL_TEXTURE_2D)
 
412
    {
 
413
        pw = 1.0 / width;
 
414
        ph = 1.0 / height;
 
415
    }
 
416
    else
 
417
    {
 
418
        pw = 1.0;
 
419
        ph = 1.0;
 
420
    }
 
421
 
 
422
    glMatrixMode (GL_PROJECTION);
 
423
    glPushMatrix ();
 
424
    glLoadIdentity ();
 
425
    glMatrixMode (GL_MODELVIEW);
 
426
    glPushMatrix ();
 
427
    glLoadIdentity ();
 
428
 
 
429
    vc[0] = ((x1 * 2.0) / screen->width ()) - 1.0;
 
430
    vc[1] = ((x2 * 2.0) / screen->width ()) - 1.0;
 
431
    vc[2] = ((y1 * -2.0) / screen->height ()) + 1.0;
 
432
    vc[3] = ((y2 * -2.0) / screen->height ()) + 1.0;
 
433
 
 
434
    tc[0] = 0.0;
 
435
    tc[1] = w * pw;
 
436
    tc[2] = h * ph;
 
437
    tc[3] = 0.0;
 
438
 
 
439
    glColor4usv (defaultColor);
 
440
 
 
441
    glPushMatrix ();
 
442
 
 
443
    glTranslatef ((float)(posX - (screen->width () / 2)) * 2 / screen->width (),
 
444
                  (float)(posY - (screen->height () / 2)) * 2 / -screen->height (), 0.0);
 
445
 
 
446
    glScalef (zoom, zoom, 1.0);
 
447
 
 
448
    glTranslatef ((float)((screen->width () / 2) - posX) * 2 / screen->width (),
 
449
                  (float)((screen->height () / 2) - posY) * 2 / -screen->height (), 0.0);
 
450
 
 
451
    glScissor (x1, screen->height () - y2, w, h);
 
452
 
 
453
    glEnable (GL_SCISSOR_TEST);
 
454
 
 
455
    glBegin (GL_QUADS);
 
456
    glTexCoord2f (tc[0], tc[2]);
 
457
    glVertex2f (vc[0], vc[2]);
 
458
    glTexCoord2f (tc[0], tc[3]);
 
459
    glVertex2f (vc[0], vc[3]);
 
460
    glTexCoord2f (tc[1], tc[3]);
 
461
    glVertex2f (vc[1], vc[3]);
 
462
    glTexCoord2f (tc[1], tc[2]);
 
463
    glVertex2f (vc[1], vc[2]);
 
464
    glEnd ();
 
465
 
 
466
    glDisable (GL_SCISSOR_TEST);
 
467
 
 
468
    glPopMatrix ();
 
469
 
 
470
    glBindTexture (target, 0);
 
471
 
 
472
    glDisable (target);
 
473
 
 
474
    glEnable (GL_BLEND);
 
475
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
476
 
 
477
    tmp = MIN (1.0, (zoom - 1) * 3.0);
 
478
 
 
479
    bw = bh = optionGetBorder ();
 
480
 
 
481
    bw = bw * 2.0 / screen->width ();
 
482
    bh = bh * 2.0 / screen->height ();
 
483
 
 
484
    bw = bh = optionGetBorder ();
 
485
 
 
486
    bw *= 2.0 / (float)screen->width ();
 
487
    bh *= 2.0 / (float)screen->height ();
 
488
 
 
489
    color = optionGetBoxColor ();
 
490
 
 
491
    glColor4us (color[0], color[1], color[2], color[3] * tmp);
 
492
 
 
493
    glBegin (GL_QUADS);
 
494
    glVertex2f (vc[0] - bw, vc[2] + bh);
 
495
    glVertex2f (vc[0] - bw, vc[2]);
 
496
    glVertex2f (vc[1] + bw, vc[2]);
 
497
    glVertex2f (vc[1] + bw, vc[2] + bh);
 
498
    glVertex2f (vc[0] - bw, vc[3]);
 
499
    glVertex2f (vc[0] - bw, vc[3] - bh);
 
500
    glVertex2f (vc[1] + bw, vc[3] - bh);
 
501
    glVertex2f (vc[1] + bw, vc[3]);
 
502
    glVertex2f (vc[0] - bw, vc[2]);
 
503
    glVertex2f (vc[0] - bw, vc[3]);
 
504
    glVertex2f (vc[0], vc[3]);
 
505
    glVertex2f (vc[0], vc[2]);
 
506
    glVertex2f (vc[1], vc[2]);
 
507
    glVertex2f (vc[1], vc[3]);
 
508
    glVertex2f (vc[1] + bw, vc[3]);
 
509
    glVertex2f (vc[1] + bw, vc[2]);
 
510
    glEnd();
 
511
 
 
512
    glColor4usv (defaultColor);
 
513
    glDisable (GL_BLEND);
 
514
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
515
 
 
516
    glPopMatrix();
 
517
    glMatrixMode (GL_PROJECTION);
 
518
    glPopMatrix ();
 
519
    glMatrixMode (GL_MODELVIEW);
 
520
}
 
521
 
 
522
void
 
523
MagScreen::paintImage ()
 
524
{
 
525
    float          pw, ph;
 
526
    int            x1, x2, y1, y2;
 
527
    float          vc[4];
 
528
    float          tc[4];
 
529
    int            w, h, cw, ch, cx, cy;
 
530
    float          tmp, xOff, yOff;
 
531
 
 
532
    w = overlaySize.width ();
 
533
    h = overlaySize.height ();
 
534
 
 
535
    xOff = MIN (w, optionGetXOffset ());
 
536
    yOff = MIN (h, optionGetYOffset ());
 
537
 
 
538
    x1 = posX - xOff;
 
539
    x2 = x1 + w;
 
540
    y1 = posY - yOff;
 
541
    y2 = y1 + h;
 
542
 
 
543
    cw = ceil ((float)w / (zoom * 2.0)) * 2.0;
 
544
    ch = ceil ((float)h / (zoom * 2.0)) * 2.0;
 
545
    cw = MIN (w, cw + 2);
 
546
    ch = MIN (h, ch + 2);
 
547
    cx = floor (xOff - (xOff / zoom));
 
548
    cy = h - ch - floor (yOff - (yOff / zoom));
 
549
 
 
550
    cx = MAX (0, MIN (w - cw, cx));
 
551
    cy = MAX (0, MIN (h - ch, cy));
 
552
 
 
553
    glPushAttrib (GL_TEXTURE_BIT);
 
554
    
 
555
    glEnable (target);
 
556
 
 
557
    glBindTexture (target, texture);
 
558
 
 
559
    if (width != w || height != h)
 
560
    {
 
561
        glCopyTexImage2D(target, 0, GL_RGB, x1, screen->height () - y2,
 
562
                         w, h, 0);
 
563
        width = w;
 
564
        height = h;
 
565
    }
 
566
    else
 
567
        glCopyTexSubImage2D (target, 0, cx, cy,
 
568
                             x1 + cx, screen->height () - y2 + cy, cw, ch);
 
569
 
 
570
    if (target == GL_TEXTURE_2D)
 
571
    {
 
572
        pw = 1.0 / width;
 
573
        ph = 1.0 / height;
 
574
    }
 
575
    else
 
576
    {
 
577
        pw = 1.0;
 
578
        ph = 1.0;
 
579
    }
 
580
 
 
581
    glMatrixMode (GL_PROJECTION);
 
582
    glPushMatrix ();
 
583
    glLoadIdentity ();
 
584
    glMatrixMode (GL_MODELVIEW);
 
585
    glPushMatrix ();
 
586
    glLoadIdentity ();
 
587
 
 
588
    vc[0] = ((x1 * 2.0) / screen->width ()) - 1.0;
 
589
    vc[1] = ((x2 * 2.0) / screen->width ()) - 1.0;
 
590
    vc[2] = ((y1 * -2.0) / screen->height ()) + 1.0;
 
591
    vc[3] = ((y2 * -2.0) / screen->height ()) + 1.0;
 
592
 
 
593
    tc[0] = xOff - (xOff / zoom);
 
594
    tc[1] = tc[0] + (w / zoom);
 
595
 
 
596
    tc[2] = h - (yOff - (yOff / zoom));
 
597
    tc[3] = tc[2] - (h / zoom);
 
598
 
 
599
    tc[0] *= pw;
 
600
    tc[1] *= pw;
 
601
    tc[2] *= ph;
 
602
    tc[3] *= ph;
 
603
 
 
604
    glEnable (GL_BLEND);
 
605
 
 
606
    glColor4usv (defaultColor);
 
607
    glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
608
 
 
609
    GL::activeTexture (GL_TEXTURE1_ARB);
 
610
    foreach (GLTexture *tex, mask)
 
611
    {
 
612
        tex->enable (GLTexture::Good);
 
613
        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
614
 
 
615
        glBegin (GL_QUADS);
 
616
        GL::multiTexCoord2f (GL_TEXTURE0_ARB, tc[0], tc[2]);
 
617
        GL::multiTexCoord2f (GL_TEXTURE1_ARB,
 
618
                           COMP_TEX_COORD_X (tex->matrix (), 0),
 
619
                           COMP_TEX_COORD_Y (tex->matrix (), 0));
 
620
        glVertex2f (vc[0], vc[2]);
 
621
        GL::multiTexCoord2f (GL_TEXTURE0_ARB, tc[0], tc[3]);
 
622
        GL::multiTexCoord2f (GL_TEXTURE1_ARB,
 
623
                           COMP_TEX_COORD_X (tex->matrix (), 0),
 
624
                           COMP_TEX_COORD_Y (tex->matrix (), h));
 
625
        glVertex2f (vc[0], vc[3]);
 
626
        GL::multiTexCoord2f (GL_TEXTURE0_ARB, tc[1], tc[3]);
 
627
        GL::multiTexCoord2f (GL_TEXTURE1_ARB,
 
628
                           COMP_TEX_COORD_X (tex->matrix (), w),
 
629
                           COMP_TEX_COORD_Y (tex->matrix (), h));
 
630
        glVertex2f (vc[1], vc[3]);
 
631
        GL::multiTexCoord2f (GL_TEXTURE0_ARB, tc[1], tc[2]);
 
632
        GL::multiTexCoord2f (GL_TEXTURE1_ARB,
 
633
                           COMP_TEX_COORD_X (tex->matrix (), w),
 
634
                           COMP_TEX_COORD_Y (tex->matrix (), 0));
 
635
        glVertex2f (vc[1], vc[2]);
 
636
        glEnd ();
 
637
 
 
638
        tex->disable ();
 
639
    }
 
640
    GL::activeTexture (GL_TEXTURE0_ARB);
 
641
 
 
642
    glBindTexture (target, 0);
 
643
 
 
644
    glDisable (target);
 
645
 
 
646
    tmp = MIN (1.0, (zoom - 1) * 3.0);
 
647
 
 
648
    glColor4f (tmp, tmp, tmp, tmp);
 
649
 
 
650
    foreach (GLTexture *tex, overlay)
 
651
    {
 
652
        tex->enable (GLTexture::Fast);
 
653
        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
654
 
 
655
        glBegin (GL_QUADS);
 
656
        glTexCoord2f (COMP_TEX_COORD_X (tex->matrix (), 0),
 
657
                  COMP_TEX_COORD_Y (tex->matrix (), 0));
 
658
        glVertex2f (vc[0], vc[2]);
 
659
        glTexCoord2f (COMP_TEX_COORD_X (tex->matrix (), 0),
 
660
                  COMP_TEX_COORD_Y (tex->matrix (), h));
 
661
        glVertex2f (vc[0], vc[3]);
 
662
        glTexCoord2f (COMP_TEX_COORD_X (tex->matrix (), w),
 
663
                  COMP_TEX_COORD_Y (tex->matrix (), h));
 
664
        glVertex2f (vc[1], vc[3]);
 
665
        glTexCoord2f (COMP_TEX_COORD_X (tex->matrix (), w),
 
666
                  COMP_TEX_COORD_Y (tex->matrix (), 0));
 
667
        glVertex2f (vc[1], vc[2]);
 
668
        glEnd ();
 
669
 
 
670
        tex->disable ();
 
671
    }
 
672
 
 
673
    glColor4usv (defaultColor);
 
674
    glDisable (GL_BLEND);
 
675
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
676
 
 
677
    glPopMatrix();
 
678
    glMatrixMode (GL_PROJECTION);
 
679
    glPopMatrix ();
 
680
    glMatrixMode (GL_MODELVIEW);
 
681
 
 
682
    glPopAttrib ();
 
683
 
 
684
}
 
685
 
 
686
void
 
687
MagScreen::paintFisheye ()
 
688
{
 
689
    float      pw, ph;
 
690
    float      radius, fZoom, base; // fZoom is the local zoom variable
 
691
    int        x1, x2, y1, y2;
 
692
    float      vc[4];
 
693
    int        size;
 
694
 
 
695
    radius = optionGetRadius ();
 
696
    base   = 0.5 + (0.0015 * radius);
 
697
    fZoom   = (zoom * base) + 1.0 - base;
 
698
 
 
699
    size = radius + 1;
 
700
 
 
701
    x1 = MAX (0.0, posX - size);
 
702
    x2 = MIN (screen->width (), posX + size);
 
703
    y1 = MAX (0.0, posY - size);
 
704
    y2 = MIN (screen->height (), posY + size);
 
705
 
 
706
    glEnable (target);
 
707
 
 
708
    glBindTexture (target, texture);
 
709
 
 
710
    if (width != 2 * size || height != 2 * size)
 
711
    {
 
712
        glCopyTexImage2D(target, 0, GL_RGB, x1, screen->height () - y2,
 
713
                         size * 2, size * 2, 0);
 
714
        width = height = 2 * size;
 
715
    }
 
716
    else
 
717
        glCopyTexSubImage2D (target, 0, 0, 0,
 
718
                             x1, screen->height () - y2, x2 - x1, y2 - y1);
 
719
 
 
720
    if (target == GL_TEXTURE_2D)
 
721
    {
 
722
        pw = 1.0 / width;
 
723
        ph = 1.0 / height;
 
724
    }
 
725
    else
 
726
    {
 
727
        pw = 1.0;
 
728
        ph = 1.0;
 
729
    }
 
730
    
 
731
    glMatrixMode (GL_PROJECTION);
 
732
    glPushMatrix ();
 
733
    glLoadIdentity ();
 
734
    glMatrixMode (GL_MODELVIEW);
 
735
    glPushMatrix ();
 
736
    glLoadIdentity ();
 
737
 
 
738
    glColor4usv (defaultColor);
 
739
 
 
740
    glEnable (GL_FRAGMENT_PROGRAM_ARB);
 
741
    GL::bindProgram (GL_FRAGMENT_PROGRAM_ARB, program);
 
742
 
 
743
    GL::programEnvParameter4f (GL_FRAGMENT_PROGRAM_ARB, 0,
 
744
                                 posX, screen->height () - posY,
 
745
                                 1.0 / radius, 0.0f);
 
746
    GL::programEnvParameter4f (GL_FRAGMENT_PROGRAM_ARB, 1,
 
747
                                 pw, ph, M_PI / radius,
 
748
                                 (fZoom - 1.0) * fZoom);
 
749
    GL::programEnvParameter4f (GL_FRAGMENT_PROGRAM_ARB, 2,
 
750
                                 -x1 * pw, -(screen->height () - y2) * ph,
 
751
                                 -M_PI / 2.0, 0.0);
 
752
 
 
753
    x1 = MAX (0.0, posX - radius);
 
754
    x2 = MIN (screen->width (), posX + radius);
 
755
    y1 = MAX (0.0, posY - radius);
 
756
    y2 = MIN (screen->height (), posY + radius);
 
757
 
 
758
    vc[0] = ((x1 * 2.0) / screen->width ()) - 1.0;
 
759
    vc[1] = ((x2 * 2.0) / screen->width ()) - 1.0;
 
760
    vc[2] = ((y1 * -2.0) / screen->height ()) + 1.0;
 
761
    vc[3] = ((y2 * -2.0) / screen->height ()) + 1.0;
 
762
 
 
763
    y1 = screen->height () - y1;
 
764
    y2 = screen->height () - y2;
 
765
 
 
766
    glBegin (GL_QUADS);
 
767
    glTexCoord2f (x1, y1);
 
768
    glVertex2f (vc[0], vc[2]);
 
769
    glTexCoord2f (x1, y2);
 
770
    glVertex2f (vc[0], vc[3]);
 
771
    glTexCoord2f (x2, y2);
 
772
    glVertex2f (vc[1], vc[3]);
 
773
    glTexCoord2f (x2, y1);
 
774
    glVertex2f (vc[1], vc[2]);
 
775
    glEnd ();
 
776
 
 
777
    glDisable (GL_FRAGMENT_PROGRAM_ARB);
 
778
 
 
779
    glColor4usv (defaultColor);
 
780
    
 
781
    glPopMatrix();
 
782
    glMatrixMode (GL_PROJECTION);
 
783
    glPopMatrix ();
 
784
    glMatrixMode (GL_MODELVIEW);
 
785
 
 
786
    glBindTexture (target, 0);
 
787
 
 
788
    glDisable (target);
 
789
}
 
790
 
 
791
 
 
792
bool
 
793
MagScreen::glPaintOutput (const GLScreenPaintAttrib &attrib,
 
794
                          const GLMatrix            &transform,
 
795
                          const CompRegion          &region,
 
796
                          CompOutput                *output,
 
797
                          unsigned int              mask)
 
798
{
 
799
    bool status = gScreen->glPaintOutput (attrib, transform, region, output, mask);
 
800
 
 
801
    if (zoom == 1.0)
 
802
        return status;
 
803
 
 
804
    /* Temporarily set the viewport to fullscreen */
 
805
 
 
806
    glViewport (0, 0, screen->width (), screen->height ());
 
807
 
 
808
    switch (mode)
 
809
    {
 
810
    case MagOptions::ModeImageOverlay:
 
811
        paintImage ();
 
812
        break;
 
813
    case MagOptions::ModeFisheye:
 
814
        paintFisheye ();
 
815
        break;
 
816
    default:
 
817
        paintSimple ();
 
818
    }
 
819
 
 
820
    gScreen->setDefaultViewport ();
 
821
 
 
822
    return status;
 
823
}
 
824
 
 
825
bool
 
826
MagScreen::terminate (CompAction          *action,
 
827
                      CompAction::State   state,
 
828
                      CompOption::Vector options)
 
829
{
 
830
    zTarget = 1.0;
 
831
    adjust  = true;
 
832
    cScreen->damageScreen ();
 
833
    return true;
 
834
}
 
835
 
 
836
bool
 
837
MagScreen::initiate (CompAction   *action,
 
838
                     CompAction::State   state,
 
839
                     CompOption::Vector options)
 
840
{
 
841
    float      factor;
 
842
    factor = CompOption::getFloatOptionNamed (options, "factor", 0);
 
843
 
 
844
    if (factor == 0.0 && zTarget != 1.0)
 
845
        return terminate (action, state, options);
 
846
 
 
847
    if (mode == MagOptions::ModeFisheye)
 
848
    {
 
849
        if (factor != 1.0)
 
850
            factor = optionGetZoomFactor () * 3;
 
851
 
 
852
        zTarget = MAX (1.0, MIN (10.0, factor));
 
853
 
 
854
    }
 
855
    else
 
856
    {
 
857
        if (factor != 1.0)
 
858
            factor = optionGetZoomFactor ();
 
859
 
 
860
        zTarget = MAX (1.0, MIN (64.0, factor));
 
861
    }
 
862
    adjust  = true;
 
863
    cScreen->damageScreen ();
 
864
 
 
865
    // Mag mode is starting
 
866
    cScreen->preparePaintSetEnabled (this, true);
 
867
    cScreen->donePaintSetEnabled (this, true);
 
868
    gScreen->glPaintOutputSetEnabled (this, true);
 
869
 
 
870
    return true;
 
871
}
 
872
 
 
873
bool
 
874
MagScreen::zoomIn (CompAction     *action,
 
875
                   CompAction::State   state,
 
876
                   CompOption::Vector options)
 
877
{
 
878
    if (mode == MagOptions::ModeFisheye)
 
879
        zTarget = MIN (10.0, zTarget + 1.0);
 
880
    else
 
881
        zTarget = MIN (64.0, zTarget * 1.2);
 
882
    adjust  = true;
 
883
    cScreen->damageScreen ();
 
884
 
 
885
    // Mag mode is starting
 
886
    cScreen->preparePaintSetEnabled (this, true);
 
887
    cScreen->donePaintSetEnabled (this, true);
 
888
    gScreen->glPaintOutputSetEnabled (this, true);
 
889
 
 
890
    return true;
 
891
}
 
892
 
 
893
bool
 
894
MagScreen::zoomOut (CompAction    *action,
 
895
                    CompAction::State   state,
 
896
                    CompOption::Vector options)
 
897
{
 
898
    if (mode == MagOptions::ModeFisheye)
 
899
        zTarget = MAX (1.0, zTarget - 1.0);
 
900
    else
 
901
        zTarget = MAX (1.0, zTarget / 1.2);
 
902
    adjust  = true;
 
903
    cScreen->damageScreen ();
 
904
 
 
905
    return true;
 
906
}
 
907
 
 
908
void
 
909
MagScreen::postLoad ()
 
910
{
 
911
    if (zTarget != 1.0f || zVelocity != 0.0f || zoom != 1.0f)
 
912
    {
 
913
        cScreen->preparePaintSetEnabled (this, true);
 
914
        cScreen->donePaintSetEnabled (this, true);
 
915
        gScreen->glPaintOutputSetEnabled (this, true);
 
916
        
 
917
        cScreen->damageScreen ();
 
918
    }
 
919
}
 
920
 
 
921
MagScreen::MagScreen (CompScreen *screen) :
 
922
    PluginClassHandler <MagScreen, CompScreen> (screen),
 
923
    PluginStateWriter <MagScreen> (this, screen->root ()),
 
924
    cScreen (CompositeScreen::get (screen)),
 
925
    gScreen (GLScreen::get (screen)),
 
926
    posX (0),
 
927
    posY (0),
 
928
    adjust (false),
 
929
    zVelocity (0.0f),
 
930
    zTarget (1.0f),
 
931
    zoom (1.0f),
 
932
    program (0)
 
933
{
 
934
    ScreenInterface::setHandler (screen, false);
 
935
    CompositeScreenInterface::setHandler (cScreen, false);
 
936
    GLScreenInterface::setHandler (gScreen, false);
 
937
 
 
938
    poller.setCallback (boost::bind (&MagScreen::positionUpdate, this, _1));
 
939
 
 
940
    glGenTextures (1, &texture);
 
941
 
 
942
    if (GL::textureNonPowerOfTwo)
 
943
        target = GL_TEXTURE_2D;
 
944
    else
 
945
        target = GL_TEXTURE_RECTANGLE_ARB;
 
946
 
 
947
    glEnable (target);
 
948
 
 
949
    /* Bind the texture */
 
950
    glBindTexture (target, texture);
 
951
 
 
952
    /* Load the parameters */
 
953
    glTexParameteri (target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
954
    glTexParameteri (target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
955
    glTexParameteri (target, GL_TEXTURE_WRAP_S, GL_CLAMP);
 
956
    glTexParameteri (target, GL_TEXTURE_WRAP_T, GL_CLAMP);
 
957
 
 
958
    glTexImage2D (target, 0, GL_RGB, 0, 0, 0,
 
959
                  GL_RGB, GL_UNSIGNED_BYTE, NULL);
 
960
 
 
961
    width = 0;
 
962
    height = 0;
 
963
 
 
964
    glBindTexture (target, 0);
 
965
 
 
966
    glDisable (target);
 
967
 
 
968
#define optionNotify(name)                                                     \
 
969
    optionSet##name##Notify (boost::bind (&MagScreen::optionChanged,           \
 
970
                                            this, _1, _2))
 
971
 
 
972
    optionNotify (Overlay);
 
973
    optionNotify (Mask);
 
974
    optionNotify (Mode);
 
975
 
 
976
#undef optionNotify
 
977
 
 
978
    optionSetInitiateInitiate (boost::bind (&MagScreen::initiate, this, _1, _2,
 
979
                                                _3));
 
980
    optionSetInitiateTerminate (boost::bind (&MagScreen::initiate, this, _1, _2,
 
981
                                                _3));
 
982
 
 
983
    optionSetZoomInButtonInitiate (boost::bind (&MagScreen::zoomIn, this, _1, _2,
 
984
                                                _3));
 
985
 
 
986
    optionSetZoomOutButtonInitiate (boost::bind (&MagScreen::zoomOut, this, _1, _2,
 
987
                                                _3));
 
988
 
 
989
    switch (optionGetMode ())
 
990
    {
 
991
    case MagOptions::ModeImageOverlay:
 
992
        if (loadImages ())
 
993
            mode = MagOptions::ModeImageOverlay;
 
994
        else
 
995
            mode = MagOptions::ModeSimple;
 
996
        break;
 
997
    case MagOptions::ModeFisheye:
 
998
        if (loadFragmentProgram ())
 
999
            mode = MagOptions::ModeFisheye;
 
1000
        else
 
1001
            mode = MagOptions::ModeSimple;
 
1002
        break;
 
1003
    default:
 
1004
        mode = MagOptions::ModeSimple;
 
1005
    }
 
1006
 
 
1007
    if (!GL::fragmentProgram)
 
1008
        compLogMessage ("mag", CompLogLevelWarn,
 
1009
                        "GL_ARB_fragment_program not supported. "
 
1010
                        "Fisheye mode will not work.");
 
1011
}
 
1012
 
 
1013
MagScreen::~MagScreen ()
 
1014
{
 
1015
    writeSerializedData ();
 
1016
 
 
1017
    poller.stop ();
 
1018
 
 
1019
    if (zoom)
 
1020
        cScreen->damageScreen ();
 
1021
 
 
1022
    glDeleteTextures (1, &target);
 
1023
 
 
1024
    cleanup ();
 
1025
}
 
1026
 
 
1027
bool
 
1028
MagPluginVTable::init ()
 
1029
{
 
1030
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
 
1031
        return false;
 
1032
    if (!CompPlugin::checkPluginABI ("composite", COMPIZ_COMPOSITE_ABI))
 
1033
        return false;
 
1034
    if (!CompPlugin::checkPluginABI ("opengl", COMPIZ_OPENGL_ABI))
 
1035
        return false;
 
1036
    if (!CompPlugin::checkPluginABI ("mousepoll", COMPIZ_MOUSEPOLL_ABI))
 
1037
        return false;
 
1038
 
 
1039
    return true;
 
1040
}