~ubuntu-branches/ubuntu/trusty/compiz/trusty

« back to all changes in this revision

Viewing changes to plugins/text/src/text.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-08-22 06:58:07 UTC
  • mto: This revision was merged to the branch mainline in revision 3352.
  • Revision ID: package-import@ubuntu.com-20130822065807-17nlzez0d30y09so
Tags: upstream-0.9.10+13.10.20130822
ImportĀ upstreamĀ versionĀ 0.9.10+13.10.20130822

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
                                    Atom   atom)
37
37
{
38
38
    Atom          type;
39
 
    int           result, format;
 
39
    int           format;
40
40
    unsigned long nItems, bytesAfter;
41
41
    char          *val;
42
42
    CompString    retval;
43
43
 
44
 
    result = XGetWindowProperty (screen->dpy (), id, atom, 0L, 65536, False,
45
 
                                 utf8StringAtom, &type, &format, &nItems,
46
 
                                 &bytesAfter, (unsigned char **) &val);
 
44
    int result = XGetWindowProperty (screen->dpy (), id, atom, 0L, 65536, False,
 
45
                                     utf8StringAtom, &type, &format, &nItems,
 
46
                                     &bytesAfter, (unsigned char **) &val);
47
47
 
48
48
    if (result != Success)
49
49
        return retval;
72
72
    CompString    retval;
73
73
 
74
74
    text.nitems = 0;
75
 
    if (XGetTextProperty (screen->dpy (), id, &text, atom))
 
75
 
 
76
    if (XGetTextProperty (screen->dpy (), id, &text, atom) &&
 
77
        text.value)
76
78
    {
77
 
        if (text.value)
78
 
        {
79
 
            char valueString[text.nitems + 1];
80
 
 
81
 
            strncpy (valueString, (char *) text.value, text.nitems);
82
 
            valueString[text.nitems] = 0;
83
 
 
84
 
            retval = valueString;
85
 
 
86
 
            XFree (text.value);
87
 
        }
 
79
        char valueString[text.nitems + 1];
 
80
 
 
81
        strncpy (valueString, (char *) text.value, text.nitems);
 
82
        valueString[text.nitems] = 0;
 
83
 
 
84
        retval = valueString;
 
85
 
 
86
        XFree (text.value);
88
87
    }
89
88
 
90
89
    return retval;
93
92
CompString
94
93
PrivateTextScreen::getWindowName (Window id)
95
94
{
96
 
    CompString name;
97
 
 
98
 
    name = getUtf8Property (id, visibleNameAtom);
 
95
    CompString name = getUtf8Property (id, visibleNameAtom);
99
96
 
100
97
    if (name.empty ())
101
98
        name = getUtf8Property (id, wmNameAtom);
112
109
 * Draw a rounded rectangle path
113
110
 */
114
111
void
115
 
TextSurface::drawBackground (int     x,
116
 
                             int     y,
117
 
                             int     width,
118
 
                             int     height,
119
 
                             int     radius)
 
112
TextSurface::drawBackground (int x,
 
113
                             int y,
 
114
                             int width,
 
115
                             int height,
 
116
                             int radius)
120
117
{
121
 
    int x0, y0, x1, y1;
 
118
    int x0 = x;
 
119
    int y0 = y;
 
120
    int x1 = x + width;
 
121
    int y1 = y + height;
122
122
 
123
 
    x0 = x;
124
 
    y0 = y;
125
 
    x1 = x + width;
126
 
    y1 = y + height;
 
123
    const float halfPi    = PI / 2.0f;
 
124
    const float triHalfPi = halfPi * 3;
127
125
 
128
126
    cairo_new_path (cr);
129
 
    cairo_arc (cr, x0 + radius, y1 - radius, radius, PI / 2, PI);
 
127
    cairo_arc (cr, x0 + radius, y1 - radius, radius, halfPi, PI);
130
128
    cairo_line_to (cr, x0, y0 + radius);
131
 
    cairo_arc (cr, x0 + radius, y0 + radius, radius, PI, 3 * PI / 2);
 
129
    cairo_arc (cr, x0 + radius, y0 + radius, radius, PI, triHalfPi);
132
130
    cairo_line_to (cr, x1 - radius, y0);
133
 
    cairo_arc (cr, x1 - radius, y0 + radius, radius, 3 * PI / 2, 2 * PI);
 
131
    cairo_arc (cr, x1 - radius, y0 + radius, radius, triHalfPi, 2 * PI);
134
132
    cairo_line_to (cr, x1, y1 - radius);
135
 
    cairo_arc (cr, x1 - radius, y1 - radius, radius, 0, PI / 2);
 
133
    cairo_arc (cr, x1 - radius, y1 - radius, radius, 0, halfPi);
136
134
    cairo_close_path (cr);
137
135
}
138
136
 
143
141
    Display *dpy = screen->dpy ();
144
142
 
145
143
    mPixmap = None;
 
144
 
146
145
    if (width > 0 && height > 0)
147
146
        mPixmap = XCreatePixmap (dpy, screen->root (), width, height, 32);
148
147
 
170
169
    }
171
170
 
172
171
    cr = cairo_create (surface);
 
172
 
173
173
    if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
174
174
    {
175
175
        compLogMessage ("text", CompLogLevelError,
202
202
TextSurface::render (const CompText::Attrib &attrib,
203
203
                     const CompString       &text)
204
204
{
205
 
    int width, height, layoutWidth;
206
 
 
207
205
    if (!valid ())
208
206
        return false;
209
207
 
210
208
    pango_font_description_set_family (font, attrib.family);
211
 
    pango_font_description_set_absolute_size (font,
212
 
                                              attrib.size * PANGO_SCALE);
 
209
    pango_font_description_set_absolute_size (font, attrib.size * PANGO_SCALE);
213
210
    pango_font_description_set_style (font, PANGO_STYLE_NORMAL);
214
211
 
215
212
    if (attrib.flags & CompText::StyleBold)
226
223
    pango_layout_set_auto_dir (layout, false);
227
224
    pango_layout_set_text (layout, text.c_str (), -1);
228
225
 
 
226
    int width, height;
 
227
 
229
228
    pango_layout_get_pixel_size (layout, &width, &height);
230
229
 
231
230
    if (attrib.flags & CompText::WithBackground)
238
237
    height = MIN (attrib.maxHeight, height);
239
238
 
240
239
    /* update the size of the pango layout */
241
 
    layoutWidth = attrib.maxWidth;
 
240
    int layoutWidth = attrib.maxWidth;
 
241
 
242
242
    if (attrib.flags & CompText::WithBackground)
243
243
        layoutWidth -= 2 * attrib.bgHMargin;
244
244
 
292
292
    mWidth  (0),
293
293
    mHeight (0),
294
294
    mPixmap (None),
295
 
    cr (NULL),
 
295
    cr      (NULL),
296
296
    surface (NULL),
297
 
    layout (NULL),
298
 
    format (NULL),
299
 
    font (NULL),
300
 
    scrn (NULL)
 
297
    layout  (NULL),
 
298
    format  (NULL),
 
299
    font    (NULL),
 
300
    scrn    (NULL)
301
301
{
302
302
    Display *dpy = screen->dpy ();
303
303
 
311
311
    }
312
312
 
313
313
    format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
 
314
 
314
315
    if (!format)
315
316
    {
316
317
        compLogMessage ("text", CompLogLevelError, "Couldn't get format.");
322
323
 
323
324
    /* init pango */
324
325
    layout = pango_cairo_create_layout (cr);
 
326
 
325
327
    if (!layout)
326
328
    {
327
329
        compLogMessage ("text", CompLogLevelError,
330
332
    }
331
333
 
332
334
    font = pango_font_description_new ();
 
335
 
333
336
    if (!font)
334
337
    {
335
338
        compLogMessage ("text", CompLogLevelError,
342
345
{
343
346
    if (layout)
344
347
        g_object_unref (layout);
 
348
 
345
349
    if (surface)
346
350
        cairo_surface_destroy (surface);
 
351
 
347
352
    if (cr)
348
353
        cairo_destroy (cr);
 
354
 
349
355
    if (font)
350
356
        pango_font_description_free (font);
351
357
}
369
375
 
370
376
    TEXT_SCREEN (screen);
371
377
 
372
 
    if (!ts)
373
 
        return false;
374
 
 
375
 
    if (!surface.valid ())
376
 
        return false;
377
 
 
378
 
    if (!(attrib.flags & NoAutoBinding) && !ts->gScreen)
 
378
    if (!ts                 ||
 
379
        !surface.valid ()   ||
 
380
        (!(attrib.flags & NoAutoBinding) && !ts->gScreen))
379
381
        return false;
380
382
 
381
383
    if (surface.render (attrib, text))
389
391
            retval  = !texture.empty ();
390
392
        }
391
393
        else
392
 
        {
393
394
            retval = true;
394
 
        }
395
395
    }
396
396
 
397
397
    if (!retval && surface.mPixmap)
410
410
}
411
411
 
412
412
bool
413
 
CompText::renderWindowTitle (Window               window,
414
 
                             bool                 withViewportNumber,
415
 
                             const CompText::Attrib &attrib)
 
413
CompText::renderWindowTitle (Window                 window,
 
414
                             bool                   withViewportNumber,
 
415
                             const CompText::Attrib &attrib)
416
416
{
 
417
    TEXT_SCREEN (screen);
 
418
 
 
419
    if (!ts)
 
420
        return false;
 
421
 
417
422
    CompString text;
418
423
 
419
 
    TEXT_SCREEN (screen);
420
 
 
421
 
    if (!ts)
422
 
        return false;
423
 
 
424
424
    if (withViewportNumber)
425
425
    {
426
 
        CompString title;
427
 
        CompPoint  winViewport;
428
 
        CompSize   viewportSize;
 
426
        CompString title = ts->getWindowName (window);
429
427
 
430
 
        title = ts->getWindowName (window);
431
428
        if (!title.empty ())
432
429
        {
433
 
            CompWindow *w;
 
430
            CompWindow *w = screen->findWindow (window);
434
431
 
435
 
            w = screen->findWindow (window);
436
432
            if (w)
437
433
            {
438
 
                int viewport;
 
434
                CompPoint winViewport  = w->defaultViewport ();
 
435
                CompSize  viewportSize = screen->vpSize ();
 
436
                int viewport = winViewport.y () * viewportSize.width () +
 
437
                               winViewport.x () + 1;
439
438
 
440
 
                winViewport  = w->defaultViewport ();
441
 
                viewportSize = screen->vpSize ();
442
 
                viewport = winViewport.y () * viewportSize.width () +
443
 
                           winViewport.x () + 1;
444
439
                text = compPrintf ("%s -[%d]-", title.c_str (), viewport);
445
440
            }
446
441
            else
447
 
            {
448
442
                text = title;
449
 
            }
450
443
        }
451
444
    }
452
445
    else
453
 
    {
454
446
        text = ts->getWindowName (window);
455
 
    }
456
447
 
457
448
    if (text.empty ())
458
449
        return false;
488
479
 
489
480
void
490
481
CompText::draw (const GLMatrix &transform,
491
 
                float           x,
492
 
                float y,
493
 
                float alpha) const
 
482
                float          x,
 
483
                float          y,
 
484
                float          alpha) const
494
485
{
495
 
    GLint      oldBlendSrc, oldBlendDst;
496
 
    GLushort        colorData[4];
497
 
    GLfloat         textureData[8];
498
 
    GLfloat         vertexData[12];
499
 
    GLVertexBuffer *streamingBuffer = GLVertexBuffer::streamingBuffer ();
500
 
 
501
486
    if (texture.empty ())
502
487
        return;
503
488
 
 
489
    GLint          oldBlendSrc, oldBlendDst;
504
490
#ifdef USE_GLES
505
 
    GLint           oldBlendSrcAlpha, oldBlendDstAlpha;
 
491
    GLint          oldBlendSrcAlpha, oldBlendDstAlpha;
506
492
    glGetIntegerv (GL_BLEND_SRC_RGB, &oldBlendSrc);
507
493
    glGetIntegerv (GL_BLEND_DST_RGB, &oldBlendDst);
508
494
    glGetIntegerv (GL_BLEND_SRC_ALPHA, &oldBlendSrcAlpha);
511
497
    glGetIntegerv (GL_BLEND_SRC, &oldBlendSrc);
512
498
    glGetIntegerv (GL_BLEND_DST, &oldBlendDst);
513
499
 
514
 
    GLboolean  wasBlend;
515
 
    wasBlend = glIsEnabled (GL_BLEND);
 
500
    GLboolean wasBlend = glIsEnabled (GL_BLEND);
 
501
 
516
502
    if (!wasBlend)
517
503
        glEnable (GL_BLEND);
518
504
#endif
519
505
 
520
506
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
521
507
 
 
508
    GLushort          colorData[4];
 
509
    GLfloat           textureData[8];
 
510
    GLfloat           vertexData[12];
 
511
    GLVertexBuffer    *streamingBuffer = GLVertexBuffer::streamingBuffer ();
 
512
    GLTexture         *tex;
 
513
    GLTexture::Matrix m;
 
514
    GLfloat           xPlusWidth   = x + width;
 
515
    GLfloat           yMinusHeight = y - height;
 
516
 
522
517
    colorData[0] = alpha * 65535;
523
518
    colorData[1] = alpha * 65535;
524
519
    colorData[2] = alpha * 65535;
525
520
    colorData[3] = alpha * 65535;
526
521
 
527
 
    for (unsigned int i = 0; i < texture.size (); i++)
 
522
    for (unsigned int i = 0; i < texture.size (); ++i)
528
523
    {
529
 
        GLTexture         *tex = texture[i];
530
 
        GLTexture::Matrix m = tex->matrix ();
 
524
        tex = texture[i];
 
525
        m = tex->matrix ();
531
526
 
532
527
        tex->enable (GLTexture::Good);
533
528
 
534
529
        streamingBuffer->begin (GL_TRIANGLE_STRIP);
535
530
 
536
531
        vertexData[0]  = x;
537
 
        vertexData[1]  = y - height;
 
532
        vertexData[1]  = yMinusHeight;
538
533
        vertexData[2]  = 0;
539
534
        vertexData[3]  = x;
540
535
        vertexData[4]  = y;
541
536
        vertexData[5]  = 0;
542
 
        vertexData[6]  = x + width;
543
 
        vertexData[7]  = y - height;
 
537
        vertexData[6]  = xPlusWidth;
 
538
        vertexData[7]  = yMinusHeight;
544
539
        vertexData[8]  = 0;
545
 
        vertexData[9]  = x + width;
 
540
        vertexData[9]  = xPlusWidth;
546
541
        vertexData[10] = y;
547
542
        vertexData[11] = 0;
548
543
 
567
562
 
568
563
#ifdef USE_GLES
569
564
    glBlendFuncSeparate (oldBlendSrc, oldBlendDst,
570
 
                         oldBlendSrcAlpha, oldBlendDstAlpha);
 
565
                         oldBlendSrcAlpha, oldBlendDstAlpha);
571
566
#else
572
567
    if (!wasBlend)
573
568
        glDisable (GL_BLEND);
 
569
 
574
570
    glBlendFunc (oldBlendSrc, oldBlendDst);
575
571
#endif
576
572
}
577
573
 
578
574
CompText::CompText () :
579
 
    width (0),
 
575
    width  (0),
580
576
    height (0),
581
577
    pixmap (None)
582
578
{
595
591
    gScreen (GLScreen::get (screen))
596
592
{
597
593
    visibleNameAtom = XInternAtom (screen->dpy (), "_NET_WM_VISIBLE_NAME", 0);
598
 
    utf8StringAtom = XInternAtom (screen->dpy (), "UTF8_STRING", 0);
599
 
    wmNameAtom = XInternAtom (screen->dpy (), "_NET_WM_NAME", 0);
 
594
    utf8StringAtom  = XInternAtom (screen->dpy (), "UTF8_STRING", 0);
 
595
    wmNameAtom      = XInternAtom (screen->dpy (), "_NET_WM_NAME", 0);
600
596
}
601
597
 
602
598
PrivateTextScreen::~PrivateTextScreen ()
606
602
bool
607
603
TextPluginVTable::init ()
608
604
{
609
 
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
610
 
         return false;
611
 
 
612
 
    CompPrivate p;
613
 
    p.uval = COMPIZ_TEXT_ABI;
614
 
    screen->storeValue ("text_ABI", p);
615
 
 
616
 
    return true;
 
605
    if (CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
 
606
    {
 
607
        CompPrivate p;
 
608
        p.uval = COMPIZ_TEXT_ABI;
 
609
        screen->storeValue ("text_ABI", p);
 
610
        return true;
 
611
    }
 
612
 
 
613
    return false;
617
614
}
618
615
 
619
616
void