~torios-dev/+junk/fltk-volume

« back to all changes in this revision

Viewing changes to src/fltk-indicator.cxx

  • Committer: Israel Dahl
  • Date: 2020-04-24 12:35:20 UTC
  • Revision ID: israeldahl@gmail.com-20200424123520-6vpi5z489weawper
heavily modified volume indicator to behave more intuitively
fixed generate_icons to make better styled code, and add function to get mute icon

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
bool FltkVolume::isMuted() {
41
41
  bool mute = false;
42
42
  int val = AlsaMute(AUDIO_VOLUME_GET);
 
43
  
43
44
  if(val == 0)
44
45
    mute=true;
45
 
  else
46
 
    std::cout<<"Mute value = "<<val<<std::endl;
 
46
  
47
47
  return mute;
48
48
}
49
49
 
192
192
  return retval;
193
193
}
194
194
 
 
195
int Volume_Slider::handle(int event) {
 
196
  Indicator* Indy = (Indicator*)(parent()->user_data());
 
197
  int ret = Fl_Slider::handle(event);
 
198
  std::string volume_text;
 
199
  int prcnt_int = Indy->get_volume();
 
200
  switch(event)
 
201
  {
 
202
    case FL_PUSH:
 
203
  
 
204
      Indy->change_volume(this->value());
 
205
      volume_text = Indy->volumeText() + " %";
 
206
      copy_tooltip(volume_text.c_str());
 
207
      Indy->update_volume();
 
208
      holding = true;
 
209
      return ret;
 
210
    case FL_RELEASE:
 
211
       Indy->change_volume(this->value());
 
212
       Indy->update_volume();//     sleep(0.7);
 
213
       parent()->hide();
 
214
       holding = false;
 
215
       return 1;
 
216
    default:
 
217
      Indy->change_volume(this->value());
 
218
      Indy->update_volume();
 
219
      value(prcnt_int);
 
220
      redraw();
 
221
      return ret;
 
222
  }
 
223
  return ret;
 
224
}
 
225
 
 
226
Volume_Slider::Volume_Slider(int X, int Y, int W, int H, const char *L):Fl_Slider(X, Y, W, H, L) {
 
227
  holding = false;
 
228
  tooltip(gettext("Volume"));
 
229
  Fl_Slider::box(FL_FLAT_BOX);
 
230
  color(FL_DARK3);
 
231
  labelcolor(FL_BACKGROUND2_COLOR);
 
232
  maximum(100);
 
233
  step(1);
 
234
}
 
235
 
195
236
void Indicator::cb_Close_i(Fl_Button*, void*) {
196
237
  about_win->hide();
197
238
}
269
310
  ((Indicator*)(o->parent()->user_data()))->cb_Cancel1_i(o,v);
270
311
}
271
312
 
272
 
void Indicator::cb_slide_i(Fl_Slider* o, void*) {
273
 
  change_volume(o->value());
274
 
std::string vol=volumeText()+ " %";
275
 
o->copy_tooltip(vol.c_str());
276
 
update_volume();
277
 
volume_slider->hide();
278
 
}
279
 
void Indicator::cb_slide(Fl_Slider* o, void* v) {
280
 
  ((Indicator*)(o->parent()->user_data()))->cb_slide_i(o,v);
281
 
}
282
 
 
283
313
Indicator::Indicator(void *Class, bool isBattery, int X , int Y , int W , int H ,const char*l ):Fl_Window(X,Y,W,H,l) {
284
314
  menu = new Fl_Menu_Bar(0, 0, 520, 25);
285
315
  menu->hide();
293
323
  About = gettext("About");
294
324
  Quit = gettext("Quit");
295
325
  user_data(this);
296
 
  battery=isBattery;
297
 
  CHARGING=false;
298
 
  LIGHT=false;
299
 
  NOTIFIEDLOW=false;
300
 
  BG_CHANGED=false;
301
 
  ActionOnClose=0;
302
 
  ICON=0;
303
 
  MIXER_PROGRAM="";
 
326
  battery = isBattery;
 
327
  CHARGING = false;
 
328
  LIGHT = false;
 
329
  NOTIFIEDLOW = false;
 
330
  BG_CHANGED = false;
 
331
  ActionOnClose = 0;
 
332
  ICON = 0;
 
333
  MIXER_PROGRAM = "";
304
334
  Icon = new Fl_Box(0,0,w(),h());
305
335
  Icon->box(FL_NO_BOX);
306
336
  Icon->show();
307
 
  if(battery)
308
 
  {
309
 
    trace("setup for battery");
310
 
  }
311
 
  else
312
 
  {
313
 
    trace("Setup for volume");
314
 
    MIXER_PROGRAM=get_mixer();
 
337
  
 
338
  if(!battery)
 
339
  {
 
340
    MIXER_PROGRAM = get_mixer();
315
341
    volume_window()->show();
316
342
    toggle_volume();
317
343
  }
 
344
  
318
345
  get_preferences();
319
346
  clear_border();
320
347
  end();
461
488
void Indicator::checkIcon(unsigned int check) {
462
489
  Icon->show();
463
490
  Icon->position(0,0);
464
 
  if((check!=ICON)&&(check > 0))
 
491
  
 
492
  if(!battery)
 
493
    if(get_muted())
 
494
      check = get_mute_icon(LIGHT);
 
495
  
 
496
  if((check != ICON) && (check > 0))
465
497
    changeIcon(check,Icon);
 
498
  
466
499
  redraw();
467
 
  ICON=check;
 
500
  ICON = check;
468
501
}
469
502
 
470
503
std::string Indicator::convert_num_to_string(int num) {
570
603
}
571
604
 
572
605
const char* Indicator::get_lid_close_pref() {
 
606
  //TODO make enums
573
607
  switch(ActionOnClose)
574
608
  {
575
609
    case 1:
583
617
std::string Indicator::get_mixer() {
584
618
  if((!test_exec(MIXER_PROGRAM))||(MIXER_PROGRAM.compare("")==0))
585
619
      MIXER_PROGRAM=get_default_mixer();
586
 
  trace("get_mixer::Mixer="+MIXER_PROGRAM);
 
620
  //trace("get_mixer::Mixer="+MIXER_PROGRAM);
587
621
  return MIXER_PROGRAM;
588
622
}
589
623
 
669
703
    case FL_RELEASE:
670
704
      switch(Fl::event_button())
671
705
      {
672
 
        case FL_MOUSEWHEEL:
673
 
          if(battery)
674
 
            return ret;
675
 
          
676
 
          if(Fl::event_dy()>0)
677
 
            change_volume(-2);
678
 
          else
679
 
            change_volume(2);
680
 
          return 1;
681
706
        case FL_RIGHT_MOUSE:
682
707
          make_menu();
683
708
          return 1;
 
709
  
684
710
        case FL_MIDDLE_MOUSE:
685
711
          if(!battery)
686
712
            toggle_volume();
687
713
          else
688
714
            make_menu();
689
715
          return 1;
 
716
  
690
717
        case FL_LEFT_MOUSE:
691
718
          if(!battery)
692
719
            toggle_volume();
693
720
          else
694
721
            make_menu();
695
722
          return 1;
 
723
  
696
724
        default:
697
725
          if(!battery)
698
726
            toggle_volume();
701
729
          return 1;
702
730
      }
703
731
      return 1;
 
732
  
704
733
    case FL_MOUSEWHEEL:
705
734
      if(battery)
706
735
        return ret;
707
736
  
708
 
      if(Fl::event_dy()>0)
 
737
      if(Fl::event_dy()<0)
709
738
        change_volume(-2);
710
739
      else
711
740
        change_volume(2);
712
741
      return 1;
 
742
  
713
743
    default:
714
744
      return 0;//Fl_Widget::handle(event);
715
745
  }
848
878
}
849
879
 
850
880
void Indicator::mixer() {
851
 
  trace("mixer::testing="+MIXER_PROGRAM);
 
881
  //trace("mixer::testing="+MIXER_PROGRAM);
 
882
  
852
883
  if(!test_exec(MIXER_PROGRAM))
853
 
    MIXER_PROGRAM=get_default_mixer();
 
884
    MIXER_PROGRAM = get_default_mixer();
 
885
  
854
886
  if(!program_is_running(MIXER_PROGRAM))
855
887
  {
856
888
    run_a_program_in_background(MIXER_PROGRAM);
1065
1097
}
1066
1098
 
1067
1099
void Indicator::toggle_volume() {
 
1100
  //TODO bottom panel support, side panel support
 
1101
  volume_slider->position(x(),y()+h());
 
1102
  
1068
1103
  if(volume_slider->visible())
1069
1104
    volume_slider->hide();
1070
1105
  else
1071
1106
  {
1072
 
    int percent_int=get_volume();
1073
 
    slide->value(percent_int);
1074
1107
    volume_slider->show();
 
1108
    volume_slider->wait_for_expose();
 
1109
    update_volume();
1075
1110
  }
1076
 
  //todo bottom panel....
1077
 
  volume_slider->position(x(),y()+h());
1078
1111
}
1079
1112
 
1080
1113
void Indicator::update_colors(unsigned int BG) {
1130
1163
  slide->value(percent_int);
1131
1164
  
1132
1165
  //get the numerical value for the icon switch/case in checkIcon()
1133
 
  check=get_icon(percent_int,LIGHT,true,muted);
 
1166
  check = get_icon(percent_int,LIGHT,true,muted);
1134
1167
  
1135
1168
  //oh yeah is it big?
1136
1169
  if(big)
1141
1174
  checkIcon(check);
1142
1175
  //changeIcon(check,Icon);
1143
1176
  //get the text for the tooltip
1144
 
  std::string vol=volumeText();
 
1177
  std::string vol = volumeText();
1145
1178
  vol+=" %";
1146
1179
  //set the tooltip!
1147
1180
  setTooltip(vol.c_str());
1148
1181
  slide->redraw();
1149
 
  //redraw();
1150
1182
}
1151
1183
 
1152
1184
std::string Indicator::volumeText() {
1158
1190
  { volume_slider = new Fl_Double_Window(91, 210);
1159
1191
    volume_slider->color(FL_FOREGROUND_COLOR);
1160
1192
    volume_slider->user_data((void*)(this));
1161
 
    { Fl_Slider* o = slide = new Fl_Slider(20, 5, 25, 180, gettext("Volume"));
1162
 
      slide->tooltip(gettext("Volume"));
1163
 
      slide->box(FL_FLAT_BOX);
1164
 
      slide->color(FL_DARK3);
1165
 
      slide->labelcolor(FL_BACKGROUND2_COLOR);
1166
 
      slide->maximum(100);
1167
 
      slide->step(1);
1168
 
      slide->callback((Fl_Callback*)cb_slide);
1169
 
      slide->when(FL_WHEN_RELEASE);
1170
 
      std::string vol=volumeText()+ " %";
1171
 
      o->copy_tooltip(vol.c_str());
1172
 
      o->value(m_volumecontrol.GetVolume());
1173
 
    } // Fl_Slider* slide
1174
1193
    { zero = new Fl_Box(45, 0, 30, 25, gettext("0"));
1175
1194
      zero->labelcolor(FL_BACKGROUND2_COLOR);
1176
1195
    } // Fl_Box* zero
1177
1196
    { hundred = new Fl_Box(50, 160, 30, 25, gettext("100"));
1178
1197
      hundred->labelcolor(FL_BACKGROUND2_COLOR);
 
1198
      volume_initializer();
1179
1199
    } // Fl_Box* hundred
1180
1200
    volume_slider->clear_border();
 
1201
    volume_slider->xclass("popup");
1181
1202
    volume_slider->end();
1182
1203
  } // Fl_Double_Window* volume_slider
1183
1204
  return volume_slider;
1184
1205
}
1185
1206
 
 
1207
void Indicator::volume_initializer() {
 
1208
  slide = new Volume_Slider(volume_slider->x() + 20,
 
1209
                            volume_slider->y() + 5,
 
1210
                            25,
 
1211
                            180,
 
1212
                            gettext("Volume"));
 
1213
  
 
1214
  std::string vol = volumeText() + " %";
 
1215
  
 
1216
  slide->copy_tooltip(vol.c_str());
 
1217
  slide->value(m_volumecontrol.GetVolume());
 
1218
  volume_slider->add(slide);
 
1219
  slide->show();
 
1220
}
 
1221
 
1186
1222
void UserInterface::battery_CB(void* myclass) {
1187
1223
  UserInterface *o = (UserInterface*)myclass;
1188
1224
  int TIME=5;
1355
1391
  if(big)
1356
1392
    check++;
1357
1393
  
1358
 
  std::cout<<"Checking:"<<check<<"::"<<percent_int<<std::endl;
1359
 
  
1360
1394
  //get and set the actual icon now unless it hasn't changed, of course
1361
1395
  INDICATOR->checkIcon(check);
1362
1396
  
1382
1416
  if(INDICATOR->w()>16)
1383
1417
    big = true;
1384
1418
  //get our volume to help get an icon
1385
 
  int percent_int=INDICATOR->get_volume();
 
1419
  int percent_int = INDICATOR->get_volume();
1386
1420
  //set the volume on the slider to match the actual volume
1387
1421
  INDICATOR->slide->value(percent_int);
1388
1422
  
1389
1423
  //get the numerical value for the icon switch/case in checkIcon()
1390
 
  check=get_icon(percent_int,INDICATOR->LIGHT,true,muted);
 
1424
  check = get_icon(percent_int,INDICATOR->LIGHT,true,muted);
1391
1425
  
1392
1426
  //oh yeah is it big?
1393
1427
  if(big)
1397
1431
  //get the actual icon and set it!
1398
1432
  INDICATOR->checkIcon(check);
1399
1433
  //get the text for the tooltip
1400
 
  std::string vol=INDICATOR->volumeText();
1401
 
  vol+=" %";
 
1434
  std::string vol = INDICATOR->volumeText();
 
1435
  vol += " %";
1402
1436
  //set the tooltip!
1403
1437
  INDICATOR->setTooltip(vol.c_str());
1404
1438
}