-
Notifications
You must be signed in to change notification settings - Fork 30
/
ObSysInnerLib.cpp
1431 lines (1239 loc) · 34.2 KB
/
ObSysInnerLib.cpp
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Oberon parser/compiler library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "ObSysInnerLib.h"
#include <lua.hpp>
#include <LjTools/Engine2.h>
#include <QPainter>
#include <QKeyEvent>
#include <QDir>
#include <QCoreApplication>
#include <QtDebug>
#include <QDateTime>
using namespace Ob;
static QtDisplay* s_disp = 0;
static QtDisplay::IdleHandler s_ih = 0;
QtDisplay*QtDisplay::inst()
{
if( s_disp == 0 )
s_disp = new QtDisplay();
return s_disp;
}
int QtDisplay::mapToQt(int yOb)
{
return Height - yOb - 1;
}
int QtDisplay::mapToOb(int yQt)
{
return Height - yQt - 1; // yQt runs from zero to Height - 1; yOb zero is therefore at Height - 1
}
void QtDisplay::paintEvent(QPaintEvent*)
{
QPainter p(this);
p.drawImage(0,0,d_img);
}
void QtDisplay::timerEvent(QTimerEvent*)
{
if( idleHandler != LUA_REFNIL && !Lua::Engine2::getInst()->isExecuting() )
{
lua_State* L = Lua::Engine2::getInst()->getCtx();
lua_getref(L, idleHandler);
Lua::Engine2::getInst()->runFunction(0,0);
}
}
void QtDisplay::mouseMoveEvent(QMouseEvent* e)
{
mapOb(e);
if( d_lock == 0 )
dispatchMouse(d_keys, d_xOb, d_yOb );
}
void QtDisplay::mousePressEvent(QMouseEvent* e)
{
if( mapOb(e) && d_lock == 0 )
dispatchMouse(d_keys, d_xOb, d_yOb );
}
void QtDisplay::mouseReleaseEvent(QMouseEvent* e)
{
if( mapOb(e) && d_lock == 0 )
dispatchMouse(d_keys, d_xOb, d_yOb );
}
void QtDisplay::keyPressEvent(QKeyEvent* e)
{
if( charHandler != LUA_REFNIL && !Lua::Engine2::getInst()->isExecuting() )
{
lua_State* L = Lua::Engine2::getInst()->getCtx();
lua_getref(L, charHandler);
_String* str = LjLib::strCreate(L, 2);
str->string[0] = e->text().toLatin1()[0];
Lua::Engine2::getInst()->runFunction(1,0);
}
}
void QtDisplay::keyReleaseEvent(QKeyEvent*)
{
// NOP
}
bool QtDisplay::mapOb(QMouseEvent* p)
{
if( !rect().contains(p->pos()) )
return false;
const int x = p->pos().x();
const int y = mapToOb(p->pos().y());
std::bitset<32> bits;
bits.set( 2, p->buttons() & Qt::LeftButton );
bits.set( 1, p->buttons() & Qt::MidButton );
if( p->modifiers() == Qt::ControlModifier && ( p->buttons() & Qt::LeftButton ) )
{
bits.set( 1, true ); // mid button == CTRL + left button
bits.set( 2, false );
}
bits.set( 0, p->buttons() & Qt::RightButton );
const bool modified = d_xOb != x || d_yOb != y || d_keys.bits != bits;
d_xOb = x;
d_yOb = y;
d_keys.bits = bits;
return modified;
}
void QtDisplay::dispatchMouse(const _Set& keys, int x, int y)
{
if( mouseHandler != LUA_REFNIL && !Lua::Engine2::getInst()->isExecuting() )
{
lua_State* L = Lua::Engine2::getInst()->getCtx();
lua_getref(L, mouseHandler);
_Set* s = LjLib::setCreate(L);
s->bits = keys.bits;
lua_pushinteger(L,x);
lua_pushinteger(L,y);
Lua::Engine2::getInst()->runFunction(3,0);
}
}
QtDisplay::QtDisplay():d_img(Width,Height,QImage::Format_Mono),d_lock(0)
{
setAttribute(Qt::WA_DeleteOnClose);
d_img.fill(0);
arrow = QByteArray::fromHex("0F0F 0060 0070 0038 001C 000E 0007 8003 C101 E300 7700 3F00 1F00 3F00 7F00 FF00");
star = QByteArray::fromHex("0F0F 8000 8220 8410 8808 9004 A002 C001 7F7F C001 A002 9004 8808 8410 8220 8000");
hook = QByteArray::fromHex("0C0C 070F 8707 C703 E701 F700 7F00 3F00 1F00 0F00 0700 0300 01");
updown = QByteArray::fromHex("080E 183C 7EFF 1818 1818 1818 FF7E3C18");
block = QByteArray::fromHex("0808 FFFF C3C3 C3C3 FFFF");
clock = 0;
start = QDateTime::currentDateTime();
mouseHandler = LUA_REFNIL;
charHandler = LUA_REFNIL;
idleHandler = LUA_REFNIL;
setFixedSize(Width,Height);
show();
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
setCursor(Qt::BlankCursor);
startTimer(30); // idle timer
}
QtDisplay::~QtDisplay()
{
lua_unref( Lua::Engine2::getInst()->getCtx(), charHandler );
lua_unref( Lua::Engine2::getInst()->getCtx(), mouseHandler );
lua_unref( Lua::Engine2::getInst()->getCtx(), idleHandler );
s_disp = 0;
}
/*********************** Tools ************************************/
static void installRecord( lua_State* L, const char* metaName, const char* recordName,
const char* moduleName, lua_CFunction gc, lua_CFunction create, lua_CFunction index = 0 )
{
if( luaL_newmetatable( L, metaName ) == 0 )
luaL_error( L, "metatable '%s' already registered", metaName );
const int metaTable = lua_gettop(L);
lua_newtable(L);
const int pubMeta = lua_gettop(L);
lua_pushliteral(L, "__metatable" );
lua_pushvalue(L,pubMeta);
lua_rawset(L, metaTable); // bytecode running getmetatable() gets this table
lua_getglobal(L, moduleName );
const int module = lua_gettop(L);
lua_pushstring(L,recordName);
lua_pushvalue(L,pubMeta);
lua_rawset(L, module);
lua_pushliteral(L, "__new");
lua_pushcfunction(L, create);
lua_rawset(L, pubMeta);
if( index )
{
lua_pushliteral(L, "__index");
lua_pushcfunction(L , index );
lua_rawset(L, metaTable);
}
lua_pushliteral(L, "__gc");
lua_pushcfunction(L , gc );
lua_rawset(L, metaTable);
#ifdef _DEBUG
lua_pushliteral(L,"__classname");
lua_pushstring(L,recordName);
lua_rawset(L,metaTable);
#endif
lua_pop(L, 3); // metaTable, pubMeta, module
}
static int DIV(int a, int b) // Source: http://lists.inf.ethz.ch/pipermail/oberon/2019/013353.html
{
if (a < 0)
return (a - b + 1) / b;
else
return a / b;
}
static int MOD(int a, int b) // Source: http://lists.inf.ethz.ch/pipermail/oberon/2019/013353.html
{
if (a < 0)
return (b - 1) + ((a - b + 1)) % b;
else
return a % b;
}
static int ASR(int x, int n) { return ((qint64)(x)>>(n)); }
static int ROR(int x, int n) { return ((qint64)(x)>>(n)); } // TODO: stimmt das?
struct BitStream
{
quint8 d_byte, d_bit;
QByteArray d_buf;
BitStream( const QByteArray& buf ):d_buf(buf),d_byte(0),d_bit(0){}
bool next()
{
quint8 cur = ( d_byte < d_buf.size() ? d_buf[d_byte] : 0 );
quint8 res = cur & ( 1 << d_bit );
d_bit++;
if( d_bit > 7 )
{
d_byte++;
d_bit = 0;
}
return res;
}
bool eof() const { return d_byte >= d_buf.size(); }
};
static QImage patternToImage( const QByteArray& pat )
{
Q_ASSERT( pat.size() >= 2 );
const int w = int(pat[0]);
const int h = int(pat[1]);
const int wBytes = ( w + 7 ) / 8;
const int wSpare = wBytes * 8 - w;
QImage img( w, h, QImage::Format_Mono );
BitStream bs(pat.mid(2));
for( int y = 0; y < h; y++ )
{
for( int x = 0; x < w; x++ )
img.setPixel(x,y, bs.next() );
for( int i = 0; i < wSpare; i++ )
bs.next();
}
return img.mirrored();
}
static int Generic_new(lua_State* L)
{
lua_newtable(L);
const int top = lua_gettop(L);
lua_pushvalue(L, lua_upvalueindex(1) );
Q_ASSERT( lua_type(L, -1 ) == LUA_TTABLE );
lua_setmetatable(L,top);
return 1;
}
/*********************** Module File ******************************/
#define FileDesc_METANAME "ObnFileDesc"
FileDesc* FileDesc::check(lua_State *L, int narg, bool nilAllowed )
{
if( nilAllowed && lua_isnil(L,narg) )
return 0;
return static_cast<FileDesc*>( luaL_checkudata( L, narg, FileDesc_METANAME ) );
}
FileDesc* FileDesc::create(lua_State* L)
{
void* buf = lua_newuserdata( L, sizeof(FileDesc) );
FileDesc* s = ::new( buf ) FileDesc();
luaL_getmetatable( L, FileDesc_METANAME );
if( !lua_istable(L, -1 ) )
luaL_error( L, "internal error: no meta table for '%s'", FileDesc_METANAME );
lua_setmetatable( L, -2 );
return s;
}
int FileDesc::_new(lua_State* L)
{
create(L);
return 1;
}
int FileDesc::_gc(lua_State* L)
{
FileDesc* s = check(L,1);
s->~FileDesc(); // call destructor
return 0;
}
static QString getFileSystemPath(lua_State* L)
{
lua_getglobal(L,"fileSystemPath");
const QByteArray path = lua_tostring(L,-1);
lua_pop(L,1);
if( !path.isEmpty() )
return path;
else
return QDir::currentPath().toUtf8();
}
int FileDesc::Old(lua_State* L)
{
_String* name = LjLib::strCheck(L, 1);
QDir dir( getFileSystemPath(L) );
QFile f( dir.absoluteFilePath(name->string.c_str()) );
if( f.exists() )
{
f.open(QIODevice::ReadOnly);
FileDesc* res = FileDesc::create(L);
res->d_name = name->string.c_str();
res->d_buf.setData(f.readAll());
res->d_buf.open( QIODevice::ReadWrite );
}else
lua_pushnil(L);
return 1;
}
int FileDesc::New(lua_State* L)
{
FileDesc* res = create(L);
_String* name = LjLib::strCheck(L, 1);
res->d_name = name->string.c_str();
res->d_buf.open( QIODevice::ReadWrite );
return 1;
}
int FileDesc::Register(lua_State* L)
{
FileDesc* f = check(L,1);
QDir dir(getFileSystemPath(L));
QFile out( dir.absoluteFilePath(f->d_name.data()) );
out.open(QIODevice::WriteOnly);
f->d_buf.close();
out.write(f->d_buf.data());
f->d_buf.open(QIODevice::ReadWrite );
return 0;
}
int FileDesc::Delete(lua_State* L)
{
_String* name = LjLib::strCheck(L, 1);
QDir dir(getFileSystemPath(L));
dir.remove(name->string.c_str());
int res = 0;
lua_pushinteger(L,res);
return 1;
}
int FileDesc::Rename(lua_State* L)
{
_String* old = LjLib::strCheck(L, 1);
_String* new_ = LjLib::strCheck(L, 2);
int res = lua_tointeger(L,3);
QDir dir(getFileSystemPath(L));
if( !QFileInfo(dir.absoluteFilePath(old->string.c_str())).exists() )
{
res = 2;
lua_pushinteger(L,res);
return 1;
}
QFileInfo info(dir.absoluteFilePath(new_->string.c_str()));
if( info.exists() )
{
dir.remove(new_->string.c_str());
res = 1;
if( !dir.rename(old->string.c_str(),new_->string.c_str()) )
res = 3;
}else
{
res = 0;
if( !dir.rename(old->string.c_str(),new_->string.c_str()) )
res = 3;
}
lua_pushinteger(L,res);
return 1;
}
int FileDesc::Length(lua_State* L)
{
FileDesc* f = check(L,1);
lua_pushinteger(L, f->d_buf.size() );
return 1;
}
void FileDesc::install(lua_State* L)
{
installRecord(L,FileDesc_METANAME,"FileDesc","Files",_gc, _new);
}
#define Rider_METANAME "ObnRider"
Rider* Rider::check(lua_State *L, int narg)
{
#ifdef _DEBUG_
lua_getmetatable(L,narg);
lua_getfield(L,-1,"__classname");
qDebug() << "class" << lua_tostring(L,-1);
lua_pop(L,2);
#endif
return static_cast<Rider*>( luaL_checkudata( L, narg, Rider_METANAME ) );
}
int Rider::_new(lua_State* L)
{
void* buf = lua_newuserdata( L, sizeof(Rider) );
Rider* s = ::new( buf ) Rider();
s->eof = false;
s->res = 0;
s->d_fileRef = LUA_REFNIL;
luaL_getmetatable( L, Rider_METANAME );
if( !lua_istable(L, -1 ) )
luaL_error( L, "internal error: no meta table for '%s'", Rider_METANAME );
lua_setmetatable( L, -2 );
return 1;
}
int Rider::_gc(lua_State* L)
{
Rider* s = check(L,1);
lua_unref(L, s->d_fileRef );
s->~Rider(); // call destructor
return 0;
}
int Rider::_index(lua_State* L)
{
Rider* s = check(L,1);
QByteArray name = lua_tostring(L,2);
if( name == "eof" )
lua_pushboolean(L,s->eof);
else if( name == "res" )
lua_pushinteger(L,s->res);
else
lua_pushnil(L);
return 1;
}
int Rider::Set(lua_State* L)
{
Rider* r = check(L,1);
FileDesc* f = FileDesc::check(L,2,true);
int pos = luaL_checkinteger(L,3);
r->eof = false;
r->res = 0;
r->d_file = f;
if( f == 0 )
{
lua_unref(L,r->d_fileRef);
r->d_fileRef = LUA_REFNIL;
lua_pushvalue(L,1);
return 1;
}
Q_ASSERT( r->d_file->d_buf.isOpen() );
if( pos < 0 )
pos = 0;
r->d_pos = pos;
lua_pushvalue(L,2);
r->d_fileRef = lua_ref(L,1);
lua_pushvalue(L,1);
return 1;
}
int Rider::Pos(lua_State* L)
{
Rider* r = check(L,1);
lua_pushinteger( L,r->d_pos );
lua_pushvalue(L,1);
return 2;
}
int Rider::Base(lua_State* L)
{
Rider* r = check(L,1);
lua_rawgeti(L, LUA_REGISTRYINDEX, r->d_fileRef);
lua_pushvalue(L,1);
return 2;
}
int Rider::ReadByte(lua_State* L)
{
Rider* r = check(L,1);
quint8 x;
r->ReadByte(x);
lua_pushvalue(L,1);
lua_pushinteger(L,x);
return 2;
}
void Rider::ReadByte(quint8& x)
{
eof = false;
res = 0;
Q_ASSERT( d_file );
d_file->d_buf.seek( d_pos );
if( d_file->d_buf.atEnd() )
{
eof = true;
x = 0;
return;
}
if( !d_file->d_buf.getChar( (char*)&x ) )
res = 1; // num of bytes not read
d_pos = d_file->d_buf.pos();
}
int Rider::Read(lua_State* L)
{
Rider* r = check(L,1);
quint8 x;
r->ReadByte(x);
lua_pushvalue(L,1);
_String* s2 = LjLib::strCreate(L, 2);
s2->string[0] = x;
return 2;
}
int Rider::ReadInt(lua_State* L)
{
Rider* r = check(L,1);
quint8 x0, x1, x2, x3;
r->ReadByte(x0); r->ReadByte(x1); r->ReadByte(x2); r->ReadByte(x3);
int x = ((x3 * 0x100 + x2) * 0x100 + x1) * 0x100 + x0;
lua_pushvalue(L,1);
lua_pushinteger(L,x);
return 2;
}
int Rider::ReadNum(lua_State* L)
{
Rider* r = check(L,1);
int n = 32;
int y = 0;
quint8 b;
r->ReadByte(b);
while( b >= 0x80 )
{
y = ROR( y + b-0x80, 7);
n -= 7;
r->ReadByte(b);
}
int x;
if( n <= 4 )
x = ROR( MOD(y + b, 0x10), 4);
else
x = ASR( ROR(y + b, 7), n-7 );
lua_pushvalue(L,1);
lua_pushinteger(L,x);
return 2;
}
int Rider::ReadString(lua_State* L)
{
Rider* r = check(L,1);
_String* x = LjLib::strCheck(L, 2);
int i = 0;
quint8 ch;
r->ReadByte(ch);
while( ch != 0 )
{
if( i < x->string.size()-1 )
{
x->string[i] = ch; i++;
}
r->ReadByte(ch);
}
x->string[i] = 0;
lua_pushvalue(L,1);
lua_pushvalue(L,2);
return 2;
}
void Rider::WriteByte(quint8 x)
{
Q_ASSERT( d_file );
d_file->d_buf.seek(d_pos);
if( !d_file->d_buf.putChar((char)x) )
res++;
d_pos = d_file->d_buf.pos();
}
int Rider::WriteByte(lua_State* L)
{
Rider* r = check(L,1);
const int x = luaL_checkinteger(L,2);
r->res = 0;
r->WriteByte(x);
lua_pushvalue(L,1);
return 1;
}
int Rider::Write(lua_State* L)
{
Rider* r = check(L,1);
std::string ch;
if( lua_type(L,2) == LUA_TSTRING )
ch = lua_tostring(L,2);
else
ch = LjLib::strCheck(L, 2)->string;
r->res = 0;
if( ch.size() >= 1 )
r->WriteByte(ch[0]);
lua_pushvalue(L,1);
return 1;
}
int Rider::WriteInt(lua_State* L)
{
Rider* r = check(L,1);
const int x = luaL_checkinteger(L,2);
r->res = 0;
r->WriteByte(MOD(x,0x100));
r->WriteByte(MOD(DIV(x,0x100),0x100));
r->WriteByte(MOD(DIV(x,0x10000),0x100));
r->WriteByte(MOD(DIV(x,0x1000000),0x100));
lua_pushvalue(L,1);
return 1;
}
int Rider::WriteNum(lua_State* L)
{
Rider* r = check(L,1);
int x = luaL_checkinteger(L,2);
while( x < -0x40 || x >= 0x40 )
{
r->WriteByte( MOD( x, 0x80 + 0x80) );
x = ASR(x, 7);
}
r->WriteByte( MOD( x, 0x80 ) );
lua_pushvalue(L,1);
return 1;
}
int Rider::WriteString(lua_State* L)
{
Rider* r = check(L,1);
_String* x = LjLib::strCheck(L, 2);
r->res = 0;
int i = 0;
quint8 ch;
do
{
ch = x->string[i];
r->WriteByte(ch);
i++;
} while( !( ch == 0x0 ) );
lua_pushvalue(L,1);
return 1;
}
int Rider::RestoreList(lua_State* L)
{
// NOP ?
return 0;
}
void Rider::install(lua_State* L)
{
installRecord(L,Rider_METANAME,"Rider","Files",_gc,_new,_index);
}
static const luaL_Reg Files_Reg[] =
{
{ "RestoreList", Rider::RestoreList },
{ "WriteString", Rider::WriteString },
{ "WriteInt", Rider::WriteInt },
{ "WriteNum", Rider::WriteNum },
{ "Write", Rider::Write },
{ "WriteByte", Rider::WriteByte },
{ "ReadString", Rider::ReadString },
{ "ReadNum", Rider::ReadNum },
{ "ReadInt", Rider::ReadInt },
{ "Read", Rider::Read },
{ "ReadByte", Rider::ReadByte },
{ "Base", Rider::Base },
{ "Pos", Rider::Pos },
{ "Set", Rider::Set },
{ "Length", FileDesc::Length },
{ "Rename", FileDesc::Rename },
{ "Delete", FileDesc::Delete },
{ "Register", FileDesc::Register },
{ "Old", FileDesc::Old },
{ "New", FileDesc::New },
{ NULL, NULL }
};
int SysInnerLib::installFiles(lua_State* L)
{
luaL_register( L, "Files", Files_Reg ); // must come first because module is used in installs
FileDesc::install(L);
Rider::install(L);
return 1;
}
/***************** MODULE FileDir ********************************/
static int FileDir_Enumerate(lua_State* L)
{
// prefix: ARRAY OF CHAR; proc: EntryHandler;
// TODO: not sure yet how this is supposed to work; currently just return all files
_String* prefix = LjLib::strCheck(L,1);
qDebug() << "FileDir_Enumerate prefix:" << prefix->string.c_str();
luaL_checktype( L,2,LUA_TFUNCTION );
lua_newtable(L);
const int fh = lua_gettop(L);
QDir dir( getFileSystemPath( L ) );
QFileInfoList files = dir.entryInfoList( QDir::Files | QDir::Readable | QDir::Writable );
bool run = true;
foreach( const QFileInfo& f, files )
{
lua_pushliteral(L,"leng");
lua_pushinteger(L,f.size());
lua_rawset(L,fh);
lua_pushliteral(L,"clock");
lua_pushinteger(L, f.created().toTime_t());
lua_rawset(L,fh);
lua_pushvalue(L,2);
_String* name = LjLib::strCreate(L, 32);
name->assign( f.fileName().left(31).toUtf8() );
lua_pushvalue(L,fh);
lua_pushboolean(L,run);
lua_call(L, 3, 1 );
run = lua_toboolean(L,-1);
lua_pop(L,1);
if( !run )
break;
}
lua_pop(L,1); // fh
return 0;
}
static const luaL_Reg FileDir_Reg[] =
{
{ "Enumerate", FileDir_Enumerate },
{ NULL, NULL }
};
int SysInnerLib::installFileDir(lua_State* L)
{
luaL_register( L, "FileDir", FileDir_Reg );
return 1;
}
/***************** MODULE Kernel ********************************/
static int Kernel_Time(lua_State* L)
{
lua_pushinteger( L, QtDisplay::inst()->start.msecsTo(QDateTime::currentDateTime()) );
return 1;
}
static int Kernel_Clock(lua_State* L)
{
lua_pushinteger( L, QtDisplay::inst()->clock );
return 1;
}
static int Kernel_SetClock(lua_State* L)
{
const int dt = luaL_checkinteger(L,1);
QtDisplay::inst()->clock = dt;
return 0;
}
static const luaL_Reg Kernel_Reg[] =
{
{ "Time", Kernel_Time },
{ "Clock", Kernel_Clock },
{ "SetClock", Kernel_SetClock },
{ NULL, NULL }
};
int SysInnerLib::installKernel(lua_State* L)
{
luaL_register( L, "Kernel", Kernel_Reg );
return 1;
}
/***************** MODULE Display ********************************/
static int Display_Width(lua_State* L)
{
lua_pushinteger(L, QtDisplay::Width );
return 1;
}
static int Display_Height(lua_State* L)
{
lua_pushinteger(L, QtDisplay::Height );
return 1;
}
static int Display_arrow(lua_State* L)
{
lua_pushlstring(L, QtDisplay::inst()->arrow.constData(), QtDisplay::inst()->arrow.size() );
return 1;
}
static int Display_star(lua_State* L)
{
lua_pushlstring(L, QtDisplay::inst()->star.constData(), QtDisplay::inst()->star.size() );
return 1;
}
static int Display_hook(lua_State* L)
{
lua_pushlstring(L, QtDisplay::inst()->hook.constData(), QtDisplay::inst()->hook.size() );
return 1;
}
static int Display_updown(lua_State* L)
{
lua_pushlstring(L, QtDisplay::inst()->updown.constData(), QtDisplay::inst()->updown.size() );
return 1;
}
static int Display_block(lua_State* L)
{
lua_pushlstring(L, QtDisplay::inst()->block.constData(), QtDisplay::inst()->block.size() );
return 1;
}
enum Display_Operation { Display_replace = 0, Display_paint = 1, Display_invert = 2,
Display_PatternLen = 15 * 15 / 8 + 2 };
static void setPoint( QImage& img, int x, int y, int mode, int color )
{
if( x < 0 || y < 0 || x >= img.width() || y >= img.height() )
return;
if( color > 1 )
color = 1; // RISK
const int dst = img.pixelIndex(x,y);
if( mode == Display_replace )
img.setPixel(x,y, color );
else if( mode == Display_paint )
img.setPixel(x,y, color || dst );
else if( mode == Display_invert )
img.setPixel(x,y, ( color || dst ) && !( color && dst ) );
else
Q_ASSERT(false);
}
static int Display_ReplConst(lua_State* L)
{
const int color = luaL_checkinteger(L,1);
const int x = luaL_checkinteger(L,2);
int y = luaL_checkinteger(L,3);
const int w = luaL_checkinteger(L,4);
const int h = luaL_checkinteger(L,5);
const int mode = luaL_checkinteger(L,6);
// qDebug() << "ReplConst" << color << x << y << w << h << mode;
QtDisplay* d = QtDisplay::inst();
y = QtDisplay::mapToQt(y);
if( w > 1 && h > 1 )
{
for( int i = x; i < x + w; i++ )
{
for( int j = y; j > y - h; j-- )
setPoint( d->d_img, i, j, mode, color );
}
}else if( w > 1 )
{
for( int i = x; i < x + w; i++ )
setPoint( d->d_img, i, y, mode, color );
}else if( h > 1 )
{
for( int i = y; i > ( y - h ); i-- )
setPoint( d->d_img, x, i, mode, color );
}else
setPoint( d->d_img, x, y, mode, color );
d->update();
return 0;
}
static void setPoint( QImage& img, int x, int y, int mode, int src, int color )
{
if( x < 0 || y < 0 || x >= img.width() || y >= img.height() )
return;
if( color > 1 )
color = 1; // RISK
const int dst = img.pixelIndex(x,y);
if( mode == Display_replace )
img.setPixel(x,y, src == 0 ? 0 : color );
else if( mode == Display_paint )
img.setPixel(x,y, src == 0 ? dst : color );
else if( mode == Display_invert )
img.setPixel(x,y, src == 0 ? dst : abs(color - dst) );
else
Q_ASSERT(false);
}
static int Display_Dot(lua_State* L)
{
const int color = luaL_checkinteger(L,1);
const int x = luaL_checkinteger(L,2);
int y = luaL_checkinteger(L,3);
const int mode = luaL_checkinteger(L,4);
QtDisplay* d = QtDisplay::inst();
y = QtDisplay::mapToQt(y);
setPoint( d->d_img, x, y, mode, color );
d->update();
return 0;
}
static int Display_CopyPattern(lua_State* L)
{
const int color = luaL_checkinteger(L,1);
QByteArray pattern;
switch( lua_type(L,2) )
{
case LUA_TSTRING:
{
size_t len;
const char* buf = lua_tolstring(L,2,&len);
pattern = QByteArray(buf,len);
}
break;
case LUA_TTABLE:
{
const int len = lua_objlen(L,2);
pattern.resize(len);
for( int i = 1; i <= len; i++ )
{
lua_rawgeti(L,2,i);
pattern[i-1] = lua_tointeger(L,-1);
lua_pop(L,1);
}
}
break;
default:
qWarning() << "Display_CopyPattern: invalid pattern";