2 * Copyright (C) 2003-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file sdl_gui.c SDL-based interface for paraslash */
28 #include <SDL/SDL_image.h>
29 #include <SDL/SDL_events.h>
30 #include <SDL/SDL_keyboard.h>
31 #include <SDL/SDL_keysym.h>
32 #include <sys/time.h> /* timeval, needed for select */
34 #include "sdl_gui.cmdline.h"
37 #define FRAME_GREEN 200
38 #define FRAME_BLUE 200
42 static int height
= 0;
44 extern const char *status_item_list
[NUM_STAT_ITEMS
];
45 struct gengetopt_args_info args_info
;
47 #define FRAME_WIDTH 10
48 #define FONT_HEIGHT 36
50 #define INPUT_WIDTH width - 2 * FRAME_WIDTH
51 #define INPUT_X FRAME_WIDTH
52 #define INPUT_HEIGHT (args_info.interactive_flag? FONT_HEIGHT : 0)
53 #define INPUT_Y height - FRAME_WIDTH - INPUT_HEIGHT
55 #define OUTPUT_WIDTH width - 2 * FRAME_WIDTH
56 #define OUTPUT_X FRAME_WIDTH
57 #define OUTPUT_Y FRAME_WIDTH
58 #define OUTPUT_HEIGHT (height - INPUT_HEIGHT - 2 * FRAME_WIDTH)
60 #define NUM_LINES (height - 2 * FRAME_WIDTH - INPUT_HEIGHT) / FONT_HEIGHT
88 SFont_FontInfo fontinfo
;
91 struct font fonts
[] = {
93 .name
= "24P_Arial_Metallic_Yellow.png",
94 .fontinfo
= {NULL
, {0}, 0}
97 .name
= "24P_Arial_NeonYellow.png",
98 .fontinfo
= {NULL
, {0}, 0}
101 .name
= "24P_Copperplate_Blue.png",
102 .fontinfo
= {NULL
, {0}, 0}
106 .fontinfo
= {NULL
, {0}, 0}
111 #define PIC_WIDTH width * 3 / 10
112 #define PIC_HEIGHT height / 3
115 #define OUTPUT_FONT 1
119 static struct stat_item stat_items
[NUM_STAT_ITEMS
];
121 void para_log(__unused
int ll
, __unused
const char* fmt
,...) /* no logging */
125 static void init_stat_items(void)
128 struct stat_item
*s
= stat_items
;
130 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
136 s
[SI_STATUS_BAR
].prefix
= "";
137 s
[SI_STATUS_BAR
].postfix
= "";
138 s
[SI_STATUS_BAR
].x
= 0;
139 s
[SI_STATUS_BAR
].y
= 10;
140 s
[SI_STATUS_BAR
].w
= 100;
141 s
[SI_STATUS_BAR
].h
= FONT_HEIGHT
;
142 s
[SI_STATUS_BAR
].r
= 0;
143 s
[SI_STATUS_BAR
].g
= 0;
144 s
[SI_STATUS_BAR
].b
= 0;
145 s
[SI_STATUS_BAR
].font
= M_YELLOW
;
146 s
[SI_STATUS_BAR
].align
= CENTER
;
148 s
[SI_PLAY_TIME
].prefix
= "";
149 s
[SI_PLAY_TIME
].postfix
= "";
150 s
[SI_PLAY_TIME
].x
= 35;
151 s
[SI_PLAY_TIME
].y
= 20;
152 s
[SI_PLAY_TIME
].w
= 65;
153 s
[SI_PLAY_TIME
].h
= FONT_HEIGHT
;
154 s
[SI_PLAY_TIME
].r
= 0;
155 s
[SI_PLAY_TIME
].g
= 0;
156 s
[SI_PLAY_TIME
].b
= 0;
157 s
[SI_PLAY_TIME
].font
= M_YELLOW
;
158 s
[SI_PLAY_TIME
].align
= CENTER
;
160 s
[SI_STATUS
].prefix
= "";
161 s
[SI_STATUS
].postfix
= "";
165 s
[SI_STATUS
].h
= FONT_HEIGHT
;
169 s
[SI_STATUS
].font
= N_YELLOW
;
170 s
[SI_STATUS
].align
= LEFT
;
172 s
[SI_STATUS_FLAGS
].prefix
= " (";
173 s
[SI_STATUS_FLAGS
].postfix
= ")";
174 s
[SI_STATUS_FLAGS
].x
= 47;
175 s
[SI_STATUS_FLAGS
].y
= 28;
176 s
[SI_STATUS_FLAGS
].w
= 15;
177 s
[SI_STATUS_FLAGS
].h
= FONT_HEIGHT
;
178 s
[SI_STATUS_FLAGS
].r
= 0;
179 s
[SI_STATUS_FLAGS
].g
= 0;
180 s
[SI_STATUS_FLAGS
].b
= 0;
181 s
[SI_STATUS_FLAGS
].font
= N_YELLOW
;
182 s
[SI_STATUS_FLAGS
].align
= CENTER
;
184 s
[SI_NUM_PLAYED
].prefix
= "#";
185 s
[SI_NUM_PLAYED
].postfix
= "";
186 s
[SI_NUM_PLAYED
].x
= 62;
187 s
[SI_NUM_PLAYED
].y
= 28;
188 s
[SI_NUM_PLAYED
].w
= 13;
189 s
[SI_NUM_PLAYED
].h
= FONT_HEIGHT
;
190 s
[SI_NUM_PLAYED
].r
= 0;
191 s
[SI_NUM_PLAYED
].g
= 0;
192 s
[SI_NUM_PLAYED
].b
= 0;
193 s
[SI_NUM_PLAYED
].font
= N_YELLOW
;
194 s
[SI_NUM_PLAYED
].align
= CENTER
;
196 s
[SI_UPTIME
].prefix
= "Up: ";
197 s
[SI_UPTIME
].postfix
= "";
201 s
[SI_UPTIME
].h
= FONT_HEIGHT
;
205 s
[SI_UPTIME
].font
= N_YELLOW
;
206 s
[SI_UPTIME
].align
= RIGHT
;
208 s
[SI_SELECTOR
].prefix
= "selector: ";
209 s
[SI_SELECTOR
].postfix
= "";
210 s
[SI_SELECTOR
].x
= 35;
211 s
[SI_SELECTOR
].y
= 48;
212 s
[SI_SELECTOR
].w
= 35;
213 s
[SI_SELECTOR
].h
= FONT_HEIGHT
;
214 s
[SI_SELECTOR
].r
= 0;
215 s
[SI_SELECTOR
].g
= 0;
216 s
[SI_SELECTOR
].b
= 0;
217 s
[SI_SELECTOR
].font
= N_YELLOW
;
218 s
[SI_SELECTOR
].align
= LEFT
;
220 s
[SI_FORMAT
].prefix
= "Format: ";
221 s
[SI_FORMAT
].postfix
= "";
225 s
[SI_FORMAT
].h
= FONT_HEIGHT
;
229 s
[SI_FORMAT
].font
= N_YELLOW
;
230 s
[SI_FORMAT
].align
= RIGHT
;
232 s
[SI_MTIME
].prefix
= "MTime: ";
233 s
[SI_MTIME
].postfix
= "";
237 s
[SI_MTIME
].h
= FONT_HEIGHT
;
241 s
[SI_MTIME
].font
= N_YELLOW
;
242 s
[SI_MTIME
].align
= LEFT
;
244 s
[SI_FILE_SIZE
].prefix
= "Size: ";
245 s
[SI_FILE_SIZE
].postfix
= "kb";
246 s
[SI_FILE_SIZE
].x
= 35;
247 s
[SI_FILE_SIZE
].y
= 42;
248 s
[SI_FILE_SIZE
].w
= 20;
249 s
[SI_FILE_SIZE
].h
= FONT_HEIGHT
;
250 s
[SI_FILE_SIZE
].r
= 0;
251 s
[SI_FILE_SIZE
].g
= 0;
252 s
[SI_FILE_SIZE
].b
= 0;
253 s
[SI_FILE_SIZE
].font
= N_YELLOW
;
254 s
[SI_FILE_SIZE
].align
= LEFT
;
256 s
[SI_AUDIO_INFO1
].prefix
= "";
257 s
[SI_AUDIO_INFO1
].postfix
= "";
258 s
[SI_AUDIO_INFO1
].x
= 0;
259 s
[SI_AUDIO_INFO1
].y
= 60;
260 s
[SI_AUDIO_INFO1
].w
= 100;
261 s
[SI_AUDIO_INFO1
].h
= FONT_HEIGHT
;
262 s
[SI_AUDIO_INFO1
].r
= 0;
263 s
[SI_AUDIO_INFO1
].g
= 0;
264 s
[SI_AUDIO_INFO1
].b
= 0;
265 s
[SI_AUDIO_INFO1
].font
= N_YELLOW
;
266 s
[SI_AUDIO_INFO1
].align
= CENTER
;
268 s
[SI_AUDIO_INFO2
].prefix
= "";
269 s
[SI_AUDIO_INFO2
].postfix
= "";
270 s
[SI_AUDIO_INFO2
].x
= 0;
271 s
[SI_AUDIO_INFO2
].y
= 65;
272 s
[SI_AUDIO_INFO2
].w
= 100;
273 s
[SI_AUDIO_INFO2
].h
= FONT_HEIGHT
;
274 s
[SI_AUDIO_INFO2
].r
= 0;
275 s
[SI_AUDIO_INFO2
].g
= 0;
276 s
[SI_AUDIO_INFO2
].b
= 0;
277 s
[SI_AUDIO_INFO2
].font
= N_YELLOW
;
278 s
[SI_AUDIO_INFO2
].align
= CENTER
;
280 s
[SI_AUDIO_INFO3
].prefix
= "";
281 s
[SI_AUDIO_INFO3
].postfix
= "";
282 s
[SI_AUDIO_INFO3
].x
= 0;
283 s
[SI_AUDIO_INFO3
].y
= 70;
284 s
[SI_AUDIO_INFO3
].w
= 100;
285 s
[SI_AUDIO_INFO3
].h
= FONT_HEIGHT
;
286 s
[SI_AUDIO_INFO3
].r
= 0;
287 s
[SI_AUDIO_INFO3
].g
= 0;
288 s
[SI_AUDIO_INFO3
].b
= 0;
289 s
[SI_AUDIO_INFO3
].font
= N_YELLOW
;
290 s
[SI_AUDIO_INFO3
].align
= CENTER
;
292 s
[SI_DBINFO1
].name
= "dbinfo1:";
293 s
[SI_DBINFO1
].prefix
= "";
294 s
[SI_DBINFO1
].postfix
= "";
296 s
[SI_DBINFO1
].y
= 83;
297 s
[SI_DBINFO1
].w
= 100;
298 s
[SI_DBINFO1
].h
= FONT_HEIGHT
;
302 s
[SI_DBINFO1
].font
= N_YELLOW
;
303 s
[SI_DBINFO1
].align
= CENTER
;
305 s
[SI_DBINFO2
].prefix
= "";
306 s
[SI_DBINFO2
].postfix
= "";
308 s
[SI_DBINFO2
].y
= 88;
309 s
[SI_DBINFO2
].w
= 100;
310 s
[SI_DBINFO2
].h
= FONT_HEIGHT
;
314 s
[SI_DBINFO2
].font
= N_YELLOW
;
315 s
[SI_DBINFO2
].align
= CENTER
;
317 s
[SI_DBINFO3
].name
= "dbinfo3:";
318 s
[SI_DBINFO3
].prefix
= "";
319 s
[SI_DBINFO3
].postfix
= "";
321 s
[SI_DBINFO3
].y
= 93;
322 s
[SI_DBINFO3
].w
= 100;
323 s
[SI_DBINFO3
].h
= FONT_HEIGHT
;
327 s
[SI_DBINFO3
].font
= N_YELLOW
;
328 s
[SI_DBINFO3
].align
= CENTER
;
332 * init SDL libary and set window title
334 static void init_SDL(void)
336 if (SDL_Init(SDL_INIT_VIDEO
) == -1) {
338 "Couldn't initialize SDL: %s\n", SDL_GetError());
341 /* Clean up on exit */
343 /* Initialize the display */
344 if (args_info
.fullscreen_flag
)
345 screen
= SDL_SetVideoMode(width
, height
, 0, SDL_FULLSCREEN
);
347 screen
= SDL_SetVideoMode(width
, height
, 0, 0);
349 fprintf(stderr
, "Couldn't set video mode: %s\n",
353 SDL_EventState(SDL_MOUSEMOTION
, SDL_IGNORE
);
354 SDL_EventState(SDL_MOUSEBUTTONDOWN
, SDL_IGNORE
);
355 SDL_EventState(SDL_MOUSEBUTTONUP
, SDL_IGNORE
);
356 /* Set the window manager title bar */
357 SDL_WM_SetCaption("The Gui of death that makes you blind (paraslash "
358 VERSION
")", "SFont");
362 * draw rectangular frame of width FRAME_WIDTH
364 static void draw_frame(Uint8 r
, Uint8 g
, Uint8 b
) {
370 rect
.h
= FRAME_WIDTH
;
371 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
372 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
375 rect
.y
= height
- FRAME_WIDTH
;
377 rect
.h
= FRAME_WIDTH
;
378 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
379 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
382 rect
.y
= FRAME_WIDTH
;
383 rect
.w
= FRAME_WIDTH
;
384 rect
.h
= height
- 2 * FRAME_WIDTH
;
385 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
386 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
388 rect
.x
= width
- FRAME_WIDTH
;
389 rect
.y
= FRAME_WIDTH
;
390 rect
.w
= FRAME_WIDTH
;
391 rect
.h
= height
- 2 * FRAME_WIDTH
;
392 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, r
, g
, b
));
393 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
397 * fill input rect with color
399 static void fill_input_rect(void)
405 rect
.w
= INPUT_WIDTH
;
406 rect
.h
= INPUT_HEIGHT
;
407 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 10, 150, 10));
411 * fill output rect with color
413 static void fill_output_rect(void)
419 rect
.w
= OUTPUT_WIDTH
;
420 rect
.h
= OUTPUT_HEIGHT
;
421 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
, 0, 0, 0));
425 * convert tab to space
427 static void tab2space(char *text
)
437 static void print_msg(const char *msg
)
439 SFont_FontInfo
*font
= &(fonts
[MSG_FONT
].fontinfo
);
440 char *buf
= strdup(msg
);
441 int len
= strlen(buf
);
445 while (TextWidth2(font
, buf
) > INPUT_WIDTH
&& len
> 0) {
450 PutString2(screen
, font
, INPUT_X
, INPUT_Y
, buf
);
454 static void update_all(void)
456 SDL_UpdateRect(screen
, 0, 0, 0, 0);
459 static void update_input(void)
461 SDL_UpdateRect(screen
, INPUT_X
, INPUT_Y
, INPUT_WIDTH
, INPUT_HEIGHT
);
466 * wait for key, ignore all other events, return 0 if there is no key event
467 * pending. Otherwise return keysym of key
473 while (SDL_PollEvent(&event
) > 0) {
474 if(event
.type
!= SDL_KEYDOWN
)
476 // printf("Key pressed, scancode: 0x%x\n",
477 // event.key.keysym.scancode);
478 return event
.key
.keysym
.sym
;
484 * print message, wait for key (blocking), return 1 for 'q', 0 else
486 static SDLKey
hit_key(const char *msg
)
492 while (!(sym
= get_key()))
503 * read paraslash command from input, execute it and print results
505 static int command_handler(void)
509 char text
[MAXLINE
]="";
510 char buf
[MAXLINE
]="";
511 SFont_FontInfo
*font
= &fonts
[OUTPUT_FONT
].fontinfo
;
513 // printf("string input\n");
514 SFont_Input2(screen
, &fonts
[INPUT_FONT
].fontinfo
,
515 INPUT_X
, INPUT_Y
- 5, INPUT_WIDTH
, text
);
518 if (!strcmp(text
, "exit") || !strcmp(text
, "quit"))
520 if (text
[0] == '!') {
523 pipe
= popen(text
+ 1, "r");
525 sprintf(buf
, BINDIR
"/para_client %s 2>&1", text
);
526 pipe
= popen(buf
, "r");
531 while(fgets(text
, MAXLINE
- 1, pipe
)) {
536 // printf("string: %s\n", dest);
537 while (TextWidth2(font
, text
) > width
- 2 * FRAME_WIDTH
&&
542 PutString2(screen
, font
, OUTPUT_X
,
543 OUTPUT_Y
+ count
* FONT_HEIGHT
, text
);
545 if (count
>= NUM_LINES
) {
547 if (hit_key("Hit any key to continue, q to return"))
554 hit_key("Hit any key to return");
555 out
: fill_output_rect();
562 * Add prefix and postfix to string, delete characters from the end
563 * if its length exceeds the max length defined in stat_items[item]
565 char *transform_string(int item
)
567 struct stat_item s
= stat_items
[item
];
570 unsigned pixels
= s
.w
* (width
- 2 * FRAME_WIDTH
) / 100;
571 SFont_FontInfo
*font
= &(fonts
[s
.font
].fontinfo
);
573 ret
= make_message("%s%s%s", s
.prefix
, s
.content
, s
.postfix
);
575 while (TextWidth2(font
, ret
) > pixels
&& len
> 0) {
582 SDL_Surface
*load_jpg(void)
585 int fds
[3] = {0, 1, 0};
589 if (para_exec_cmdline_pid(&pid
, args_info
.pic_cmd_arg
, fds
) < 0)
591 pipe
= fdopen(fds
[1], "r");
594 if (!(rwop
= SDL_RWFromFP(pipe
, 0)))
596 return IMG_LoadJPG_RW(rwop
);
599 void update_pic(void)
602 SDL_Rect src_pic_rect
= {
608 SDL_Rect dest_pic_rect
= {
610 .y
= OUTPUT_HEIGHT
/ 5,
618 if (!(img
= load_jpg()))
620 SDL_FillRect(screen
, &dest_pic_rect
, SDL_MapRGB(screen
->format
,
622 SDL_BlitSurface(img
, &src_pic_rect
, screen
, &dest_pic_rect
);
624 SDL_FreeSurface(img
);
628 * update status item number i.
630 static void do_update(int i
)
632 static int last_played
= -1;
635 SFont_FontInfo
*font
= &(fonts
[stat_items
[i
].font
].fontinfo
);
636 if (!stat_items
[i
].w
)
639 rect
.x
= stat_items
[i
].x
* (width
- FRAME_WIDTH
* 2) / 100
641 rect
.y
= stat_items
[i
].y
* (height
- 2 * FRAME_WIDTH
- INPUT_HEIGHT
)
643 rect
.w
= stat_items
[i
].w
* (width
- 2 * FRAME_WIDTH
) / 100;
644 rect
.h
= stat_items
[i
].h
;
645 buf
= transform_string(i
);
646 SDL_FillRect(screen
, &rect
, SDL_MapRGB(screen
->format
,
647 stat_items
[i
].r
, stat_items
[i
].g
, stat_items
[i
].b
));
648 switch(stat_items
[i
].align
) {
650 PutString2(screen
, font
,
651 rect
.x
+ (rect
.w
- TextWidth2(font
, buf
)) / 2,
655 PutString2(screen
, font
, rect
.x
, rect
.y
, buf
);
658 PutString2(screen
, font
, rect
.x
+ (rect
.w
-
659 TextWidth2(font
, buf
)), rect
.y
, buf
);
663 SDL_UpdateRect(screen
, rect
.x
, rect
.y
, rect
.w
, rect
.h
);
664 if (i
== SI_NUM_PLAYED
&& atoi(stat_items
[i
].content
) != last_played
) {
666 last_played
= atoi(stat_items
[i
].content
);
671 * Check if buf is a known status line. If so call do_update and return 1.
672 * Return 0 otherwise.
674 void update_status(char *buf
)
678 i
= stat_line_valid(buf
);
681 //free(stat_items[i].content);
682 stat_items
[i
].content
= para_strdup(buf
+
683 strlen(status_item_list
[i
]) + 1);
688 * Read stat line from pipe if pipe is ready, call update_status to
689 * display information.
691 static int draw_status(int pipe
)
698 tv
.tv_usec
= 3000000;
701 ret
= select(pipe
+ 1, &rfds
, NULL
, NULL
, &tv
);
702 // printf("select returned %d\n", ret);
705 if (read_audiod_pipe(pipe
, update_status
) > 0)
707 // clear_all_items();
708 free(stat_items
[SI_STATUS_BAR
].content
);
709 stat_items
[SI_STATUS_BAR
].content
=
710 para_strdup("audiod not running!?\n");
716 static void clean_exit(int ret
)
722 static void print_help(void)
724 print_msg("Hit q to quit, any other key to enter command mode");
727 static int configfile_exists(void)
729 if (!args_info
.config_file_given
) {
730 char *home
= para_homedir();
731 args_info
.config_file_arg
= make_message(
732 "%s/.paraslash/sdl_gui.conf", home
);
735 return file_exists(args_info
.config_file_arg
);
741 int main(int argc
, char *argv
[])
746 cmdline_parser(argc
, argv
, &args_info
);
747 ret
= configfile_exists();
748 // printf("w=%i,h=%i,ret=%i, cf=%s\n", width, height, ret, args_info.config_file_arg);
750 if (!ret
&& args_info
.config_file_given
) {
751 fprintf(stderr
, "Can't read config file %s\n",
752 args_info
.config_file_arg
);
756 cmdline_parser_configfile(args_info
.config_file_arg
,
757 &args_info
, 0, 0, 0);
758 signal(SIGCHLD
, SIG_IGN
);
759 width
= args_info
.width_arg
;
760 height
= args_info
.height_arg
;
761 // printf("w=%i,h=%i,ret=%i, cf=%s\n", width, height, ret, args_info.config_file_arg);
763 pipe
= para_open_audiod_pipe(args_info
.stat_cmd_arg
);
765 for (i
= 0; fonts
[i
].name
[0]; i
++) {
767 sprintf(buf
, "%s/%s", FONTDIR
, fonts
[i
].name
);
768 /* Load the font - You don't have to use the IMGlib for this */
769 fonts
[i
].fontinfo
.Surface
= IMG_Load(buf
);
770 /* Prepare the font for use */
771 InitFont2(&fonts
[i
].fontinfo
);
773 draw_frame(FRAME_RED
, FRAME_GREEN
, FRAME_BLUE
);
774 if (args_info
.interactive_flag
) {
779 ret
= draw_status(pipe
);
784 if (SDL_QuitRequested())
786 while ((sym
= get_key())) {
787 if (!args_info
.interactive_flag
)
791 if ( sym
== SDLK_LSHIFT
792 || sym
== SDLK_RSHIFT
798 || sym
== SDLK_CAPSLOCK
801 || sym
== SDLK_RSUPER
802 || sym
== SDLK_LSUPER
803 || sym
== SDLK_COMPOSE
807 // printf("closing pipe\n");
810 // printf("pipe closed\n");
814 if (!command_handler())
819 SDL_UpdateRect(screen
, 0, 0, 0, 0);
820 pipe
= para_open_audiod_pipe(args_info
.stat_cmd_arg
);