1 /*-
2  * Copyright (c) 2009-2021 Hans Petter Selasky. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef _MIDIPP_MAINWINDOW_H_
27 #define	_MIDIPP_MAINWINDOW_H_
28 
29 #include "midipp.h"
30 
31 class MppMainWindow : public QWidget
32 {
33 	Q_OBJECT;
34 
35 public:
36 	MppMainWindow(QWidget *parent = 0);
37 	~MppMainWindow();
38 
39 #ifdef HAVE_SCREENSHOT
40 	void ScreenShot(QApplication &);
41 #endif
42 	void MidiInit(void);
43 	void MidiUnInit(void);
44 
45 	void atomic_lock(void);
46 	void atomic_unlock(void);
47 
48 	void closeEvent(QCloseEvent *event);
49 	void handle_stop(int flag = 0);
50 	void handle_midi_file_open(int);
51 	void handle_midi_file_clear_name(void);
52 	void handle_midi_file_instr_prepend(void);
53 	void handle_midi_file_instr_delete(void);
54 	void handle_jump_locked(int index);
55 	void handle_make_scores_visible(MppScoreMain *);
56 	void handle_make_tab_visible(QWidget *);
57 
58 	QString get_midi_score_duration(uint32_t *psum);
59 	int log_midi_score_duration();
60 	int convert_midi_duration(struct umidi20_track *, uint32_t thres, uint32_t chan_mask);
61 	void import_midi_track(struct umidi20_track *, uint32_t = 0, int = -1, int = 0);
62 
63 	void update_play_device_no(void);
64 
65 	void do_clock_stats(void);
66 	int do_extended_alloc(int key, int refcount);
67 	void do_key_press(int key, int vel, int dur);
68   	void do_key_pitch(int key, int pressure);
69 	void do_key_pressure(int key, int pressure);
70 	void do_key_control(int key, uint8_t control, int value);
71 	void output_key(int index, int chan, int key, int vel, int delay, int dur);
72 	void output_key_pitch(int index, int chan, int key, int amount, uint32_t delay = 0);
73 	void output_key_control(int index, int chan, int key, uint8_t control, int value, uint32_t delay = 0);
74 	void output_key_pressure(int index, int chan, int key, int pressure, uint32_t delay = 0);
75 
76 	uint32_t get_time_offset(void);
77 
78 	uint8_t noise8(uint8_t factor);
79 	uint8_t do_instr_check(struct umidi20_event *event, int = 0);
80 	bool check_play(uint8_t index, uint8_t chan, uint32_t off, uint8_t = MPP_MAGIC_DEVNO);
81 	bool check_record(uint8_t index, uint8_t chan, uint32_t off);
82 
83 	void handle_watchdog_sub(MppScoreMain *, int);
84 
85 	void send_song_stop_locked();
86 	void send_song_trigger_locked();
87 	void send_song_select_locked(uint8_t);
88 
89 	void send_byte_event_locked(uint8_t);
90 
91 	MppScoreMain *getCurrTransposeView(void);
92 
93 	QPlainTextEdit *currEditor();
94 	MppScoreMain *currScores();
95 
96 	pthread_mutex_t mtx;
97 
98 	QFont defaultFont;
99 	QFont editFont;
100 	QFont printFont;
101 	QFont showFont;
102 
103 	uint8_t auto_zero_start[0];
104 
105 	struct MppInstr instr[16];
106 
107 	int extended_keys[128][2];
108 
109 	uint32_t convLineStart[MPP_MAX_LINES];
110 	uint32_t convLineEnd[MPP_MAX_LINES];
111 	uint32_t convIndex;
112 
113 	uint32_t lastKeyPress;
114 	uint32_t noiseRem;
115 
116 	uint32_t devInputMask[MPP_MAX_DEVS];
117 	uint32_t startPosition;
118 	uint32_t pausePosition;
119 	uint32_t deviceBits;
120 #define	MPP_DEV0_PLAY	0x0001UL
121 #define	MPP_DEV0_RECORD	0x0002UL
122 
123 	uint16_t trackVolume[MPP_MAX_TRACKS];
124 	int16_t masterPitchBend;
125 
126 	uint8_t devSelMap[MPP_MAX_DEVS];
127 	uint8_t muteProgram[MPP_MAX_DEVS];
128 	uint8_t mutePedal[MPP_MAX_DEVS];
129 	uint8_t enableLocalKeys[MPP_MAX_DEVS];
130 	uint8_t disableLocalKeys[MPP_MAX_DEVS];
131 	uint8_t muteAllControl[MPP_MAX_DEVS];
132 	uint8_t muteAllNonChannel[MPP_MAX_DEVS];
133 	uint8_t muteMap[MPP_MAX_DEVS][16];
134 	uint8_t cursorUpdate;
135 
136 	uint8_t scoreRecordOn;
137 	uint8_t controlRecordOn;
138 	uint8_t instrUpdated;
139 	uint8_t midiRecordOff;
140 	uint8_t midiPlayOff;
141 	uint8_t midiTriggered;
142 	uint8_t midiPaused;
143 	uint8_t lastViewIndex;
144 	uint8_t keyModeUpdated;
145 	uint8_t doOperation;
146 #define	MPP_OPERATION_PAUSE 0x01
147 #define	MPP_OPERATION_REWIND 0x02
148 #define	MPP_OPERATION_BPM 0x04
149 
150 	uint8_t noteMode;
151 
152 	char *deviceName[MPP_MAX_DEVS];
153 
154 	QGridLayout *main_gl;
155 
156 	MppTabBar *main_tb;
157 	int main_tb_state;
158 
159 	/* main <> */
160 
161 	QPushButton *mwRewind;
162 	QPushButton *mwPlay;
163 	QPushButton *mwReload;
164 	QPushButton *mwPaste;
165 	QPushButton *mwCopy;
166 	QPushButton *mwUndo;
167 	QPushButton *mwRedo;
168 	QPushButton *mwEdit;
169 	QPushButton *mwUpDown;
170 
171 	/* tab <Scores> */
172 
173 	MppScoreMain *scores_main[MPP_MAX_VIEWS];
174 
175 	/* tab <Import> */
176 
177 	MppImportTab *tab_import;
178 
179 	QTimer *watchdog;
180 
181 	/* tab <File> */
182 
183 	MppGridLayout *tab_file_gl;
184 
185 	QPushButton *but_quit;
186 
187 	MppGroupBox *gb_midi_file;
188 	QPushButton *but_midi_file_new;
189 	QPushButton *but_midi_file_open_single;
190 	QPushButton *but_midi_file_open_multi;
191 	QPushButton *but_midi_file_merge_single;
192 	QPushButton *but_midi_file_merge_multi;
193 	QPushButton *but_midi_file_save;
194 	QPushButton *but_midi_file_save_as;
195 	MppButton *but_midi_file_import[MPP_MAX_VIEWS];
196 
197 	MppGroupBox *gb_gpro_file_import;
198 	MppButton *but_gpro_file_import[MPP_MAX_VIEWS];
199 
200 	MppGroupBox *gb_mxml_file_import;
201 	MppButton *but_mxml_file_import[MPP_MAX_VIEWS];
202 
203 	/* tab <Play> */
204 
205 	MppGroupBox *gl_ctrl;
206 	MppGroupBox *gl_time;
207 	MppGroupBox *gl_bpm;
208 	MppGroupBox *gl_synth_play;
209 	MppGroupBox *gl_tuning;
210 
211 	QLCDNumber *lbl_curr_time_val;
212 	QLCDNumber *lbl_bpm_avg_val;
213 
214 	MppGridLayout *tab_play_gl;
215 
216 	MppVolume *spn_tuning;
217 
218 	MppButtonMap *mbm_midi_play;
219 	MppButtonMap *mbm_midi_record;
220 	MppButtonMap *mbm_score_record;
221 	MppButtonMap *mbm_key_mode_a;
222 	MppButtonMap *mbm_key_mode_b;
223 
224 	QPushButton *but_jump[MPP_MAX_LBUTTON];
225 	QPushButton *but_compile;
226 	QPushButton *but_midi_pause;
227 	QPushButton *but_midi_trigger;
228 	QPushButton *but_midi_rewind;
229 	QPushButton *but_bpm;
230 
231 	MppButton *but_mode[MPP_MAX_VIEWS];
232 	MppMode *dlg_mode[MPP_MAX_VIEWS];
233 
234 	MppBpm *dlg_bpm;
235 
236 	/* tab <Chord> */
237 
238 	MppDecodeTab *tab_chord_gl;
239 
240 	/* tab <PianoTab> */
241 
242 	MppPianoTab *tab_pianotab;
243 
244 	/* tab <Configuration> */
245 
246 	MppGridLayout *tab_config_gl;
247 
248 	MppGroupBox *gb_config_device;
249 
250 	MppSettings *mpp_settings;
251 
252 	MppDevSel *but_config_sel[MPP_MAX_DEVS];
253 	MppButton *but_config_dev[MPP_MAX_DEVS];
254 	MppButton *but_config_mm[MPP_MAX_DEVS];
255 	QLineEdit *led_config_dev[MPP_MAX_DEVS];
256 	MppCheckBox *cbx_config_dev[MPP_MAX_DEVS][1 + MPP_MAX_VIEWS];
257 
258 	uint32_t dirty_config_mask;
259 
260 	QTimer *tim_config_apply;
261 	QPushButton *but_config_view_fontsel;
262 	QPushButton *but_config_edit_fontsel;
263 	QPushButton *but_config_print_fontsel;
264 
265 	QString *CurrMidiFileName;
266 
267 	/* tab <Shortcut> */
268 
269 	MppShortcutTab *tab_shortcut;
270 
271 	/* tab <Custom> */
272 
273 	MppCustomTab *tab_custom;
274 
275 	/* tab <Instrument> */
276 
277 	MppInstrumentTab *tab_instrument;
278 
279 	/* tab <Loop> */
280 
281 	MppLoopTab *tab_loop;
282 
283 	/* tab <RePlay> */
284 
285 	MppReplayTab *tab_replay;
286 
287 	/* tab <DataBase> */
288 
289 	MppDataBase *tab_database;
290 
291 	/* tab <Show> */
292 #ifndef HAVE_NO_SHOW
293 	MppShowControl *tab_show_control;
294 #endif
295 	/* tab <Help> */
296 
297 	QPlainTextEdit *tab_help;
298 
299 	/* MIDI stuff */
300 	struct mid_data mid_data;
301 	struct umidi20_song *song;
302 	struct umidi20_track *track[MPP_MAX_TRACKS];
303 
304 	uint8_t auto_zero_end[0];
305 
306 public slots:
307 	void handle_quit();
308 	void handle_jump(int index);
309 	void handle_compile(int force = 0);
310 	void handle_score_record(int);
311 	void handle_midi_record(int);
312 	void handle_midi_pause();
313 	void handle_midi_play(int);
314 	void handle_play_press(int, int);
315 	void handle_play_release(int, int);
316 	void handle_sustain_press(int);
317 	void handle_sustain_release(int);
318 	void handle_watchdog();
319 	void handle_midi_file_new();
320 	void handle_midi_file_merge_single_open();
321 	void handle_midi_file_new_single_open();
322 	void handle_midi_file_merge_multi_open();
323 	void handle_midi_file_new_multi_open();
324 	void handle_midi_file_save();
325 	void handle_midi_file_save_as();
326 	void handle_rewind();
327 	void handle_midi_trigger();
328 	void handle_config_changed();
329 	void handle_config_apply();
330   	void handle_config_local_keys();
331 	void handle_config_reload();
332 	void handle_config_view_fontsel();
333 	void handle_config_edit_fontsel();
334 	void handle_config_print_fontsel();
335 
336 	void handle_midi_file_import(int);
337 	void handle_gpro_file_import(int);
338 	void handle_mxml_file_import(int);
339 
340 	void handle_mute_map(int);
341 	int handle_config_dev(int, int = 0);
342 
343 	void handle_bpm();
344 	void handle_mode(int,int = 1);
345 
346 	void handle_key_mode_a(int);
347 	void handle_key_mode_b(int);
348 
349 	void handle_move_right();
350 	void handle_move_left();
351 
352 	void handle_tab_changed(int force = 0);
353 
354 	void handle_copy();
355 	void handle_paste();
356 	void handle_redo();
357 	void handle_undo();
358 	void handle_edit();
359 	void handle_up_down();
360 	void handle_tuning();
361 };
362 
363 #endif		/* _MIDIPP_MAINWINDOW_H_ */
364