4e420b75d4cc4c1be2ada8f86193092be908461e
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <glib.h>
7 #include "config.h"
8 #include "libmpdclient.h"
9 #include "support.h"
10 #include "mpc.h"
11 #include "options.h"
12 #include "command.h"
13 #include "screen.h"
15 static mpd_client_t *mpc = NULL;
18 void
19 exit_and_cleanup(void)
20 {
21 screen_exit();
22 charset_close();
23 if( mpc )
24 {
25 if( mpc_error(mpc) )
26 fprintf(stderr,"Error: %s\n", mpc_error_str(mpc));
27 mpc_close(mpc);
28 }
29 }
31 void
32 catch_sigint( int sig )
33 {
34 printf( "\nExiting...\n");
35 exit(EXIT_SUCCESS);
36 }
38 int
39 main(int argc, const char *argv[])
40 {
41 options_t *options;
42 struct sigaction act;
43 int counter, connected;
45 /* parse command line options */
46 options_init();
47 options = options_parse(argc, argv);
49 /* initialize local charset */
50 if( charset_init() )
51 exit(EXIT_FAILURE);
53 /* setup signal behavior - SIGINT */
54 sigemptyset( &act.sa_mask );
55 act.sa_flags = 0;
56 act.sa_handler = catch_sigint;
57 if( sigaction( SIGINT, &act, NULL )<0 )
58 {
59 perror("signal");
60 exit(EXIT_FAILURE);
61 }
62 /* setup signal behavior - SIGWINCH */
63 sigemptyset( &act.sa_mask );
64 act.sa_flags = 0;
65 act.sa_handler = screen_resized;
66 if( sigaction( SIGWINCH, &act, NULL )<0 )
67 {
68 perror("sigaction()");
69 exit(EXIT_FAILURE);
70 }
71 /* setup signal behavior - SIGTERM */
72 sigemptyset( &act.sa_mask );
73 act.sa_flags = 0;
74 act.sa_handler = catch_sigint;
75 if( sigaction( SIGTERM, &act, NULL )<0 )
76 {
77 perror("sigaction()");
78 exit(EXIT_FAILURE);
79 }
81 /* set xterm title */
82 if( getenv("DISPLAY") )
83 printf("%c]0;%s%c", '\033', PACKAGE " v" VERSION, '\007');
85 /* install exit function */
86 atexit(exit_and_cleanup);
88 /* connect to our music player daemon */
89 mpc = mpc_connect(options->host, options->port);
90 if( mpc_error(mpc) )
91 exit(EXIT_FAILURE);
93 /* initialize curses */
94 screen_init();
96 counter=0;
97 connected=1;
98 while( connected || options->reconnect )
99 {
100 command_t cmd;
102 if( connected && counter==0 )
103 {
104 mpc_update(mpc);
105 if( mpc_error(mpc) )
106 {
107 connected=0;
108 screen_status_printf("Lost connection to %s", options->host);
109 mpd_closeConnection(mpc->connection);
110 mpc->connection = NULL;
111 }
112 else
113 mpd_finishCommand(mpc->connection);
114 counter=10;
115 }
117 if( connected )
118 {
119 screen_update(mpc);
120 if( (cmd=get_keyboard_command()) != CMD_NONE )
121 {
122 screen_cmd(mpc, cmd);
123 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
124 counter=10;
125 else
126 counter=0;
127 }
128 }
129 else if( options->reconnect )
130 {
131 sleep(3);
132 screen_status_printf("Connecting to %s... [Press Ctrl-C to abort]",
133 options->host);
134 if( mpc_reconnect(mpc, options->host, options->port) == 0 )
135 {
136 screen_status_printf("Connected to %s!", options->host);
137 connected=1;
138 counter=0;
139 }
140 }
142 if( counter>0 )
143 counter--;
145 }
146 exit(EXIT_FAILURE);
147 }