|
|
1.1 root 1: /*
2: Hatari - statusbar.c
3:
4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
6:
7: Code to draw statusbar area, floppy leds etc.
8:
9: Use like this:
10: - Before screen surface is (re-)created Statusbar_SetHeight()
11: has to be called with the new screen height. Add the returned
12: value to screen height (zero means no statusbar). After this,
13: Statusbar_GetHeight() can be used to retrieve the statusbar size
14: - After screen surface is (re-)created, call Statusbar_Init()
15: to re-initialize / re-draw the statusbar
16: - Call Statusbar_SetFloppyLed() to set floppy drive led ON/OFF,
17: or call Statusbar_EnableHDLed() to enabled HD led for a while
18: - Whenever screen is redrawn, call Statusbar_Update() to draw the
19: updated information to the statusbar (outside of screen locking)
20: - If screen redraws may be partial, Statusbar_OverlayRestore()
21: needs to be called before locking the screen for drawing and
22: Statusbar_OverlayBackup() needs to be called after screen unlocking,
23: but before calling Statusbar_Update(). These are needed for
24: hiding the overlay drive led when drive leds are turned OFF.
25: - If other information shown by Statusbar (TOS version etc) changes,
26: call Statusbar_UpdateInfo()
27:
28: TODO:
29: - re-calculate colors on each update to make sure they're
30: correct in Falcon & TT 8-bit palette modes?
31: - call Statusbar_AddMessage() from log.c?
32: */
33: const char Statusbar_fileid[] = "Hatari statusbar.c : " __DATE__ " " __TIME__;
34:
35: #include <assert.h>
36: #include "main.h"
37: #include "configuration.h"
38: #include "screenSnapShot.h"
39: #include "sdlgui.h"
40: #include "statusbar.h"
1.1.1.2 ! root 41: #include "screen.h"
1.1 root 42: #include "video.h"
43: #include "avi_record.h"
44:
45: #define DEBUG 0
46: #if DEBUG
47: #define DEBUGPRINT(x) printf x
48: #else
49: #define DEBUGPRINT(x)
50: #endif
51:
52: #define MAX_DRIVE_LEDS (DRIVE_LED_HD + 1)
53:
54: /* whether drive leds should be ON and their previous shown state */
55: static struct {
56: bool state;
57: bool oldstate;
58: Uint32 expire; /* when to disable led, valid only if >0 && state=TRUE */
59: int offset; /* led x-pos on screen */
60: } Led[MAX_DRIVE_LEDS];
61:
62: /* drive leds size & y-pos */
63: static SDL_Rect LedRect;
64:
65: /* overlay led size & pos */
66: static SDL_Rect OverlayLedRect;
67:
68: /* screen contents left under overlay led */
69: static SDL_Surface *OverlayUnderside;
70:
71: static enum {
72: OVERLAY_NONE,
73: OVERLAY_DRAWN,
74: OVERLAY_RESTORED
75: } bOverlayState;
76:
77: static SDL_Rect RecLedRect;
78: static bool bOldRecording;
79:
80: /* led colors */
81: static Uint32 LedColorOn, LedColorOff, RecColorOn, RecColorOff;
82: static Uint32 GrayBg, LedColorBg;
83:
84:
85: #define MAX_MESSAGE_LEN 33
86: typedef struct msg_item {
87: struct msg_item *next;
88: char msg[MAX_MESSAGE_LEN+1];
89: Uint32 timeout; /* msecs, zero=no timeout */
90: Uint32 expire; /* when to expire message */
91: bool shown;
92: } msg_item_t;
93:
94: static msg_item_t DefaultMessage;
95: static msg_item_t *MessageList = &DefaultMessage;
96: static SDL_Rect MessageRect;
97:
98: /* rect for both frame skip value and fast forward indicator */
99: static SDL_Rect FrameSkipsRect;
100: static int nOldFrameSkips;
101: static int bOldFastForward;
102:
103:
104: /* screen height above statusbar and height of statusbar below screen */
105: static int ScreenHeight;
106: static int StatusbarHeight;
107:
108:
109: /*-----------------------------------------------------------------------*/
110: /**
111: * Return statusbar height for given width and height
112: */
113: int Statusbar_GetHeightForSize(int width, int height)
114: {
115: if (ConfigureParams.Screen.bShowStatusbar) {
116: /* Should check the same thing as SDLGui_SetScreen()
117: * does to decide the font size.
118: */
119: if (width >= 640 && height >= (400-24)) {
120: return 24;
121: } else {
122: return 12;
123: }
124: }
125: return 0;
126: }
127:
128: /*-----------------------------------------------------------------------*/
129: /**
130: * Set screen height used for statusbar height calculation.
131: *
132: * Return height of statusbar that should be added to the screen
133: * height when screen is (re-)created, or zero if statusbar will
134: * not be shown
135: */
136: int Statusbar_SetHeight(int width, int height)
137: {
138: ScreenHeight = height;
139: StatusbarHeight = Statusbar_GetHeightForSize(width, height);
140: return StatusbarHeight;
141: }
142:
143: /*-----------------------------------------------------------------------*/
144: /**
145: * Return height of statusbar set with Statusbar_SetHeight()
146: */
147: int Statusbar_GetHeight(void)
148: {
149: return StatusbarHeight;
150: }
151:
152:
153: /*-----------------------------------------------------------------------*/
154: /**
155: * Enable HD drive led, it will be automatically disabled after a while.
156: */
157: void Statusbar_EnableHDLed(void)
158: {
159: /* leds are shown for 1/2 sec after enabling */
160: Led[DRIVE_LED_HD].expire = SDL_GetTicks() + 1000/2;
161: Led[DRIVE_LED_HD].state = true;
162: }
163:
164: /*-----------------------------------------------------------------------*/
165: /**
166: * Set given floppy drive led state, anything enabling led with this
167: * needs also to take care of disabling it.
168: */
169: void Statusbar_SetFloppyLed(drive_index_t drive, bool state)
170: {
171: assert(drive == DRIVE_LED_A || drive == DRIVE_LED_B);
172: Led[drive].state = state;
173: }
174:
175:
176: /*-----------------------------------------------------------------------*/
177: /**
178: * Set overlay led size/pos on given screen to internal Rect
179: * and free previous resources.
180: */
181: static void Statusbar_OverlayInit(const SDL_Surface *surf)
182: {
183: int h;
184: /* led size/pos needs to be re-calculated in case screen changed */
185: h = surf->h / 50;
186: OverlayLedRect.w = 2*h;
187: OverlayLedRect.h = h;
188: OverlayLedRect.x = surf->w - 5*h/2;
189: OverlayLedRect.y = h/2;
190: /* free previous restore surface if it's incompatible */
191: if (OverlayUnderside &&
192: OverlayUnderside->w == OverlayLedRect.w &&
193: OverlayUnderside->h == OverlayLedRect.h &&
194: OverlayUnderside->format->BitsPerPixel == surf->format->BitsPerPixel) {
195: SDL_FreeSurface(OverlayUnderside);
196: OverlayUnderside = NULL;
197: }
198: bOverlayState = OVERLAY_NONE;
199: }
200:
201: /*-----------------------------------------------------------------------*/
202: /**
203: * (re-)initialize statusbar internal variables for given screen surface
204: * (sizes&colors may need to be re-calculated for the new SDL surface)
205: * and draw the statusbar background.
206: */
207: void Statusbar_Init(SDL_Surface *surf)
208: {
209: msg_item_t *item;
210: SDL_Rect ledbox, sbarbox;
211: int i, fontw, fonth, offset;
212: const char *text[MAX_DRIVE_LEDS] = { "A:", "B:", "HD:" };
213:
214: assert(surf);
215:
216: /* dark green and light green for leds themselves */
217: LedColorOff = SDL_MapRGB(surf->format, 0x00, 0x40, 0x00);
218: LedColorOn = SDL_MapRGB(surf->format, 0x00, 0xe0, 0x00);
219: LedColorBg = SDL_MapRGB(surf->format, 0x00, 0x00, 0x00);
220: RecColorOff = SDL_MapRGB(surf->format, 0x40, 0x00, 0x00);
221: RecColorOn = SDL_MapRGB(surf->format, 0xe0, 0x00, 0x00);
222: GrayBg = SDL_MapRGB(surf->format, 0xc0, 0xc0, 0xc0);
223:
224: /* disable leds */
225: for (i = 0; i < MAX_DRIVE_LEDS; i++) {
226: Led[i].state = Led[i].oldstate = false;
227: Led[i].expire = 0;
228: }
229: Statusbar_OverlayInit(surf);
230:
231: /* disable statusbar if it doesn't fit to video mode */
232: if (surf->h < ScreenHeight + StatusbarHeight) {
233: StatusbarHeight = 0;
234: }
235: if (!StatusbarHeight) {
236: return;
237: }
238:
239: /* prepare fonts */
240: SDLGui_Init();
241: SDLGui_SetScreen(surf);
242: SDLGui_GetFontSize(&fontw, &fonth);
243:
244: /* video mode didn't match, need to recalculate sizes */
245: if (surf->h > ScreenHeight + StatusbarHeight) {
246: StatusbarHeight = fonth + 2;
247: /* actually statusbar vertical offset */
248: ScreenHeight = surf->h - StatusbarHeight;
249: } else {
250: assert(fonth+2 < StatusbarHeight);
251: }
252:
253: /* draw statusbar background gray so that text shows */
254: sbarbox.x = 0;
255: sbarbox.y = surf->h - StatusbarHeight;
256: sbarbox.w = surf->w;
257: sbarbox.h = StatusbarHeight;
258: SDL_FillRect(surf, &sbarbox, GrayBg);
259:
260: /* led size */
261: LedRect.w = fonth/2;
262: LedRect.h = fonth - 4;
263: LedRect.y = ScreenHeight + StatusbarHeight/2 - LedRect.h/2;
264:
265: /* black box for the leds */
266: ledbox = LedRect;
267: ledbox.y -= 1;
268: ledbox.w += 2;
269: ledbox.h += 2;
270:
271: offset = fontw;
272: MessageRect.y = LedRect.y - 2;
273: /* draw led texts and boxes + calculate box offsets */
274: for (i = 0; i < MAX_DRIVE_LEDS; i++) {
275: SDLGui_Text(offset, MessageRect.y, text[i]);
276: offset += strlen(text[i]) * fontw;
277: offset += fontw/2;
278:
279: ledbox.x = offset - 1;
280: SDL_FillRect(surf, &ledbox, LedColorBg);
281:
282: LedRect.x = offset;
283: SDL_FillRect(surf, &LedRect, LedColorOff);
284:
285: Led[i].offset = offset;
286: offset += LedRect.w + fontw;
287: }
288:
289: /* draw frameskip */
290: FrameSkipsRect.x = offset;
291: FrameSkipsRect.y = MessageRect.y;
292: SDLGui_Text(FrameSkipsRect.x, FrameSkipsRect.y, "FS:");
293: FrameSkipsRect.x += 3 * fontw + fontw/2;
294: FrameSkipsRect.w = 4 * fontw;
295: FrameSkipsRect.h = fonth;
296:
297: if(ConfigureParams.System.bFastForward) {
298: SDLGui_Text(FrameSkipsRect.x, FrameSkipsRect.y, "0 >>");
299: } else {
300: SDLGui_Text(FrameSkipsRect.x, FrameSkipsRect.y, "0");
301: }
302:
303: nOldFrameSkips = 0;
304:
305: /* intialize messages */
306: MessageRect.x = FrameSkipsRect.x + FrameSkipsRect.w + fontw;
307: MessageRect.w = MAX_MESSAGE_LEN * fontw;
308: MessageRect.h = fonth;
309: for (item = MessageList; item; item = item->next) {
310: item->shown = false;
311: }
312:
313: /* draw recording led box */
314: RecLedRect = LedRect;
315: RecLedRect.x = surf->w - fontw - RecLedRect.w;
316: ledbox.x = RecLedRect.x - 1;
317: SDLGui_Text(ledbox.x - 4*fontw - fontw/2, MessageRect.y, "REC:");
318: SDL_FillRect(surf, &ledbox, LedColorBg);
319: SDL_FillRect(surf, &RecLedRect, RecColorOff);
320: bOldRecording = false;
321:
322: /* and blit statusbar on screen */
323: SDL_UpdateRects(surf, 1, &sbarbox);
324: DEBUGPRINT(("Draw statusbar\n"));
325: }
326:
327:
328: /*-----------------------------------------------------------------------*/
329: /**
330: * Qeueue new statusbar message 'msg' to be shown for 'msecs' milliseconds
331: */
332: void Statusbar_AddMessage(const char *msg, Uint32 msecs)
333: {
334: msg_item_t *item;
335:
336: if (!ConfigureParams.Screen.bShowStatusbar) {
337: /* no sense in queuing messages that aren't shown */
338: return;
339: }
340: item = calloc(1, sizeof(msg_item_t));
341: assert(item);
342:
343: item->next = MessageList;
344: MessageList = item;
345:
346: strncpy(item->msg, msg, MAX_MESSAGE_LEN);
347: item->msg[MAX_MESSAGE_LEN] = '\0';
348: DEBUGPRINT(("Add message: '%s'\n", item->msg));
349:
350: if (msecs) {
351: item->timeout = msecs;
352: } else {
353: /* show items by default for 2.5 secs */
354: item->timeout = 2500;
355: }
356: item->shown = false;
357: }
358:
359: /*-----------------------------------------------------------------------*/
360: /**
361: * Write given 'more' string to 'buffer' and return new end of 'buffer'
362: */
363: static char *Statusbar_AddString(char *buffer, const char *more)
364: {
365: while(*more) {
366: *buffer++ = *more++;
367: }
368: return buffer;
369: }
370:
371: /*-----------------------------------------------------------------------*/
372: /**
373: * Retrieve/update default statusbar information
374: */
375: void Statusbar_UpdateInfo(void)
376: {
377: char *end = DefaultMessage.msg;
378:
379: /* CPU MHz */
380: if (ConfigureParams.System.nCpuFreq > 9) {
381: *end++ = '0' + ConfigureParams.System.nCpuFreq / 10;
382: }
383: *end++ = '0' + ConfigureParams.System.nCpuFreq % 10;
384: end = Statusbar_AddString(end, "MHz/");
385:
386: /* CPU type */
387: if(ConfigureParams.System.nCpuLevel > 0) {
388: *end++ = '0';
389: *end++ = '0' + ConfigureParams.System.nCpuLevel % 10;
390: *end++ = '0';
391: *end++ = '/';
392: }
393:
394: /* amount of memory */
395: if (ConfigureParams.Memory.nMemorySize > 9) {
396: *end++ = '1';
397: *end++ = '0' + ConfigureParams.Memory.nMemorySize % 10;
398: } else {
399: if (ConfigureParams.Memory.nMemorySize) {
400: *end++ = '0' + ConfigureParams.Memory.nMemorySize;
401: } else {
402: end = Statusbar_AddString(end, "1/2");
403: }
404: }
405: end = Statusbar_AddString(end, "MB ");
406:
407: /* machine type */
408: end = Statusbar_AddString(end, "NEXT");
409:
410: *end = '\0';
411:
412: assert(end - DefaultMessage.msg < MAX_MESSAGE_LEN);
413: DEBUGPRINT(("Set default message: '%s'\n", DefaultMessage.msg));
1.1.1.2 ! root 414: /* make sure default message gets (re-)drawn when next checked */
1.1 root 415: DefaultMessage.shown = false;
416: }
417:
418: /*-----------------------------------------------------------------------*/
419: /**
420: * Draw 'msg' centered to the message area
421: */
422: static void Statusbar_DrawMessage(SDL_Surface *surf, const char *msg)
423: {
424: int fontw, fonth, offset;
425: SDL_FillRect(surf, &MessageRect, GrayBg);
426: if (*msg) {
427: SDLGui_GetFontSize(&fontw, &fonth);
428: offset = (MessageRect.w - strlen(msg) * fontw) / 2;
429: SDLGui_Text(MessageRect.x + offset, MessageRect.y, msg);
430: }
431: SDL_UpdateRects(surf, 1, &MessageRect);
432: DEBUGPRINT(("Draw message: '%s'\n", msg));
433: }
434:
435: /*-----------------------------------------------------------------------*/
436: /**
437: * If message's not shown, show it. If message's timed out,
438: * remove it and show next one.
439: */
440: static void Statusbar_ShowMessage(SDL_Surface *surf, Uint32 ticks)
441: {
442: msg_item_t *next;
443:
444: if (MessageList->shown) {
445: if (!MessageList->expire) {
1.1.1.2 ! root 446: /* last/default message never expires */
1.1 root 447: return;
448: }
449: if (MessageList->expire > ticks) {
450: /* not timed out yet */
451: return;
452: }
453: assert(MessageList->next); /* last message shouldn't end here */
454: next = MessageList->next;
455: free(MessageList);
456: MessageList = next;
457: /* make sure next message gets shown */
458: MessageList->shown = false;
459: }
460: if (!MessageList->shown) {
461: /* not shown yet, show */
462: Statusbar_DrawMessage(surf, MessageList->msg);
463: if (MessageList->timeout && !MessageList->expire) {
464: MessageList->expire = ticks + MessageList->timeout;
465: }
466: MessageList->shown = true;
467: }
468: }
469:
470:
471: /*-----------------------------------------------------------------------*/
472: /**
473: * Save the area that will be left under overlay led
474: */
475: void Statusbar_OverlayBackup(SDL_Surface *surf)
476: {
477: if ((StatusbarHeight && ConfigureParams.Screen.bShowStatusbar)
478: || !ConfigureParams.Screen.bShowDriveLed) {
479: /* overlay not used with statusbar */
480: return;
481: }
482: assert(surf);
483: if (!OverlayUnderside) {
484: SDL_Surface *bak;
485: SDL_PixelFormat *fmt = surf->format;
486: bak = SDL_CreateRGBSurface(surf->flags,
487: OverlayLedRect.w, OverlayLedRect.h,
488: fmt->BitsPerPixel,
489: fmt->Rmask, fmt->Gmask, fmt->Bmask,
490: fmt->Amask);
491: assert(bak);
492: OverlayUnderside = bak;
493: }
494: SDL_BlitSurface(surf, &OverlayLedRect, OverlayUnderside, NULL);
495: }
496:
497: /*-----------------------------------------------------------------------*/
498: /**
499: * Restore the area left under overlay led
500: */
501: void Statusbar_OverlayRestore(SDL_Surface *surf)
502: {
503: if ((StatusbarHeight && ConfigureParams.Screen.bShowStatusbar)
504: || !ConfigureParams.Screen.bShowDriveLed) {
505: /* overlay not used with statusbar */
506: return;
507: }
508: if (bOverlayState == OVERLAY_DRAWN && OverlayUnderside) {
509: assert(surf);
510: SDL_BlitSurface(OverlayUnderside, NULL, surf, &OverlayLedRect);
511: /* this will make the draw function to update this the screen */
512: bOverlayState = OVERLAY_RESTORED;
513: }
514: }
515:
516: /*-----------------------------------------------------------------------*/
517: /**
518: * Draw overlay led
519: */
520: static void Statusbar_OverlayDrawLed(SDL_Surface *surf, Uint32 color)
521: {
522: SDL_Rect rect;
523: if (bOverlayState == OVERLAY_DRAWN) {
524: /* some led already drawn */
525: return;
526: }
527: bOverlayState = OVERLAY_DRAWN;
528:
529: /* enabled led with border */
530: rect = OverlayLedRect;
531: rect.x += 1;
532: rect.y += 1;
533: rect.w -= 2;
534: rect.h -= 2;
535: SDL_FillRect(surf, &OverlayLedRect, LedColorBg);
536: SDL_FillRect(surf, &rect, color);
537: }
538:
539: /*-----------------------------------------------------------------------*/
540: /**
541: * Draw overlay led onto screen surface if any drives are enabled.
542: */
543: static void Statusbar_OverlayDraw(SDL_Surface *surf)
544: {
545: Uint32 currentticks = SDL_GetTicks();
546: int i;
547:
548: assert(surf);
549: for (i = 0; i < MAX_DRIVE_LEDS; i++) {
550: if (Led[i].state) {
551: if (Led[i].expire && Led[i].expire < currentticks) {
552: Led[i].state = false;
553: continue;
554: }
555: Statusbar_OverlayDrawLed(surf, LedColorOn);
556: break;
557: }
558: }
559: /* possible state transitions:
560: * NONE -> DRAWN -> RESTORED -> DRAWN -> RESTORED -> NONE
561: * Other than NONE state needs to be updated on screen
562: */
563: switch (bOverlayState) {
564: case OVERLAY_RESTORED:
565: bOverlayState = OVERLAY_NONE;
566: case OVERLAY_DRAWN:
567: SDL_UpdateRects(surf, 1, &OverlayLedRect);
568: DEBUGPRINT(("Overlay LED = %s\n", bOverlayState==OVERLAY_DRAWN?"ON":"OFF"));
569: break;
570: case OVERLAY_NONE:
571: break;
572: }
573: }
574:
575:
576: /*-----------------------------------------------------------------------*/
577: /**
578: * Update statusbar information (leds etc) if/when needed.
579: *
580: * May not be called when screen is locked (SDL limitation).
581: */
582: void Statusbar_Update(SDL_Surface *surf)
583: {
584: Uint32 color, currentticks;
585: SDL_Rect rect;
586: int i;
587:
588: if (!(StatusbarHeight && ConfigureParams.Screen.bShowStatusbar)) {
589: /* not enabled (anymore), show overlay led instead? */
590: if (ConfigureParams.Screen.bShowDriveLed) {
591: Statusbar_OverlayDraw(surf);
592: }
593: return;
594: }
595: assert(surf);
596: /* Statusbar_Init() not called before this? */
597: assert(surf->h == ScreenHeight + StatusbarHeight);
598:
599: rect = LedRect;
600: currentticks = SDL_GetTicks();
601: for (i = 0; i < MAX_DRIVE_LEDS; i++) {
602: if (Led[i].expire && Led[i].expire < currentticks) {
603: Led[i].state = false;
604: }
605: if (Led[i].state == Led[i].oldstate) {
606: continue;
607: }
608: Led[i].oldstate = Led[i].state;
609: if (Led[i].state) {
610: color = LedColorOn;
611: } else {
612: color = LedColorOff;
613: }
614: rect.x = Led[i].offset;
615: SDL_FillRect(surf, &rect, color);
616: SDL_UpdateRects(surf, 1, &rect);
617: DEBUGPRINT(("LED[%d] = %s\n", i, Led[i].state?"ON":"OFF"));
618: }
619:
620: Statusbar_ShowMessage(surf, currentticks);
621:
622:
623: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.