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 */
17 #include <SDL/SDL_image.h>
18 #include <SDL/SDL_events.h>
19 #include <SDL/SDL_keyboard.h>
20 #include <SDL/SDL_keysym.h>
21 #include <sys/time.h> /* timeval, needed for select */
23 #include "sdl_gui.cmdline.h"
26 #define FRAME_GREEN 200
27 #define FRAME_BLUE 200
31 static int height
= 0;
33 extern const char *status_item_list
[NUM_STAT_ITEMS
];
34 struct sdl_gui_args_info args_info
;
36 #define FRAME_WIDTH 10
37 #define FONT_HEIGHT 36
39 #define INPUT_WIDTH width - 2 * FRAME_WIDTH
40 #define INPUT_X FRAME_WIDTH
41 #define INPUT_HEIGHT (args_info.interactive_flag? FONT_HEIGHT : 0)
42 #define INPUT_Y height - FRAME_WIDTH - INPUT_HEIGHT
44 #define OUTPUT_WIDTH width - 2 * FRAME_WIDTH
45 #define OUTPUT_X FRAME_WIDTH
46 #define OUTPUT_Y FRAME_WIDTH
47 #define OUTPUT_HEIGHT (height - INPUT_HEIGHT - 2 * FRAME_WIDTH)
49 #define NUM_LINES (height - 2 * FRAME_WIDTH - INPUT_HEIGHT) / FONT_HEIGHT
77 SFont_FontInfo fontinfo
;
80 struct font fonts
[] = {
82 .name
= "24P_Arial_Metallic_Yellow.png",
83 .fontinfo
= {NULL
, {0}, 0}
86 .name
= "24P_Arial_NeonYellow.png",
87 .fontinfo
= {NULL
, {0}, 0}
90 .name
= "24P_Copperplate_Blue.png",
91 .fontinfo
= {NULL
, {0}, 0}
95 .fontinfo
= {NULL
, {0}, 0}
100 #define PIC_WIDTH width * 3 / 10
101 #define PIC_HEIGHT height / 3
104 #define OUTPUT_FONT 1
108 static struct stat_item stat_items
[NUM_STAT_ITEMS
];
110 void para_log(__a_unused
int ll
, __a_unused
const char* fmt
,...) /* no logging */
114 static void init_stat_items(void)
117 struct stat_item
*s
= stat_items
;
119 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
125 s
[SI_STATUS_BAR
].prefix
= "";
126 s
[SI_STATUS_BAR
].postfix
= "";
127 s
[SI_STATUS_BAR
].x
= 0;
128 s
[SI_STATUS_BAR
].y
= 10;
129 s
[SI_STATUS_BAR
].w
= 100;
130 s
[SI_STATUS_BAR
].h
= FONT_HEIGHT
;
131 s
[SI_STATUS_BAR
].r
= 0;
132 s
[SI_STATUS_BAR
].g
= 0;
133 s
[SI_STATUS_BAR
].b
= 0;
134 s
[SI_STATUS_BAR
].font
= M_YELLOW
;
135 s
[SI_STATUS_BAR
].align
= CENTER
;
137 s
[SI_PLAY_TIME
].prefix
= "";
138 s
[SI_PLAY_TIME
].postfix
= "";
139 s
[SI_PLAY_TIME
].x
= 35;
140 s
[SI_PLAY_TIME
].y
= 20;
141 s
[SI_PLAY_TIME
].w
= 65;
142 s
[SI_PLAY_TIME
].h
= FONT_HEIGHT
;
143 s
[SI_PLAY_TIME
].r
= 0;
144 s
[SI_PLAY_TIME
].g
= 0;
145 s
[SI_PLAY_TIME
].b
= 0;
146 s
[SI_PLAY_TIME
].font
= M_YELLOW
;
147 s
[SI_PLAY_TIME
].align
= CENTER
;
149 s
[SI_STATUS
].prefix
= "";
150 s
[SI_STATUS
].postfix
= "";
154 s
[SI_STATUS
].h
= FONT_HEIGHT
;
158 s
[SI_STATUS
].font
= N_YELLOW
;
159 s
[SI_STATUS
].align
= LEFT
;
161 s
[SI_STATUS_FLAGS
].prefix
= " (";
162 s
[SI_STATUS_FLAGS
].postfix
= ")";
163 s
[SI_STATUS_FLAGS
].x
= 47;
164 s
[SI_STATUS_FLAGS
].y
= 28;
165 s
[SI_STATUS_FLAGS
].w
= 15;
166 s
[SI_STATUS_FLAGS
].h
= FONT_HEIGHT
;
167 s
[SI_STATUS_FLAGS
].r
= 0;
168 s
[SI_STATUS_FLAGS
].g
= 0;
169 s
[SI_STATUS_FLAGS
].b
= 0;
170 s
[SI_STATUS_FLAGS
].font
= N_YELLOW
;
171 s
[SI_STATUS_FLAGS
].align
= CENTER
;
173 s
[SI_NUM_PLAYED
].prefix
= "#";
174 s
[SI_NUM_PLAYED
].postfix
= "";
175 s
[SI_NUM_PLAYED
].x
= 62;
176 s
[SI_NUM_PLAYED
].y
= 28;
177 s
[SI_NUM_PLAYED
].w
= 13;
178 s
[SI_NUM_PLAYED
].h
= FONT_HEIGHT
;
179 s
[SI_NUM_PLAYED
].r
= 0;
180 s
[SI_NUM_PLAYED
].g
= 0;
181 s
[SI_NUM_PLAYED
].b
= 0;
182 s
[SI_NUM_PLAYED
].font
= N_YELLOW
;
183 s
[SI_NUM_PLAYED
].align
= CENTER
;
185 s
[SI_UPTIME
].prefix
= "Up: ";
186 s
[SI_UPTIME
].postfix
= "";
190 s
[SI_UPTIME
].h
= FONT_HEIGHT
;
194 s
[SI_UPTIME
].font
= N_YELLOW
;
195 s
[SI_UPTIME
].align
= RIGHT
;
197 s
[SI_SELECTOR
].prefix
= "selector: ";
198 s
[SI_SELECTOR
].postfix
= "";
199 s
[SI_SELECTOR
].x
= 35;
200 s
[SI_SELECTOR
].y
= 48;
201 s
[SI_SELECTOR
].w
= 35;
202 s
[SI_SELECTOR
].h
= FONT_HEIGHT
;
203 s
[SI_SELECTOR
].r
= 0;
204 s
[SI_SELECTOR
].g
= 0;
205 s
[SI_SELECTOR
].b
= 0;
206 s
[SI_SELECTOR
].font
= N_YELLOW
;
207 s
[SI_SELECTOR
].align
= LEFT
;
209 s
[SI_FORMAT
].prefix
= "Format: ";
210 s
[SI_FORMAT
].postfix
= "";
214 s
[SI_FORMAT
].h
= FONT_HEIGHT
;
218 s
[SI_FORMAT
].font
= N_YELLOW
;
219 s
[SI_FORMAT
].align
= RIGHT
;
221 s
[SI_MTIME
].prefix
= "MTime: ";
222 s
[SI_MTIME
].postfix
= "";
226 s
[SI_MTIME
].h
= FONT_HEIGHT
;
230 s
[SI_MTIME
].font
= N_YELLOW
;
231 s
[SI_MTIME
].align
= LEFT
;
233 s
[SI_FILE_SIZE
].prefix
= "Size: ";
234 s
[SI_FILE_SIZE
].postfix
= "kb";
235 s
[SI_FILE_SIZE
].x
= 35;
236 s
[SI_FILE_SIZE
].y
= 42;
237 s
[SI_FILE_SIZE
].w
= 20;
238 s
[SI_FILE_SIZE
].h
= FONT_HEIGHT
;
239 s
[SI_FILE_SIZE
].r
= 0;
240 s
[SI_FILE_SIZE
].g
= 0;
241 s
[SI_FILE_SIZE
].b
= 0;
242 s
[SI_FILE_SIZE
].font
= N_YELLOW
;
243 s
[SI_FILE_SIZE
].align
= LEFT
;
245 s
[SI_AUDIO_INFO1
].prefix
= "";
246 s
[SI_AUDIO_INFO1
].postfix
= "";
247 s
[SI_AUDIO_INFO1
].x
= 0;
248 s
[SI_AUDIO_INFO1
].y
= 60;
249 s
[SI_AUDIO_INFO1
].w
= 100;
250 s
[SI_AUDIO_INFO1
].h
= FONT_HEIGHT
;
251 s
[SI_AUDIO_INFO1
].r
= 0;
252 s
[SI_AUDIO_INFO1
].g
= 0;
253 s
[SI_AUDIO_INFO1
].b
= 0;
254 s
[SI_AUDIO_INFO1
].font
= N_YELLOW
;
255 s
[SI_AUDIO_INFO1
].align
= CENTER
;
257 s
[SI_AUDIO_INFO2
].prefix
= "";
258 s
[SI_AUDIO_INFO2
].postfix
= "";
259 s
[SI_AUDIO_INFO2
].x
= 0;
260 s
[SI_AUDIO_INFO2
].y
= 65;
261 s
[SI_AUDIO_INFO2
].w
= 100;
262 s
[SI_AUDIO_INFO2
].h
= FONT_HEIGHT
;
263 s
[SI_AUDIO_INFO2
].r
= 0;
264 s
[SI_AUDIO_INFO2
].g
= 0;
265 s
[SI_AUDIO_INFO2
].b
= 0;
266 s
[SI_AUDIO_INFO2
].font
= N_YELLOW
;
267 s
[SI_AUDIO_INFO2
].align
= CENTER
;
269 s
[SI_AUDIO_INFO3
].prefix
= "";
270 s
[SI_AUDIO_INFO3
].postfix
= "";
271 s
[SI_AUDIO_INFO3
].x
= 0;
272 s
[SI_AUDIO_INFO3
].y
= 70;
273 s
[SI_AUDIO_INFO3
].w
= 100;
274 s
[SI_AUDIO_INFO3
].h
= FONT_HEIGHT
;
275 s
[SI_AUDIO_INFO3
].r
= 0;
276 s
[SI_AUDIO_INFO3
].g
= 0;
277 s
[SI_AUDIO_INFO3
].b
= 0;
278 s
[SI_AUDIO_INFO3
].font
= N_YELLOW
;
279 s
[SI_AUDIO_INFO3
].align
= CENTER
;
281 s
[SI_DBINFO1
].name
= "dbinfo1:";
282 s
[SI_DBINFO1
].prefix
= "";
283 s
[SI_DBINFO1
].postfix
= "";
285 s
[SI_DBINFO1
].y
= 83;
286 s
[SI_DBINFO1
].w
= 100;
287 s
[SI_DBINFO1
].h
= FONT_HEIGHT
;
291 s
[SI_DBINFO1
].font
= N_YELLOW
;
292 s
[SI_DBINFO1
].align
= CENTER
;
294 s
[SI_DBINFO2
].prefix
= "";
295 s
[SI_DBINFO2
].postfix
= "";
297 s
[SI_DBINFO2
].y
= 88;
298 s
[SI_DBINFO2
].w
= 100;
299 s
[SI_DBINFO2
].h
= FONT_HEIGHT
;
303 s
[SI_DBINFO2
].font
= N_YELLOW
;
304 s
[SI_DBINFO2
].align
= CENTER
;
306 s
[SI_DBINFO3
].name
= "dbinfo3:";
307 s
[SI_DBINFO3
].prefix
= "";
308 s
[SI_DBINFO3
].postfix
= "";
310 s
[SI_DBINFO3
].y
= 93;
311 s
[SI_DBINFO3
].w
= 100;
312 s
[SI_DBINFO3
].h
= FONT_HEIGHT
;
316 s
[SI_DBINFO3
].font
= N_YELLOW
;
317 s
[SI_DBINFO3
].align
= CENTER
;
321 * init SDL libary and set window title
323 static void init_SDL(void)
325 if (SDL_Init(SDL_INIT_VIDEO
) == -1) {
327 "Couldn't initialize SDL: %s\n", SDL_GetError());
330 /* Clean up on exit */
332 /* Initialize the display */
333 if (args_info
.fullscreen_flag
)
334 screen
= SDL_SetVideoMode(width
, height
, 0, SDL_FULLSCREEN
);
336 screen
= SDL_SetVideoMode(width
, height
, 0, 0);
338 fprintf(stderr
, "Couldn't set video mode: %s\n",
342 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
343 SDL_EventState(SDL_MOUSEBUTTONDOWN
, SDL_IGNORE
);
344 SDL_EventState(SDL_MOUSEBUTTONUP
, SDL_IGNORE
);
345 /* Set the window manager title bar */
346 SDL_WM_SetCaption("The Gui of death that makes you blind (paraslash "
347 PACKAGE_VERSION
")", "SFont");
351 * draw rectangular frame of width FRAME_WIDTH
353 static void draw_frame(Uint8 r
, Uint8 g
, Uint8 b
) {
359 rect
.h
= FRAME_WIDTH
;
360 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
361 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
364 rect
.y
= height
- FRAME_WIDTH
;
366 rect
.h
= FRAME_WIDTH
;
367 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
368 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
371 rect
.y
= FRAME_WIDTH
;
372 rect
.w
= FRAME_WIDTH
;
373 rect
.h
= height
- 2 * FRAME_WIDTH
;
374 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
375 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
377 rect
.x
= width
- FRAME_WIDTH
;
378 rect
.y
= FRAME_WIDTH
;
379 rect
.w
= FRAME_WIDTH
;
380 rect
.h
= height
- 2 * FRAME_WIDTH
;
381 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
382 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
386 * fill input rect with color
388 static void fill_input_rect(void)
394 rect
.w
= INPUT_WIDTH
;
395 rect
.h
= INPUT_HEIGHT
;
396 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 10, 150, 10));
400 * fill output rect with color
402 static void fill_output_rect(void)
408 rect
.w
= OUTPUT_WIDTH
;
409 rect
.h
= OUTPUT_HEIGHT
;
410 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
414 * convert tab to space
416 static void tab2space(char *text
)
426 static void print_msg(const char *msg
)
428 SFont_FontInfo
*font
= &(fonts
[MSG_FONT
].fontinfo
);
429 char *buf
= strdup(msg
);
430 int len
= strlen(buf
);
434 while (TextWidth2(font
, buf
) > INPUT_WIDTH
&& len
> 0) {
439 PutString2(screen
, font
, INPUT_X
, INPUT_Y
, buf
);
443 static void update_all(void)
445 SDL_UpdateRect(screen
, 0, 0, 0, 0);
448 static void update_input(void)
450 SDL_UpdateRect(screen
, INPUT_X
, INPUT_Y
, INPUT_WIDTH
, INPUT_HEIGHT
);
455 * wait for key, ignore all other events, return 0 if there is no key event
456 * pending. Otherwise return keysym of key
462 while (SDL_PollEvent(&event
) > 0) {
463 if(event
.type
!= SDL_KEYDOWN
)
465 // printf("Key pressed, scancode: 0x%x\n",
466 // event.key.keysym.scancode);
467 return event
.key
.keysym
.sym
;
473 * print message, wait for key (blocking), return 1 for 'q', 0 else
475 static SDLKey
hit_key(const char *msg
)
481 while (!(sym
= get_key()))
492 * read paraslash command from input, execute it and print results
494 static int command_handler(void)
498 char text
[MAXLINE
]="";
499 char buf
[MAXLINE
]="";
500 SFont_FontInfo
*font
= &fonts
[OUTPUT_FONT
].fontinfo
;
502 // printf("string input\n");
503 SFont_Input2(screen
, &fonts
[INPUT_FONT
].fontinfo
,
504 INPUT_X
, INPUT_Y
- 5, INPUT_WIDTH
, text
);
507 if (!strcmp(text
, "exit") || !strcmp(text
, "quit"))
509 if (text
[0] == '!') {
512 f
= popen(text
+ 1, "r");
514 sprintf(buf
, BINDIR
"/para_client %s 2>&1", text
);
520 while(fgets(text
, MAXLINE
- 1, f
)) {
525 // printf("string: %s\n", dest);
526 while (TextWidth2(font
, text
) > width
- 2 * FRAME_WIDTH
&&
531 PutString2(screen
, font
, OUTPUT_X
,
532 OUTPUT_Y
+ count
* FONT_HEIGHT
, text
);
534 if (count
>= NUM_LINES
) {
536 if (hit_key("Hit any key to continue, q to return"))
543 hit_key("Hit any key to return");
544 out
: fill_output_rect();
551 * Add prefix and postfix to string, delete characters from the end
552 * if its length exceeds the max length defined in stat_items[item]
554 char *transform_string(int item
)
556 struct stat_item s
= stat_items
[item
];
559 unsigned pixels
= s
.w
* (width
- 2 * FRAME_WIDTH
) / 100;
560 SFont_FontInfo
*font
= &(fonts
[s
.font
].fontinfo
);
562 ret
= make_message("%s%s%s", s
.prefix
, s
.content
, s
.postfix
);
564 while (TextWidth2(font
, ret
) > pixels
&& len
> 0) {
571 SDL_Surface
*load_jpg(void)
574 int fds
[3] = {0, 1, 0};
578 if (para_exec_cmdline_pid(&pid
, args_info
.pic_cmd_arg
, fds
) < 0)
580 f
= fdopen(fds
[1], "r");
583 if (!(rwop
= SDL_RWFromFP(f
, 0)))
585 return IMG_LoadJPG_RW(rwop
);
588 void update_pic(void)
591 SDL_Rect src_pic_rect
= {
597 SDL_Rect dest_pic_rect
= {
599 .y
= OUTPUT_HEIGHT
/ 5,
607 if (!(img
= load_jpg()))
609 SDL_FillRect(screen
, &dest_pic_rect
, SDL_MapRGB(screen
->format
,
611 SDL_BlitSurface(img
, &src_pic_rect
, screen
, &dest_pic_rect
);
613 SDL_FreeSurface(img
);
617 * update status item number i.
619 static void do_update(int i
)
621 static int last_played
= -1;
624 SFont_FontInfo
*font
= &(fonts
[stat_items
[i
].font
].fontinfo
);
625 if (!stat_items
[i
].w
)
628 rect
.x
= stat_items
[i
].x
* (width
- FRAME_WIDTH
* 2) / 100
630 rect
.y
= stat_items
[i
].y
* (height
- 2 * FRAME_WIDTH
- INPUT_HEIGHT
)
632 rect
.w
= stat_items
[i
].w
* (width
- 2 * FRAME_WIDTH
) / 100;
633 rect
.h
= stat_items
[i
].h
;
634 buf
= transform_string(i
);
635 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
,
636 stat_items
[i
].r
, stat_items
[i
].g
, stat_items
[i
].b
));
637 switch(stat_items
[i
].align
) {
639 PutString2(screen
, font
,
640 rect
.x
+ (rect
.w
- TextWidth2(font
, buf
)) / 2,
644 PutString2(screen
, font
, rect
.x
, rect
.y
, buf
);
647 PutString2(screen
, font
, rect
.x
+ (rect
.w
-
648 TextWidth2(font
, buf
)), rect
.y
, buf
);
652 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
653 if (i
== SI_NUM_PLAYED
&& atoi(stat_items
[i
].content
) != last_played
) {
655 last_played
= atoi(stat_items
[i
].content
);
660 * Check if buf is a known status line. If so call do_update and return 1.
661 * Return 0 otherwise.
663 void update_status(char *buf
)
667 i
= stat_line_valid(buf
);
670 //free(stat_items[i].content);
671 stat_items
[i
].content
= para_strdup(buf
+
672 strlen(status_item_list
[i
]) + 1);
677 * Read stat line from pipe if pipe is ready, call update_status to
678 * display information.
680 static int draw_status(int fd
)
687 tv
.tv_usec
= 3000000;
690 ret
= para_select(fd
+ 1, &rfds
, NULL
, &tv
);
693 if (read_audiod_pipe(fd
, update_status
) > 0)
695 free(stat_items
[SI_STATUS_BAR
].content
);
696 stat_items
[SI_STATUS_BAR
].content
=
697 para_strdup("audiod not running!?\n");
703 static void clean_exit(int ret
)
709 static void print_help(void)
711 print_msg("Hit q to quit, any other key to enter command mode");
714 static int configfile_exists(void)
716 if (!args_info
.config_file_given
) {
717 char *home
= para_homedir();
718 args_info
.config_file_arg
= make_message(
719 "%s/.paraslash/sdl_gui.conf", home
);
722 return file_exists(args_info
.config_file_arg
);
728 int main(int argc
, char *argv
[])
733 sdl_gui_cmdline_parser(argc
, argv
, &args_info
);
734 HANDLE_VERSION_FLAG("sdl_gui", args_info
);
735 ret
= configfile_exists();
736 // printf("w=%i,h=%i,ret=%i, cf=%s\n", width, height, ret, args_info.config_file_arg);
738 if (!ret
&& args_info
.config_file_given
) {
739 fprintf(stderr
, "Can't read config file %s\n",
740 args_info
.config_file_arg
);
744 struct sdl_gui_cmdline_parser_params params
= {
750 sdl_gui_cmdline_parser_config_file(args_info
.config_file_arg
,
751 &args_info
, ¶ms
);
753 signal(SIGCHLD
, SIG_IGN
);
754 width
= args_info
.width_arg
;
755 height
= args_info
.height_arg
;
756 // printf("w=%i,h=%i,ret=%i, cf=%s\n", width, height, ret, args_info.config_file_arg);
759 for (i
= 0; fonts
[i
].name
[0]; i
++) {
761 sprintf(buf
, "%s/%s", FONTDIR
, fonts
[i
].name
);
762 /* Load the font - You don't have to use the IMGlib for this */
763 fonts
[i
].fontinfo
.Surface
= IMG_Load(buf
);
764 /* Prepare the font for use */
765 InitFont2(&fonts
[i
].fontinfo
);
767 draw_frame(FRAME_RED
, FRAME_GREEN
, FRAME_BLUE
);
768 if (args_info
.interactive_flag
) {
774 fd
= para_open_audiod_pipe(args_info
.stat_cmd_arg
);
776 clean_exit(EXIT_FAILURE
);
778 ret
= draw_status(fd
);
783 if (SDL_QuitRequested())
785 while ((sym
= get_key())) {
786 if (!args_info
.interactive_flag
)
790 if ( sym
== SDLK_LSHIFT
791 || sym
== SDLK_RSHIFT
797 || sym
== SDLK_CAPSLOCK
800 || sym
== SDLK_RSUPER
801 || sym
== SDLK_LSUPER
802 || sym
== SDLK_COMPOSE
807 if (!command_handler())
812 SDL_UpdateRect(screen
, 0, 0, 0, 0);