added option to hide fps label.
[gjacktransport.git] / src / resourceconfig.c
blob6aba191836e4707028dd434c1df28ed65faf72f9
1 /* gjacktransport - .rc interface
2 * Copyright (C) 2007, 2010 Robin Gareus <robin@gareus.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, or (at your option)
7 * 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 Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #ifndef GJACKTRANSPORTRC
24 # define GJACKTRANSPORTRC "gjacktransportrc"
25 #endif
27 #ifdef PACKAGE_SYSCONFIGDIR
28 # define SYSCFGDIR PACKAGE_SYSCONFIGDIR
29 #else
30 # define SYSCFGDIR "/etc"
31 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
40 #include <gtk/gtk.h>
41 #include "gjack.h"
42 #include "gjacktransport.h"
43 #include "framerate.h"
45 /* test if file exists and is a regular file - returns 1 if ok */
46 int testfile (char *filename) {
47 struct stat s;
48 int result= stat(filename, &s);
49 if (result != 0) return 0; /* stat() failed */
50 if (S_ISREG(s.st_mode)) return 1; /* is a regular file - ok */
51 return(0);
54 extern FrameRate *fffr;
55 extern FrameRate *uffr;
56 extern FrameRate *jffr;
58 extern gint key_cfg[MAX_KEYBINDING];
60 extern int mem;
61 extern double stride;
62 extern gjtdata stg[MAX_MEM];
63 extern GtkWidget *window1;
65 extern int no_lash;
66 extern int want_quiet;
68 /* restore config functions - see callbacks.c */
69 void select_mem (int newmem);
70 void showhide (char *togglebtn, char *widget, gboolean display);
71 gboolean ishidden (char *widget); //toggle-btn name
72 void dothejack (gboolean onoff);
73 void update_spinbtns(void);
75 gint my_root_x, my_root_y, my_width, my_height;
77 int parse_rc(const char*key, const char *value) {
78 if (!strncmp(key,"memory_",7)) {
79 int mymem = atoi(key+7);
80 if (mymem < 1 || mymem > MAX_MEM) return -1;
81 mymem--;
82 if (!strcmp(key+9,"_start")) {
83 stg[mymem].start=strtod(value, NULL);
84 } else if (!strcmp(key+9,"_end")) {
85 stg[mymem].end=strtod(value, NULL);
86 } else if (!strcmp(key+9,"_unit")) {
87 stg[mymem].unit=atoi(value);
88 } else {
89 if (!want_quiet)
90 printf("Warning: unhandled mem: %s=%f\n",key+7,strtod(value, NULL));
91 return -1;
93 //update_spinbtns();
94 } else if (!strncmp(key,"keybinding_",11)) {
95 int mykey = atoi(key+11);
96 if (mykey < 1 || mykey > MAX_KEYBINDING) return -1;
97 mykey--;
98 key_cfg[mykey]=atoi(value);
99 } else if (!strcmp(key,"syncsource")) {
100 dothejack(atoi(value));
101 } else if (!strcmp(key,"cur_mem")) {
102 select_mem(atoi(value)-1);
103 } else if (!strcmp(key,"seek_stride")) {
104 stride=strtod(value, NULL);
105 } else if (!strcmp(key,"tb_status")) {
106 showhide("checkbutton5","statusbar1",atoi(value));
107 } else if (!strcmp(key,"tb_zoom")) {
108 showhide("checkbutton3","handlebox3",atoi(value));
109 } else if (!strcmp(key,"tb_memory")) {
110 showhide("checkbutton7","handlebox4",atoi(value));
111 } else if (!strcmp(key,"tb_all")) {
112 showhide("checkbutton6","table1",atoi(value));
113 } else if (!strcmp(key,"tb_transport")) {
114 showhide("checkbutton4","handlebox1",atoi(value));
115 } else if (!strcmp(key,"tb_unit")) {
116 showhide("checkbutton2","handlebox2",atoi(value));
117 } else if (!strcmp(key,"tb_timelabels")) {
118 showhide("checkbutton8","hbox_labels",atoi(value));
119 } else if (!strcmp(key,"FR_mode")) {
120 if (atoi(value)) fffr=jffr; // XXX
121 else fffr=uffr;
122 } else if (!strcmp(key,"FR_flags")) {
123 uffr->flags=atoi(value);
124 } else if (!strcmp(key,"win_x")) {
125 my_root_x=atoi(value);
126 } else if (!strcmp(key,"win_y")) {
127 my_root_y=atoi(value);
128 } else if (!strcmp(key,"win_w")) {
129 my_width=atoi(value);
130 } else if (!strcmp(key,"win_h")) {
131 my_height=atoi(value);
132 } else if (!strcmp(key,"no_lash")) {
133 no_lash=atoi(value);
134 } else {
135 if (!want_quiet)
136 fprintf (stderr, "WARNING: unhandled Config parameter Key = '%s' \n", key);
137 return -1;
139 return 0;
142 #define MAX_LINE_LEN 256
144 void read_rc(const char *fn) {
145 FILE* config_fp;
146 gtk_window_get_size (GTK_WINDOW(window1), &my_width, &my_height);
147 gtk_window_get_position (GTK_WINDOW(window1), &my_root_x, &my_root_y);
149 char line[MAX_LINE_LEN];
150 int lineno=0;
152 if (!want_quiet)
153 printf("reading .rc: '%s'\n", fn);
155 if (!(config_fp = fopen(fn, "r"))) {
156 if (!want_quiet)
157 fprintf(stderr,"reading configfile failed: %s (%s)\n",fn,strerror(errno));
158 return;
160 while( fgets( line, MAX_LINE_LEN-1, config_fp ) != NULL ) {
161 char *item, *token, *value;
162 lineno++;
163 line[MAX_LINE_LEN-1]=0;
164 token = strtok(line, "\t =\n\r");
166 if(token == NULL || token[0] == '#' || token[0] == ';') continue;
167 item=strdup(token);
168 token = strtok(NULL, "\t =\n\r");
169 if (!token) {
170 free(item);
171 #ifdef CFG_WARN_ONLY
172 printf("WARNING: ignored line in config file. %s:%d\n",fn,lineno);
173 continue;
174 #else
175 printf("ERROR parsing config file. %s:%d\n",fn,lineno);
176 exit(1);
177 #endif
179 value=strdup(token);
180 if (parse_rc(item,value)) {
181 #ifdef CFG_WARN_ONLY
182 printf("WARNING: ignored error in config file. %s:%d\n",fn,lineno);
183 #else
184 printf("ERROR parsing config file. %s:%d\n",fn,lineno);
185 exit(1);
186 #endif
188 free(item); free(value);
190 fclose(config_fp);
192 update_spinbtns();
193 if (my_width > 0 && my_height > 0)
194 gtk_window_resize(GTK_WINDOW(window1),my_width,my_height);
195 if (my_root_x > 0 && my_root_y > 0)
196 gtk_window_move(GTK_WINDOW(window1),my_root_x,my_root_y);
199 void load_rc (void) {
200 char *home;
201 char filename[PATH_MAX];
202 if ((strlen(SYSCFGDIR) + strlen(GJACKTRANSPORTRC) + 2) < PATH_MAX) {
203 sprintf(filename, "%s/%s", SYSCFGDIR, GJACKTRANSPORTRC);
204 if (testfile(filename)) read_rc(filename);
206 home = getenv("HOME");
207 if (home && (strlen(home) + strlen(GJACKTRANSPORTRC) + 2) < PATH_MAX) {
208 sprintf(filename, "%s/.%s", home, GJACKTRANSPORTRC);
209 if (testfile(filename)) read_rc(filename);
211 if (strlen(GJACKTRANSPORTRC) + 2 < PATH_MAX) {
212 sprintf(filename, "./%s", GJACKTRANSPORTRC);
213 if (testfile(filename)) read_rc(filename);
217 void write_rc(const char *fn) {
218 int i;
219 FILE* fp;
220 if (!(fp = fopen(fn, "w"))) {
221 if (!want_quiet)
222 fprintf(stderr,"writing configfile failed: %s (%s)\n",fn,strerror(errno));
223 return;
225 gtk_window_get_size (GTK_WINDOW(window1), &my_width, &my_height);
226 gtk_window_get_position (GTK_WINDOW(window1), &my_root_x, &my_root_y);
228 fprintf(fp, "# config file for gjacktransport\n#\n# lines beginning with '#' or ';' are ignored.\n#\n");
229 fprintf(fp, "# @sysconfdir@/gjacktransportrc\n# $HOME/.gjacktransportrc\n# ./gjacktransportrc\n#\n");
231 fprintf(fp, "\n# PRESET/MEMORY #\n; load-memory preset: 01..06\n");
232 fprintf(fp, "cur_mem=%02i\n", mem+1);
234 fprintf(fp, "\n; mem-id 01..06\n; start/end values are in seconds\n; unit: hour:0, min:1, sec:2, audio-frames:3 - display only\n");
235 for (i=0; i< MAX_MEM; i++) {
236 fprintf(fp, "memory_%02i_start=%f\n", i+1, stg[i].start);
237 fprintf(fp, "memory_%02i_end=%f\n", i+1, stg[i].end);
238 fprintf(fp, "memory_%02i_unit=%i\n\n", i+1, stg[i].unit);
241 fprintf(fp, "\n# KEYBINDINGS #\n");
242 const char *kbc[MAX_KEYBINDING] = {
243 /* TODO : sync w/ callbacks.c -> .h*/
244 "play/pause", "fast-rew", "fast-ffw", "play", "pause", "rewind", "skip-start", "skip-end"
247 for (i=0; i< MAX_KEYBINDING; i++) {
248 fprintf(fp ,"; %s\n", kbc[i]);
249 fprintf(fp ,"keybinding_%i=%i\n",i+1, key_cfg[i]);
252 fprintf(fp, "\n\n# SHOW/HIDE TOOL-BARS #\n");
253 fprintf(fp, "tb_all=%i\n", ishidden("checkbutton6")?1:0);
254 fprintf(fp, "tb_status=%i\n", ishidden("checkbutton5")?1:0);
255 fprintf(fp, "tb_zoom=%i\n", ishidden("checkbutton3")?1:0);
256 fprintf(fp, "tb_memory=%i\n", ishidden("checkbutton7")?1:0);
257 fprintf(fp, "tb_transport=%i\n", ishidden("checkbutton4")?1:0);
258 fprintf(fp, "tb_unit=%i\n", ishidden("checkbutton2")?1:0);
259 fprintf(fp, "tb_timelabels=%i\n", ishidden("checkbutton8")?1:0);
261 fprintf(fp, "\n\n# WINDOW POSITION/SIZE #\n");
262 fprintf(fp, "win_x=%i\n", my_root_x);
263 fprintf(fp, "win_y=%i\n", my_root_y);
264 fprintf(fp, "win_w=%i\n", my_width);
265 fprintf(fp, "win_h=%i\n", my_height);
267 fprintf(fp, "\n\n# MISC #\n");
268 fprintf(fp, "; set to 1 to auto-connect to JACK\n");
269 fprintf(fp, "syncsource=%i\n", jack_connected()?1:0);
270 fprintf(fp, "\n; 1: use JACK video-framerate (if set by time-master)\n");
271 fprintf(fp, "FR_mode=%i\n", (jffr==fffr)?1:0);
272 fprintf(fp, "\n; seek-stride (in seconds)\n");
273 fprintf(fp, "seek_stride=%f\n", stride);
274 fprintf(fp, "\n; do not try to use LASH\n");
275 fprintf(fp, "no_lash=%i\n", no_lash?1:0);
276 fprintf(fp, "\n");
278 fclose(fp);
279 if (!want_quiet)
280 fprintf(stderr,"written config: %s\n",fn);