ecere:gui/gfx:drivers/cocoa: Partial native Cocoa drivers implementation for Mac...
[sdk] / ecere / src / gfx / drivers / NCursesDisplayDriver.ec
1 namespace gfx::drivers;
2
3 import "instance"
4
5 #undef __BLOCKS__
6
7 #if (defined(__unix__) || defined(__APPLE__)) && !defined(__DOS__)
8
9 #define bool CursesBool
10
11 #include <curses.h>
12
13 #undef bool
14
15 import "Display"
16
17 static int CC(int color)
18 {
19    int result = 0;
20    int foreground = (color & 0x0700) >> 8;
21    int background = (color & 0x7000) >> 9;
22    int character = color & 0xFF;
23    int pair = background|foreground;
24    if(pair == 0) result |= A_REVERSE;
25    result |= COLOR_PAIR(pair);
26    if(character == 127 || character == 132 || character == 133 || character == 136 ||
27       character == 141 || character == 142 || character == 143 || character == 155 ||
28       character == 156)
29       character = 32;
30    else if(character < 32) 
31    {
32       character += 95;
33       result |= A_ALTCHARSET;
34    }
35    if(color == 0x0800)
36       result |= A_DIM; 
37    else if(color & 0x0800) 
38       result |= A_BOLD;
39    if(color & 0x8000) 
40       result |= A_BLINK;
41    result |= character;
42    return result;
43 }
44
45 class CursesDisplay : LFBDisplay
46 {
47    Box updateBox;
48 };
49
50 class NCursesDisplayDriver : DisplayDriver
51 {
52    class_property(name) = "NCurses";
53
54    bool CreateDisplaySystem(DisplaySystem displaySystem)
55    {
56       displaySystem.flags.text = true;
57       return true;
58    }
59
60    void DestroyDisplaySystem(DisplaySystem displaySystem)
61    {
62
63    }
64
65    void DestroyDisplay(Display display)
66    {
67       CursesDisplay cursesDisplay = display.driverData;
68       delete cursesDisplay.bitmap.picture;
69       ((subclass(DisplayDriver))class(LFBDisplayDriver)).DestroyDisplay(display);
70       delete cursesDisplay;
71       display.driverData = null;
72    }
73
74    void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
75    {
76
77    }
78
79    bool CreateDisplay(Display display)
80    {
81       bool result = false;
82       CursesDisplay cursesDisplay = display.driverData = CursesDisplay { };
83
84       if(cursesDisplay)
85       {
86          if(((subclass(DisplayDriver))class(LFBDisplayDriver)).CreateDisplay(display))
87          {
88             cursesDisplay.bitmap.pixelFormat = pixelFormatText;
89             result = true;
90          }
91       }
92       return result;
93    }
94
95    bool DisplaySize(Display display, int width, int height)
96    {
97       bool result = false;
98       CursesDisplay cursesDisplay = display.driverData;
99
100       delete cursesDisplay.bitmap.picture;
101
102       cursesDisplay.bitmap.picture = new0 byte[2 * width / textCellW * height / textCellW];
103       if(cursesDisplay.bitmap.picture)
104       {  
105          cursesDisplay.bitmap.stride = width / textCellW;
106          cursesDisplay.bitmap.pixelFormat = pixelFormatText;
107          cursesDisplay.bitmap.size = width / textCellW * height / textCellH;
108          display.width = width;
109          display.height = height;
110
111          ((subclass(DisplayDriver))class(LFBDisplayDriver)).DisplaySize(display, width, height);
112          result = true;
113       }
114       return result;
115    }
116
117    void DisplayPosition(Display display, int x, int y)
118    {
119
120    }
121
122    void RestorePalette(Display display)
123    {
124
125    }
126
127    void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
128    {
129
130    }
131
132    bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
133    {
134       return true;
135    }
136
137    bool GetSurface(Display display, Surface surface, int x, int y, Box clip)
138    {
139       if((surface.driverData = LFBSurface { }))
140          return ((subclass(DisplayDriver))class(LFBDisplayDriver)).GetSurface(display, surface, x, y, clip);
141       return false;
142    }
143
144    bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
145    {
146       return false;
147    }
148
149    void ReleaseSurface(Display display, Surface surface)
150    {
151       ((subclass(DisplayDriver))class(LFBDisplayDriver)).ReleaseSurface(display, surface);
152    }
153
154    void Clip(Display display, Surface surface, Box clip)
155    {
156       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Clip(display, surface, clip);
157    }
158
159    bool GrabScreen(Display display, Bitmap bitmapAddress, int x, int y, unsigned int w, unsigned int h)
160    {
161
162       return false;
163    }
164
165    void SetForeground(Display display, Surface surface, ColorAlpha color)
166    {
167       ((subclass(DisplayDriver))class(LFBDisplayDriver)).SetForeground(display, surface, color);
168    }
169
170    void SetBackground(Display display, Surface surface, ColorAlpha color)
171    {
172       ((subclass(DisplayDriver))class(LFBDisplayDriver)).SetBackground(display, surface, color);
173    }
174
175    ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
176    {
177       return 0;
178    }
179
180    void PutPixel(Display display, Surface surface,int x,int y)
181    {
182       LFBSurface lfbSurface = surface.driverData;
183       ((subclass(DisplayDriver))class(LFBDisplayDriver)).PutPixel(display, surface, x, y);
184       x /= textCellW;
185       y /= textCellH;
186       if((x<=surface.box.right)&&(y<=surface.box.bottom)&&(x>=surface.box.left)&&(y>=surface.box.top))
187       {
188
189          if(lfbSurface.opaqueText)
190          {
191             mvaddch((y+surface.offset.y),x+surface.offset.x, 
192                CC(lfbSurface.background|lfbSurface.foreground|lfbSurface.drawingChar));
193          }
194          else
195          {
196             mvaddch((y+surface.offset.y),x+surface.offset.x, 
197               CC(lfbSurface.background|lfbSurface.foreground|lfbSurface.drawingChar));
198          }
199       }
200    }
201
202    void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
203    {
204       LFBSurface lfbSurface = surface.driverData;
205       ((subclass(DisplayDriver))class(LFBDisplayDriver)).DrawLine(display, surface, x1, y1, x2, y2);
206       x1 /= textCellW;
207       x2 /= textCellW;
208       y1 /= textCellH;
209       y2 /= textCellH;
210       if(x1 == x2)
211       {
212          if(y1>y2) { int tmp = y2; y2 = y1; y1 = tmp; }
213
214          if((x1>surface.box.right)||(x1<surface.box.left))return;
215          if((y1>surface.box.bottom)||(y2<surface.box.top))return;
216          if(y1<surface.box.top)y1=surface.box.top;
217          if(y2>surface.box.bottom)y2=surface.box.bottom;
218          if(y2 < y1) return;
219
220          if(lfbSurface.opaqueText)
221          {
222             mvvline(y1+surface.offset.y,x1+surface.offset.x,
223                CC(lfbSurface.background | lfbSurface.foreground | lfbSurface.drawingChar),y2-y1+1);
224          }
225          else
226          {
227             uint16 * lfbPtr = ((uint16 *)lfbSurface.bitmap.picture) + 
228                (y1+surface.offset.y)*lfbSurface.bitmap.stride+x1+surface.offset.x;
229             int y;
230             for(y=y1;y<=y2; y++, lfbPtr += lfbSurface.bitmap.stride)
231             {
232                mvaddch(y+surface.offset.y,x1+surface.offset.x,
233                   CC((*lfbPtr & 0xF000) | lfbSurface.foreground | lfbSurface.drawingChar));
234             }
235          }
236
237       }
238       else if(y1 == y2)
239       {
240          if(x1>x2) { int tmp = x2; x2 = x1; x1 = tmp; }
241
242          if((y1>surface.box.bottom)||(y1<surface.box.top)) return;
243          if((x1>surface.box.right)||(x2<surface.box.left)) return;
244          if(x1<surface.box.left)x1=surface.box.left;
245          if(x2>surface.box.right)x2=surface.box.right;
246          if(x2 < x1) return;
247
248          if(lfbSurface.opaqueText)
249          {
250             mvhline(y1+surface.offset.y,x1+surface.offset.x,
251                CC(lfbSurface.background | lfbSurface.foreground | lfbSurface.drawingChar),x2-x1+1);
252          }
253          else
254          {
255             uint16 * lfbPtr = ((uint16 *)lfbSurface.bitmap.picture) + 
256                (y1+surface.offset.y)*display.width / textCellW+x1+surface.offset.x;
257             int x;
258             for(x=x1;x<=x2; x++, lfbPtr++)
259             {
260                mvaddch(y1+surface.offset.y,x+surface.offset.x,
261                   CC((*lfbPtr & 0xF000) | lfbSurface.foreground | lfbSurface.drawingChar));
262             }
263          }
264       }
265    }
266
267    void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
268    {
269
270    }
271
272    void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
273    {
274       LFBSurface lfbSurface = surface.driverData;
275       ((subclass(DisplayDriver))class(LFBDisplayDriver)).Area(display, surface, x1, y1, x2, y2);
276
277       x1 /= textCellW;
278       x2 /= textCellW;
279       y1 /= textCellH;
280       y2 /= textCellH;
281
282       if(x1>x2) { int tmp = x2; x2 = x1; x1 = tmp; }
283
284       if(x1<surface.box.left)  x1=surface.box.left;
285       if(x2>surface.box.right) x2=surface.box.right;
286       if(y1<surface.box.top)   y1=surface.box.top;
287       if(y2>surface.box.bottom)  y2=surface.box.bottom;
288
289       if(x2>=x1 && y2>=y1)
290       {
291          int y;
292          for(y=y1;y<=y2; y++)
293          {
294             mvhline(y+surface.offset.y,x1+surface.offset.x,
295                CC(lfbSurface.background | lfbSurface.foreground | lfbSurface.drawingChar),x2-x1+1); 
296          }
297       }
298    }
299
300    void Clear(Display display, Surface surface, ClearType flags)
301    {
302       if(flags != depthBuffer)
303          Area(display, surface, 0, 0, surface.width - 1, surface.height - 1);
304    }
305
306    bool ConvertBitmap(DisplaySystem displaySystem, Bitmap src, PixelFormat format, ColorAlpha * palette)
307    {
308       return true;
309    }
310
311    bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
312    {
313       return false;
314    }
315
316    void Blit(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
317    {
318
319    }
320
321    void Stretch(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
322    {
323
324    }
325
326    void Filter(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
327    {
328
329    }
330
331    void BlitDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
332    {
333
334    }
335
336    void StretchDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
337    {
338
339    }
340
341    void FilterDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
342    {
343
344    }
345
346    Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
347    {
348       return (void *) true;
349    }
350
351    void UnloadFont(DisplaySystem displaySystem, Font font)
352    {
353
354    }
355
356    void TextFont(Display display, Surface surface, Font font)
357    {
358    }
359
360    void TextOpacity(Display display, Surface surface, bool opaque)
361    {
362       ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextOpacity(display, surface, opaque);
363    }
364
365    void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
366    {
367       LFBSurface lfbSurface = surface.driverData;
368       int c;
369       uint16 * lfbPtr;
370
371       ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
372
373       x /= textCellW;
374       y /= textCellH;
375
376       if(y > surface.box.bottom || y < surface.box.top) 
377          return;
378       y += surface.offset.y;
379       lfbPtr = ((uint16 *)lfbSurface.bitmap.picture) + y * lfbSurface.bitmap.stride + x + surface.offset.x;
380       for(c=0; (c<len && x < surface.box.left); c++, x++, lfbPtr++);
381       for(; (c<len && x <= surface.box.right); c++, x++, lfbPtr++)
382       {
383          if(lfbSurface.opaqueText)
384             mvaddch(y,x+surface.offset.x,CC(lfbSurface.background|lfbSurface.foreground|text[c]));
385          else
386             mvaddch(y,x+surface.offset.x,CC( (*lfbPtr & 0xF000)|lfbSurface.foreground|text[c] ));
387       }
388    }
389
390    void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
391    {
392       ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
393    }
394
395    void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
396    {
397       FontExtent(display.displaySystem, null, text, len, width, height);
398    }
399
400    void DrawingChar(Display display, Surface surface, char character)
401    {
402       ((subclass(DisplayDriver))class(LFBDisplayDriver)).DrawingChar(display, surface, character);
403    }
404
405
406    void LineStipple(Display display, Surface surface, uint stipple)
407    {
408
409    }
410
411    bool Lock(Display display)
412    {
413       return true;
414    }
415
416    void Unlock(Display display)
417    {
418    }
419
420    void StartUpdate(Display display)
421    {
422
423    }
424
425    void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
426    {
427
428    }
429
430    void Update(Display display, Box updateBox)
431    {
432       CursesDisplay cursesDisplay = display.driverData;
433       curs_set(false);
434       leaveok(stdscr, (_Bool)true);
435       refresh();
436       if(updateBox == null)
437       {
438          cursesDisplay.updateBox.left = display.width;
439          cursesDisplay.updateBox.top = display.height;
440          cursesDisplay.updateBox.right = 0;
441          cursesDisplay.updateBox.bottom = 0;
442       }
443       // Log("Here in displayscreen\n");
444    }
445
446    void EndUpdate(Display display)
447    {
448
449    }
450 }
451
452 #endif