1<?php
2
3function buildControlCommand($monitor) {
4  $ctrlCommand = '';
5  $control = $monitor->Control();
6
7  if ( isset($_REQUEST['xge']) || isset($_REQUEST['yge']) ) {
8    $slow = 0.9; // Threshold for slow speed/timeouts
9    $turbo = 0.9; // Threshold for turbo speed
10
11    if ( preg_match('/^([a-z]+)([A-Z][a-z]+)([A-Za-z]+)+$/', $_REQUEST['control'], $matches) ) {
12      $command = $matches[1];
13      $mode = $matches[2];
14      $dirn = $matches[3];
15
16      switch( $command ) {
17      case 'focus' :
18      {
19        $factor = $_REQUEST['yge']/100;
20        if ( $control->HasFocusSpeed() ) {
21          $speed = intval(round($control->MinFocusSpeed()+(($control->MaxFocusSpeed()-$control->MinFocusSpeed())*$factor)));
22          $ctrlCommand .= ' --speed='.$speed;
23        }
24        switch( $mode ) {
25        case 'Abs' :
26        case 'Rel' :
27        {
28          $step = intval(round($control->MinFocusStep()+(($control->MaxFocusStep()-$control->MinFocusStep())*$factor)));
29          $ctrlCommand .= ' --step='.$step;
30          break;
31        }
32        case 'Con' :
33        {
34          if ( $monitor->AutoStopTimeout() ) {
35            $slowSpeed = intval(round($control->MinFocusSpeed()+(($control->MaxFocusSpeed()-$control->MinFocusSpeed())*$slow)));
36            if ( $speed < $slowSpeed ) {
37              $ctrlCommand .= ' --autostop';
38            }
39          }
40          break;
41        }
42        }
43        break;
44      }
45      case 'zoom' :
46        $factor = $_REQUEST['yge']/100;
47        if ( $control->HasZoomSpeed() ) {
48          $speed = intval(round($control->MinZoomSpeed()+(($control->MaxZoomSpeed()-$control->MinZoomSpeed())*$factor)));
49          $ctrlCommand .= ' --speed='.$speed;
50        }
51        switch( $mode ) {
52        case 'Abs' :
53        case 'Rel' :
54          $step = intval(round($control->MinZoomStep()+(($control->MaxZoomStep()-$control->MinZoomStep())*$factor)));
55          $ctrlCommand .= ' --step='.$step;
56          break;
57        case 'Con' :
58          if ( $monitor->AutoStopTimeout() ) {
59            $slowSpeed = intval(round($control->MinZoomSpeed()+(($control->MaxZoomSpeed()-$control->MinZoomSpeed())*$slow)));
60            if ( $speed < $slowSpeed ) {
61              $ctrlCommand .= ' --autostop';
62            }
63          }
64          break;
65        }
66        break;
67      case 'iris' :
68        $factor = $_REQUEST['yge']/100;
69        if ( $control->HasIrisSpeed() ) {
70          $speed = intval(round($control->MinIrisSpeed()+(($control->MaxIrisSpeed()-$control->MinIrisSpeed())*$factor)));
71          $ctrlCommand .= ' --speed='.$speed;
72        }
73        switch( $mode ) {
74        case 'Abs' :
75        case 'Rel' :
76          $step = intval(round($control->MinIrisStep()+(($control->MaxIrisStep()-$control->MinIrisStep())*$factor)));
77          $ctrlCommand .= ' --step='.$step;
78          break;
79        }
80        break;
81      case 'white' :
82        $factor = $_REQUEST['yge']/100;
83        if ( $control->HasWhiteSpeed() ) {
84          $speed = intval(round($control->MinWhiteSpeed()+(($control->MaxWhiteSpeed()-$control->MinWhiteSpeed())*$factor)));
85          $ctrlCommand .= ' --speed='.$speed;
86        }
87        switch( $mode ) {
88        case 'Abs' :
89        case 'Rel' :
90          $step = intval(round($control->MinWhiteStep()+(($control->MaxWhiteStep()-$control->MinWhiteStep())*$factor)));
91          $ctrlCommand .= ' --step='.$step;
92          break;
93        }
94        break;
95      case 'gain' :
96        $factor = $_REQUEST['yge']/100;
97        if ( $control->HasGainSpeed() ) {
98          $speed = intval(round($control->MinGainSpeed()+(($control->MaxGainSpeed()-$control->MinGainSpeed())*$factor)));
99          $ctrlCommand .= ' --speed='.$speed;
100        }
101        switch( $mode ) {
102        case 'Abs' :
103        case 'Rel' :
104          $step = intval(round($control->MinGainStep()+(($control->MaxGainStep()-$control->MinGainStep())*$factor)));
105          $ctrlCommand .= ' --step='.$step;
106          break;
107        }
108        break;
109      case 'move' :
110        $xFactor = empty($_REQUEST['xge'])?0:$_REQUEST['xge']/100;
111        $yFactor = empty($_REQUEST['yge'])?0:$_REQUEST['yge']/100;
112
113        if ( $monitor->Orientation() != 'ROTATE_0' ) {
114          $conversions = array(
115            'ROTATE_90' => array(
116              'Up' => 'Left',
117              'Down' => 'Right',
118              'Left' => 'Down',
119              'Right' => 'Up',
120              'UpLeft' => 'DownLeft',
121              'UpRight' => 'UpLeft',
122              'DownLeft' => 'DownRight',
123              'DownRight' => 'UpRight',
124            ),
125            'ROTATE_180' => array(
126              'Up' => 'Down',
127              'Down' => 'Up',
128              'Left' => 'Right',
129              'Right' => 'Left',
130              'UpLeft' => 'DownRight',
131              'UpRight' => 'DownLeft',
132              'DownLeft' => 'UpRight',
133              'DownRight' => 'UpLeft',
134            ),
135            'ROTATE_270' => array(
136              'Up' => 'Right',
137              'Down' => 'Left',
138              'Left' => 'Up',
139              'Right' => 'Down',
140              'UpLeft' => 'UpRight',
141              'UpRight' => 'DownRight',
142              'DownLeft' => 'UpLeft',
143              'DownRight' => 'DownLeft',
144            ),
145            'FLIP_HORI' => array(
146              'Up' => 'Up',
147              'Down' => 'Down',
148              'Left' => 'Right',
149              'Right' => 'Left',
150              'UpLeft' => 'UpRight',
151              'UpRight' => 'UpLeft',
152              'DownLeft' => 'DownRight',
153              'DownRight' => 'DownLeft',
154            ),
155            'FLIP_VERT' => array(
156              'Up' => 'Down',
157              'Down' => 'Up',
158              'Left' => 'Left',
159              'Right' => 'Right',
160              'UpLeft' => 'DownLeft',
161              'UpRight' => 'DownRight',
162              'DownLeft' => 'UpLeft',
163              'DownRight' => 'UpRight',
164            ),
165          );
166          $new_dirn = $conversions[$monitor->Orientation()][$dirn];
167          $_REQUEST['control'] = preg_replace( "/_$dirn\$/", "_$new_dirn", $_REQUEST['control'] );
168          $dirn = $new_dirn;
169        }
170
171        if ( $control->HasPanSpeed() && $xFactor ) {
172          if ( $control->HasTurboPan() ) {
173            if ( $xFactor >= $turbo ) {
174              $panSpeed = $control->TurboPanSpeed();
175            } else {
176              $xFactor = $xFactor/$turbo;
177              $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
178            }
179          } else {
180            $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
181          }
182          $ctrlCommand .= ' --panspeed='.$panSpeed;
183        }
184        if ( $control->HasTiltSpeed() && $yFactor ) {
185          if ( $control->HasTurboTilt() ) {
186            if ( $yFactor >= $turbo ) {
187              $tiltSpeed = $control->TurboTiltSpeed();
188            } else {
189              $yFactor = $yFactor/$turbo;
190              $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
191            }
192          } else {
193            $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
194          }
195          $ctrlCommand .= ' --tiltspeed='.$tiltSpeed;
196        }
197        switch( $mode ) {
198        case 'Rel' :
199        case 'Abs' :
200          if ( preg_match( '/(Left|Right)$/', $dirn ) ) {
201            $panStep = intval(round($control->MinPanStep()+(($control->MaxPanStep()-$control->MinPanStep())*$xFactor)));
202            $ctrlCommand .= ' --panstep='.$panStep;
203          }
204          if ( preg_match( '/^(Up|Down)/', $dirn ) ) {
205            $tiltStep = intval(round($control->MinTiltStep()+(($control->MaxTiltStep()-$control->MinTiltStep())*$yFactor)));
206            $ctrlCommand .= ' --tiltstep='.$tiltStep;
207          }
208          break;
209        case 'Con' :
210          if ( $monitor->AutoStopTimeout() ) {
211            $slowPanSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$slow)));
212            $slowTiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$slow)));
213            if ( (!isset($panSpeed) || ($panSpeed < $slowPanSpeed)) && (!isset($tiltSpeed) || ($tiltSpeed < $slowTiltSpeed)) ) {
214              $ctrlCommand .= ' --autostop';
215            }
216          }
217          break;
218        }
219      }
220    } else {
221      Error('Invalid control parameter: ' . $_REQUEST['control'] );
222    }
223  } elseif ( isset($_REQUEST['x']) && isset($_REQUEST['y']) ) {
224    if ( $_REQUEST['control'] == 'moveMap' ) {
225      $x = deScale( $_REQUEST['x'], $_REQUEST['scale'] );
226      $y = deScale( $_REQUEST['y'], $_REQUEST['scale'] );
227      switch ( $monitor->Orientation() ) {
228      case 'ROTATE_0' :
229      case 'ROTATE_180' :
230      case 'FLIP_HORI' :
231      case 'FLIP_VERT' :
232        $width = $monitor->Width();
233        $height = $monitor->Height();
234        break;
235      case 'ROTATE_90' :
236      case 'ROTATE_270' :
237        $width = $monitor->Height();
238        $height = $monitor->Width();
239        break;
240      }
241      switch ( $monitor->Orientation() ) {
242      case 'ROTATE_90' :
243        $tempY = $y;
244        $y = $height - $x;
245        $x = $tempY;
246        break;
247      case 'ROTATE_180' :
248        $x = $width - $x;
249        $y = $height - $y;
250        break;
251      case 'ROTATE_270' :
252        $tempX = $x;
253        $x = $width - $y;
254        $y = $tempX;
255        break;
256      case 'FLIP_HORI' :
257        $x = $width - $x;
258        break;
259      case 'FLIP_VERT' :
260        $y = $height - $y;
261        break;
262      }
263      //$ctrlCommand .= " --xcoord=$x --ycoord=$y --width=$width --height=$height";
264      $ctrlCommand .= " --xcoord=$x --ycoord=$y";
265    } elseif ( $_REQUEST['control'] == 'movePseudoMap' ) {
266      $x = deScale( $_REQUEST['x'], $_REQUEST['scale'] );
267      $y = deScale( $_REQUEST['y'], $_REQUEST['scale'] );
268
269      $halfWidth = $monitor->Width() / 2;
270      $halfHeight = $monitor->Height() / 2;
271      $xFactor = ($x - $halfWidth)/$halfWidth;
272      $yFactor = ($y - $halfHeight)/$halfHeight;
273
274      switch ( $monitor->Orientation() ) {
275      case 'ROTATE_90' :
276        $tempYFactor = $y;
277        $yFactor = -$xFactor;
278        $xFactor = $tempYFactor;
279        break;
280      case 'ROTATE_180' :
281        $xFactor = -$xFactor;
282        $yFactor = -$yFactor;
283        break;
284      case 'ROTATE_270' :
285        $tempXFactor = $x;
286        $xFactor = -$yFactor;
287        $yFactor = $tempXFactor;
288        break;
289      case 'FLIP_HORI' :
290        $xFactor = -$xFactor;
291        break;
292      case 'FLIP_VERT' :
293        $yFactor = -$yFactor;
294        break;
295      }
296
297      $turbo = 0.9; // Threshold for turbo speed
298      $blind = 0.1; // Threshold for blind spot
299
300      $panControl = '';
301      $tiltControl = '';
302      if ( $xFactor > $blind ) {
303        $panControl = 'Right';
304      } elseif ( $xFactor < -$blind ) {
305        $panControl = 'Left';
306      }
307      if ( $yFactor > $blind ) {
308        $tiltControl = 'Down';
309      } elseif ( $yFactor < -$blind ) {
310        $tiltControl = 'Up';
311      }
312
313      $dirn = $tiltControl.$panControl;
314      if ( !$dirn ) {
315        // No command, probably in blind spot in middle
316        $_REQUEST['control'] = 'null';
317        return( false );
318      } else {
319        $_REQUEST['control'] = 'moveRel'.$dirn;
320        $xFactor = abs($xFactor);
321        $yFactor = abs($yFactor);
322
323        if ( $control->HasPanSpeed() && $xFactor ) {
324          if ( $control->HasTurboPan() ) {
325            if ( $xFactor >= $turbo ) {
326              $panSpeed = $control->TurboPanSpeed();
327            } else {
328              $xFactor = $xFactor/$turbo;
329              $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
330            }
331          } else {
332            $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
333          }
334        }
335        if ( $control->HasTiltSpeed() && $yFactor ) {
336          if ( $control->HasTurboTilt() ) {
337            if ( $yFactor >= $turbo ) {
338              $tiltSpeed = $control->TurboTiltSpeed();
339            } else {
340              $yFactor = $yFactor/$turbo;
341              $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
342            }
343          } else {
344            $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
345          }
346        }
347        if ( preg_match( '/(Left|Right)$/', $dirn ) ) {
348          $panStep = intval(round($control->MinPanStep()+(($control->MaxPanStep()-$control->MinPanStep())*$xFactor)));
349          $ctrlCommand .= ' --panstep='.$panStep.' --panspeed='.$panSpeed;
350        }
351        if ( preg_match( '/^(Up|Down)/', $dirn ) ) {
352          $tiltStep = intval(round($control->MinTiltStep()+(($control->MaxTiltStep()-$control->MinTiltStep())*$yFactor)));
353          $ctrlCommand .= ' --tiltstep='.$tiltStep.' --tiltspeed='.$tiltSpeed;
354        }
355      }
356    } elseif ( $_REQUEST['control'] == 'moveConMap' ) {
357      $x = deScale( $_REQUEST['x'], $_REQUEST['scale'] );
358      $y = deScale( $_REQUEST['y'], $_REQUEST['scale'] );
359
360      $halfWidth = $monitor->Width() / 2;
361      $halfHeight = $monitor->Height() / 2;
362      $xFactor = ($x - $halfWidth)/$halfWidth;
363      $yFactor = ($y - $halfHeight)/$halfHeight;
364
365      switch ( $monitor->Orientation() ) {
366      case 'ROTATE_90' :
367        $tempYFactor = $y;
368        $yFactor = -$xFactor;
369        $xFactor = $tempYFactor;
370        break;
371      case 'ROTATE_180' :
372        $xFactor = -$xFactor;
373        $yFactor = -$yFactor;
374        break;
375      case 'ROTATE_270' :
376        $tempXFactor = $x;
377        $xFactor = -$yFactor;
378        $yFactor = $tempXFactor;
379        break;
380      case 'FLIP_HORI' :
381        $xFactor = -$xFactor;
382        break;
383      case 'FLIP_VERT' :
384        $yFactor = -$yFactor;
385        break;
386      }
387
388      $slow = 0.9; // Threshold for slow speed/timeouts
389      $turbo = 0.9; // Threshold for turbo speed
390      $blind = 0.1; // Threshold for blind spot
391
392      $panControl = '';
393      $tiltControl = '';
394      if ( $xFactor > $blind ) {
395        $panControl = 'Right';
396      } elseif ( $xFactor < -$blind ) {
397        $panControl = 'Left';
398      }
399      if ( $yFactor > $blind ) {
400        $tiltControl = 'Down';
401      } elseif ( $yFactor < -$blind ) {
402        $tiltControl = 'Up';
403      }
404
405      $dirn = $tiltControl.$panControl;
406      if ( !$dirn ) {
407        // No command, probably in blind spot in middle
408        $_REQUEST['control'] = 'moveStop';
409      } else {
410        $_REQUEST['control'] = 'moveCon'.$dirn;
411        $xFactor = abs($xFactor);
412        $yFactor = abs($yFactor);
413
414        if ( $control->HasPanSpeed() && $xFactor ) {
415          if ( $control->HasTurboPan() ) {
416            if ( $xFactor >= $turbo ) {
417              $panSpeed = $control->TurboPanSpeed();
418            } else {
419              $xFactor = $xFactor/$turbo;
420              $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
421            }
422          } else {
423            $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
424          }
425        }
426        if ( $control->HasTiltSpeed() && $yFactor ) {
427          if ( $control->HasTurboTilt() ) {
428            if ( $yFactor >= $turbo ) {
429              $tiltSpeed = $control->TurboTiltSpeed();
430            } else {
431              $yFactor = $yFactor/$turbo;
432              $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
433            }
434          } else {
435            $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
436          }
437        }
438        if ( preg_match( '/(Left|Right)$/', $dirn ) ) {
439          $ctrlCommand .= ' --panspeed='.$panSpeed;
440        }
441        if ( preg_match( '/^(Up|Down)/', $dirn ) ) {
442          $ctrlCommand .= ' --tiltspeed='.$tiltSpeed;
443        }
444        if ( $monitor->AutoStopTimeout() ) {
445          $slowPanSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$slow)));
446          $slowTiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$slow)));
447          if ( (!isset($panSpeed) || ($panSpeed < $slowPanSpeed)) && (!isset($tiltSpeed) || ($tiltSpeed < $slowTiltSpeed)) ) {
448            $ctrlCommand .= ' --autostop';
449          }
450        }
451      }
452    } else {
453      $slow = 0.9; // Threshold for slow speed/timeouts
454      $turbo = 0.9; // Threshold for turbo speed
455      $long_y = 48;
456      $short_x = 32;
457      $short_y = 32;
458
459      if ( preg_match( '/^([a-z]+)([A-Z][a-z]+)([A-Z][a-z]+)$/', $_REQUEST['control'], $matches ) ) {
460        $command = $matches[1];
461        $mode = $matches[2];
462        $dirn = $matches[3];
463
464        switch( $command ) {
465        case 'focus' :
466          switch( $dirn ) {
467          case 'Near' :
468            $factor = ($long_y-($y+1))/$long_y;
469            break;
470          case 'Far' :
471            $factor = ($y+1)/$long_y;
472            break;
473          }
474          if ( $control->HasFocusSpeed() ) {
475            $speed = intval(round($control->MinFocusSpeed()+(($control->MaxFocusSpeed()-$control->MinFocusSpeed())*$factor)));
476            $ctrlCommand .= ' --speed='.$speed;
477          }
478          switch( $mode ) {
479          case 'Abs' :
480          case 'Rel' :
481            $step = intval(round($control->MinFocusStep()+(($control->MaxFocusStep()-$control->MinFocusStep())*$factor)));
482            $ctrlCommand .= ' --step='.$step;
483            break;
484          case 'Con' :
485            if ( $monitor->AutoStopTimeout() ) {
486              $slowSpeed = intval(round($control->MinFocusSpeed()+(($control->MaxFocusSpeed()-$control->MinFocusSpeed())*$slow)));
487              if ( $speed < $slowSpeed ) {
488                $ctrlCommand .= ' --autostop';
489              }
490            }
491            break;
492          }
493          break;
494        case 'zoom' :
495          switch( $dirn ) {
496          case 'Tele' :
497            $factor = ($long_y-($y+1))/$long_y;
498            break;
499          case 'Wide' :
500            $factor = ($y+1)/$long_y;
501            break;
502          }
503          if ( $control->HasZoomSpeed() ) {
504            $speed = intval(round($control->MinZoomSpeed()+(($control->MaxZoomSpeed()-$control->MinZoomSpeed())*$factor)));
505            $ctrlCommand .= ' --speed='.$speed;
506          }
507          switch( $mode ) {
508          case 'Abs' :
509          case 'Rel' :
510            $step = intval(round($control->MinZoomStep()+(($control->MaxZoomStep()-$control->MinZoomStep())*$factor)));
511            $ctrlCommand .= ' --step='.$step;
512            break;
513          case 'Con' :
514            if ( $monitor->AutoStopTimeout() ) {
515              $slowSpeed = intval(round($control->MinZoomSpeed()+(($control->MaxZoomSpeed()-$control->MinZoomSpeed())*$slow)));
516              if ( $speed < $slowSpeed ) {
517                $ctrlCommand .= ' --autostop';
518              }
519            }
520            break;
521          }
522          break;
523        case 'iris' :
524          switch( $dirn ) {
525          case 'Open' :
526            $factor = ($long_y-($y+1))/$long_y;
527            break;
528          case 'Close' :
529            $factor = ($y+1)/$long_y;
530            break;
531          }
532          if ( $control->HasIrisSpeed() ) {
533            $speed = intval(round($control->MinIrisSpeed()+(($control->MaxIrisSpeed()-$control->MinIrisSpeed())*$factor)));
534            $ctrlCommand .= ' --speed='.$speed;
535          }
536          switch( $mode ) {
537          case 'Abs' :
538          case 'Rel' :
539            $step = intval(round($control->MinIrisStep()+(($control->MaxIrisStep()-$control->MinIrisStep())*$factor)));
540            $ctrlCommand .= ' --step='.$step;
541            break;
542          }
543          break;
544        case 'white' :
545          switch( $dirn ) {
546          case 'In' :
547            $factor = ($long_y-($y+1))/$long_y;
548            break;
549          case 'Out' :
550            $factor = ($y+1)/$long_y;
551            break;
552          }
553          if ( $control->HasWhiteSpeed() ) {
554            $speed = intval(round($control->MinWhiteSpeed()+(($control->MaxWhiteSpeed()-$control->MinWhiteSpeed())*$factor)));
555            $ctrlCommand .= ' --speed='.$speed;
556          }
557          switch( $mode ) {
558          case 'Abs' :
559          case 'Rel' :
560            $step = intval(round($control->MinWhiteStep()+(($control->MaxWhiteStep()-$control->MinWhiteStep())*$factor)));
561            $ctrlCommand .= ' --step='.$step;
562            break;
563          }
564          break;
565        case 'gain' :
566          switch( $dirn ) {
567          case 'Up' :
568            $factor = ($long_y-($y+1))/$long_y;
569            break;
570          case 'Down' :
571            $factor = ($y+1)/$long_y;
572            break;
573          }
574          if ( $control->HasGainSpeed() ) {
575            $speed = intval(round($control->MinGainSpeed()+(($control->MaxGainSpeed()-$control->MinGainSpeed())*$factor)));
576            $ctrlCommand .= ' --speed='.$speed;
577          }
578          switch( $mode ) {
579          case 'Abs' :
580          case 'Rel' :
581            $step = intval(round($control->MinGainStep()+(($control->MaxGainStep()-$control->MinGainStep())*$factor)));
582            $ctrlCommand .= ' --step='.$step;
583            break;
584          }
585          break;
586        case 'move' :
587          $xFactor = 0;
588          $yFactor = 0;
589
590          if ( preg_match( '/^Up/', $dirn ) ) {
591            $yFactor = ($short_y-($y+1))/$short_y;
592          } elseif ( preg_match( '/^Down/', $dirn ) ) {
593            $yFactor = ($y+1)/$short_y;
594          }
595          if ( preg_match( '/Left$/', $dirn ) ) {
596            $xFactor = ($short_x-($x+1))/$short_x;
597          } elseif ( preg_match( '/Right$/', $dirn ) ) {
598            $xFactor = ($x+1)/$short_x;
599          }
600
601          if ( $monitor->Orientation() != 'ROTATE_0' ) {
602            $conversions = array(
603              'ROTATE_90' => array(
604                'Up' => 'Left',
605                'Down' => 'Right',
606                'Left' => 'Down',
607                'Right' => 'Up',
608                'UpLeft' => 'DownLeft',
609                'UpRight' => 'UpLeft',
610                'DownLeft' => 'DownRight',
611                'DownRight' => 'UpRight',
612              ),
613              'ROTATE_180' => array(
614                'Up' => 'Down',
615                'Down' => 'Up',
616                'Left' => 'Right',
617                'Right' => 'Left',
618                'UpLeft' => 'DownRight',
619                'UpRight' => 'DownLeft',
620                'DownLeft' => 'UpRight',
621                'DownRight' => 'UpLeft',
622              ),
623              'ROTATE_270' => array(
624                'Up' => 'Right',
625                'Down' => 'Left',
626                'Left' => 'Up',
627                'Right' => 'Down',
628                'UpLeft' => 'UpRight',
629                'UpRight' => 'DownRight',
630                'DownLeft' => 'UpLeft',
631                'DownRight' => 'DownLeft',
632              ),
633              'FLIP_HORI' => array(
634                'Up' => 'Up',
635                'Down' => 'Down',
636                'Left' => 'Right',
637                'Right' => 'Left',
638                'UpLeft' => 'UpRight',
639                'UpRight' => 'UpLeft',
640                'DownLeft' => 'DownRight',
641                'DownRight' => 'DownLeft',
642              ),
643              'FLIP_VERT' => array(
644                'Up' => 'Down',
645                'Down' => 'Up',
646                'Left' => 'Left',
647                'Right' => 'Right',
648                'UpLeft' => 'DownLeft',
649                'UpRight' => 'DownRight',
650                'DownLeft' => 'UpLeft',
651                'DownRight' => 'UpRight',
652              ),
653            );
654            $new_dirn = $conversions[$monitor->Orientation()][$dirn];
655            $_REQUEST['control'] = preg_replace( "/_$dirn\$/", "_$new_dirn", $_REQUEST['control'] );
656            $dirn = $new_dirn;
657          }
658
659          if ( $control->HasPanSpeed() && $xFactor ) {
660            if ( $control->HasTurboPan() ) {
661              if ( $xFactor >= $turbo ) {
662                $panSpeed = $control->TurboPanSpeed();
663              } else {
664                $xFactor = $xFactor/$turbo;
665                $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
666              }
667            } else {
668              $panSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$xFactor)));
669            }
670            $ctrlCommand .= ' --panspeed='.$panSpeed;
671          }
672          if ( $control->HasTiltSpeed() && $yFactor ) {
673            if ( $control->HasTurboTilt() ) {
674              if ( $yFactor >= $turbo ) {
675                $tiltSpeed = $control->TurboTiltSpeed();
676              } else {
677                $yFactor = $yFactor/$turbo;
678                $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
679              }
680            } else {
681              $tiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$yFactor)));
682            }
683            $ctrlCommand .= ' --tiltspeed='.$tiltSpeed;
684          }
685          switch( $mode ) {
686          case 'Rel' :
687          case 'Abs' :
688            if ( preg_match( '/(Left|Right)$/', $dirn ) ) {
689              $panStep = intval(round($control->MinPanStep()+(($control->MaxPanStep()-$control->MinPanStep())*$xFactor)));
690              $ctrlCommand .= ' --panstep='.$panStep;
691            }
692            if ( preg_match( '/^(Up|Down)/', $dirn ) ) {
693              $tiltStep = intval(round($control->MinTiltStep()+(($control->MaxTiltStep()-$control->MinTiltStep())*$yFactor)));
694              $ctrlCommand .= ' --tiltstep='.$tiltStep;
695            }
696            break;
697          case 'Con' :
698            if ( $monitor->AutoStopTimeout() ) {
699              $slowPanSpeed = intval(round($control->MinPanSpeed()+(($control->MaxPanSpeed()-$control->MinPanSpeed())*$slow)));
700              $slowTiltSpeed = intval(round($control->MinTiltSpeed()+(($control->MaxTiltSpeed()-$control->MinTiltSpeed())*$slow)));
701              if ( (!isset($panSpeed) || ($panSpeed < $slowPanSpeed)) && (!isset($tiltSpeed) || ($tiltSpeed < $slowTiltSpeed)) ) {
702                $ctrlCommand .= ' --autostop';
703              }
704            }
705            break;
706          }
707        }
708      }
709    }
710  } else {
711    if ( preg_match( '/^presetGoto(\d+)$/', $_REQUEST['control'], $matches ) ) {
712      $_REQUEST['control'] = 'presetGoto';
713      $ctrlCommand .= ' --preset='.$matches[1];
714    } elseif ( $_REQUEST['control'] == 'presetGoto' && !empty($_REQUEST['preset']) ) {
715      $ctrlCommand .= ' --preset='.$_REQUEST['preset'];
716    } elseif ( $_REQUEST['control'] == 'presetSet' ) {
717      if ( canEdit( 'Control' ) ) {
718        $preset = validInt($_REQUEST['preset']);
719        $newLabel = validJsStr($_REQUEST['newLabel']);
720        $row = dbFetchOne(
721          'SELECT * FROM `ControlPresets` WHERE `MonitorId` = ? AND `Preset`=?',
722          NULL, array($monitor->Id(), $preset));
723        if ( $newLabel != $row['Label'] ) {
724          if ( $newLabel ) {
725            dbQuery('REPLACE INTO `ControlPresets` (`MonitorId`, `Preset`, `Label`) VALUES ( ?, ?, ? )',
726              array($monitor->Id(), $preset, $newLabel));
727          } else {
728            dbQuery('DELETE FROM `ControlPresets` WHERE `MonitorId`=? AND `Preset`=?',
729              array($monitor->Id(), $preset));
730          }
731        }
732        $ctrlCommand .= ' --preset='.$preset;
733      }
734      $ctrlCommand .= ' --preset='.$preset;
735    } elseif ( $_REQUEST['control'] == 'moveMap' ) {
736      $ctrlCommand .= " --xcoord=$x --ycoord=$y";
737    }
738  }
739  $ctrlCommand .= ' --command='.$_REQUEST['control'];
740  return $ctrlCommand;
741}
742
743