system-controller: added initial support for application tracking.
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "alert",
102     priority = 51,
103     modal    = false,
104     share    = false,
105     order    = "fifo"
106 }
107
108 application_class {
109     name     = "navigator",
110     priority = 50,
111     modal    = false,
112     share    = true,
113     order    = "fifo"
114 }
115
116 application_class {
117     name     = "phone",
118     priority = 6 ,
119     modal    = false,
120     share    = true ,
121     order    = "lifo"
122 }
123 application_class {
124     name     = "camera",
125     priority = 5,
126     modal    = false,
127     share    = false,
128     order    = "lifo"
129 }
130
131 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
132 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
133 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
134 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
135 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
136 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
137
138 -- define zone attributes
139 zone.attributes {
140     type = {mdb.string, "common", "rw"},
141     location = {mdb.string, "anywhere", "rw"}
142 }
143
144 -- define zones
145 zone {
146      name = "driver",
147      attributes = {
148          type = "common",
149          location = "front-left"
150      }
151 }
152
153 zone {
154      name = "passanger1",
155      attributes = {
156          type = "private",
157          location = "front-right"
158      }
159 }
160
161 zone {
162      name = "passanger2",
163      attributes = {
164          type = "private",
165          location = "back-left"
166      }
167 }
168
169 zone {
170      name = "passanger3",
171      attributes = {
172          type = "private",
173          location = "back-right"
174      }
175 }
176
177 zone {
178      name = "passanger4",
179      attributes = {
180          type = "private",
181          location = "back-left"
182      }
183 }
184
185
186 -- define resource classes
187 if not m:plugin_exists('ivi-resource-manager') and
188    not with_system_controller
189 then
190    resource.class {
191         name = "audio_playback",
192         shareable = true,
193         attributes = {
194             role = { mdb.string, "music", "rw" },
195             pid = { mdb.string, "<unknown>", "rw" },
196             policy = { mdb.string, "relaxed", "rw" }
197         }
198    }
199 end
200
201 resource.class {
202      name = "audio_recording",
203      shareable = true,
204      attributes = {
205          role   = { mdb.string, "music"    , "rw" },
206          pid    = { mdb.string, "<unknown>", "rw" },
207          policy = { mdb.string, "relaxed"  , "rw" }
208      }
209 }
210
211 resource.class {
212      name = "video_playback",
213      shareable = false,
214 }
215
216 resource.class {
217      name = "video_recording",
218      shareable = false
219 }
220
221 if not m:plugin_exists('ivi-resource-manager') and
222    not with_system_controller
223 then
224     resource.method.veto = {
225         function(zone, rset, grant, owners, req_set)
226             return true
227          end
228     }
229 end
230
231 -- test for creating selections
232 mdb.select {
233            name = "audio_owner",
234            table = "audio_playback_owner",
235            columns = {"application_class"},
236            condition = "zone_name = 'driver'"
237 }
238
239 mdb.select {
240            name = "vehicle_speed",
241            table = "amb_vehicle_speed",
242            columns = {"value"},
243            condition = "key = 'VehicleSpeed'"
244 }
245
246 element.lua {
247    name    = "speed2volume",
248    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
249    outputs = {  mdb.table { name = "speedvol",
250                             index = {"zone", "device"},
251                             columns = {{"zone", mdb.string, 16},
252                                        {"device", mdb.string, 16},
253                                        {"value", mdb.floating}},
254                             create = true
255                            }
256              },
257    oldvolume = 0.0,
258    update  = function(self)
259                 speed = self.inputs.speed.single_value
260                 if (speed) then
261                     volume = (speed - 144.0) / 7.0
262                 else
263                     volume = 0.0
264                 end
265                 diff = volume - self.oldvolume
266                 if (diff*diff > self.inputs.param) then
267                     print("*** element "..self.name.." update "..volume)
268                     self.oldvolume = volume
269                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
270                 end
271              end
272 }
273
274 -- Night mode processing chain
275
276 mdb.select {
277     name = "exterior_brightness",
278     table = "amb_exterior_brightness",
279     columns = { "value" },
280     condition = "key = 'ExteriorBrightness'"
281 }
282
283 element.lua {
284     name    = "nightmode",
285     inputs  = { brightness = mdb.select.exterior_brightness },
286     oldmode = -1;
287     outputs = {
288         mdb.table {
289             name = "amb_nightmode",
290             index = { "id" },
291             create = true,
292             columns = {
293                 { "id", mdb.unsigned },
294                 { "night_mode", mdb.unsigned }
295             }
296         }
297     },
298     update = function(self)
299         -- This is a trivial function to calculate night mode. Later, we will
300         -- need a better threshold value and hysteresis to prevent oscillation.
301
302         brightness = self.inputs.brightness.single_value
303
304         if not brightness then
305             return
306         end
307
308         print("*** element "..self.name.." update brightness: "..brightness)
309
310         if brightness > 300 then
311             mode = 0
312         else
313             mode = 1
314         end
315
316         print("*** resulting mode: ".. mode)
317
318         if not (mode == self.oldmode) then
319             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
320         end
321
322         self.oldmode = mode
323     end
324 }
325
326 mdb.select {
327     name = "select_night_mode",
328     table = "amb_nightmode",
329     columns = { "night_mode" },
330     condition = "id = 0"
331 }
332
333 if with_amb then
334     sink.lua {
335         name = "night_mode",
336         inputs = { owner = mdb.select.select_night_mode },
337         property = "NightMode",
338         type = "b",
339         initiate = builtin.method.amb_initiate,
340         update = builtin.method.amb_update
341     }
342 end
343
344 -- Night mode general handlers
345
346 sink.lua {
347     name = "nightmode_homescreen",
348     inputs = { owner = mdb.select.select_night_mode },
349     initiate = function(self)
350         data = mdb.select.select_night_mode.single_value
351         print("Night mode initiated: " .. tostring(data))
352         return true
353     end,
354     update = function(self)
355         data = mdb.select.select_night_mode.single_value
356         print("Night mode updated: " .. tostring(data))
357
358         if sc then
359             -- tell homescreen that night mode was updated
360             sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=2,state=data})}))
361         end
362         return true
363     end
364 }
365
366 -- Driving mode processing chain
367
368 element.lua {
369     name    = "drivingmode",
370     inputs  = { speed = mdb.select.vehicle_speed },
371     oldmode = -1;
372     outputs = {
373         mdb.table {
374             name = "amb_drivingmode",
375             index = { "id" },
376             create = true,
377             columns = {
378                 { "id", mdb.unsigned },
379                 { "driving_mode", mdb.unsigned }
380             }
381         }
382     },
383     update = function(self)
384
385         speed = self.inputs.speed.single_value
386
387         if not speed then
388             return
389         end
390
391         if speed == 0 then
392             mode = 0
393         else
394             mode = 1
395         end
396
397         if not (mode == self.oldmode) then
398             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
399         end
400
401         self.oldmode = mode
402     end
403 }
404
405 mdb.select {
406     name = "select_driving_mode",
407     table = "amb_drivingmode",
408     columns = { "driving_mode" },
409     condition = "id = 0"
410 }
411
412 if with_amb then
413     sink.lua {
414         name = "driving_mode",
415         inputs = { owner = mdb.select.select_driving_mode },
416         property = "DrivingMode",
417         type = "u",
418         initiate = builtin.method.amb_initiate,
419         update = builtin.method.amb_update
420     }
421 end
422
423 -- turn signals (left, right)
424
425 mdb.select {
426     name = "winker",
427     table = "amb_turn_signal",
428     columns = { "value" },
429     condition = "key = 'TurnSignal'"
430 }
431
432 -- regulation (on), use "select_driving_mode"
433
434 sink.lua {
435     name = "driving_regulation",
436     inputs = { owner = mdb.select.select_driving_mode },
437     initiate = function(self)
438         data = mdb.select.select_driving_mode.single_value
439         print("Driving mode initiated: " .. tostring(data))
440         return true
441     end,
442     update = function(self)
443         data = mdb.select.select_driving_mode.single_value
444         print("Driving mode updated: " .. tostring(data))
445
446         if sc then
447             -- tell homescreen that driving mode was updated
448             sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=1,state=data})}))
449         end
450         return true
451     end
452 }
453
454 -- shift position (parking, reverse, other)
455
456 mdb.select {
457     name = "gear_position",
458     table = "amb_gear_position",
459     columns = { "value" },
460     condition = "key = 'GearPosition'"
461 }
462
463 -- cameras (back, front, left, right)
464
465 element.lua {
466     name    = "camera_state",
467     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
468     oldmode = -1;
469     outputs = {
470         mdb.table {
471             name = "target_camera_state",
472             index = { "id" },
473             create = true,
474             columns = {
475                 { "id", mdb.unsigned },
476                 { "front_camera", mdb.unsigned },
477                 { "back_camera", mdb.unsigned },
478                 { "right_camera", mdb.unsigned },
479                 { "left_camera", mdb.unsigned }
480             }
481         }
482     },
483     update = function(self)
484
485         front_camera = 0
486         back_camera = 0
487         right_camera = 0
488         left_camera = 0
489
490         if self.inputs.gear == 128 then
491             back_camera = 1
492         elseif self.inputs.winker == 1 then
493             right_camera = 1
494         elseif self.inputs.winker == 2 then
495             left_camera = 1
496         end
497
498         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
499
500     end
501 }
502
503 -- load the telephony plugin
504 m:try_load_plugin('telephony')
505
506
507 -- system controller test setup
508
509 if not with_system_controller then
510    return
511 end
512
513 m:load_plugin('system-controller')
514
515 window_manager_operation_names = {
516     [1] = "create",
517     [2] = "destroy"
518 }
519
520 function window_manager_operation_name(oper)
521     local name = window_manager_operation_names[oper]
522     if name then return name end
523     return "<unknown " .. tostring(oper) .. ">"
524 end
525
526 window_operation_names = {
527     [1] = "create",
528     [2] = "destroy",
529     [3] = "name_change",
530     [4] = "visible",
531     [5] = "configure",
532     [6] = "active"
533 }
534
535 function window_operation_name(oper)
536     local name = window_operation_names[oper]
537     if name then return name end
538     return "<unknown " .. tostring(oper) .. ">"
539 end
540
541 layer_operation_names = {
542     [1] = "create",
543     [2] = "destroy",
544     [3] = "visible"
545 }
546
547 function layer_operation_name(oper)
548     local name = layer_operation_names[oper]
549     if name then return name end
550     return "<unknown " .. tostring(oper) .. ">"
551 end
552
553 input_manager_operation_names = {
554     [1] = "create",
555     [2] = "destroy",
556     [3] = "ready"
557 }
558
559 function input_manager_operation_name(oper)
560     local name = input_manager_operation_names[oper]
561     if name then return name end
562     return "<unknown " .. tostring(oper) .. ">"
563 end
564
565 input_operation_names = {
566     [1] = "create",
567     [2] = "destroy",
568     [3] = "update"
569 }
570
571 function input_operation_name(oper)
572     local name = input_operation_names[oper]
573     if name then return name end
574     return "<unknown " .. tostring(oper) .. ">"
575 end
576
577 code_operation_names = {
578     [1] = "create",
579     [2] = "destroy",
580     [3] = "state_change"
581 }
582
583 function code_operation_name(oper)
584     local name = code_operation_names[oper]
585     if name then return name end
586     return "<unknown " .. tostring(oper) .. ">"
587 end
588
589 command_names = {
590     [0x00001] = "send_appid",
591     [0x10001] = "create",
592     [0x10002] = "destroy",
593     [0x10003] = "show",
594     [0x10004] = "hide",
595     [0x10005] = "move",
596     [0x10006] = "change_active",
597     [0x10007] = "change_layer",
598     [0x10008] = "change_attr",
599     [0x10009] = "name",
600     [0x10011] = "map_thumb",
601     [0x10012] = "unmap_thumb",
602     [0x10020] = "show layer",
603     [0x10021] = "hide_layer",
604     [0x10022] = "change_layer_attr",
605     [0x20001] = "add_input",
606     [0x20002] = "del_input",
607     [0x20003] = "send_input",
608     [0x40001] = "acquire_res",
609     [0x40002] = "release_res",
610     [0x40003] = "deprive_res",
611     [0x40004] = "waiting_res",
612     [0x40005] = "revert_res",
613     [0x40011] = "create_res",
614     [0x40012] = "destroy_res",
615     [0x50001] = "set_region",
616     [0x50002] = "unset_region",
617     [0x60001] = "change_state"
618 }
619
620 function command_name(command)
621     local name = command_names[command]
622     if name then return name end
623     return "<unknown " .. tostring(command) .. ">"
624 end
625
626 input_layer = {
627    [3] = true, -- input
628    [4] = true, -- touch
629    [5] = true  -- cursor
630 }
631
632 resmgr = resource_manager {
633   screen_event_handler = function(self, ev)
634                              local event = ev.event
635                              local surface = ev.surface
636
637                              if event == "grant" then
638                                  if verbose > 0 then
639                                     print("*** make visible surface "..surface)
640                                  end
641                                  local a = animation({})
642                                  local r = m:JSON({surface = surface,
643                                                    visible = 1,
644                                                    raise   = 1})
645                                  wmgr:window_request(r,a,0)
646                                  elseif event == "revoke" then
647                                  if verbose > 0 then
648                                     print("*** hide surface "..surface)
649                                  end
650                                  local a = animation({})
651                                  local r = m:JSON({surface = ev.surface,
652                                                    visible = 0})
653                                  wmgr:window_request(r,a,0)
654                              else
655                                  if verbose > 0 then
656                                     print("*** screen resource event: " ..
657                                           tostring(ev))
658                                  end
659                              end
660                          end,
661   audio_event_handler = function(self, ev)
662                              local event = ev.event
663                              local appid = ev.appid
664                              local audioid = ev.audioid
665
666                              if event == "grant" then
667                                  if verbose > 0 or true then
668                                     print("*** grant audio to "..appid..
669                                           " ("..audioid..") in '" ..
670                                           ev.zone .. "' zone")
671                                  end
672                              elseif event == "revoke" then
673                                  if verbose > 0 or true then
674                                     print("*** revoke audio from "..appid..
675                                           " ("..audioid..") in '" ..
676                                           ev.zone .. "' zone")
677                                  end
678                              else
679                                  if verbose > 0 or true then
680                                     print("*** audio resource event: " ..
681                                           tostring(ev))
682                                  end
683                              end
684                         end
685 }
686
687 resclnt = resource_client {}
688
689 wmgr = window_manager {
690   geometry = function(self, w,h, v)
691                   if type(v) == "function" then
692                       return v(w,h)
693                   end
694                   return v
695              end,
696
697   application = function(self, appid)
698                      if appid then
699                          local app = application_lookup(appid)
700                          if not app then
701                              app = application_lookup("default")
702                          end
703                          return app
704                      end
705                      return { privileges = {screen="none", audio="none"} }
706                 end,
707
708   outputs = { { name  = "Center",
709                 id    = 0,
710                 zone  = "driver",
711                 areas = { Status = {
712                               id     = 0,
713                               pos_x  = 0,
714                               pos_y  = 0,
715                               width  = function(w,h) return w end,
716                               height = 64
717                           },
718                           Full = {
719                               id     = 1,
720                               pos_x  = 0,
721                               pos_y  = 64,
722                               width  = function(w,h) return w end,
723                               height = function(w,h) return h-64-128 end
724                           },
725                           Upper = {
726                               id     = 2,
727                               pos_x  = 0,
728                               pos_y  = 64,
729                               width  = function(w,h) return w end,
730                               height = function(w,h) return (h-64-128)/2 end
731                           },
732                           Lower = {
733                               id     = 3,
734                               pos_x  = 0,
735                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
736                               width  = function(w,h) return w end,
737                               height = function(w,h) return (h-64-128)/2 end
738                           },
739                           UpperLeft = {
740                               id     = 4,
741                               pos_x  = 0,
742                               pos_y  = 64,
743                               width  = function(w,h) return w/2 end,
744                               height = function(w,h) return (h-64-128)/2 end
745                           },
746                           UpperRight = {
747                               id     = 5,
748                               pos_x  = function(w,h) return w/2 end,
749                               pos_y  = 64,
750                               width  = function(w,h) return w/2 end,
751                               height = function(w,h) return (h-64-128)/2 end
752                           },
753                           LowerLeft = {
754                               id     = 6,
755                               pos_x  = 0,
756                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
757                               width  = function(w,h) return w/2 end,
758                               height = function(w,h) return (h-64-128)/2 end
759                           },
760                           LowerRight = {
761                               id     = 7,
762                               pos_x  = function(w,h) return w/2 end,
763                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
764                               width  = function(w,h) return w/2 end,
765                               height = function(w,h) return (h-64-128)/2 end
766                           },
767                           SysApp = {
768                               id     = 8,
769                               pos_x  = 0,
770                               pos_y  = 64,
771                               width  = function(w,h) return w end,
772                               height = function(w,h) return h-64-128 end
773                           },
774                           ["SysApp.Left"] = {
775                               id     = 9,
776                               pos_x  = 0,
777                               pos_y  = 64,
778                               width  = function(w,h) return w/2-181 end,
779                               height = function(w,h) return h-64-128 end
780                           },
781                           ["SysApp.Right"] = {
782                               id     = 10,
783                               pos_x  = function(w,h) return w/2+181 end,
784                               pos_y  = 64,
785                               width  = function(w,h) return w/2-181 end,
786                               height = function(w,h) return h-64-128 end
787                           },
788                         }
789                },
790                { name  = "Mid",
791                  zone  = "driver",
792                  id    = 1
793                }
794
795   },
796   layers = { {      0, "Background"   , 1 },
797              {      1, "Application"  , 2 },
798              {      2, "Softkeyboard" , 4 },
799              {      3, "HomeScreen"   , 2 },
800              {      4, "ControlBar"   , 2 },
801              {      5, "InterruptApp" , 2 },
802              {      6, "OnScreen"     , 2 },
803              {    101, "Input"        , 3 },
804              {    102, "Cursor"       , 5 },
805              {    103, "Startup"      , 6 },
806              { 0x1000, "Background"   , 1 },
807              { 0x2000, "Normal"       , 2 },
808              { 0x3000, "Fullscreen"   , 2 },
809              { 0x4000, "InputPanel"   , 3 },
810              { 0xA000, "Touch"        , 4 },
811              { 0xB000, "Cursor"       , 5 },
812              { 0xC000, "Startup"      , 6 }
813   },
814
815
816   manager_update = function(self, oper)
817                        if verbose > 0 then
818                            print("### <== WINDOW MANAGER UPDATE:" ..
819                                  window_manager_operation_name(oper))
820                        end
821                        if oper == 1 then
822                            local umask = window_mask { raise   = true,
823                                                        visible = true,
824                                                        active  = true }
825                            local rmask = window_mask { active  = true }
826                            local req = m:JSON({
827                                        passthrough_update  = umask:tointeger(),
828                                        passthrough_request = rmask:tointeger()
829                            })
830                            self:manager_request(req)
831                        end
832                    end,
833
834   window_update = function(self, oper, win, mask)
835                       if verbose > 0 then
836                           print("### <== WINDOW UPDATE oper:" ..
837                                 window_operation_name(oper) ..
838                                 " mask: " .. tostring(mask))
839                           if verbose > 1 then
840                               print(win)
841                           end
842                       end
843
844                       local arg = m:JSON({ surface = win.surface,
845                                            winname = win.name,
846                       })
847                       local command = 0
848
849                       if oper == 1 then -- create
850                            local layertype = win.layertype
851                            if layertype and input_layer[layertype] then
852                                if verbose > 0 then
853                                    print("ignoring input panel creation")
854                                end
855                                return
856                            end
857                            command     = 0x10001
858                       elseif oper == 2 then -- destroy
859                            command     = 0x10002
860                       elseif oper == 3 then  -- namechange
861                            command     = 0x10009
862                       elseif oper == 4 or oper == 5 then --visible or configure
863                            command     = 0x10008
864                            arg.zone    = win.area
865                            arg.node    = win.node
866                            arg.layer   = win.layer
867                            arg.pos_x   = win.pos_x
868                            arg.pos_y   = win.pos_y
869                            arg.width   = win.width
870                            arg.height  = win.height
871                            arg.raise   = win.raise
872                            arg.visible = win.visible
873                            arg.active  = win.active
874                       elseif oper == 6 then -- active
875                            command     = 0x10006
876                            arg.active  = win.active
877                       else
878                            if verbose > 0 then
879                                print("### nothing to do")
880                            end
881                            return
882                       end
883
884                       local msg = m:JSON({ command = command,
885                                            appid   = win.appid,
886                                            pid     = win.pid,
887                                            arg     = arg
888                       })
889                       if verbose > 0 then
890                           print("### <== sending " ..
891                                 command_name(msg.command) ..
892                                 " window message to '" .. win.name .. "'")
893                           if verbose > 1 then
894                               print(msg)
895                           end
896                       end
897                       sc:send_message(homescreen, msg)
898
899                       if oper == 1 then -- create
900                           local i = input_layer[win.layertype]
901                           local p = self:application(win.appid)
902                           local s = p.privileges.screen
903
904                           if s == "system" then
905                               local a = animation({})
906                               local r = m:JSON({surface = win.surface,
907                                                 visible = 1,
908                                                 raise   = 1})
909                               self:window_request(r,a,0)
910                           else
911                               if i then
912                                   if verbose > 0 then
913                                       print("do not make resource for " ..
914                                             "input window")
915                                   end
916                               else
917                                   resclnt:resource_set_create("screen",
918                                                                "driver",
919                                                                win.appid,
920                                                                win.surface)
921                               end
922                           end
923                       elseif oper == 2 then -- destroy
924                           resclnt:resource_set_destroy("screen", win.surface)
925                       elseif oper == 6 then -- active
926                           if win.active then
927                               local i = input_layer[win.layertype]
928                               local p = self:application(win.appid)
929                               local s = p.privileges.screen
930                               local surface = win.surface
931                               if not i and s ~= "system" then
932                                  resclnt:resource_set_acquire("screen",surface)
933                                  resmgr:window_raise(win.appid, surface, 1)
934                               end
935                           end
936                       end
937                   end,
938
939   layer_update = function(self, oper, layer, mask)
940                       if verbose > 0 then
941                           print("### LAYER UPDATE:" ..
942                                 layer_operation_name(oper) ..
943                                 " mask: " .. tostring(mask))
944                           if verbose > 1 then
945                               print(layer)
946                           end
947                       end
948                       if oper == 3 then -- visible
949                          local command = 0x10022
950                          local msg = m:JSON({
951                                          command = command,
952                                          appid = "",
953                                          arg = m:JSON({layer = layer.id,
954                                                        visible = layer.visible
955                                          })
956                                 })
957                          if verbose > 0 then
958                             print("### <== sending "..command_name(command)..
959                                   " layer message")
960                             if verbose > 1 then
961                                 print(msg)
962                             end
963                          end
964                          sc:send_message(homescreen, msg)
965                       else
966                            if verbose > 0 then
967                                print("### nothing to do")
968                            end
969                       end
970                  end,
971
972   output_update = function(self, oper, out, mask)
973                       local idx = out.index
974                       if verbose > 0 then
975                           print("### OUTPUT UPDATE:" .. oper ..
976                                 " mask: "..tostring(mask))
977                       end
978                       print(out)
979                       local outdef = self.outputs[idx+1]
980                       if (oper == 1) then -- create
981                           if outdef then
982                               self:output_request(m:JSON({index = idx,
983                                                           id    = outdef.id,
984                                                           name  = outdef.name
985                                                           }))
986                           end
987                       elseif (oper == 5) then -- done
988                           local ads = outdef.areas
989                           local on = outdef.name
990                           if ads then
991                               for name,ad in pairs(ads) do
992                                   local can = wmgr:canonical_name(on.."."..name)
993                                   local a = m:JSON({name   = name,
994                                                     output = out.index})
995                                   for fld,val in pairs(ad) do
996                                       a[fld] = self:geometry(out.width,
997                                                              out.height,
998                                                              val)
999                                    end
1000                                    self:area_create(a)
1001                                    resmgr:area_create(area[can], outdef.zone)
1002                               end
1003                           end
1004                       end
1005                   end
1006 }
1007
1008
1009 imgr = input_manager {
1010   inputs = {{ name = "G27 Racing Wheel",
1011               id = 0,
1012               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1013                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1014                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1015                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1016              }}
1017   },
1018
1019   manager_update = function(self, oper)
1020                        if verbose > 0 then
1021                            print("### <== INPUT MANAGER UPDATE:" ..
1022                                  input_manager_operation_name(oper))
1023                        end
1024                    end,
1025
1026   input_update = function(self, oper, inp, mask)
1027                      if verbose > 0 then
1028                          print("### INPUT UPDATE:" .. 
1029                                 input_operation_name(oper) ..
1030                                 " mask: " .. tostring(mask))
1031                           if verbose > 1 then
1032                               print(inp)
1033                           end
1034                       end
1035                  end,
1036   code_update  = function(self, oper, code, mask)
1037                      if verbose > 0 then
1038                          print("### CODE UPDATE: mask: " .. tostring(mask))
1039                          if verbose > 1 then
1040                              print(code)
1041                          end
1042                      end
1043                      local msg = m:JSON({ command = 1,
1044                                           appid = "org.tizen.ico.homescreen",
1045                                           arg = m:JSON({ device = code.device,
1046                                                          time = code.time,
1047                                                          input = code.input,
1048                                                          code = code.id,
1049                                                          state = code.state
1050                                            })
1051                      })
1052                      if verbose > 0 then
1053                          print("### <== sending " ..
1054                                command_name(msg.command) ..
1055                                " input message")
1056                          if verbose > 1 then
1057                              print(msg)
1058                          end
1059                      end
1060                      sc:send_message(homescreen, msg)
1061                  end
1062 }
1063
1064 sc = m:get_system_controller()
1065
1066 -- resource sets
1067 sets = {}
1068
1069 -- user manager
1070 um = m:UserManager()
1071
1072 connected = false
1073 homescreen = ""
1074
1075 -- these shoud be before wmgr:connect() is called
1076 if verbose > 0 then
1077    print("====== creating applications ======")
1078 end
1079 application {
1080     appid          = "default",
1081     area           = "Center.Full",
1082     privileges     = { screen = "none", audio = "none" },
1083     resource_class = "player",
1084     screen_priority = 0
1085 }
1086
1087 application {
1088     appid           = "weston",
1089     area            = "Center.Full",
1090     privileges      = { screen = "system", audio = "none" },
1091     resource_class  = "implicit",
1092     screen_priority = 30
1093 }
1094
1095 application {
1096     appid           = "org.tizen.ico.homescreen",
1097     area            = "Center.Full",
1098     privileges      = { screen = "system", audio = "system" },
1099     resource_class  = "player",
1100     screen_priority = 20
1101 }
1102
1103 application {
1104     appid           = "org.tizen.ico.statusbar",
1105     area            = "Center.Status",
1106     privileges      = { screen = "system", audio = "none" },
1107     resource_class  = "player",
1108     screen_priority = 20
1109 }
1110
1111 application {
1112     appid           = "org.tizen.ico.login",
1113     area            = "Center.Full",
1114     privileges      = { screen = "system", audio = "system" },
1115     resource_class  = "player",
1116     screen_priority = 20
1117 }
1118
1119 application {
1120     appid           = "org.tizen.ico.camera_left",
1121     area            = "Center.SysApp.Left",
1122     privileges      = { screen = "system", audio = "none" },
1123     requisites      = { screen = "blinker_left", audio = "none" },
1124     resource_class  = "player",
1125     screen_priority = 30
1126 }
1127
1128
1129 application {
1130     appid           = "org.tizen.ico.camera_right",
1131     area            = "Center.SysApp.Right",
1132     privileges      = { screen = "system", audio = "none" },
1133     requisites      = { screen = "blinker_right", audio = "none" },
1134     resource_class  = "player",
1135     screen_priority = 30
1136 }
1137
1138
1139 if sc then
1140     sc.client_handler = function (self, cid, msg)
1141         local command = msg.command
1142         if verbose > 0 then
1143             print('### ==> client handler:')
1144             if verbose > 1 then
1145                 print(msg)
1146             end
1147         end
1148         if not connected then
1149             print('Setting homescreen='..msg.appid)
1150             homescreen = msg.appid
1151             print('Trying to connect to wayland...')
1152             connected = wmgr:connect()
1153         end
1154         if connected and command then
1155             if command == 1 then -- send_appid
1156                 local driving_mode = mdb.select.select_driving_mode.single_value
1157                 local night_mode = mdb.select.select_night_mode.single_value
1158
1159                 if not driving_mode then driving_mode = 0 end
1160                 if not night_mode then night_mode = 0 end
1161
1162                 local reply = m:JSON({ command = 0x60001,
1163                                        arg     = m:JSON({ stateid = 1,
1164                                                           state   = driving_mode})
1165                                      })
1166                  if verbose > 0 then
1167                      print("### <== sending " ..
1168                            command_name(command) .. " message")
1169                      if verbose > 1 then
1170                          print(reply)
1171                      end
1172                  end
1173                  sc:send_message(homescreen, reply)
1174
1175                  reply = m:JSON({ command = 0x60001,
1176                                   arg     = m:JSON({ stateid = 2,
1177                                                      state   = night_mode})
1178                                 })
1179                  if verbose > 0 then
1180                      print("### <== sending " ..
1181                            command_name(command) .. " message")
1182                      if verbose > 1 then
1183                          print(reply)
1184                      end
1185                  end
1186                  sc:send_message(homescreen, reply)
1187             end
1188         end
1189     end
1190
1191     sc.generic_handler = function (self, cid, msg)
1192         if verbose > 0 then
1193             print('### ==> generic handler:')
1194             if verbose > 1 then
1195                print(msg)
1196             end
1197         end
1198     end
1199
1200     sc.window_handler = function (self, cid, msg)
1201         if verbose > 0 then
1202             print('### ==> received ' ..
1203                    command_name(msg.command) .. ' message')
1204             if verbose > 1 then
1205                 print(tostring(msg))
1206             end
1207         end
1208
1209         local a = animation({})
1210         local nores = false
1211         if msg.command == 0x10003 then       -- ico SHOW command
1212             local raise_mask = 0x01000000
1213             local lower_mask = 0x02000000
1214             local nores_mask = 0x40000000
1215             local time_mask  = 0x00ffffff
1216             msg.arg.visible = 1
1217             if msg.arg then
1218                 local time = 200
1219                 if  msg.arg.anim_time then
1220                     local t = msg.arg.anim_time
1221                     time = m:AND(t, time_mask)
1222                     nores = m:AND(t, nores_mask)
1223                     if m:AND(t, raise_mask) then
1224                         msg.arg.raise = 1
1225                     elseif m:AND(t, lower_mask) then
1226                         msg.arg.raise = 0
1227                     end
1228                 end
1229                 if msg.arg.anim_name then
1230                     a.show = { msg.arg.anim_name, time }
1231                     print('time: ' .. tostring(a.show[2]))
1232                 end
1233             end
1234             if not nores then
1235                 local p = wmgr:application(msg.appid)
1236                 local s = p.privileges.screen
1237                 if s == "system" then
1238                     nores = true
1239                 end
1240             end
1241             if verbose > 2 then
1242                 print('### ==> SHOW')
1243                 print(tostring(msg.arg))
1244             end
1245             if nores then
1246                 wmgr:window_request(msg.arg, a, 0)
1247             else
1248                 local surface = msg.arg.surface
1249                 resclnt:resource_set_acquire("screen", surface)
1250                 resmgr:window_raise(msg.appid, surface, 1)
1251             end
1252         elseif msg.command == 0x10004 then   -- ico HIDE command
1253             local raise_mask = 0x01000000
1254             local lower_mask = 0x02000000
1255             local nores_mask = 0x40000000
1256             local time_mask  = 0x00ffffff
1257             msg.arg.visible = 0
1258             if msg.arg then
1259                 local time = 200
1260                 if msg.arg.anim_time then
1261                     local t = msg.arg.anim_time
1262                     time = m:AND(t, time_mask)
1263                     nores = m:AND(t, nores_mask)
1264                 end
1265                 if msg.arg.anim_name then
1266                     a.hide = { msg.arg.anim_name, time }
1267                     print('hide animation time: ' .. tostring(a.hide[2]))
1268                 end
1269             end
1270             if not nores then
1271                 local p = wmgr:application(msg.appid)
1272                 local s = p.privileges.screen
1273                 if s == "system" then
1274                     nores = true
1275                 end
1276             end
1277             if verbose > 2 then
1278                 print('### ==> HIDE REQUEST')
1279                 print(tostring(msg.arg))
1280             end
1281             if nores then
1282                 wmgr:window_request(msg.arg, a, 0)
1283             else
1284                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1285             end
1286         elseif msg.command == 0x10005 then   -- ico MOVE
1287             if verbose > 2 then
1288                 print('### ==> MOVE REQUEST')
1289                 print(tostring(msg.arg))
1290             end
1291             wmgr:window_request(msg.arg, a, 0)
1292             -- TODO: handle if area changed
1293         elseif msg.command == 0x10006 then   -- ico ACTIVE
1294             if not msg.arg.active then
1295                 msg.arg.active = 3 -- pointer + keyboard
1296             end
1297             if verbose > 2 then
1298                 print('### ==> ACTIVE REQUEST')
1299                 print(tostring(msg.arg))
1300             end
1301             wmgr:window_request(msg.arg, a, 0)
1302         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1303             if verbose > 2 then
1304                 print('### ==> CHANGE_LAYER REQUEST')
1305                 print(tostring(msg.arg))
1306             end
1307             --[[
1308             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1309                 print("do not change layer for other than cursor or touch")
1310                 return
1311             end
1312             --]]
1313             wmgr:window_request(msg.arg, a, 0)
1314         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1315             local framerate = msg.arg.framerate
1316             if not framerate or framerate < 0 then
1317                 framerate = 0
1318             end
1319             msg.arg.map = 1
1320             if verbose > 2 then
1321                 print('### ==> MAP_THUMB REQUEST')
1322                 print(msg.arg)
1323                 print('framerate: '..framerate)
1324             end
1325             wmgr:window_request(msg.arg, a, framerate)
1326         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1327             msg.arg.map = 0
1328             if verbose > 2 then
1329                 print('### ==> UNMAP_THUMB REQUEST')
1330                 print(msg.arg)
1331             end
1332             wmgr:window_request(msg.arg, a, 0)
1333         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1334             msg.arg.visible = 1
1335             if verbose > 2 then
1336                 print('### ==> SHOW_LAYER REQUEST')
1337                 print(msg.arg)
1338             end
1339             wmgr:layer_request(msg.arg)
1340         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1341             msg.arg.visible = 0
1342             if verbose > 2 then
1343                 print('### ==> HIDE_LAYER REQUEST')
1344                 print(msg.arg)
1345             end
1346             wmgr:layer_request(msg.arg)
1347         end
1348     end
1349
1350     sc.input_handler = function (self, cid, msg)
1351         if verbose > 0 then
1352             print('### ==> input handler: ' .. command_name(msg.command))
1353             if verbose > 1 then
1354                 print(msg)
1355             end
1356         end
1357         if msg.command == 0x20001 then -- add_input
1358             msg.arg.appid = msg.appid
1359             if verbose > 2 then
1360                 print('### ==> ADD_INPUT REQUEST')
1361                 print(tostring(msg.arg))
1362             end
1363             imgr:input_request(msg.arg)
1364         elseif msg.command == 0x20002 then -- del_input
1365             msg.arg.appid = ''
1366             if verbose > 2 then
1367                 print('### ==> DEL_INPUT REQUEST')
1368                 print(tostring(msg.arg))
1369             end
1370             imgr:input_request(msg.arg)
1371         elseif msg.command == 0x20003 then -- send_input
1372         end
1373     end
1374
1375     sc.user_handler = function (self, cid, msg)
1376         if verbose > 0 then
1377             print('### ==> user handler: ' .. command_name(msg.command))
1378             if verbose > 1 then
1379                 print(msg)
1380             end
1381         end
1382
1383         if not um then
1384             print("User Manager not initialized")
1385             return
1386         end
1387
1388         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1389             print("command CHANGE_USER")
1390             if not msg.arg then
1391                 print("invalid message")
1392                 return
1393             end
1394
1395             username = msg.arg.user
1396             passwd = msg.arg.pass
1397
1398             if not username then
1399                 username = ""
1400             end
1401
1402             if not passwd then
1403                 passwd = ""
1404             end
1405
1406             success = um:changeUser(username, passwd)
1407
1408             if not success then
1409                 reply = m.JSON({
1410                     appid = msg.appid,
1411                     command = cmd
1412                 })
1413                 if sc:send_message(msg.appid, reply) then
1414                     print('*** sent authentication failed message')
1415                 else
1416                     print('*** failed to send authentication failed message')
1417                 end
1418             end
1419
1420         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1421             print("command GET_USERLIST")
1422             if not msg.appid then
1423                 print("invalid message")
1424                 return
1425             end
1426
1427             users, currentUser = um:getUserList()
1428
1429             if not users then
1430                 print("failed to get user list")
1431                 return
1432             end
1433
1434             nUsers = 0
1435
1436             for i,v in pairs(users) do
1437                 nUsers = nUsers + 1
1438             end
1439
1440             if not currentUser then
1441                 currentUser = ""
1442             end
1443
1444             if verbose > 1 then
1445                 print("current user: " .. currentUser)
1446                 print("user list:")
1447                 for i,v in pairs(users) do
1448                     print(v)
1449                 end
1450             end
1451
1452             reply = m.JSON({
1453                 appid = msg.appid,
1454                 command = cmd,
1455                 arg = m.JSON({
1456                     user_num = nUsers,
1457                     user_list = users,
1458                     user_login = currentUser
1459                 })
1460             })
1461
1462             if verbose > 1 then
1463                 print("### <== GetUserList reply: " .. tostring(reply))
1464             end
1465
1466             if sc:send_message(msg.appid, reply) then
1467                 print('*** reply OK')
1468             else
1469                 print('*** reply FAILED')
1470             end
1471
1472         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1473             print("command GET_LASTINFO")
1474             if not msg.appid then
1475                 print("invalid message")
1476                 return
1477             end
1478
1479             lastInfo = um:getLastInfo(msg.appid)
1480
1481             if not lastInfo then
1482                 print("failed to get last info for app" .. msg.appid)
1483                 return
1484             end
1485
1486             reply = m.JSON({
1487                 appid = msg.appid,
1488                 command = cmd,
1489                 arg = m.JSON({
1490                     lastinfo = lastinfo
1491                 })
1492             })
1493
1494             if sc:send_message(msg.appid, reply) then
1495                 print('*** reply OK')
1496             else
1497                 print('*** reply FAILED')
1498             end
1499
1500         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1501             print("command SET_LASTINFO")
1502             if not msg.arg or not msg.appid then
1503                 print("invalid message")
1504                 return
1505             end
1506
1507             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1508         end
1509     end
1510
1511     sc.resource_handler = function (self, cid, msg)
1512         if verbose > 0 then
1513             print('### ==> resource handler: ' .. command_name(msg.command))
1514             if verbose > 1 then
1515                 print(msg)
1516             end
1517         end
1518
1519         createResourceSet = function (ctl, client, msg)
1520             cb = function(rset, data)
1521                 print("> resource callback")
1522
1523                 -- type is either basic (0) or interrupt (1)
1524                 requestType = 0
1525                 if msg.res.type then
1526                     requestType = msg.res.type
1527                 end
1528
1529                 if rset.acquired then
1530                     cmd = 0x00040001 -- acquire
1531                 else
1532                     cmd = 0x00040002 -- release
1533                 end
1534
1535                 reply = m.JSON({
1536                         appid = data.client,
1537                         command = cmd,
1538                         res = {
1539                             type = requestType
1540                         }
1541                     })
1542
1543                 if rset.resources.audio_playback then
1544                     reply.res.sound = {
1545                         zone = "driver",
1546                         name = msg.appid,
1547                         adjust = 0,
1548                         -- id = "0"
1549                     }
1550                 end
1551
1552                 if rset.resources.display then
1553                     reply.res.window = {
1554                         zone = "driver",
1555                         name = msg.appid,
1556                         -- id = "0"
1557                     }
1558                 end
1559
1560                 if rset.resources.input then
1561                     reply.res.input = {
1562                         name = msg.appid,
1563                         event = 0
1564                     }
1565                 end
1566                 print("sending message to client: " .. data.client)
1567
1568                 if sc:send_message(data.client, reply) then
1569                     print('*** reply OK')
1570                 else
1571                     print('*** reply FAILED')
1572                 end
1573             end
1574
1575             rset = m:ResourceSet({
1576                     application_class = "player",
1577                     zone = "driver", -- msg.zone ("full")
1578                     callback = cb
1579                 })
1580
1581             rset.data = {
1582                 cid = cid,
1583                 ctl = ctl
1584             }
1585
1586             if msg.res.sound then
1587                 rset:addResource({
1588                         resource_name = "audio_playback"
1589                     })
1590                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1591                 rset.resources.audio_playback.attributes.appid = msg.appid
1592                 print("sound name: " .. msg.res.sound.name)
1593                 print("sound zone:" .. msg.res.sound.zone)
1594                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1595                 if msg.res.sound.id then
1596                     print("sound id: " .. msg.res.sound.id)
1597                 end
1598             end
1599
1600             if msg.res.input then
1601                 rset:addResource({
1602                         resource_name = "input"
1603                     })
1604                 rset.resources.input.attributes.pid = tostring(msg.pid)
1605                 rset.resources.input.attributes.appid = msg.appid
1606                 print("input name: " .. msg.res.sound.name)
1607                 print("input event:" .. tostring(msg.res.input.event))
1608             end
1609
1610             if msg.res.window then
1611                 rset:addResource({
1612                         resource_name = "display"
1613                     })
1614                 rset.resources.display.attributes.pid = tostring(msg.pid)
1615                 rset.resources.display.attributes.appid = msg.appid
1616                 print("display name: " .. msg.res.display.name)
1617                 print("display zone:" .. msg.res.display.zone)
1618                 if msg.res.display.id then
1619                     print("display id: " .. msg.res.display.id)
1620                 end
1621             end
1622
1623             return rset
1624         end
1625
1626         -- parse the message
1627
1628         -- fields common to all messages:
1629         --      msg.command
1630         --      msg.appid
1631         --      msg.pid
1632
1633         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1634             print("command CREATE")
1635
1636             if not sets.cid then
1637                 sets.cid = createResourceSet(self, cid, msg)
1638             end
1639
1640         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1641             print("command DESTROY")
1642
1643             if sets.cid then
1644                 sets.cid:release()
1645             end
1646
1647             sets.cid = nil -- garbage collecting
1648
1649         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1650             print("command ACQUIRE")
1651
1652             if not sets.cid then
1653                 sets.cid = createResourceSet(self, cid, msg)
1654             end
1655
1656             sets.cid:acquire()
1657
1658         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1659             print("command RELEASE")
1660
1661             if sets.cid then
1662                 sets.cid:release()
1663             end
1664
1665         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1666             print("command DEPRIVE")
1667
1668         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1669             print("command WAITING")
1670
1671         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1672             print("command REVERT")
1673         end
1674     end
1675
1676     sc.inputdev_handler = function (self, cid, msg)
1677         if verbose > 0 then
1678             print('*** inputdev handler: ' .. command_name(msg.command))
1679             if verbose > 1 then
1680                 print(msg)
1681             end
1682         end
1683     end
1684 end