summaryrefslogtreecommitdiffstats
path: root/tools/tqvfb/tqvfbview.cpp
blob: f96df8ca13e270a2502a64ebc73c3c73a36bb411 (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/**********************************************************************
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of TQt/Embedded virtual framebuffer.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** Licensees holding valid TQt Commercial licenses may use this file in
** accordance with the TQt Commercial License Agreement provided with
** the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "ntqglobal.h"
#if !defined( Q_WS_QWS ) || defined( TQT_NO_QWS_MULTIPROCESS )
#define TQLock TQWSSemaphore
#undef TQT_NO_QWS_MULTIPROCESS
#include "../../src/kernel/qlock.cpp"
#else
#include "qlock_p.h"
#endif

#include "tqvfbview.h"
#include "ntqvfbhdr.h"

#define TQTE_PIPE "TQtEmbedded-%1"

#include <ntqapplication.h>
#include <ntqimage.h>
#include <ntqbitmap.h>
#include <ntqtimer.h>
#include <ntqwmatrix.h>
#include <ntqpainter.h>
#include "qanimationwriter.h"

#include <unistd.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>

//#define QT_QWS_EXPERIMENTAL_REVERSE_BIT_ENDIANNESS

TQVFbView::TQVFbView( int display_id, int w, int h, int d, TQWidget *parent,
		    const char *name, uint flags )
    : TQScrollView( parent, name, flags ), emulateTouchscreen(FALSE), qwslock(NULL)
{
    displayid = display_id;
    viewport()->setMouseTracking( TRUE );
    viewport()->setFocusPolicy( StrongFocus );
    zm = 1;
    animation = 0;
    int actualdepth=d;

    switch ( d ) {
	case 12:
	    actualdepth=16;
	    break;
	case 1:
	case 4:
	case 8:
	case 16:
	case 32:
	    break;

	default:
	    tqFatal( "Unsupported bit depth %d\n", d );
    }

    mousePipe = TQString(QT_VFB_MOUSE_PIPE).arg(display_id);
    keyboardPipe = TQString(QT_VFB_KEYBOARD_PIPE).arg(display_id);

    unlink( mousePipe.latin1() );
    mkfifo( mousePipe.latin1(), 0666 );

    mouseFd = open( mousePipe.latin1(), O_RDWR | O_NDELAY );
    if ( mouseFd == -1 ) {
	tqFatal( "Cannot open mouse pipe" );
    }

    unlink( keyboardPipe );
    mkfifo( keyboardPipe, 0666 );
    keyboardFd = open( keyboardPipe, O_RDWR | O_NDELAY );
    if ( keyboardFd == -1 ) {
	tqFatal( "Cannot open keyboard pipe" );
    }

    key_t key = ftok( mousePipe.latin1(), 'b' );

    int bpl;
    if ( d == 1 )
	bpl = (w*d+7)/8;
    else
	bpl = ((w*actualdepth+31)/32)*4;

    int dataSize = bpl * h + sizeof( TQVFbHeader );
    shmId = shmget( key, dataSize, IPC_CREAT|0666);
    if ( shmId != -1 )
	data = (unsigned char *)shmat( shmId, 0, 0 );
    else {
	struct shmid_ds shm;
	shmctl( shmId, IPC_RMID, &shm );
	shmId = shmget( key, dataSize, IPC_CREAT|0666);
	data = (unsigned char *)shmat( shmId, 0, 0 );
    }

    if ( (long)data == -1 )
	tqFatal( "Cannot attach to shared memory" );

    hdr = (TQVFbHeader *)data;
    hdr->width = w;
    hdr->height = h;
    viewdepth = d;
    hdr->depth = actualdepth;
    hdr->linestep = bpl;
    hdr->numcols = 0;
    hdr->dataoffset = sizeof( TQVFbHeader );
    hdr->update = TQRect();

    resizeContents( w, h );

    timer = new TQTimer( this );
    connect( timer, SIGNAL(timeout()), this, SLOT(timeout()) );

    gammatable=0;
    setGamma(1.0,1.0,1.0);
    setRate( 30 );
}

TQVFbView::~TQVFbView()
{
    stopAnimation();
    sendKeyboardData( 0, 0, 0, TRUE, FALSE ); // magic die key
    delete qwslock;
    struct shmid_ds shm;
    shmdt( (char*)data );
    shmctl( shmId, IPC_RMID, &shm );
    ::close( mouseFd );
    ::close( keyboardFd );
    unlink( mousePipe );
    unlink( keyboardPipe );
}

TQSize TQVFbView::sizeHint() const
{
    int f = 2 * frameWidth();
    return TQSize( contentsWidth() + f, contentsHeight() + f );
}

void TQVFbView::setGamma(double gr, double gg, double gb)
{
    if ( viewdepth < 12 )
	return; // not implemented

    gred=gr; ggreen=gg; gblue=gb;

    switch ( viewdepth ) {
      case 12:
	rsh = 12;
	gsh = 7;
	bsh = 1;
	rmax = 15;
	gmax = 15;
	bmax = 15;
	break;
      case 16:
	rsh = 11;
	gsh = 5;
	bsh = 0;
	rmax = 31;
	gmax = 63;
	bmax = 31;
	break;
      case 32:
	rsh = 16;
	gsh = 8;
	bsh = 0;
	rmax = 255;
	gmax = 255;
	bmax = 255;
    }
    int mm = TQMAX(rmax,TQMAX(gmax,bmax))+1;
    if ( gammatable )
	delete [] gammatable;
    gammatable = new TQRgb[mm];
    for (int i=0; i<mm; i++) {
	int r = int(pow(i,gr)*255/rmax);
	int g = int(pow(i,gg)*255/gmax);
	int b = int(pow(i,gb)*255/bmax);
	if ( r > 255 ) r = 255;
	if ( g > 255 ) g = 255;
	if ( b > 255 ) b = 255;
	gammatable[i] = tqRgb(r,g,b);
//tqDebug("%d: %d,%d,%d",i,r,g,b);
    }

    setDirty(rect());
}

void TQVFbView::getGamma(int i, TQRgb& rgb)
{
    if ( i > 255 ) i = 255;
    if ( i < 0 ) i = 0;
    rgb = tqRgb(tqRed(gammatable[i*rmax/255]),
               tqGreen(gammatable[i*rmax/255]),
               tqBlue(gammatable[i*rmax/255]));
}

int TQVFbView::displayId() const
{
    return displayid;
}

int TQVFbView::displayWidth() const
{
    return hdr->width;
}

int TQVFbView::displayHeight() const
{
    return hdr->height;
}

int TQVFbView::displayDepth() const
{
    return viewdepth;
}

void TQVFbView::setZoom( double z )
{
    if ( zm != z ) {
	zm = z;
	setDirty(TQRect(0,0,hdr->width,hdr->height));
	resizeContents( int(hdr->width*z), int(hdr->height*z) );
	updateGeometry();
	tqApp->sendPostedEvents();
	topLevelWidget()->adjustSize();
	drawScreen();
    }
}

void TQVFbView::setRate( int r )
{
    refreshRate = r;
    timer->start( 1000/r );
}

#ifndef Q_WS_QWS
// Get the name of the directory where TQt/Embedded temporary data should
// live.
static TQString qws_dataDir()
{
    TQString username = "unknown";
    const char *logname = getenv("LOGNAME");
    if ( logname )
	username = logname;

    TQString dataDir = "/tmp/qtembedded-" + username;
    if ( mkdir( dataDir.latin1(), 0700 ) ) {
	if ( errno != EEXIST ) {
	    tqFatal( "Cannot create TQt/Embedded data directory: %s",
		    dataDir.latin1() );
	}
    }

    struct stat buf;
    if ( lstat( dataDir.latin1(), &buf ) )
	tqFatal( "stat failed for TQt/Embedded data directory: %s",
		dataDir.latin1() );

    if ( !S_ISDIR( buf.st_mode ) )
	tqFatal( "%s is not a directory", dataDir.latin1() );

    if ( buf.st_uid != getuid() )
	tqFatal( "TQt/Embedded data directory is not owned by user %u",
		getuid() );

    if ( (buf.st_mode & 0677) != 0600 )
	tqFatal( "TQt/Embedded data directory has incorrect permissions: %s",
		dataDir.latin1() );

    dataDir += "/";

    return dataDir;
}
#endif

void TQVFbView::initLock()
{
    TQString username = "unknown";
    const char *logname = getenv("LOGNAME");
    if ( logname )
	username = logname;
    qwslock = new TQLock(qws_dataDir() + TQString( TQTE_PIPE ).arg( displayid ),
			'd', TRUE);
}

void TQVFbView::lock()
{
    if ( !qwslock )
	initLock();
    qwslock->lock(TQLock::Read);
}

void TQVFbView::unlock()
{
    if ( qwslock )
	qwslock->unlock();
}

void TQVFbView::sendMouseData( const TQPoint &pos, int buttons )
{
    write( mouseFd, &pos, sizeof( TQPoint ) );
    write( mouseFd, &buttons, sizeof( int ) );
}

void TQVFbView::sendKeyboardData( int unicode, int keycode, int modifiers,
				 bool press, bool repeat )
{
    TQVFbKeyData kd;
    kd.unicode = unicode | (keycode << 16);
    kd.modifiers = modifiers;
    kd.press = press;
    kd.repeat = repeat;
    write( keyboardFd, &kd, sizeof( TQVFbKeyData ) );
}

void TQVFbView::timeout()
{
    lock();
    if ( animation ) {
	    TQRect r( hdr->update );
	    r = r.intersect( TQRect(0, 0, hdr->width, hdr->height ) );
	    if ( r.isEmpty() ) {
		animation->appendBlankFrame();
	    } else {
		int l;
		TQImage img = getBuffer( r, l );
		animation->appendFrame(img,TQPoint(r.x(),r.y()));
	    }
    }
    if ( hdr->dirty ) {
	drawScreen();
    }
    unlock();
}

TQImage TQVFbView::getBuffer( const TQRect &r, int &leading ) const
{
    switch ( viewdepth ) {
      case 12:
      case 16: {
	static unsigned char *imgData = 0;
	if ( !imgData ) {
	    int bpl = ((hdr->width*32+31)/32)*4;
	    imgData = new unsigned char [ bpl * hdr->height ];
	}
	TQImage img( imgData, r.width(), r.height(), 32, 0, 0, TQImage::IgnoreEndian );
	const int rsh = viewdepth == 12 ? 12 : 11;
	const int gsh = viewdepth == 12 ? 7 : 5;
	const int bsh = viewdepth == 12 ? 1 : 0;
	const int rmax = viewdepth == 12 ? 15 : 31;
	const int gmax = viewdepth == 12 ? 15 : 63;
	const int bmax = viewdepth == 12 ? 15 : 31;
	for ( int row = 0; row < r.height(); row++ ) {
	    TQRgb *dptr = (TQRgb*)img.scanLine( row );
	    ushort *sptr = (ushort*)(data + hdr->dataoffset + (r.y()+row)*hdr->linestep);
	    sptr += r.x();
#ifdef QT_QWS_REVERSE_BYTE_ENDIANNESS
	    for ( int col=0; col < r.width()/2; col++ ) {
#else
	    for ( int col=0; col < r.width(); col++ ) {
#endif
		ushort s = *sptr++;
#ifdef QT_QWS_REVERSE_BYTE_ENDIANNESS
		ushort s2 = *sptr++;
		*dptr++ = tqRgb(tqRed(gammatable[(s2>>rsh)&rmax]),tqGreen(gammatable[(s2>>gsh)&gmax]),tqBlue(gammatable[(s2>>bsh)&bmax]));
#endif
		*dptr++ = tqRgb(tqRed(gammatable[(s>>rsh)&rmax]),tqGreen(gammatable[(s>>gsh)&gmax]),tqBlue(gammatable[(s>>bsh)&bmax]));
		//*dptr++ = tqRgb(((s>>rsh)&rmax)*255/rmax,((s>>gsh)&gmax)*255/gmax,((s>>bsh)&bmax)*255/bmax);
	    }
	}
	leading = 0;
	return img;
      }
      case 4: {
	static unsigned char *imgData = 0;
	if ( !imgData ) {
	    int bpl = ((hdr->width*8+31)/32)*4;
	    imgData = new unsigned char [ bpl * hdr->height ];
	}
	TQImage img( imgData, r.width(), r.height(), 8, hdr->clut, 16,
		    TQImage::IgnoreEndian );
	for ( int row = 0; row < r.height(); row++ ) {
	    unsigned char *dptr = img.scanLine( row );
	    unsigned char *sptr = data + hdr->dataoffset + (r.y()+row)*hdr->linestep;
	    sptr += r.x()/2;
	    int col = 0;
#ifdef QT_QWS_EXPERIMENTAL_REVERSE_BIT_ENDIANNESS
	    if ( r.x() & 1 ) {
		*dptr++ = *sptr++ & 0x0f;
		col++;
	    }
	    for ( ; col < r.width()-1; col+=2 ) {
		unsigned char s = *sptr++;
		*dptr++ = s >> 4;
		*dptr++ = s & 0x0f;
	    }
	    if ( !(r.right() & 1) )
		*dptr = *sptr >> 4;
#else
	    if ( r.x() & 1 ) {
		*dptr++ = *sptr++ >> 4;
		col++;
	    }
	    for ( ; col < r.width()-1; col+=2 ) {
		unsigned char s = *sptr++;
		*dptr++ = s & 0x0f;
		*dptr++ = s >> 4;
	    }
	    if ( !(r.right() & 1) )
		*dptr = *sptr & 0x0f;
#endif
	}
	leading = 0;
	return img;
      }
      case 32: {
	leading = r.x();
	return TQImage( data + hdr->dataoffset + r.y() * hdr->linestep,
		    hdr->width, r.height(), hdr->depth, 0,
		    0, TQImage::LittleEndian );
      }
      case 8: {
	leading = r.x();
	return TQImage( data + hdr->dataoffset + r.y() * hdr->linestep,
		    hdr->width, r.height(), hdr->depth, hdr->clut,
		    256, TQImage::LittleEndian );
      }
      case 1: {
	leading = r.x();
	return TQImage( data + hdr->dataoffset + r.y() * hdr->linestep,
		    hdr->width, r.height(), hdr->depth, hdr->clut,
#ifndef QT_QWS_EXPERIMENTAL_REVERSE_BIT_ENDIANNESS
		    0, TQImage::LittleEndian );
#else
		    0, TQImage::BigEndian );
#endif
      }
    }
    return TQImage();
}

void TQVFbView::drawScreen()
{
    TQPainter p( viewport() );

    p.translate( -contentsX(), -contentsY() );

    lock();
    TQRect r( hdr->update );
    hdr->dirty = FALSE;
    hdr->update = TQRect();
    // tqDebug( "update %d, %d, %dx%d", r.y(), r.x(), r.width(), r.height() );
    r = r.intersect( TQRect(0, 0, hdr->width, hdr->height ) );
    if ( !r.isEmpty() )  {
	if ( int(zm) != zm ) {
	    r.rLeft() = int(int(r.left()*zm)/zm);
	    r.rTop() = int(int(r.top()*zm)/zm);
	    r.rRight() = int(int(r.right()*zm+zm+0.0000001)/zm+1.9999);
	    r.rBottom() = int(int(r.bottom()*zm+zm+0.0000001)/zm+1.9999);
	    r.rRight() = TQMIN(r.right(),hdr->width-1);
	    r.rBottom() = TQMIN(r.bottom(),hdr->height-1);
	}
	int leading;
	TQImage img( getBuffer( r, leading ) );
	TQPixmap pm;
	if ( zm == 1 ) {
	    pm.convertFromImage( img );
	} else if ( int(zm) == zm ) {
	    TQWMatrix m;
	    m.scale(zm,zm);
	    pm.convertFromImage( img );
	    pm = pm.xForm(m);
	} else {
	    pm.convertFromImage( img.smoothScale(int(img.width()*zm),int(img.height()*zm)) );
	}
	unlock();
	p.setPen( black );
	p.setBrush( white );
	p.drawPixmap( int(r.x()*zm), int(r.y()*zm), pm,
			int(leading*zm), 0, pm.width(), pm.height() );
    } else {
	unlock();
    }
}

bool TQVFbView::eventFilter( TQObject *obj, TQEvent *e )
{
    if ( obj == viewport() &&
	 (e->type() == TQEvent::FocusIn || e->type() == TQEvent::FocusOut) )
	return TRUE;

    return TQScrollView::eventFilter( obj, e );
}

void TQVFbView::viewportPaintEvent( TQPaintEvent *pe )
{
    TQRect r( pe->rect() );
    r.moveBy( contentsX(), contentsY() );
    r = TQRect(int(r.x()/zm),int(r.y()/zm),
	    int(r.width()/zm)+1,int(r.height()/zm)+1);
    setDirty(r);
    drawScreen();
}

void TQVFbView::setDirty( const TQRect& r )
{
    lock();
    hdr->update |= r;
    hdr->dirty = TRUE;
    unlock();
}

void TQVFbView::contentsMousePressEvent( TQMouseEvent *e )
{
    sendMouseData( e->pos()/zm, e->stateAfter() );
}

void TQVFbView::contentsMouseDoubleClickEvent( TQMouseEvent *e )
{
    sendMouseData( e->pos()/zm, e->stateAfter() );
}

void TQVFbView::contentsMouseReleaseEvent( TQMouseEvent *e )
{
    sendMouseData( e->pos()/zm, e->stateAfter() );
}

void TQVFbView::contentsMouseMoveEvent( TQMouseEvent *e )
{
    if ( !emulateTouchscreen || (e->state() & MouseButtonMask ) )
	sendMouseData( e->pos()/zm, e->state() );
}



void TQVFbView::keyPressEvent( TQKeyEvent *e )
{
    sendKeyboardData(e->text()[0].unicode(), e->key(),
		     e->stateAfter()&(ShiftButton|ControlButton|AltButton),
		     TRUE, e->isAutoRepeat());
}

void TQVFbView::keyReleaseEvent( TQKeyEvent *e )
{
    sendKeyboardData(e->ascii(), e->key(),
		     e->stateAfter()&(ShiftButton|ControlButton|AltButton),
		     FALSE, e->isAutoRepeat());
}


TQImage TQVFbView::image() const
{
    ((TQVFbView*)this)->lock();
    int l;
    TQImage r = getBuffer( TQRect(0, 0, hdr->width, hdr->height), l ).copy();
    ((TQVFbView*)this)->unlock();
    return r;
}

void TQVFbView::startAnimation( const TQString& filename )
{
    delete animation;
    animation = new TQAnimationWriter(filename,"MNG");
    animation->setFrameRate(refreshRate);
    animation->appendFrame(TQImage(data + hdr->dataoffset,
                hdr->width, hdr->height, hdr->depth, hdr->clut,
                256, TQImage::LittleEndian));
}

void TQVFbView::stopAnimation()
{
    delete animation;
    animation = 0;
}

void TQVFbView::setTouchscreenEmulation( bool b )
{
    emulateTouchscreen = b;
}