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