2 * Copyright (C) 2003-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sdl_gui.c SDL-based interface for paraslash */
13 #include "gui_common.h"
20 #include <SDL/SDL_image.h>
21 #include <SDL/SDL_events.h>
22 #include <SDL/SDL_keyboard.h>
23 #include <SDL/SDL_keysym.h>
24 #include <sys/time.h> /* timeval, needed for select */
26 #include "sdl_gui.cmdline.h"
29 #define FRAME_GREEN 200
30 #define FRAME_BLUE 200
34 static int height
= 0;
36 extern const char *status_item_list
[NUM_STAT_ITEMS
];
37 struct sdl_gui_args_info args_info
;
39 #define FRAME_WIDTH 10
40 #define FONT_HEIGHT 36
42 #define INPUT_WIDTH width - 2 * FRAME_WIDTH
43 #define INPUT_X FRAME_WIDTH
44 #define INPUT_HEIGHT (args_info.interactive_flag? FONT_HEIGHT : 0)
45 #define INPUT_Y height - FRAME_WIDTH - INPUT_HEIGHT
47 #define OUTPUT_WIDTH width - 2 * FRAME_WIDTH
48 #define OUTPUT_X FRAME_WIDTH
49 #define OUTPUT_Y FRAME_WIDTH
50 #define OUTPUT_HEIGHT (height - INPUT_HEIGHT - 2 * FRAME_WIDTH)
52 #define NUM_LINES (height - 2 * FRAME_WIDTH - INPUT_HEIGHT) / FONT_HEIGHT
80 SFont_FontInfo fontinfo
;
83 struct font fonts
[] = {
85 .name
= "24P_Arial_Metallic_Yellow.png",
86 .fontinfo
= {NULL
, {0}, 0}
89 .name
= "24P_Arial_NeonYellow.png",
90 .fontinfo
= {NULL
, {0}, 0}
93 .name
= "24P_Copperplate_Blue.png",
94 .fontinfo
= {NULL
, {0}, 0}
98 .fontinfo
= {NULL
, {0}, 0}
103 #define PIC_WIDTH width * 3 / 10
104 #define PIC_HEIGHT height / 3
107 #define OUTPUT_FONT 1
111 static struct stat_item stat_items
[NUM_STAT_ITEMS
];
113 void para_log(__a_unused
int ll
, __a_unused
const char* fmt
,...) /* no logging */
117 static void init_stat_items(void)
120 struct stat_item
*s
= stat_items
;
122 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
128 s
[SI_STATUS_BAR
].prefix
= "";
129 s
[SI_STATUS_BAR
].postfix
= "";
130 s
[SI_STATUS_BAR
].x
= 0;
131 s
[SI_STATUS_BAR
].y
= 10;
132 s
[SI_STATUS_BAR
].w
= 100;
133 s
[SI_STATUS_BAR
].h
= FONT_HEIGHT
;
134 s
[SI_STATUS_BAR
].r
= 0;
135 s
[SI_STATUS_BAR
].g
= 0;
136 s
[SI_STATUS_BAR
].b
= 0;
137 s
[SI_STATUS_BAR
].font
= M_YELLOW
;
138 s
[SI_STATUS_BAR
].align
= CENTER
;
140 s
[SI_PLAY_TIME
].prefix
= "";
141 s
[SI_PLAY_TIME
].postfix
= "";
142 s
[SI_PLAY_TIME
].x
= 35;
143 s
[SI_PLAY_TIME
].y
= 20;
144 s
[SI_PLAY_TIME
].w
= 65;
145 s
[SI_PLAY_TIME
].h
= FONT_HEIGHT
;
146 s
[SI_PLAY_TIME
].r
= 0;
147 s
[SI_PLAY_TIME
].g
= 0;
148 s
[SI_PLAY_TIME
].b
= 0;
149 s
[SI_PLAY_TIME
].font
= M_YELLOW
;
150 s
[SI_PLAY_TIME
].align
= CENTER
;
152 s
[SI_STATUS
].prefix
= "";
153 s
[SI_STATUS
].postfix
= "";
157 s
[SI_STATUS
].h
= FONT_HEIGHT
;
161 s
[SI_STATUS
].font
= N_YELLOW
;
162 s
[SI_STATUS
].align
= LEFT
;
164 s
[SI_STATUS_FLAGS
].prefix
= " (";
165 s
[SI_STATUS_FLAGS
].postfix
= ")";
166 s
[SI_STATUS_FLAGS
].x
= 47;
167 s
[SI_STATUS_FLAGS
].y
= 28;
168 s
[SI_STATUS_FLAGS
].w
= 15;
169 s
[SI_STATUS_FLAGS
].h
= FONT_HEIGHT
;
170 s
[SI_STATUS_FLAGS
].r
= 0;
171 s
[SI_STATUS_FLAGS
].g
= 0;
172 s
[SI_STATUS_FLAGS
].b
= 0;
173 s
[SI_STATUS_FLAGS
].font
= N_YELLOW
;
174 s
[SI_STATUS_FLAGS
].align
= CENTER
;
176 s
[SI_NUM_PLAYED
].prefix
= "#";
177 s
[SI_NUM_PLAYED
].postfix
= "";
178 s
[SI_NUM_PLAYED
].x
= 62;
179 s
[SI_NUM_PLAYED
].y
= 28;
180 s
[SI_NUM_PLAYED
].w
= 13;
181 s
[SI_NUM_PLAYED
].h
= FONT_HEIGHT
;
182 s
[SI_NUM_PLAYED
].r
= 0;
183 s
[SI_NUM_PLAYED
].g
= 0;
184 s
[SI_NUM_PLAYED
].b
= 0;
185 s
[SI_NUM_PLAYED
].font
= N_YELLOW
;
186 s
[SI_NUM_PLAYED
].align
= CENTER
;
188 s
[SI_UPTIME
].prefix
= "Up: ";
189 s
[SI_UPTIME
].postfix
= "";
193 s
[SI_UPTIME
].h
= FONT_HEIGHT
;
197 s
[SI_UPTIME
].font
= N_YELLOW
;
198 s
[SI_UPTIME
].align
= RIGHT
;
200 s
[SI_SELECTOR
].prefix
= "selector: ";
201 s
[SI_SELECTOR
].postfix
= "";
202 s
[SI_SELECTOR
].x
= 35;
203 s
[SI_SELECTOR
].y
= 48;
204 s
[SI_SELECTOR
].w
= 35;
205 s
[SI_SELECTOR
].h
= FONT_HEIGHT
;
206 s
[SI_SELECTOR
].r
= 0;
207 s
[SI_SELECTOR
].g
= 0;
208 s
[SI_SELECTOR
].b
= 0;
209 s
[SI_SELECTOR
].font
= N_YELLOW
;
210 s
[SI_SELECTOR
].align
= LEFT
;
212 s
[SI_FORMAT
].prefix
= "Format: ";
213 s
[SI_FORMAT
].postfix
= "";
217 s
[SI_FORMAT
].h
= FONT_HEIGHT
;
221 s
[SI_FORMAT
].font
= N_YELLOW
;
222 s
[SI_FORMAT
].align
= RIGHT
;
224 s
[SI_MTIME
].prefix
= "MTime: ";
225 s
[SI_MTIME
].postfix
= "";
229 s
[SI_MTIME
].h
= FONT_HEIGHT
;
233 s
[SI_MTIME
].font
= N_YELLOW
;
234 s
[SI_MTIME
].align
= LEFT
;
236 s
[SI_FILE_SIZE
].prefix
= "Size: ";
237 s
[SI_FILE_SIZE
].postfix
= "kb";
238 s
[SI_FILE_SIZE
].x
= 35;
239 s
[SI_FILE_SIZE
].y
= 42;
240 s
[SI_FILE_SIZE
].w
= 20;
241 s
[SI_FILE_SIZE
].h
= FONT_HEIGHT
;
242 s
[SI_FILE_SIZE
].r
= 0;
243 s
[SI_FILE_SIZE
].g
= 0;
244 s
[SI_FILE_SIZE
].b
= 0;
245 s
[SI_FILE_SIZE
].font
= N_YELLOW
;
246 s
[SI_FILE_SIZE
].align
= LEFT
;
248 s
[SI_AUDIO_INFO1
].prefix
= "";
249 s
[SI_AUDIO_INFO1
].postfix
= "";
250 s
[SI_AUDIO_INFO1
].x
= 0;
251 s
[SI_AUDIO_INFO1
].y
= 60;
252 s
[SI_AUDIO_INFO1
].w
= 100;
253 s
[SI_AUDIO_INFO1
].h
= FONT_HEIGHT
;
254 s
[SI_AUDIO_INFO1
].r
= 0;
255 s
[SI_AUDIO_INFO1
].g
= 0;
256 s
[SI_AUDIO_INFO1
].b
= 0;
257 s
[SI_AUDIO_INFO1
].font
= N_YELLOW
;
258 s
[SI_AUDIO_INFO1
].align
= CENTER
;
260 s
[SI_AUDIO_INFO2
].prefix
= "";
261 s
[SI_AUDIO_INFO2
].postfix
= "";
262 s
[SI_AUDIO_INFO2
].x
= 0;
263 s
[SI_AUDIO_INFO2
].y
= 65;
264 s
[SI_AUDIO_INFO2
].w
= 100;
265 s
[SI_AUDIO_INFO2
].h
= FONT_HEIGHT
;
266 s
[SI_AUDIO_INFO2
].r
= 0;
267 s
[SI_AUDIO_INFO2
].g
= 0;
268 s
[SI_AUDIO_INFO2
].b
= 0;
269 s
[SI_AUDIO_INFO2
].font
= N_YELLOW
;
270 s
[SI_AUDIO_INFO2
].align
= CENTER
;
272 s
[SI_AUDIO_INFO3
].prefix
= "";
273 s
[SI_AUDIO_INFO3
].postfix
= "";
274 s
[SI_AUDIO_INFO3
].x
= 0;
275 s
[SI_AUDIO_INFO3
].y
= 70;
276 s
[SI_AUDIO_INFO3
].w
= 100;
277 s
[SI_AUDIO_INFO3
].h
= FONT_HEIGHT
;
278 s
[SI_AUDIO_INFO3
].r
= 0;
279 s
[SI_AUDIO_INFO3
].g
= 0;
280 s
[SI_AUDIO_INFO3
].b
= 0;
281 s
[SI_AUDIO_INFO3
].font
= N_YELLOW
;
282 s
[SI_AUDIO_INFO3
].align
= CENTER
;
284 s
[SI_DBINFO1
].name
= "dbinfo1:";
285 s
[SI_DBINFO1
].prefix
= "";
286 s
[SI_DBINFO1
].postfix
= "";
288 s
[SI_DBINFO1
].y
= 83;
289 s
[SI_DBINFO1
].w
= 100;
290 s
[SI_DBINFO1
].h
= FONT_HEIGHT
;
294 s
[SI_DBINFO1
].font
= N_YELLOW
;
295 s
[SI_DBINFO1
].align
= CENTER
;
297 s
[SI_DBINFO2
].prefix
= "";
298 s
[SI_DBINFO2
].postfix
= "";
300 s
[SI_DBINFO2
].y
= 88;
301 s
[SI_DBINFO2
].w
= 100;
302 s
[SI_DBINFO2
].h
= FONT_HEIGHT
;
306 s
[SI_DBINFO2
].font
= N_YELLOW
;
307 s
[SI_DBINFO2
].align
= CENTER
;
309 s
[SI_DBINFO3
].name
= "dbinfo3:";
310 s
[SI_DBINFO3
].prefix
= "";
311 s
[SI_DBINFO3
].postfix
= "";
313 s
[SI_DBINFO3
].y
= 93;
314 s
[SI_DBINFO3
].w
= 100;
315 s
[SI_DBINFO3
].h
= FONT_HEIGHT
;
319 s
[SI_DBINFO3
].font
= N_YELLOW
;
320 s
[SI_DBINFO3
].align
= CENTER
;
324 * init SDL libary and set window title
326 static void init_SDL(void)
328 if (SDL_Init(SDL_INIT_VIDEO
) == -1) {
330 "Couldn't initialize SDL: %s\n", SDL_GetError());
333 /* Clean up on exit */
335 /* Initialize the display */
336 if (args_info
.fullscreen_flag
)
337 screen
= SDL_SetVideoMode(width
, height
, 0, SDL_FULLSCREEN
);
339 screen
= SDL_SetVideoMode(width
, height
, 0, 0);
341 fprintf(stderr
, "Couldn't set video mode: %s\n",
345 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
346 SDL_EventState(SDL_MOUSEBUTTONDOWN
, SDL_IGNORE
);
347 SDL_EventState(SDL_MOUSEBUTTONUP
, SDL_IGNORE
);
348 /* Set the window manager title bar */
349 SDL_WM_SetCaption("The Gui of death that makes you blind (paraslash "
350 PACKAGE_VERSION
")", "SFont");
354 * draw rectangular frame of width FRAME_WIDTH
356 static void draw_frame(Uint8 r
, Uint8 g
, Uint8 b
) {
362 rect
.h
= FRAME_WIDTH
;
363 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
364 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
367 rect
.y
= height
- FRAME_WIDTH
;
369 rect
.h
= FRAME_WIDTH
;
370 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
371 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
374 rect
.y
= FRAME_WIDTH
;
375 rect
.w
= FRAME_WIDTH
;
376 rect
.h
= height
- 2 * FRAME_WIDTH
;
377 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
378 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
380 rect
.x
= width
- FRAME_WIDTH
;
381 rect
.y
= FRAME_WIDTH
;
382 rect
.w
= FRAME_WIDTH
;
383 rect
.h
= height
- 2 * FRAME_WIDTH
;
384 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
385 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
389 * fill input rect with color
391 static void fill_input_rect(void)
397 rect
.w
= INPUT_WIDTH
;
398 rect
.h
= INPUT_HEIGHT
;
399 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 10, 150, 10));
403 * fill output rect with color
405 static void fill_output_rect(void)
411 rect
.w
= OUTPUT_WIDTH
;
412 rect
.h
= OUTPUT_HEIGHT
;
413 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
417 * convert tab to space
419 static void tab2space(char *text
)
429 static void print_msg(const char *msg
)
431 SFont_FontInfo
*font
= &(fonts
[MSG_FONT
].fontinfo
);
432 char *buf
= strdup(msg
);
433 int len
= strlen(buf
);
437 while (TextWidth2(font
, buf
) > INPUT_WIDTH
&& len
> 0) {
442 PutString2(screen
, font
, INPUT_X
, INPUT_Y
, buf
);
446 static void update_all(void)
448 SDL_UpdateRect(screen
, 0, 0, 0, 0);
451 static void update_input(void)
453 SDL_UpdateRect(screen
, INPUT_X
, INPUT_Y
, INPUT_WIDTH
, INPUT_HEIGHT
);
458 * wait for key, ignore all other events, return 0 if there is no key event
459 * pending. Otherwise return keysym of key
465 while (SDL_PollEvent(&event
) > 0) {
466 if(event
.type
!= SDL_KEYDOWN
)
468 // printf("Key pressed, scancode: 0x%x\n",
469 // event.key.keysym.scancode);
470 return event
.key
.keysym
.sym
;
476 * print message, wait for key (blocking), return 1 for 'q', 0 else
478 static SDLKey
hit_key(const char *msg
)
484 while (!(sym
= get_key()))
495 * read paraslash command from input, execute it and print results
497 static int command_handler(void)
501 char text
[MAXLINE
]="";
502 char buf
[MAXLINE
]="";
503 SFont_FontInfo
*font
= &fonts
[OUTPUT_FONT
].fontinfo
;
505 // printf("string input\n");
506 SFont_Input2(screen
, &fonts
[INPUT_FONT
].fontinfo
,
507 INPUT_X
, INPUT_Y
- 5, INPUT_WIDTH
, text
);
510 if (!strcmp(text
, "exit") || !strcmp(text
, "quit"))
512 if (text
[0] == '!') {
515 f
= popen(text
+ 1, "r");
517 sprintf(buf
, BINDIR
"/para_client %s 2>&1", text
);
523 while(fgets(text
, MAXLINE
- 1, f
)) {
528 // printf("string: %s\n", dest);
529 while (TextWidth2(font
, text
) > width
- 2 * FRAME_WIDTH
&&
534 PutString2(screen
, font
, OUTPUT_X
,
535 OUTPUT_Y
+ count
* FONT_HEIGHT
, text
);
537 if (count
>= NUM_LINES
) {
539 if (hit_key("Hit any key to continue, q to return"))
546 hit_key("Hit any key to return");
547 out
: fill_output_rect();
554 * Add prefix and postfix to string, delete characters from the end
555 * if its length exceeds the max length defined in stat_items[item]
557 char *transform_string(int item
)
559 struct stat_item s
= stat_items
[item
];
562 unsigned pixels
= s
.w
* (width
- 2 * FRAME_WIDTH
) / 100;
563 SFont_FontInfo
*font
= &(fonts
[s
.font
].fontinfo
);
565 ret
= make_message("%s%s%s", s
.prefix
, s
.content
, s
.postfix
);
567 while (TextWidth2(font
, ret
) > pixels
&& len
> 0) {
574 SDL_Surface
*load_jpg(void)
577 int fds
[3] = {0, 1, 0};
581 if (para_exec_cmdline_pid(&pid
, args_info
.pic_cmd_arg
, fds
) < 0)
583 f
= fdopen(fds
[1], "r");
586 if (!(rwop
= SDL_RWFromFP(f
, 0)))
588 return IMG_LoadJPG_RW(rwop
);
591 void update_pic(void)
594 SDL_Rect src_pic_rect
= {
600 SDL_Rect dest_pic_rect
= {
602 .y
= OUTPUT_HEIGHT
/ 5,
610 if (!(img
= load_jpg()))
612 SDL_FillRect(screen
, &dest_pic_rect
, SDL_MapRGB(screen
->format
,
614 SDL_BlitSurface(img
, &src_pic_rect
, screen
, &dest_pic_rect
);
616 SDL_FreeSurface(img
);
620 * update status item number i.
622 static void do_update(int i
)
624 static int last_played
= -1;
627 SFont_FontInfo
*font
= &(fonts
[stat_items
[i
].font
].fontinfo
);
628 if (!stat_items
[i
].w
)
631 rect
.x
= stat_items
[i
].x
* (width
- FRAME_WIDTH
* 2) / 100
633 rect
.y
= stat_items
[i
].y
* (height
- 2 * FRAME_WIDTH
- INPUT_HEIGHT
)
635 rect
.w
= stat_items
[i
].w
* (width
- 2 * FRAME_WIDTH
) / 100;
636 rect
.h
= stat_items
[i
].h
;
637 buf
= transform_string(i
);
638 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
,
639 stat_items
[i
].r
, stat_items
[i
].g
, stat_items
[i
].b
));
640 switch(stat_items
[i
].align
) {
642 PutString2(screen
, font
,
643 rect
.x
+ (rect
.w
- TextWidth2(font
, buf
)) / 2,
647 PutString2(screen
, font
, rect
.x
, rect
.y
, buf
);
650 PutString2(screen
, font
, rect
.x
+ (rect
.w
-
651 TextWidth2(font
, buf
)), rect
.y
, buf
);
655 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
656 if (i
== SI_NUM_PLAYED
&& atoi(stat_items
[i
].content
) != last_played
) {
658 last_played
= atoi(stat_items
[i
].content
);
663 * Check if buf is a known status line. If so call do_update and return 1.
664 * Return 0 otherwise.
666 int update_status(char *buf
, __a_unused
void *data
)
670 i
= stat_line_valid(buf
);
673 //free(stat_items[i].content);
674 stat_items
[i
].content
= para_strdup(buf
+
675 strlen(status_item_list
[i
]) + 1);
681 * Read stat line from pipe if pipe is ready, call update_status to
682 * display information.
684 static int draw_status(int fd
)
691 tv
.tv_usec
= 3000000;
694 ret
= para_select(fd
+ 1, &rfds
, NULL
, &tv
);
697 if (read_audiod_pipe(fd
, update_status
) > 0)
699 free(stat_items
[SI_STATUS_BAR
].content
);
700 stat_items
[SI_STATUS_BAR
].content
=
701 para_strdup("audiod not running!?\n");
707 static void clean_exit(int ret
)
713 static void print_help(void)
715 print_msg("Hit q to quit, any other key to enter command mode");
718 static int configfile_exists(void)
720 if (!args_info
.config_file_given
) {
721 char *home
= para_homedir();
722 args_info
.config_file_arg
= make_message(
723 "%s/.paraslash/sdl_gui.conf", home
);
726 return file_exists(args_info
.config_file_arg
);
732 int main(int argc
, char *argv
[])
737 sdl_gui_cmdline_parser(argc
, argv
, &args_info
);
738 HANDLE_VERSION_FLAG("sdl_gui", args_info
);
739 ret
= configfile_exists();
740 // printf("w=%i,h=%i,ret=%i, cf=%s\n", width, height, ret, args_info.config_file_arg);
742 if (!ret
&& args_info
.config_file_given
) {
743 fprintf(stderr
, "Can't read config file %s\n",
744 args_info
.config_file_arg
);
748 struct sdl_gui_cmdline_parser_params params
= {
754 sdl_gui_cmdline_parser_config_file(args_info
.config_file_arg
,
755 &args_info
, ¶ms
);
757 signal(SIGCHLD
, SIG_IGN
);
758 width
= args_info
.width_arg
;
759 height
= args_info
.height_arg
;
760 // printf("w=%i,h=%i,ret=%i, cf=%s\n", width, height, ret, args_info.config_file_arg);
763 for (i
= 0; fonts
[i
].name
[0]; i
++) {
765 sprintf(buf
, "%s/%s", FONTDIR
, fonts
[i
].name
);
766 /* Load the font - You don't have to use the IMGlib for this */
767 fonts
[i
].fontinfo
.Surface
= IMG_Load(buf
);
768 /* Prepare the font for use */
769 InitFont2(&fonts
[i
].fontinfo
);
771 draw_frame(FRAME_RED
, FRAME_GREEN
, FRAME_BLUE
);
772 if (args_info
.interactive_flag
) {
778 fd
= para_open_audiod_pipe(args_info
.stat_cmd_arg
);
780 clean_exit(EXIT_FAILURE
);
782 ret
= draw_status(fd
);
787 if (SDL_QuitRequested())
789 while ((sym
= get_key())) {
790 if (!args_info
.interactive_flag
)
794 if ( sym
== SDLK_LSHIFT
795 || sym
== SDLK_RSHIFT
801 || sym
== SDLK_CAPSLOCK
804 || sym
== SDLK_RSUPER
805 || sym
== SDLK_LSUPER
806 || sym
== SDLK_COMPOSE
811 if (!command_handler())
816 SDL_UpdateRect(screen
, 0, 0, 0, 0);